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"
15 #include <Cocoa/Cocoa.h>
17 #include <UIKit/UIKit.h>
21 #include "wx/osx/private.h"
24 NSRect wxToNSRect( NSView* parent, const wxRect& r )
26 NSRect frame = parent ? [parent bounds] : [[NSScreen mainScreen] frame];
29 if ( parent != NULL && ![ parent isFlipped ] )
30 y = frame.size.height - ( r.y + r.height );
31 return NSMakeRect(x, y, r.width , r.height);
34 wxRect wxFromNSRect( NSView* parent, const NSRect& rect )
36 NSRect frame = parent ? [parent bounds] : [[NSScreen mainScreen] frame];
37 int y = rect.origin.y;
38 int x = rect.origin.x;
39 if ( parent != NULL && ![ parent isFlipped ] )
40 y = frame.size.height - (rect.origin.y + rect.size.height);
41 return wxRect( x, y, rect.size.width, rect.size.height );
44 NSPoint wxToNSPoint( NSView* parent, const wxPoint& p )
46 NSRect frame = parent ? [parent bounds] : [[NSScreen mainScreen] frame];
49 if ( parent != NULL && ![ parent isFlipped ] )
50 y = frame.size.height - ( p.y );
51 return NSMakePoint(x, y);
54 wxPoint wxFromNSPoint( NSView* parent, const NSPoint& p )
56 NSRect frame = parent ? [parent bounds] : [[NSScreen mainScreen] frame];
59 if ( parent != NULL && ![ parent isFlipped ] )
60 y = frame.size.height - ( p.y );
61 return wxPoint( x, y);
65 // wx native implementation classes
68 @interface wxNSWindow : NSWindow
71 wxNonOwnedWindowCocoaImpl* impl;
74 - (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation;
75 - (wxNonOwnedWindowCocoaImpl*) implementation;
79 @implementation wxNSWindow
81 - (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation
83 impl = theImplementation;
86 - (wxNonOwnedWindowCocoaImpl*) implementation
94 @interface wxNSPanel : wxNSWindow
101 @implementation wxNSPanel
110 @interface wxNonOwnedWindowController : NSObject
114 - (void)windowDidResize:(NSNotification *)notification;
115 - (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize;
116 - (void)windowDidResignMain:(NSNotification *)notification;
117 - (void)windowDidBecomeMain:(NSNotification *)notification;
118 - (void)windowDidMove:(NSNotification *)notification;
119 - (BOOL)windowShouldClose:(id)window;
123 @implementation wxNonOwnedWindowController
131 - (BOOL)windowShouldClose:(id)nwindow
133 wxNSWindow* window = (wxNSWindow*) nwindow;
134 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
137 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
144 - (NSSize)windowWillResize:(NSWindow *)window
145 toSize:(NSSize)proposedFrameSize
148 return proposedFrameSize;
151 - (void)windowDidResize:(NSNotification *)notification
153 wxNSWindow* window = (wxNSWindow*) [notification object];
154 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
157 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
159 wxpeer->HandleResized(0);
163 - (void)windowDidMove:(NSNotification *)notification
165 wxNSWindow* window = (wxNSWindow*) [notification object];
166 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
169 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
171 wxpeer->HandleMoved(0);
175 - (void)windowDidBecomeMain:(NSNotification *)notification
177 wxNSWindow* window = (wxNSWindow*) [notification object];
178 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
181 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
183 wxpeer->HandleActivated(0, true);
187 - (void)windowDidResignMain:(NSNotification *)notification
189 wxNSWindow* window = (wxNSWindow*) [notification object];
190 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
193 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
195 wxpeer->HandleActivated(0, false);
201 IMPLEMENT_DYNAMIC_CLASS( wxNonOwnedWindowCocoaImpl , wxNonOwnedWindowImpl )
203 wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl( wxNonOwnedWindow* nonownedwnd) :
204 wxNonOwnedWindowImpl(nonownedwnd)
207 m_macFullScreenData = NULL;
210 wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl()
213 m_macFullScreenData = NULL;
216 wxNonOwnedWindowCocoaImpl::~wxNonOwnedWindowCocoaImpl()
218 [m_macWindow release];
221 void wxNonOwnedWindowCocoaImpl::Destroy()
223 wxPendingDelete.Append( new wxDeferredObjectDeleter( this ) );
226 void wxNonOwnedWindowCocoaImpl::Create( wxWindow* parent, const wxPoint& pos, const wxSize& size,
227 long style, long extraStyle, const wxString& name )
229 static wxNonOwnedWindowController* controller = NULL;
232 controller =[[wxNonOwnedWindowController alloc] init];
235 int windowstyle = NSBorderlessWindowMask;
237 if ( style & wxFRAME_TOOL_WINDOW )
238 m_macWindow = [wxNSPanel alloc];
240 m_macWindow = [wxNSWindow alloc];
242 CGWindowLevel level = kCGNormalWindowLevelKey;
244 if ( style & wxFRAME_TOOL_WINDOW )
246 if ( ( style & wxSTAY_ON_TOP ) )
247 level = kCGUtilityWindowLevel;
249 level = kCGFloatingWindowLevel ;
252 else if ( ( style & wxPOPUP_WINDOW ) )
254 level = kCGPopUpMenuWindowLevel;
256 if ( ( style & wxBORDER_NONE ) )
258 wclass = kHelpWindowClass ; // has no border
259 attr |= kWindowNoShadowAttribute;
263 wclass = kPlainWindowClass ; // has a single line border, it will have to do for now
267 else if ( ( style & wxCAPTION ) )
269 windowstyle |= NSTitledWindowMask ;
271 else if ( ( style & wxFRAME_DRAWER ) )
274 wclass = kDrawerWindowClass;
279 // set these even if we have no title, otherwise the controls won't be visible
280 if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) ||
281 ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) )
283 windowstyle |= NSTitledWindowMask ;
286 else if ( ( style & wxNO_BORDER ) )
288 wclass = kSimpleWindowClass ;
292 wclass = kPlainWindowClass ;
297 if ( windowstyle & NSTitledWindowMask )
299 if ( ( style & wxMINIMIZE_BOX ) )
300 windowstyle |= NSMiniaturizableWindowMask ;
302 if ( ( style & wxMAXIMIZE_BOX ) )
303 windowstyle |= NSResizableWindowMask ; // TODO showing ZOOM ?
305 if ( ( style & wxRESIZE_BORDER ) )
306 windowstyle |= NSResizableWindowMask ;
308 if ( ( style & wxCLOSE_BOX) )
309 windowstyle |= NSClosableWindowMask ;
311 if ( extraStyle & wxFRAME_EX_METAL)
312 windowstyle |= NSTexturedBackgroundWindowMask;
314 if ( ( style & wxSTAY_ON_TOP ) )
315 level = kCGUtilityWindowLevel;
317 if ( ( style & wxFRAME_FLOAT_ON_PARENT ) )
318 group = GetWindowGroupOfClass(kFloatingWindowClass);
321 NSRect r = wxToNSRect( NULL, wxRect( pos, size) );
323 [m_macWindow setImplementation:this];
325 [m_macWindow initWithContentRect:r
326 styleMask:windowstyle
327 backing:NSBackingStoreBuffered
331 [m_macWindow setLevel:level];
333 [m_macWindow setDelegate:controller];
335 // [m_macWindow makeKeyAndOrderFront:nil];
339 WXWindow wxNonOwnedWindowCocoaImpl::GetWXWindow() const
344 void wxNonOwnedWindowCocoaImpl::Raise()
346 [m_macWindow orderWindow:NSWindowAbove relativeTo:0];
349 void wxNonOwnedWindowCocoaImpl::Lower()
351 [m_macWindow orderWindow:NSWindowBelow relativeTo:0];
354 bool wxNonOwnedWindowCocoaImpl::Show(bool show)
358 [m_macWindow orderFront:nil];
359 [[m_macWindow contentView] setNeedsDisplay:YES];
362 [m_macWindow orderOut:nil];
366 bool wxNonOwnedWindowCocoaImpl::ShowWithEffect(bool show, wxShowEffect effect, unsigned timeout)
371 void wxNonOwnedWindowCocoaImpl::Update()
373 [m_macWindow displayIfNeeded];
376 bool wxNonOwnedWindowCocoaImpl::SetTransparent(wxByte alpha)
378 [m_macWindow setAlphaValue:(CGFloat) alpha/255.0];
382 bool wxNonOwnedWindowCocoaImpl::SetBackgroundColour(const wxColour& col )
387 void wxNonOwnedWindowCocoaImpl::SetExtraStyle( long exStyle )
391 bool metal = exStyle & wxFRAME_EX_METAL ;
392 int windowStyle = [ m_macWindow styleMask];
393 if ( metal && !(windowStyle & NSTexturedBackgroundWindowMask) )
395 wxFAIL_MSG( _T("Metal Style cannot be changed after creation") );
397 else if ( !metal && (windowStyle & NSTexturedBackgroundWindowMask ) )
399 wxFAIL_MSG( _T("Metal Style cannot be changed after creation") );
404 bool wxNonOwnedWindowCocoaImpl::SetBackgroundStyle(wxBackgroundStyle style)
409 bool wxNonOwnedWindowCocoaImpl::CanSetTransparent()
414 void wxNonOwnedWindowCocoaImpl::MoveWindow(int x, int y, int width, int height)
416 NSRect r = wxToNSRect( NULL, wxRect(x,y,width, height) );
417 [m_macWindow setFrame:r display:YES];
420 void wxNonOwnedWindowCocoaImpl::GetPosition( int &x, int &y ) const
422 wxRect r = wxFromNSRect( NULL, [m_macWindow frame] );
427 void wxNonOwnedWindowCocoaImpl::GetSize( int &width, int &height ) const
429 NSRect rect = [m_macWindow frame];
430 width = rect.size.width;
431 height = rect.size.height;
434 void wxNonOwnedWindowCocoaImpl::GetContentArea( int& left, int &right, int &width, int &height ) const
436 NSRect outer = NSMakeRect(100,100,100,100);
437 NSRect content = [NSWindow contentRectForFrameRect:outer styleMask:[m_macWindow styleMask] ];
438 NSRect rect = [[m_macWindow contentView] frame];
439 width = rect.size.width;
440 height = rect.size.height;
443 bool wxNonOwnedWindowCocoaImpl::SetShape(const wxRegion& region)
448 void wxNonOwnedWindowCocoaImpl::SetTitle( const wxString& title, wxFontEncoding encoding )
450 [m_macWindow setTitle:wxCFStringRef( title , encoding ).AsNSString()];
453 bool wxNonOwnedWindowCocoaImpl::IsMaximized() const
455 return [m_macWindow isZoomed];
458 bool wxNonOwnedWindowCocoaImpl::IsIconized() const
460 return [m_macWindow isMiniaturized];
463 void wxNonOwnedWindowCocoaImpl::Iconize( bool iconize )
466 [m_macWindow miniaturize:nil];
468 [m_macWindow deminiaturize:nil];
471 void wxNonOwnedWindowCocoaImpl::Maximize(bool maximize)
473 [m_macWindow zoom:nil];
477 // http://cocoadevcentral.com/articles/000028.php
482 NSRect m_formerFrame;
485 bool wxNonOwnedWindowCocoaImpl::IsFullScreen() const
487 return m_macFullScreenData != NULL ;
490 bool wxNonOwnedWindowCocoaImpl::ShowFullScreen(bool show, long style)
494 FullScreenData *data = (FullScreenData *)m_macFullScreenData ;
496 data = new FullScreenData();
498 m_macFullScreenData = data ;
499 data->m_formerLevel = [m_macWindow level];
500 data->m_formerFrame = [m_macWindow frame];
501 CGDisplayCapture( kCGDirectMainDisplay );
502 [m_macWindow setLevel:CGShieldingWindowLevel()];
503 [m_macWindow setFrame:[[NSScreen mainScreen] frame] display:YES];
505 else if ( m_macFullScreenData != NULL )
507 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
508 CGDisplayRelease( kCGDirectMainDisplay );
509 [m_macWindow setLevel:data->m_formerLevel];
510 [m_macWindow setFrame:data->m_formerFrame display:YES];
512 m_macFullScreenData = NULL ;
518 void wxNonOwnedWindowCocoaImpl::RequestUserAttention(int WXUNUSED(flags))
522 void wxNonOwnedWindowCocoaImpl::ScreenToWindow( int *x, int *y )
524 wxPoint p((x ? *x : 0), (y ? *y : 0) );
526 NSPoint nspt = wxToNSPoint( NULL, p );
528 nspt = [[m_macWindow contentView] convertPoint:p toV:nil];
537 void wxNonOwnedWindowCocoaImpl::WindowToScreen( int *x, int *y )
539 wxPoint p( (x ? *x : 0), (y ? *y : 0) );
541 p = [m_macWindow convertPoint:p toWindow:nil];
549 wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, const wxPoint& pos, const wxSize& size,
550 long style, long extraStyle, const wxString& name )
552 wxNonOwnedWindowImpl* now = new wxNonOwnedWindowCocoaImpl( wxpeer );
553 now->Create( parent, pos, size, style , extraStyle, name );