1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/nonownedwnd.mm
3 // Purpose: non owned window for cocoa
4 // Author: DavidStefan Csomor
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 #include "wx/wxprec.h"
14 #include "wx/nonownedwnd.h"
17 #include "wx/dialog.h"
18 #include "wx/menuitem.h"
22 #include "wx/osx/private.h"
24 NSScreen* wxOSXGetMenuScreen()
26 if ( [NSScreen screens] == nil )
27 return [NSScreen mainScreen];
30 return [[NSScreen screens] objectAtIndex:0];
34 NSRect wxToNSRect( NSView* parent, const wxRect& r )
36 NSRect frame = parent ? [parent bounds] : [wxOSXGetMenuScreen() frame];
39 if ( parent == NULL || ![ parent isFlipped ] )
40 y = (int)(frame.size.height - ( r.y + r.height ));
41 return NSMakeRect(x, y, r.width , r.height);
44 wxRect wxFromNSRect( NSView* parent, const NSRect& rect )
46 NSRect frame = parent ? [parent bounds] : [wxOSXGetMenuScreen() frame];
47 int y = (int)rect.origin.y;
48 int x = (int)rect.origin.x;
49 if ( parent == NULL || ![ parent isFlipped ] )
50 y = (int)(frame.size.height - (rect.origin.y + rect.size.height));
51 return wxRect( x, y, (int)rect.size.width, (int)rect.size.height );
54 NSPoint wxToNSPoint( NSView* parent, const wxPoint& p )
56 NSRect frame = parent ? [parent bounds] : [wxOSXGetMenuScreen() frame];
59 if ( parent == NULL || ![ parent isFlipped ] )
60 y = (int)(frame.size.height - ( p.y ));
61 return NSMakePoint(x, y);
64 wxPoint wxFromNSPoint( NSView* parent, const NSPoint& p )
66 NSRect frame = parent ? [parent bounds] : [wxOSXGetMenuScreen() frame];
69 if ( parent == NULL || ![ parent isFlipped ] )
70 y = (int)(frame.size.height - ( p.y ));
71 return wxPoint( x, y);
74 bool shouldHandleSelector(SEL selector)
76 if (selector == @selector(noop:)
77 || selector == @selector(complete:)
78 || selector == @selector(deleteBackward:)
79 || selector == @selector(deleteForward:)
80 || selector == @selector(insertNewline:)
81 || selector == @selector(insertTab:)
82 || selector == @selector(insertBacktab:)
83 || selector == @selector(keyDown:)
84 || selector == @selector(keyUp:)
85 || selector == @selector(scrollPageUp:)
86 || selector == @selector(scrollPageDown:)
87 || selector == @selector(scrollToBeginningOfDocument:)
88 || selector == @selector(scrollToEndOfDocument:))
96 // wx category for NSWindow (our own and wrapped instances)
99 @interface NSWindow (wxNSWindowSupport)
101 - (wxNonOwnedWindowCocoaImpl*) WX_implementation;
103 - (bool) WX_filterSendEvent:(NSEvent *) event;
107 @implementation NSWindow (wxNSWindowSupport)
109 - (wxNonOwnedWindowCocoaImpl*) WX_implementation
111 return (wxNonOwnedWindowCocoaImpl*) wxNonOwnedWindowImpl::FindFromWXWindow( self );
114 // TODO in cocoa everything during a drag is sent to the NSWindow the mouse down occurred,
115 // this does not conform to the wx behaviour if the window is not captured, so try to resend
116 // or capture all wx mouse event handling at the tlw as we did for carbon
118 - (bool) WX_filterSendEvent:(NSEvent *) event
120 bool handled = false;
121 if ( ([event type] >= NSLeftMouseDown) && ([event type] <= NSMouseExited) )
123 WXEVENTREF formerEvent = wxTheApp == NULL ? NULL : wxTheApp->MacGetCurrentEvent();
124 WXEVENTHANDLERCALLREF formerHandler = wxTheApp == NULL ? NULL : wxTheApp->MacGetCurrentEventHandlerCallRef();
126 wxWindow* cw = wxWindow::GetCapture();
130 wxTheApp->MacSetCurrentEvent(event, NULL);
131 ((wxWidgetCocoaImpl*)cw->GetPeer())->DoHandleMouseEvent( event);
137 wxTheApp->MacSetCurrentEvent(formerEvent , formerHandler);
145 // wx native implementation
148 static NSResponder* s_nextFirstResponder = NULL;
150 @interface wxNSWindow : NSWindow
154 - (void) sendEvent:(NSEvent *)event;
155 - (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen *)screen;
156 - (void)noResponderFor: (SEL) selector;
157 - (BOOL)makeFirstResponder:(NSResponder *)aResponder;
160 @implementation wxNSWindow
162 - (void)sendEvent:(NSEvent *) event
164 if ( ![self WX_filterSendEvent: event] )
166 WXEVENTREF formerEvent = wxTheApp == NULL ? NULL : wxTheApp->MacGetCurrentEvent();
167 WXEVENTHANDLERCALLREF formerHandler = wxTheApp == NULL ? NULL : wxTheApp->MacGetCurrentEventHandlerCallRef();
170 wxTheApp->MacSetCurrentEvent(event, NULL);
172 [super sendEvent: event];
175 wxTheApp->MacSetCurrentEvent(formerEvent , formerHandler);
179 // The default implementation always moves the window back onto the screen,
180 // even when the programmer explicitly wants to hide it.
181 - (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen *)screen
187 - (void)doCommandBySelector:(SEL)selector
189 if (shouldHandleSelector(selector) &&
190 !(selector == @selector(cancel:) || selector == @selector(cancelOperation:)) )
191 [super doCommandBySelector:selector];
195 // NB: if we don't do this, all key downs that get handled lead to a NSBeep
196 - (void)noResponderFor: (SEL) selector
198 if (selector != @selector(keyDown:) && selector != @selector(keyUp:))
200 [super noResponderFor:selector];
204 // We need this for borderless windows, i.e. shaped windows or windows without
205 // a title bar. For more info, see:
206 // http://lists.apple.com/archives/cocoa-dev/2008/May/msg02091.html
207 - (BOOL)canBecomeKeyWindow
212 - (BOOL)makeFirstResponder:(NSResponder *)aResponder
214 s_nextFirstResponder = aResponder;
215 BOOL retval = [super makeFirstResponder:aResponder];
216 s_nextFirstResponder = nil;
222 @interface wxNSPanel : NSPanel
226 - (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen *)screen;
227 - (void)noResponderFor: (SEL) selector;
228 - (void)sendEvent:(NSEvent *)event;
229 - (BOOL)makeFirstResponder:(NSResponder *)aResponder;
232 @implementation wxNSPanel
234 - (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen *)screen
236 wxNonOwnedWindowCocoaImpl* impl = (wxNonOwnedWindowCocoaImpl*) wxNonOwnedWindowImpl::FindFromWXWindow( self );
237 if (impl && impl->IsFullScreen())
240 return [super constrainFrameRect:frameRect toScreen:screen];
243 - (BOOL)canBecomeKeyWindow
248 - (void)doCommandBySelector:(SEL)selector
250 if (shouldHandleSelector(selector))
251 [super doCommandBySelector:selector];
254 // NB: if we don't do this, it seems that all events that end here lead to a NSBeep
255 - (void)noResponderFor: (SEL) selector
257 if (selector != @selector(keyDown:) && selector != @selector(keyUp:))
259 [super noResponderFor:selector];
263 - (void)sendEvent:(NSEvent *) event
265 if ( ![self WX_filterSendEvent: event] )
267 WXEVENTREF formerEvent = wxTheApp == NULL ? NULL : wxTheApp->MacGetCurrentEvent();
268 WXEVENTHANDLERCALLREF formerHandler = wxTheApp == NULL ? NULL : wxTheApp->MacGetCurrentEventHandlerCallRef();
271 wxTheApp->MacSetCurrentEvent(event, NULL);
273 [super sendEvent: event];
276 wxTheApp->MacSetCurrentEvent(formerEvent , formerHandler);
280 - (BOOL)makeFirstResponder:(NSResponder *)aResponder
282 s_nextFirstResponder = aResponder;
283 BOOL retval = [super makeFirstResponder:aResponder];
284 s_nextFirstResponder = nil;
295 @interface wxNonOwnedWindowController : NSObject wxOSX_10_6_AND_LATER(<NSWindowDelegate>)
299 - (void)windowDidResize:(NSNotification *)notification;
300 - (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize;
301 - (void)windowDidResignKey:(NSNotification *)notification;
302 - (void)windowDidBecomeKey:(NSNotification *)notification;
303 - (void)windowDidMove:(NSNotification *)notification;
304 - (BOOL)windowShouldClose:(id)window;
305 - (BOOL)windowShouldZoom:(NSWindow *)window toFrame:(NSRect)newFrame;
309 extern int wxOSXGetIdFromSelector(SEL action );
311 @implementation wxNonOwnedWindowController
319 - (BOOL) triggerMenu:(SEL) action
321 wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar();
325 wxMenuItem* menuitem = mbar->FindItem(wxOSXGetIdFromSelector(action), &menu);
326 if ( menu != NULL && menuitem != NULL)
327 return menu->HandleCommandProcess(menuitem);
332 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
334 SEL action = [menuItem action];
336 wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar();
340 wxMenuItem* menuitem = mbar->FindItem(wxOSXGetIdFromSelector(action), &menu);
341 if ( menu != NULL && menuitem != NULL)
343 menu->HandleCommandUpdateStatus(menuitem);
344 return menuitem->IsEnabled();
350 - (void)undo:(id)sender
353 [self triggerMenu:_cmd];
356 - (void)redo:(id)sender
359 [self triggerMenu:_cmd];
362 - (void)cut:(id)sender
365 [self triggerMenu:_cmd];
368 - (void)copy:(id)sender
371 [self triggerMenu:_cmd];
374 - (void)paste:(id)sender
377 [self triggerMenu:_cmd];
380 - (void)delete:(id)sender
383 [self triggerMenu:_cmd];
386 - (void)selectAll:(id)sender
389 [self triggerMenu:_cmd];
392 - (BOOL)windowShouldClose:(id)nwindow
394 wxNonOwnedWindowCocoaImpl* windowimpl = [(NSWindow*) nwindow WX_implementation];
397 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
404 - (NSSize)windowWillResize:(NSWindow *)window
405 toSize:(NSSize)proposedFrameSize
407 NSRect frame = [window frame];
408 wxRect wxframe = wxFromNSRect( NULL, frame );
409 wxframe.SetWidth( (int)proposedFrameSize.width );
410 wxframe.SetHeight( (int)proposedFrameSize.height );
412 wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
415 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
418 wxpeer->HandleResizing( 0, &wxframe );
419 NSSize newSize = NSMakeSize(wxframe.GetWidth(), wxframe.GetHeight());
424 return proposedFrameSize;
427 - (void)windowDidResize:(NSNotification *)notification
429 NSWindow* window = (NSWindow*) [notification object];
430 wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
433 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
435 wxpeer->HandleResized(0);
439 - (void)windowDidMove:(NSNotification *)notification
441 wxNSWindow* window = (wxNSWindow*) [notification object];
442 wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
445 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
447 wxpeer->HandleMoved(0);
451 - (void)windowDidBecomeKey:(NSNotification *)notification
453 NSWindow* window = (NSWindow*) [notification object];
454 wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
457 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
459 wxpeer->HandleActivated(0, true);
463 - (void)windowDidResignKey:(NSNotification *)notification
465 NSWindow* window = (NSWindow*) [notification object];
466 wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
469 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
472 wxpeer->HandleActivated(0, false);
473 // as for wx the deactivation also means losing focus we
474 // must trigger this manually
475 [window makeFirstResponder:nil];
477 // TODO Remove if no problems arise with Popup Windows
479 // Needed for popup window since the firstResponder
480 // (focus in wx) doesn't change when this
481 // TLW becomes inactive.
482 wxFocusEvent event( wxEVT_KILL_FOCUS, wxpeer->GetId());
483 event.SetEventObject(wxpeer);
484 wxpeer->HandleWindowEvent(event);
490 - (id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(id)anObject
494 if ([anObject isKindOfClass:[wxNSTextField class]])
496 wxNSTextField* tf = (wxNSTextField*) anObject;
497 wxNSTextFieldEditor* editor = [tf fieldEditor];
500 editor = [[wxNSTextFieldEditor alloc] init];
501 [editor setFieldEditor:YES];
502 [tf setFieldEditor:editor];
507 else if ([anObject isKindOfClass:[wxNSComboBox class]])
509 wxNSComboBox * cb = (wxNSComboBox*) anObject;
510 wxNSTextFieldEditor* editor = [cb fieldEditor];
513 editor = [[wxNSTextFieldEditor alloc] init];
514 [editor setFieldEditor:YES];
515 [cb setFieldEditor:editor];
524 - (BOOL)windowShouldZoom:(NSWindow *)window toFrame:(NSRect)newFrame
526 wxUnusedVar(newFrame);
527 wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
530 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
531 wxMaximizeEvent event(wxpeer->GetId());
532 event.SetEventObject(wxpeer);
533 return !wxpeer->HandleWindowEvent(event);
540 IMPLEMENT_DYNAMIC_CLASS( wxNonOwnedWindowCocoaImpl , wxNonOwnedWindowImpl )
542 wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl( wxNonOwnedWindow* nonownedwnd) :
543 wxNonOwnedWindowImpl(nonownedwnd)
546 m_macFullScreenData = NULL;
549 wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl()
552 m_macFullScreenData = NULL;
555 wxNonOwnedWindowCocoaImpl::~wxNonOwnedWindowCocoaImpl()
557 if ( !m_wxPeer->IsNativeWindowWrapper() )
559 [m_macWindow setDelegate:nil];
561 // make sure we remove this first, otherwise the ref count will not lead to the
562 // native window's destruction
563 if ([m_macWindow parentWindow] != 0)
564 [[m_macWindow parentWindow] removeChildWindow: m_macWindow];
566 [m_macWindow release];
570 void wxNonOwnedWindowCocoaImpl::WillBeDestroyed()
572 if ( !m_wxPeer->IsNativeWindowWrapper() )
574 [m_macWindow setDelegate:nil];
578 void wxNonOwnedWindowCocoaImpl::Create( wxWindow* parent, const wxPoint& pos, const wxSize& size,
579 long style, long extraStyle, const wxString& WXUNUSED(name) )
581 static wxNonOwnedWindowController* controller = NULL;
584 controller =[[wxNonOwnedWindowController alloc] init];
587 int windowstyle = NSBorderlessWindowMask;
589 if ( style & wxFRAME_TOOL_WINDOW || ( style & wxPOPUP_WINDOW ) ||
590 GetWXPeer()->GetExtraStyle() & wxTOPLEVEL_EX_DIALOG )
591 m_macWindow = [wxNSPanel alloc];
593 m_macWindow = [wxNSWindow alloc];
595 [m_macWindow setAcceptsMouseMovedEvents:YES];
597 CGWindowLevel level = kCGNormalWindowLevel;
599 if ( style & wxFRAME_TOOL_WINDOW )
601 windowstyle |= NSUtilityWindowMask;
603 else if ( ( style & wxPOPUP_WINDOW ) )
605 level = kCGPopUpMenuWindowLevel;
607 else if ( ( style & wxFRAME_DRAWER ) )
610 wclass = kDrawerWindowClass;
614 if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) ||
615 ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) || ( style & wxCAPTION ) )
617 windowstyle |= NSTitledWindowMask ;
618 if ( ( style & wxMINIMIZE_BOX ) )
619 windowstyle |= NSMiniaturizableWindowMask ;
621 if ( ( style & wxMAXIMIZE_BOX ) )
622 windowstyle |= NSResizableWindowMask ;
624 if ( ( style & wxCLOSE_BOX) )
625 windowstyle |= NSClosableWindowMask ;
628 if ( ( style & wxRESIZE_BORDER ) )
629 windowstyle |= NSResizableWindowMask ;
631 if ( extraStyle & wxFRAME_EX_METAL)
632 windowstyle |= NSTexturedBackgroundWindowMask;
634 if ( ( style & wxFRAME_FLOAT_ON_PARENT ) || ( style & wxFRAME_TOOL_WINDOW ) )
635 level = kCGFloatingWindowLevel;
637 if ( ( style & wxSTAY_ON_TOP ) )
638 level = kCGUtilityWindowLevel;
640 NSRect r = wxToNSRect( NULL, wxRect( pos, size) );
642 r = [NSWindow contentRectForFrameRect:r styleMask:windowstyle];
644 [m_macWindow initWithContentRect:r
645 styleMask:windowstyle
646 backing:NSBackingStoreBuffered
650 // if we just have a title bar with no buttons needed, hide them
651 if ( (windowstyle & NSTitledWindowMask) &&
652 !(style & wxCLOSE_BOX) && !(style & wxMAXIMIZE_BOX) && !(style & wxMINIMIZE_BOX) )
654 [[m_macWindow standardWindowButton:NSWindowZoomButton] setHidden:YES];
655 [[m_macWindow standardWindowButton:NSWindowCloseButton] setHidden:YES];
656 [[m_macWindow standardWindowButton:NSWindowMiniaturizeButton] setHidden:YES];
659 // If the parent is modal, windows with wxFRAME_FLOAT_ON_PARENT style need
660 // to be in kCGUtilityWindowLevel and not kCGFloatingWindowLevel to stay
662 wxDialog * const parentDialog = wxDynamicCast(parent, wxDialog);
663 if (parentDialog && parentDialog->IsModal())
665 if (level == kCGFloatingWindowLevel)
667 level = kCGUtilityWindowLevel;
670 // Cocoa's modal loop does not process other windows by default, but
671 // don't call this on normal window levels so nested modal dialogs will
672 // still behave modally.
673 if (level != kCGNormalWindowLevel)
675 if ([m_macWindow isKindOfClass:[NSPanel class]])
677 [(NSPanel*)m_macWindow setWorksWhenModal:YES];
682 [m_macWindow setLevel:level];
683 m_macWindowLevel = level;
685 [m_macWindow setDelegate:controller];
687 if ( ( style & wxFRAME_SHAPED) )
689 [m_macWindow setOpaque:NO];
690 [m_macWindow setAlphaValue:1.0];
693 if ( !(style & wxFRAME_TOOL_WINDOW) )
694 [m_macWindow setHidesOnDeactivate:NO];
697 void wxNonOwnedWindowCocoaImpl::Create( wxWindow* WXUNUSED(parent), WXWindow nativeWindow )
699 m_macWindow = nativeWindow;
702 WXWindow wxNonOwnedWindowCocoaImpl::GetWXWindow() const
707 void wxNonOwnedWindowCocoaImpl::Raise()
709 [m_macWindow makeKeyAndOrderFront:nil];
712 void wxNonOwnedWindowCocoaImpl::Lower()
714 [m_macWindow orderWindow:NSWindowBelow relativeTo:0];
717 void wxNonOwnedWindowCocoaImpl::ShowWithoutActivating()
719 [m_macWindow orderFront:nil];
720 [[m_macWindow contentView] setNeedsDisplay: YES];
723 bool wxNonOwnedWindowCocoaImpl::Show(bool show)
727 wxNonOwnedWindow* wxpeer = GetWXPeer();
730 // add to parent window before showing
731 wxDialog * const dialog = wxDynamicCast(wxpeer, wxDialog);
732 if ( wxpeer->GetParent() && dialog && dialog->IsModal())
734 NSView * parentView = wxpeer->GetParent()->GetPeer()->GetWXWidget();
737 NSWindow* parentNSWindow = [parentView window];
738 if ( parentNSWindow )
739 [parentNSWindow addChildWindow:m_macWindow ordered:NSWindowAbove];
743 if (!(wxpeer->GetWindowStyle() & wxFRAME_TOOL_WINDOW))
744 [m_macWindow makeKeyAndOrderFront:nil];
746 [m_macWindow orderFront:nil];
748 [[m_macWindow contentView] setNeedsDisplay: YES];
752 // avoid propagation of orderOut to parent
753 if ([m_macWindow parentWindow] != 0)
754 [[m_macWindow parentWindow] removeChildWindow: m_macWindow];
755 [m_macWindow orderOut:nil];
760 bool wxNonOwnedWindowCocoaImpl::ShowWithEffect(bool show,
764 return wxWidgetCocoaImpl::
765 ShowViewOrWindowWithEffect(m_wxPeer, show, effect, timeout);
768 void wxNonOwnedWindowCocoaImpl::Update()
770 [m_macWindow displayIfNeeded];
773 bool wxNonOwnedWindowCocoaImpl::SetTransparent(wxByte alpha)
775 [m_macWindow setAlphaValue:(CGFloat) alpha/255.0];
779 bool wxNonOwnedWindowCocoaImpl::SetBackgroundColour(const wxColour& col )
781 [m_macWindow setBackgroundColor:[NSColor colorWithCalibratedRed:(CGFloat) (col.Red() / 255.0)
782 green:(CGFloat) (col.Green() / 255.0)
783 blue:(CGFloat) (col.Blue() / 255.0)
784 alpha:(CGFloat) (col.Alpha() / 255.0)]];
788 void wxNonOwnedWindowCocoaImpl::SetExtraStyle( long exStyle )
792 bool metal = exStyle & wxFRAME_EX_METAL ;
793 int windowStyle = [ m_macWindow styleMask];
794 if ( metal && !(windowStyle & NSTexturedBackgroundWindowMask) )
796 wxFAIL_MSG( wxT("Metal Style cannot be changed after creation") );
798 else if ( !metal && (windowStyle & NSTexturedBackgroundWindowMask ) )
800 wxFAIL_MSG( wxT("Metal Style cannot be changed after creation") );
805 void wxNonOwnedWindowCocoaImpl::SetWindowStyleFlag( long style )
807 // don't mess with native wrapped windows, they might throw an exception when their level is changed
808 if (!m_wxPeer->IsNativeWindowWrapper() && m_macWindow)
810 CGWindowLevel level = kCGNormalWindowLevel;
812 if (style & wxSTAY_ON_TOP)
813 level = kCGUtilityWindowLevel;
814 else if (( style & wxFRAME_FLOAT_ON_PARENT ) || ( style & wxFRAME_TOOL_WINDOW ))
815 level = kCGFloatingWindowLevel;
817 [m_macWindow setLevel: level];
818 m_macWindowLevel = level;
822 bool wxNonOwnedWindowCocoaImpl::SetBackgroundStyle(wxBackgroundStyle style)
824 if ( style == wxBG_STYLE_TRANSPARENT )
826 [m_macWindow setOpaque:NO];
827 [m_macWindow setBackgroundColor:[NSColor clearColor]];
833 bool wxNonOwnedWindowCocoaImpl::CanSetTransparent()
838 void wxNonOwnedWindowCocoaImpl::MoveWindow(int x, int y, int width, int height)
840 NSRect r = wxToNSRect( NULL, wxRect(x,y,width, height) );
841 // do not trigger refreshes upon invisible and possible partly created objects
842 [m_macWindow setFrame:r display:GetWXPeer()->IsShownOnScreen()];
845 void wxNonOwnedWindowCocoaImpl::GetPosition( int &x, int &y ) const
847 wxRect r = wxFromNSRect( NULL, [m_macWindow frame] );
852 void wxNonOwnedWindowCocoaImpl::GetSize( int &width, int &height ) const
854 NSRect rect = [m_macWindow frame];
855 width = (int)rect.size.width;
856 height = (int)rect.size.height;
859 void wxNonOwnedWindowCocoaImpl::GetContentArea( int& left, int &top, int &width, int &height ) const
861 NSRect rect = [[m_macWindow contentView] frame];
862 left = (int)rect.origin.x;
863 top = (int)rect.origin.y;
864 width = (int)rect.size.width;
865 height = (int)rect.size.height;
868 bool wxNonOwnedWindowCocoaImpl::SetShape(const wxRegion& WXUNUSED(region))
870 [m_macWindow setOpaque:NO];
871 [m_macWindow setBackgroundColor:[NSColor clearColor]];
876 void wxNonOwnedWindowCocoaImpl::SetTitle( const wxString& title, wxFontEncoding encoding )
878 [m_macWindow setTitle:wxCFStringRef( title , encoding ).AsNSString()];
881 bool wxNonOwnedWindowCocoaImpl::IsMaximized() const
883 if (([m_macWindow styleMask] & NSResizableWindowMask) != 0)
885 return [m_macWindow isZoomed];
889 NSRect rectScreen = [[NSScreen mainScreen] visibleFrame];
890 NSRect rectWindow = [m_macWindow frame];
891 return (rectScreen.origin.x == rectWindow.origin.x &&
892 rectScreen.origin.y == rectWindow.origin.y &&
893 rectScreen.size.width == rectWindow.size.width &&
894 rectScreen.size.height == rectWindow.size.height);
898 bool wxNonOwnedWindowCocoaImpl::IsIconized() const
900 return [m_macWindow isMiniaturized];
903 void wxNonOwnedWindowCocoaImpl::Iconize( bool iconize )
906 [m_macWindow miniaturize:nil];
908 [m_macWindow deminiaturize:nil];
911 void wxNonOwnedWindowCocoaImpl::Maximize(bool WXUNUSED(maximize))
913 [m_macWindow zoom:nil];
917 // http://cocoadevcentral.com/articles/000028.php
921 NSUInteger m_formerStyleMask;
923 NSRect m_formerFrame;
926 bool wxNonOwnedWindowCocoaImpl::IsFullScreen() const
928 return m_macFullScreenData != NULL ;
931 bool wxNonOwnedWindowCocoaImpl::ShowFullScreen(bool show, long WXUNUSED(style))
935 FullScreenData *data = (FullScreenData *)m_macFullScreenData ;
937 data = new FullScreenData();
939 m_macFullScreenData = data ;
940 data->m_formerLevel = [m_macWindow level];
941 data->m_formerFrame = [m_macWindow frame];
942 data->m_formerStyleMask = [m_macWindow styleMask];
944 // CGDisplayCapture( kCGDirectMainDisplay );
945 //[m_macWindow setLevel:NSMainMenuWindowLevel+1/*CGShieldingWindowLevel()*/];
947 NSRect screenframe = [[NSScreen mainScreen] frame];
948 NSRect frame = NSMakeRect (0, 0, 100, 100);
951 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
952 if ( [ m_macWindow respondsToSelector:@selector(setStyleMask:) ] )
953 [m_macWindow setStyleMask:data->m_formerStyleMask & ~ NSResizableWindowMask];
956 contentRect = [NSWindow contentRectForFrameRect: frame
957 styleMask: [m_macWindow styleMask]];
958 screenframe.origin.y += (frame.origin.y - contentRect.origin.y);
959 screenframe.size.height += (frame.size.height - contentRect.size.height);
960 [m_macWindow setFrame:screenframe display:YES];
962 SetSystemUIMode(kUIModeAllHidden,
963 kUIOptionDisableAppleMenu
965 | kUIOptionDisableProcessSwitch
966 | kUIOptionDisableForceQuit
969 else if ( m_macFullScreenData != NULL )
971 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
973 // CGDisplayRelease( kCGDirectMainDisplay );
974 // [m_macWindow setLevel:data->m_formerLevel];
977 [m_macWindow setFrame:data->m_formerFrame display:YES];
978 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
979 if ( [ m_macWindow respondsToSelector:@selector(setStyleMask:) ] )
980 [m_macWindow setStyleMask:data->m_formerStyleMask];
983 m_macFullScreenData = NULL ;
985 SetSystemUIMode(kUIModeNormal, 0);
991 void wxNonOwnedWindowCocoaImpl::RequestUserAttention(int flagsWX)
993 NSRequestUserAttentionType flagsOSX;
996 case wxUSER_ATTENTION_INFO:
997 flagsOSX = NSInformationalRequest;
1000 case wxUSER_ATTENTION_ERROR:
1001 flagsOSX = NSCriticalRequest;
1005 wxFAIL_MSG( "invalid RequestUserAttention() flags" );
1009 [NSApp requestUserAttention:flagsOSX];
1012 void wxNonOwnedWindowCocoaImpl::ScreenToWindow( int *x, int *y )
1014 wxPoint p((x ? *x : 0), (y ? *y : 0) );
1015 NSPoint nspt = wxToNSPoint( NULL, p );
1016 nspt = [m_macWindow convertScreenToBase:nspt];
1017 nspt = [[m_macWindow contentView] convertPoint:nspt fromView:nil];
1018 p = wxFromNSPoint([m_macWindow contentView], nspt);
1025 void wxNonOwnedWindowCocoaImpl::WindowToScreen( int *x, int *y )
1027 wxPoint p((x ? *x : 0), (y ? *y : 0) );
1028 NSPoint nspt = wxToNSPoint( [m_macWindow contentView], p );
1029 nspt = [[m_macWindow contentView] convertPoint:nspt toView:nil];
1030 nspt = [m_macWindow convertBaseToScreen:nspt];
1031 p = wxFromNSPoint( NULL, nspt);
1038 bool wxNonOwnedWindowCocoaImpl::IsActive()
1040 return [m_macWindow isKeyWindow];
1043 void wxNonOwnedWindowCocoaImpl::SetModified(bool modified)
1045 [m_macWindow setDocumentEdited:modified];
1048 bool wxNonOwnedWindowCocoaImpl::IsModified() const
1050 return [m_macWindow isDocumentEdited];
1053 void wxNonOwnedWindowCocoaImpl::SetRepresentedFilename(const wxString& filename)
1055 [m_macWindow setRepresentedFilename:wxCFStringRef(filename).AsNSString()];
1058 void wxNonOwnedWindowCocoaImpl::RestoreWindowLevel()
1060 if ( [m_macWindow level] != m_macWindowLevel )
1061 [m_macWindow setLevel:m_macWindowLevel];
1064 WX_NSResponder wxNonOwnedWindowCocoaImpl::GetNextFirstResponder()
1066 return s_nextFirstResponder;
1074 wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, WXWindow nativeWindow)
1076 wxNonOwnedWindowCocoaImpl* now = new wxNonOwnedWindowCocoaImpl( wxpeer );
1077 now->Create( parent, nativeWindow );
1081 wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, const wxPoint& pos, const wxSize& size,
1082 long style, long extraStyle, const wxString& name )
1084 wxNonOwnedWindowImpl* now = new wxNonOwnedWindowCocoaImpl( wxpeer );
1085 now->Create( parent, pos, size, style , extraStyle, name );