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 @interface wxNSWindow : NSWindow
67 wxNonOwnedWindowCocoaImpl* impl;
70 - (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation;
71 - (wxNonOwnedWindowCocoaImpl*) implementation;
75 @implementation wxNSWindow
77 - (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation
79 impl = theImplementation;
82 - (wxNonOwnedWindowCocoaImpl*) implementation
90 @interface wxNSPanel : wxNSWindow
97 @implementation wxNSPanel
106 @interface wxNonOwnedWindowController : NSObject
110 - (void)windowDidResize:(NSNotification *)notification;
111 - (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize;
112 - (void)windowDidResignMain:(NSNotification *)notification;
113 - (void)windowDidBecomeMain:(NSNotification *)notification;
114 - (void)windowDidMove:(NSNotification *)notification;
115 - (BOOL)windowShouldClose:(id)window;
119 @implementation wxNonOwnedWindowController
127 - (BOOL)windowShouldClose:(id)nwindow
129 wxNSWindow* window = (wxNSWindow*) nwindow;
130 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
133 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
140 - (NSSize)windowWillResize:(NSWindow *)window
141 toSize:(NSSize)proposedFrameSize
144 return proposedFrameSize;
147 - (void)windowDidResize:(NSNotification *)notification
149 wxNSWindow* window = (wxNSWindow*) [notification object];
150 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
153 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
155 wxpeer->HandleResized(0);
159 - (void)windowDidMove:(NSNotification *)notification
161 wxNSWindow* window = (wxNSWindow*) [notification object];
162 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
165 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
167 wxpeer->HandleMoved(0);
171 - (void)windowDidBecomeMain:(NSNotification *)notification
173 wxNSWindow* window = (wxNSWindow*) [notification object];
174 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
177 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
179 wxpeer->HandleActivated(0, true);
183 - (void)windowDidResignMain:(NSNotification *)notification
185 wxNSWindow* window = (wxNSWindow*) [notification object];
186 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
189 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
191 wxpeer->HandleActivated(0, false);
197 IMPLEMENT_DYNAMIC_CLASS( wxNonOwnedWindowCocoaImpl , wxNonOwnedWindowImpl )
199 wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl( wxNonOwnedWindow* nonownedwnd) :
200 wxNonOwnedWindowImpl(nonownedwnd)
203 m_macFullScreenData = NULL;
206 wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl()
209 m_macFullScreenData = NULL;
212 wxNonOwnedWindowCocoaImpl::~wxNonOwnedWindowCocoaImpl()
214 [m_macWindow release];
217 void wxNonOwnedWindowCocoaImpl::Destroy()
219 wxPendingDelete.Append( new wxDeferredObjectDeleter( this ) );
222 void wxNonOwnedWindowCocoaImpl::Create( wxWindow* parent, const wxPoint& pos, const wxSize& size,
223 long style, long extraStyle, const wxString& name )
225 static wxNonOwnedWindowController* controller = NULL;
228 controller =[[wxNonOwnedWindowController alloc] init];
231 int windowstyle = NSBorderlessWindowMask;
233 if ( style & wxFRAME_TOOL_WINDOW )
234 m_macWindow = [wxNSPanel alloc];
236 m_macWindow = [wxNSWindow alloc];
238 CGWindowLevel level = kCGNormalWindowLevelKey;
240 if ( style & wxFRAME_TOOL_WINDOW )
242 if ( ( style & wxSTAY_ON_TOP ) )
243 level = kCGUtilityWindowLevel;
245 level = kCGFloatingWindowLevel ;
248 else if ( ( style & wxPOPUP_WINDOW ) )
250 level = kCGPopUpMenuWindowLevel;
252 if ( ( style & wxBORDER_NONE ) )
254 wclass = kHelpWindowClass ; // has no border
255 attr |= kWindowNoShadowAttribute;
259 wclass = kPlainWindowClass ; // has a single line border, it will have to do for now
263 else if ( ( style & wxCAPTION ) )
265 windowstyle |= NSTitledWindowMask ;
267 else if ( ( style & wxFRAME_DRAWER ) )
270 wclass = kDrawerWindowClass;
275 // set these even if we have no title, otherwise the controls won't be visible
276 if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) ||
277 ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) )
279 windowstyle |= NSTitledWindowMask ;
282 else if ( ( style & wxNO_BORDER ) )
284 wclass = kSimpleWindowClass ;
288 wclass = kPlainWindowClass ;
293 if ( windowstyle & NSTitledWindowMask )
295 if ( ( style & wxMINIMIZE_BOX ) )
296 windowstyle |= NSMiniaturizableWindowMask ;
298 if ( ( style & wxMAXIMIZE_BOX ) )
299 windowstyle |= NSResizableWindowMask ; // TODO showing ZOOM ?
301 if ( ( style & wxRESIZE_BORDER ) )
302 windowstyle |= NSResizableWindowMask ;
304 if ( ( style & wxCLOSE_BOX) )
305 windowstyle |= NSClosableWindowMask ;
307 if ( extraStyle & wxFRAME_EX_METAL)
308 windowstyle |= NSTexturedBackgroundWindowMask;
310 if ( ( style & wxSTAY_ON_TOP ) )
311 level = kCGUtilityWindowLevel;
313 if ( ( style & wxFRAME_FLOAT_ON_PARENT ) )
314 group = GetWindowGroupOfClass(kFloatingWindowClass);
317 NSRect r = wxToNSRect( NULL, wxRect( pos, size) );
319 [m_macWindow setImplementation:this];
321 [m_macWindow initWithContentRect:r
322 styleMask:windowstyle
323 backing:NSBackingStoreBuffered
327 [m_macWindow setLevel:level];
329 [m_macWindow setDelegate:controller];
331 // [m_macWindow makeKeyAndOrderFront:nil];
335 WXWindow wxNonOwnedWindowCocoaImpl::GetWXWindow() const
340 void wxNonOwnedWindowCocoaImpl::Raise()
342 [m_macWindow orderWindow:NSWindowAbove relativeTo:0];
345 void wxNonOwnedWindowCocoaImpl::Lower()
347 [m_macWindow orderWindow:NSWindowBelow relativeTo:0];
350 bool wxNonOwnedWindowCocoaImpl::Show(bool show)
354 [m_macWindow orderFront:nil];
355 [[m_macWindow contentView] setNeedsDisplay:YES];
358 [m_macWindow orderOut:nil];
362 bool wxNonOwnedWindowCocoaImpl::ShowWithEffect(bool show, wxShowEffect effect, unsigned timeout)
367 void wxNonOwnedWindowCocoaImpl::Update()
369 [m_macWindow displayIfNeeded];
372 bool wxNonOwnedWindowCocoaImpl::SetTransparent(wxByte alpha)
374 [m_macWindow setAlphaValue:(CGFloat) alpha/255.0];
378 bool wxNonOwnedWindowCocoaImpl::SetBackgroundColour(const wxColour& col )
383 void wxNonOwnedWindowCocoaImpl::SetExtraStyle( long exStyle )
387 bool metal = exStyle & wxFRAME_EX_METAL ;
388 int windowStyle = [ m_macWindow styleMask];
389 if ( metal && !(windowStyle & NSTexturedBackgroundWindowMask) )
391 wxFAIL_MSG( _T("Metal Style cannot be changed after creation") );
393 else if ( !metal && (windowStyle & NSTexturedBackgroundWindowMask ) )
395 wxFAIL_MSG( _T("Metal Style cannot be changed after creation") );
400 bool wxNonOwnedWindowCocoaImpl::SetBackgroundStyle(wxBackgroundStyle style)
405 bool wxNonOwnedWindowCocoaImpl::CanSetTransparent()
410 void wxNonOwnedWindowCocoaImpl::MoveWindow(int x, int y, int width, int height)
412 NSRect r = wxToNSRect( NULL, wxRect(x,y,width, height) );
413 [m_macWindow setFrame:r display:YES];
416 void wxNonOwnedWindowCocoaImpl::GetPosition( int &x, int &y ) const
418 wxRect r = wxFromNSRect( NULL, [m_macWindow frame] );
423 void wxNonOwnedWindowCocoaImpl::GetSize( int &width, int &height ) const
425 NSRect rect = [m_macWindow frame];
426 width = rect.size.width;
427 height = rect.size.height;
430 void wxNonOwnedWindowCocoaImpl::GetContentArea( int& left, int &right, int &width, int &height ) const
432 NSRect outer = NSMakeRect(100,100,100,100);
433 NSRect content = [NSWindow contentRectForFrameRect:outer styleMask:[m_macWindow styleMask] ];
434 NSRect rect = [[m_macWindow contentView] frame];
435 width = rect.size.width;
436 height = rect.size.height;
439 bool wxNonOwnedWindowCocoaImpl::SetShape(const wxRegion& region)
444 void wxNonOwnedWindowCocoaImpl::SetTitle( const wxString& title, wxFontEncoding encoding )
446 [m_macWindow setTitle:wxCFStringRef( title , encoding ).AsNSString()];
449 bool wxNonOwnedWindowCocoaImpl::IsMaximized() const
451 return [m_macWindow isZoomed];
454 bool wxNonOwnedWindowCocoaImpl::IsIconized() const
456 return [m_macWindow isMiniaturized];
459 void wxNonOwnedWindowCocoaImpl::Iconize( bool iconize )
462 [m_macWindow miniaturize:nil];
464 [m_macWindow deminiaturize:nil];
467 void wxNonOwnedWindowCocoaImpl::Maximize(bool maximize)
469 [m_macWindow zoom:nil];
473 // http://cocoadevcentral.com/articles/000028.php
478 NSRect m_formerFrame;
481 bool wxNonOwnedWindowCocoaImpl::IsFullScreen() const
483 return m_macFullScreenData != NULL ;
486 bool wxNonOwnedWindowCocoaImpl::ShowFullScreen(bool show, long style)
490 FullScreenData *data = (FullScreenData *)m_macFullScreenData ;
492 data = new FullScreenData();
494 m_macFullScreenData = data ;
495 data->m_formerLevel = [m_macWindow level];
496 data->m_formerFrame = [m_macWindow frame];
497 CGDisplayCapture( kCGDirectMainDisplay );
498 [m_macWindow setLevel:CGShieldingWindowLevel()];
499 [m_macWindow setFrame:[[NSScreen mainScreen] frame] display:YES];
501 else if ( m_macFullScreenData != NULL )
503 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
504 CGDisplayRelease( kCGDirectMainDisplay );
505 [m_macWindow setLevel:data->m_formerLevel];
506 [m_macWindow setFrame:data->m_formerFrame display:YES];
508 m_macFullScreenData = NULL ;
514 void wxNonOwnedWindowCocoaImpl::RequestUserAttention(int WXUNUSED(flags))
518 void wxNonOwnedWindowCocoaImpl::ScreenToWindow( int *x, int *y )
520 wxPoint p((x ? *x : 0), (y ? *y : 0) );
522 NSPoint nspt = wxToNSPoint( NULL, p );
524 nspt = [[m_macWindow contentView] convertPoint:p toV:nil];
533 void wxNonOwnedWindowCocoaImpl::WindowToScreen( int *x, int *y )
535 wxPoint p( (x ? *x : 0), (y ? *y : 0) );
537 p = [m_macWindow convertPoint:p toWindow:nil];
545 wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, const wxPoint& pos, const wxSize& size,
546 long style, long extraStyle, const wxString& name )
548 wxNonOwnedWindowImpl* now = new wxNonOwnedWindowCocoaImpl( wxpeer );
549 now->Create( parent, pos, size, style , extraStyle, name );