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