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