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