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/runtime.h>
25 WXWidget wxWidgetImpl::FindFocus()
27 UIView* focusedView = nil;
28 UIWindow* keyWindow = [[UIApplication sharedApplication] keyWindow];
29 if ( keyWindow != nil )
32 NSResponder* responder = [keyWindow firstResponder];
33 if ( [responder isKindOfClass:[NSTextView class]] &&
34 [keyWindow fieldEditor:NO forObject:nil] != nil )
36 focusedView = [(NSTextView*)responder delegate];
40 if ( [responder isKindOfClass:[NSView class]] )
41 focusedView = (NSView*) responder;
48 CGRect wxOSXGetFrameForControl( wxWindowMac* window , const wxPoint& pos , const wxSize &size , bool adjustForOrigin )
52 window->MacGetBoundsForControl( pos , size , x , y, w, h , adjustForOrigin ) ;
53 wxRect bounds(x,y,w,h);
54 UIView* sv = (window->GetParent()->GetHandle() );
56 return wxToNSRect( sv, bounds );
59 @interface wxUIView : UIView
65 @interface wxUIView(PossibleMethods)
66 - (void)setTitle:(NSString *)title forState:(UIControlState)state;
68 - (void)drawRect: (CGRect) rect;
70 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
71 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
72 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
73 - (void)handleTouchEvent:(NSSet *)touches withEvent:(UIEvent *)event;
75 - (BOOL) becomeFirstResponder;
76 - (BOOL) resignFirstResponder;
79 @interface wxUIContentView : wxUIView
85 @interface wxUIContentViewController : UIViewController
95 void SetupMouseEvent( wxMouseEvent &wxevent , NSSet* touches, UIEvent * nsEvent )
97 UInt32 modifiers = 0 ;
98 UITouch *touch = [touches anyObject];
100 // these parameters are not given for all events
101 UInt32 button = 0; // no secondary button
102 UInt32 clickCount = [touch tapCount];
103 UInt32 mouseChord = 0; // TODO does this exist for cocoa
105 // will be overridden
108 wxevent.m_shiftDown = 0;
109 wxevent.m_controlDown = 0;
110 wxevent.m_altDown = 0;
111 wxevent.m_metaDown = 0;
112 wxevent.m_clickCount = clickCount;
113 wxevent.SetTimestamp( [touch timestamp] ) ;
115 // a control click is interpreted as a right click
116 bool thisButtonIsFakeRight = false ;
117 if ( button == kEventMouseButtonPrimary && (modifiers & controlKey) )
119 button = kEventMouseButtonSecondary ;
120 thisButtonIsFakeRight = true ;
123 // otherwise we report double clicks by connecting a left click with a ctrl-left click
124 if ( clickCount > 1 && button != g_lastButton )
126 // we must make sure that our synthetic 'right' button corresponds in
127 // mouse down, moved and mouse up, and does not deliver a right down and left up
129 if ( cEvent.GetKind() == kEventMouseDown )
131 g_lastButton = button ;
132 g_lastButtonWasFakeRight = thisButtonIsFakeRight ;
138 g_lastButtonWasFakeRight = false ;
140 else if ( g_lastButton == kEventMouseButtonSecondary && g_lastButtonWasFakeRight )
141 button = g_lastButton ;
143 // Adjust the chord mask to remove the primary button and add the
144 // secondary button. It is possible that the secondary button is
145 // already pressed, e.g. on a mouse connected to a laptop, but this
146 // possibility is ignored here:
147 if( thisButtonIsFakeRight && ( mouseChord & 1U ) )
148 mouseChord = ((mouseChord & ~1U) | 2U);
151 wxevent.m_leftDown = true ;
153 wxevent.m_rightDown = true ;
155 wxevent.m_middleDown = true ;
158 // translate into wx types
159 int eventType = [touch phase];
162 case UITouchPhaseBegan :
166 wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ;
174 case UITouchPhaseEnded :
178 wxevent.SetEventType( wxEVT_LEFT_UP ) ;
186 case UITouchPhaseMoved :
187 wxevent.SetEventType( wxEVT_MOTION ) ;
194 @implementation wxUIView
198 static BOOL initialized = NO;
202 wxOSXIPhoneClassAddWXMethods( self );
209 - (void)drawRect: (CGRect) rect
211 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
214 CGContextRef context = (CGContextRef) UIGraphicsGetCurrentContext();
215 CGContextSaveGState( context );
218 CGContextSetFillColorWithColor( context, impl->GetWXPeer()->GetBackgroundColour().GetCGColor());
219 CGContextFillRect(context, rect );
221 impl->GetWXPeer()->MacSetCGContextRef( context );
223 impl->GetWXPeer()->GetUpdateRegion() =
224 wxRegion(rect.origin.x,rect.origin.y,rect.size.width,rect.size.height) ;
227 event.SetTimestamp(0); // todo
228 event.SetEventObject(impl->GetWXPeer());
229 impl->GetWXPeer()->HandleWindowEvent(event);
231 CGContextRestoreGState( context );
236 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
238 [self handleTouchEvent:touches withEvent:event];
241 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
243 [self handleTouchEvent:touches withEvent:event];
246 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
248 [self handleTouchEvent:touches withEvent:event];
251 -(void)handleTouchEvent:(NSSet *)touches withEvent:(UIEvent *)event
253 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
254 CGPoint clickLocation;
255 UITouch *touch = [touches anyObject];
256 clickLocation = [touch locationInView:self];
258 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
259 SetupMouseEvent( wxevent , touches, event ) ;
260 wxevent.m_x = clickLocation.x;
261 wxevent.m_y = clickLocation.y;
262 wxevent.SetEventObject( impl->GetWXPeer() ) ;
263 wxevent.SetId( impl->GetWXPeer()->GetId() ) ;
264 impl->GetWXPeer()->HandleWindowEvent(wxevent);
269 void wxOSX_touchEvent(UIView* self, SEL _cmd, NSSet* touches, UIEvent *event )
271 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
275 impl->touchEvent(touches, event, self, _cmd);
278 BOOL wxOSX_becomeFirstResponder(UIView* self, SEL _cmd)
280 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
284 return impl->becomeFirstResponder(self, _cmd);
287 BOOL wxOSX_resignFirstResponder(UIView* self, SEL _cmd)
289 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
293 return impl->resignFirstResponder(self, _cmd);
296 void wxOSX_drawRect(UIView* self, SEL _cmd, CGRect rect)
298 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
302 return impl->drawRect(&rect, self, _cmd);
306 void wxOSXIPhoneClassAddWXMethods(Class c)
308 class_addMethod(c, @selector(touchesBegan:withEvent:), (IMP) wxOSX_touchEvent, "v@:@@");
309 class_addMethod(c, @selector(touchesMoved:withEvent:), (IMP) wxOSX_touchEvent, "v@:@@");
310 class_addMethod(c, @selector(touchesEnded:withEvent:), (IMP) wxOSX_touchEvent, "v@:@@");
311 class_addMethod(c, @selector(becomeFirstResponder), (IMP) wxOSX_becomeFirstResponder, "c@:" );
312 class_addMethod(c, @selector(resignFirstResponder), (IMP) wxOSX_resignFirstResponder, "c@:" );
313 class_addMethod(c, @selector(drawRect:), (IMP) wxOSX_drawRect, "v@:{_CGRect={_CGPoint=ff}{_CGSize=ff}}" );
316 @implementation wxUIContentView
320 @implementation wxUIContentViewController
322 - (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation
324 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
325 wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (impl->GetWXPeer());
327 // TODO: determine NO or YES based on min size requirements (whether it fits on the new orientation)
332 - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
334 CGRect fr = [self.view frame];
335 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( [self view] );
336 wxNonOwnedWindow* now = dynamic_cast<wxNonOwnedWindow*> (impl->GetWXPeer());
338 now->HandleResized(0);
344 IMPLEMENT_DYNAMIC_CLASS( wxWidgetIPhoneImpl , wxWidgetImpl )
346 wxWidgetIPhoneImpl::wxWidgetIPhoneImpl( wxWindowMac* peer , WXWidget w, bool isRootControl ) :
347 wxWidgetImpl( peer, isRootControl ), m_osxView(w)
351 wxWidgetIPhoneImpl::wxWidgetIPhoneImpl()
355 void wxWidgetIPhoneImpl::Init()
360 wxWidgetIPhoneImpl::~wxWidgetIPhoneImpl()
362 RemoveAssociations( this );
364 if ( !IsRootControl() )
366 UIView *sv = [m_osxView superview];
368 [m_osxView removeFromSuperview];
373 bool wxWidgetIPhoneImpl::IsVisible() const
375 UIView* view = m_osxView;
376 while ( view != nil && [view isHidden] == NO )
378 view = [view superview];
383 void wxWidgetIPhoneImpl::SetVisibility( bool visible )
385 [m_osxView setHidden:(visible ? NO:YES)];
388 void wxWidgetIPhoneImpl::Raise()
390 [[m_osxView superview] bringSubviewToFront:m_osxView];
393 void wxWidgetIPhoneImpl::Lower()
395 [[m_osxView superview] sendSubviewToBack:m_osxView];
398 void wxWidgetIPhoneImpl::ScrollRect( const wxRect *rect, int dx, int dy )
403 void wxWidgetIPhoneImpl::Move(int x, int y, int width, int height)
405 CGRect r = CGRectMake( x, y, width, height) ;
406 [m_osxView setFrame:r];
409 void wxWidgetIPhoneImpl::GetPosition( int &x, int &y ) const
411 CGRect r = [m_osxView frame];
416 void wxWidgetIPhoneImpl::GetSize( int &width, int &height ) const
418 CGRect rect = [m_osxView frame];
419 width = rect.size.width;
420 height = rect.size.height;
423 void wxWidgetIPhoneImpl::GetContentArea( int&left, int &top, int &width, int &height ) const
426 CGRect rect = [m_osxView bounds];
427 width = rect.size.width;
428 height = rect.size.height;
431 void wxWidgetIPhoneImpl::SetNeedsDisplay( const wxRect* where )
435 CGRect r = CGRectMake( where->x, where->y, where->width, where->height) ;
436 [m_osxView setNeedsDisplayInRect:r];
439 [m_osxView setNeedsDisplay];
442 bool wxWidgetIPhoneImpl::GetNeedsDisplay() const
445 // return [m_osxView needsDisplay];
448 bool wxWidgetIPhoneImpl::CanFocus() const
450 return [m_osxView canBecomeFirstResponder] == YES;
451 // ? return [m_osxView isUserInteractionEnabled] == YES;
454 bool wxWidgetIPhoneImpl::HasFocus() const
456 return [m_osxView isFirstResponder] == YES;
459 bool wxWidgetIPhoneImpl::SetFocus()
461 // [m_osxView makeKeyWindow] ;
467 void wxWidgetIPhoneImpl::RemoveFromParent()
469 [m_osxView removeFromSuperview];
472 void wxWidgetIPhoneImpl::Embed( wxWidgetImpl *parent )
474 UIView* container = parent->GetWXWidget() ;
475 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
476 [container addSubview:m_osxView];
479 void wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to )
481 CGPoint p = CGPointMake( pt->x , pt->y );
482 p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ];
487 void wxWidgetIPhoneImpl::SetBackgroundColour( const wxColour &col )
489 // m_osxView.backgroundColor = [[UIColor alloc] initWithCGColor:col.GetCGColor()];
492 void wxWidgetIPhoneImpl::SetLabel(const wxString& title, wxFontEncoding encoding)
494 if ( [m_osxView respondsToSelector:@selector(setTitle:forState:) ] )
496 wxCFStringRef cf( title , encoding );
497 [m_osxView setTitle:cf.AsNSString() forState:UIControlStateNormal ];
499 else if ( [m_osxView respondsToSelector:@selector(setStringValue:) ] )
501 wxCFStringRef cf( title , encoding );
502 [m_osxView setStringValue:cf.AsNSString()];
507 void wxWidgetIPhoneImpl::SetCursor( const wxCursor & cursor )
511 void wxWidgetIPhoneImpl::CaptureMouse()
515 void wxWidgetIPhoneImpl::ReleaseMouse()
519 wxInt32 wxWidgetIPhoneImpl::GetValue() const
523 void wxWidgetIPhoneImpl::SetValue( wxInt32 v )
527 void wxWidgetIPhoneImpl::SetBitmap( const wxBitmap& bitmap )
531 wxBitmap wxWidgetIPhoneImpl::GetBitmap() const
537 void wxWidgetIPhoneImpl::SetBitmapPosition( wxDirection dir )
541 void wxWidgetIPhoneImpl::SetupTabs( const wxNotebook ¬ebook )
545 void wxWidgetIPhoneImpl::GetBestRect( wxRect *r ) const
547 r->x = r->y = r->width = r->height = 0;
549 if ( [m_osxView respondsToSelector:@selector(sizeToFit)] )
551 CGRect former = [m_osxView frame];
552 [m_osxView sizeToFit];
553 CGRect best = [m_osxView frame];
554 [m_osxView setFrame:former];
555 r->width = best.size.width;
556 r->height = best.size.height;
560 bool wxWidgetIPhoneImpl::IsEnabled() const
564 void wxWidgetIPhoneImpl::Enable( bool enable )
568 void wxWidgetIPhoneImpl::SetMinimum( wxInt32 v )
572 void wxWidgetIPhoneImpl::SetMaximum( wxInt32 v )
576 wxInt32 wxWidgetIPhoneImpl::GetMinimum() const
580 wxInt32 wxWidgetIPhoneImpl::GetMaximum() const
584 void wxWidgetIPhoneImpl::PulseGauge()
588 void wxWidgetIPhoneImpl::SetScrollThumb( wxInt32 value, wxInt32 thumbSize )
592 void wxWidgetIPhoneImpl::SetControlSize( wxWindowVariant variant )
596 void wxWidgetIPhoneImpl::SetFont( const wxFont & font , const wxColour& foreground , long windowStyle, bool ignoreBlack )
600 void wxWidgetIPhoneImpl::InstallEventHandler( WXWidget control )
602 WXWidget c = control ? control : (WXWidget) m_osxView;
603 wxWidgetImpl::Associate( c, this ) ;
605 if ([c isKindOfClass:[UIControl class] ])
607 UIControl* cc = (UIControl*) c;
609 [cc addTarget:self action:@selector(touchUpInsideAction:event:) forControlEvents:UIControlEventTouchUpInside];
614 void wxWidgetIPhoneImpl::DoNotifyFocusEvent(bool receivedFocus, wxWidgetImpl* otherWindow)
616 wxWindow* thisWindow = GetWXPeer();
617 if ( thisWindow->MacGetTopLevelWindow() && NeedsFocusRect() )
619 thisWindow->MacInvalidateBorders();
624 wxLogTrace(wxT("Focus"), wxT("focus set(%p)"), static_cast<void*>(thisWindow));
625 wxChildFocusEvent eventFocus((wxWindow*)thisWindow);
626 thisWindow->HandleWindowEvent(eventFocus);
629 if ( thisWindow->GetCaret() )
630 thisWindow->GetCaret()->OnSetFocus();
633 wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId());
634 event.SetEventObject(thisWindow);
636 event.SetWindow(otherWindow->GetWXPeer());
637 thisWindow->HandleWindowEvent(event) ;
639 else // !receivedFocuss
642 if ( thisWindow->GetCaret() )
643 thisWindow->GetCaret()->OnKillFocus();
646 wxLogTrace(wxT("Focus"), wxT("focus lost(%p)"), static_cast<void*>(thisWindow));
648 wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId());
649 event.SetEventObject(thisWindow);
651 event.SetWindow(otherWindow->GetWXPeer());
652 thisWindow->HandleWindowEvent(event) ;
656 typedef void (*wxOSX_DrawRectHandlerPtr)(UIView* self, SEL _cmd, CGRect rect);
657 typedef BOOL (*wxOSX_FocusHandlerPtr)(UIView* self, SEL _cmd);
659 bool wxWidgetIPhoneImpl::becomeFirstResponder(WXWidget slf, void *_cmd)
661 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
662 // get the current focus before running becomeFirstResponder
663 UIView* otherView = FindFocus();
664 wxWidgetImpl* otherWindow = FindFromWXWidget(otherView);
665 BOOL r = superimpl(slf, (SEL)_cmd);
668 DoNotifyFocusEvent( true, otherWindow );
673 bool wxWidgetIPhoneImpl::resignFirstResponder(WXWidget slf, void *_cmd)
675 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
676 BOOL r = superimpl(slf, (SEL)_cmd);
677 // get the current focus after running resignFirstResponder
678 UIView* otherView = FindFocus();
679 wxWidgetImpl* otherWindow = FindFromWXWidget(otherView);
680 // NSTextViews have an editor as true responder, therefore the might get the
681 // resign notification if their editor takes over, don't trigger any event hen
682 if ( r && otherWindow != this)
684 DoNotifyFocusEvent( false, otherWindow );
689 void wxWidgetIPhoneImpl::drawRect(CGRect* rect, WXWidget slf, void *WXUNUSED(_cmd))
691 CGContextRef context = (CGContextRef) UIGraphicsGetCurrentContext();
692 CGContextSaveGState( context );
695 CGContextSetFillColorWithColor( context, GetWXPeer()->GetBackgroundColour().GetCGColor());
696 CGContextFillRect(context, *rect );
698 GetWXPeer()->MacSetCGContextRef( context );
700 GetWXPeer()->GetUpdateRegion() =
701 wxRegion(rect->origin.x,rect->origin.y,rect->size.width,rect->size.height) ;
703 wxRegion updateRgn( wxFromNSRect( slf, *rect ) );
705 wxWindow* wxpeer = GetWXPeer();
706 wxpeer->GetUpdateRegion() = updateRgn;
707 wxpeer->MacSetCGContextRef( context );
709 bool handled = wxpeer->MacDoRedraw( 0 );
711 CGContextRestoreGState( context );
713 CGContextSaveGState( context );
717 SEL _cmd = @selector(drawRect:);
718 wxOSX_DrawRectHandlerPtr superimpl = (wxOSX_DrawRectHandlerPtr) [[slf superclass] instanceMethodForSelector:_cmd];
719 if ( superimpl != wxOSX_drawRect )
721 superimpl(slf, _cmd, *rect);
722 CGContextRestoreGState( context );
723 CGContextSaveGState( context );
726 wxpeer->MacPaintChildrenBorders();
727 wxpeer->MacSetCGContextRef( NULL );
729 CGContextRestoreGState( context );
732 void wxWidgetIPhoneImpl::touchEvent(NSSet* touches, UIEvent *event, WXWidget slf, void *WXUNUSED(_cmd))
734 CGPoint clickLocation;
735 UITouch *touch = [touches anyObject];
736 clickLocation = [touch locationInView:slf];
737 wxPoint pt = wxFromNSPoint( m_osxView, clickLocation );
739 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
740 SetupMouseEvent( wxevent , touches, event ) ;
743 wxevent.SetEventObject( GetWXPeer() ) ;
744 //?wxevent.SetId( GetWXPeer()->GetId() ) ;
746 GetWXPeer()->HandleWindowEvent(wxevent);
749 void wxWidgetIPhoneImpl::touchUpInsideAction(void* sender, WX_UIEvent evt, WXWidget slf, void* _cmd)
757 wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* WXUNUSED(parent),
758 wxWindowID WXUNUSED(id), const wxPoint& pos, const wxSize& size,
759 long WXUNUSED(style), long WXUNUSED(extraStyle))
761 UIView* sv = (wxpeer->GetParent()->GetHandle() );
763 CGRect r = CGRectMake( pos.x, pos.y, size.x, size.y) ;
764 // Rect bounds = wxMacGetBoundsForControl( wxpeer, pos , size ) ;
765 wxUIView* v = [[wxUIView alloc] initWithFrame:r];
766 sv.clipsToBounds = YES;
767 sv.contentMode = UIViewContentModeRedraw;
768 sv.clearsContextBeforeDrawing = NO;
769 wxWidgetIPhoneImpl* c = new wxWidgetIPhoneImpl( wxpeer, v );
773 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
775 UIWindow* toplevelwindow = now->GetWXWindow();
776 CGRect frame = [toplevelwindow bounds];
777 CGRect appframe = [[UIScreen mainScreen] applicationFrame];
779 if ( now->GetWindowStyle() == wxDEFAULT_FRAME_STYLE )
781 double offset = appframe.origin.y;
782 frame.origin.y += offset;
783 frame.size.height -= offset;
786 wxUIContentView* contentview = [[wxUIContentView alloc] initWithFrame:frame];
787 contentview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
788 wxUIContentViewController* controller = [[wxUIContentViewController alloc] init];
789 controller.view = contentview;
791 wxWidgetIPhoneImpl* impl = new wxWidgetIPhoneImpl( now, contentview, true );
792 impl->InstallEventHandler();
793 [toplevelwindow addSubview:contentview];