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