PageRenderTime 163ms CodeModel.GetById 28ms app.highlight 30ms RepoModel.GetById 86ms app.codeStats 0ms

/src/backend/iphone/ftk_iphone_view.m

http://ftk.googlecode.com/
Objective C | 191 lines | 158 code | 33 blank | 0 comment | 12 complexity | b4a6ad02dec72892ef749581e8a581b6 MD5 | raw file
  1
  2#import "ftk_typedef.h"
  3#import "ftk_key.h"
  4#import "ftk_allocator.h"
  5#import "ftk_iphone_view.h"
  6#import "ftk_iphone_quartz_view.h"
  7#import "ftk_iphone_gles_view.h"
  8
  9@implementation FtkView
 10
 11-(id)initWithFrame:(CGRect)frame
 12{
 13	if((self = [super initWithFrame:frame]) == nil)
 14	{
 15		return nil;
 16	}
 17
 18	self.userInteractionEnabled = YES;
 19
 20	text_field = [[[UITextField alloc] initWithFrame:CGRectZero] autorelease];
 21	text_field.delegate = self;
 22	text_field.text = @" ";
 23	text_field.autocapitalizationType = UITextAutocapitalizationTypeNone;
 24	text_field.autocorrectionType = UITextAutocorrectionTypeNo;
 25	text_field.enablesReturnKeyAutomatically = NO;
 26	text_field.keyboardAppearance = UIKeyboardAppearanceDefault;
 27	text_field.keyboardType = UIKeyboardTypeDefault;
 28	text_field.returnKeyType = UIReturnKeyDefault;
 29	text_field.secureTextEntry = NO;
 30	text_field.hidden = YES;
 31	[self addSubview: text_field];
 32
 33	return self;
 34}
 35
 36-(void)layoutSubviews
 37{
 38}
 39
 40-(void)dealloc
 41{
 42	[text_field release];
 43	[super dealloc];
 44}
 45
 46-(void)drawView:(FtkBitmap*)bitmap rect:(FtkRect*)rect bits:(void*)bits xoffset:(int)xoffset yoffset:(int)yoffset width:(int)width height:(int)height
 47{
 48}
 49
 50-(void)showKeyboard
 51{
 52	[text_field becomeFirstResponder];
 53}
 54
 55-(void)hideKeyboard
 56{
 57	[text_field resignFirstResponder];
 58}
 59
 60-(BOOL)textField:(UITextField*)_textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string
 61{
 62	if([string length] == 0)
 63	{
 64		ev.type = FTK_EVT_KEY_DOWN;
 65		ev.u.key.code = FTK_KEY_BACKSPACE;
 66		ftk_wnd_manager_queue_event_auto_rotate(ftk_default_wnd_manager(), &ev);
 67
 68		ev.type = FTK_EVT_KEY_UP;
 69		ev.u.key.code = FTK_KEY_BACKSPACE;
 70		ftk_wnd_manager_queue_event_auto_rotate(ftk_default_wnd_manager(), &ev);
 71	}
 72	else
 73	{
 74		const char* utf8 = [string UTF8String];
 75		size_t len = strlen(utf8);
 76		char* buf = FTK_ALLOC(len + 1);
 77		if (buf == NULL)
 78		{
 79			return NO;
 80		}
 81		memcpy(buf, utf8, len);
 82		buf[len] = '\0';
 83
 84		ftk_logd("%s:%d range:%d-%d %s\n", __FILE__, __LINE__, range.location, range.length, buf);
 85
 86		ev.u.extra = buf;
 87		ev.type = FTK_EVT_OS_IM_COMMIT;
 88		ftk_wnd_manager_queue_event_auto_rotate(ftk_default_wnd_manager(), &ev);
 89	}
 90
 91	return YES;//NO; /* don't allow the edit! (keep placeholder text there) */
 92}
 93
 94-(BOOL)textFieldShouldReturn:(UITextField*)_textField
 95{
 96	[self hideKeyboard];
 97	return YES;
 98}
 99
100-(void)touchesBegan:(NSSet*)touches withEvent:(UIEvent*)event
101{
102	NSEnumerator* enumerator = [touches objectEnumerator];
103	UITouch* touch = (UITouch*)[enumerator nextObject];
104
105	if(touch != NULL)
106	{
107		CGPoint locationInView = [touch locationInView:self];
108		ev.type = FTK_EVT_MOUSE_DOWN;
109		ev.u.mouse.x = locationInView.x;
110		ev.u.mouse.y = locationInView.y;
111		ftk_wnd_manager_queue_event_auto_rotate(ftk_default_wnd_manager(), &ev);
112	}
113}
114
115-(void)touchesEnded:(NSSet*)touches withEvent:(UIEvent*)event
116{
117	NSEnumerator* enumerator = [touches objectEnumerator];
118	UITouch* touch = (UITouch*)[enumerator nextObject];
119
120	if(touch != NULL)
121	{
122		CGPoint locationInView = [touch locationInView:self];
123		ev.type = FTK_EVT_MOUSE_UP;
124		ev.u.mouse.x = locationInView.x;
125		ev.u.mouse.y = locationInView.y;
126		ftk_wnd_manager_queue_event_auto_rotate(ftk_default_wnd_manager(), &ev);
127	}
128}
129
130-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent*)event
131{
132	[self touchesEnded:touches withEvent:event];
133}
134
135-(void)touchesMoved:(NSSet*)touches withEvent:(UIEvent*)event
136{
137	NSEnumerator* enumerator = [touches objectEnumerator];
138	UITouch* touch = (UITouch*)[enumerator nextObject];
139
140	if(touch != NULL)
141	{
142		CGPoint locationInView = [touch locationInView:self];
143		ev.u.mouse.x = locationInView.x;
144		ev.u.mouse.y = locationInView.y;
145		ev.type = FTK_EVT_MOUSE_MOVE;
146		ftk_wnd_manager_queue_event_auto_rotate(ftk_default_wnd_manager(), &ev);
147	}
148}
149
150@end
151
152UIWindow* window;
153FtkView* view;
154
155void ftk_iphone_view_create_win(void)
156{
157	CGRect rect = [[UIScreen mainScreen] bounds];
158
159	[UIApplication sharedApplication].statusBarHidden = YES;
160
161	window = [[UIWindow alloc] initWithFrame:rect];
162#if 1
163	view = [FtkGlesView alloc];
164#else
165	view = [FtkQuartzView alloc];
166#endif
167	view = [view initWithFrame:rect];
168	[window addSubview:view];
169	[window makeKeyAndVisible];
170}
171
172void ftk_iphone_view_destroy_win(void)
173{
174	[window release];
175	[view release];
176}
177
178void ftk_iphone_view_draw(FtkBitmap* bitmap, FtkRect* rect, void* bits, int xoffset, int yoffset, int width, int height)
179{
180	[view drawView:bitmap rect:rect bits:bits xoffset:xoffset yoffset:yoffset width:width height:height];
181}
182
183void ftk_iphone_view_show_keyboard(void)
184{
185	[view showKeyboard];
186}
187
188void ftk_iphone_view_hide_keyboard(void)
189{
190	[view hideKeyboard];
191}