1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/iphone/window.mm
3 // Purpose: widgets (non tlw) for iphone
4 // Author: Stefan Csomor
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 "wx/textctrl.h"
25 #include <objc/runtime.h>
27 WXWidget wxWidgetImpl::FindFocus()
29 UIView* focusedView = nil;
30 UIWindow* keyWindow = [[UIApplication sharedApplication] keyWindow];
31 if ( keyWindow != nil )
34 NSResponder* responder = [keyWindow firstResponder];
35 if ( [responder isKindOfClass:[NSTextView class]] &&
36 [keyWindow fieldEditor:NO forObject:nil] != nil )
38 focusedView = [(NSTextView*)responder delegate];
42 if ( [responder isKindOfClass:[NSView class]] )
43 focusedView = (NSView*) responder;
50 CGRect wxOSXGetFrameForControl( wxWindowMac* window , const wxPoint& pos , const wxSize &size , bool adjustForOrigin )
54 window->MacGetBoundsForControl( pos , size , x , y, w, h , adjustForOrigin ) ;
55 wxRect bounds(x,y,w,h);
56 UIView* sv = (window->GetParent()->GetHandle() );
58 return wxToNSRect( sv, bounds );
62 @interface wxUIView(PossibleMethods)
63 - (void)setTitle:(NSString *)title forState:(UIControlState)state;
65 - (void)drawRect: (CGRect) rect;
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;
72 - (BOOL) becomeFirstResponder;
73 - (BOOL) resignFirstResponder;
80 void SetupMouseEvent( wxMouseEvent &wxevent , NSSet* touches, UIEvent * nsEvent )
82 UInt32 modifiers = 0 ;
83 UITouch *touch = [touches anyObject];
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
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] ) ;
100 // a control click is interpreted as a right click
101 bool thisButtonIsFakeRight = false ;
102 if ( button == kEventMouseButtonPrimary && (modifiers & controlKey) )
104 button = kEventMouseButtonSecondary ;
105 thisButtonIsFakeRight = true ;
108 // otherwise we report double clicks by connecting a left click with a ctrl-left click
109 if ( clickCount > 1 && button != g_lastButton )
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
114 if ( cEvent.GetKind() == kEventMouseDown )
116 g_lastButton = button ;
117 g_lastButtonWasFakeRight = thisButtonIsFakeRight ;
123 g_lastButtonWasFakeRight = false ;
125 else if ( g_lastButton == kEventMouseButtonSecondary && g_lastButtonWasFakeRight )
126 button = g_lastButton ;
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);
136 wxevent.m_leftDown = true ;
138 wxevent.m_rightDown = true ;
140 wxevent.m_middleDown = true ;
143 // translate into wx types
144 int eventType = [touch phase];
147 case UITouchPhaseBegan :
151 wxevent.SetEventType( clickCount > 1 ? wxEVT_LEFT_DCLICK : wxEVT_LEFT_DOWN ) ;
159 case UITouchPhaseEnded :
163 wxevent.SetEventType( wxEVT_LEFT_UP ) ;
171 case UITouchPhaseMoved :
172 wxevent.SetEventType( wxEVT_MOTION ) ;
179 @implementation wxUIView
183 static BOOL initialized = NO;
187 wxOSXIPhoneClassAddWXMethods( self );
194 - (void)drawRect: (CGRect) rect
196 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
199 CGContextRef context = (CGContextRef) UIGraphicsGetCurrentContext();
200 CGContextSaveGState( context );
203 CGContextSetFillColorWithColor( context, impl->GetWXPeer()->GetBackgroundColour().GetCGColor());
204 CGContextFillRect(context, rect );
206 impl->GetWXPeer()->MacSetCGContextRef( context );
208 impl->GetWXPeer()->GetUpdateRegion() =
209 wxRegion(rect.origin.x,rect.origin.y,rect.size.width,rect.size.height) ;
212 event.SetTimestamp(0); // todo
213 event.SetEventObject(impl->GetWXPeer());
214 impl->GetWXPeer()->HandleWindowEvent(event);
216 CGContextRestoreGState( context );
221 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
223 [self handleTouchEvent:touches withEvent:event];
226 - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
228 [self handleTouchEvent:touches withEvent:event];
231 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
233 [self handleTouchEvent:touches withEvent:event];
236 -(void)handleTouchEvent:(NSSet *)touches withEvent:(UIEvent *)event
238 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
239 CGPoint clickLocation;
240 UITouch *touch = [touches anyObject];
241 clickLocation = [touch locationInView:self];
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);
254 void wxOSX_touchEvent(UIView* self, SEL _cmd, NSSet* touches, UIEvent *event )
256 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
260 impl->touchEvent(touches, event, self, _cmd);
263 BOOL wxOSX_becomeFirstResponder(UIView* self, SEL _cmd)
265 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
269 return impl->becomeFirstResponder(self, _cmd);
272 BOOL wxOSX_resignFirstResponder(UIView* self, SEL _cmd)
274 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
278 return impl->resignFirstResponder(self, _cmd);
281 void wxOSX_drawRect(UIView* self, SEL _cmd, CGRect rect)
283 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
287 return impl->drawRect(&rect, self, _cmd);
291 void wxOSXIPhoneClassAddWXMethods(Class c)
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}}" );
302 // UIControl extensions
305 @interface UIControl (wxUIControlActionSupport)
307 - (void) WX_touchUpInsideAction:(id)sender event:(UIEvent*)event;
308 - (void) WX_valueChangedAction:(id)sender event:(UIEvent*)event;
312 @implementation UIControl (wxUIControlActionSupport)
314 - (void) WX_touchUpInsideAction:(id)sender event:(UIEvent*)event
316 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
318 impl->controlAction(sender, UIControlEventTouchUpInside, event);
321 - (void) WX_valueChangedAction:(id)sender event:(UIEvent*)event
323 wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self );
325 impl->controlAction(sender, UIControlEventValueChanged, event);
330 IMPLEMENT_DYNAMIC_CLASS( wxWidgetIPhoneImpl , wxWidgetImpl )
332 wxWidgetIPhoneImpl::wxWidgetIPhoneImpl( wxWindowMac* peer , WXWidget w, bool isRootControl ) :
333 wxWidgetImpl( peer, isRootControl ), m_osxView(w)
337 wxWidgetIPhoneImpl::wxWidgetIPhoneImpl()
341 void wxWidgetIPhoneImpl::Init()
346 wxWidgetIPhoneImpl::~wxWidgetIPhoneImpl()
348 RemoveAssociations( this );
350 if ( !IsRootControl() )
352 UIView *sv = [m_osxView superview];
354 [m_osxView removeFromSuperview];
359 bool wxWidgetIPhoneImpl::IsVisible() const
361 UIView* view = m_osxView;
362 while ( view != nil && [view isHidden] == NO )
364 view = [view superview];
369 void wxWidgetIPhoneImpl::SetVisibility( bool visible )
371 [m_osxView setHidden:(visible ? NO:YES)];
374 void wxWidgetIPhoneImpl::Raise()
376 [[m_osxView superview] bringSubviewToFront:m_osxView];
379 void wxWidgetIPhoneImpl::Lower()
381 [[m_osxView superview] sendSubviewToBack:m_osxView];
384 void wxWidgetIPhoneImpl::ScrollRect( const wxRect *rect, int dx, int dy )
389 void wxWidgetIPhoneImpl::Move(int x, int y, int width, int height)
391 CGRect r = CGRectMake( x, y, width, height) ;
392 [m_osxView setFrame:r];
397 void wxWidgetIPhoneImpl::GetPosition( int &x, int &y ) const
399 CGRect r = [m_osxView frame];
404 void wxWidgetIPhoneImpl::GetSize( int &width, int &height ) const
406 CGRect rect = [m_osxView frame];
407 width = rect.size.width;
408 height = rect.size.height;
411 void wxWidgetIPhoneImpl::GetContentArea( int&left, int &top, int &width, int &height ) const
414 CGRect rect = [m_osxView bounds];
415 width = rect.size.width;
416 height = rect.size.height;
419 void wxWidgetIPhoneImpl::SetNeedsDisplay( const wxRect* where )
423 CGRect r = CGRectMake( where->x, where->y, where->width, where->height) ;
424 [m_osxView setNeedsDisplayInRect:r];
427 [m_osxView setNeedsDisplay];
430 bool wxWidgetIPhoneImpl::GetNeedsDisplay() const
433 // return [m_osxView needsDisplay];
436 bool wxWidgetIPhoneImpl::CanFocus() const
438 return [m_osxView canBecomeFirstResponder] == YES;
439 // ? return [m_osxView isUserInteractionEnabled] == YES;
442 bool wxWidgetIPhoneImpl::HasFocus() const
444 return [m_osxView isFirstResponder] == YES;
447 bool wxWidgetIPhoneImpl::SetFocus()
449 // [m_osxView makeKeyWindow] ;
455 void wxWidgetIPhoneImpl::RemoveFromParent()
457 [m_osxView removeFromSuperview];
460 void wxWidgetIPhoneImpl::Embed( wxWidgetImpl *parent )
462 UIView* container = parent->GetWXWidget() ;
463 wxASSERT_MSG( container != NULL , wxT("No valid mac container control") ) ;
464 [container addSubview:m_osxView];
467 void wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to )
469 CGPoint p = CGPointMake( pt->x , pt->y );
470 p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ];
475 void wxWidgetIPhoneImpl::SetBackgroundColour( const wxColour &col )
477 m_osxView.backgroundColor = [[UIColor alloc] initWithCGColor:col.GetCGColor()];
480 bool wxWidgetIPhoneImpl::SetBackgroundStyle(wxBackgroundStyle style)
482 if ( style == wxBG_STYLE_PAINT )
483 [m_osxView setOpaque: YES ];
486 [m_osxView setOpaque: NO ];
487 m_osxView.backgroundColor = [UIColor clearColor];
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 float wxWidgetIPhoneImpl::GetContentScaleFactor() const
598 return [m_osxView contentScaleFactor];
601 void wxWidgetIPhoneImpl::SetFont( const wxFont & font , const wxColour& foreground , long windowStyle, bool ignoreBlack )
605 void wxWidgetIPhoneImpl::InstallEventHandler( WXWidget control )
607 WXWidget c = control ? control : (WXWidget) m_osxView;
608 wxWidgetImpl::Associate( c, this ) ;
610 if ([c isKindOfClass:[UIControl class] ])
612 UIControl* cc = (UIControl*) c;
613 [cc addTarget:cc action:@selector(WX_touchUpInsideAction:event:) forControlEvents:UIControlEventTouchUpInside];
614 [cc addTarget:cc action:@selector(WX_valueChangedAction:event:) forControlEvents:UIControlEventValueChanged];
618 void wxWidgetIPhoneImpl::DoNotifyFocusEvent(bool receivedFocus, wxWidgetImpl* otherWindow)
620 wxWindow* thisWindow = GetWXPeer();
621 if ( thisWindow->MacGetTopLevelWindow() && NeedsFocusRect() )
623 thisWindow->MacInvalidateBorders();
628 wxLogTrace(wxT("Focus"), wxT("focus set(%p)"), static_cast<void*>(thisWindow));
629 wxChildFocusEvent eventFocus((wxWindow*)thisWindow);
630 thisWindow->HandleWindowEvent(eventFocus);
633 if ( thisWindow->GetCaret() )
634 thisWindow->GetCaret()->OnSetFocus();
637 wxFocusEvent event(wxEVT_SET_FOCUS, thisWindow->GetId());
638 event.SetEventObject(thisWindow);
640 event.SetWindow(otherWindow->GetWXPeer());
641 thisWindow->HandleWindowEvent(event) ;
643 else // !receivedFocuss
646 if ( thisWindow->GetCaret() )
647 thisWindow->GetCaret()->OnKillFocus();
650 wxLogTrace(wxT("Focus"), wxT("focus lost(%p)"), static_cast<void*>(thisWindow));
652 wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId());
653 event.SetEventObject(thisWindow);
655 event.SetWindow(otherWindow->GetWXPeer());
656 thisWindow->HandleWindowEvent(event) ;
660 typedef void (*wxOSX_DrawRectHandlerPtr)(UIView* self, SEL _cmd, CGRect rect);
661 typedef BOOL (*wxOSX_FocusHandlerPtr)(UIView* self, SEL _cmd);
663 bool wxWidgetIPhoneImpl::becomeFirstResponder(WXWidget slf, void *_cmd)
665 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
666 // get the current focus before running becomeFirstResponder
667 UIView* otherView = FindFocus();
668 wxWidgetImpl* otherWindow = FindFromWXWidget(otherView);
669 BOOL r = superimpl(slf, (SEL)_cmd);
672 DoNotifyFocusEvent( true, otherWindow );
677 bool wxWidgetIPhoneImpl::resignFirstResponder(WXWidget slf, void *_cmd)
679 wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd];
680 BOOL r = superimpl(slf, (SEL)_cmd);
681 // get the current focus after running resignFirstResponder
682 UIView* otherView = FindFocus();
683 wxWidgetImpl* otherWindow = FindFromWXWidget(otherView);
684 // NSTextViews have an editor as true responder, therefore the might get the
685 // resign notification if their editor takes over, don't trigger any event hen
686 if ( r && otherWindow != this)
688 DoNotifyFocusEvent( false, otherWindow );
693 void wxWidgetIPhoneImpl::drawRect(CGRect* rect, WXWidget slf, void *WXUNUSED(_cmd))
695 CGContextRef context = (CGContextRef) UIGraphicsGetCurrentContext();
696 CGContextSaveGState( context );
699 CGContextSetFillColorWithColor( context, GetWXPeer()->GetBackgroundColour().GetCGColor());
700 CGContextFillRect(context, *rect );
702 GetWXPeer()->MacSetCGContextRef( context );
704 GetWXPeer()->GetUpdateRegion() =
705 wxRegion(rect->origin.x,rect->origin.y,rect->size.width,rect->size.height) ;
707 wxRegion updateRgn( wxFromNSRect( slf, *rect ) );
709 wxWindow* wxpeer = GetWXPeer();
710 wxpeer->GetUpdateRegion() = updateRgn;
711 wxpeer->MacSetCGContextRef( context );
713 bool handled = wxpeer->MacDoRedraw( 0 );
715 CGContextRestoreGState( context );
717 CGContextSaveGState( context );
721 SEL _cmd = @selector(drawRect:);
722 wxOSX_DrawRectHandlerPtr superimpl = (wxOSX_DrawRectHandlerPtr) [[slf superclass] instanceMethodForSelector:_cmd];
723 if ( superimpl != wxOSX_drawRect )
725 superimpl(slf, _cmd, *rect);
726 CGContextRestoreGState( context );
727 CGContextSaveGState( context );
730 wxpeer->MacPaintChildrenBorders();
731 wxpeer->MacSetCGContextRef( NULL );
733 CGContextRestoreGState( context );
736 void wxWidgetIPhoneImpl::touchEvent(NSSet* touches, UIEvent *event, WXWidget slf, void *WXUNUSED(_cmd))
738 bool inRecursion = false;
742 UITouch *touch = [touches anyObject];
743 CGPoint clickLocation;
744 if ( [touch view] != slf && IsRootControl() )
746 NSLog(@"self is %@ and touch view is %@",slf,[touch view]);
752 clickLocation = [touch locationInView:slf];
753 wxPoint pt = wxFromNSPoint( m_osxView, clickLocation );
755 wxMouseEvent wxevent(wxEVT_LEFT_DOWN);
756 SetupMouseEvent( wxevent , touches, event ) ;
759 wxevent.SetEventObject( GetWXPeer() ) ;
760 //?wxevent.SetId( GetWXPeer()->GetId() ) ;
762 GetWXPeer()->HandleWindowEvent(wxevent);
766 void wxWidgetIPhoneImpl::controlAction(void* sender, wxUint32 controlEvent, WX_UIEvent rawEvent)
768 if ( controlEvent == UIControlEventTouchUpInside )
769 GetWXPeer()->OSXHandleClicked(0);
772 void wxWidgetIPhoneImpl::controlTextDidChange()
774 wxTextCtrl* wxpeer = wxDynamicCast((wxWindow*)GetWXPeer(),wxTextCtrl);
777 wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId());
778 event.SetEventObject( wxpeer );
779 event.SetString( wxpeer->GetValue() );
780 wxpeer->HandleWindowEvent( event );
788 wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* WXUNUSED(parent),
789 wxWindowID WXUNUSED(id), const wxPoint& pos, const wxSize& size,
790 long WXUNUSED(style), long WXUNUSED(extraStyle))
792 UIView* sv = (wxpeer->GetParent()->GetHandle() );
794 CGRect r = CGRectMake( pos.x, pos.y, size.x, size.y) ;
795 // Rect bounds = wxMacGetBoundsForControl( wxpeer, pos , size ) ;
796 wxUIView* v = [[wxUIView alloc] initWithFrame:r];
797 sv.clipsToBounds = YES;
798 sv.contentMode = UIViewContentModeRedraw;
799 sv.clearsContextBeforeDrawing = NO;
800 wxWidgetIPhoneImpl* c = new wxWidgetIPhoneImpl( wxpeer, v );