PageRenderTime 69ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/ExtLibs/wxWidgets/src/osx/iphone/window.mm

https://bitbucket.org/lennonchan/cafu
Objective C++ | 806 lines | 526 code | 125 blank | 155 comment | 62 complexity | bbf310c2d7a1f8db3a9a681d5de0733e MD5 | raw file
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: src/osx/iphone/window.mm
  3. // Purpose: widgets (non tlw) for iphone
  4. // Author: Stefan Csomor
  5. // Modified by:
  6. // Created: 2008-06-20
  7. // RCS-ID: $Id$
  8. // Copyright: (c) Stefan Csomor
  9. // Licence: wxWindows licence
  10. /////////////////////////////////////////////////////////////////////////////
  11. #include "wx/wxprec.h"
  12. #include "wx/osx/private.h"
  13. #ifndef WX_PRECOMP
  14. #include "wx/nonownedwnd.h"
  15. #include "wx/frame.h"
  16. #include "wx/event.h"
  17. #include "wx/log.h"
  18. #endif
  19. #include "wx/textctrl.h"
  20. #include <objc/runtime.h>
  21. WXWidget wxWidgetImpl::FindFocus()
  22. {
  23. UIView* focusedView = nil;
  24. UIWindow* keyWindow = [[UIApplication sharedApplication] keyWindow];
  25. if ( keyWindow != nil )
  26. {
  27. /*
  28. NSResponder* responder = [keyWindow firstResponder];
  29. if ( [responder isKindOfClass:[NSTextView class]] &&
  30. [keyWindow fieldEditor:NO forObject:nil] != nil )
  31. {
  32. focusedView = [(NSTextView*)responder delegate];
  33. }
  34. else
  35. {
  36. if ( [responder isKindOfClass:[NSView class]] )
  37. focusedView = (NSView*) responder;
  38. }
  39. */
  40. }
  41. return focusedView;
  42. }
  43. CGRect wxOSXGetFrameForControl( wxWindowMac* window , const wxPoint& pos , const wxSize &size , bool adjustForOrigin )
  44. {
  45. int x, y, w, h ;
  46. window->MacGetBoundsForControl( pos , size , x , y, w, h , adjustForOrigin ) ;
  47. wxRect bounds(x,y,w,h);
  48. UIView* sv = (window->GetParent()->GetHandle() );
  49. return wxToNSRect( sv, bounds );
  50. }
  51. @interface wxUIView(PossibleMethods)
  52. - (void)setTitle:(NSString *)title forState:(UIControlState)state;
  53. - (void)drawRect: (CGRect) rect;
  54. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
  55. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
  56. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
  57. - (void)handleTouchEvent:(NSSet *)touches withEvent:(UIEvent *)event;
  58. - (BOOL) becomeFirstResponder;
  59. - (BOOL) resignFirstResponder;
  60. @end
  61. //
  62. //
  63. //
  64. void SetupMouseEvent( wxMouseEvent &wxevent , NSSet* touches, UIEvent * nsEvent )
  65. {
  66. UInt32 modifiers = 0 ;
  67. UITouch *touch = [touches anyObject];
  68. // these parameters are not given for all events
  69. UInt32 button = 0; // no secondary button
  70. UInt32 clickCount = [touch tapCount];
  71. UInt32 mouseChord = 0; // TODO does this exist for cocoa
  72. // will be overridden
  73. wxevent.m_x = 0;
  74. wxevent.m_y = 0;
  75. wxevent.m_shiftDown = 0;
  76. wxevent.m_controlDown = 0;
  77. wxevent.m_altDown = 0;
  78. wxevent.m_metaDown = 0;
  79. wxevent.m_clickCount = clickCount;
  80. wxevent.SetTimestamp( [touch timestamp] ) ;
  81. /*
  82. // a control click is interpreted as a right click
  83. bool thisButtonIsFakeRight = false ;
  84. if ( button == kEventMouseButtonPrimary && (modifiers & controlKey) )
  85. {
  86. button = kEventMouseButtonSecondary ;
  87. thisButtonIsFakeRight = true ;
  88. }
  89. // otherwise we report double clicks by connecting a left click with a ctrl-left click
  90. if ( clickCount > 1 && button != g_lastButton )
  91. clickCount = 1 ;
  92. // we must make sure that our synthetic 'right' button corresponds in
  93. // mouse down, moved and mouse up, and does not deliver a right down and left up
  94. if ( cEvent.GetKind() == kEventMouseDown )
  95. {
  96. g_lastButton = button ;
  97. g_lastButtonWasFakeRight = thisButtonIsFakeRight ;
  98. }
  99. if ( button == 0 )
  100. {
  101. g_lastButton = 0 ;
  102. g_lastButtonWasFakeRight = false ;
  103. }
  104. else if ( g_lastButton == kEventMouseButtonSecondary && g_lastButtonWasFakeRight )
  105. button = g_lastButton ;
  106. // Adjust the chord mask to remove the primary button and add the
  107. // secondary button. It is possible that the secondary button is
  108. // already pressed, e.g. on a mouse connected to a laptop, but this
  109. // possibility is ignored here:
  110. if( thisButtonIsFakeRight && ( mouseChord & 1U ) )
  111. mouseChord = ((mouseChord & ~1U) | 2U);
  112. if(mouseChord & 1U)
  113. wxevent.m_leftDown = true ;
  114. if(mouseChord & 2U)
  115. wxevent.m_rightDown = true ;
  116. if(mouseChord & 4U)
  117. wxevent.m_middleDown = true ;
  118. */
  119. // translate into wx types
  120. int eventType = [touch phase];
  121. switch (eventType)
  122. {
  123. case UITouchPhaseBegan :
  124. switch ( button )
  125. {
  126. case 0 :
  127. wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ;
  128. break ;
  129. default:
  130. break ;
  131. }
  132. break ;
  133. case UITouchPhaseEnded :
  134. switch ( button )
  135. {
  136. case 0 :
  137. wxevent.SetEventType( wxEVT_LEFT_UP ) ;
  138. break ;
  139. default:
  140. break ;
  141. }
  142. break ;
  143. case UITouchPhaseMoved :
  144. wxevent.SetEventType( wxEVT_MOTION ) ;
  145. break;
  146. default :
  147. break ;
  148. }
  149. }
  150. @implementation wxUIView
  151. + (void)initialize
  152. {
  153. static BOOL initialized = NO;
  154. if (!initialized)
  155. {
  156. initialized = YES;
  157. wxOSXIPhoneClassAddWXMethods( self );
  158. }
  159. }
  160. @end // wxUIView
  161. /*
  162. - (void)drawRect: (CGRect) rect
  163. {
  164. wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
  165. if ( impl )
  166. {
  167. CGContextRef context = (CGContextRef) UIGraphicsGetCurrentContext();
  168. CGContextSaveGState( context );
  169. // draw background
  170. CGContextSetFillColorWithColor( context, impl->GetWXPeer()->GetBackgroundColour().GetCGColor());
  171. CGContextFillRect(context, rect );
  172. impl->GetWXPeer()->MacSetCGContextRef( context );
  173. impl->GetWXPeer()->GetUpdateRegion() =
  174. wxRegion(rect.origin.x,rect.origin.y,rect.size.width,rect.size.height) ;
  175. wxPaintEvent event;
  176. event.SetTimestamp(0); // todo
  177. event.SetEventObject(impl->GetWXPeer());
  178. impl->GetWXPeer()->HandleWindowEvent(event);
  179. CGContextRestoreGState( context );
  180. }
  181. }
  182. - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
  183. {
  184. [self handleTouchEvent:touches withEvent:event];
  185. }
  186. - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
  187. {
  188. [self handleTouchEvent:touches withEvent:event];
  189. }
  190. - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
  191. {
  192. [self handleTouchEvent:touches withEvent:event];
  193. }
  194. -(void)handleTouchEvent:(NSSet *)touches withEvent:(UIEvent *)event
  195. {
  196. wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
  197. CGPoint clickLocation;
  198. UITouch *touch = [touches anyObject];
  199. clickLocation = [touch locationInView:self];
  200. wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
  201. SetupMouseEvent( wxevent , touches, event ) ;
  202. wxevent.m_x = clickLocation.x;
  203. wxevent.m_y = clickLocation.y;
  204. wxevent.SetEventObject( impl->GetWXPeer() ) ;
  205. wxevent.SetId( impl->GetWXPeer()->GetId() ) ;
  206. impl->GetWXPeer()->HandleWindowEvent(wxevent);
  207. }
  208. */
  209. void wxOSX_touchEvent(UIView* self, SEL _cmd, NSSet* touches, UIEvent *event )
  210. {
  211. wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
  212. if (impl == NULL)
  213. return;
  214. impl->touchEvent(touches, event, self, _cmd);
  215. }
  216. BOOL wxOSX_becomeFirstResponder(UIView* self, SEL _cmd)
  217. {
  218. wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
  219. if (impl == NULL)
  220. return NO;
  221. return impl->becomeFirstResponder(self, _cmd);
  222. }
  223. BOOL wxOSX_resignFirstResponder(UIView* self, SEL _cmd)
  224. {
  225. wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
  226. if (impl == NULL)
  227. return NO;
  228. return impl->resignFirstResponder(self, _cmd);
  229. }
  230. void wxOSX_drawRect(UIView* self, SEL _cmd, CGRect rect)
  231. {
  232. wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
  233. if (impl == NULL)
  234. return;
  235. return impl->drawRect(&rect, self, _cmd);
  236. }
  237. void wxOSXIPhoneClassAddWXMethods(Class c)
  238. {
  239. class_addMethod(c, @selector(touchesBegan:withEvent:), (IMP) wxOSX_touchEvent, "v@:@@");
  240. class_addMethod(c, @selector(touchesMoved:withEvent:), (IMP) wxOSX_touchEvent, "v@:@@");
  241. class_addMethod(c, @selector(touchesEnded:withEvent:), (IMP) wxOSX_touchEvent, "v@:@@");
  242. class_addMethod(c, @selector(becomeFirstResponder), (IMP) wxOSX_becomeFirstResponder, "c@:" );
  243. class_addMethod(c, @selector(resignFirstResponder), (IMP) wxOSX_resignFirstResponder, "c@:" );
  244. class_addMethod(c, @selector(drawRect:), (IMP) wxOSX_drawRect, "v@:{_CGRect={_CGPoint=ff}{_CGSize=ff}}" );
  245. }
  246. //
  247. // UIControl extensions
  248. //
  249. @interface UIControl (wxUIControlActionSupport)
  250. - (void) WX_touchUpInsideAction:(id)sender event:(UIEvent*)event;
  251. - (void) WX_valueChangedAction:(id)sender event:(UIEvent*)event;
  252. @end
  253. @implementation UIControl (wxUIControlActionSupport)
  254. - (void) WX_touchUpInsideAction:(id)sender event:(UIEvent*)event
  255. {
  256. wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
  257. if (impl != NULL)
  258. impl->controlAction(sender, UIControlEventTouchUpInside, event);
  259. }
  260. - (void) WX_valueChangedAction:(id)sender event:(UIEvent*)event
  261. {
  262. wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
  263. if (impl != NULL)
  264. impl->controlAction(sender, UIControlEventValueChanged, event);
  265. }
  266. @end
  267. IMPLEMENT_DYNAMIC_CLASS( wxWidgetIPhoneImpl , wxWidgetImpl )
  268. wxWidgetIPhoneImpl::wxWidgetIPhoneImpl( wxWindowMac* peer , WXWidget w, bool isRootControl, bool isUserPane ) :
  269. wxWidgetImpl( peer, isRootControl, isUserPane ), m_osxView(w)
  270. {
  271. }
  272. wxWidgetIPhoneImpl::wxWidgetIPhoneImpl()
  273. {
  274. }
  275. void wxWidgetIPhoneImpl::Init()
  276. {
  277. m_osxView = NULL;
  278. }
  279. wxWidgetIPhoneImpl::~wxWidgetIPhoneImpl()
  280. {
  281. RemoveAssociations( this );
  282. if ( !IsRootControl() )
  283. {
  284. UIView *sv = [m_osxView superview];
  285. if ( sv != nil )
  286. [m_osxView removeFromSuperview];
  287. }
  288. [m_osxView release];
  289. }
  290. bool wxWidgetIPhoneImpl::IsVisible() const
  291. {
  292. UIView* view = m_osxView;
  293. while ( view != nil && [view isHidden] == NO )
  294. {
  295. view = [view superview];
  296. }
  297. return view == nil;
  298. }
  299. void wxWidgetIPhoneImpl::SetVisibility( bool visible )
  300. {
  301. [m_osxView setHidden:(visible ? NO:YES)];
  302. }
  303. void wxWidgetIPhoneImpl::Raise()
  304. {
  305. [[m_osxView superview] bringSubviewToFront:m_osxView];
  306. }
  307. void wxWidgetIPhoneImpl::Lower()
  308. {
  309. [[m_osxView superview] sendSubviewToBack:m_osxView];
  310. }
  311. void wxWidgetIPhoneImpl::ScrollRect( const wxRect *rect, int dx, int dy )
  312. {
  313. SetNeedsDisplay() ;
  314. }
  315. void wxWidgetIPhoneImpl::Move(int x, int y, int width, int height)
  316. {
  317. CGRect r = CGRectMake( x, y, width, height) ;
  318. [m_osxView setFrame:r];
  319. }
  320. void wxWidgetIPhoneImpl::GetPosition( int &x, int &y ) const
  321. {
  322. CGRect r = [m_osxView frame];
  323. x = r.origin.x;
  324. y = r.origin.y;
  325. }
  326. void wxWidgetIPhoneImpl::GetSize( int &width, int &height ) const
  327. {
  328. CGRect rect = [m_osxView frame];
  329. width = rect.size.width;
  330. height = rect.size.height;
  331. }
  332. void wxWidgetIPhoneImpl::GetContentArea( int&left, int &top, int &width, int &height ) const
  333. {
  334. left = top = 0;
  335. CGRect rect = [m_osxView bounds];
  336. width = rect.size.width;
  337. height = rect.size.height;
  338. }
  339. void wxWidgetIPhoneImpl::SetNeedsDisplay( const wxRect* where )
  340. {
  341. if ( where )
  342. {
  343. CGRect r = CGRectMake( where->x, where->y, where->width, where->height) ;
  344. [m_osxView setNeedsDisplayInRect:r];
  345. }
  346. else
  347. [m_osxView setNeedsDisplay];
  348. }
  349. bool wxWidgetIPhoneImpl::GetNeedsDisplay() const
  350. {
  351. return false;
  352. // return [m_osxView needsDisplay];
  353. }
  354. bool wxWidgetIPhoneImpl::CanFocus() const
  355. {
  356. return [m_osxView canBecomeFirstResponder] == YES;
  357. // ? return [m_osxView isUserInteractionEnabled] == YES;
  358. }
  359. bool wxWidgetIPhoneImpl::HasFocus() const
  360. {
  361. return [m_osxView isFirstResponder] == YES;
  362. }
  363. bool wxWidgetIPhoneImpl::SetFocus()
  364. {
  365. // [m_osxView makeKeyWindow] ;
  366. // TODO
  367. return false;
  368. }
  369. void wxWidgetIPhoneImpl::RemoveFromParent()
  370. {
  371. [m_osxView removeFromSuperview];
  372. }
  373. void wxWidgetIPhoneImpl::Embed( wxWidgetImpl *parent )
  374. {
  375. UIView* container = parent->GetWXWidget() ;
  376. wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
  377. [container addSubview:m_osxView];
  378. }
  379. void wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to )
  380. {
  381. CGPoint p = CGPointMake( pt->x , pt->y );
  382. p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ];
  383. pt->x = (int)p.x;
  384. pt->y = (int)p.y;
  385. }
  386. void wxWidgetIPhoneImpl::SetBackgroundColour( const wxColour &col )
  387. {
  388. m_osxView.backgroundColor = [[UIColor alloc] initWithCGColor:col.GetCGColor()];
  389. }
  390. bool wxWidgetIPhoneImpl::SetBackgroundStyle(wxBackgroundStyle style)
  391. {
  392. if ( style == wxBG_STYLE_PAINT )
  393. [m_osxView setOpaque: YES ];
  394. else
  395. {
  396. [m_osxView setOpaque: NO ];
  397. m_osxView.backgroundColor = [UIColor clearColor];
  398. }
  399. return true;
  400. }
  401. void wxWidgetIPhoneImpl::SetLabel(const wxString& title, wxFontEncoding encoding)
  402. {
  403. if ( [m_osxView respondsToSelector:@selector(setTitle:forState:) ] )
  404. {
  405. wxCFStringRef cf( title , encoding );
  406. [m_osxView setTitle:cf.AsNSString() forState:UIControlStateNormal ];
  407. }
  408. else if ( [m_osxView respondsToSelector:@selector(setStringValue:) ] )
  409. {
  410. wxCFStringRef cf( title , encoding );
  411. [m_osxView setStringValue:cf.AsNSString()];
  412. }
  413. }
  414. void wxWidgetIPhoneImpl::SetCursor( const wxCursor & cursor )
  415. {
  416. }
  417. void wxWidgetIPhoneImpl::CaptureMouse()
  418. {
  419. }
  420. void wxWidgetIPhoneImpl::ReleaseMouse()
  421. {
  422. }
  423. wxInt32 wxWidgetIPhoneImpl::GetValue() const
  424. {
  425. }
  426. void wxWidgetIPhoneImpl::SetValue( wxInt32 v )
  427. {
  428. }
  429. void wxWidgetIPhoneImpl::SetBitmap( const wxBitmap& bitmap )
  430. {
  431. }
  432. wxBitmap wxWidgetIPhoneImpl::GetBitmap() const
  433. {
  434. wxBitmap bmp;
  435. return bmp;
  436. }
  437. void wxWidgetIPhoneImpl::SetBitmapPosition( wxDirection dir )
  438. {
  439. }
  440. void wxWidgetIPhoneImpl::SetupTabs( const wxNotebook &notebook )
  441. {
  442. }
  443. void wxWidgetIPhoneImpl::GetBestRect( wxRect *r ) const
  444. {
  445. r->x = r->y = r->width = r->height = 0;
  446. if ( [m_osxView respondsToSelector:@selector(sizeToFit)] )
  447. {
  448. CGRect former = [m_osxView frame];
  449. [m_osxView sizeToFit];
  450. CGRect best = [m_osxView frame];
  451. [m_osxView setFrame:former];
  452. r->width = best.size.width;
  453. r->height = best.size.height;
  454. }
  455. }
  456. bool wxWidgetIPhoneImpl::IsEnabled() const
  457. {
  458. }
  459. void wxWidgetIPhoneImpl::Enable( bool enable )
  460. {
  461. }
  462. void wxWidgetIPhoneImpl::SetMinimum( wxInt32 v )
  463. {
  464. }
  465. void wxWidgetIPhoneImpl::SetMaximum( wxInt32 v )
  466. {
  467. }
  468. wxInt32 wxWidgetIPhoneImpl::GetMinimum() const
  469. {
  470. }
  471. wxInt32 wxWidgetIPhoneImpl::GetMaximum() const
  472. {
  473. }
  474. void wxWidgetIPhoneImpl::PulseGauge()
  475. {
  476. }
  477. void wxWidgetIPhoneImpl::SetScrollThumb( wxInt32 value, wxInt32 thumbSize )
  478. {
  479. }
  480. void wxWidgetIPhoneImpl::SetControlSize( wxWindowVariant variant )
  481. {
  482. }
  483. float wxWidgetIPhoneImpl::GetContentScaleFactor() const
  484. {
  485. if ( [m_osxView respondsToSelector:@selector(contentScaleFactor) ])
  486. return [m_osxView contentScaleFactor];
  487. else
  488. return 1.0;
  489. }
  490. void wxWidgetIPhoneImpl::SetFont( const wxFont & font , const wxColour& foreground , long windowStyle, bool ignoreBlack )
  491. {
  492. }
  493. void wxWidgetIPhoneImpl::InstallEventHandler( WXWidget control )
  494. {
  495. WXWidget c = control ? control : (WXWidget) m_osxView;
  496. wxWidgetImpl::Associate( c, this ) ;
  497. if ([c isKindOfClass:[UIControl class] ])
  498. {
  499. UIControl* cc = (UIControl*) c;
  500. [cc addTarget:cc action:@selector(WX_touchUpInsideAction:event:) forControlEvents:UIControlEventTouchUpInside];
  501. [cc addTarget:cc action:@selector(WX_valueChangedAction:event:) forControlEvents:UIControlEventValueChanged];
  502. }
  503. }
  504. void wxWidgetIPhoneImpl::DoNotifyFocusEvent(bool receivedFocus, wxWidgetImpl* otherWindow)
  505. {
  506. wxWindow* thisWindow = GetWXPeer();
  507. if ( thisWindow->MacGetTopLevelWindow() && NeedsFocusRect() )
  508. {
  509. thisWindow->MacInvalidateBorders();
  510. }
  511. if ( receivedFocus )
  512. {
  513. wxLogTrace(wxT("Focus"), wxT("focus set(%p)"), static_cast<void*>(thisWindow));
  514. wxChildFocusEvent eventFocus((wxWindow*)thisWindow);
  515. thisWindow->HandleWindowEvent(eventFocus);
  516. #if wxUSE_CARET
  517. if ( thisWindow->GetCaret() )
  518. thisWindow->GetCaret()->OnSetFocus();
  519. #endif
  520. wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId());
  521. event.SetEventObject(thisWindow);
  522. if (otherWindow)
  523. event.SetWindow(otherWindow->GetWXPeer());
  524. thisWindow->HandleWindowEvent(event) ;
  525. }
  526. else // !receivedFocuss
  527. {
  528. #if wxUSE_CARET
  529. if ( thisWindow->GetCaret() )
  530. thisWindow->GetCaret()->OnKillFocus();
  531. #endif
  532. wxLogTrace(wxT("Focus"), wxT("focus lost(%p)"), static_cast<void*>(thisWindow));
  533. wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId());
  534. event.SetEventObject(thisWindow);
  535. if (otherWindow)
  536. event.SetWindow(otherWindow->GetWXPeer());
  537. thisWindow->HandleWindowEvent(event) ;
  538. }
  539. }
  540. typedef void (*wxOSX_DrawRectHandlerPtr)(UIView* self, SEL _cmd, CGRect rect);
  541. typedef BOOL (*wxOSX_FocusHandlerPtr)(UIView* self, SEL _cmd);
  542. bool wxWidgetIPhoneImpl::becomeFirstResponder(WXWidget slf, void *_cmd)
  543. {
  544. wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
  545. // get the current focus before running becomeFirstResponder
  546. UIView* otherView = FindFocus();
  547. wxWidgetImpl* otherWindow = FindFromWXWidget(otherView);
  548. BOOL r = superimpl(slf, (SEL)_cmd);
  549. if ( r )
  550. {
  551. DoNotifyFocusEvent( true, otherWindow );
  552. }
  553. return r;
  554. }
  555. bool wxWidgetIPhoneImpl::resignFirstResponder(WXWidget slf, void *_cmd)
  556. {
  557. wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
  558. BOOL r = superimpl(slf, (SEL)_cmd);
  559. // get the current focus after running resignFirstResponder
  560. UIView* otherView = FindFocus();
  561. wxWidgetImpl* otherWindow = FindFromWXWidget(otherView);
  562. // NSTextViews have an editor as true responder, therefore the might get the
  563. // resign notification if their editor takes over, don't trigger any event hen
  564. if ( r && otherWindow != this)
  565. {
  566. DoNotifyFocusEvent( false, otherWindow );
  567. }
  568. return r;
  569. }
  570. void wxWidgetIPhoneImpl::drawRect(CGRect* rect, WXWidget slf, void *WXUNUSED(_cmd))
  571. {
  572. CGContextRef context = (CGContextRef) UIGraphicsGetCurrentContext();
  573. CGContextSaveGState( context );
  574. // draw background
  575. /*
  576. CGContextSetFillColorWithColor( context, GetWXPeer()->GetBackgroundColour().GetCGColor());
  577. CGContextFillRect(context, *rect );
  578. */
  579. GetWXPeer()->MacSetCGContextRef( context );
  580. GetWXPeer()->GetUpdateRegion() =
  581. wxRegion(rect->origin.x,rect->origin.y,rect->size.width,rect->size.height) ;
  582. wxRegion updateRgn( wxFromNSRect( slf, *rect ) );
  583. wxWindow* wxpeer = GetWXPeer();
  584. wxpeer->GetUpdateRegion() = updateRgn;
  585. wxpeer->MacSetCGContextRef( context );
  586. bool handled = wxpeer->MacDoRedraw( 0 );
  587. CGContextRestoreGState( context );
  588. CGContextSaveGState( context );
  589. if ( !handled )
  590. {
  591. // call super
  592. SEL _cmd = @selector(drawRect:);
  593. wxOSX_DrawRectHandlerPtr superimpl = (wxOSX_DrawRectHandlerPtr) [[slf superclass] instanceMethodForSelector:_cmd];
  594. if ( superimpl != wxOSX_drawRect )
  595. {
  596. superimpl(slf, _cmd, *rect);
  597. CGContextRestoreGState( context );
  598. CGContextSaveGState( context );
  599. }
  600. }
  601. wxpeer->MacPaintChildrenBorders();
  602. wxpeer->MacSetCGContextRef( NULL );
  603. CGContextRestoreGState( context );
  604. }
  605. void wxWidgetIPhoneImpl::touchEvent(NSSet* touches, UIEvent *event, WXWidget slf, void *WXUNUSED(_cmd))
  606. {
  607. bool inRecursion = false;
  608. if ( inRecursion )
  609. return;
  610. UITouch *touch = [touches anyObject];
  611. CGPoint clickLocation;
  612. if ( [touch view] != slf && IsRootControl() )
  613. {
  614. NSLog(@"self is %@ and touch view is %@",slf,[touch view]);
  615. inRecursion = true;
  616. inRecursion = false;
  617. }
  618. else
  619. {
  620. clickLocation = [touch locationInView:slf];
  621. wxPoint pt = wxFromNSPoint( m_osxView, clickLocation );
  622. wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
  623. SetupMouseEvent( wxevent , touches, event ) ;
  624. wxevent.m_x = pt.x;
  625. wxevent.m_y = pt.y;
  626. wxevent.SetEventObject( GetWXPeer() ) ;
  627. //?wxevent.SetId( GetWXPeer()->GetId() ) ;
  628. GetWXPeer()->HandleWindowEvent(wxevent);
  629. }
  630. }
  631. void wxWidgetIPhoneImpl::controlAction(void* sender, wxUint32 controlEvent, WX_UIEvent rawEvent)
  632. {
  633. if ( controlEvent == UIControlEventTouchUpInside )
  634. GetWXPeer()->OSXHandleClicked(0);
  635. }
  636. void wxWidgetIPhoneImpl::controlTextDidChange()
  637. {
  638. wxTextCtrl* wxpeer = wxDynamicCast((wxWindow*)GetWXPeer(),wxTextCtrl);
  639. if ( wxpeer )
  640. {
  641. wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
  642. event.SetEventObject( wxpeer );
  643. event.SetString( wxpeer->GetValue() );
  644. wxpeer->HandleWindowEvent( event );
  645. }
  646. }
  647. //
  648. // Factory methods
  649. //
  650. wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* WXUNUSED(parent),
  651. wxWindowID WXUNUSED(id), const wxPoint& pos, const wxSize& size,
  652. long WXUNUSED(style), long WXUNUSED(extraStyle))
  653. {
  654. UIView* sv = (wxpeer->GetParent()->GetHandle() );
  655. CGRect r = CGRectMake( pos.x, pos.y, size.x, size.y) ;
  656. // Rect bounds = wxMacGetBoundsForControl( wxpeer, pos , size ) ;
  657. wxUIView* v = [[wxUIView alloc] initWithFrame:r];
  658. sv.clipsToBounds = YES;
  659. sv.contentMode = UIViewContentModeRedraw;
  660. sv.clearsContextBeforeDrawing = NO;
  661. wxWidgetIPhoneImpl* c = new wxWidgetIPhoneImpl( wxpeer, v, false, true );
  662. return c;
  663. }