]> git.saurik.com Git - wxWidgets.git/blame - src/osx/cocoa/nonownedwnd.mm
fix MSVC warnings about possibly uninitialized variables; some reindentation
[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
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
5da125c7 140- (NSSize)windowWillResize:(NSWindow *)win
dbeddfb9
SC
141 toSize:(NSSize)proposedFrameSize
142{
5da125c7
SC
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
dbeddfb9
SC
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
33e90275
SC
213IMPLEMENT_DYNAMIC_CLASS( wxNonOwnedWindowCocoaImpl , wxNonOwnedWindowImpl )
214
215wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl( wxNonOwnedWindow* nonownedwnd) :
216 wxNonOwnedWindowImpl(nonownedwnd)
217{
218 m_macWindow = NULL;
219 m_macFullScreenData = NULL;
220}
221
222wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl()
223{
224 m_macWindow = NULL;
225 m_macFullScreenData = NULL;
226}
227
228wxNonOwnedWindowCocoaImpl::~wxNonOwnedWindowCocoaImpl()
229{
42c2b729 230 [m_macWindow setDelegate:nil];
33e90275
SC
231 [m_macWindow release];
232}
233
234void wxNonOwnedWindowCocoaImpl::Destroy()
235{
236 wxPendingDelete.Append( new wxDeferredObjectDeleter( this ) );
237}
238
239void wxNonOwnedWindowCocoaImpl::Create( wxWindow* parent, const wxPoint& pos, const wxSize& size,
240long style, long extraStyle, const wxString& name )
241{
dbeddfb9
SC
242 static wxNonOwnedWindowController* controller = NULL;
243
244 if ( !controller )
245 controller =[[wxNonOwnedWindowController alloc] init];
246
247
33e90275
SC
248 int windowstyle = NSBorderlessWindowMask;
249
250 if ( style & wxFRAME_TOOL_WINDOW )
dbeddfb9 251 m_macWindow = [wxNSPanel alloc];
33e90275 252 else
dbeddfb9 253 m_macWindow = [wxNSWindow alloc];
33e90275 254
e912ebe3 255 CGWindowLevel level = kCGNormalWindowLevel;
33e90275
SC
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
dbeddfb9
SC
336 [m_macWindow setImplementation:this];
337
33e90275
SC
338 [m_macWindow initWithContentRect:r
339 styleMask:windowstyle
340 backing:NSBackingStoreBuffered
341 defer:NO
342 ];
343
344 [m_macWindow setLevel:level];
dbeddfb9
SC
345
346 [m_macWindow setDelegate:controller];
347
33e90275
SC
348 // [m_macWindow makeKeyAndOrderFront:nil];
349}
350
351
352WXWindow wxNonOwnedWindowCocoaImpl::GetWXWindow() const
353{
354 return m_macWindow;
355}
356
357void wxNonOwnedWindowCocoaImpl::Raise()
358{
359 [m_macWindow orderWindow:NSWindowAbove relativeTo:0];
360}
361
362void wxNonOwnedWindowCocoaImpl::Lower()
363{
364 [m_macWindow orderWindow:NSWindowBelow relativeTo:0];
365}
366
367bool 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
379bool wxNonOwnedWindowCocoaImpl::ShowWithEffect(bool show, wxShowEffect effect, unsigned timeout)
380{
381 return Show(show);
382}
383
384void wxNonOwnedWindowCocoaImpl::Update()
385{
386 [m_macWindow displayIfNeeded];
387}
388
389bool wxNonOwnedWindowCocoaImpl::SetTransparent(wxByte alpha)
390{
391 [m_macWindow setAlphaValue:(CGFloat) alpha/255.0];
392 return true;
393}
394
395bool wxNonOwnedWindowCocoaImpl::SetBackgroundColour(const wxColour& col )
396{
397 return true;
398}
399
400void 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
417bool wxNonOwnedWindowCocoaImpl::SetBackgroundStyle(wxBackgroundStyle style)
418{
419 return true;
420}
421
422bool wxNonOwnedWindowCocoaImpl::CanSetTransparent()
423{
424 return true;
425}
426
427void wxNonOwnedWindowCocoaImpl::MoveWindow(int x, int y, int width, int height)
428{
429 NSRect r = wxToNSRect( NULL, wxRect(x,y,width, height) );
54f11060
SC
430 // do not trigger refreshes upon invisible and possible partly created objects
431 [m_macWindow setFrame:r display:GetWXPeer()->IsShownOnScreen()];
33e90275
SC
432}
433
434void 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
441void 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
448void 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
457bool wxNonOwnedWindowCocoaImpl::SetShape(const wxRegion& region)
458{
459 return false;
460}
461
462void wxNonOwnedWindowCocoaImpl::SetTitle( const wxString& title, wxFontEncoding encoding )
463{
464 [m_macWindow setTitle:wxCFStringRef( title , encoding ).AsNSString()];
465}
466
467bool wxNonOwnedWindowCocoaImpl::IsMaximized() const
468{
469 return [m_macWindow isZoomed];
470}
471
472bool wxNonOwnedWindowCocoaImpl::IsIconized() const
473{
474 return [m_macWindow isMiniaturized];
475}
476
477void wxNonOwnedWindowCocoaImpl::Iconize( bool iconize )
478{
479 if ( iconize )
480 [m_macWindow miniaturize:nil];
481 else
482 [m_macWindow deminiaturize:nil];
483}
484
485void wxNonOwnedWindowCocoaImpl::Maximize(bool maximize)
486{
487 [m_macWindow zoom:nil];
488}
489
490
491// http://cocoadevcentral.com/articles/000028.php
492
493typedef struct
494{
495 int m_formerLevel;
496 NSRect m_formerFrame;
497} FullScreenData ;
498
499bool wxNonOwnedWindowCocoaImpl::IsFullScreen() const
500{
501 return m_macFullScreenData != NULL ;
502}
503
504bool 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
532void wxNonOwnedWindowCocoaImpl::RequestUserAttention(int WXUNUSED(flags))
533{
534}
dbeddfb9
SC
535
536void wxNonOwnedWindowCocoaImpl::ScreenToWindow( int *x, int *y )
537{
538 wxPoint p((x ? *x : 0), (y ? *y : 0) );
dbeddfb9 539 NSPoint nspt = wxToNSPoint( NULL, p );
54f11060
SC
540 nspt = [m_macWindow convertScreenToBase:nspt];
541 nspt = [[m_macWindow contentView] convertPoint:nspt fromView:nil];
542 p = wxFromNSPoint([m_macWindow contentView], nspt);
dbeddfb9 543 if ( x )
54f11060 544 *x = p.x;
dbeddfb9
SC
545 if ( y )
546 *y = p.y;
547}
548
549void wxNonOwnedWindowCocoaImpl::WindowToScreen( int *x, int *y )
550{
54f11060
SC
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);
dbeddfb9
SC
556 if ( x )
557 *x = p.x;
558 if ( y )
559 *y = p.y;
560}
561
562wxNonOwnedWindowImpl* 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;
54f11060 568}