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