]> git.saurik.com Git - wxWidgets.git/blame - src/osx/cocoa/nonownedwnd.mm
minor fixes; replace references to Windows95 with references to wxMSW where possible
[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
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{
230 [m_macWindow release];
231}
232
233void wxNonOwnedWindowCocoaImpl::Destroy()
234{
235 wxPendingDelete.Append( new wxDeferredObjectDeleter( this ) );
236}
237
238void wxNonOwnedWindowCocoaImpl::Create( wxWindow* parent, const wxPoint& pos, const wxSize& size,
239long style, long extraStyle, const wxString& name )
240{
dbeddfb9
SC
241 static wxNonOwnedWindowController* controller = NULL;
242
243 if ( !controller )
244 controller =[[wxNonOwnedWindowController alloc] init];
245
246
33e90275
SC
247 int windowstyle = NSBorderlessWindowMask;
248
249 if ( style & wxFRAME_TOOL_WINDOW )
dbeddfb9 250 m_macWindow = [wxNSPanel alloc];
33e90275 251 else
dbeddfb9 252 m_macWindow = [wxNSWindow alloc];
33e90275 253
e912ebe3 254 CGWindowLevel level = kCGNormalWindowLevel;
33e90275
SC
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
dbeddfb9
SC
335 [m_macWindow setImplementation:this];
336
33e90275
SC
337 [m_macWindow initWithContentRect:r
338 styleMask:windowstyle
339 backing:NSBackingStoreBuffered
340 defer:NO
341 ];
342
343 [m_macWindow setLevel:level];
dbeddfb9
SC
344
345 [m_macWindow setDelegate:controller];
346
33e90275
SC
347 // [m_macWindow makeKeyAndOrderFront:nil];
348}
349
350
351WXWindow wxNonOwnedWindowCocoaImpl::GetWXWindow() const
352{
353 return m_macWindow;
354}
355
356void wxNonOwnedWindowCocoaImpl::Raise()
357{
358 [m_macWindow orderWindow:NSWindowAbove relativeTo:0];
359}
360
361void wxNonOwnedWindowCocoaImpl::Lower()
362{
363 [m_macWindow orderWindow:NSWindowBelow relativeTo:0];
364}
365
366bool 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
378bool wxNonOwnedWindowCocoaImpl::ShowWithEffect(bool show, wxShowEffect effect, unsigned timeout)
379{
380 return Show(show);
381}
382
383void wxNonOwnedWindowCocoaImpl::Update()
384{
385 [m_macWindow displayIfNeeded];
386}
387
388bool wxNonOwnedWindowCocoaImpl::SetTransparent(wxByte alpha)
389{
390 [m_macWindow setAlphaValue:(CGFloat) alpha/255.0];
391 return true;
392}
393
394bool wxNonOwnedWindowCocoaImpl::SetBackgroundColour(const wxColour& col )
395{
396 return true;
397}
398
399void 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
416bool wxNonOwnedWindowCocoaImpl::SetBackgroundStyle(wxBackgroundStyle style)
417{
418 return true;
419}
420
421bool wxNonOwnedWindowCocoaImpl::CanSetTransparent()
422{
423 return true;
424}
425
426void wxNonOwnedWindowCocoaImpl::MoveWindow(int x, int y, int width, int height)
427{
428 NSRect r = wxToNSRect( NULL, wxRect(x,y,width, height) );
429 [m_macWindow setFrame:r display:YES];
430}
431
432void wxNonOwnedWindowCocoaImpl::GetPosition( int &x, int &y ) const
433{
434 wxRect r = wxFromNSRect( NULL, [m_macWindow frame] );
435 x = r.GetLeft();
436 y = r.GetTop();
437}
438
439void wxNonOwnedWindowCocoaImpl::GetSize( int &width, int &height ) const
440{
441 NSRect rect = [m_macWindow frame];
442 width = rect.size.width;
443 height = rect.size.height;
444}
445
446void wxNonOwnedWindowCocoaImpl::GetContentArea( int& left, int &right, int &width, int &height ) const
447{
448 NSRect outer = NSMakeRect(100,100,100,100);
449 NSRect content = [NSWindow contentRectForFrameRect:outer styleMask:[m_macWindow styleMask] ];
450 NSRect rect = [[m_macWindow contentView] frame];
451 width = rect.size.width;
452 height = rect.size.height;
453}
454
455bool wxNonOwnedWindowCocoaImpl::SetShape(const wxRegion& region)
456{
457 return false;
458}
459
460void wxNonOwnedWindowCocoaImpl::SetTitle( const wxString& title, wxFontEncoding encoding )
461{
462 [m_macWindow setTitle:wxCFStringRef( title , encoding ).AsNSString()];
463}
464
465bool wxNonOwnedWindowCocoaImpl::IsMaximized() const
466{
467 return [m_macWindow isZoomed];
468}
469
470bool wxNonOwnedWindowCocoaImpl::IsIconized() const
471{
472 return [m_macWindow isMiniaturized];
473}
474
475void wxNonOwnedWindowCocoaImpl::Iconize( bool iconize )
476{
477 if ( iconize )
478 [m_macWindow miniaturize:nil];
479 else
480 [m_macWindow deminiaturize:nil];
481}
482
483void wxNonOwnedWindowCocoaImpl::Maximize(bool maximize)
484{
485 [m_macWindow zoom:nil];
486}
487
488
489// http://cocoadevcentral.com/articles/000028.php
490
491typedef struct
492{
493 int m_formerLevel;
494 NSRect m_formerFrame;
495} FullScreenData ;
496
497bool wxNonOwnedWindowCocoaImpl::IsFullScreen() const
498{
499 return m_macFullScreenData != NULL ;
500}
501
502bool wxNonOwnedWindowCocoaImpl::ShowFullScreen(bool show, long style)
503{
504 if ( show )
505 {
506 FullScreenData *data = (FullScreenData *)m_macFullScreenData ;
507 delete data ;
508 data = new FullScreenData();
509
510 m_macFullScreenData = data ;
511 data->m_formerLevel = [m_macWindow level];
512 data->m_formerFrame = [m_macWindow frame];
513 CGDisplayCapture( kCGDirectMainDisplay );
514 [m_macWindow setLevel:CGShieldingWindowLevel()];
515 [m_macWindow setFrame:[[NSScreen mainScreen] frame] display:YES];
516 }
517 else if ( m_macFullScreenData != NULL )
518 {
519 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
520 CGDisplayRelease( kCGDirectMainDisplay );
521 [m_macWindow setLevel:data->m_formerLevel];
522 [m_macWindow setFrame:data->m_formerFrame display:YES];
523 delete data ;
524 m_macFullScreenData = NULL ;
525 }
526
527 return true;
528}
529
530void wxNonOwnedWindowCocoaImpl::RequestUserAttention(int WXUNUSED(flags))
531{
532}
dbeddfb9
SC
533
534void wxNonOwnedWindowCocoaImpl::ScreenToWindow( int *x, int *y )
535{
536 wxPoint p((x ? *x : 0), (y ? *y : 0) );
537 /*
538 NSPoint nspt = wxToNSPoint( NULL, p );
539
540 nspt = [[m_macWindow contentView] convertPoint:p toV:nil];
541 p = wxFromNSPoint(
542 */
543 if ( x )
544 *x = p.x;
545 if ( y )
546 *y = p.y;
547}
548
549void wxNonOwnedWindowCocoaImpl::WindowToScreen( int *x, int *y )
550{
551 wxPoint p( (x ? *x : 0), (y ? *y : 0) );
552 /*
553 p = [m_macWindow convertPoint:p toWindow:nil];
554 */
555 if ( x )
556 *x = p.x;
557 if ( y )
558 *y = p.y;
559}
560
561wxNonOwnedWindowImpl* 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}