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