]> git.saurik.com Git - wxWidgets.git/blob - src/osx/cocoa/nonownedwnd.mm
Fill out the left and top fields as well.
[wxWidgets.git] / src / osx / cocoa / nonownedwnd.mm
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/osx/cocoa/nonownedwnd.mm
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"
13 #ifndef WX_PRECOMP
14 #include "wx/nonownedwnd.h"
15 #include "wx/frame.h"
16 #endif
17
18 #include "wx/osx/private.h"
19
20 NSRect 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 ;
25 if ( parent == NULL || ![ parent isFlipped ] )
26 y = frame.size.height - ( r.y + r.height );
27 return NSMakeRect(x, y, r.width , r.height);
28 }
29
30 wxRect 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;
35 if ( parent == NULL || ![ parent isFlipped ] )
36 y = frame.size.height - (rect.origin.y + rect.size.height);
37 return wxRect( x, y, rect.size.width, rect.size.height );
38 }
39
40 NSPoint 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;
45 if ( parent == NULL || ![ parent isFlipped ] )
46 y = frame.size.height - ( p.y );
47 return NSMakePoint(x, y);
48 }
49
50 wxPoint 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;
55 if ( parent == NULL || ![ parent isFlipped ] )
56 y = frame.size.height - ( p.y );
57 return wxPoint( x, y);
58 }
59
60 //
61 // wx native implementation classes
62 //
63
64 typedef void (*wxOSX_NoResponderHandlerPtr)(NSView* self, SEL _cmd, SEL selector);
65
66 @interface wxNSWindow : NSWindow
67
68 {
69 wxNonOwnedWindowCocoaImpl* impl;
70 }
71
72 - (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation;
73 - (wxNonOwnedWindowCocoaImpl*) implementation;
74 - (void)noResponderFor: (SEL) selector;
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
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 }
98
99 @end
100
101 @interface wxNSPanel : NSPanel
102
103 {
104 wxNonOwnedWindowCocoaImpl* impl;
105 }
106
107 - (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation;
108 - (wxNonOwnedWindowCocoaImpl*) implementation;
109 - (void)noResponderFor: (SEL) selector;
110 @end
111
112 @implementation wxNSPanel
113
114 - (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation
115 {
116 impl = theImplementation;
117 }
118
119 - (wxNonOwnedWindowCocoaImpl*) implementation
120 {
121 return impl;
122 }
123
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
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
175 - (NSSize)windowWillResize:(NSWindow *)win
176 toSize:(NSSize)proposedFrameSize
177 {
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
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
248 IMPLEMENT_DYNAMIC_CLASS( wxNonOwnedWindowCocoaImpl , wxNonOwnedWindowImpl )
249
250 wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl( wxNonOwnedWindow* nonownedwnd) :
251 wxNonOwnedWindowImpl(nonownedwnd)
252 {
253 m_macWindow = NULL;
254 m_macFullScreenData = NULL;
255 }
256
257 wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl()
258 {
259 m_macWindow = NULL;
260 m_macFullScreenData = NULL;
261 }
262
263 wxNonOwnedWindowCocoaImpl::~wxNonOwnedWindowCocoaImpl()
264 {
265 [m_macWindow setImplementation:nil];
266 [m_macWindow setDelegate:nil];
267 [m_macWindow release];
268 }
269
270 void wxNonOwnedWindowCocoaImpl::Destroy()
271 {
272 wxPendingDelete.Append( new wxDeferredObjectDeleter( this ) );
273 }
274
275 void wxNonOwnedWindowCocoaImpl::Create( wxWindow* parent, const wxPoint& pos, const wxSize& size,
276 long style, long extraStyle, const wxString& name )
277 {
278 static wxNonOwnedWindowController* controller = NULL;
279
280 if ( !controller )
281 controller =[[wxNonOwnedWindowController alloc] init];
282
283
284 int windowstyle = NSBorderlessWindowMask;
285
286 if ( style & wxFRAME_TOOL_WINDOW )
287 m_macWindow = [wxNSPanel alloc];
288 else
289 m_macWindow = [wxNSWindow alloc];
290
291 CGWindowLevel level = kCGNormalWindowLevel;
292
293 if ( style & wxFRAME_TOOL_WINDOW )
294 {
295 windowstyle |= NSUtilityWindowMask;
296 if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) ||
297 ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) )
298 {
299 windowstyle |= NSTitledWindowMask ;
300 }
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
364 if ( ( style & wxFRAME_FLOAT_ON_PARENT ) || ( style & wxFRAME_TOOL_WINDOW ) )
365 level = kCGFloatingWindowLevel;
366
367 if ( ( style & wxSTAY_ON_TOP ) )
368 level = kCGUtilityWindowLevel;
369
370 NSRect r = wxToNSRect( NULL, wxRect( pos, size) );
371
372 [m_macWindow setImplementation:this];
373
374 [m_macWindow initWithContentRect:r
375 styleMask:windowstyle
376 backing:NSBackingStoreBuffered
377 defer:NO
378 ];
379
380 [m_macWindow setLevel:level];
381
382 [m_macWindow setDelegate:controller];
383
384 [m_macWindow setAcceptsMouseMovedEvents: YES];
385
386 // [m_macWindow makeKeyAndOrderFront:nil];
387 }
388
389
390 WXWindow wxNonOwnedWindowCocoaImpl::GetWXWindow() const
391 {
392 return m_macWindow;
393 }
394
395 void wxNonOwnedWindowCocoaImpl::Raise()
396 {
397 [m_macWindow orderWindow:NSWindowAbove relativeTo:0];
398 }
399
400 void wxNonOwnedWindowCocoaImpl::Lower()
401 {
402 [m_macWindow orderWindow:NSWindowBelow relativeTo:0];
403 }
404
405 bool wxNonOwnedWindowCocoaImpl::Show(bool show)
406 {
407 if ( show )
408 {
409 [m_macWindow orderFront:nil];
410 [[m_macWindow contentView] setNeedsDisplay:YES];
411 }
412 else
413 [m_macWindow orderOut:nil];
414 return true;
415 }
416
417 bool wxNonOwnedWindowCocoaImpl::ShowWithEffect(bool show, wxShowEffect effect, unsigned timeout)
418 {
419 return Show(show);
420 }
421
422 void wxNonOwnedWindowCocoaImpl::Update()
423 {
424 [m_macWindow displayIfNeeded];
425 }
426
427 bool wxNonOwnedWindowCocoaImpl::SetTransparent(wxByte alpha)
428 {
429 [m_macWindow setAlphaValue:(CGFloat) alpha/255.0];
430 return true;
431 }
432
433 bool wxNonOwnedWindowCocoaImpl::SetBackgroundColour(const wxColour& col )
434 {
435 return true;
436 }
437
438 void wxNonOwnedWindowCocoaImpl::SetExtraStyle( long exStyle )
439 {
440 if ( m_macWindow )
441 {
442 bool metal = exStyle & wxFRAME_EX_METAL ;
443 int windowStyle = [ m_macWindow styleMask];
444 if ( metal && !(windowStyle & NSTexturedBackgroundWindowMask) )
445 {
446 wxFAIL_MSG( _T("Metal Style cannot be changed after creation") );
447 }
448 else if ( !metal && (windowStyle & NSTexturedBackgroundWindowMask ) )
449 {
450 wxFAIL_MSG( _T("Metal Style cannot be changed after creation") );
451 }
452 }
453 }
454
455 bool wxNonOwnedWindowCocoaImpl::SetBackgroundStyle(wxBackgroundStyle style)
456 {
457 return true;
458 }
459
460 bool wxNonOwnedWindowCocoaImpl::CanSetTransparent()
461 {
462 return true;
463 }
464
465 void wxNonOwnedWindowCocoaImpl::MoveWindow(int x, int y, int width, int height)
466 {
467 NSRect r = wxToNSRect( NULL, wxRect(x,y,width, height) );
468 // do not trigger refreshes upon invisible and possible partly created objects
469 [m_macWindow setFrame:r display:GetWXPeer()->IsShownOnScreen()];
470 }
471
472 void wxNonOwnedWindowCocoaImpl::GetPosition( int &x, int &y ) const
473 {
474 wxRect r = wxFromNSRect( NULL, [m_macWindow frame] );
475 x = r.GetLeft();
476 y = r.GetTop();
477 }
478
479 void wxNonOwnedWindowCocoaImpl::GetSize( int &width, int &height ) const
480 {
481 NSRect rect = [m_macWindow frame];
482 width = rect.size.width;
483 height = rect.size.height;
484 }
485
486 void wxNonOwnedWindowCocoaImpl::GetContentArea( int& left, int &top, int &width, int &height ) const
487 {
488 NSRect outer = NSMakeRect(100,100,100,100);
489 NSRect content = [NSWindow contentRectForFrameRect:outer styleMask:[m_macWindow styleMask] ];
490 NSRect rect = [[m_macWindow contentView] frame];
491 left = rect.origin.x;
492 top = rect.origin.y;
493 width = rect.size.width;
494 height = rect.size.height;
495 }
496
497 bool wxNonOwnedWindowCocoaImpl::SetShape(const wxRegion& region)
498 {
499 return false;
500 }
501
502 void wxNonOwnedWindowCocoaImpl::SetTitle( const wxString& title, wxFontEncoding encoding )
503 {
504 [m_macWindow setTitle:wxCFStringRef( title , encoding ).AsNSString()];
505 }
506
507 bool wxNonOwnedWindowCocoaImpl::IsMaximized() const
508 {
509 return [m_macWindow isZoomed];
510 }
511
512 bool wxNonOwnedWindowCocoaImpl::IsIconized() const
513 {
514 return [m_macWindow isMiniaturized];
515 }
516
517 void wxNonOwnedWindowCocoaImpl::Iconize( bool iconize )
518 {
519 if ( iconize )
520 [m_macWindow miniaturize:nil];
521 else
522 [m_macWindow deminiaturize:nil];
523 }
524
525 void wxNonOwnedWindowCocoaImpl::Maximize(bool maximize)
526 {
527 [m_macWindow zoom:nil];
528 }
529
530
531 // http://cocoadevcentral.com/articles/000028.php
532
533 typedef struct
534 {
535 int m_formerLevel;
536 NSRect m_formerFrame;
537 } FullScreenData ;
538
539 bool wxNonOwnedWindowCocoaImpl::IsFullScreen() const
540 {
541 return m_macFullScreenData != NULL ;
542 }
543
544 bool wxNonOwnedWindowCocoaImpl::ShowFullScreen(bool show, long style)
545 {
546 if ( show )
547 {
548 FullScreenData *data = (FullScreenData *)m_macFullScreenData ;
549 delete data ;
550 data = new FullScreenData();
551
552 m_macFullScreenData = data ;
553 data->m_formerLevel = [m_macWindow level];
554 data->m_formerFrame = [m_macWindow frame];
555 CGDisplayCapture( kCGDirectMainDisplay );
556 [m_macWindow setLevel:CGShieldingWindowLevel()];
557 [m_macWindow setFrame:[[NSScreen mainScreen] frame] display:YES];
558 }
559 else if ( m_macFullScreenData != NULL )
560 {
561 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
562 CGDisplayRelease( kCGDirectMainDisplay );
563 [m_macWindow setLevel:data->m_formerLevel];
564 [m_macWindow setFrame:data->m_formerFrame display:YES];
565 delete data ;
566 m_macFullScreenData = NULL ;
567 }
568
569 return true;
570 }
571
572 void wxNonOwnedWindowCocoaImpl::RequestUserAttention(int WXUNUSED(flags))
573 {
574 }
575
576 void wxNonOwnedWindowCocoaImpl::ScreenToWindow( int *x, int *y )
577 {
578 wxPoint p((x ? *x : 0), (y ? *y : 0) );
579 NSPoint nspt = wxToNSPoint( NULL, p );
580 nspt = [m_macWindow convertScreenToBase:nspt];
581 nspt = [[m_macWindow contentView] convertPoint:nspt fromView:nil];
582 p = wxFromNSPoint([m_macWindow contentView], nspt);
583 if ( x )
584 *x = p.x;
585 if ( y )
586 *y = p.y;
587 }
588
589 void wxNonOwnedWindowCocoaImpl::WindowToScreen( int *x, int *y )
590 {
591 wxPoint p((x ? *x : 0), (y ? *y : 0) );
592 NSPoint nspt = wxToNSPoint( [m_macWindow contentView], p );
593 nspt = [[m_macWindow contentView] convertPoint:nspt toView:nil];
594 nspt = [m_macWindow convertBaseToScreen:nspt];
595 p = wxFromNSPoint( NULL, nspt);
596 if ( x )
597 *x = p.x;
598 if ( y )
599 *y = p.y;
600 }
601
602 wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, const wxPoint& pos, const wxSize& size,
603 long style, long extraStyle, const wxString& name )
604 {
605 wxNonOwnedWindowImpl* now = new wxNonOwnedWindowCocoaImpl( wxpeer );
606 now->Create( parent, pos, size, style , extraStyle, name );
607 return now;
608 }