X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/9e55f38df79556665b8f6aa1992cf3733ebf4e93..5a376169b1b6d476a2a149ddf68c0ecd9f8b6913:/src/osx/iphone/window.mm?ds=sidebyside diff --git a/src/osx/iphone/window.mm b/src/osx/iphone/window.mm index 154329afb9..d453aa8871 100644 --- a/src/osx/iphone/window.mm +++ b/src/osx/iphone/window.mm @@ -4,7 +4,7 @@ // Author: Stefan Csomor // Modified by: // Created: 2008-06-20 -// RCS-ID: $Id: window.mm 48805 2007-09-19 14:52:25Z SC $ +// RCS-ID: $Id$ // Copyright: (c) Stefan Csomor // Licence: wxWindows licence ///////////////////////////////////////////////////////////////////////////// @@ -20,8 +20,9 @@ #include "wx/log.h" #endif -#include +#include "wx/textctrl.h" +#include WXWidget wxWidgetImpl::FindFocus() { @@ -31,7 +32,7 @@ WXWidget wxWidgetImpl::FindFocus() { /* NSResponder* responder = [keyWindow firstResponder]; - if ( [responder isKindOfClass:[NSTextView class]] && + if ( [responder isKindOfClass:[NSTextView class]] && [keyWindow fieldEditor:NO forObject:nil] != nil ) { focusedView = [(NSTextView*)responder delegate]; @@ -57,11 +58,6 @@ CGRect wxOSXGetFrameForControl( wxWindowMac* window , const wxPoint& pos , const return wxToNSRect( sv, bounds ); } -@interface wxUIView : UIView -{ -} - -@end // wxUIView @interface wxUIView(PossibleMethods) - (void)setTitle:(NSString *)title forState:(UIControlState)state; @@ -77,18 +73,6 @@ CGRect wxOSXGetFrameForControl( wxWindowMac* window , const wxPoint& pos , const - (BOOL) resignFirstResponder; @end -@interface wxUIContentView : wxUIView -{ -} - -@end - -@interface wxUIContentViewController : UIViewController -{ -} - -@end - // // // @@ -99,7 +83,7 @@ void SetupMouseEvent( wxMouseEvent &wxevent , NSSet* touches, UIEvent * nsEvent UITouch *touch = [touches anyObject]; // these parameters are not given for all events - UInt32 button = 0; // no secondary button + UInt32 button = 0; // no secondary button UInt32 clickCount = [touch tapCount]; UInt32 mouseChord = 0; // TODO does this exist for cocoa @@ -197,7 +181,7 @@ void SetupMouseEvent( wxMouseEvent &wxevent , NSSet* touches, UIEvent * nsEvent + (void)initialize { static BOOL initialized = NO; - if (!initialized) + if (!initialized) { initialized = YES; wxOSXIPhoneClassAddWXMethods( self ); @@ -215,13 +199,13 @@ void SetupMouseEvent( wxMouseEvent &wxevent , NSSet* touches, UIEvent * nsEvent CGContextRef context = (CGContextRef) UIGraphicsGetCurrentContext(); CGContextSaveGState( context ); // draw background - + CGContextSetFillColorWithColor( context, impl->GetWXPeer()->GetBackgroundColour().GetCGColor()); CGContextFillRect(context, rect ); impl->GetWXPeer()->MacSetCGContextRef( context ); - impl->GetWXPeer()->GetUpdateRegion() = + impl->GetWXPeer()->GetUpdateRegion() = wxRegion(rect.origin.x,rect.origin.y,rect.size.width,rect.size.height) ; wxPaintEvent event; @@ -234,25 +218,25 @@ void SetupMouseEvent( wxMouseEvent &wxevent , NSSet* touches, UIEvent * nsEvent } -- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event +- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { [self handleTouchEvent:touches withEvent:event]; } -- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event +- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { [self handleTouchEvent:touches withEvent:event]; } -- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event +- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { [self handleTouchEvent:touches withEvent:event]; } --(void)handleTouchEvent:(NSSet *)touches withEvent:(UIEvent *)event -{ +-(void)handleTouchEvent:(NSSet *)touches withEvent:(UIEvent *)event +{ wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self ); - CGPoint clickLocation; + CGPoint clickLocation; UITouch *touch = [touches anyObject]; clickLocation = [touch locationInView:self]; @@ -267,12 +251,12 @@ void SetupMouseEvent( wxMouseEvent &wxevent , NSSet* touches, UIEvent * nsEvent */ -void wxOSX_touchEvent(UIView* self, SEL _cmd, NSSet* touches, UIEvent *event ) +void wxOSX_touchEvent(UIView* self, SEL _cmd, NSSet* touches, UIEvent *event ) { wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self ); if (impl == NULL) return; - + impl->touchEvent(touches, event, self, _cmd); } @@ -281,7 +265,7 @@ BOOL wxOSX_becomeFirstResponder(UIView* self, SEL _cmd) wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self ); if (impl == NULL) return NO; - + return impl->becomeFirstResponder(self, _cmd); } @@ -290,7 +274,7 @@ BOOL wxOSX_resignFirstResponder(UIView* self, SEL _cmd) wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self ); if (impl == NULL) return NO; - + return impl->resignFirstResponder(self, _cmd); } @@ -299,7 +283,7 @@ void wxOSX_drawRect(UIView* self, SEL _cmd, CGRect rect) wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self ); if (impl == NULL) return; - + return impl->drawRect(&rect, self, _cmd); } @@ -314,38 +298,43 @@ void wxOSXIPhoneClassAddWXMethods(Class c) class_addMethod(c, @selector(drawRect:), (IMP) wxOSX_drawRect, "v@:{_CGRect={_CGPoint=ff}{_CGSize=ff}}" ); } -@implementation wxUIContentView +// +// UIControl extensions +// + +@interface UIControl (wxUIControlActionSupport) + +- (void) WX_touchUpInsideAction:(id)sender event:(UIEvent*)event; +- (void) WX_valueChangedAction:(id)sender event:(UIEvent*)event; @end -@implementation wxUIContentViewController +@implementation UIControl (wxUIControlActionSupport) -- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation) interfaceOrientation +- (void) WX_touchUpInsideAction:(id)sender event:(UIEvent*)event { - return YES; + wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self ); + if (impl != NULL) + impl->controlAction(sender, UIControlEventTouchUpInside, event); } -- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation +- (void) WX_valueChangedAction:(id)sender event:(UIEvent*)event { - CGRect fr = [self.view frame]; -// CGRect cv = [[self.view superview] frame]; -// CGRect bounds = CGRectMake( 0,0,fr.size.width, fr.size.height); -// [[self.view superview] setFrame: fr ]; -// [self.view setFrame: bounds]; -// [self.view setNeedsDisplayInRect:bounds]; + wxWidgetIPhoneImpl* impl = (wxWidgetIPhoneImpl* ) wxWidgetImpl::FindFromWXWidget( self ); + if (impl != NULL) + impl->controlAction(sender, UIControlEventValueChanged, event); } @end - IMPLEMENT_DYNAMIC_CLASS( wxWidgetIPhoneImpl , wxWidgetImpl ) -wxWidgetIPhoneImpl::wxWidgetIPhoneImpl( wxWindowMac* peer , WXWidget w, bool isRootControl ) : - wxWidgetImpl( peer, isRootControl ), m_osxView(w) +wxWidgetIPhoneImpl::wxWidgetIPhoneImpl( wxWindowMac* peer , WXWidget w, bool isRootControl, bool isUserPane ) : + wxWidgetImpl( peer, isRootControl, isUserPane ), m_osxView(w) { } -wxWidgetIPhoneImpl::wxWidgetIPhoneImpl() +wxWidgetIPhoneImpl::wxWidgetIPhoneImpl() { } @@ -366,11 +355,15 @@ wxWidgetIPhoneImpl::~wxWidgetIPhoneImpl() } [m_osxView release]; } - -bool wxWidgetIPhoneImpl::IsVisible() const + +bool wxWidgetIPhoneImpl::IsVisible() const { - // TODO reflect Superviews state - return [m_osxView isHidden] == NO; + UIView* view = m_osxView; + while ( view != nil && [view isHidden] == NO ) + { + view = [view superview]; + } + return view == nil; } void wxWidgetIPhoneImpl::SetVisibility( bool visible ) @@ -382,7 +375,7 @@ void wxWidgetIPhoneImpl::Raise() { [[m_osxView superview] bringSubviewToFront:m_osxView]; } - + void wxWidgetIPhoneImpl::Lower() { [[m_osxView superview] sendSubviewToBack:m_osxView]; @@ -399,6 +392,8 @@ void wxWidgetIPhoneImpl::Move(int x, int y, int width, int height) [m_osxView setFrame:r]; } + + void wxWidgetIPhoneImpl::GetPosition( int &x, int &y ) const { CGRect r = [m_osxView frame]; @@ -416,7 +411,9 @@ void wxWidgetIPhoneImpl::GetSize( int &width, int &height ) const void wxWidgetIPhoneImpl::GetContentArea( int&left, int &top, int &width, int &height ) const { left = top = 0; - GetSize( width, height ); + CGRect rect = [m_osxView bounds]; + width = rect.size.width; + height = rect.size.height; } void wxWidgetIPhoneImpl::SetNeedsDisplay( const wxRect* where ) @@ -447,7 +444,7 @@ bool wxWidgetIPhoneImpl::HasFocus() const return [m_osxView isFirstResponder] == YES; } -bool wxWidgetIPhoneImpl::SetFocus() +bool wxWidgetIPhoneImpl::SetFocus() { // [m_osxView makeKeyWindow] ; // TODO @@ -470,14 +467,26 @@ void wxWidgetIPhoneImpl::Embed( wxWidgetImpl *parent ) void wxWidgetImpl::Convert( wxPoint *pt , wxWidgetImpl *from , wxWidgetImpl *to ) { CGPoint p = CGPointMake( pt->x , pt->y ); - p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ]; + p = [from->GetWXWidget() convertPoint:p toView:to->GetWXWidget() ]; pt->x = (int)p.x; pt->y = (int)p.y; } void wxWidgetIPhoneImpl::SetBackgroundColour( const wxColour &col ) { - // m_osxView.backgroundColor = [[UIColor alloc] initWithCGColor:col.GetCGColor()]; + m_osxView.backgroundColor = [[UIColor alloc] initWithCGColor:col.GetCGColor()]; +} + +bool wxWidgetIPhoneImpl::SetBackgroundStyle(wxBackgroundStyle style) +{ + if ( style == wxBG_STYLE_PAINT ) + [m_osxView setOpaque: YES ]; + else + { + [m_osxView setOpaque: NO ]; + m_osxView.backgroundColor = [UIColor clearColor]; + } + return true; } void wxWidgetIPhoneImpl::SetLabel(const wxString& title, wxFontEncoding encoding) @@ -487,11 +496,13 @@ void wxWidgetIPhoneImpl::SetLabel(const wxString& title, wxFontEncoding encoding wxCFStringRef cf( title , encoding ); [m_osxView setTitle:cf.AsNSString() forState:UIControlStateNormal ]; } +#if 0 // nonpublic API problems else if ( [m_osxView respondsToSelector:@selector(setStringValue:) ] ) { wxCFStringRef cf( title , encoding ); [m_osxView setStringValue:cf.AsNSString()]; } +#endif } @@ -580,8 +591,16 @@ void wxWidgetIPhoneImpl::SetScrollThumb( wxInt32 value, wxInt32 thumbSize ) { } -void wxWidgetIPhoneImpl::SetControlSize( wxWindowVariant variant ) +void wxWidgetIPhoneImpl::SetControlSize( wxWindowVariant variant ) +{ +} + +float wxWidgetIPhoneImpl::GetContentScaleFactor() const { + if ( [m_osxView respondsToSelector:@selector(contentScaleFactor) ]) + return [m_osxView contentScaleFactor]; + else + return 1.0; } void wxWidgetIPhoneImpl::SetFont( const wxFont & font , const wxColour& foreground , long windowStyle, bool ignoreBlack ) @@ -592,13 +611,12 @@ void wxWidgetIPhoneImpl::InstallEventHandler( WXWidget control ) { WXWidget c = control ? control : (WXWidget) m_osxView; wxWidgetImpl::Associate( c, this ) ; - - if ([c isKindOfClass:[UIControl class] ]) + + if ([c isKindOfClass:[UIControl class] ]) { UIControl* cc = (UIControl*) c; - /* - [cc addTarget:self action:@selector(touchUpInsideAction:event:) forControlEvents:UIControlEventTouchUpInside]; - */ + [cc addTarget:cc action:@selector(WX_touchUpInsideAction:event:) forControlEvents:UIControlEventTouchUpInside]; + [cc addTarget:cc action:@selector(WX_valueChangedAction:event:) forControlEvents:UIControlEventValueChanged]; } } @@ -612,7 +630,7 @@ void wxWidgetIPhoneImpl::DoNotifyFocusEvent(bool receivedFocus, wxWidgetImpl* ot if ( receivedFocus ) { - wxLogTrace(_T("Focus"), _T("focus set(%p)"), static_cast(thisWindow)); + wxLogTrace(wxT("Focus"), wxT("focus set(%p)"), static_cast(thisWindow)); wxChildFocusEvent eventFocus((wxWindow*)thisWindow); thisWindow->HandleWindowEvent(eventFocus); @@ -634,8 +652,8 @@ void wxWidgetIPhoneImpl::DoNotifyFocusEvent(bool receivedFocus, wxWidgetImpl* ot thisWindow->GetCaret()->OnKillFocus(); #endif - wxLogTrace(_T("Focus"), _T("focus lost(%p)"), static_cast(thisWindow)); - + wxLogTrace(wxT("Focus"), wxT("focus lost(%p)"), static_cast(thisWindow)); + wxFocusEvent event( wxEVT_KILL_FOCUS, thisWindow->GetId()); event.SetEventObject(thisWindow); if (otherWindow) @@ -651,7 +669,7 @@ bool wxWidgetIPhoneImpl::becomeFirstResponder(WXWidget slf, void *_cmd) { wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd]; // get the current focus before running becomeFirstResponder - UIView* otherView = FindFocus(); + UIView* otherView = FindFocus(); wxWidgetImpl* otherWindow = FindFromWXWidget(otherView); BOOL r = superimpl(slf, (SEL)_cmd); if ( r ) @@ -666,7 +684,7 @@ bool wxWidgetIPhoneImpl::resignFirstResponder(WXWidget slf, void *_cmd) wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd]; BOOL r = superimpl(slf, (SEL)_cmd); // get the current focus after running resignFirstResponder - UIView* otherView = FindFocus(); + UIView* otherView = FindFocus(); wxWidgetImpl* otherWindow = FindFromWXWidget(otherView); // NSTextViews have an editor as true responder, therefore the might get the // resign notification if their editor takes over, don't trigger any event hen @@ -682,13 +700,13 @@ void wxWidgetIPhoneImpl::drawRect(CGRect* rect, WXWidget slf, void *WXUNUSED(_cm CGContextRef context = (CGContextRef) UIGraphicsGetCurrentContext(); CGContextSaveGState( context ); // draw background - +/* CGContextSetFillColorWithColor( context, GetWXPeer()->GetBackgroundColour().GetCGColor()); CGContextFillRect(context, *rect ); - +*/ GetWXPeer()->MacSetCGContextRef( context ); - GetWXPeer()->GetUpdateRegion() = + GetWXPeer()->GetUpdateRegion() = wxRegion(rect->origin.x,rect->origin.y,rect->size.width,rect->size.height) ; wxRegion updateRgn( wxFromNSRect( slf, *rect ) ); @@ -696,9 +714,9 @@ void wxWidgetIPhoneImpl::drawRect(CGRect* rect, WXWidget slf, void *WXUNUSED(_cm wxWindow* wxpeer = GetWXPeer(); wxpeer->GetUpdateRegion() = updateRgn; wxpeer->MacSetCGContextRef( context ); - + bool handled = wxpeer->MacDoRedraw( 0 ); - + CGContextRestoreGState( context ); CGContextSaveGState( context ); @@ -707,9 +725,12 @@ void wxWidgetIPhoneImpl::drawRect(CGRect* rect, WXWidget slf, void *WXUNUSED(_cm // call super SEL _cmd = @selector(drawRect:); wxOSX_DrawRectHandlerPtr superimpl = (wxOSX_DrawRectHandlerPtr) [[slf superclass] instanceMethodForSelector:_cmd]; - superimpl(slf, _cmd, *rect); - CGContextRestoreGState( context ); - CGContextSaveGState( context ); + if ( superimpl != wxOSX_drawRect ) + { + superimpl(slf, _cmd, *rect); + CGContextRestoreGState( context ); + CGContextSaveGState( context ); + } } wxpeer->MacPaintChildrenBorders(); wxpeer->MacSetCGContextRef( NULL ); @@ -719,76 +740,69 @@ void wxWidgetIPhoneImpl::drawRect(CGRect* rect, WXWidget slf, void *WXUNUSED(_cm void wxWidgetIPhoneImpl::touchEvent(NSSet* touches, UIEvent *event, WXWidget slf, void *WXUNUSED(_cmd)) { - CGPoint clickLocation; + bool inRecursion = false; + if ( inRecursion ) + return; + UITouch *touch = [touches anyObject]; - clickLocation = [touch locationInView:slf]; - wxPoint pt = wxFromNSPoint( m_osxView, clickLocation ); - - wxMouseEvent wxevent(wxEVT_LEFT_DOWN); - SetupMouseEvent( wxevent , touches, event ) ; - wxevent.m_x = pt.x; - wxevent.m_y = pt.y; - wxevent.SetEventObject( GetWXPeer() ) ; - //?wxevent.SetId( GetWXPeer()->GetId() ) ; + CGPoint clickLocation; + if ( [touch view] != slf && IsRootControl() ) + { + NSLog(@"self is %@ and touch view is %@",slf,[touch view]); + inRecursion = true; + inRecursion = false; + } + else + { + clickLocation = [touch locationInView:slf]; + wxPoint pt = wxFromNSPoint( m_osxView, clickLocation ); + + wxMouseEvent wxevent(wxEVT_LEFT_DOWN); + SetupMouseEvent( wxevent , touches, event ) ; + wxevent.m_x = pt.x; + wxevent.m_y = pt.y; + wxevent.SetEventObject( GetWXPeer() ) ; + //?wxevent.SetId( GetWXPeer()->GetId() ) ; + + GetWXPeer()->HandleWindowEvent(wxevent); + } +} - GetWXPeer()->HandleWindowEvent(wxevent); +void wxWidgetIPhoneImpl::controlAction(void* sender, wxUint32 controlEvent, WX_UIEvent rawEvent) +{ + if ( controlEvent == UIControlEventTouchUpInside ) + GetWXPeer()->OSXHandleClicked(0); } -void wxWidgetIPhoneImpl::touchUpInsideAction(void* sender, WX_UIEvent evt, WXWidget slf, void* _cmd) +void wxWidgetIPhoneImpl::controlTextDidChange() { + wxTextCtrl* wxpeer = wxDynamicCast((wxWindow*)GetWXPeer(),wxTextCtrl); + if ( wxpeer ) + { + wxCommandEvent event(wxEVT_COMMAND_TEXT_UPDATED, wxpeer->GetId()); + event.SetEventObject( wxpeer ); + event.SetString( wxpeer->GetValue() ); + wxpeer->HandleWindowEvent( event ); + } } // // Factory methods // -wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* WXUNUSED(parent), +wxWidgetImpl* wxWidgetImpl::CreateUserPane( wxWindowMac* wxpeer, wxWindowMac* WXUNUSED(parent), wxWindowID WXUNUSED(id), const wxPoint& pos, const wxSize& size, long WXUNUSED(style), long WXUNUSED(extraStyle)) { UIView* sv = (wxpeer->GetParent()->GetHandle() ); - + CGRect r = CGRectMake( pos.x, pos.y, size.x, size.y) ; // Rect bounds = wxMacGetBoundsForControl( wxpeer, pos , size ) ; wxUIView* v = [[wxUIView alloc] initWithFrame:r]; sv.clipsToBounds = YES; sv.contentMode = UIViewContentModeRedraw; sv.clearsContextBeforeDrawing = NO; - wxWidgetIPhoneImpl* c = new wxWidgetIPhoneImpl( wxpeer, v ); + wxWidgetIPhoneImpl* c = new wxWidgetIPhoneImpl( wxpeer, v, false, true ); return c; } -wxWidgetImpl* wxWidgetImpl::CreateContentView( wxNonOwnedWindow* now ) -{ - UIWindow* toplevelwindow = now->GetWXWindow(); - - wxUIContentView* contentview = [[wxUIContentView alloc] initWithFrame:[toplevelwindow bounds]]; - contentview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; - wxUIContentViewController* controller = [[wxUIContentViewController alloc] init]; - controller.view = contentview; - /* - UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation]; - // left orientation not yet implemented ! - if (orientation == UIInterfaceOrientationLandscapeRight ) - { - CGAffineTransform transform = v.transform; - - // Use the status bar frame to determine the center point of the window's content area. - CGRect statusBarFrame = [[UIApplication sharedApplication] statusBarFrame]; - CGRect bounds = CGRectMake(0, 0, statusBarFrame.size.height, statusBarFrame.origin.x); - CGPoint center = CGPointMake(bounds.size.height / 2.0, bounds.size.width / 2.0); - - // Set the center point of the view to the center point of the window's content area. - v.center = center; - - // Rotate the view 90 degrees around its new center point. - transform = CGAffineTransformRotate(transform, ( M_PI / 2.0)); - v.transform = transform; - } - */ - wxWidgetIPhoneImpl* impl = new wxWidgetIPhoneImpl( now, contentview, true ); - impl->InstallEventHandler(); - [toplevelwindow addSubview:contentview]; - return impl; -} -