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