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