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