1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/iphone/window.mm
3 // Purpose: widgets (non tlw) for iphone
4 // Author: Stefan Csomor
7 // RCS-ID: $Id: window.mm 48805 2007-09-19 14:52:25Z SC $
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include "wx/osx/private.h"
17 #include "wx/nonownedwnd.h"
23 #include <objc/objc-runtime.h>
26 WXWidget wxWidgetImpl::FindFocus()
28 UIView* focusedView = nil;
29 UIWindow* keyWindow = [[UIApplication sharedApplication] keyWindow];
30 if ( keyWindow != nil )
33 NSResponder* responder = [keyWindow firstResponder];
34 if ( [responder isKindOfClass:[NSTextView class]] &&
35 [keyWindow fieldEditor:NO forObject:nil] != nil )
37 focusedView = [(NSTextView*)responder delegate];
41 if ( [responder isKindOfClass:[NSView class]] )
42 focusedView = (NSView*) responder;
49 CGRect wxOSXGetFrameForControl( wxWindowMac* window , const wxPoint& pos , const wxSize &size , bool adjustForOrigin )
53 window->MacGetBoundsForControl( pos , size , x , y, w, h , adjustForOrigin ) ;
54 wxRect bounds(x,y,w,h);
55 UIView* sv = (window->GetParent()->GetHandle() );
57 return wxToNSRect( sv, bounds );
60 @interface wxUIView : UIView
66 @interface wxUIView(PossibleMethods)
67 - (void)setTitle:(NSString *)title forState:(UIControlState)state;
69 - (void)drawRect: (CGRect) rect;
71 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
72 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
73 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
74 - (void)handleTouchEvent:(NSSet *)touches withEvent:(UIEvent *)event;
76 - (BOOL) becomeFirstResponder;
77 - (BOOL) resignFirstResponder;
80 @interface wxUIContentView : wxUIView
86 @interface wxUIContentViewController : UIViewController
96 void SetupMouseEvent( wxMouseEvent &wxevent , NSSet* touches, UIEvent * nsEvent )
98 UInt32 modifiers = 0 ;
99 UITouch *touch = [touches anyObject];
101 // these parameters are not given for all events
102 UInt32 button = 0; // no secondary button
103 UInt32 clickCount = [touch tapCount];
104 UInt32 mouseChord = 0; // TODO does this exist for cocoa
106 // will be overridden
109 wxevent.m_shiftDown = 0;
110 wxevent.m_controlDown = 0;
111 wxevent.m_altDown = 0;
112 wxevent.m_metaDown = 0;
113 wxevent.m_clickCount = clickCount;
114 wxevent.SetTimestamp( [touch timestamp] ) ;
116 // a control click is interpreted as a right click
117 bool thisButtonIsFakeRight = false ;
118 if ( button == kEventMouseButtonPrimary && (modifiers & controlKey) )
120 button = kEventMouseButtonSecondary ;
121 thisButtonIsFakeRight = true ;
124 // otherwise we report double clicks by connecting a left click with a ctrl-left click
125 if ( clickCount > 1 && button != g_lastButton )
127 // we must make sure that our synthetic 'right' button corresponds in
128 // mouse down, moved and mouse up, and does not deliver a right down and left up
130 if ( cEvent.GetKind() == kEventMouseDown )
132 g_lastButton = button ;
133 g_lastButtonWasFakeRight = thisButtonIsFakeRight ;
139 g_lastButtonWasFakeRight = false ;
141 else if ( g_lastButton == kEventMouseButtonSecondary && g_lastButtonWasFakeRight )
142 button = g_lastButton ;
144 // Adjust the chord mask to remove the primary button and add the
145 // secondary button. It is possible that the secondary button is
146 // already pressed, e.g. on a mouse connected to a laptop, but this
147 // possibility is ignored here:
148 if( thisButtonIsFakeRight && ( mouseChord & 1U ) )
149 mouseChord = ((mouseChord & ~1U) | 2U);
152 wxevent.m_leftDown = true ;
154 wxevent.m_rightDown = true ;
156 wxevent.m_middleDown = true ;
159 // translate into wx types
160 int eventType = [touch phase];
163 case UITouchPhaseBegan :
167 wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ;
175 case UITouchPhaseEnded :
179 wxevent.SetEventType( wxEVT_LEFT_UP ) ;
187 case UITouchPhaseMoved :
188 wxevent.SetEventType( wxEVT_MOTION ) ;
195 @implementation wxUIView
199 static BOOL initialized = NO;
203 wxOSXIPhoneClassAddWXMethods( self );
210 - (void)drawRect: (CGRect) rect
212 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
215 CGContextRef context = (CGContextRef) UIGraphicsGetCurrentContext();
216 CGContextSaveGState( context );
219 CGContextSetFillColorWithColor( context, impl->GetWXPeer()->GetBackgroundColour().GetCGColor());
220 CGContextFillRect(context, rect );
222 impl->GetWXPeer()->MacSetCGContextRef( context );
224 impl->GetWXPeer()->GetUpdateRegion() =
225 wxRegion(rect.origin.x,rect.origin.y,rect.size.width,rect.size.height) ;
228 event.SetTimestamp(0); // todo
229 event.SetEventObject(impl->GetWXPeer());
230 impl->GetWXPeer()->HandleWindowEvent(event);
232 CGContextRestoreGState( context );
237 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
239 [self handleTouchEvent:touches withEvent:event];
242 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
244 [self handleTouchEvent:touches withEvent:event];
247 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
249 [self handleTouchEvent:touches withEvent:event];
252 -(void)handleTouchEvent:(NSSet *)touches withEvent:(UIEvent *)event
254 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
255 CGPoint clickLocation;
256 UITouch *touch = [touches anyObject];
257 clickLocation = [touch locationInView:self];
259 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
260 SetupMouseEvent( wxevent , touches, event ) ;
261 wxevent.m_x = clickLocation.x;
262 wxevent.m_y = clickLocation.y;
263 wxevent.SetEventObject( impl->GetWXPeer() ) ;
264 wxevent.SetId( impl->GetWXPeer()->GetId() ) ;
265 impl->GetWXPeer()->HandleWindowEvent(wxevent);
270 void wxOSX_touchEvent(UIView* self, SEL _cmd, NSSet* touches, UIEvent *event )
272 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
276 impl->touchEvent(touches, event, self, _cmd);
279 BOOL wxOSX_becomeFirstResponder(UIView* self, SEL _cmd)
281 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
285 return impl->becomeFirstResponder(self, _cmd);
288 BOOL wxOSX_resignFirstResponder(UIView* self, SEL _cmd)
290 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
294 return impl->resignFirstResponder(self, _cmd);
297 void wxOSX_drawRect(UIView* self, SEL _cmd, CGRect rect)
299 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
303 return impl->drawRect(&rect, self, _cmd);
307 void wxOSXIPhoneClassAddWXMethods(Class c)
309 class_addMethod(c, @selector(touchesBegan:withEvent:), (IMP) wxOSX_touchEvent, "v@:@@");
310 class_addMethod(c, @selector(touchesMoved:withEvent:), (IMP) wxOSX_touchEvent, "v@:@@");
311 class_addMethod(c, @selector(touchesEnded:withEvent:), (IMP) wxOSX_touchEvent, "v@:@@");
312 class_addMethod(c, @selector(becomeFirstResponder), (IMP) wxOSX_becomeFirstResponder, "c@:" );
313 class_addMethod(c, @selector(resignFirstResponder), (IMP) wxOSX_resignFirstResponder, "c@:" );
314 class_addMethod(c, @selector(drawRect:), (IMP) wxOSX_drawRect, "v@:{_CGRect={_CGPoint=ff}{_CGSize=ff}}" );
317 @implementation wxUIContentView
321 @implementation wxUIContentViewController
323 - (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation
328 - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
330 CGRect fr = [self.view frame];
331 // CGRect cv = [[self.view superview] frame];
332 // CGRect bounds = CGRectMake( 0,0,fr.size.width, fr.size.height);
333 // [[self.view superview] setFrame: fr ];
334 // [self.view setFrame: bounds];
335 // [self.view setNeedsDisplayInRect:bounds];
341 IMPLEMENT_DYNAMIC_CLASS( wxWidgetIPhoneImpl , wxWidgetImpl )
343 wxWidgetIPhoneImpl::wxWidgetIPhoneImpl( wxWindowMac* peer , WXWidget w, bool isRootControl ) :
344 wxWidgetImpl( peer, isRootControl ), m_osxView(w)
348 wxWidgetIPhoneImpl::wxWidgetIPhoneImpl()
352 void wxWidgetIPhoneImpl::Init()
357 wxWidgetIPhoneImpl::~wxWidgetIPhoneImpl()
359 RemoveAssociations( this );
361 if ( !IsRootControl() )
363 UIView *sv = [m_osxView superview];
365 [m_osxView removeFromSuperview];
370 bool wxWidgetIPhoneImpl::IsVisible() const
372 // TODO reflect Superviews state
373 return [m_osxView isHidden] == NO;
376 void wxWidgetIPhoneImpl::SetVisibility( bool visible )
378 [m_osxView setHidden:(visible ? NO:YES)];
381 void wxWidgetIPhoneImpl::Raise()
383 [[m_osxView superview] bringSubviewToFront:m_osxView];
386 void wxWidgetIPhoneImpl::Lower()
388 [[m_osxView superview] sendSubviewToBack:m_osxView];
391 void wxWidgetIPhoneImpl::ScrollRect( const wxRect *rect, int dx, int dy )
396 void wxWidgetIPhoneImpl::Move(int x, int y, int width, int height)
398 CGRect r = CGRectMake( x, y, width, height) ;
399 [m_osxView setFrame:r];
402 void wxWidgetIPhoneImpl::GetPosition( int &x, int &y ) const
404 CGRect r = [m_osxView frame];
409 void wxWidgetIPhoneImpl::GetSize( int &width, int &height ) const
411 CGRect rect = [m_osxView frame];
412 width = rect.size.width;
413 height = rect.size.height;
416 void wxWidgetIPhoneImpl::GetContentArea( int&left, int &top, int &width, int &height ) const
419 GetSize( width, height );
422 void wxWidgetIPhoneImpl::SetNeedsDisplay( const wxRect* where )
426 CGRect r = CGRectMake( where->x, where->y, where->width, where->height) ;
427 [m_osxView setNeedsDisplayInRect:r];
430 [m_osxView setNeedsDisplay];
433 bool wxWidgetIPhoneImpl::GetNeedsDisplay() const
436 // return [m_osxView needsDisplay];
439 bool wxWidgetIPhoneImpl::CanFocus() const
441 return [m_osxView canBecomeFirstResponder] == YES;
442 // ? return [m_osxView isUserInteractionEnabled] == YES;
445 bool wxWidgetIPhoneImpl::HasFocus() const
447 return [m_osxView isFirstResponder] == YES;
450 bool wxWidgetIPhoneImpl::SetFocus()
452 // [m_osxView makeKeyWindow] ;
458 void wxWidgetIPhoneImpl::RemoveFromParent()
460 [m_osxView removeFromSuperview];
463 void wxWidgetIPhoneImpl::Embed( wxWidgetImpl *parent )
465 UIView* container = parent->GetWXWidget() ;
466 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
467 [container addSubview:m_osxView];
470 void wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to )
472 CGPoint p = CGPointMake( pt->x , pt->y );
473 p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ];
478 void wxWidgetIPhoneImpl::SetBackgroundColour( const wxColour &col )
480 // m_osxView.backgroundColor = [[UIColor alloc] initWithCGColor:col.GetCGColor()];
483 void wxWidgetIPhoneImpl::SetLabel(const wxString& title, wxFontEncoding encoding)
485 if ( [m_osxView respondsToSelector:@selector(setTitle:forState:) ] )
487 wxCFStringRef cf( title , encoding );
488 [m_osxView setTitle:cf.AsNSString() forState:UIControlStateNormal ];
490 else if ( [m_osxView respondsToSelector:@selector(setStringValue:) ] )
492 wxCFStringRef cf( title , encoding );
493 [m_osxView setStringValue:cf.AsNSString()];
498 void wxWidgetIPhoneImpl::SetCursor( const wxCursor & cursor )
502 void wxWidgetIPhoneImpl::CaptureMouse()
506 void wxWidgetIPhoneImpl::ReleaseMouse()
510 wxInt32 wxWidgetIPhoneImpl::GetValue() const
514 void wxWidgetIPhoneImpl::SetValue( wxInt32 v )
518 void wxWidgetIPhoneImpl::SetBitmap( const wxBitmap& bitmap )
522 wxBitmap wxWidgetIPhoneImpl::GetBitmap() const
528 void wxWidgetIPhoneImpl::SetBitmapPosition( wxDirection dir )
532 void wxWidgetIPhoneImpl::SetupTabs( const wxNotebook ¬ebook )
536 void wxWidgetIPhoneImpl::GetBestRect( wxRect *r ) const
538 r->x = r->y = r->width = r->height = 0;
540 if ( [m_osxView respondsToSelector:@selector(sizeToFit)] )
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;
551 bool wxWidgetIPhoneImpl::IsEnabled() const
555 void wxWidgetIPhoneImpl::Enable( bool enable )
559 void wxWidgetIPhoneImpl::SetMinimum( wxInt32 v )
563 void wxWidgetIPhoneImpl::SetMaximum( wxInt32 v )
567 wxInt32 wxWidgetIPhoneImpl::GetMinimum() const
571 wxInt32 wxWidgetIPhoneImpl::GetMaximum() const
575 void wxWidgetIPhoneImpl::PulseGauge()
579 void wxWidgetIPhoneImpl::SetScrollThumb( wxInt32 value, wxInt32 thumbSize )
583 void wxWidgetIPhoneImpl::SetControlSize( wxWindowVariant variant )
587 void wxWidgetIPhoneImpl::SetFont( const wxFont & font , const wxColour& foreground , long windowStyle, bool ignoreBlack )
591 void wxWidgetIPhoneImpl::InstallEventHandler( WXWidget control )
593 WXWidget c = control ? control : (WXWidget) m_osxView;
594 wxWidgetImpl::Associate( c, this ) ;
596 if ([c isKindOfClass:[UIControl class] ])
598 UIControl* cc = (UIControl*) c;
600 [cc addTarget:self action:@selector(touchUpInsideAction:event:) forControlEvents:UIControlEventTouchUpInside];
605 void wxWidgetIPhoneImpl::DoNotifyFocusEvent(bool receivedFocus, wxWidgetImpl* otherWindow)
607 wxWindow* thisWindow = GetWXPeer();
608 if ( thisWindow->MacGetTopLevelWindow() && NeedsFocusRect() )
610 thisWindow->MacInvalidateBorders();
615 wxLogTrace(_T("Focus"), _T("focus set(%p)"), static_cast<void*>(thisWindow));
616 wxChildFocusEvent eventFocus((wxWindow*)thisWindow);
617 thisWindow->HandleWindowEvent(eventFocus);
620 if ( thisWindow->GetCaret() )
621 thisWindow->GetCaret()->OnSetFocus();
624 wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId());
625 event.SetEventObject(thisWindow);
627 event.SetWindow(otherWindow->GetWXPeer());
628 thisWindow->HandleWindowEvent(event) ;
630 else // !receivedFocuss
633 if ( thisWindow->GetCaret() )
634 thisWindow->GetCaret()->OnKillFocus();
637 wxLogTrace(_T("Focus"), _T("focus lost(%p)"), static_cast<void*>(thisWindow));
639 wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId());
640 event.SetEventObject(thisWindow);
642 event.SetWindow(otherWindow->GetWXPeer());
643 thisWindow->HandleWindowEvent(event) ;
647 typedef void (*wxOSX_DrawRectHandlerPtr)(UIView* self, SEL _cmd, CGRect rect);
648 typedef BOOL (*wxOSX_FocusHandlerPtr)(UIView* self, SEL _cmd);
650 bool wxWidgetIPhoneImpl::becomeFirstResponder(WXWidget slf, void *_cmd)
652 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
653 // get the current focus before running becomeFirstResponder
654 UIView* otherView = FindFocus();
655 wxWidgetImpl* otherWindow = FindFromWXWidget(otherView);
656 BOOL r = superimpl(slf, (SEL)_cmd);
659 DoNotifyFocusEvent( true, otherWindow );
664 bool wxWidgetIPhoneImpl::resignFirstResponder(WXWidget slf, void *_cmd)
666 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
667 BOOL r = superimpl(slf, (SEL)_cmd);
668 // get the current focus after running resignFirstResponder
669 UIView* otherView = FindFocus();
670 wxWidgetImpl* otherWindow = FindFromWXWidget(otherView);
671 // NSTextViews have an editor as true responder, therefore the might get the
672 // resign notification if their editor takes over, don't trigger any event hen
673 if ( r && otherWindow != this)
675 DoNotifyFocusEvent( false, otherWindow );
680 void wxWidgetIPhoneImpl::drawRect(CGRect* rect, WXWidget slf, void *WXUNUSED(_cmd))
682 CGContextRef context = (CGContextRef) UIGraphicsGetCurrentContext();
683 CGContextSaveGState( context );
686 CGContextSetFillColorWithColor( context, GetWXPeer()->GetBackgroundColour().GetCGColor());
687 CGContextFillRect(context, *rect );
689 GetWXPeer()->MacSetCGContextRef( context );
691 GetWXPeer()->GetUpdateRegion() =
692 wxRegion(rect->origin.x,rect->origin.y,rect->size.width,rect->size.height) ;
694 wxRegion updateRgn( wxFromNSRect( slf, *rect ) );
696 wxWindow* wxpeer = GetWXPeer();
697 wxpeer->GetUpdateRegion() = updateRgn;
698 wxpeer->MacSetCGContextRef( context );
700 bool handled = wxpeer->MacDoRedraw( 0 );
702 CGContextRestoreGState( context );
704 CGContextSaveGState( context );
708 SEL _cmd = @selector(drawRect:);
709 wxOSX_DrawRectHandlerPtr superimpl = (wxOSX_DrawRectHandlerPtr) [[slf superclass] instanceMethodForSelector:_cmd];
710 superimpl(slf, _cmd, *rect);
711 CGContextRestoreGState( context );
712 CGContextSaveGState( context );
714 wxpeer->MacPaintChildrenBorders();
715 wxpeer->MacSetCGContextRef( NULL );
717 CGContextRestoreGState( context );
720 void wxWidgetIPhoneImpl::touchEvent(NSSet* touches, UIEvent *event, WXWidget slf, void *WXUNUSED(_cmd))
722 CGPoint clickLocation;
723 UITouch *touch = [touches anyObject];
724 clickLocation = [touch locationInView:slf];
725 wxPoint pt = wxFromNSPoint( m_osxView, clickLocation );
727 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
728 SetupMouseEvent( wxevent , touches, event ) ;
731 wxevent.SetEventObject( GetWXPeer() ) ;
732 //?wxevent.SetId( GetWXPeer()->GetId() ) ;
734 GetWXPeer()->HandleWindowEvent(wxevent);
737 void wxWidgetIPhoneImpl::touchUpInsideAction(void* sender, WX_UIEvent evt, WXWidget slf, void* _cmd)
745 wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* WXUNUSED(parent),
746 wxWindowID WXUNUSED(id), const wxPoint& pos, const wxSize& size,
747 long WXUNUSED(style), long WXUNUSED(extraStyle))
749 UIView* sv = (wxpeer->GetParent()->GetHandle() );
751 CGRect r = CGRectMake( pos.x, pos.y, size.x, size.y) ;
752 // Rect bounds = wxMacGetBoundsForControl( wxpeer, pos , size ) ;
753 wxUIView* v = [[wxUIView alloc] initWithFrame:r];
754 sv.clipsToBounds = YES;
755 sv.contentMode = UIViewContentModeRedraw;
756 sv.clearsContextBeforeDrawing = NO;
757 wxWidgetIPhoneImpl* c = new wxWidgetIPhoneImpl( wxpeer, v );
761 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
763 UIWindow* toplevelwindow = now->GetWXWindow();
765 wxUIContentView* contentview = [[wxUIContentView alloc] initWithFrame:[toplevelwindow bounds]];
766 contentview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
767 wxUIContentViewController* controller = [[wxUIContentViewController alloc] init];
768 controller.view = contentview;
770 UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
771 // left orientation not yet implemented !
772 if (orientation == UIInterfaceOrientationLandscapeRight )
774 CGAffineTransform transform = v.transform;
776 // Use the status bar frame to determine the center point of the window's content area.
777 CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
778 CGRect bounds = CGRectMake(0, 0, statusBarFrame.size.height, statusBarFrame.origin.x);
779 CGPoint center = CGPointMake(bounds.size.height / 2.0, bounds.size.width / 2.0);
781 // Set the center point of the view to the center point of the window's content area.
784 // Rotate the view 90 degrees around its new center point.
785 transform = CGAffineTransformRotate(transform, ( M_PI / 2.0));
786 v.transform = transform;
789 wxWidgetIPhoneImpl* impl = new wxWidgetIPhoneImpl( now, contentview, true );
790 impl->InstallEventHandler();
791 [toplevelwindow addSubview:contentview];