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