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 void wxWidgetIPhoneImpl::SetupTabs( const wxNotebook ¬ebook )
526 void wxWidgetIPhoneImpl::GetBestRect( wxRect *r ) const
528 r->x = r->y = r->width = r->height = 0;
530 if ( [m_osxView respondsToSelector:@selector(sizeToFit)] )
532 CGRect former = [m_osxView frame];
533 [m_osxView sizeToFit];
534 CGRect best = [m_osxView frame];
535 [m_osxView setFrame:former];
536 r->width = best.size.width;
537 r->height = best.size.height;
541 bool wxWidgetIPhoneImpl::IsEnabled() const
545 void wxWidgetIPhoneImpl::Enable( bool enable )
549 void wxWidgetIPhoneImpl::SetMinimum( wxInt32 v )
553 void wxWidgetIPhoneImpl::SetMaximum( wxInt32 v )
557 wxInt32 wxWidgetIPhoneImpl::GetMinimum() const
561 wxInt32 wxWidgetIPhoneImpl::GetMaximum() const
565 void wxWidgetIPhoneImpl::PulseGauge()
569 void wxWidgetIPhoneImpl::SetScrollThumb( wxInt32 value, wxInt32 thumbSize )
573 void wxWidgetIPhoneImpl::SetControlSize( wxWindowVariant variant )
577 void wxWidgetIPhoneImpl::SetFont( const wxFont & font , const wxColour& foreground , long windowStyle, bool ignoreBlack )
581 void wxWidgetIPhoneImpl::InstallEventHandler( WXWidget control )
583 WXWidget c = control ? control : (WXWidget) m_osxView;
584 wxWidgetImpl::Associate( c, this ) ;
586 if ([c isKindOfClass:[UIControl class] ])
588 UIControl* cc = (UIControl*) c;
590 [cc addTarget:self action:@selector(touchUpInsideAction:event:) forControlEvents:UIControlEventTouchUpInside];
595 void wxWidgetIPhoneImpl::DoNotifyFocusEvent(bool receivedFocus, wxWidgetImpl* otherWindow)
597 wxWindow* thisWindow = GetWXPeer();
598 if ( thisWindow->MacGetTopLevelWindow() && NeedsFocusRect() )
600 thisWindow->MacInvalidateBorders();
605 wxLogTrace(_T("Focus"), _T("focus set(%p)"), static_cast<void*>(thisWindow));
606 wxChildFocusEvent eventFocus((wxWindow*)thisWindow);
607 thisWindow->HandleWindowEvent(eventFocus);
610 if ( thisWindow->GetCaret() )
611 thisWindow->GetCaret()->OnSetFocus();
614 wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId());
615 event.SetEventObject(thisWindow);
617 event.SetWindow(otherWindow->GetWXPeer());
618 thisWindow->HandleWindowEvent(event) ;
620 else // !receivedFocuss
623 if ( thisWindow->GetCaret() )
624 thisWindow->GetCaret()->OnKillFocus();
627 wxLogTrace(_T("Focus"), _T("focus lost(%p)"), static_cast<void*>(thisWindow));
629 wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId());
630 event.SetEventObject(thisWindow);
632 event.SetWindow(otherWindow->GetWXPeer());
633 thisWindow->HandleWindowEvent(event) ;
637 typedef void (*wxOSX_DrawRectHandlerPtr)(UIView* self, SEL _cmd, CGRect rect);
638 typedef BOOL (*wxOSX_FocusHandlerPtr)(UIView* self, SEL _cmd);
640 bool wxWidgetIPhoneImpl::becomeFirstResponder(WXWidget slf, void *_cmd)
642 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
643 // get the current focus before running becomeFirstResponder
644 UIView* otherView = FindFocus();
645 wxWidgetImpl* otherWindow = FindFromWXWidget(otherView);
646 BOOL r = superimpl(slf, (SEL)_cmd);
649 DoNotifyFocusEvent( true, otherWindow );
654 bool wxWidgetIPhoneImpl::resignFirstResponder(WXWidget slf, void *_cmd)
656 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
657 BOOL r = superimpl(slf, (SEL)_cmd);
658 // get the current focus after running resignFirstResponder
659 UIView* otherView = FindFocus();
660 wxWidgetImpl* otherWindow = FindFromWXWidget(otherView);
661 // NSTextViews have an editor as true responder, therefore the might get the
662 // resign notification if their editor takes over, don't trigger any event hen
663 if ( r && otherWindow != this)
665 DoNotifyFocusEvent( false, otherWindow );
670 void wxWidgetIPhoneImpl::drawRect(CGRect* rect, WXWidget slf, void *WXUNUSED(_cmd))
672 CGContextRef context = (CGContextRef) UIGraphicsGetCurrentContext();
673 CGContextSaveGState( context );
676 CGContextSetFillColorWithColor( context, GetWXPeer()->GetBackgroundColour().GetCGColor());
677 CGContextFillRect(context, *rect );
679 GetWXPeer()->MacSetCGContextRef( context );
681 GetWXPeer()->GetUpdateRegion() =
682 wxRegion(rect->origin.x,rect->origin.y,rect->size.width,rect->size.height) ;
684 wxRegion updateRgn( wxFromNSRect( slf, *rect ) );
686 wxWindow* wxpeer = GetWXPeer();
687 wxpeer->GetUpdateRegion() = updateRgn;
688 wxpeer->MacSetCGContextRef( context );
690 bool handled = wxpeer->MacDoRedraw( 0 );
692 CGContextRestoreGState( context );
694 CGContextSaveGState( context );
698 SEL _cmd = @selector(drawRect:);
699 wxOSX_DrawRectHandlerPtr superimpl = (wxOSX_DrawRectHandlerPtr) [[slf superclass] instanceMethodForSelector:_cmd];
700 superimpl(slf, _cmd, *rect);
701 CGContextRestoreGState( context );
702 CGContextSaveGState( context );
704 wxpeer->MacPaintChildrenBorders();
705 wxpeer->MacSetCGContextRef( NULL );
707 CGContextRestoreGState( context );
710 void wxWidgetIPhoneImpl::touchEvent(NSSet* touches, UIEvent *event, WXWidget slf, void *WXUNUSED(_cmd))
712 CGPoint clickLocation;
713 UITouch *touch = [touches anyObject];
714 clickLocation = [touch locationInView:slf];
715 wxPoint pt = wxFromNSPoint( m_osxView, clickLocation );
717 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
718 SetupMouseEvent( wxevent , touches, event ) ;
721 wxevent.SetEventObject( GetWXPeer() ) ;
722 //?wxevent.SetId( GetWXPeer()->GetId() ) ;
724 GetWXPeer()->HandleWindowEvent(wxevent);
727 void wxWidgetIPhoneImpl::touchUpInsideAction(void* sender, WX_UIEvent evt, WXWidget slf, void* _cmd)
735 wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* WXUNUSED(parent),
736 wxWindowID WXUNUSED(id), const wxPoint& pos, const wxSize& size,
737 long WXUNUSED(style), long WXUNUSED(extraStyle))
739 UIView* sv = (wxpeer->GetParent()->GetHandle() );
741 CGRect r = CGRectMake( pos.x, pos.y, size.x, size.y) ;
742 // Rect bounds = wxMacGetBoundsForControl( wxpeer, pos , size ) ;
743 wxUIView* v = [[wxUIView alloc] initWithFrame:r];
744 sv.clipsToBounds = YES;
745 sv.contentMode = UIViewContentModeRedraw;
746 sv.clearsContextBeforeDrawing = NO;
747 wxWidgetIPhoneImpl* c = new wxWidgetIPhoneImpl( wxpeer, v );
751 wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now )
753 UIWindow* toplevelwindow = now->GetWXWindow();
755 wxUIContentView* contentview = [[wxUIContentView alloc] initWithFrame:[toplevelwindow bounds]];
756 contentview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
757 wxUIContentViewController* controller = [[wxUIContentViewController alloc] init];
758 controller.view = contentview;
760 UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
761 // left orientation not yet implemented !
762 if (orientation == UIInterfaceOrientationLandscapeRight )
764 CGAffineTransform transform = v.transform;
766 // Use the status bar frame to determine the center point of the window's content area.
767 CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame];
768 CGRect bounds = CGRectMake(0, 0, statusBarFrame.size.height, statusBarFrame.origin.x);
769 CGPoint center = CGPointMake(bounds.size.height / 2.0, bounds.size.width / 2.0);
771 // Set the center point of the view to the center point of the window's content area.
774 // Rotate the view 90 degrees around its new center point.
775 transform = CGAffineTransformRotate(transform, ( M_PI / 2.0));
776 v.transform = transform;
779 wxWidgetIPhoneImpl* impl = new wxWidgetIPhoneImpl( now, contentview, true );
780 impl->InstallEventHandler();
781 [toplevelwindow addSubview:contentview];