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