fix MSVC warnings about possibly uninitialized variables; some reindentation
[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 setDelegate:nil];
231 [m_macWindow release];
232 }
233
234 void wxNonOwnedWindowCocoaImpl::Destroy()
235 {
236 wxPendingDelete.Append( new wxDeferredObjectDeleter( this ) );
237 }
238
239 void wxNonOwnedWindowCocoaImpl::Create( wxWindow* parent, const wxPoint& pos, const wxSize& size,
240 long style, long extraStyle, const wxString& name )
241 {
242 static wxNonOwnedWindowController* controller = NULL;
243
244 if ( !controller )
245 controller =[[wxNonOwnedWindowController alloc] init];
246
247
248 int windowstyle = NSBorderlessWindowMask;
249
250 if ( style & wxFRAME_TOOL_WINDOW )
251 m_macWindow = [wxNSPanel alloc];
252 else
253 m_macWindow = [wxNSWindow alloc];
254
255 CGWindowLevel level = kCGNormalWindowLevel;
256
257 if ( style & wxFRAME_TOOL_WINDOW )
258 {
259 if ( ( style & wxSTAY_ON_TOP ) )
260 level = kCGUtilityWindowLevel;
261 else
262 level = kCGFloatingWindowLevel ;
263
264 }
265 else if ( ( style & wxPOPUP_WINDOW ) )
266 {
267 level = kCGPopUpMenuWindowLevel;
268 /*
269 if ( ( style & wxBORDER_NONE ) )
270 {
271 wclass = kHelpWindowClass ; // has no border
272 attr |= kWindowNoShadowAttribute;
273 }
274 else
275 {
276 wclass = kPlainWindowClass ; // has a single line border, it will have to do for now
277 }
278 */
279 }
280 else if ( ( style & wxCAPTION ) )
281 {
282 windowstyle |= NSTitledWindowMask ;
283 }
284 else if ( ( style & wxFRAME_DRAWER ) )
285 {
286 /*
287 wclass = kDrawerWindowClass;
288 */
289 }
290 else
291 {
292 // set these even if we have no title, otherwise the controls won't be visible
293 if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) ||
294 ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) )
295 {
296 windowstyle |= NSTitledWindowMask ;
297 }
298 /*
299 else if ( ( style & wxNO_BORDER ) )
300 {
301 wclass = kSimpleWindowClass ;
302 }
303 else
304 {
305 wclass = kPlainWindowClass ;
306 }
307 */
308 }
309
310 if ( windowstyle & NSTitledWindowMask )
311 {
312 if ( ( style & wxMINIMIZE_BOX ) )
313 windowstyle |= NSMiniaturizableWindowMask ;
314
315 if ( ( style & wxMAXIMIZE_BOX ) )
316 windowstyle |= NSResizableWindowMask ; // TODO showing ZOOM ?
317
318 if ( ( style & wxRESIZE_BORDER ) )
319 windowstyle |= NSResizableWindowMask ;
320
321 if ( ( style & wxCLOSE_BOX) )
322 windowstyle |= NSClosableWindowMask ;
323 }
324 if ( extraStyle & wxFRAME_EX_METAL)
325 windowstyle |= NSTexturedBackgroundWindowMask;
326
327 if ( ( style & wxSTAY_ON_TOP ) )
328 level = kCGUtilityWindowLevel;
329 /*
330 if ( ( style & wxFRAME_FLOAT_ON_PARENT ) )
331 group = GetWindowGroupOfClass(kFloatingWindowClass);
332 */
333
334 NSRect r = wxToNSRect( NULL, wxRect( pos, size) );
335
336 [m_macWindow setImplementation:this];
337
338 [m_macWindow initWithContentRect:r
339 styleMask:windowstyle
340 backing:NSBackingStoreBuffered
341 defer:NO
342 ];
343
344 [m_macWindow setLevel:level];
345
346 [m_macWindow setDelegate:controller];
347
348 // [m_macWindow makeKeyAndOrderFront:nil];
349 }
350
351
352 WXWindow wxNonOwnedWindowCocoaImpl::GetWXWindow() const
353 {
354 return m_macWindow;
355 }
356
357 void wxNonOwnedWindowCocoaImpl::Raise()
358 {
359 [m_macWindow orderWindow:NSWindowAbove relativeTo:0];
360 }
361
362 void wxNonOwnedWindowCocoaImpl::Lower()
363 {
364 [m_macWindow orderWindow:NSWindowBelow relativeTo:0];
365 }
366
367 bool wxNonOwnedWindowCocoaImpl::Show(bool show)
368 {
369 if ( show )
370 {
371 [m_macWindow orderFront:nil];
372 [[m_macWindow contentView] setNeedsDisplay:YES];
373 }
374 else
375 [m_macWindow orderOut:nil];
376 return true;
377 }
378
379 bool wxNonOwnedWindowCocoaImpl::ShowWithEffect(bool show, wxShowEffect effect, unsigned timeout)
380 {
381 return Show(show);
382 }
383
384 void wxNonOwnedWindowCocoaImpl::Update()
385 {
386 [m_macWindow displayIfNeeded];
387 }
388
389 bool wxNonOwnedWindowCocoaImpl::SetTransparent(wxByte alpha)
390 {
391 [m_macWindow setAlphaValue:(CGFloat) alpha/255.0];
392 return true;
393 }
394
395 bool wxNonOwnedWindowCocoaImpl::SetBackgroundColour(const wxColour& col )
396 {
397 return true;
398 }
399
400 void wxNonOwnedWindowCocoaImpl::SetExtraStyle( long exStyle )
401 {
402 if ( m_macWindow )
403 {
404 bool metal = exStyle & wxFRAME_EX_METAL ;
405 int windowStyle = [ m_macWindow styleMask];
406 if ( metal && !(windowStyle & NSTexturedBackgroundWindowMask) )
407 {
408 wxFAIL_MSG( _T("Metal Style cannot be changed after creation") );
409 }
410 else if ( !metal && (windowStyle & NSTexturedBackgroundWindowMask ) )
411 {
412 wxFAIL_MSG( _T("Metal Style cannot be changed after creation") );
413 }
414 }
415 }
416
417 bool wxNonOwnedWindowCocoaImpl::SetBackgroundStyle(wxBackgroundStyle style)
418 {
419 return true;
420 }
421
422 bool wxNonOwnedWindowCocoaImpl::CanSetTransparent()
423 {
424 return true;
425 }
426
427 void wxNonOwnedWindowCocoaImpl::MoveWindow(int x, int y, int width, int height)
428 {
429 NSRect r = wxToNSRect( NULL, wxRect(x,y,width, height) );
430 // do not trigger refreshes upon invisible and possible partly created objects
431 [m_macWindow setFrame:r display:GetWXPeer()->IsShownOnScreen()];
432 }
433
434 void wxNonOwnedWindowCocoaImpl::GetPosition( int &x, int &y ) const
435 {
436 wxRect r = wxFromNSRect( NULL, [m_macWindow frame] );
437 x = r.GetLeft();
438 y = r.GetTop();
439 }
440
441 void wxNonOwnedWindowCocoaImpl::GetSize( int &width, int &height ) const
442 {
443 NSRect rect = [m_macWindow frame];
444 width = rect.size.width;
445 height = rect.size.height;
446 }
447
448 void wxNonOwnedWindowCocoaImpl::GetContentArea( int& left, int &right, int &width, int &height ) const
449 {
450 NSRect outer = NSMakeRect(100,100,100,100);
451 NSRect content = [NSWindow contentRectForFrameRect:outer styleMask:[m_macWindow styleMask] ];
452 NSRect rect = [[m_macWindow contentView] frame];
453 width = rect.size.width;
454 height = rect.size.height;
455 }
456
457 bool wxNonOwnedWindowCocoaImpl::SetShape(const wxRegion& region)
458 {
459 return false;
460 }
461
462 void wxNonOwnedWindowCocoaImpl::SetTitle( const wxString& title, wxFontEncoding encoding )
463 {
464 [m_macWindow setTitle:wxCFStringRef( title , encoding ).AsNSString()];
465 }
466
467 bool wxNonOwnedWindowCocoaImpl::IsMaximized() const
468 {
469 return [m_macWindow isZoomed];
470 }
471
472 bool wxNonOwnedWindowCocoaImpl::IsIconized() const
473 {
474 return [m_macWindow isMiniaturized];
475 }
476
477 void wxNonOwnedWindowCocoaImpl::Iconize( bool iconize )
478 {
479 if ( iconize )
480 [m_macWindow miniaturize:nil];
481 else
482 [m_macWindow deminiaturize:nil];
483 }
484
485 void wxNonOwnedWindowCocoaImpl::Maximize(bool maximize)
486 {
487 [m_macWindow zoom:nil];
488 }
489
490
491 // http://cocoadevcentral.com/articles/000028.php
492
493 typedef struct
494 {
495 int m_formerLevel;
496 NSRect m_formerFrame;
497 } FullScreenData ;
498
499 bool wxNonOwnedWindowCocoaImpl::IsFullScreen() const
500 {
501 return m_macFullScreenData != NULL ;
502 }
503
504 bool wxNonOwnedWindowCocoaImpl::ShowFullScreen(bool show, long style)
505 {
506 if ( show )
507 {
508 FullScreenData *data = (FullScreenData *)m_macFullScreenData ;
509 delete data ;
510 data = new FullScreenData();
511
512 m_macFullScreenData = data ;
513 data->m_formerLevel = [m_macWindow level];
514 data->m_formerFrame = [m_macWindow frame];
515 CGDisplayCapture( kCGDirectMainDisplay );
516 [m_macWindow setLevel:CGShieldingWindowLevel()];
517 [m_macWindow setFrame:[[NSScreen mainScreen] frame] display:YES];
518 }
519 else if ( m_macFullScreenData != NULL )
520 {
521 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
522 CGDisplayRelease( kCGDirectMainDisplay );
523 [m_macWindow setLevel:data->m_formerLevel];
524 [m_macWindow setFrame:data->m_formerFrame display:YES];
525 delete data ;
526 m_macFullScreenData = NULL ;
527 }
528
529 return true;
530 }
531
532 void wxNonOwnedWindowCocoaImpl::RequestUserAttention(int WXUNUSED(flags))
533 {
534 }
535
536 void wxNonOwnedWindowCocoaImpl::ScreenToWindow( int *x, int *y )
537 {
538 wxPoint p((x ? *x : 0), (y ? *y : 0) );
539 NSPoint nspt = wxToNSPoint( NULL, p );
540 nspt = [m_macWindow convertScreenToBase:nspt];
541 nspt = [[m_macWindow contentView] convertPoint:nspt fromView:nil];
542 p = wxFromNSPoint([m_macWindow contentView], nspt);
543 if ( x )
544 *x = p.x;
545 if ( y )
546 *y = p.y;
547 }
548
549 void wxNonOwnedWindowCocoaImpl::WindowToScreen( int *x, int *y )
550 {
551 wxPoint p((x ? *x : 0), (y ? *y : 0) );
552 NSPoint nspt = wxToNSPoint( [m_macWindow contentView], p );
553 nspt = [[m_macWindow contentView] convertPoint:nspt toView:nil];
554 nspt = [m_macWindow convertBaseToScreen:nspt];
555 p = wxFromNSPoint( NULL, nspt);
556 if ( x )
557 *x = p.x;
558 if ( y )
559 *y = p.y;
560 }
561
562 wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, const wxPoint& pos, const wxSize& size,
563 long style, long extraStyle, const wxString& name )
564 {
565 wxNonOwnedWindowImpl* now = new wxNonOwnedWindowCocoaImpl( wxpeer );
566 now->Create( parent, pos, size, style , extraStyle, name );
567 return now;
568 }