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