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