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 |= NSTitledWindowMask | NSUtilityWindowMask;
297 else if ( ( style & wxPOPUP_WINDOW ) )
299 level = kCGPopUpMenuWindowLevel;
301 if ( ( style & wxBORDER_NONE ) )
303 wclass = kHelpWindowClass ; // has no border
304 attr |= kWindowNoShadowAttribute;
308 wclass = kPlainWindowClass ; // has a single line border, it will have to do for now
312 else if ( ( style & wxCAPTION ) )
314 windowstyle |= NSTitledWindowMask ;
316 else if ( ( style & wxFRAME_DRAWER ) )
319 wclass = kDrawerWindowClass;
324 // set these even if we have no title, otherwise the controls won't be visible
325 if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) ||
326 ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) )
328 windowstyle |= NSTitledWindowMask ;
331 else if ( ( style & wxNO_BORDER ) )
333 wclass = kSimpleWindowClass ;
337 wclass = kPlainWindowClass ;
342 if ( windowstyle & NSTitledWindowMask )
344 if ( ( style & wxMINIMIZE_BOX ) )
345 windowstyle |= NSMiniaturizableWindowMask ;
347 if ( ( style & wxMAXIMIZE_BOX ) )
348 windowstyle |= NSResizableWindowMask ; // TODO showing ZOOM ?
350 if ( ( style & wxRESIZE_BORDER ) )
351 windowstyle |= NSResizableWindowMask ;
353 if ( ( style & wxCLOSE_BOX) )
354 windowstyle |= NSClosableWindowMask ;
356 if ( extraStyle & wxFRAME_EX_METAL)
357 windowstyle |= NSTexturedBackgroundWindowMask;
359 if ( ( style & wxFRAME_FLOAT_ON_PARENT ) || ( style & wxFRAME_TOOL_WINDOW ) )
360 level = kCGFloatingWindowLevel;
362 if ( ( style & wxSTAY_ON_TOP ) )
363 level = kCGUtilityWindowLevel;
365 NSRect r = wxToNSRect( NULL, wxRect( pos, size) );
367 [m_macWindow setImplementation:this];
369 [m_macWindow initWithContentRect:r
370 styleMask:windowstyle
371 backing:NSBackingStoreBuffered
375 [m_macWindow setLevel:level];
377 [m_macWindow setDelegate:controller];
379 // [m_macWindow makeKeyAndOrderFront:nil];
383 WXWindow wxNonOwnedWindowCocoaImpl::GetWXWindow() const
388 void wxNonOwnedWindowCocoaImpl::Raise()
390 [m_macWindow orderWindow:NSWindowAbove relativeTo:0];
393 void wxNonOwnedWindowCocoaImpl::Lower()
395 [m_macWindow orderWindow:NSWindowBelow relativeTo:0];
398 bool wxNonOwnedWindowCocoaImpl::Show(bool show)
402 [m_macWindow orderFront:nil];
403 [[m_macWindow contentView] setNeedsDisplay:YES];
406 [m_macWindow orderOut:nil];
410 bool wxNonOwnedWindowCocoaImpl::ShowWithEffect(bool show, wxShowEffect effect, unsigned timeout)
415 void wxNonOwnedWindowCocoaImpl::Update()
417 [m_macWindow displayIfNeeded];
420 bool wxNonOwnedWindowCocoaImpl::SetTransparent(wxByte alpha)
422 [m_macWindow setAlphaValue:(CGFloat) alpha/255.0];
426 bool wxNonOwnedWindowCocoaImpl::SetBackgroundColour(const wxColour& col )
431 void wxNonOwnedWindowCocoaImpl::SetExtraStyle( long exStyle )
435 bool metal = exStyle & wxFRAME_EX_METAL ;
436 int windowStyle = [ m_macWindow styleMask];
437 if ( metal && !(windowStyle & NSTexturedBackgroundWindowMask) )
439 wxFAIL_MSG( _T("Metal Style cannot be changed after creation") );
441 else if ( !metal && (windowStyle & NSTexturedBackgroundWindowMask ) )
443 wxFAIL_MSG( _T("Metal Style cannot be changed after creation") );
448 bool wxNonOwnedWindowCocoaImpl::SetBackgroundStyle(wxBackgroundStyle style)
453 bool wxNonOwnedWindowCocoaImpl::CanSetTransparent()
458 void wxNonOwnedWindowCocoaImpl::MoveWindow(int x, int y, int width, int height)
460 NSRect r = wxToNSRect( NULL, wxRect(x,y,width, height) );
461 // do not trigger refreshes upon invisible and possible partly created objects
462 [m_macWindow setFrame:r display:GetWXPeer()->IsShownOnScreen()];
465 void wxNonOwnedWindowCocoaImpl::GetPosition( int &x, int &y ) const
467 wxRect r = wxFromNSRect( NULL, [m_macWindow frame] );
472 void wxNonOwnedWindowCocoaImpl::GetSize( int &width, int &height ) const
474 NSRect rect = [m_macWindow frame];
475 width = rect.size.width;
476 height = rect.size.height;
479 void wxNonOwnedWindowCocoaImpl::GetContentArea( int& left, int &right, int &width, int &height ) const
481 NSRect outer = NSMakeRect(100,100,100,100);
482 NSRect content = [NSWindow contentRectForFrameRect:outer styleMask:[m_macWindow styleMask] ];
483 NSRect rect = [[m_macWindow contentView] frame];
484 width = rect.size.width;
485 height = rect.size.height;
488 bool wxNonOwnedWindowCocoaImpl::SetShape(const wxRegion& region)
493 void wxNonOwnedWindowCocoaImpl::SetTitle( const wxString& title, wxFontEncoding encoding )
495 [m_macWindow setTitle:wxCFStringRef( title , encoding ).AsNSString()];
498 bool wxNonOwnedWindowCocoaImpl::IsMaximized() const
500 return [m_macWindow isZoomed];
503 bool wxNonOwnedWindowCocoaImpl::IsIconized() const
505 return [m_macWindow isMiniaturized];
508 void wxNonOwnedWindowCocoaImpl::Iconize( bool iconize )
511 [m_macWindow miniaturize:nil];
513 [m_macWindow deminiaturize:nil];
516 void wxNonOwnedWindowCocoaImpl::Maximize(bool maximize)
518 [m_macWindow zoom:nil];
522 // http://cocoadevcentral.com/articles/000028.php
527 NSRect m_formerFrame;
530 bool wxNonOwnedWindowCocoaImpl::IsFullScreen() const
532 return m_macFullScreenData != NULL ;
535 bool wxNonOwnedWindowCocoaImpl::ShowFullScreen(bool show, long style)
539 FullScreenData *data = (FullScreenData *)m_macFullScreenData ;
541 data = new FullScreenData();
543 m_macFullScreenData = data ;
544 data->m_formerLevel = [m_macWindow level];
545 data->m_formerFrame = [m_macWindow frame];
546 CGDisplayCapture( kCGDirectMainDisplay );
547 [m_macWindow setLevel:CGShieldingWindowLevel()];
548 [m_macWindow setFrame:[[NSScreen mainScreen] frame] display:YES];
550 else if ( m_macFullScreenData != NULL )
552 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
553 CGDisplayRelease( kCGDirectMainDisplay );
554 [m_macWindow setLevel:data->m_formerLevel];
555 [m_macWindow setFrame:data->m_formerFrame display:YES];
557 m_macFullScreenData = NULL ;
563 void wxNonOwnedWindowCocoaImpl::RequestUserAttention(int WXUNUSED(flags))
567 void wxNonOwnedWindowCocoaImpl::ScreenToWindow( int *x, int *y )
569 wxPoint p((x ? *x : 0), (y ? *y : 0) );
570 NSPoint nspt = wxToNSPoint( NULL, p );
571 nspt = [m_macWindow convertScreenToBase:nspt];
572 nspt = [[m_macWindow contentView] convertPoint:nspt fromView:nil];
573 p = wxFromNSPoint([m_macWindow contentView], nspt);
580 void wxNonOwnedWindowCocoaImpl::WindowToScreen( int *x, int *y )
582 wxPoint p((x ? *x : 0), (y ? *y : 0) );
583 NSPoint nspt = wxToNSPoint( [m_macWindow contentView], p );
584 nspt = [[m_macWindow contentView] convertPoint:nspt toView:nil];
585 nspt = [m_macWindow convertBaseToScreen:nspt];
586 p = wxFromNSPoint( NULL, nspt);
593 wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, const wxPoint& pos, const wxSize& size,
594 long style, long extraStyle, const wxString& name )
596 wxNonOwnedWindowImpl* now = new wxNonOwnedWindowCocoaImpl( wxpeer );
597 now->Create( parent, pos, size, style , extraStyle, name );