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