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 *)win
141 toSize:(NSSize)proposedFrameSize
143 NSRect frame = [win frame];
144 wxRect wxframe = wxFromNSRect( NULL, frame );
145 wxframe.SetWidth( proposedFrameSize.width );
146 wxframe.SetHeight( proposedFrameSize.height );
147 wxNSWindow* window = (wxNSWindow*) win;
148 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
151 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
154 wxpeer->HandleResizing( 0, &wxframe );
155 NSSize newSize = NSMakeSize(wxframe.GetWidth(), wxframe.GetHeight());
160 return proposedFrameSize;
163 - (void)windowDidResize:(NSNotification *)notification
165 wxNSWindow* window = (wxNSWindow*) [notification object];
166 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
169 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
171 wxpeer->HandleResized(0);
175 - (void)windowDidMove:(NSNotification *)notification
177 wxNSWindow* window = (wxNSWindow*) [notification object];
178 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
181 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
183 wxpeer->HandleMoved(0);
187 - (void)windowDidBecomeMain:(NSNotification *)notification
189 wxNSWindow* window = (wxNSWindow*) [notification object];
190 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
193 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
195 wxpeer->HandleActivated(0, true);
199 - (void)windowDidResignMain:(NSNotification *)notification
201 wxNSWindow* window = (wxNSWindow*) [notification object];
202 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
205 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
207 wxpeer->HandleActivated(0, false);
213 IMPLEMENT_DYNAMIC_CLASS( wxNonOwnedWindowCocoaImpl , wxNonOwnedWindowImpl )
215 wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl( wxNonOwnedWindow* nonownedwnd) :
216 wxNonOwnedWindowImpl(nonownedwnd)
219 m_macFullScreenData = NULL;
222 wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl()
225 m_macFullScreenData = NULL;
228 wxNonOwnedWindowCocoaImpl::~wxNonOwnedWindowCocoaImpl()
230 [m_macWindow release];
233 void wxNonOwnedWindowCocoaImpl::Destroy()
235 wxPendingDelete.Append( new wxDeferredObjectDeleter( this ) );
238 void wxNonOwnedWindowCocoaImpl::Create( wxWindow* parent, const wxPoint& pos, const wxSize& size,
239 long style, long extraStyle, const wxString& name )
241 static wxNonOwnedWindowController* controller = NULL;
244 controller =[[wxNonOwnedWindowController alloc] init];
247 int windowstyle = NSBorderlessWindowMask;
249 if ( style & wxFRAME_TOOL_WINDOW )
250 m_macWindow = [wxNSPanel alloc];
252 m_macWindow = [wxNSWindow alloc];
254 CGWindowLevel level = kCGNormalWindowLevel;
256 if ( style & wxFRAME_TOOL_WINDOW )
258 if ( ( style & wxSTAY_ON_TOP ) )
259 level = kCGUtilityWindowLevel;
261 level = kCGFloatingWindowLevel ;
264 else if ( ( style & wxPOPUP_WINDOW ) )
266 level = kCGPopUpMenuWindowLevel;
268 if ( ( style & wxBORDER_NONE ) )
270 wclass = kHelpWindowClass ; // has no border
271 attr |= kWindowNoShadowAttribute;
275 wclass = kPlainWindowClass ; // has a single line border, it will have to do for now
279 else if ( ( style & wxCAPTION ) )
281 windowstyle |= NSTitledWindowMask ;
283 else if ( ( style & wxFRAME_DRAWER ) )
286 wclass = kDrawerWindowClass;
291 // set these even if we have no title, otherwise the controls won't be visible
292 if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) ||
293 ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) )
295 windowstyle |= NSTitledWindowMask ;
298 else if ( ( style & wxNO_BORDER ) )
300 wclass = kSimpleWindowClass ;
304 wclass = kPlainWindowClass ;
309 if ( windowstyle & NSTitledWindowMask )
311 if ( ( style & wxMINIMIZE_BOX ) )
312 windowstyle |= NSMiniaturizableWindowMask ;
314 if ( ( style & wxMAXIMIZE_BOX ) )
315 windowstyle |= NSResizableWindowMask ; // TODO showing ZOOM ?
317 if ( ( style & wxRESIZE_BORDER ) )
318 windowstyle |= NSResizableWindowMask ;
320 if ( ( style & wxCLOSE_BOX) )
321 windowstyle |= NSClosableWindowMask ;
323 if ( extraStyle & wxFRAME_EX_METAL)
324 windowstyle |= NSTexturedBackgroundWindowMask;
326 if ( ( style & wxSTAY_ON_TOP ) )
327 level = kCGUtilityWindowLevel;
329 if ( ( style & wxFRAME_FLOAT_ON_PARENT ) )
330 group = GetWindowGroupOfClass(kFloatingWindowClass);
333 NSRect r = wxToNSRect( NULL, wxRect( pos, size) );
335 [m_macWindow setImplementation:this];
337 [m_macWindow initWithContentRect:r
338 styleMask:windowstyle
339 backing:NSBackingStoreBuffered
343 [m_macWindow setLevel:level];
345 [m_macWindow setDelegate:controller];
347 // [m_macWindow makeKeyAndOrderFront:nil];
351 WXWindow wxNonOwnedWindowCocoaImpl::GetWXWindow() const
356 void wxNonOwnedWindowCocoaImpl::Raise()
358 [m_macWindow orderWindow:NSWindowAbove relativeTo:0];
361 void wxNonOwnedWindowCocoaImpl::Lower()
363 [m_macWindow orderWindow:NSWindowBelow relativeTo:0];
366 bool wxNonOwnedWindowCocoaImpl::Show(bool show)
370 [m_macWindow orderFront:nil];
371 [[m_macWindow contentView] setNeedsDisplay:YES];
374 [m_macWindow orderOut:nil];
378 bool wxNonOwnedWindowCocoaImpl::ShowWithEffect(bool show, wxShowEffect effect, unsigned timeout)
383 void wxNonOwnedWindowCocoaImpl::Update()
385 [m_macWindow displayIfNeeded];
388 bool wxNonOwnedWindowCocoaImpl::SetTransparent(wxByte alpha)
390 [m_macWindow setAlphaValue:(CGFloat) alpha/255.0];
394 bool wxNonOwnedWindowCocoaImpl::SetBackgroundColour(const wxColour& col )
399 void wxNonOwnedWindowCocoaImpl::SetExtraStyle( long exStyle )
403 bool metal = exStyle & wxFRAME_EX_METAL ;
404 int windowStyle = [ m_macWindow styleMask];
405 if ( metal && !(windowStyle & NSTexturedBackgroundWindowMask) )
407 wxFAIL_MSG( _T("Metal Style cannot be changed after creation") );
409 else if ( !metal && (windowStyle & NSTexturedBackgroundWindowMask ) )
411 wxFAIL_MSG( _T("Metal Style cannot be changed after creation") );
416 bool wxNonOwnedWindowCocoaImpl::SetBackgroundStyle(wxBackgroundStyle style)
421 bool wxNonOwnedWindowCocoaImpl::CanSetTransparent()
426 void wxNonOwnedWindowCocoaImpl::MoveWindow(int x, int y, int width, int height)
428 NSRect r = wxToNSRect( NULL, wxRect(x,y,width, height) );
429 [m_macWindow setFrame:r display:YES];
432 void wxNonOwnedWindowCocoaImpl::GetPosition( int &x, int &y ) const
434 wxRect r = wxFromNSRect( NULL, [m_macWindow frame] );
439 void wxNonOwnedWindowCocoaImpl::GetSize( int &width, int &height ) const
441 NSRect rect = [m_macWindow frame];
442 width = rect.size.width;
443 height = rect.size.height;
446 void wxNonOwnedWindowCocoaImpl::GetContentArea( int& left, int &right, int &width, int &height ) const
448 NSRect outer = NSMakeRect(100,100,100,100);
449 NSRect content = [NSWindow contentRectForFrameRect:outer styleMask:[m_macWindow styleMask] ];
450 NSRect rect = [[m_macWindow contentView] frame];
451 width = rect.size.width;
452 height = rect.size.height;
455 bool wxNonOwnedWindowCocoaImpl::SetShape(const wxRegion& region)
460 void wxNonOwnedWindowCocoaImpl::SetTitle( const wxString& title, wxFontEncoding encoding )
462 [m_macWindow setTitle:wxCFStringRef( title , encoding ).AsNSString()];
465 bool wxNonOwnedWindowCocoaImpl::IsMaximized() const
467 return [m_macWindow isZoomed];
470 bool wxNonOwnedWindowCocoaImpl::IsIconized() const
472 return [m_macWindow isMiniaturized];
475 void wxNonOwnedWindowCocoaImpl::Iconize( bool iconize )
478 [m_macWindow miniaturize:nil];
480 [m_macWindow deminiaturize:nil];
483 void wxNonOwnedWindowCocoaImpl::Maximize(bool maximize)
485 [m_macWindow zoom:nil];
489 // http://cocoadevcentral.com/articles/000028.php
494 NSRect m_formerFrame;
497 bool wxNonOwnedWindowCocoaImpl::IsFullScreen() const
499 return m_macFullScreenData != NULL ;
502 bool wxNonOwnedWindowCocoaImpl::ShowFullScreen(bool show, long style)
506 FullScreenData *data = (FullScreenData *)m_macFullScreenData ;
508 data = new FullScreenData();
510 m_macFullScreenData = data ;
511 data->m_formerLevel = [m_macWindow level];
512 data->m_formerFrame = [m_macWindow frame];
513 CGDisplayCapture( kCGDirectMainDisplay );
514 [m_macWindow setLevel:CGShieldingWindowLevel()];
515 [m_macWindow setFrame:[[NSScreen mainScreen] frame] display:YES];
517 else if ( m_macFullScreenData != NULL )
519 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
520 CGDisplayRelease( kCGDirectMainDisplay );
521 [m_macWindow setLevel:data->m_formerLevel];
522 [m_macWindow setFrame:data->m_formerFrame display:YES];
524 m_macFullScreenData = NULL ;
530 void wxNonOwnedWindowCocoaImpl::RequestUserAttention(int WXUNUSED(flags))
534 void wxNonOwnedWindowCocoaImpl::ScreenToWindow( int *x, int *y )
536 wxPoint p((x ? *x : 0), (y ? *y : 0) );
538 NSPoint nspt = wxToNSPoint( NULL, p );
540 nspt = [[m_macWindow contentView] convertPoint:p toV:nil];
549 void wxNonOwnedWindowCocoaImpl::WindowToScreen( int *x, int *y )
551 wxPoint p( (x ? *x : 0), (y ? *y : 0) );
553 p = [m_macWindow convertPoint:p toWindow:nil];
561 wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, const wxPoint& pos, const wxSize& size,
562 long style, long extraStyle, const wxString& name )
564 wxNonOwnedWindowImpl* now = new wxNonOwnedWindowCocoaImpl( wxpeer );
565 now->Create( parent, pos, size, style , extraStyle, name );