]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/nonownedwnd.mm
adding idle processing in modal loop, closes #10871
[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 bool shouldHandleSelector(SEL selector)
61 {
62 if (selector == @selector(noop:)
63 || selector == @selector(complete:)
64 || selector == @selector(deleteBackward:)
65 || selector == @selector(deleteForward:)
66 || selector == @selector(insertNewline:)
67 || selector == @selector(insertTab:)
68 || selector == @selector(keyDown:)
69 || selector == @selector(keyUp:)
70 || selector == @selector(scrollPageUp:)
71 || selector == @selector(scrollPageDown:)
72 || selector == @selector(scrollToBeginningOfDocument:)
73 || selector == @selector(scrollToEndOfDocument:))
74 return false;
75
76 return true;
77
78 }
79
80 //
81 // wx native implementation classes
82 //
83
84 typedef void (*wxOSX_NoResponderHandlerPtr)(NSView* self, SEL _cmd, SEL selector);
85
86 @interface wxNSWindow : NSWindow
87 {
88 wxNonOwnedWindowCocoaImpl* impl;
89 }
90
91 - (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation;
92 - (wxNonOwnedWindowCocoaImpl*) implementation;
93 - (void)noResponderFor: (SEL) selector;
94 @end
95
96 @implementation wxNSWindow
97
98 - (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation
99 {
100 impl = theImplementation;
101 }
102
103 - (wxNonOwnedWindowCocoaImpl*) implementation
104 {
105 return impl;
106 }
107
108 - (void)doCommandBySelector:(SEL)selector
109 {
110 if (shouldHandleSelector(selector) &&
111 !(selector == @selector(cancel:) || selector == @selector(cancelOperation:)) )
112 [super doCommandBySelector:selector];
113 }
114
115
116 // NB: if we don't do this, all key downs that get handled lead to a NSBeep
117 - (void)noResponderFor: (SEL) selector
118 {
119 if (selector != @selector(keyDown:) && selector != @selector(keyUp:))
120 {
121 [super noResponderFor:selector];
122 // wxOSX_NoResponderHandlerPtr superimpl = (wxOSX_NoResponderHandlerPtr) [[self superclass] instanceMethodForSelector:@selector(noResponderFor:)];
123 // superimpl(self, @selector(noResponderFor:), selector);
124 }
125 }
126
127 @end
128
129 @interface wxNSPanel : NSPanel
130
131 {
132 wxNonOwnedWindowCocoaImpl* impl;
133 }
134
135 - (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation;
136 - (wxNonOwnedWindowCocoaImpl*) implementation;
137 - (void)noResponderFor: (SEL) selector;
138 @end
139
140 @implementation wxNSPanel
141
142 - (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation
143 {
144 impl = theImplementation;
145 }
146
147 - (BOOL)canBecomeKeyWindow
148 {
149 return YES;
150 }
151
152 - (wxNonOwnedWindowCocoaImpl*) implementation
153 {
154 return impl;
155 }
156
157 - (void)doCommandBySelector:(SEL)selector
158 {
159 if (shouldHandleSelector(selector))
160 [super doCommandBySelector:selector];
161 }
162
163 // NB: if we don't do this, it seems that all events that end here lead to a NSBeep
164 - (void)noResponderFor: (SEL) selector
165 {
166 if (selector != @selector(keyDown:) && selector != @selector(keyUp:))
167 {
168 [super noResponderFor:selector];
169 // wxOSX_NoResponderHandlerPtr superimpl = (wxOSX_NoResponderHandlerPtr) [[self superclass] instanceMethodForSelector:@selector(noResponderFor:)];
170 // superimpl(self, @selector(noResponderFor:), selector);
171 }
172 }
173
174 @end
175
176
177 //
178 // controller
179 //
180
181 @interface wxNonOwnedWindowController : NSObject
182 {
183 }
184
185 - (void)windowDidResize:(NSNotification *)notification;
186 - (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize;
187 - (void)windowDidResignKey:(NSNotification *)notification;
188 - (void)windowDidBecomeKey:(NSNotification *)notification;
189 - (void)windowDidMove:(NSNotification *)notification;
190 - (BOOL)windowShouldClose:(id)window;
191
192 @end
193
194 @implementation wxNonOwnedWindowController
195
196 - (id) init
197 {
198 [super init];
199 return self;
200 }
201
202 - (BOOL)windowShouldClose:(id)nwindow
203 {
204 wxNSWindow* window = (wxNSWindow*) nwindow;
205 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
206 if ( windowimpl )
207 {
208 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
209 if ( wxpeer )
210 wxpeer->Close();
211 }
212 return NO;
213 }
214
215 - (NSSize)windowWillResize:(NSWindow *)win
216 toSize:(NSSize)proposedFrameSize
217 {
218 NSRect frame = [win frame];
219 wxRect wxframe = wxFromNSRect( NULL, frame );
220 wxframe.SetWidth( proposedFrameSize.width );
221 wxframe.SetHeight( proposedFrameSize.height );
222 wxNSWindow* window = (wxNSWindow*) win;
223 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
224 if ( windowimpl )
225 {
226 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
227 if ( wxpeer )
228 {
229 wxpeer->HandleResizing( 0, &wxframe );
230 NSSize newSize = NSMakeSize(wxframe.GetWidth(), wxframe.GetHeight());
231 return newSize;
232 }
233 }
234
235 return proposedFrameSize;
236 }
237
238 - (void)windowDidResize:(NSNotification *)notification
239 {
240 wxNSWindow* window = (wxNSWindow*) [notification object];
241 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
242 if ( windowimpl )
243 {
244 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
245 if ( wxpeer )
246 wxpeer->HandleResized(0);
247 }
248 }
249
250 - (void)windowDidMove:(NSNotification *)notification
251 {
252 wxNSWindow* window = (wxNSWindow*) [notification object];
253 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
254 if ( windowimpl )
255 {
256 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
257 if ( wxpeer )
258 wxpeer->HandleMoved(0);
259 }
260 }
261
262 - (void)windowDidBecomeKey:(NSNotification *)notification
263 {
264 wxNSWindow* window = (wxNSWindow*) [notification object];
265 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
266 if ( windowimpl )
267 {
268 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
269 if ( wxpeer )
270 wxpeer->HandleActivated(0, true);
271 }
272 }
273
274 - (void)windowDidResignKey:(NSNotification *)notification
275 {
276 wxNSWindow* window = (wxNSWindow*) [notification object];
277 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
278 if ( windowimpl )
279 {
280 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
281 if ( wxpeer )
282 {
283 wxpeer->HandleActivated(0, false);
284 // Needed for popup window since the firstResponder
285 // (focus in wx) doesn't change when this
286 // TLW becomes inactive.
287 wxFocusEvent event( wxEVT_KILL_FOCUS, wxpeer->GetId());
288 event.SetEventObject(wxpeer);
289 wxpeer->HandleWindowEvent(event);
290 }
291 }
292 }
293
294 @end
295
296 IMPLEMENT_DYNAMIC_CLASS( wxNonOwnedWindowCocoaImpl , wxNonOwnedWindowImpl )
297
298 wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl( wxNonOwnedWindow* nonownedwnd) :
299 wxNonOwnedWindowImpl(nonownedwnd)
300 {
301 m_macWindow = NULL;
302 m_macFullScreenData = NULL;
303 }
304
305 wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl()
306 {
307 m_macWindow = NULL;
308 m_macFullScreenData = NULL;
309 }
310
311 wxNonOwnedWindowCocoaImpl::~wxNonOwnedWindowCocoaImpl()
312 {
313 [m_macWindow setImplementation:nil];
314 [m_macWindow setDelegate:nil];
315 [m_macWindow release];
316 }
317
318 void wxNonOwnedWindowCocoaImpl::Destroy()
319 {
320 wxPendingDelete.Append( new wxDeferredObjectDeleter( this ) );
321 }
322
323 void wxNonOwnedWindowCocoaImpl::Create( wxWindow* WXUNUSED(parent), const wxPoint& pos, const wxSize& size,
324 long style, long extraStyle, const wxString& WXUNUSED(name) )
325 {
326 static wxNonOwnedWindowController* controller = NULL;
327
328 if ( !controller )
329 controller =[[wxNonOwnedWindowController alloc] init];
330
331
332 int windowstyle = NSBorderlessWindowMask;
333
334 if ( style & wxFRAME_TOOL_WINDOW || ( style & wxPOPUP_WINDOW ) ||
335 GetWXPeer()->GetExtraStyle() & wxTOPLEVEL_EX_DIALOG )
336 {
337 m_macWindow = [wxNSPanel alloc];
338 }
339 else
340 m_macWindow = [wxNSWindow alloc];
341
342 CGWindowLevel level = kCGNormalWindowLevel;
343
344 if ( style & wxFRAME_TOOL_WINDOW )
345 {
346 windowstyle |= NSUtilityWindowMask;
347 if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) ||
348 ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) )
349 {
350 windowstyle |= NSTitledWindowMask ;
351 }
352 }
353 else if ( ( style & wxPOPUP_WINDOW ) )
354 {
355 level = kCGPopUpMenuWindowLevel;
356 /*
357 if ( ( style & wxBORDER_NONE ) )
358 {
359 wclass = kHelpWindowClass ; // has no border
360 attr |= kWindowNoShadowAttribute;
361 }
362 else
363 {
364 wclass = kPlainWindowClass ; // has a single line border, it will have to do for now
365 }
366 */
367 }
368 else if ( ( style & wxCAPTION ) )
369 {
370 windowstyle |= NSTitledWindowMask ;
371 }
372 else if ( ( style & wxFRAME_DRAWER ) )
373 {
374 /*
375 wclass = kDrawerWindowClass;
376 */
377 }
378 else
379 {
380 // set these even if we have no title, otherwise the controls won't be visible
381 if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) ||
382 ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) )
383 {
384 windowstyle |= NSTitledWindowMask ;
385 }
386 /*
387 else if ( ( style & wxNO_BORDER ) )
388 {
389 wclass = kSimpleWindowClass ;
390 }
391 else
392 {
393 wclass = kPlainWindowClass ;
394 }
395 */
396 }
397
398 if ( windowstyle & NSTitledWindowMask )
399 {
400 if ( ( style & wxMINIMIZE_BOX ) )
401 windowstyle |= NSMiniaturizableWindowMask ;
402
403 if ( ( style & wxMAXIMIZE_BOX ) )
404 windowstyle |= NSResizableWindowMask ; // TODO showing ZOOM ?
405
406 if ( ( style & wxRESIZE_BORDER ) )
407 windowstyle |= NSResizableWindowMask ;
408
409 if ( ( style & wxCLOSE_BOX) )
410 windowstyle |= NSClosableWindowMask ;
411 }
412 if ( extraStyle & wxFRAME_EX_METAL)
413 windowstyle |= NSTexturedBackgroundWindowMask;
414
415 if ( ( style & wxFRAME_FLOAT_ON_PARENT ) || ( style & wxFRAME_TOOL_WINDOW ) )
416 level = kCGFloatingWindowLevel;
417
418 if ( ( style & wxSTAY_ON_TOP ) )
419 level = kCGUtilityWindowLevel;
420
421 NSRect r = wxToNSRect( NULL, wxRect( pos, size) );
422
423 [m_macWindow setImplementation:this];
424
425 [m_macWindow initWithContentRect:r
426 styleMask:windowstyle
427 backing:NSBackingStoreBuffered
428 defer:NO
429 ];
430
431 [m_macWindow setLevel:level];
432
433 [m_macWindow setDelegate:controller];
434
435 [m_macWindow setAcceptsMouseMovedEvents: YES];
436 }
437
438
439 WXWindow wxNonOwnedWindowCocoaImpl::GetWXWindow() const
440 {
441 return m_macWindow;
442 }
443
444 void wxNonOwnedWindowCocoaImpl::Raise()
445 {
446 [m_macWindow orderWindow:NSWindowAbove relativeTo:0];
447 }
448
449 void wxNonOwnedWindowCocoaImpl::Lower()
450 {
451 [m_macWindow orderWindow:NSWindowBelow relativeTo:0];
452 }
453
454 bool wxNonOwnedWindowCocoaImpl::Show(bool show)
455 {
456 if ( show )
457 {
458 [m_macWindow makeKeyAndOrderFront:nil];
459 [[m_macWindow contentView] setNeedsDisplay:YES];
460 }
461 else
462 [m_macWindow orderOut:nil];
463 return true;
464 }
465
466 bool wxNonOwnedWindowCocoaImpl::ShowWithEffect(bool show, wxShowEffect WXUNUSED(effect), unsigned WXUNUSED(timeout))
467 {
468 return Show(show);
469 }
470
471 void wxNonOwnedWindowCocoaImpl::Update()
472 {
473 [m_macWindow displayIfNeeded];
474 }
475
476 bool wxNonOwnedWindowCocoaImpl::SetTransparent(wxByte alpha)
477 {
478 [m_macWindow setAlphaValue:(CGFloat) alpha/255.0];
479 return true;
480 }
481
482 bool wxNonOwnedWindowCocoaImpl::SetBackgroundColour(const wxColour& WXUNUSED(col) )
483 {
484 return true;
485 }
486
487 void wxNonOwnedWindowCocoaImpl::SetExtraStyle( long exStyle )
488 {
489 if ( m_macWindow )
490 {
491 bool metal = exStyle & wxFRAME_EX_METAL ;
492 int windowStyle = [ m_macWindow styleMask];
493 if ( metal && !(windowStyle & NSTexturedBackgroundWindowMask) )
494 {
495 wxFAIL_MSG( _T("Metal Style cannot be changed after creation") );
496 }
497 else if ( !metal && (windowStyle & NSTexturedBackgroundWindowMask ) )
498 {
499 wxFAIL_MSG( _T("Metal Style cannot be changed after creation") );
500 }
501 }
502 }
503
504 bool wxNonOwnedWindowCocoaImpl::SetBackgroundStyle(wxBackgroundStyle WXUNUSED(style))
505 {
506 return true;
507 }
508
509 bool wxNonOwnedWindowCocoaImpl::CanSetTransparent()
510 {
511 return true;
512 }
513
514 void wxNonOwnedWindowCocoaImpl::MoveWindow(int x, int y, int width, int height)
515 {
516 NSRect r = wxToNSRect( NULL, wxRect(x,y,width, height) );
517 // do not trigger refreshes upon invisible and possible partly created objects
518 [m_macWindow setFrame:r display:GetWXPeer()->IsShownOnScreen()];
519 }
520
521 void wxNonOwnedWindowCocoaImpl::GetPosition( int &x, int &y ) const
522 {
523 wxRect r = wxFromNSRect( NULL, [m_macWindow frame] );
524 x = r.GetLeft();
525 y = r.GetTop();
526 }
527
528 void wxNonOwnedWindowCocoaImpl::GetSize( int &width, int &height ) const
529 {
530 NSRect rect = [m_macWindow frame];
531 width = rect.size.width;
532 height = rect.size.height;
533 }
534
535 void wxNonOwnedWindowCocoaImpl::GetContentArea( int& left, int &top, int &width, int &height ) const
536 {
537 NSRect outer = NSMakeRect(100,100,100,100);
538 NSRect content = [NSWindow contentRectForFrameRect:outer styleMask:[m_macWindow styleMask] ];
539 NSRect rect = [[m_macWindow contentView] frame];
540 left = rect.origin.x;
541 top = rect.origin.y;
542 width = rect.size.width;
543 height = rect.size.height;
544 }
545
546 bool wxNonOwnedWindowCocoaImpl::SetShape(const wxRegion& WXUNUSED(region))
547 {
548 return false;
549 }
550
551 void wxNonOwnedWindowCocoaImpl::SetTitle( const wxString& title, wxFontEncoding encoding )
552 {
553 [m_macWindow setTitle:wxCFStringRef( title , encoding ).AsNSString()];
554 }
555
556 bool wxNonOwnedWindowCocoaImpl::IsMaximized() const
557 {
558 return [m_macWindow isZoomed];
559 }
560
561 bool wxNonOwnedWindowCocoaImpl::IsIconized() const
562 {
563 return [m_macWindow isMiniaturized];
564 }
565
566 void wxNonOwnedWindowCocoaImpl::Iconize( bool iconize )
567 {
568 if ( iconize )
569 [m_macWindow miniaturize:nil];
570 else
571 [m_macWindow deminiaturize:nil];
572 }
573
574 void wxNonOwnedWindowCocoaImpl::Maximize(bool WXUNUSED(maximize))
575 {
576 [m_macWindow zoom:nil];
577 }
578
579
580 // http://cocoadevcentral.com/articles/000028.php
581
582 typedef struct
583 {
584 int m_formerLevel;
585 NSRect m_formerFrame;
586 } FullScreenData ;
587
588 bool wxNonOwnedWindowCocoaImpl::IsFullScreen() const
589 {
590 return m_macFullScreenData != NULL ;
591 }
592
593 bool wxNonOwnedWindowCocoaImpl::ShowFullScreen(bool show, long WXUNUSED(style))
594 {
595 if ( show )
596 {
597 FullScreenData *data = (FullScreenData *)m_macFullScreenData ;
598 delete data ;
599 data = new FullScreenData();
600
601 m_macFullScreenData = data ;
602 data->m_formerLevel = [m_macWindow level];
603 data->m_formerFrame = [m_macWindow frame];
604 CGDisplayCapture( kCGDirectMainDisplay );
605 [m_macWindow setLevel:CGShieldingWindowLevel()];
606 [m_macWindow setFrame:[[NSScreen mainScreen] frame] display:YES];
607 }
608 else if ( m_macFullScreenData != NULL )
609 {
610 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
611 CGDisplayRelease( kCGDirectMainDisplay );
612 [m_macWindow setLevel:data->m_formerLevel];
613 [m_macWindow setFrame:data->m_formerFrame display:YES];
614 delete data ;
615 m_macFullScreenData = NULL ;
616 }
617
618 return true;
619 }
620
621 void wxNonOwnedWindowCocoaImpl::RequestUserAttention(int WXUNUSED(flags))
622 {
623 }
624
625 void wxNonOwnedWindowCocoaImpl::ScreenToWindow( int *x, int *y )
626 {
627 wxPoint p((x ? *x : 0), (y ? *y : 0) );
628 NSPoint nspt = wxToNSPoint( NULL, p );
629 nspt = [m_macWindow convertScreenToBase:nspt];
630 nspt = [[m_macWindow contentView] convertPoint:nspt fromView:nil];
631 p = wxFromNSPoint([m_macWindow contentView], nspt);
632 if ( x )
633 *x = p.x;
634 if ( y )
635 *y = p.y;
636 }
637
638 void wxNonOwnedWindowCocoaImpl::WindowToScreen( int *x, int *y )
639 {
640 wxPoint p((x ? *x : 0), (y ? *y : 0) );
641 NSPoint nspt = wxToNSPoint( [m_macWindow contentView], p );
642 nspt = [[m_macWindow contentView] convertPoint:nspt toView:nil];
643 nspt = [m_macWindow convertBaseToScreen:nspt];
644 p = wxFromNSPoint( NULL, nspt);
645 if ( x )
646 *x = p.x;
647 if ( y )
648 *y = p.y;
649 }
650
651 wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, const wxPoint& pos, const wxSize& size,
652 long style, long extraStyle, const wxString& name )
653 {
654 wxNonOwnedWindowImpl* now = new wxNonOwnedWindowCocoaImpl( wxpeer );
655 now->Create( parent, pos, size, style , extraStyle, name );
656 return now;
657 }