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