]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/nonownedwnd.mm
adapting new osx modifier handling, fixes #14377
[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$
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 #include "wx/app.h"
17 #include "wx/dialog.h"
18 #include "wx/menuitem.h"
19 #include "wx/menu.h"
20 #endif
21
22 #include "wx/osx/private.h"
23
24 NSScreen* wxOSXGetMenuScreen()
25 {
26 if ( [NSScreen screens] == nil )
27 return [NSScreen mainScreen];
28 else
29 {
30 return [[NSScreen screens] objectAtIndex:0];
31 }
32 }
33
34 NSRect wxToNSRect( NSView* parent, const wxRect& r )
35 {
36 NSRect frame = parent ? [parent bounds] : [wxOSXGetMenuScreen() frame];
37 int y = r.y;
38 int x = r.x ;
39 if ( parent == NULL || ![ parent isFlipped ] )
40 y = (int)(frame.size.height - ( r.y + r.height ));
41 return NSMakeRect(x, y, r.width , r.height);
42 }
43
44 wxRect wxFromNSRect( NSView* parent, const NSRect& rect )
45 {
46 NSRect frame = parent ? [parent bounds] : [wxOSXGetMenuScreen() frame];
47 int y = (int)rect.origin.y;
48 int x = (int)rect.origin.x;
49 if ( parent == NULL || ![ parent isFlipped ] )
50 y = (int)(frame.size.height - (rect.origin.y + rect.size.height));
51 return wxRect( x, y, (int)rect.size.width, (int)rect.size.height );
52 }
53
54 NSPoint wxToNSPoint( NSView* parent, const wxPoint& p )
55 {
56 NSRect frame = parent ? [parent bounds] : [wxOSXGetMenuScreen() frame];
57 int x = p.x ;
58 int y = p.y;
59 if ( parent == NULL || ![ parent isFlipped ] )
60 y = (int)(frame.size.height - ( p.y ));
61 return NSMakePoint(x, y);
62 }
63
64 wxPoint wxFromNSPoint( NSView* parent, const NSPoint& p )
65 {
66 NSRect frame = parent ? [parent bounds] : [wxOSXGetMenuScreen() frame];
67 int x = (int)p.x;
68 int y = (int)p.y;
69 if ( parent == NULL || ![ parent isFlipped ] )
70 y = (int)(frame.size.height - ( p.y ));
71 return wxPoint( x, y);
72 }
73
74 bool shouldHandleSelector(SEL selector)
75 {
76 if (selector == @selector(noop:)
77 || selector == @selector(complete:)
78 || selector == @selector(deleteBackward:)
79 || selector == @selector(deleteForward:)
80 || selector == @selector(insertNewline:)
81 || selector == @selector(insertTab:)
82 || selector == @selector(insertBacktab:)
83 || selector == @selector(keyDown:)
84 || selector == @selector(keyUp:)
85 || selector == @selector(scrollPageUp:)
86 || selector == @selector(scrollPageDown:)
87 || selector == @selector(scrollToBeginningOfDocument:)
88 || selector == @selector(scrollToEndOfDocument:))
89 return false;
90
91 return true;
92
93 }
94
95 //
96 // wx category for NSWindow (our own and wrapped instances)
97 //
98
99 @interface NSWindow (wxNSWindowSupport)
100
101 - (wxNonOwnedWindowCocoaImpl*) WX_implementation;
102
103 - (bool) WX_filterSendEvent:(NSEvent *) event;
104
105 @end
106
107 @implementation NSWindow (wxNSWindowSupport)
108
109 - (wxNonOwnedWindowCocoaImpl*) WX_implementation
110 {
111 return (wxNonOwnedWindowCocoaImpl*) wxNonOwnedWindowImpl::FindFromWXWindow( self );
112 }
113
114 // TODO in cocoa everything during a drag is sent to the NSWindow the mouse down occured,
115 // this does not conform to the wx behaviour if the window is not captured, so try to resend
116 // or capture all wx mouse event handling at the tlw as we did for carbon
117
118 - (bool) WX_filterSendEvent:(NSEvent *) event
119 {
120 bool handled = false;
121 if ( ([event type] >= NSLeftMouseDown) && ([event type] <= NSMouseExited) )
122 {
123 WXEVENTREF formerEvent = wxTheApp == NULL ? NULL : wxTheApp->MacGetCurrentEvent();
124 WXEVENTHANDLERCALLREF formerHandler = wxTheApp == NULL ? NULL : wxTheApp->MacGetCurrentEventHandlerCallRef();
125
126 wxWindow* cw = wxWindow::GetCapture();
127 if ( cw != NULL )
128 {
129 if (wxTheApp)
130 wxTheApp->MacSetCurrentEvent(event, NULL);
131 ((wxWidgetCocoaImpl*)cw->GetPeer())->DoHandleMouseEvent( event);
132 handled = true;
133 }
134 if ( handled )
135 {
136 if (wxTheApp)
137 wxTheApp->MacSetCurrentEvent(formerEvent , formerHandler);
138 }
139 }
140 return handled;
141 }
142 @end
143
144 //
145 // wx native implementation
146 //
147
148 @interface wxNSWindow : NSWindow
149 {
150 }
151
152 - (void) sendEvent:(NSEvent *)event;
153 - (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen *)screen;
154 - (void)noResponderFor: (SEL) selector;
155 @end
156
157 @implementation wxNSWindow
158
159 - (void)sendEvent:(NSEvent *) event
160 {
161 if ( ![self WX_filterSendEvent: event] )
162 {
163 WXEVENTREF formerEvent = wxTheApp == NULL ? NULL : wxTheApp->MacGetCurrentEvent();
164 WXEVENTHANDLERCALLREF formerHandler = wxTheApp == NULL ? NULL : wxTheApp->MacGetCurrentEventHandlerCallRef();
165
166 if (wxTheApp)
167 wxTheApp->MacSetCurrentEvent(event, NULL);
168
169 [super sendEvent: event];
170
171 if (wxTheApp)
172 wxTheApp->MacSetCurrentEvent(formerEvent , formerHandler);
173 }
174 }
175
176 // The default implementation always moves the window back onto the screen,
177 // even when the programmer explicitly wants to hide it.
178 - (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen *)screen
179 {
180 wxUnusedVar(screen);
181 return frameRect;
182 }
183
184 - (void)doCommandBySelector:(SEL)selector
185 {
186 if (shouldHandleSelector(selector) &&
187 !(selector == @selector(cancel:) || selector == @selector(cancelOperation:)) )
188 [super doCommandBySelector:selector];
189 }
190
191
192 // NB: if we don't do this, all key downs that get handled lead to a NSBeep
193 - (void)noResponderFor: (SEL) selector
194 {
195 if (selector != @selector(keyDown:) && selector != @selector(keyUp:))
196 {
197 [super noResponderFor:selector];
198 }
199 }
200
201 // We need this for borderless windows, i.e. shaped windows or windows without
202 // a title bar. For more info, see:
203 // http://lists.apple.com/archives/cocoa-dev/2008/May/msg02091.html
204 - (BOOL)canBecomeKeyWindow
205 {
206 return YES;
207 }
208
209 @end
210
211 @interface wxNSPanel : NSPanel
212 {
213 }
214
215 - (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen *)screen;
216 - (void)noResponderFor: (SEL) selector;
217 - (void)sendEvent:(NSEvent *)event;
218 @end
219
220 @implementation wxNSPanel
221
222 - (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen *)screen
223 {
224 wxNonOwnedWindowCocoaImpl* impl = (wxNonOwnedWindowCocoaImpl*) wxNonOwnedWindowImpl::FindFromWXWindow( self );
225 if (impl && impl->IsFullScreen())
226 return frameRect;
227 else
228 return [super constrainFrameRect:frameRect toScreen:screen];
229 }
230
231 - (BOOL)canBecomeKeyWindow
232 {
233 return YES;
234 }
235
236 - (void)doCommandBySelector:(SEL)selector
237 {
238 if (shouldHandleSelector(selector))
239 [super doCommandBySelector:selector];
240 }
241
242 // NB: if we don't do this, it seems that all events that end here lead to a NSBeep
243 - (void)noResponderFor: (SEL) selector
244 {
245 if (selector != @selector(keyDown:) && selector != @selector(keyUp:))
246 {
247 [super noResponderFor:selector];
248 }
249 }
250
251 - (void)sendEvent:(NSEvent *) event
252 {
253 if ( ![self WX_filterSendEvent: event] )
254 {
255 WXEVENTREF formerEvent = wxTheApp == NULL ? NULL : wxTheApp->MacGetCurrentEvent();
256 WXEVENTHANDLERCALLREF formerHandler = wxTheApp == NULL ? NULL : wxTheApp->MacGetCurrentEventHandlerCallRef();
257
258 if (wxTheApp)
259 wxTheApp->MacSetCurrentEvent(event, NULL);
260
261 [super sendEvent: event];
262
263 if (wxTheApp)
264 wxTheApp->MacSetCurrentEvent(formerEvent , formerHandler);
265 }
266 }
267
268 @end
269
270
271 //
272 // controller
273 //
274
275 @interface wxNonOwnedWindowController : NSObject wxOSX_10_6_AND_LATER(<NSWindowDelegate>)
276 {
277 }
278
279 - (void)windowDidResize:(NSNotification *)notification;
280 - (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize;
281 - (void)windowDidResignKey:(NSNotification *)notification;
282 - (void)windowDidBecomeKey:(NSNotification *)notification;
283 - (void)windowDidMove:(NSNotification *)notification;
284 - (BOOL)windowShouldClose:(id)window;
285 - (BOOL)windowShouldZoom:(NSWindow *)window toFrame:(NSRect)newFrame;
286
287 @end
288
289 extern int wxOSXGetIdFromSelector(SEL action );
290
291 @implementation wxNonOwnedWindowController
292
293 - (id) init
294 {
295 self = [super init];
296 return self;
297 }
298
299 - (BOOL) triggerMenu:(SEL) action
300 {
301 wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar();
302 if ( mbar )
303 {
304 wxMenu* menu = NULL;
305 wxMenuItem* menuitem = mbar->FindItem(wxOSXGetIdFromSelector(action), &menu);
306 if ( menu != NULL && menuitem != NULL)
307 return menu->HandleCommandProcess(menuitem);
308 }
309 return NO;
310 }
311
312 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
313 {
314 SEL action = [menuItem action];
315
316 wxMenuBar* mbar = wxMenuBar::MacGetInstalledMenuBar();
317 if ( mbar )
318 {
319 wxMenu* menu = NULL;
320 wxMenuItem* menuitem = mbar->FindItem(wxOSXGetIdFromSelector(action), &menu);
321 if ( menu != NULL && menuitem != NULL)
322 {
323 menu->HandleCommandUpdateStatus(menuitem);
324 return menuitem->IsEnabled();
325 }
326 }
327 return YES;
328 }
329
330 - (void)undo:(id)sender
331 {
332 wxUnusedVar(sender);
333 [self triggerMenu:_cmd];
334 }
335
336 - (void)redo:(id)sender
337 {
338 wxUnusedVar(sender);
339 [self triggerMenu:_cmd];
340 }
341
342 - (void)cut:(id)sender
343 {
344 wxUnusedVar(sender);
345 [self triggerMenu:_cmd];
346 }
347
348 - (void)copy:(id)sender
349 {
350 wxUnusedVar(sender);
351 [self triggerMenu:_cmd];
352 }
353
354 - (void)paste:(id)sender
355 {
356 wxUnusedVar(sender);
357 [self triggerMenu:_cmd];
358 }
359
360 - (void)delete:(id)sender
361 {
362 wxUnusedVar(sender);
363 [self triggerMenu:_cmd];
364 }
365
366 - (void)selectAll:(id)sender
367 {
368 wxUnusedVar(sender);
369 [self triggerMenu:_cmd];
370 }
371
372 - (BOOL)windowShouldClose:(id)nwindow
373 {
374 wxNonOwnedWindowCocoaImpl* windowimpl = [(NSWindow*) nwindow WX_implementation];
375 if ( windowimpl )
376 {
377 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
378 if ( wxpeer )
379 wxpeer->Close();
380 }
381 return NO;
382 }
383
384 - (NSSize)windowWillResize:(NSWindow *)window
385 toSize:(NSSize)proposedFrameSize
386 {
387 NSRect frame = [window frame];
388 wxRect wxframe = wxFromNSRect( NULL, frame );
389 wxframe.SetWidth( (int)proposedFrameSize.width );
390 wxframe.SetHeight( (int)proposedFrameSize.height );
391
392 wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
393 if ( windowimpl )
394 {
395 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
396 if ( wxpeer )
397 {
398 wxpeer->HandleResizing( 0, &wxframe );
399 NSSize newSize = NSMakeSize(wxframe.GetWidth(), wxframe.GetHeight());
400 return newSize;
401 }
402 }
403
404 return proposedFrameSize;
405 }
406
407 - (void)windowDidResize:(NSNotification *)notification
408 {
409 NSWindow* window = (NSWindow*) [notification object];
410 wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
411 if ( windowimpl )
412 {
413 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
414 if ( wxpeer )
415 wxpeer->HandleResized(0);
416 }
417 }
418
419 - (void)windowDidMove:(NSNotification *)notification
420 {
421 wxNSWindow* window = (wxNSWindow*) [notification object];
422 wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
423 if ( windowimpl )
424 {
425 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
426 if ( wxpeer )
427 wxpeer->HandleMoved(0);
428 }
429 }
430
431 - (void)windowDidBecomeKey:(NSNotification *)notification
432 {
433 NSWindow* window = (NSWindow*) [notification object];
434 wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
435 if ( windowimpl )
436 {
437 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
438 if ( wxpeer )
439 wxpeer->HandleActivated(0, true);
440 }
441 }
442
443 - (void)windowDidResignKey:(NSNotification *)notification
444 {
445 NSWindow* window = (NSWindow*) [notification object];
446 wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
447 if ( windowimpl )
448 {
449 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
450 if ( wxpeer )
451 {
452 wxpeer->HandleActivated(0, false);
453 // Needed for popup window since the firstResponder
454 // (focus in wx) doesn't change when this
455 // TLW becomes inactive.
456 wxFocusEvent event( wxEVT_KILL_FOCUS, wxpeer->GetId());
457 event.SetEventObject(wxpeer);
458 wxpeer->HandleWindowEvent(event);
459 }
460 }
461 }
462
463 - (id)windowWillReturnFieldEditor:(NSWindow *)sender toObject:(id)anObject
464 {
465 wxUnusedVar(sender);
466
467 if ([anObject isKindOfClass:[wxNSTextField class]])
468 {
469 wxNSTextField* tf = (wxNSTextField*) anObject;
470 wxNSTextFieldEditor* editor = [tf fieldEditor];
471 if ( editor == nil )
472 {
473 editor = [[wxNSTextFieldEditor alloc] init];
474 [editor setFieldEditor:YES];
475 [tf setFieldEditor:editor];
476 [editor release];
477 }
478 return editor;
479 }
480
481 return nil;
482 }
483
484 - (BOOL)windowShouldZoom:(NSWindow *)window toFrame:(NSRect)newFrame
485 {
486 wxUnusedVar(newFrame);
487 wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
488 if ( windowimpl )
489 {
490 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
491 wxMaximizeEvent event(wxpeer->GetId());
492 event.SetEventObject(wxpeer);
493 return !wxpeer->HandleWindowEvent(event);
494 }
495 return true;
496 }
497
498 @end
499
500 IMPLEMENT_DYNAMIC_CLASS( wxNonOwnedWindowCocoaImpl , wxNonOwnedWindowImpl )
501
502 wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl( wxNonOwnedWindow* nonownedwnd) :
503 wxNonOwnedWindowImpl(nonownedwnd)
504 {
505 m_macWindow = NULL;
506 m_macFullScreenData = NULL;
507 }
508
509 wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl()
510 {
511 m_macWindow = NULL;
512 m_macFullScreenData = NULL;
513 }
514
515 wxNonOwnedWindowCocoaImpl::~wxNonOwnedWindowCocoaImpl()
516 {
517 if ( !m_wxPeer->IsNativeWindowWrapper() )
518 {
519 [m_macWindow setDelegate:nil];
520
521 // make sure we remove this first, otherwise the ref count will not lead to the
522 // native window's destruction
523 if ([m_macWindow parentWindow] != 0)
524 [[m_macWindow parentWindow] removeChildWindow: m_macWindow];
525
526 [m_macWindow release];
527 }
528 }
529
530 void wxNonOwnedWindowCocoaImpl::WillBeDestroyed()
531 {
532 if ( !m_wxPeer->IsNativeWindowWrapper() )
533 {
534 [m_macWindow setDelegate:nil];
535 }
536 }
537
538 void wxNonOwnedWindowCocoaImpl::Create( wxWindow* parent, const wxPoint& pos, const wxSize& size,
539 long style, long extraStyle, const wxString& WXUNUSED(name) )
540 {
541 static wxNonOwnedWindowController* controller = NULL;
542
543 if ( !controller )
544 controller =[[wxNonOwnedWindowController alloc] init];
545
546
547 int windowstyle = NSBorderlessWindowMask;
548
549 if ( style & wxFRAME_TOOL_WINDOW || ( style & wxPOPUP_WINDOW ) ||
550 GetWXPeer()->GetExtraStyle() & wxTOPLEVEL_EX_DIALOG )
551 {
552 m_macWindow = [wxNSPanel alloc];
553 }
554 else
555 m_macWindow = [wxNSWindow alloc];
556
557 CGWindowLevel level = kCGNormalWindowLevel;
558
559 if ( style & wxFRAME_TOOL_WINDOW )
560 {
561 windowstyle |= NSUtilityWindowMask;
562 }
563 else if ( ( style & wxPOPUP_WINDOW ) )
564 {
565 level = kCGPopUpMenuWindowLevel;
566 }
567 else if ( ( style & wxFRAME_DRAWER ) )
568 {
569 /*
570 wclass = kDrawerWindowClass;
571 */
572 }
573
574 if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) ||
575 ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) || ( style & wxCAPTION ) )
576 {
577 windowstyle |= NSTitledWindowMask ;
578 if ( ( style & wxMINIMIZE_BOX ) )
579 windowstyle |= NSMiniaturizableWindowMask ;
580
581 if ( ( style & wxMAXIMIZE_BOX ) )
582 windowstyle |= NSResizableWindowMask ;
583
584 if ( ( style & wxCLOSE_BOX) )
585 windowstyle |= NSClosableWindowMask ;
586 }
587
588 if ( ( style & wxRESIZE_BORDER ) )
589 windowstyle |= NSResizableWindowMask ;
590
591 if ( extraStyle & wxFRAME_EX_METAL)
592 windowstyle |= NSTexturedBackgroundWindowMask;
593
594 if ( ( style & wxFRAME_FLOAT_ON_PARENT ) || ( style & wxFRAME_TOOL_WINDOW ) )
595 level = kCGFloatingWindowLevel;
596
597 if ( ( style & wxSTAY_ON_TOP ) )
598 level = kCGUtilityWindowLevel;
599
600 NSRect r = wxToNSRect( NULL, wxRect( pos, size) );
601
602 r = [NSWindow contentRectForFrameRect:r styleMask:windowstyle];
603
604 [m_macWindow initWithContentRect:r
605 styleMask:windowstyle
606 backing:NSBackingStoreBuffered
607 defer:NO
608 ];
609
610 // if we just have a title bar with no buttons needed, hide them
611 if ( (windowstyle & NSTitledWindowMask) &&
612 !(style & wxCLOSE_BOX) && !(style & wxMAXIMIZE_BOX) && !(style & wxMINIMIZE_BOX) )
613 {
614 [[m_macWindow standardWindowButton:NSWindowZoomButton] setHidden:YES];
615 [[m_macWindow standardWindowButton:NSWindowCloseButton] setHidden:YES];
616 [[m_macWindow standardWindowButton:NSWindowMiniaturizeButton] setHidden:YES];
617 }
618
619 // If the parent is modal, windows with wxFRAME_FLOAT_ON_PARENT style need
620 // to be in kCGUtilityWindowLevel and not kCGFloatingWindowLevel to stay
621 // above the parent.
622 wxDialog * const parentDialog = wxDynamicCast(parent, wxDialog);
623 if (parentDialog && parentDialog->IsModal())
624 {
625 if (level == kCGFloatingWindowLevel)
626 {
627 level = kCGUtilityWindowLevel;
628 }
629
630 // Cocoa's modal loop does not process other windows by default, but
631 // don't call this on normal window levels so nested modal dialogs will
632 // still behave modally.
633 if (level != kCGNormalWindowLevel)
634 {
635 if ([m_macWindow isKindOfClass:[NSPanel class]])
636 {
637 [(NSPanel*)m_macWindow setWorksWhenModal:YES];
638 }
639 }
640 }
641
642 [m_macWindow setLevel:level];
643 m_macWindowLevel = level;
644
645 [m_macWindow setDelegate:controller];
646
647 if ( ( style & wxFRAME_SHAPED) )
648 {
649 [m_macWindow setOpaque:NO];
650 [m_macWindow setAlphaValue:1.0];
651 }
652
653 if ( !(style & wxFRAME_TOOL_WINDOW) )
654 [m_macWindow setHidesOnDeactivate:NO];
655 }
656
657 void wxNonOwnedWindowCocoaImpl::Create( wxWindow* WXUNUSED(parent), WXWindow nativeWindow )
658 {
659 m_macWindow = nativeWindow;
660 }
661
662 WXWindow wxNonOwnedWindowCocoaImpl::GetWXWindow() const
663 {
664 return m_macWindow;
665 }
666
667 void wxNonOwnedWindowCocoaImpl::Raise()
668 {
669 [m_macWindow makeKeyAndOrderFront:nil];
670 }
671
672 void wxNonOwnedWindowCocoaImpl::Lower()
673 {
674 [m_macWindow orderWindow:NSWindowBelow relativeTo:0];
675 }
676
677 void wxNonOwnedWindowCocoaImpl::ShowWithoutActivating()
678 {
679 [m_macWindow orderFront:nil];
680 [[m_macWindow contentView] setNeedsDisplay: YES];
681 }
682
683 bool wxNonOwnedWindowCocoaImpl::Show(bool show)
684 {
685 if ( show )
686 {
687 wxNonOwnedWindow* wxpeer = GetWXPeer();
688 if ( wxpeer )
689 {
690 // add to parent window before showing
691 if ( wxpeer->GetParent() )
692 {
693 NSView * parentView = wxpeer->GetParent()->GetPeer()->GetWXWidget();
694 if ( parentView )
695 {
696 NSWindow* parentNSWindow = [parentView window];
697 if ( parentNSWindow )
698 [parentNSWindow addChildWindow:m_macWindow ordered:NSWindowAbove];
699 }
700 }
701
702 if (!(wxpeer->GetWindowStyle() & wxFRAME_TOOL_WINDOW))
703 [m_macWindow makeKeyAndOrderFront:nil];
704 else
705 [m_macWindow orderFront:nil];
706 }
707 [[m_macWindow contentView] setNeedsDisplay: YES];
708 }
709 else
710 {
711 // avoid propagation of orderOut to parent
712 if ([m_macWindow parentWindow] != 0)
713 [[m_macWindow parentWindow] removeChildWindow: m_macWindow];
714 [m_macWindow orderOut:nil];
715 }
716 return true;
717 }
718
719 bool wxNonOwnedWindowCocoaImpl::ShowWithEffect(bool show,
720 wxShowEffect effect,
721 unsigned timeout)
722 {
723 return wxWidgetCocoaImpl::
724 ShowViewOrWindowWithEffect(m_wxPeer, show, effect, timeout);
725 }
726
727 void wxNonOwnedWindowCocoaImpl::Update()
728 {
729 [m_macWindow displayIfNeeded];
730 }
731
732 bool wxNonOwnedWindowCocoaImpl::SetTransparent(wxByte alpha)
733 {
734 [m_macWindow setAlphaValue:(CGFloat) alpha/255.0];
735 return true;
736 }
737
738 bool wxNonOwnedWindowCocoaImpl::SetBackgroundColour(const wxColour& col )
739 {
740 [m_macWindow setBackgroundColor:[NSColor colorWithCalibratedRed:(CGFloat) (col.Red() / 255.0)
741 green:(CGFloat) (col.Green() / 255.0)
742 blue:(CGFloat) (col.Blue() / 255.0)
743 alpha:(CGFloat) (col.Alpha() / 255.0)]];
744 return true;
745 }
746
747 void wxNonOwnedWindowCocoaImpl::SetExtraStyle( long exStyle )
748 {
749 if ( m_macWindow )
750 {
751 bool metal = exStyle & wxFRAME_EX_METAL ;
752 int windowStyle = [ m_macWindow styleMask];
753 if ( metal && !(windowStyle & NSTexturedBackgroundWindowMask) )
754 {
755 wxFAIL_MSG( wxT("Metal Style cannot be changed after creation") );
756 }
757 else if ( !metal && (windowStyle & NSTexturedBackgroundWindowMask ) )
758 {
759 wxFAIL_MSG( wxT("Metal Style cannot be changed after creation") );
760 }
761 }
762 }
763
764 void wxNonOwnedWindowCocoaImpl::SetWindowStyleFlag( long style )
765 {
766 if (m_macWindow)
767 {
768 CGWindowLevel level = kCGNormalWindowLevel;
769
770 if (style & wxSTAY_ON_TOP)
771 level = kCGUtilityWindowLevel;
772 else if (( style & wxFRAME_FLOAT_ON_PARENT ) || ( style & wxFRAME_TOOL_WINDOW ))
773 level = kCGFloatingWindowLevel;
774
775 [m_macWindow setLevel: level];
776 m_macWindowLevel = level;
777 }
778 }
779
780 bool wxNonOwnedWindowCocoaImpl::SetBackgroundStyle(wxBackgroundStyle style)
781 {
782 if ( style == wxBG_STYLE_TRANSPARENT )
783 {
784 [m_macWindow setOpaque:NO];
785 [m_macWindow setBackgroundColor:[NSColor clearColor]];
786 }
787
788 return true;
789 }
790
791 bool wxNonOwnedWindowCocoaImpl::CanSetTransparent()
792 {
793 return true;
794 }
795
796 void wxNonOwnedWindowCocoaImpl::MoveWindow(int x, int y, int width, int height)
797 {
798 NSRect r = wxToNSRect( NULL, wxRect(x,y,width, height) );
799 // do not trigger refreshes upon invisible and possible partly created objects
800 [m_macWindow setFrame:r display:GetWXPeer()->IsShownOnScreen()];
801 }
802
803 void wxNonOwnedWindowCocoaImpl::GetPosition( int &x, int &y ) const
804 {
805 wxRect r = wxFromNSRect( NULL, [m_macWindow frame] );
806 x = r.GetLeft();
807 y = r.GetTop();
808 }
809
810 void wxNonOwnedWindowCocoaImpl::GetSize( int &width, int &height ) const
811 {
812 NSRect rect = [m_macWindow frame];
813 width = (int)rect.size.width;
814 height = (int)rect.size.height;
815 }
816
817 void wxNonOwnedWindowCocoaImpl::GetContentArea( int& left, int &top, int &width, int &height ) const
818 {
819 NSRect rect = [[m_macWindow contentView] frame];
820 left = (int)rect.origin.x;
821 top = (int)rect.origin.y;
822 width = (int)rect.size.width;
823 height = (int)rect.size.height;
824 }
825
826 bool wxNonOwnedWindowCocoaImpl::SetShape(const wxRegion& WXUNUSED(region))
827 {
828 [m_macWindow setOpaque:NO];
829 [m_macWindow setBackgroundColor:[NSColor clearColor]];
830
831 return true;
832 }
833
834 void wxNonOwnedWindowCocoaImpl::SetTitle( const wxString& title, wxFontEncoding encoding )
835 {
836 [m_macWindow setTitle:wxCFStringRef( title , encoding ).AsNSString()];
837 }
838
839 bool wxNonOwnedWindowCocoaImpl::IsMaximized() const
840 {
841 if (([m_macWindow styleMask] & NSResizableWindowMask) != 0)
842 {
843 return [m_macWindow isZoomed];
844 }
845 else
846 {
847 NSRect rectScreen = [[NSScreen mainScreen] visibleFrame];
848 NSRect rectWindow = [m_macWindow frame];
849 return (rectScreen.origin.x == rectWindow.origin.x &&
850 rectScreen.origin.y == rectWindow.origin.y &&
851 rectScreen.size.width == rectWindow.size.width &&
852 rectScreen.size.height == rectWindow.size.height);
853 }
854 }
855
856 bool wxNonOwnedWindowCocoaImpl::IsIconized() const
857 {
858 return [m_macWindow isMiniaturized];
859 }
860
861 void wxNonOwnedWindowCocoaImpl::Iconize( bool iconize )
862 {
863 if ( iconize )
864 [m_macWindow miniaturize:nil];
865 else
866 [m_macWindow deminiaturize:nil];
867 }
868
869 void wxNonOwnedWindowCocoaImpl::Maximize(bool WXUNUSED(maximize))
870 {
871 [m_macWindow zoom:nil];
872 }
873
874
875 // http://cocoadevcentral.com/articles/000028.php
876
877 typedef struct
878 {
879 NSUInteger m_formerStyleMask;
880 int m_formerLevel;
881 NSRect m_formerFrame;
882 } FullScreenData ;
883
884 bool wxNonOwnedWindowCocoaImpl::IsFullScreen() const
885 {
886 return m_macFullScreenData != NULL ;
887 }
888
889 bool wxNonOwnedWindowCocoaImpl::ShowFullScreen(bool show, long WXUNUSED(style))
890 {
891 if ( show )
892 {
893 FullScreenData *data = (FullScreenData *)m_macFullScreenData ;
894 delete data ;
895 data = new FullScreenData();
896
897 m_macFullScreenData = data ;
898 data->m_formerLevel = [m_macWindow level];
899 data->m_formerFrame = [m_macWindow frame];
900 data->m_formerStyleMask = [m_macWindow styleMask];
901 #if 0
902 // CGDisplayCapture( kCGDirectMainDisplay );
903 //[m_macWindow setLevel:NSMainMenuWindowLevel+1/*CGShieldingWindowLevel()*/];
904 #endif
905 NSRect screenframe = [[NSScreen mainScreen] frame];
906 NSRect frame = NSMakeRect (0, 0, 100, 100);
907 NSRect contentRect;
908
909 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
910 if ( [ m_macWindow respondsToSelector:@selector(setStyleMask:) ] )
911 [m_macWindow setStyleMask:data->m_formerStyleMask & ~ NSResizableWindowMask];
912 #endif
913
914 contentRect = [NSWindow contentRectForFrameRect: frame
915 styleMask: [m_macWindow styleMask]];
916 screenframe.origin.y += (frame.origin.y - contentRect.origin.y);
917 screenframe.size.height += (frame.size.height - contentRect.size.height);
918 [m_macWindow setFrame:screenframe display:YES];
919
920 SetSystemUIMode(kUIModeAllHidden,
921 kUIOptionDisableAppleMenu
922 /*
923 | kUIOptionDisableProcessSwitch
924 | kUIOptionDisableForceQuit
925 */);
926 }
927 else if ( m_macFullScreenData != NULL )
928 {
929 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
930 #if 0
931 // CGDisplayRelease( kCGDirectMainDisplay );
932 // [m_macWindow setLevel:data->m_formerLevel];
933 #endif
934
935 [m_macWindow setFrame:data->m_formerFrame display:YES];
936 #if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6
937 if ( [ m_macWindow respondsToSelector:@selector(setStyleMask:) ] )
938 [m_macWindow setStyleMask:data->m_formerStyleMask];
939 #endif
940 delete data ;
941 m_macFullScreenData = NULL ;
942
943 SetSystemUIMode(kUIModeNormal, 0);
944 }
945
946 return true;
947 }
948
949 void wxNonOwnedWindowCocoaImpl::RequestUserAttention(int flagsWX)
950 {
951 NSRequestUserAttentionType flagsOSX;
952 switch ( flagsWX )
953 {
954 case wxUSER_ATTENTION_INFO:
955 flagsOSX = NSInformationalRequest;
956 break;
957
958 case wxUSER_ATTENTION_ERROR:
959 flagsOSX = NSCriticalRequest;
960 break;
961
962 default:
963 wxFAIL_MSG( "invalid RequestUserAttention() flags" );
964 return;
965 }
966
967 [NSApp requestUserAttention:flagsOSX];
968 }
969
970 void wxNonOwnedWindowCocoaImpl::ScreenToWindow( int *x, int *y )
971 {
972 wxPoint p((x ? *x : 0), (y ? *y : 0) );
973 NSPoint nspt = wxToNSPoint( NULL, p );
974 nspt = [m_macWindow convertScreenToBase:nspt];
975 nspt = [[m_macWindow contentView] convertPoint:nspt fromView:nil];
976 p = wxFromNSPoint([m_macWindow contentView], nspt);
977 if ( x )
978 *x = p.x;
979 if ( y )
980 *y = p.y;
981 }
982
983 void wxNonOwnedWindowCocoaImpl::WindowToScreen( int *x, int *y )
984 {
985 wxPoint p((x ? *x : 0), (y ? *y : 0) );
986 NSPoint nspt = wxToNSPoint( [m_macWindow contentView], p );
987 nspt = [[m_macWindow contentView] convertPoint:nspt toView:nil];
988 nspt = [m_macWindow convertBaseToScreen:nspt];
989 p = wxFromNSPoint( NULL, nspt);
990 if ( x )
991 *x = p.x;
992 if ( y )
993 *y = p.y;
994 }
995
996 bool wxNonOwnedWindowCocoaImpl::IsActive()
997 {
998 return [m_macWindow isKeyWindow];
999 }
1000
1001 void wxNonOwnedWindowCocoaImpl::SetModified(bool modified)
1002 {
1003 [m_macWindow setDocumentEdited:modified];
1004 }
1005
1006 bool wxNonOwnedWindowCocoaImpl::IsModified() const
1007 {
1008 return [m_macWindow isDocumentEdited];
1009 }
1010
1011 void wxNonOwnedWindowCocoaImpl::SetRepresentedFilename(const wxString& filename)
1012 {
1013 [m_macWindow setRepresentedFilename:wxCFStringRef(filename).AsNSString()];
1014 }
1015
1016 void wxNonOwnedWindowCocoaImpl::RestoreWindowLevel()
1017 {
1018 if ( [m_macWindow level] != m_macWindowLevel )
1019 [m_macWindow setLevel:m_macWindowLevel];
1020 }
1021
1022 //
1023 //
1024 //
1025
1026 wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, WXWindow nativeWindow)
1027 {
1028 wxNonOwnedWindowCocoaImpl* now = new wxNonOwnedWindowCocoaImpl( wxpeer );
1029 now->Create( parent, nativeWindow );
1030 return now;
1031 }
1032
1033 wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, const wxPoint& pos, const wxSize& size,
1034 long style, long extraStyle, const wxString& name )
1035 {
1036 wxNonOwnedWindowImpl* now = new wxNonOwnedWindowCocoaImpl( wxpeer );
1037 now->Create( parent, pos, size, style , extraStyle, name );
1038 return now;
1039 }
1040