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