1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/nonownedwnd.mm
3 // Purpose: non owned window for cocoa
4 // Author: DavidStefan Csomor
7 // RCS-ID: $Id: nonownedwnd.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/nonownedwnd.h"
18 #include "wx/osx/private.h"
20 NSRect wxToNSRect( NSView* parent, const wxRect& r )
22 NSRect frame = parent ? [parent bounds] : [[NSScreen mainScreen] frame];
25 if ( parent == NULL || ![ parent isFlipped ] )
26 y = frame.size.height - ( r.y + r.height );
27 return NSMakeRect(x, y, r.width , r.height);
30 wxRect wxFromNSRect( NSView* parent, const NSRect& rect )
32 NSRect frame = parent ? [parent bounds] : [[NSScreen mainScreen] frame];
33 int y = rect.origin.y;
34 int x = rect.origin.x;
35 if ( parent == NULL || ![ parent isFlipped ] )
36 y = frame.size.height - (rect.origin.y + rect.size.height);
37 return wxRect( x, y, rect.size.width, rect.size.height );
40 NSPoint wxToNSPoint( NSView* parent, const wxPoint& p )
42 NSRect frame = parent ? [parent bounds] : [[NSScreen mainScreen] frame];
45 if ( parent == NULL || ![ parent isFlipped ] )
46 y = frame.size.height - ( p.y );
47 return NSMakePoint(x, y);
50 wxPoint wxFromNSPoint( NSView* parent, const NSPoint& p )
52 NSRect frame = parent ? [parent bounds] : [[NSScreen mainScreen] frame];
55 if ( parent == NULL || ![ parent isFlipped ] )
56 y = frame.size.height - ( p.y );
57 return wxPoint( x, y);
61 // wx native implementation classes
64 typedef void (*wxOSX_NoResponderHandlerPtr)(NSView* self, SEL _cmd, SEL selector);
66 @interface wxNSWindow : NSWindow
69 wxNonOwnedWindowCocoaImpl* impl;
72 - (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation;
73 - (wxNonOwnedWindowCocoaImpl*) implementation;
74 - (void)noResponderFor: (SEL) selector;
77 @implementation wxNSWindow
79 - (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation
81 impl = theImplementation;
84 - (wxNonOwnedWindowCocoaImpl*) implementation
89 // NB: if we don't do this, all key downs that get handled lead to a NSBeep
90 - (void)noResponderFor: (SEL) selector
92 if (selector != @selector(keyDown:))
94 wxOSX_NoResponderHandlerPtr superimpl = (wxOSX_NoResponderHandlerPtr) [[self superclass] instanceMethodForSelector:@selector(noResponderFor:)];
95 superimpl(self, @selector(noResponderFor:), selector);
101 @interface wxNSPanel : NSPanel
104 wxNonOwnedWindowCocoaImpl* impl;
107 - (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation;
108 - (wxNonOwnedWindowCocoaImpl*) implementation;
109 - (void)noResponderFor: (SEL) selector;
112 @implementation wxNSPanel
114 - (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation
116 impl = theImplementation;
119 - (wxNonOwnedWindowCocoaImpl*) implementation
124 // NB: if we don't do this, all key downs that get handled lead to a NSBeep
125 - (void)noResponderFor: (SEL) selector
127 if (selector != @selector(keyDown:))
129 wxOSX_NoResponderHandlerPtr superimpl = (wxOSX_NoResponderHandlerPtr) [[self superclass] instanceMethodForSelector:@selector(noResponderFor:)];
130 superimpl(self, @selector(noResponderFor:), selector);
141 @interface wxNonOwnedWindowController : NSObject
145 - (void)windowDidResize:(NSNotification *)notification;
146 - (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize;
147 - (void)windowDidResignMain:(NSNotification *)notification;
148 - (void)windowDidBecomeMain:(NSNotification *)notification;
149 - (void)windowDidMove:(NSNotification *)notification;
150 - (BOOL)windowShouldClose:(id)window;
154 @implementation wxNonOwnedWindowController
162 - (BOOL)windowShouldClose:(id)nwindow
164 wxNSWindow* window = (wxNSWindow*) nwindow;
165 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
168 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
175 - (NSSize)windowWillResize:(NSWindow *)win
176 toSize:(NSSize)proposedFrameSize
178 NSRect frame = [win frame];
179 wxRect wxframe = wxFromNSRect( NULL, frame );
180 wxframe.SetWidth( proposedFrameSize.width );
181 wxframe.SetHeight( proposedFrameSize.height );
182 wxNSWindow* window = (wxNSWindow*) win;
183 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
186 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
189 wxpeer->HandleResizing( 0, &wxframe );
190 NSSize newSize = NSMakeSize(wxframe.GetWidth(), wxframe.GetHeight());
195 return proposedFrameSize;
198 - (void)windowDidResize:(NSNotification *)notification
200 wxNSWindow* window = (wxNSWindow*) [notification object];
201 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
204 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
206 wxpeer->HandleResized(0);
210 - (void)windowDidMove:(NSNotification *)notification
212 wxNSWindow* window = (wxNSWindow*) [notification object];
213 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
216 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
218 wxpeer->HandleMoved(0);
222 - (void)windowDidBecomeMain:(NSNotification *)notification
224 wxNSWindow* window = (wxNSWindow*) [notification object];
225 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
228 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
230 wxpeer->HandleActivated(0, true);
234 - (void)windowDidResignMain:(NSNotification *)notification
236 wxNSWindow* window = (wxNSWindow*) [notification object];
237 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
240 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
242 wxpeer->HandleActivated(0, false);
248 IMPLEMENT_DYNAMIC_CLASS( wxNonOwnedWindowCocoaImpl , wxNonOwnedWindowImpl )
250 wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl( wxNonOwnedWindow* nonownedwnd) :
251 wxNonOwnedWindowImpl(nonownedwnd)
254 m_macFullScreenData = NULL;
257 wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl()
260 m_macFullScreenData = NULL;
263 wxNonOwnedWindowCocoaImpl::~wxNonOwnedWindowCocoaImpl()
265 [m_macWindow setImplementation:nil];
266 [m_macWindow setDelegate:nil];
267 [m_macWindow release];
270 void wxNonOwnedWindowCocoaImpl::Destroy()
272 wxPendingDelete.Append( new wxDeferredObjectDeleter( this ) );
275 void wxNonOwnedWindowCocoaImpl::Create( wxWindow* parent, const wxPoint& pos, const wxSize& size,
276 long style, long extraStyle, const wxString& name )
278 static wxNonOwnedWindowController* controller = NULL;
281 controller =[[wxNonOwnedWindowController alloc] init];
284 int windowstyle = NSBorderlessWindowMask;
286 if ( style & wxFRAME_TOOL_WINDOW )
287 m_macWindow = [wxNSPanel alloc];
289 m_macWindow = [wxNSWindow alloc];
291 CGWindowLevel level = kCGNormalWindowLevel;
293 if ( style & wxFRAME_TOOL_WINDOW )
295 windowstyle |= NSUtilityWindowMask;
296 if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) ||
297 ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) )
299 windowstyle |= NSTitledWindowMask ;
302 else if ( ( style & wxPOPUP_WINDOW ) )
304 level = kCGPopUpMenuWindowLevel;
306 if ( ( style & wxBORDER_NONE ) )
308 wclass = kHelpWindowClass ; // has no border
309 attr |= kWindowNoShadowAttribute;
313 wclass = kPlainWindowClass ; // has a single line border, it will have to do for now
317 else if ( ( style & wxCAPTION ) )
319 windowstyle |= NSTitledWindowMask ;
321 else if ( ( style & wxFRAME_DRAWER ) )
324 wclass = kDrawerWindowClass;
329 // set these even if we have no title, otherwise the controls won't be visible
330 if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) ||
331 ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) )
333 windowstyle |= NSTitledWindowMask ;
336 else if ( ( style & wxNO_BORDER ) )
338 wclass = kSimpleWindowClass ;
342 wclass = kPlainWindowClass ;
347 if ( windowstyle & NSTitledWindowMask )
349 if ( ( style & wxMINIMIZE_BOX ) )
350 windowstyle |= NSMiniaturizableWindowMask ;
352 if ( ( style & wxMAXIMIZE_BOX ) )
353 windowstyle |= NSResizableWindowMask ; // TODO showing ZOOM ?
355 if ( ( style & wxRESIZE_BORDER ) )
356 windowstyle |= NSResizableWindowMask ;
358 if ( ( style & wxCLOSE_BOX) )
359 windowstyle |= NSClosableWindowMask ;
361 if ( extraStyle & wxFRAME_EX_METAL)
362 windowstyle |= NSTexturedBackgroundWindowMask;
364 if ( ( style & wxFRAME_FLOAT_ON_PARENT ) || ( style & wxFRAME_TOOL_WINDOW ) )
365 level = kCGFloatingWindowLevel;
367 if ( ( style & wxSTAY_ON_TOP ) )
368 level = kCGUtilityWindowLevel;
370 NSRect r = wxToNSRect( NULL, wxRect( pos, size) );
372 [m_macWindow setImplementation:this];
374 [m_macWindow initWithContentRect:r
375 styleMask:windowstyle
376 backing:NSBackingStoreBuffered
380 [m_macWindow setLevel:level];
382 [m_macWindow setDelegate:controller];
384 [m_macWindow setAcceptsMouseMovedEvents: YES];
386 // [m_macWindow makeKeyAndOrderFront:nil];
390 WXWindow wxNonOwnedWindowCocoaImpl::GetWXWindow() const
395 void wxNonOwnedWindowCocoaImpl::Raise()
397 [m_macWindow orderWindow:NSWindowAbove relativeTo:0];
400 void wxNonOwnedWindowCocoaImpl::Lower()
402 [m_macWindow orderWindow:NSWindowBelow relativeTo:0];
405 bool wxNonOwnedWindowCocoaImpl::Show(bool show)
409 [m_macWindow orderFront:nil];
410 [[m_macWindow contentView] setNeedsDisplay:YES];
413 [m_macWindow orderOut:nil];
417 bool wxNonOwnedWindowCocoaImpl::ShowWithEffect(bool show, wxShowEffect effect, unsigned timeout)
422 void wxNonOwnedWindowCocoaImpl::Update()
424 [m_macWindow displayIfNeeded];
427 bool wxNonOwnedWindowCocoaImpl::SetTransparent(wxByte alpha)
429 [m_macWindow setAlphaValue:(CGFloat) alpha/255.0];
433 bool wxNonOwnedWindowCocoaImpl::SetBackgroundColour(const wxColour& col )
438 void wxNonOwnedWindowCocoaImpl::SetExtraStyle( long exStyle )
442 bool metal = exStyle & wxFRAME_EX_METAL ;
443 int windowStyle = [ m_macWindow styleMask];
444 if ( metal && !(windowStyle & NSTexturedBackgroundWindowMask) )
446 wxFAIL_MSG( _T("Metal Style cannot be changed after creation") );
448 else if ( !metal && (windowStyle & NSTexturedBackgroundWindowMask ) )
450 wxFAIL_MSG( _T("Metal Style cannot be changed after creation") );
455 bool wxNonOwnedWindowCocoaImpl::SetBackgroundStyle(wxBackgroundStyle style)
460 bool wxNonOwnedWindowCocoaImpl::CanSetTransparent()
465 void wxNonOwnedWindowCocoaImpl::MoveWindow(int x, int y, int width, int height)
467 NSRect r = wxToNSRect( NULL, wxRect(x,y,width, height) );
468 // do not trigger refreshes upon invisible and possible partly created objects
469 [m_macWindow setFrame:r display:GetWXPeer()->IsShownOnScreen()];
472 void wxNonOwnedWindowCocoaImpl::GetPosition( int &x, int &y ) const
474 wxRect r = wxFromNSRect( NULL, [m_macWindow frame] );
479 void wxNonOwnedWindowCocoaImpl::GetSize( int &width, int &height ) const
481 NSRect rect = [m_macWindow frame];
482 width = rect.size.width;
483 height = rect.size.height;
486 void wxNonOwnedWindowCocoaImpl::GetContentArea( int& left, int &right, int &width, int &height ) const
488 NSRect outer = NSMakeRect(100,100,100,100);
489 NSRect content = [NSWindow contentRectForFrameRect:outer styleMask:[m_macWindow styleMask] ];
490 NSRect rect = [[m_macWindow contentView] frame];
491 width = rect.size.width;
492 height = rect.size.height;
495 bool wxNonOwnedWindowCocoaImpl::SetShape(const wxRegion& region)
500 void wxNonOwnedWindowCocoaImpl::SetTitle( const wxString& title, wxFontEncoding encoding )
502 [m_macWindow setTitle:wxCFStringRef( title , encoding ).AsNSString()];
505 bool wxNonOwnedWindowCocoaImpl::IsMaximized() const
507 return [m_macWindow isZoomed];
510 bool wxNonOwnedWindowCocoaImpl::IsIconized() const
512 return [m_macWindow isMiniaturized];
515 void wxNonOwnedWindowCocoaImpl::Iconize( bool iconize )
518 [m_macWindow miniaturize:nil];
520 [m_macWindow deminiaturize:nil];
523 void wxNonOwnedWindowCocoaImpl::Maximize(bool maximize)
525 [m_macWindow zoom:nil];
529 // http://cocoadevcentral.com/articles/000028.php
534 NSRect m_formerFrame;
537 bool wxNonOwnedWindowCocoaImpl::IsFullScreen() const
539 return m_macFullScreenData != NULL ;
542 bool wxNonOwnedWindowCocoaImpl::ShowFullScreen(bool show, long style)
546 FullScreenData *data = (FullScreenData *)m_macFullScreenData ;
548 data = new FullScreenData();
550 m_macFullScreenData = data ;
551 data->m_formerLevel = [m_macWindow level];
552 data->m_formerFrame = [m_macWindow frame];
553 CGDisplayCapture( kCGDirectMainDisplay );
554 [m_macWindow setLevel:CGShieldingWindowLevel()];
555 [m_macWindow setFrame:[[NSScreen mainScreen] frame] display:YES];
557 else if ( m_macFullScreenData != NULL )
559 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
560 CGDisplayRelease( kCGDirectMainDisplay );
561 [m_macWindow setLevel:data->m_formerLevel];
562 [m_macWindow setFrame:data->m_formerFrame display:YES];
564 m_macFullScreenData = NULL ;
570 void wxNonOwnedWindowCocoaImpl::RequestUserAttention(int WXUNUSED(flags))
574 void wxNonOwnedWindowCocoaImpl::ScreenToWindow( int *x, int *y )
576 wxPoint p((x ? *x : 0), (y ? *y : 0) );
577 NSPoint nspt = wxToNSPoint( NULL, p );
578 nspt = [m_macWindow convertScreenToBase:nspt];
579 nspt = [[m_macWindow contentView] convertPoint:nspt fromView:nil];
580 p = wxFromNSPoint([m_macWindow contentView], nspt);
587 void wxNonOwnedWindowCocoaImpl::WindowToScreen( int *x, int *y )
589 wxPoint p((x ? *x : 0), (y ? *y : 0) );
590 NSPoint nspt = wxToNSPoint( [m_macWindow contentView], p );
591 nspt = [[m_macWindow contentView] convertPoint:nspt toView:nil];
592 nspt = [m_macWindow convertBaseToScreen:nspt];
593 p = wxFromNSPoint( NULL, nspt);
600 wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, const wxPoint& pos, const wxSize& size,
601 long style, long extraStyle, const wxString& name )
603 wxNonOwnedWindowImpl* now = new wxNonOwnedWindowCocoaImpl( wxpeer );
604 now->Create( parent, pos, size, style , extraStyle, name );