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