]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/nonownedwnd.mm
Remove no longer necessary re-entrancy check, fix function prototype definition for...
[wxWidgets.git] / src / osx / cocoa / nonownedwnd.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/nonownedwnd.mm
3 // Purpose: non owned window for cocoa
4 // Author: DavidStefan Csomor
5 // Modified by:
6 // Created: 2008-06-20
7 // RCS-ID: $Id: nonownedwnd.mm 48805 2007-09-19 14:52:25Z SC $
8 // Copyright: (c) Stefan Csomor
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 #include "wx/wxprec.h"
13 #ifndef WX_PRECOMP
14 #include "wx/nonownedwnd.h"
15 #include "wx/frame.h"
16 #endif
17
18 #include "wx/osx/private.h"
19
20 NSRect wxToNSRect( NSView* parent, const wxRect& r )
21 {
22 NSRect frame = parent ? [parent bounds] : [[NSScreen mainScreen] frame];
23 int y = r.y;
24 int x = r.x ;
25 if ( parent == NULL || ![ parent isFlipped ] )
26 y = frame.size.height - ( r.y + r.height );
27 return NSMakeRect(x, y, r.width , r.height);
28 }
29
30 wxRect wxFromNSRect( NSView* parent, const NSRect& rect )
31 {
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 );
38 }
39
40 NSPoint wxToNSPoint( NSView* parent, const wxPoint& p )
41 {
42 NSRect frame = parent ? [parent bounds] : [[NSScreen mainScreen] frame];
43 int x = p.x ;
44 int y = p.y;
45 if ( parent == NULL || ![ parent isFlipped ] )
46 y = frame.size.height - ( p.y );
47 return NSMakePoint(x, y);
48 }
49
50 wxPoint wxFromNSPoint( NSView* parent, const NSPoint& p )
51 {
52 NSRect frame = parent ? [parent bounds] : [[NSScreen mainScreen] frame];
53 int x = p.x;
54 int y = p.y;
55 if ( parent == NULL || ![ parent isFlipped ] )
56 y = frame.size.height - ( p.y );
57 return wxPoint( x, y);
58 }
59
60 //
61 // wx native implementation classes
62 //
63
64 typedef void (*wxOSX_NoResponderHandlerPtr)(NSView* self, SEL _cmd, SEL selector);
65
66 @interface wxNSWindow : NSWindow
67
68 {
69 wxNonOwnedWindowCocoaImpl* impl;
70 }
71
72 - (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation;
73 - (wxNonOwnedWindowCocoaImpl*) implementation;
74 - (void)noResponderFor: (SEL) selector;
75 @end
76
77 @implementation wxNSWindow
78
79 - (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation
80 {
81 impl = theImplementation;
82 }
83
84 - (wxNonOwnedWindowCocoaImpl*) implementation
85 {
86 return impl;
87 }
88
89 // NB: if we don't do this, all key downs that get handled lead to a NSBeep
90 - (void)noResponderFor: (SEL) selector
91 {
92 if (selector != @selector(keyDown:))
93 {
94 wxOSX_NoResponderHandlerPtr superimpl = (wxOSX_NoResponderHandlerPtr) [[self superclass] instanceMethodForSelector:@selector(noResponderFor:)];
95 superimpl(self, @selector(noResponderFor:), selector);
96 }
97 }
98
99 @end
100
101 @interface wxNSPanel : NSPanel
102
103 {
104 wxNonOwnedWindowCocoaImpl* impl;
105 }
106
107 - (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation;
108 - (wxNonOwnedWindowCocoaImpl*) implementation;
109 - (void)noResponderFor: (SEL) selector;
110 @end
111
112 @implementation wxNSPanel
113
114 - (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation
115 {
116 impl = theImplementation;
117 }
118
119 - (wxNonOwnedWindowCocoaImpl*) implementation
120 {
121 return impl;
122 }
123
124 // NB: if we don't do this, all key downs that get handled lead to a NSBeep
125 - (void)noResponderFor: (SEL) selector
126 {
127 if (selector != @selector(keyDown:))
128 {
129 wxOSX_NoResponderHandlerPtr superimpl = (wxOSX_NoResponderHandlerPtr) [[self superclass] instanceMethodForSelector:@selector(noResponderFor:)];
130 superimpl(self, @selector(noResponderFor:), selector);
131 }
132 }
133
134 @end
135
136
137 //
138 // controller
139 //
140
141 @interface wxNonOwnedWindowController : NSObject
142 {
143 }
144
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;
151
152 @end
153
154 @implementation wxNonOwnedWindowController
155
156 - (id) init
157 {
158 [super init];
159 return self;
160 }
161
162 - (BOOL)windowShouldClose:(id)nwindow
163 {
164 wxNSWindow* window = (wxNSWindow*) nwindow;
165 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
166 if ( windowimpl )
167 {
168 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
169 if ( wxpeer )
170 wxpeer->Close();
171 }
172 return NO;
173 }
174
175 - (NSSize)windowWillResize:(NSWindow *)win
176 toSize:(NSSize)proposedFrameSize
177 {
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];
184 if ( windowimpl )
185 {
186 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
187 if ( wxpeer )
188 {
189 wxpeer->HandleResizing( 0, &wxframe );
190 NSSize newSize = NSMakeSize(wxframe.GetWidth(), wxframe.GetHeight());
191 return newSize;
192 }
193 }
194
195 return proposedFrameSize;
196 }
197
198 - (void)windowDidResize:(NSNotification *)notification
199 {
200 wxNSWindow* window = (wxNSWindow*) [notification object];
201 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
202 if ( windowimpl )
203 {
204 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
205 if ( wxpeer )
206 wxpeer->HandleResized(0);
207 }
208 }
209
210 - (void)windowDidMove:(NSNotification *)notification
211 {
212 wxNSWindow* window = (wxNSWindow*) [notification object];
213 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
214 if ( windowimpl )
215 {
216 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
217 if ( wxpeer )
218 wxpeer->HandleMoved(0);
219 }
220 }
221
222 - (void)windowDidBecomeMain:(NSNotification *)notification
223 {
224 wxNSWindow* window = (wxNSWindow*) [notification object];
225 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
226 if ( windowimpl )
227 {
228 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
229 if ( wxpeer )
230 wxpeer->HandleActivated(0, true);
231 }
232 }
233
234 - (void)windowDidResignMain:(NSNotification *)notification
235 {
236 wxNSWindow* window = (wxNSWindow*) [notification object];
237 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
238 if ( windowimpl )
239 {
240 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
241 if ( wxpeer )
242 wxpeer->HandleActivated(0, false);
243 }
244 }
245
246 @end
247
248 IMPLEMENT_DYNAMIC_CLASS( wxNonOwnedWindowCocoaImpl , wxNonOwnedWindowImpl )
249
250 wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl( wxNonOwnedWindow* nonownedwnd) :
251 wxNonOwnedWindowImpl(nonownedwnd)
252 {
253 m_macWindow = NULL;
254 m_macFullScreenData = NULL;
255 }
256
257 wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl()
258 {
259 m_macWindow = NULL;
260 m_macFullScreenData = NULL;
261 }
262
263 wxNonOwnedWindowCocoaImpl::~wxNonOwnedWindowCocoaImpl()
264 {
265 [m_macWindow setImplementation:nil];
266 [m_macWindow setDelegate:nil];
267 [m_macWindow release];
268 }
269
270 void wxNonOwnedWindowCocoaImpl::Destroy()
271 {
272 wxPendingDelete.Append( new wxDeferredObjectDeleter( this ) );
273 }
274
275 void wxNonOwnedWindowCocoaImpl::Create( wxWindow* parent, const wxPoint& pos, const wxSize& size,
276 long style, long extraStyle, const wxString& name )
277 {
278 static wxNonOwnedWindowController* controller = NULL;
279
280 if ( !controller )
281 controller =[[wxNonOwnedWindowController alloc] init];
282
283
284 int windowstyle = NSBorderlessWindowMask;
285
286 if ( style & wxFRAME_TOOL_WINDOW )
287 m_macWindow = [wxNSPanel alloc];
288 else
289 m_macWindow = [wxNSWindow alloc];
290
291 CGWindowLevel level = kCGNormalWindowLevel;
292
293 if ( style & wxFRAME_TOOL_WINDOW )
294 {
295 windowstyle |= NSTitledWindowMask | NSUtilityWindowMask;
296 }
297 else if ( ( style & wxPOPUP_WINDOW ) )
298 {
299 level = kCGPopUpMenuWindowLevel;
300 /*
301 if ( ( style & wxBORDER_NONE ) )
302 {
303 wclass = kHelpWindowClass ; // has no border
304 attr |= kWindowNoShadowAttribute;
305 }
306 else
307 {
308 wclass = kPlainWindowClass ; // has a single line border, it will have to do for now
309 }
310 */
311 }
312 else if ( ( style & wxCAPTION ) )
313 {
314 windowstyle |= NSTitledWindowMask ;
315 }
316 else if ( ( style & wxFRAME_DRAWER ) )
317 {
318 /*
319 wclass = kDrawerWindowClass;
320 */
321 }
322 else
323 {
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 ) )
327 {
328 windowstyle |= NSTitledWindowMask ;
329 }
330 /*
331 else if ( ( style & wxNO_BORDER ) )
332 {
333 wclass = kSimpleWindowClass ;
334 }
335 else
336 {
337 wclass = kPlainWindowClass ;
338 }
339 */
340 }
341
342 if ( windowstyle & NSTitledWindowMask )
343 {
344 if ( ( style & wxMINIMIZE_BOX ) )
345 windowstyle |= NSMiniaturizableWindowMask ;
346
347 if ( ( style & wxMAXIMIZE_BOX ) )
348 windowstyle |= NSResizableWindowMask ; // TODO showing ZOOM ?
349
350 if ( ( style & wxRESIZE_BORDER ) )
351 windowstyle |= NSResizableWindowMask ;
352
353 if ( ( style & wxCLOSE_BOX) )
354 windowstyle |= NSClosableWindowMask ;
355 }
356 if ( extraStyle & wxFRAME_EX_METAL)
357 windowstyle |= NSTexturedBackgroundWindowMask;
358
359 if ( ( style & wxFRAME_FLOAT_ON_PARENT ) || ( style & wxFRAME_TOOL_WINDOW ) )
360 level = kCGFloatingWindowLevel;
361
362 if ( ( style & wxSTAY_ON_TOP ) )
363 level = kCGUtilityWindowLevel;
364
365 NSRect r = wxToNSRect( NULL, wxRect( pos, size) );
366
367 [m_macWindow setImplementation:this];
368
369 [m_macWindow initWithContentRect:r
370 styleMask:windowstyle
371 backing:NSBackingStoreBuffered
372 defer:NO
373 ];
374
375 [m_macWindow setLevel:level];
376
377 [m_macWindow setDelegate:controller];
378
379 // [m_macWindow makeKeyAndOrderFront:nil];
380 }
381
382
383 WXWindow wxNonOwnedWindowCocoaImpl::GetWXWindow() const
384 {
385 return m_macWindow;
386 }
387
388 void wxNonOwnedWindowCocoaImpl::Raise()
389 {
390 [m_macWindow orderWindow:NSWindowAbove relativeTo:0];
391 }
392
393 void wxNonOwnedWindowCocoaImpl::Lower()
394 {
395 [m_macWindow orderWindow:NSWindowBelow relativeTo:0];
396 }
397
398 bool wxNonOwnedWindowCocoaImpl::Show(bool show)
399 {
400 if ( show )
401 {
402 [m_macWindow orderFront:nil];
403 [[m_macWindow contentView] setNeedsDisplay:YES];
404 }
405 else
406 [m_macWindow orderOut:nil];
407 return true;
408 }
409
410 bool wxNonOwnedWindowCocoaImpl::ShowWithEffect(bool show, wxShowEffect effect, unsigned timeout)
411 {
412 return Show(show);
413 }
414
415 void wxNonOwnedWindowCocoaImpl::Update()
416 {
417 [m_macWindow displayIfNeeded];
418 }
419
420 bool wxNonOwnedWindowCocoaImpl::SetTransparent(wxByte alpha)
421 {
422 [m_macWindow setAlphaValue:(CGFloat) alpha/255.0];
423 return true;
424 }
425
426 bool wxNonOwnedWindowCocoaImpl::SetBackgroundColour(const wxColour& col )
427 {
428 return true;
429 }
430
431 void wxNonOwnedWindowCocoaImpl::SetExtraStyle( long exStyle )
432 {
433 if ( m_macWindow )
434 {
435 bool metal = exStyle & wxFRAME_EX_METAL ;
436 int windowStyle = [ m_macWindow styleMask];
437 if ( metal && !(windowStyle & NSTexturedBackgroundWindowMask) )
438 {
439 wxFAIL_MSG( _T("Metal Style cannot be changed after creation") );
440 }
441 else if ( !metal && (windowStyle & NSTexturedBackgroundWindowMask ) )
442 {
443 wxFAIL_MSG( _T("Metal Style cannot be changed after creation") );
444 }
445 }
446 }
447
448 bool wxNonOwnedWindowCocoaImpl::SetBackgroundStyle(wxBackgroundStyle style)
449 {
450 return true;
451 }
452
453 bool wxNonOwnedWindowCocoaImpl::CanSetTransparent()
454 {
455 return true;
456 }
457
458 void wxNonOwnedWindowCocoaImpl::MoveWindow(int x, int y, int width, int height)
459 {
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()];
463 }
464
465 void wxNonOwnedWindowCocoaImpl::GetPosition( int &x, int &y ) const
466 {
467 wxRect r = wxFromNSRect( NULL, [m_macWindow frame] );
468 x = r.GetLeft();
469 y = r.GetTop();
470 }
471
472 void wxNonOwnedWindowCocoaImpl::GetSize( int &width, int &height ) const
473 {
474 NSRect rect = [m_macWindow frame];
475 width = rect.size.width;
476 height = rect.size.height;
477 }
478
479 void wxNonOwnedWindowCocoaImpl::GetContentArea( int& left, int &right, int &width, int &height ) const
480 {
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;
486 }
487
488 bool wxNonOwnedWindowCocoaImpl::SetShape(const wxRegion& region)
489 {
490 return false;
491 }
492
493 void wxNonOwnedWindowCocoaImpl::SetTitle( const wxString& title, wxFontEncoding encoding )
494 {
495 [m_macWindow setTitle:wxCFStringRef( title , encoding ).AsNSString()];
496 }
497
498 bool wxNonOwnedWindowCocoaImpl::IsMaximized() const
499 {
500 return [m_macWindow isZoomed];
501 }
502
503 bool wxNonOwnedWindowCocoaImpl::IsIconized() const
504 {
505 return [m_macWindow isMiniaturized];
506 }
507
508 void wxNonOwnedWindowCocoaImpl::Iconize( bool iconize )
509 {
510 if ( iconize )
511 [m_macWindow miniaturize:nil];
512 else
513 [m_macWindow deminiaturize:nil];
514 }
515
516 void wxNonOwnedWindowCocoaImpl::Maximize(bool maximize)
517 {
518 [m_macWindow zoom:nil];
519 }
520
521
522 // http://cocoadevcentral.com/articles/000028.php
523
524 typedef struct
525 {
526 int m_formerLevel;
527 NSRect m_formerFrame;
528 } FullScreenData ;
529
530 bool wxNonOwnedWindowCocoaImpl::IsFullScreen() const
531 {
532 return m_macFullScreenData != NULL ;
533 }
534
535 bool wxNonOwnedWindowCocoaImpl::ShowFullScreen(bool show, long style)
536 {
537 if ( show )
538 {
539 FullScreenData *data = (FullScreenData *)m_macFullScreenData ;
540 delete data ;
541 data = new FullScreenData();
542
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];
549 }
550 else if ( m_macFullScreenData != NULL )
551 {
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];
556 delete data ;
557 m_macFullScreenData = NULL ;
558 }
559
560 return true;
561 }
562
563 void wxNonOwnedWindowCocoaImpl::RequestUserAttention(int WXUNUSED(flags))
564 {
565 }
566
567 void wxNonOwnedWindowCocoaImpl::ScreenToWindow( int *x, int *y )
568 {
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);
574 if ( x )
575 *x = p.x;
576 if ( y )
577 *y = p.y;
578 }
579
580 void wxNonOwnedWindowCocoaImpl::WindowToScreen( int *x, int *y )
581 {
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);
587 if ( x )
588 *x = p.x;
589 if ( y )
590 *y = p.y;
591 }
592
593 wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, const wxPoint& pos, const wxSize& size,
594 long style, long extraStyle, const wxString& name )
595 {
596 wxNonOwnedWindowImpl* now = new wxNonOwnedWindowCocoaImpl( wxpeer );
597 now->Create( parent, pos, size, style , extraStyle, name );
598 return now;
599 }