]> git.saurik.com Git - wxWidgets.git/blame - src/osx/cocoa/nonownedwnd.mm
build fix
[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"
33e90275
SC
16#endif
17
33e90275 18#include "wx/osx/private.h"
33e90275 19
33e90275
SC
20NSRect wxToNSRect( NSView* parent, const wxRect& r )
21{
22 NSRect frame = parent ? [parent bounds] : [[NSScreen mainScreen] frame];
23 int y = r.y;
24 int x = r.x ;
94929e7b 25 if ( parent == NULL || ![ parent isFlipped ] )
33e90275
SC
26 y = frame.size.height - ( r.y + r.height );
27 return NSMakeRect(x, y, r.width , r.height);
28}
29
30wxRect wxFromNSRect( NSView* parent, const NSRect& rect )
31{
32 NSRect frame = parent ? [parent bounds] : [[NSScreen mainScreen] frame];
33 int y = rect.origin.y;
34 int x = rect.origin.x;
94929e7b 35 if ( parent == NULL || ![ parent isFlipped ] )
33e90275
SC
36 y = frame.size.height - (rect.origin.y + rect.size.height);
37 return wxRect( x, y, rect.size.width, rect.size.height );
38}
39
40NSPoint wxToNSPoint( NSView* parent, const wxPoint& p )
41{
42 NSRect frame = parent ? [parent bounds] : [[NSScreen mainScreen] frame];
43 int x = p.x ;
44 int y = p.y;
94929e7b 45 if ( parent == NULL || ![ parent isFlipped ] )
33e90275
SC
46 y = frame.size.height - ( p.y );
47 return NSMakePoint(x, y);
48}
49
50wxPoint wxFromNSPoint( NSView* parent, const NSPoint& p )
51{
52 NSRect frame = parent ? [parent bounds] : [[NSScreen mainScreen] frame];
53 int x = p.x;
54 int y = p.y;
94929e7b 55 if ( parent == NULL || ![ parent isFlipped ] )
33e90275
SC
56 y = frame.size.height - ( p.y );
57 return wxPoint( x, y);
58}
59
dbeddfb9
SC
60//
61// wx native implementation classes
62//
63
9130dfd7
KO
64typedef void (*wxOSX_NoResponderHandlerPtr)(NSView* self, SEL _cmd, SEL selector);
65
dbeddfb9
SC
66@interface wxNSWindow : NSWindow
67
68{
69 wxNonOwnedWindowCocoaImpl* impl;
70}
71
72- (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation;
73- (wxNonOwnedWindowCocoaImpl*) implementation;
9130dfd7 74- (void)noResponderFor: (SEL) selector;
dbeddfb9
SC
75@end
76
77@implementation wxNSWindow
78
79- (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation
80{
81 impl = theImplementation;
82}
83
84- (wxNonOwnedWindowCocoaImpl*) implementation
85{
86 return impl;
87}
88
9130dfd7
KO
89// NB: if we don't do this, all key downs that get handled lead to a NSBeep
90- (void)noResponderFor: (SEL) selector
91{
92 if (selector != @selector(keyDown:))
93 {
94 wxOSX_NoResponderHandlerPtr superimpl = (wxOSX_NoResponderHandlerPtr) [[self superclass] instanceMethodForSelector:@selector(noResponderFor:)];
95 superimpl(self, @selector(noResponderFor:), selector);
96 }
97}
dbeddfb9
SC
98
99@end
100
bb839504 101@interface wxNSPanel : NSPanel
dbeddfb9
SC
102
103{
bb839504 104 wxNonOwnedWindowCocoaImpl* impl;
dbeddfb9
SC
105}
106
bb839504
KO
107- (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation;
108- (wxNonOwnedWindowCocoaImpl*) implementation;
9130dfd7 109- (void)noResponderFor: (SEL) selector;
dbeddfb9
SC
110@end
111
112@implementation wxNSPanel
113
bb839504
KO
114- (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation
115{
116 impl = theImplementation;
117}
118
119- (wxNonOwnedWindowCocoaImpl*) implementation
120{
121 return impl;
122}
123
9130dfd7
KO
124// NB: if we don't do this, all key downs that get handled lead to a NSBeep
125- (void)noResponderFor: (SEL) selector
126{
127 if (selector != @selector(keyDown:))
128 {
129 wxOSX_NoResponderHandlerPtr superimpl = (wxOSX_NoResponderHandlerPtr) [[self superclass] instanceMethodForSelector:@selector(noResponderFor:)];
130 superimpl(self, @selector(noResponderFor:), selector);
131 }
132}
133
dbeddfb9
SC
134@end
135
136
137//
138// controller
139//
140
141@interface wxNonOwnedWindowController : NSObject
142{
143}
144
145- (void)windowDidResize:(NSNotification *)notification;
146- (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize;
147- (void)windowDidResignMain:(NSNotification *)notification;
148- (void)windowDidBecomeMain:(NSNotification *)notification;
149- (void)windowDidMove:(NSNotification *)notification;
150- (BOOL)windowShouldClose:(id)window;
151
152@end
153
154@implementation wxNonOwnedWindowController
155
156- (id) init
157{
158 [super init];
159 return self;
160}
161
162- (BOOL)windowShouldClose:(id)nwindow
163{
164 wxNSWindow* window = (wxNSWindow*) nwindow;
165 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
166 if ( windowimpl )
167 {
168 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
169 if ( wxpeer )
170 wxpeer->Close();
171 }
172 return NO;
173}
174
5da125c7 175- (NSSize)windowWillResize:(NSWindow *)win
dbeddfb9
SC
176 toSize:(NSSize)proposedFrameSize
177{
5da125c7
SC
178 NSRect frame = [win frame];
179 wxRect wxframe = wxFromNSRect( NULL, frame );
180 wxframe.SetWidth( proposedFrameSize.width );
181 wxframe.SetHeight( proposedFrameSize.height );
182 wxNSWindow* window = (wxNSWindow*) win;
183 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
184 if ( windowimpl )
185 {
186 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
187 if ( wxpeer )
188 {
189 wxpeer->HandleResizing( 0, &wxframe );
190 NSSize newSize = NSMakeSize(wxframe.GetWidth(), wxframe.GetHeight());
191 return newSize;
192 }
193 }
194
dbeddfb9
SC
195 return proposedFrameSize;
196}
197
198- (void)windowDidResize:(NSNotification *)notification
199{
200 wxNSWindow* window = (wxNSWindow*) [notification object];
201 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
202 if ( windowimpl )
203 {
204 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
205 if ( wxpeer )
206 wxpeer->HandleResized(0);
207 }
208}
209
210- (void)windowDidMove:(NSNotification *)notification
211{
212 wxNSWindow* window = (wxNSWindow*) [notification object];
213 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
214 if ( windowimpl )
215 {
216 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
217 if ( wxpeer )
218 wxpeer->HandleMoved(0);
219 }
220}
221
222- (void)windowDidBecomeMain:(NSNotification *)notification
223{
224 wxNSWindow* window = (wxNSWindow*) [notification object];
225 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
226 if ( windowimpl )
227 {
228 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
229 if ( wxpeer )
230 wxpeer->HandleActivated(0, true);
231 }
232}
233
234- (void)windowDidResignMain:(NSNotification *)notification
235{
236 wxNSWindow* window = (wxNSWindow*) [notification object];
237 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
238 if ( windowimpl )
239 {
240 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
241 if ( wxpeer )
242 wxpeer->HandleActivated(0, false);
243 }
244}
245
246@end
247
33e90275
SC
248IMPLEMENT_DYNAMIC_CLASS( wxNonOwnedWindowCocoaImpl , wxNonOwnedWindowImpl )
249
250wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl( wxNonOwnedWindow* nonownedwnd) :
251 wxNonOwnedWindowImpl(nonownedwnd)
252{
253 m_macWindow = NULL;
254 m_macFullScreenData = NULL;
255}
256
257wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl()
258{
259 m_macWindow = NULL;
260 m_macFullScreenData = NULL;
261}
262
263wxNonOwnedWindowCocoaImpl::~wxNonOwnedWindowCocoaImpl()
264{
be136f07 265 [m_macWindow setImplementation:nil];
42c2b729 266 [m_macWindow setDelegate:nil];
33e90275
SC
267 [m_macWindow release];
268}
269
270void wxNonOwnedWindowCocoaImpl::Destroy()
271{
272 wxPendingDelete.Append( new wxDeferredObjectDeleter( this ) );
273}
274
275void wxNonOwnedWindowCocoaImpl::Create( wxWindow* parent, const wxPoint& pos, const wxSize& size,
276long style, long extraStyle, const wxString& name )
277{
dbeddfb9
SC
278 static wxNonOwnedWindowController* controller = NULL;
279
280 if ( !controller )
281 controller =[[wxNonOwnedWindowController alloc] init];
282
283
33e90275
SC
284 int windowstyle = NSBorderlessWindowMask;
285
286 if ( style & wxFRAME_TOOL_WINDOW )
dbeddfb9 287 m_macWindow = [wxNSPanel alloc];
33e90275 288 else
dbeddfb9 289 m_macWindow = [wxNSWindow alloc];
33e90275 290
e912ebe3 291 CGWindowLevel level = kCGNormalWindowLevel;
33e90275
SC
292
293 if ( style & wxFRAME_TOOL_WINDOW )
294 {
aa960026
KO
295 windowstyle |= NSUtilityWindowMask;
296 if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) ||
297 ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) )
298 {
299 windowstyle |= NSTitledWindowMask ;
300 }
33e90275
SC
301 }
302 else if ( ( style & wxPOPUP_WINDOW ) )
303 {
304 level = kCGPopUpMenuWindowLevel;
305 /*
306 if ( ( style & wxBORDER_NONE ) )
307 {
308 wclass = kHelpWindowClass ; // has no border
309 attr |= kWindowNoShadowAttribute;
310 }
311 else
312 {
313 wclass = kPlainWindowClass ; // has a single line border, it will have to do for now
314 }
315 */
316 }
317 else if ( ( style & wxCAPTION ) )
318 {
319 windowstyle |= NSTitledWindowMask ;
320 }
321 else if ( ( style & wxFRAME_DRAWER ) )
322 {
323 /*
324 wclass = kDrawerWindowClass;
325 */
326 }
327 else
328 {
329 // set these even if we have no title, otherwise the controls won't be visible
330 if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) ||
331 ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) )
332 {
333 windowstyle |= NSTitledWindowMask ;
334 }
335 /*
336 else if ( ( style & wxNO_BORDER ) )
337 {
338 wclass = kSimpleWindowClass ;
339 }
340 else
341 {
342 wclass = kPlainWindowClass ;
343 }
344 */
345 }
346
347 if ( windowstyle & NSTitledWindowMask )
348 {
349 if ( ( style & wxMINIMIZE_BOX ) )
350 windowstyle |= NSMiniaturizableWindowMask ;
351
352 if ( ( style & wxMAXIMIZE_BOX ) )
353 windowstyle |= NSResizableWindowMask ; // TODO showing ZOOM ?
354
355 if ( ( style & wxRESIZE_BORDER ) )
356 windowstyle |= NSResizableWindowMask ;
357
358 if ( ( style & wxCLOSE_BOX) )
359 windowstyle |= NSClosableWindowMask ;
360 }
361 if ( extraStyle & wxFRAME_EX_METAL)
362 windowstyle |= NSTexturedBackgroundWindowMask;
363
bb839504
KO
364 if ( ( style & wxFRAME_FLOAT_ON_PARENT ) || ( style & wxFRAME_TOOL_WINDOW ) )
365 level = kCGFloatingWindowLevel;
366
33e90275
SC
367 if ( ( style & wxSTAY_ON_TOP ) )
368 level = kCGUtilityWindowLevel;
33e90275
SC
369
370 NSRect r = wxToNSRect( NULL, wxRect( pos, size) );
371
dbeddfb9
SC
372 [m_macWindow setImplementation:this];
373
33e90275
SC
374 [m_macWindow initWithContentRect:r
375 styleMask:windowstyle
376 backing:NSBackingStoreBuffered
377 defer:NO
378 ];
379
380 [m_macWindow setLevel:level];
dbeddfb9
SC
381
382 [m_macWindow setDelegate:controller];
383
33e90275
SC
384 // [m_macWindow makeKeyAndOrderFront:nil];
385}
386
387
388WXWindow wxNonOwnedWindowCocoaImpl::GetWXWindow() const
389{
390 return m_macWindow;
391}
392
393void wxNonOwnedWindowCocoaImpl::Raise()
394{
395 [m_macWindow orderWindow:NSWindowAbove relativeTo:0];
396}
397
398void wxNonOwnedWindowCocoaImpl::Lower()
399{
400 [m_macWindow orderWindow:NSWindowBelow relativeTo:0];
401}
402
403bool wxNonOwnedWindowCocoaImpl::Show(bool show)
404{
405 if ( show )
406 {
407 [m_macWindow orderFront:nil];
408 [[m_macWindow contentView] setNeedsDisplay:YES];
409 }
410 else
411 [m_macWindow orderOut:nil];
412 return true;
413}
414
415bool wxNonOwnedWindowCocoaImpl::ShowWithEffect(bool show, wxShowEffect effect, unsigned timeout)
416{
417 return Show(show);
418}
419
420void wxNonOwnedWindowCocoaImpl::Update()
421{
422 [m_macWindow displayIfNeeded];
423}
424
425bool wxNonOwnedWindowCocoaImpl::SetTransparent(wxByte alpha)
426{
427 [m_macWindow setAlphaValue:(CGFloat) alpha/255.0];
428 return true;
429}
430
431bool wxNonOwnedWindowCocoaImpl::SetBackgroundColour(const wxColour& col )
432{
433 return true;
434}
435
436void wxNonOwnedWindowCocoaImpl::SetExtraStyle( long exStyle )
437{
438 if ( m_macWindow )
439 {
440 bool metal = exStyle & wxFRAME_EX_METAL ;
441 int windowStyle = [ m_macWindow styleMask];
442 if ( metal && !(windowStyle & NSTexturedBackgroundWindowMask) )
443 {
444 wxFAIL_MSG( _T("Metal Style cannot be changed after creation") );
445 }
446 else if ( !metal && (windowStyle & NSTexturedBackgroundWindowMask ) )
447 {
448 wxFAIL_MSG( _T("Metal Style cannot be changed after creation") );
449 }
450 }
451}
452
453bool wxNonOwnedWindowCocoaImpl::SetBackgroundStyle(wxBackgroundStyle style)
454{
455 return true;
456}
457
458bool wxNonOwnedWindowCocoaImpl::CanSetTransparent()
459{
460 return true;
461}
462
463void wxNonOwnedWindowCocoaImpl::MoveWindow(int x, int y, int width, int height)
464{
465 NSRect r = wxToNSRect( NULL, wxRect(x,y,width, height) );
54f11060
SC
466 // do not trigger refreshes upon invisible and possible partly created objects
467 [m_macWindow setFrame:r display:GetWXPeer()->IsShownOnScreen()];
33e90275
SC
468}
469
470void wxNonOwnedWindowCocoaImpl::GetPosition( int &x, int &y ) const
471{
472 wxRect r = wxFromNSRect( NULL, [m_macWindow frame] );
473 x = r.GetLeft();
474 y = r.GetTop();
475}
476
477void wxNonOwnedWindowCocoaImpl::GetSize( int &width, int &height ) const
478{
479 NSRect rect = [m_macWindow frame];
480 width = rect.size.width;
481 height = rect.size.height;
482}
483
484void wxNonOwnedWindowCocoaImpl::GetContentArea( int& left, int &right, int &width, int &height ) const
485{
486 NSRect outer = NSMakeRect(100,100,100,100);
487 NSRect content = [NSWindow contentRectForFrameRect:outer styleMask:[m_macWindow styleMask] ];
488 NSRect rect = [[m_macWindow contentView] frame];
489 width = rect.size.width;
490 height = rect.size.height;
491}
492
493bool wxNonOwnedWindowCocoaImpl::SetShape(const wxRegion& region)
494{
495 return false;
496}
497
498void wxNonOwnedWindowCocoaImpl::SetTitle( const wxString& title, wxFontEncoding encoding )
499{
500 [m_macWindow setTitle:wxCFStringRef( title , encoding ).AsNSString()];
501}
502
503bool wxNonOwnedWindowCocoaImpl::IsMaximized() const
504{
505 return [m_macWindow isZoomed];
506}
507
508bool wxNonOwnedWindowCocoaImpl::IsIconized() const
509{
510 return [m_macWindow isMiniaturized];
511}
512
513void wxNonOwnedWindowCocoaImpl::Iconize( bool iconize )
514{
515 if ( iconize )
516 [m_macWindow miniaturize:nil];
517 else
518 [m_macWindow deminiaturize:nil];
519}
520
521void wxNonOwnedWindowCocoaImpl::Maximize(bool maximize)
522{
523 [m_macWindow zoom:nil];
524}
525
526
527// http://cocoadevcentral.com/articles/000028.php
528
529typedef struct
530{
531 int m_formerLevel;
532 NSRect m_formerFrame;
533} FullScreenData ;
534
535bool wxNonOwnedWindowCocoaImpl::IsFullScreen() const
536{
537 return m_macFullScreenData != NULL ;
538}
539
540bool wxNonOwnedWindowCocoaImpl::ShowFullScreen(bool show, long style)
541{
542 if ( show )
543 {
544 FullScreenData *data = (FullScreenData *)m_macFullScreenData ;
545 delete data ;
546 data = new FullScreenData();
547
548 m_macFullScreenData = data ;
549 data->m_formerLevel = [m_macWindow level];
550 data->m_formerFrame = [m_macWindow frame];
551 CGDisplayCapture( kCGDirectMainDisplay );
552 [m_macWindow setLevel:CGShieldingWindowLevel()];
553 [m_macWindow setFrame:[[NSScreen mainScreen] frame] display:YES];
554 }
555 else if ( m_macFullScreenData != NULL )
556 {
557 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
558 CGDisplayRelease( kCGDirectMainDisplay );
559 [m_macWindow setLevel:data->m_formerLevel];
560 [m_macWindow setFrame:data->m_formerFrame display:YES];
561 delete data ;
562 m_macFullScreenData = NULL ;
563 }
564
565 return true;
566}
567
568void wxNonOwnedWindowCocoaImpl::RequestUserAttention(int WXUNUSED(flags))
569{
570}
dbeddfb9
SC
571
572void wxNonOwnedWindowCocoaImpl::ScreenToWindow( int *x, int *y )
573{
574 wxPoint p((x ? *x : 0), (y ? *y : 0) );
dbeddfb9 575 NSPoint nspt = wxToNSPoint( NULL, p );
54f11060
SC
576 nspt = [m_macWindow convertScreenToBase:nspt];
577 nspt = [[m_macWindow contentView] convertPoint:nspt fromView:nil];
578 p = wxFromNSPoint([m_macWindow contentView], nspt);
dbeddfb9 579 if ( x )
54f11060 580 *x = p.x;
dbeddfb9
SC
581 if ( y )
582 *y = p.y;
583}
584
585void wxNonOwnedWindowCocoaImpl::WindowToScreen( int *x, int *y )
586{
54f11060
SC
587 wxPoint p((x ? *x : 0), (y ? *y : 0) );
588 NSPoint nspt = wxToNSPoint( [m_macWindow contentView], p );
589 nspt = [[m_macWindow contentView] convertPoint:nspt toView:nil];
590 nspt = [m_macWindow convertBaseToScreen:nspt];
591 p = wxFromNSPoint( NULL, nspt);
dbeddfb9
SC
592 if ( x )
593 *x = p.x;
594 if ( y )
595 *y = p.y;
596}
597
598wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, const wxPoint& pos, const wxSize& size,
599 long style, long extraStyle, const wxString& name )
600{
601 wxNonOwnedWindowImpl* now = new wxNonOwnedWindowCocoaImpl( wxpeer );
602 now->Create( parent, pos, size, style , extraStyle, name );
603 return now;
54f11060 604}