From 09a9eb2069fe15a010efebe0b5378376a2c337fa Mon Sep 17 00:00:00 2001 From: Kevin Ollivier Date: Sun, 8 Mar 2009 04:15:58 +0000 Subject: [PATCH] Fixes needed to get transient popup windows working, also implement SetFont for OS X Cocoa for classes that support it. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@59424 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/osx/cocoa/private.h | 1 + src/common/popupcmn.cpp | 2 +- src/osx/cocoa/nonownedwnd.mm | 29 +++++++++++++++++++++++------ src/osx/cocoa/window.mm | 32 +++++++++++++++++++++++++++++--- 4 files changed, 54 insertions(+), 10 deletions(-) diff --git a/include/wx/osx/cocoa/private.h b/include/wx/osx/cocoa/private.h index 2e7de760b9..daea56363d 100644 --- a/include/wx/osx/cocoa/private.h +++ b/include/wx/osx/cocoa/private.h @@ -141,6 +141,7 @@ public : virtual void keyEvent(WX_NSEvent event, WXWidget slf, void* _cmd); virtual void insertText(NSString* text, WXWidget slf, void* _cmd); virtual bool performKeyEquivalent(WX_NSEvent event, WXWidget slf, void* _cmd); + virtual bool acceptsFirstResponder(WXWidget slf, void* _cmd); virtual bool becomeFirstResponder(WXWidget slf, void* _cmd); virtual bool resignFirstResponder(WXWidget slf, void* _cmd); virtual void resetCursorRects(WXWidget slf, void* _cmd); diff --git a/src/common/popupcmn.cpp b/src/common/popupcmn.cpp index 4f04391aa0..5b1a49504b 100644 --- a/src/common/popupcmn.cpp +++ b/src/common/popupcmn.cpp @@ -291,7 +291,7 @@ void wxPopupTransientWindow::Popup(wxWindow *winFocus) m_focus->SetFocus(); } -#if defined( __WXMSW__ ) || defined( __WXMAC__) +#if defined( __WXMSW__ ) || (defined( __WXMAC__) && wxOSX_USE_CARBON) // MSW doesn't allow to set focus to the popup window, but we need to // subclass the window which has the focus, and not winFocus passed in or // otherwise everything else breaks down diff --git a/src/osx/cocoa/nonownedwnd.mm b/src/osx/cocoa/nonownedwnd.mm index aa35ba8d4d..bdee26db1a 100644 --- a/src/osx/cocoa/nonownedwnd.mm +++ b/src/osx/cocoa/nonownedwnd.mm @@ -116,6 +116,11 @@ typedef void (*wxOSX_NoResponderHandlerPtr)(NSView* self, SEL _cmd, SEL selector impl = theImplementation; } +- (BOOL)canBecomeKeyWindow +{ + return YES; +} + - (wxNonOwnedWindowCocoaImpl*) implementation { return impl; @@ -144,8 +149,8 @@ typedef void (*wxOSX_NoResponderHandlerPtr)(NSView* self, SEL _cmd, SEL selector - (void)windowDidResize:(NSNotification *)notification; - (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize; -- (void)windowDidResignMain:(NSNotification *)notification; -- (void)windowDidBecomeMain:(NSNotification *)notification; +- (void)windowDidResignKey:(NSNotification *)notification; +- (void)windowDidBecomeKey:(NSNotification *)notification; - (void)windowDidMove:(NSNotification *)notification; - (BOOL)windowShouldClose:(id)window; @@ -219,7 +224,7 @@ typedef void (*wxOSX_NoResponderHandlerPtr)(NSView* self, SEL _cmd, SEL selector } } -- (void)windowDidBecomeMain:(NSNotification *)notification +- (void)windowDidBecomeKey:(NSNotification *)notification { wxNSWindow* window = (wxNSWindow*) [notification object]; wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation]; @@ -231,7 +236,7 @@ typedef void (*wxOSX_NoResponderHandlerPtr)(NSView* self, SEL _cmd, SEL selector } } -- (void)windowDidResignMain:(NSNotification *)notification +- (void)windowDidResignKey:(NSNotification *)notification { wxNSWindow* window = (wxNSWindow*) [notification object]; wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation]; @@ -239,7 +244,15 @@ typedef void (*wxOSX_NoResponderHandlerPtr)(NSView* self, SEL _cmd, SEL selector { wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer(); if ( wxpeer ) + { wxpeer->HandleActivated(0, false); + // Needed for popup window since the firstResponder + // (focus in wx) doesn't change when this + // TLW becomes inactive. + wxFocusEvent event( wxEVT_KILL_FOCUS, wxpeer->GetId()); + event.SetEventObject(wxpeer); + wxpeer->HandleWindowEvent(event); + } } } @@ -283,8 +296,11 @@ long style, long extraStyle, const wxString& name ) int windowstyle = NSBorderlessWindowMask; - if ( style & wxFRAME_TOOL_WINDOW ) + if ( style & wxFRAME_TOOL_WINDOW || ( style & wxPOPUP_WINDOW ) || + GetWXPeer()->GetExtraStyle() & wxTOPLEVEL_EX_DIALOG ) + { m_macWindow = [wxNSPanel alloc]; + } else m_macWindow = [wxNSWindow alloc]; @@ -383,7 +399,8 @@ long style, long extraStyle, const wxString& name ) [m_macWindow setAcceptsMouseMovedEvents: YES]; - // [m_macWindow makeKeyAndOrderFront:nil]; + if ( ( style & wxPOPUP_WINDOW ) ) + [m_macWindow makeKeyAndOrderFront:nil]; } diff --git a/src/osx/cocoa/window.mm b/src/osx/cocoa/window.mm index 30f7692e91..f26fa31831 100644 --- a/src/osx/cocoa/window.mm +++ b/src/osx/cocoa/window.mm @@ -535,6 +535,15 @@ BOOL wxOSX_performKeyEquivalent(NSView* self, SEL _cmd, NSEvent *event) return impl->performKeyEquivalent(event, self, _cmd); } +BOOL wxOSX_acceptsFirstResponder(NSView* self, SEL _cmd) +{ + wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); + if (impl == NULL) + return NO; + + return impl->acceptsFirstResponder(self, _cmd); +} + BOOL wxOSX_becomeFirstResponder(NSView* self, SEL _cmd) { wxWidgetCocoaImpl* impl = (wxWidgetCocoaImpl* ) wxWidgetImpl::FindFromWXWidget( self ); @@ -780,6 +789,17 @@ bool wxWidgetCocoaImpl::performKeyEquivalent(WX_NSEvent event, WXWidget slf, voi return superimpl(slf, (SEL)_cmd, event); } +bool wxWidgetCocoaImpl::acceptsFirstResponder(WXWidget slf, void *_cmd) +{ + // Make sure wxWindows can receive focus by default like they do for other ports. + + // FIXME: It'd be nice to have some way to tie this in with AcceptsFocus + // but that currently causes loops because the call to m_peer->CanFocus() + // ends up calling this method again. Don't know how to avoid it without + // making a setter method for focus. + return YES; +} + bool wxWidgetCocoaImpl::becomeFirstResponder(WXWidget slf, void *_cmd) { wxOSX_FocusHandlerPtr superimpl = (wxOSX_FocusHandlerPtr) [[slf superclass] instanceMethodForSelector:(SEL)_cmd]; @@ -943,6 +963,7 @@ void wxOSXCocoaClassAddWXMethods(Class c) wxOSX_CLASS_ADD_METHOD(c, @selector(performKeyEquivalent:), (IMP) wxOSX_performKeyEquivalent, "v@:@" ) + wxOSX_CLASS_ADD_METHOD(c, @selector(acceptsFirstResponder), (IMP) wxOSX_acceptsFirstResponder, "c@:" ) wxOSX_CLASS_ADD_METHOD(c, @selector(becomeFirstResponder), (IMP) wxOSX_becomeFirstResponder, "c@:" ) wxOSX_CLASS_ADD_METHOD(c, @selector(resignFirstResponder), (IMP) wxOSX_resignFirstResponder, "c@:" ) wxOSX_CLASS_ADD_METHOD(c, @selector(resetCursorRects), (IMP) wxOSX_resetCursorRects, "v@:" ) @@ -1256,7 +1277,7 @@ void wxWidgetCocoaImpl::SetupTabs( const wxNotebook& notebook) void wxWidgetCocoaImpl::GetBestRect( wxRect *r ) const { r->x = r->y = r->width = r->height = 0; -// if ( [m_osxView isKindOfClass:[NSControl class]] ) + if ( [m_osxView respondsToSelector:@selector(sizeToFit)] ) { NSRect former = [m_osxView frame]; @@ -1319,9 +1340,14 @@ void wxWidgetCocoaImpl::SetControlSize( wxWindowVariant variant ) [m_osxView setControlSize:size]; } -void wxWidgetCocoaImpl::SetFont(wxFont const&, wxColour const&, long, bool) +void wxWidgetCocoaImpl::SetFont(wxFont const& font, wxColour const&, long, bool) { - // TODO + if ([m_osxView respondsToSelector:@selector(setFont:)]) +#if wxOSX_USE_CORE_TEXT + [m_osxView setFont: (CTFontRef)font.MacGetCTFont()]; +#else + +#endif } void wxWidgetCocoaImpl::InstallEventHandler( WXWidget control ) -- 2.45.2