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