]> git.saurik.com Git - wxWidgets.git/blame - src/osx/cocoa/nonownedwnd.mm
Fix harmless unused parameter warnings in wxDEBUG_LEVEL==0 build.
[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"
33e90275
SC
17#endif
18
33e90275 19#include "wx/osx/private.h"
33e90275 20
33e90275
SC
21NSRect 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 ;
94929e7b 26 if ( parent == NULL || ![ parent isFlipped ] )
e490b0d2 27 y = (int)(frame.size.height - ( r.y + r.height ));
33e90275
SC
28 return NSMakeRect(x, y, r.width , r.height);
29}
30
31wxRect wxFromNSRect( NSView* parent, const NSRect& rect )
32{
33 NSRect frame = parent ? [parent bounds] : [[NSScreen mainScreen] frame];
e490b0d2
VZ
34 int y = (int)rect.origin.y;
35 int x = (int)rect.origin.x;
94929e7b 36 if ( parent == NULL || ![ parent isFlipped ] )
e490b0d2
VZ
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 );
33e90275
SC
39}
40
41NSPoint 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;
94929e7b 46 if ( parent == NULL || ![ parent isFlipped ] )
e490b0d2 47 y = (int)(frame.size.height - ( p.y ));
33e90275
SC
48 return NSMakePoint(x, y);
49}
50
51wxPoint wxFromNSPoint( NSView* parent, const NSPoint& p )
52{
53 NSRect frame = parent ? [parent bounds] : [[NSScreen mainScreen] frame];
e490b0d2
VZ
54 int x = (int)p.x;
55 int y = (int)p.y;
94929e7b 56 if ( parent == NULL || ![ parent isFlipped ] )
e490b0d2 57 y = (int)(frame.size.height - ( p.y ));
33e90275
SC
58 return wxPoint( x, y);
59}
60
46f573dc
KO
61bool shouldHandleSelector(SEL selector)
62{
03647350 63 if (selector == @selector(noop:)
46f573dc
KO
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;
03647350 76
46f573dc
KO
77 return true;
78
79}
80
dbeddfb9 81//
e17ac396 82// wx category for NSWindow (our own and wrapped instances)
dbeddfb9
SC
83//
84
e17ac396
SC
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//
9130dfd7 123
dbeddfb9 124@interface wxNSWindow : NSWindow
dbeddfb9 125{
dbeddfb9
SC
126}
127
e17ac396 128- (void) sendEvent:(NSEvent *)event;
0bcac6c7 129- (NSRect)constrainFrameRect:(NSRect)frameRect toScreen:(NSScreen *)screen;
9130dfd7 130- (void)noResponderFor: (SEL) selector;
dbeddfb9
SC
131@end
132
133@implementation wxNSWindow
134
e17ac396
SC
135- (void)sendEvent:(NSEvent *) event
136{
137 if ( ![self WX_filterSendEvent: event] )
0b6f851f
SC
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
e17ac396 145 [super sendEvent: event];
0b6f851f
SC
146
147 if (wxTheApp)
148 wxTheApp->MacSetCurrentEvent(formerEvent , formerHandler);
149 }
e17ac396
SC
150}
151
0bcac6c7
KO
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{
219665bb 156 wxUnusedVar(screen);
0bcac6c7
KO
157 return frameRect;
158}
159
46f573dc
KO
160- (void)doCommandBySelector:(SEL)selector
161{
03647350 162 if (shouldHandleSelector(selector) &&
46f573dc
KO
163 !(selector == @selector(cancel:) || selector == @selector(cancelOperation:)) )
164 [super doCommandBySelector:selector];
165}
166
167
9130dfd7
KO
168// NB: if we don't do this, all key downs that get handled lead to a NSBeep
169- (void)noResponderFor: (SEL) selector
170{
46f573dc 171 if (selector != @selector(keyDown:) && selector != @selector(keyUp:))
9130dfd7 172 {
20ede392 173 [super noResponderFor:selector];
9130dfd7
KO
174 }
175}
dbeddfb9 176
2c3ce6c4
KO
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
dbeddfb9
SC
185@end
186
bb839504 187@interface wxNSPanel : NSPanel
dbeddfb9
SC
188{
189}
190
9130dfd7 191- (void)noResponderFor: (SEL) selector;
e17ac396 192- (void)sendEvent:(NSEvent *)event;
dbeddfb9
SC
193@end
194
03647350 195@implementation wxNSPanel
dbeddfb9 196
09a9eb20
KO
197- (BOOL)canBecomeKeyWindow
198{
199 return YES;
200}
201
46f573dc
KO
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
9130dfd7
KO
209- (void)noResponderFor: (SEL) selector
210{
46f573dc 211 if (selector != @selector(keyDown:) && selector != @selector(keyUp:))
9130dfd7 212 {
20ede392 213 [super noResponderFor:selector];
9130dfd7
KO
214 }
215}
216
e17ac396
SC
217- (void)sendEvent:(NSEvent *) event
218{
219 if ( ![self WX_filterSendEvent: event] )
220 [super sendEvent: event];
221}
222
dbeddfb9
SC
223@end
224
225
226//
227// controller
228//
229
c8fdb345 230@interface wxNonOwnedWindowController : NSObject wxOSX_10_6_AND_LATER(<NSWindowDelegate>)
dbeddfb9
SC
231{
232}
233
234- (void)windowDidResize:(NSNotification *)notification;
235- (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize;
09a9eb20
KO
236- (void)windowDidResignKey:(NSNotification *)notification;
237- (void)windowDidBecomeKey:(NSNotification *)notification;
dbeddfb9
SC
238- (void)windowDidMove:(NSNotification *)notification;
239- (BOOL)windowShouldClose:(id)window;
1232607d 240- (BOOL)windowShouldZoom:(NSWindow *)window toFrame:(NSRect)newFrame;
dbeddfb9
SC
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{
e17ac396 254 wxNonOwnedWindowCocoaImpl* windowimpl = [(NSWindow*) nwindow WX_implementation];
dbeddfb9
SC
255 if ( windowimpl )
256 {
257 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
258 if ( wxpeer )
259 wxpeer->Close();
260 }
261 return NO;
262}
263
e17ac396 264- (NSSize)windowWillResize:(NSWindow *)window
dbeddfb9
SC
265 toSize:(NSSize)proposedFrameSize
266{
e17ac396 267 NSRect frame = [window frame];
5da125c7 268 wxRect wxframe = wxFromNSRect( NULL, frame );
e490b0d2
VZ
269 wxframe.SetWidth( (int)proposedFrameSize.width );
270 wxframe.SetHeight( (int)proposedFrameSize.height );
e17ac396
SC
271
272 wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
5da125c7
SC
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
dbeddfb9
SC
284 return proposedFrameSize;
285}
286
287- (void)windowDidResize:(NSNotification *)notification
288{
e17ac396
SC
289 NSWindow* window = (NSWindow*) [notification object];
290 wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
dbeddfb9
SC
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];
e17ac396 302 wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
dbeddfb9
SC
303 if ( windowimpl )
304 {
305 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
306 if ( wxpeer )
307 wxpeer->HandleMoved(0);
308 }
309}
310
09a9eb20 311- (void)windowDidBecomeKey:(NSNotification *)notification
dbeddfb9 312{
e17ac396
SC
313 NSWindow* window = (NSWindow*) [notification object];
314 wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
dbeddfb9
SC
315 if ( windowimpl )
316 {
317 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
318 if ( wxpeer )
319 wxpeer->HandleActivated(0, true);
320 }
321}
322
09a9eb20 323- (void)windowDidResignKey:(NSNotification *)notification
dbeddfb9 324{
e17ac396
SC
325 NSWindow* window = (NSWindow*) [notification object];
326 wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
dbeddfb9
SC
327 if ( windowimpl )
328 {
329 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
330 if ( wxpeer )
09a9eb20 331 {
dbeddfb9 332 wxpeer->HandleActivated(0, false);
03647350
VZ
333 // Needed for popup window since the firstResponder
334 // (focus in wx) doesn't change when this
09a9eb20
KO
335 // TLW becomes inactive.
336 wxFocusEvent event( wxEVT_KILL_FOCUS, wxpeer->GetId());
337 event.SetEventObject(wxpeer);
338 wxpeer->HandleWindowEvent(event);
339 }
dbeddfb9
SC
340 }
341}
342
7cb2a241
SC
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
1232607d
KO
363- (BOOL)windowShouldZoom:(NSWindow *)window toFrame:(NSRect)newFrame
364{
e17ac396
SC
365 wxUnusedVar(newFrame);
366 wxNonOwnedWindowCocoaImpl* windowimpl = [window WX_implementation];
1232607d
KO
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
dbeddfb9
SC
377@end
378
33e90275
SC
379IMPLEMENT_DYNAMIC_CLASS( wxNonOwnedWindowCocoaImpl , wxNonOwnedWindowImpl )
380
03647350 381wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl( wxNonOwnedWindow* nonownedwnd) :
33e90275
SC
382 wxNonOwnedWindowImpl(nonownedwnd)
383{
384 m_macWindow = NULL;
385 m_macFullScreenData = NULL;
386}
03647350
VZ
387
388wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl()
33e90275
SC
389{
390 m_macWindow = NULL;
391 m_macFullScreenData = NULL;
392}
03647350 393
33e90275
SC
394wxNonOwnedWindowCocoaImpl::~wxNonOwnedWindowCocoaImpl()
395{
17e2694c
SC
396 if ( !m_wxPeer->IsNativeWindowWrapper() )
397 {
17e2694c
SC
398 [m_macWindow setDelegate:nil];
399 [m_macWindow release];
400 }
33e90275
SC
401}
402
0aaa6ace 403void wxNonOwnedWindowCocoaImpl::WillBeDestroyed()
33e90275 404{
17e2694c
SC
405 if ( !m_wxPeer->IsNativeWindowWrapper() )
406 {
407 [m_macWindow setDelegate:nil];
408 }
33e90275
SC
409}
410
20ede392
SC
411void wxNonOwnedWindowCocoaImpl::Create( wxWindow* WXUNUSED(parent), const wxPoint& pos, const wxSize& size,
412long style, long extraStyle, const wxString& WXUNUSED(name) )
33e90275 413{
dbeddfb9 414 static wxNonOwnedWindowController* controller = NULL;
03647350 415
dbeddfb9
SC
416 if ( !controller )
417 controller =[[wxNonOwnedWindowController alloc] init];
418
419
33e90275 420 int windowstyle = NSBorderlessWindowMask;
03647350
VZ
421
422 if ( style & wxFRAME_TOOL_WINDOW || ( style & wxPOPUP_WINDOW ) ||
09a9eb20
KO
423 GetWXPeer()->GetExtraStyle() & wxTOPLEVEL_EX_DIALOG )
424 {
dbeddfb9 425 m_macWindow = [wxNSPanel alloc];
09a9eb20 426 }
33e90275 427 else
dbeddfb9 428 m_macWindow = [wxNSWindow alloc];
03647350 429
e912ebe3 430 CGWindowLevel level = kCGNormalWindowLevel;
03647350 431
33e90275
SC
432 if ( style & wxFRAME_TOOL_WINDOW )
433 {
aa960026
KO
434 windowstyle |= NSUtilityWindowMask;
435 if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) ||
436 ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) )
437 {
438 windowstyle |= NSTitledWindowMask ;
439 }
33e90275
SC
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
bb839504
KO
503 if ( ( style & wxFRAME_FLOAT_ON_PARENT ) || ( style & wxFRAME_TOOL_WINDOW ) )
504 level = kCGFloatingWindowLevel;
505
33e90275
SC
506 if ( ( style & wxSTAY_ON_TOP ) )
507 level = kCGUtilityWindowLevel;
03647350 508
33e90275 509 NSRect r = wxToNSRect( NULL, wxRect( pos, size) );
03647350 510
01639b08 511 r = [NSWindow contentRectForFrameRect:r styleMask:windowstyle];
03647350 512
33e90275
SC
513 [m_macWindow initWithContentRect:r
514 styleMask:windowstyle
515 backing:NSBackingStoreBuffered
03647350 516 defer:NO
33e90275 517 ];
03647350 518
33e90275 519 [m_macWindow setLevel:level];
dbeddfb9
SC
520
521 [m_macWindow setDelegate:controller];
03647350 522
826da451 523 [m_macWindow setAcceptsMouseMovedEvents: YES];
b61b8371
SC
524
525 if ( ( style & wxFRAME_SHAPED) )
526 {
527 [m_macWindow setOpaque:NO];
528 [m_macWindow setAlphaValue:1.0];
529 }
4539e903
SC
530
531 if ( !(style & wxFRAME_TOOL_WINDOW) )
532 [m_macWindow setHidesOnDeactivate:NO];
33e90275
SC
533}
534
17e2694c
SC
535void wxNonOwnedWindowCocoaImpl::Create( wxWindow* WXUNUSED(parent), WXWindow nativeWindow )
536{
537 m_macWindow = nativeWindow;
538}
33e90275
SC
539
540WXWindow wxNonOwnedWindowCocoaImpl::GetWXWindow() const
541{
542 return m_macWindow;
543}
544
545void wxNonOwnedWindowCocoaImpl::Raise()
546{
547 [m_macWindow orderWindow:NSWindowAbove relativeTo:0];
548}
03647350 549
33e90275
SC
550void wxNonOwnedWindowCocoaImpl::Lower()
551{
552 [m_macWindow orderWindow:NSWindowBelow relativeTo:0];
553}
554
dbc7ceb9
KO
555void wxNonOwnedWindowCocoaImpl::ShowWithoutActivating()
556{
a029e98a 557 [m_macWindow orderFront:nil];
eabe8426 558 [[m_macWindow contentView] setNeedsDisplay: YES];
dbc7ceb9
KO
559}
560
33e90275
SC
561bool wxNonOwnedWindowCocoaImpl::Show(bool show)
562{
563 if ( show )
564 {
1f83c631
SC
565 wxNonOwnedWindow* wxpeer = GetWXPeer();
566 if (wxpeer && !(wxpeer->GetWindowStyle() & wxFRAME_TOOL_WINDOW))
dbc7ceb9 567 [m_macWindow makeKeyAndOrderFront:nil];
1f83c631
SC
568 else
569 [m_macWindow orderFront:nil];
eabe8426 570 [[m_macWindow contentView] setNeedsDisplay: YES];
33e90275 571 }
03647350 572 else
33e90275
SC
573 [m_macWindow orderOut:nil];
574 return true;
575}
03647350 576
ab9a0b84
VZ
577bool wxNonOwnedWindowCocoaImpl::ShowWithEffect(bool show,
578 wxShowEffect effect,
579 unsigned timeout)
33e90275 580{
ab9a0b84
VZ
581 return wxWidgetCocoaImpl::
582 ShowViewOrWindowWithEffect(m_wxPeer, show, effect, timeout);
33e90275
SC
583}
584
585void wxNonOwnedWindowCocoaImpl::Update()
586{
587 [m_macWindow displayIfNeeded];
588}
589
590bool wxNonOwnedWindowCocoaImpl::SetTransparent(wxByte alpha)
591{
592 [m_macWindow setAlphaValue:(CGFloat) alpha/255.0];
593 return true;
594}
595
20ede392 596bool wxNonOwnedWindowCocoaImpl::SetBackgroundColour(const wxColour& WXUNUSED(col) )
33e90275
SC
597{
598 return true;
599}
600
601void 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 {
9a83f860 609 wxFAIL_MSG( wxT("Metal Style cannot be changed after creation") );
33e90275
SC
610 }
611 else if ( !metal && (windowStyle & NSTexturedBackgroundWindowMask ) )
612 {
9a83f860 613 wxFAIL_MSG( wxT("Metal Style cannot be changed after creation") );
03647350 614 }
33e90275
SC
615 }
616}
03647350 617
b6dc21e7
KO
618void 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
eabe8426 633bool wxNonOwnedWindowCocoaImpl::SetBackgroundStyle(wxBackgroundStyle style)
33e90275 634{
eabe8426
KO
635 if ( style == wxBG_STYLE_TRANSPARENT )
636 {
637 [m_macWindow setOpaque:NO];
638 [m_macWindow setBackgroundColor:[NSColor clearColor]];
639 }
640
33e90275
SC
641 return true;
642}
03647350 643
33e90275
SC
644bool wxNonOwnedWindowCocoaImpl::CanSetTransparent()
645{
646 return true;
647}
648
649void wxNonOwnedWindowCocoaImpl::MoveWindow(int x, int y, int width, int height)
650{
651 NSRect r = wxToNSRect( NULL, wxRect(x,y,width, height) );
54f11060
SC
652 // do not trigger refreshes upon invisible and possible partly created objects
653 [m_macWindow setFrame:r display:GetWXPeer()->IsShownOnScreen()];
33e90275
SC
654}
655
656void 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
663void wxNonOwnedWindowCocoaImpl::GetSize( int &width, int &height ) const
664{
665 NSRect rect = [m_macWindow frame];
e490b0d2
VZ
666 width = (int)rect.size.width;
667 height = (int)rect.size.height;
33e90275
SC
668}
669
b0ec7fbb 670void wxNonOwnedWindowCocoaImpl::GetContentArea( int& left, int &top, int &width, int &height ) const
33e90275
SC
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];
e490b0d2
VZ
675 left = (int)rect.origin.x;
676 top = (int)rect.origin.y;
677 width = (int)rect.size.width;
678 height = (int)rect.size.height;
33e90275 679}
03647350 680
20ede392 681bool wxNonOwnedWindowCocoaImpl::SetShape(const wxRegion& WXUNUSED(region))
33e90275 682{
eabe8426
KO
683 [m_macWindow setOpaque:NO];
684 [m_macWindow setBackgroundColor:[NSColor clearColor]];
685
686 return true;
33e90275
SC
687}
688
03647350 689void wxNonOwnedWindowCocoaImpl::SetTitle( const wxString& title, wxFontEncoding encoding )
33e90275
SC
690{
691 [m_macWindow setTitle:wxCFStringRef( title , encoding ).AsNSString()];
692}
03647350 693
33e90275
SC
694bool wxNonOwnedWindowCocoaImpl::IsMaximized() const
695{
cbe06760
VZ
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 }
33e90275 709}
03647350 710
33e90275
SC
711bool wxNonOwnedWindowCocoaImpl::IsIconized() const
712{
713 return [m_macWindow isMiniaturized];
714}
03647350 715
33e90275
SC
716void wxNonOwnedWindowCocoaImpl::Iconize( bool iconize )
717{
718 if ( iconize )
719 [m_macWindow miniaturize:nil];
720 else
721 [m_macWindow deminiaturize:nil];
722}
03647350 723
20ede392 724void wxNonOwnedWindowCocoaImpl::Maximize(bool WXUNUSED(maximize))
33e90275
SC
725{
726 [m_macWindow zoom:nil];
727}
03647350 728
33e90275
SC
729
730// http://cocoadevcentral.com/articles/000028.php
731
732typedef struct
733{
734 int m_formerLevel;
735 NSRect m_formerFrame;
736} FullScreenData ;
737
738bool wxNonOwnedWindowCocoaImpl::IsFullScreen() const
739{
740 return m_macFullScreenData != NULL ;
741}
03647350 742
20ede392 743bool wxNonOwnedWindowCocoaImpl::ShowFullScreen(bool show, long WXUNUSED(style))
33e90275
SC
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 }
03647350 767
33e90275
SC
768 return true;
769}
770
32b8e701 771void wxNonOwnedWindowCocoaImpl::RequestUserAttention(int flagsWX)
33e90275 772{
98182acc 773 NSRequestUserAttentionType flagsOSX;
32b8e701
VZ
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];
33e90275 790}
dbeddfb9
SC
791
792void wxNonOwnedWindowCocoaImpl::ScreenToWindow( int *x, int *y )
793{
794 wxPoint p((x ? *x : 0), (y ? *y : 0) );
dbeddfb9 795 NSPoint nspt = wxToNSPoint( NULL, p );
54f11060
SC
796 nspt = [m_macWindow convertScreenToBase:nspt];
797 nspt = [[m_macWindow contentView] convertPoint:nspt fromView:nil];
798 p = wxFromNSPoint([m_macWindow contentView], nspt);
dbeddfb9 799 if ( x )
03647350 800 *x = p.x;
dbeddfb9
SC
801 if ( y )
802 *y = p.y;
803}
804
805void wxNonOwnedWindowCocoaImpl::WindowToScreen( int *x, int *y )
806{
54f11060
SC
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);
dbeddfb9
SC
812 if ( x )
813 *x = p.x;
814 if ( y )
815 *y = p.y;
816}
817
dbc7ceb9
KO
818bool wxNonOwnedWindowCocoaImpl::IsActive()
819{
820 return [m_macWindow isKeyWindow];
821}
822
efb2fa41
KO
823void wxNonOwnedWindowCocoaImpl::SetModified(bool modified)
824{
825 [m_macWindow setDocumentEdited:modified];
826}
827
ebf7d5c4 828bool wxNonOwnedWindowCocoaImpl::IsModified() const
efb2fa41
KO
829{
830 return [m_macWindow isDocumentEdited];
831}
832
17e2694c
SC
833wxNonOwnedWindowImpl* 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
dbeddfb9
SC
840wxNonOwnedWindowImpl* 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;
54f11060 846}