]> git.saurik.com Git - wxWidgets.git/blame - src/osx/cocoa/nonownedwnd.mm
Make sure the user can't select the text.
[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
9130dfd7
KO
64typedef void (*wxOSX_NoResponderHandlerPtr)(NSView* self, SEL _cmd, SEL selector);
65
dbeddfb9
SC
66@interface wxNSWindow : NSWindow
67
68{
69 wxNonOwnedWindowCocoaImpl* impl;
70}
71
72- (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation;
73- (wxNonOwnedWindowCocoaImpl*) implementation;
9130dfd7 74- (void)noResponderFor: (SEL) selector;
dbeddfb9
SC
75@end
76
77@implementation wxNSWindow
78
79- (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation
80{
81 impl = theImplementation;
82}
83
84- (wxNonOwnedWindowCocoaImpl*) implementation
85{
86 return impl;
87}
88
9130dfd7
KO
89// NB: if we don't do this, all key downs that get handled lead to a NSBeep
90- (void)noResponderFor: (SEL) selector
91{
92 if (selector != @selector(keyDown:))
93 {
94 wxOSX_NoResponderHandlerPtr superimpl = (wxOSX_NoResponderHandlerPtr) [[self superclass] instanceMethodForSelector:@selector(noResponderFor:)];
95 superimpl(self, @selector(noResponderFor:), selector);
96 }
97}
dbeddfb9
SC
98
99@end
100
bb839504 101@interface wxNSPanel : NSPanel
dbeddfb9
SC
102
103{
bb839504 104 wxNonOwnedWindowCocoaImpl* impl;
dbeddfb9
SC
105}
106
bb839504
KO
107- (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation;
108- (wxNonOwnedWindowCocoaImpl*) implementation;
9130dfd7 109- (void)noResponderFor: (SEL) selector;
dbeddfb9
SC
110@end
111
112@implementation wxNSPanel
113
bb839504
KO
114- (void)setImplementation: (wxNonOwnedWindowCocoaImpl *) theImplementation
115{
116 impl = theImplementation;
117}
118
119- (wxNonOwnedWindowCocoaImpl*) implementation
120{
121 return impl;
122}
123
9130dfd7
KO
124// NB: if we don't do this, all key downs that get handled lead to a NSBeep
125- (void)noResponderFor: (SEL) selector
126{
127 if (selector != @selector(keyDown:))
128 {
129 wxOSX_NoResponderHandlerPtr superimpl = (wxOSX_NoResponderHandlerPtr) [[self superclass] instanceMethodForSelector:@selector(noResponderFor:)];
130 superimpl(self, @selector(noResponderFor:), selector);
131 }
132}
133
dbeddfb9
SC
134@end
135
136
137//
138// controller
139//
140
141@interface wxNonOwnedWindowController : NSObject
142{
143}
144
145- (void)windowDidResize:(NSNotification *)notification;
146- (NSSize)windowWillResize:(NSWindow *)window toSize:(NSSize)proposedFrameSize;
147- (void)windowDidResignMain:(NSNotification *)notification;
148- (void)windowDidBecomeMain:(NSNotification *)notification;
149- (void)windowDidMove:(NSNotification *)notification;
150- (BOOL)windowShouldClose:(id)window;
151
152@end
153
154@implementation wxNonOwnedWindowController
155
156- (id) init
157{
158 [super init];
159 return self;
160}
161
162- (BOOL)windowShouldClose:(id)nwindow
163{
164 wxNSWindow* window = (wxNSWindow*) nwindow;
165 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
166 if ( windowimpl )
167 {
168 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
169 if ( wxpeer )
170 wxpeer->Close();
171 }
172 return NO;
173}
174
5da125c7 175- (NSSize)windowWillResize:(NSWindow *)win
dbeddfb9
SC
176 toSize:(NSSize)proposedFrameSize
177{
5da125c7
SC
178 NSRect frame = [win frame];
179 wxRect wxframe = wxFromNSRect( NULL, frame );
180 wxframe.SetWidth( proposedFrameSize.width );
181 wxframe.SetHeight( proposedFrameSize.height );
182 wxNSWindow* window = (wxNSWindow*) win;
183 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
184 if ( windowimpl )
185 {
186 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
187 if ( wxpeer )
188 {
189 wxpeer->HandleResizing( 0, &wxframe );
190 NSSize newSize = NSMakeSize(wxframe.GetWidth(), wxframe.GetHeight());
191 return newSize;
192 }
193 }
194
dbeddfb9
SC
195 return proposedFrameSize;
196}
197
198- (void)windowDidResize:(NSNotification *)notification
199{
200 wxNSWindow* window = (wxNSWindow*) [notification object];
201 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
202 if ( windowimpl )
203 {
204 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
205 if ( wxpeer )
206 wxpeer->HandleResized(0);
207 }
208}
209
210- (void)windowDidMove:(NSNotification *)notification
211{
212 wxNSWindow* window = (wxNSWindow*) [notification object];
213 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
214 if ( windowimpl )
215 {
216 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
217 if ( wxpeer )
218 wxpeer->HandleMoved(0);
219 }
220}
221
222- (void)windowDidBecomeMain:(NSNotification *)notification
223{
224 wxNSWindow* window = (wxNSWindow*) [notification object];
225 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
226 if ( windowimpl )
227 {
228 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
229 if ( wxpeer )
230 wxpeer->HandleActivated(0, true);
231 }
232}
233
234- (void)windowDidResignMain:(NSNotification *)notification
235{
236 wxNSWindow* window = (wxNSWindow*) [notification object];
237 wxNonOwnedWindowCocoaImpl* windowimpl = [window implementation];
238 if ( windowimpl )
239 {
240 wxNonOwnedWindow* wxpeer = windowimpl->GetWXPeer();
241 if ( wxpeer )
242 wxpeer->HandleActivated(0, false);
243 }
244}
245
246@end
247
33e90275
SC
248IMPLEMENT_DYNAMIC_CLASS( wxNonOwnedWindowCocoaImpl , wxNonOwnedWindowImpl )
249
250wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl( wxNonOwnedWindow* nonownedwnd) :
251 wxNonOwnedWindowImpl(nonownedwnd)
252{
253 m_macWindow = NULL;
254 m_macFullScreenData = NULL;
255}
256
257wxNonOwnedWindowCocoaImpl::wxNonOwnedWindowCocoaImpl()
258{
259 m_macWindow = NULL;
260 m_macFullScreenData = NULL;
261}
262
263wxNonOwnedWindowCocoaImpl::~wxNonOwnedWindowCocoaImpl()
264{
be136f07 265 [m_macWindow setImplementation:nil];
42c2b729 266 [m_macWindow setDelegate:nil];
33e90275
SC
267 [m_macWindow release];
268}
269
270void wxNonOwnedWindowCocoaImpl::Destroy()
271{
272 wxPendingDelete.Append( new wxDeferredObjectDeleter( this ) );
273}
274
275void wxNonOwnedWindowCocoaImpl::Create( wxWindow* parent, const wxPoint& pos, const wxSize& size,
276long style, long extraStyle, const wxString& name )
277{
dbeddfb9
SC
278 static wxNonOwnedWindowController* controller = NULL;
279
280 if ( !controller )
281 controller =[[wxNonOwnedWindowController alloc] init];
282
283
33e90275
SC
284 int windowstyle = NSBorderlessWindowMask;
285
286 if ( style & wxFRAME_TOOL_WINDOW )
dbeddfb9 287 m_macWindow = [wxNSPanel alloc];
33e90275 288 else
dbeddfb9 289 m_macWindow = [wxNSWindow alloc];
33e90275 290
e912ebe3 291 CGWindowLevel level = kCGNormalWindowLevel;
33e90275
SC
292
293 if ( style & wxFRAME_TOOL_WINDOW )
294 {
aa960026
KO
295 windowstyle |= NSUtilityWindowMask;
296 if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) ||
297 ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) )
298 {
299 windowstyle |= NSTitledWindowMask ;
300 }
33e90275
SC
301 }
302 else if ( ( style & wxPOPUP_WINDOW ) )
303 {
304 level = kCGPopUpMenuWindowLevel;
305 /*
306 if ( ( style & wxBORDER_NONE ) )
307 {
308 wclass = kHelpWindowClass ; // has no border
309 attr |= kWindowNoShadowAttribute;
310 }
311 else
312 {
313 wclass = kPlainWindowClass ; // has a single line border, it will have to do for now
314 }
315 */
316 }
317 else if ( ( style & wxCAPTION ) )
318 {
319 windowstyle |= NSTitledWindowMask ;
320 }
321 else if ( ( style & wxFRAME_DRAWER ) )
322 {
323 /*
324 wclass = kDrawerWindowClass;
325 */
326 }
327 else
328 {
329 // set these even if we have no title, otherwise the controls won't be visible
330 if ( ( style & wxMINIMIZE_BOX ) || ( style & wxMAXIMIZE_BOX ) ||
331 ( style & wxCLOSE_BOX ) || ( style & wxSYSTEM_MENU ) )
332 {
333 windowstyle |= NSTitledWindowMask ;
334 }
335 /*
336 else if ( ( style & wxNO_BORDER ) )
337 {
338 wclass = kSimpleWindowClass ;
339 }
340 else
341 {
342 wclass = kPlainWindowClass ;
343 }
344 */
345 }
346
347 if ( windowstyle & NSTitledWindowMask )
348 {
349 if ( ( style & wxMINIMIZE_BOX ) )
350 windowstyle |= NSMiniaturizableWindowMask ;
351
352 if ( ( style & wxMAXIMIZE_BOX ) )
353 windowstyle |= NSResizableWindowMask ; // TODO showing ZOOM ?
354
355 if ( ( style & wxRESIZE_BORDER ) )
356 windowstyle |= NSResizableWindowMask ;
357
358 if ( ( style & wxCLOSE_BOX) )
359 windowstyle |= NSClosableWindowMask ;
360 }
361 if ( extraStyle & wxFRAME_EX_METAL)
362 windowstyle |= NSTexturedBackgroundWindowMask;
363
bb839504
KO
364 if ( ( style & wxFRAME_FLOAT_ON_PARENT ) || ( style & wxFRAME_TOOL_WINDOW ) )
365 level = kCGFloatingWindowLevel;
366
33e90275
SC
367 if ( ( style & wxSTAY_ON_TOP ) )
368 level = kCGUtilityWindowLevel;
33e90275
SC
369
370 NSRect r = wxToNSRect( NULL, wxRect( pos, size) );
371
dbeddfb9
SC
372 [m_macWindow setImplementation:this];
373
33e90275
SC
374 [m_macWindow initWithContentRect:r
375 styleMask:windowstyle
376 backing:NSBackingStoreBuffered
377 defer:NO
378 ];
379
380 [m_macWindow setLevel:level];
dbeddfb9
SC
381
382 [m_macWindow setDelegate:controller];
383
826da451
KO
384 [m_macWindow setAcceptsMouseMovedEvents: YES];
385
33e90275
SC
386 // [m_macWindow makeKeyAndOrderFront:nil];
387}
388
389
390WXWindow wxNonOwnedWindowCocoaImpl::GetWXWindow() const
391{
392 return m_macWindow;
393}
394
395void wxNonOwnedWindowCocoaImpl::Raise()
396{
397 [m_macWindow orderWindow:NSWindowAbove relativeTo:0];
398}
399
400void wxNonOwnedWindowCocoaImpl::Lower()
401{
402 [m_macWindow orderWindow:NSWindowBelow relativeTo:0];
403}
404
405bool wxNonOwnedWindowCocoaImpl::Show(bool show)
406{
407 if ( show )
408 {
409 [m_macWindow orderFront:nil];
410 [[m_macWindow contentView] setNeedsDisplay:YES];
411 }
412 else
413 [m_macWindow orderOut:nil];
414 return true;
415}
416
417bool wxNonOwnedWindowCocoaImpl::ShowWithEffect(bool show, wxShowEffect effect, unsigned timeout)
418{
419 return Show(show);
420}
421
422void wxNonOwnedWindowCocoaImpl::Update()
423{
424 [m_macWindow displayIfNeeded];
425}
426
427bool wxNonOwnedWindowCocoaImpl::SetTransparent(wxByte alpha)
428{
429 [m_macWindow setAlphaValue:(CGFloat) alpha/255.0];
430 return true;
431}
432
433bool wxNonOwnedWindowCocoaImpl::SetBackgroundColour(const wxColour& col )
434{
435 return true;
436}
437
438void wxNonOwnedWindowCocoaImpl::SetExtraStyle( long exStyle )
439{
440 if ( m_macWindow )
441 {
442 bool metal = exStyle & wxFRAME_EX_METAL ;
443 int windowStyle = [ m_macWindow styleMask];
444 if ( metal && !(windowStyle & NSTexturedBackgroundWindowMask) )
445 {
446 wxFAIL_MSG( _T("Metal Style cannot be changed after creation") );
447 }
448 else if ( !metal && (windowStyle & NSTexturedBackgroundWindowMask ) )
449 {
450 wxFAIL_MSG( _T("Metal Style cannot be changed after creation") );
451 }
452 }
453}
454
455bool wxNonOwnedWindowCocoaImpl::SetBackgroundStyle(wxBackgroundStyle style)
456{
457 return true;
458}
459
460bool wxNonOwnedWindowCocoaImpl::CanSetTransparent()
461{
462 return true;
463}
464
465void wxNonOwnedWindowCocoaImpl::MoveWindow(int x, int y, int width, int height)
466{
467 NSRect r = wxToNSRect( NULL, wxRect(x,y,width, height) );
54f11060
SC
468 // do not trigger refreshes upon invisible and possible partly created objects
469 [m_macWindow setFrame:r display:GetWXPeer()->IsShownOnScreen()];
33e90275
SC
470}
471
472void wxNonOwnedWindowCocoaImpl::GetPosition( int &x, int &y ) const
473{
474 wxRect r = wxFromNSRect( NULL, [m_macWindow frame] );
475 x = r.GetLeft();
476 y = r.GetTop();
477}
478
479void wxNonOwnedWindowCocoaImpl::GetSize( int &width, int &height ) const
480{
481 NSRect rect = [m_macWindow frame];
482 width = rect.size.width;
483 height = rect.size.height;
484}
485
b0ec7fbb 486void wxNonOwnedWindowCocoaImpl::GetContentArea( int& left, int &top, int &width, int &height ) const
33e90275
SC
487{
488 NSRect outer = NSMakeRect(100,100,100,100);
489 NSRect content = [NSWindow contentRectForFrameRect:outer styleMask:[m_macWindow styleMask] ];
490 NSRect rect = [[m_macWindow contentView] frame];
b0ec7fbb
KO
491 left = rect.origin.x;
492 top = rect.origin.y;
33e90275
SC
493 width = rect.size.width;
494 height = rect.size.height;
495}
496
497bool wxNonOwnedWindowCocoaImpl::SetShape(const wxRegion& region)
498{
499 return false;
500}
501
502void wxNonOwnedWindowCocoaImpl::SetTitle( const wxString& title, wxFontEncoding encoding )
503{
504 [m_macWindow setTitle:wxCFStringRef( title , encoding ).AsNSString()];
505}
506
507bool wxNonOwnedWindowCocoaImpl::IsMaximized() const
508{
509 return [m_macWindow isZoomed];
510}
511
512bool wxNonOwnedWindowCocoaImpl::IsIconized() const
513{
514 return [m_macWindow isMiniaturized];
515}
516
517void wxNonOwnedWindowCocoaImpl::Iconize( bool iconize )
518{
519 if ( iconize )
520 [m_macWindow miniaturize:nil];
521 else
522 [m_macWindow deminiaturize:nil];
523}
524
525void wxNonOwnedWindowCocoaImpl::Maximize(bool maximize)
526{
527 [m_macWindow zoom:nil];
528}
529
530
531// http://cocoadevcentral.com/articles/000028.php
532
533typedef struct
534{
535 int m_formerLevel;
536 NSRect m_formerFrame;
537} FullScreenData ;
538
539bool wxNonOwnedWindowCocoaImpl::IsFullScreen() const
540{
541 return m_macFullScreenData != NULL ;
542}
543
544bool wxNonOwnedWindowCocoaImpl::ShowFullScreen(bool show, long style)
545{
546 if ( show )
547 {
548 FullScreenData *data = (FullScreenData *)m_macFullScreenData ;
549 delete data ;
550 data = new FullScreenData();
551
552 m_macFullScreenData = data ;
553 data->m_formerLevel = [m_macWindow level];
554 data->m_formerFrame = [m_macWindow frame];
555 CGDisplayCapture( kCGDirectMainDisplay );
556 [m_macWindow setLevel:CGShieldingWindowLevel()];
557 [m_macWindow setFrame:[[NSScreen mainScreen] frame] display:YES];
558 }
559 else if ( m_macFullScreenData != NULL )
560 {
561 FullScreenData *data = (FullScreenData *) m_macFullScreenData ;
562 CGDisplayRelease( kCGDirectMainDisplay );
563 [m_macWindow setLevel:data->m_formerLevel];
564 [m_macWindow setFrame:data->m_formerFrame display:YES];
565 delete data ;
566 m_macFullScreenData = NULL ;
567 }
568
569 return true;
570}
571
572void wxNonOwnedWindowCocoaImpl::RequestUserAttention(int WXUNUSED(flags))
573{
574}
dbeddfb9
SC
575
576void wxNonOwnedWindowCocoaImpl::ScreenToWindow( int *x, int *y )
577{
578 wxPoint p((x ? *x : 0), (y ? *y : 0) );
dbeddfb9 579 NSPoint nspt = wxToNSPoint( NULL, p );
54f11060
SC
580 nspt = [m_macWindow convertScreenToBase:nspt];
581 nspt = [[m_macWindow contentView] convertPoint:nspt fromView:nil];
582 p = wxFromNSPoint([m_macWindow contentView], nspt);
dbeddfb9 583 if ( x )
54f11060 584 *x = p.x;
dbeddfb9
SC
585 if ( y )
586 *y = p.y;
587}
588
589void wxNonOwnedWindowCocoaImpl::WindowToScreen( int *x, int *y )
590{
54f11060
SC
591 wxPoint p((x ? *x : 0), (y ? *y : 0) );
592 NSPoint nspt = wxToNSPoint( [m_macWindow contentView], p );
593 nspt = [[m_macWindow contentView] convertPoint:nspt toView:nil];
594 nspt = [m_macWindow convertBaseToScreen:nspt];
595 p = wxFromNSPoint( NULL, nspt);
dbeddfb9
SC
596 if ( x )
597 *x = p.x;
598 if ( y )
599 *y = p.y;
600}
601
602wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::CreateNonOwnedWindow( wxNonOwnedWindow* wxpeer, wxWindow* parent, const wxPoint& pos, const wxSize& size,
603 long style, long extraStyle, const wxString& name )
604{
605 wxNonOwnedWindowImpl* now = new wxNonOwnedWindowCocoaImpl( wxpeer );
606 now->Create( parent, pos, size, style , extraStyle, name );
607 return now;
54f11060 608}