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