]> git.saurik.com Git - wxWidgets.git/blame - src/osx/nonownedwnd_osx.cpp
The alignment controls are now left-aligned if the floating controls are not shown.
[wxWidgets.git] / src / osx / nonownedwnd_osx.cpp
CommitLineData
3ccc5735
SC
1/////////////////////////////////////////////////////////////////////////////
2// Name: src/osx/nonownedwnd_osx.cpp
3// Purpose: implementation of wxNonOwnedWindow
4// Author: Stefan Csomor
5// Created: 2008-03-24
3ccc5735
SC
6// Copyright: (c) Stefan Csomor 2008
7// Licence: wxWindows licence
8/////////////////////////////////////////////////////////////////////////////
9
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
13#ifndef WX_PRECOMP
14 #include "wx/app.h"
83ed867e 15 #include "wx/dcmemory.h"
e1f833da 16 #include "wx/log.h"
3ccc5735
SC
17#endif // WX_PRECOMP
18
19#include "wx/hashmap.h"
20#include "wx/evtloop.h"
21#include "wx/tooltip.h"
22#include "wx/nonownedwnd.h"
23
24#include "wx/osx/private.h"
25#include "wx/settings.h"
26#include "wx/frame.h"
27
28#if wxUSE_SYSTEM_OPTIONS
29 #include "wx/sysopt.h"
30#endif
31
32// ----------------------------------------------------------------------------
33// constants
34// ----------------------------------------------------------------------------
35
36// trace mask for activation tracing messages
37#define TRACE_ACTIVATE "activation"
38
39wxWindow* g_MacLastWindow = NULL ;
40
7dc941e3
SC
41clock_t wxNonOwnedWindow::s_lastFlush = 0;
42
3ccc5735
SC
43// unified title and toolbar constant - not in Tiger headers, so we duplicate it here
44#define kWindowUnifiedTitleAndToolbarAttribute (1 << 7)
45
46// ---------------------------------------------------------------------------
47// wxWindowMac utility functions
48// ---------------------------------------------------------------------------
49
638b3cd7 50WX_DECLARE_HASH_MAP(WXWindow, wxNonOwnedWindowImpl*, wxPointerHash, wxPointerEqual, MacWindowMap);
3ccc5735
SC
51
52static MacWindowMap wxWinMacWindowList;
53
638b3cd7 54wxNonOwnedWindow* wxNonOwnedWindow::GetFromWXWindow( WXWindow win )
3ccc5735 55{
638b3cd7 56 wxNonOwnedWindowImpl* impl = wxNonOwnedWindowImpl::FindFromWXWindow(win);
ce00f59b 57
638b3cd7 58 return ( impl != NULL ? impl->GetWXPeer() : NULL ) ;
3ccc5735
SC
59}
60
638b3cd7 61wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::FindFromWXWindow (WXWindow window)
3ccc5735 62{
638b3cd7 63 MacWindowMap::iterator node = wxWinMacWindowList.find(window);
ce00f59b 64
638b3cd7 65 return (node == wxWinMacWindowList.end()) ? NULL : node->second;
3ccc5735
SC
66}
67
638b3cd7 68void wxNonOwnedWindowImpl::RemoveAssociations( wxNonOwnedWindowImpl* impl)
3ccc5735
SC
69{
70 MacWindowMap::iterator it;
71 for ( it = wxWinMacWindowList.begin(); it != wxWinMacWindowList.end(); ++it )
72 {
638b3cd7 73 if ( it->second == impl )
3ccc5735
SC
74 {
75 wxWinMacWindowList.erase(it);
76 break;
77 }
78 }
79}
80
638b3cd7 81void wxNonOwnedWindowImpl::Associate( WXWindow window, wxNonOwnedWindowImpl *impl )
3ccc5735 82{
638b3cd7
SC
83 // adding NULL WindowRef is (first) surely a result of an error and
84 // nothing else :-)
85 wxCHECK_RET( window != (WXWindow) NULL, wxT("attempt to add a NULL WindowRef to window list") );
ce00f59b 86
638b3cd7 87 wxWinMacWindowList[window] = impl;
3ccc5735
SC
88}
89
90// ----------------------------------------------------------------------------
91// wxNonOwnedWindow creation
92// ----------------------------------------------------------------------------
93
94IMPLEMENT_ABSTRACT_CLASS( wxNonOwnedWindowImpl , wxObject )
95
96wxNonOwnedWindow *wxNonOwnedWindow::s_macDeactivateWindow = NULL;
97
98void wxNonOwnedWindow::Init()
99{
100 m_nowpeer = NULL;
17e2694c 101 m_isNativeWindowWrapper = false;
3ccc5735
SC
102}
103
104bool wxNonOwnedWindow::Create(wxWindow *parent,
105 wxWindowID id,
e08b4823
VZ
106 const wxPoint& posOrig,
107 const wxSize& sizeOrig,
3ccc5735
SC
108 long style,
109 const wxString& name)
110{
3ccc5735
SC
111 m_windowStyle = style;
112
113 SetName( name );
114
115 m_windowId = id == -1 ? NewControlId() : id;
116 m_windowStyle = style;
117 m_isShown = false;
118
e08b4823 119 // use the appropriate defaults for the position and size if necessary
e08b4823
VZ
120 wxSize size(sizeOrig);
121 if ( !size.IsFullySpecified() )
122 size.SetDefaults(wxTopLevelWindow::GetDefaultSize());
3ccc5735 123
0d65f8d2
VZ
124 wxPoint pos(posOrig);
125 if ( !pos.IsFullySpecified() )
126 {
127 wxRect rectWin = wxRect(size).CentreIn(wxGetClientDisplayRect());
128
129 // The size of the window is often not really known yet, TLWs are often
130 // created with some small initial size and later are fitted to contain
131 // their children so centering the window will show it too much to the
132 // right and bottom, adjust for it by putting it more to the left and
133 // center.
134 rectWin.x /= 2;
135 rectWin.y /= 2;
136
137 pos.SetDefaults(rectWin.GetPosition());
138 }
139
e08b4823
VZ
140 // create frame.
141 m_nowpeer = wxNonOwnedWindowImpl::CreateNonOwnedWindow
142 (
143 this, parent,
144 pos , size,
145 style, GetExtraStyle(),
146 name
147 );
638b3cd7 148 wxNonOwnedWindowImpl::Associate( m_nowpeer->GetWXWindow() , m_nowpeer ) ;
22756322 149 SetPeer(wxWidgetImpl::CreateContentView(this));
3ccc5735
SC
150
151 DoSetWindowVariant( m_windowVariant ) ;
152
153 wxWindowCreateEvent event(this);
154 HandleWindowEvent(event);
155
156 SetBackgroundColour(wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE ));
157
158 if ( parent )
159 parent->AddChild(this);
160
161 return true;
162}
163
17e2694c
SC
164bool wxNonOwnedWindow::Create(wxWindow *parent, WXWindow nativeWindow)
165{
d623e8b1
SC
166 if ( parent )
167 parent->AddChild(this);
ce00f59b 168
d623e8b1 169 SubclassWin(nativeWindow);
ce00f59b 170
d623e8b1
SC
171 return true;
172}
173
174void wxNonOwnedWindow::SubclassWin(WXWindow nativeWindow)
175{
176 wxASSERT_MSG( !m_isNativeWindowWrapper, wxT("subclassing window twice?") );
177 wxASSERT_MSG( m_nowpeer == NULL, wxT("window already was created") );
178
179 m_nowpeer = wxNonOwnedWindowImpl::CreateNonOwnedWindow(this, GetParent(), nativeWindow );
17e2694c 180 m_isNativeWindowWrapper = true;
638b3cd7 181 wxNonOwnedWindowImpl::Associate( m_nowpeer->GetWXWindow() , m_nowpeer ) ;
22756322 182 SetPeer(wxWidgetImpl::CreateContentView(this));
d623e8b1 183}
17e2694c 184
d623e8b1
SC
185void wxNonOwnedWindow::UnsubclassWin()
186{
187 wxASSERT_MSG( m_isNativeWindowWrapper, wxT("window was not subclassed") );
188
189 if ( GetParent() )
190 GetParent()->RemoveChild(this);
ce00f59b
VZ
191
192 wxNonOwnedWindowImpl::RemoveAssociations(m_nowpeer) ;
d623e8b1 193 wxDELETE(m_nowpeer);
22756322 194 SetPeer(NULL);
77eb08cc 195 m_isNativeWindowWrapper = false;
17e2694c
SC
196}
197
d623e8b1 198
3ccc5735
SC
199wxNonOwnedWindow::~wxNonOwnedWindow()
200{
c6212a0c 201 SendDestroyEvent();
03647350 202
638b3cd7 203 wxNonOwnedWindowImpl::RemoveAssociations(m_nowpeer) ;
03647350 204
524c47aa 205 DestroyChildren();
03647350 206
5276b0a5 207 wxDELETE(m_nowpeer);
3ccc5735
SC
208
209 // avoid dangling refs
210 if ( s_macDeactivateWindow == this )
211 s_macDeactivateWindow = NULL;
212}
213
e2758e21 214bool wxNonOwnedWindow::Destroy()
0aaa6ace 215{
e2758e21 216 WillBeDestroyed();
ce00f59b 217
e2758e21
KO
218 return wxWindow::Destroy();
219}
220
221void wxNonOwnedWindow::WillBeDestroyed()
222{
0aaa6ace
SC
223 if ( m_nowpeer )
224 m_nowpeer->WillBeDestroyed();
225}
226
3ccc5735
SC
227// ----------------------------------------------------------------------------
228// wxNonOwnedWindow misc
229// ----------------------------------------------------------------------------
230
ab9a0b84
VZ
231bool wxNonOwnedWindow::OSXShowWithEffect(bool show,
232 wxShowEffect effect,
233 unsigned timeout)
03647350 234{
ab9a0b84
VZ
235 // Cocoa code needs to manage window visibility on its own and so calls
236 // wxWindow::Show() as needed but if we already changed the internal
237 // visibility flag here, Show() would do nothing, so avoid doing it
238#if wxOSX_USE_CARBON
239 if ( !wxWindow::Show(show) )
3ccc5735 240 return false;
ab9a0b84 241#endif // Carbon
3ccc5735 242
ab9a0b84
VZ
243 if ( effect == wxSHOW_EFFECT_NONE ||
244 !m_nowpeer || !m_nowpeer->ShowWithEffect(show, effect, timeout) )
245 return Show(show);
3ccc5735 246
ab9a0b84
VZ
247 if ( show )
248 {
249 // as apps expect a size event to occur when the window is shown,
250 // generate one when it is shown with effect too
c609b780 251 SendSizeEvent();
ab9a0b84 252 }
3ccc5735 253
ab9a0b84 254 return true;
3ccc5735
SC
255}
256
257wxPoint wxNonOwnedWindow::GetClientAreaOrigin() const
258{
259 int left, top, width, height;
260 m_nowpeer->GetContentArea(left, top, width, height);
261 return wxPoint(left, top);
262}
263
264bool wxNonOwnedWindow::SetBackgroundColour(const wxColour& c )
03647350 265{
3ccc5735
SC
266 if ( !wxWindow::SetBackgroundColour(c) && m_hasBgCol )
267 return false ;
03647350 268
3ccc5735
SC
269 if ( GetBackgroundStyle() != wxBG_STYLE_CUSTOM )
270 {
524c47aa
SC
271 if ( m_nowpeer )
272 return m_nowpeer->SetBackgroundColour(c);
3ccc5735
SC
273 }
274 return true;
03647350 275}
3ccc5735 276
b6dc21e7
KO
277void wxNonOwnedWindow::SetWindowStyleFlag(long flags)
278{
279 if (flags == GetWindowStyleFlag())
280 return;
ce00f59b 281
b6dc21e7 282 wxWindow::SetWindowStyleFlag(flags);
ce00f59b 283
b6dc21e7
KO
284 if (m_nowpeer)
285 m_nowpeer->SetWindowStyleFlag(flags);
286}
287
3ccc5735
SC
288// Raise the window to the top of the Z order
289void wxNonOwnedWindow::Raise()
290{
291 m_nowpeer->Raise();
292}
293
294// Lower the window to the bottom of the Z order
295void wxNonOwnedWindow::Lower()
296{
297 m_nowpeer->Lower();
298}
299
300void wxNonOwnedWindow::HandleActivated( double timestampsec, bool didActivate )
301{
302 MacActivate( (int) (timestampsec * 1000) , didActivate);
303 wxActivateEvent wxevent(wxEVT_ACTIVATE, didActivate , GetId());
304 wxevent.SetTimestamp( (int) (timestampsec * 1000) );
305 wxevent.SetEventObject(this);
306 HandleWindowEvent(wxevent);
307}
308
f86a4930 309void wxNonOwnedWindow::HandleResized( double WXUNUSED(timestampsec) )
3ccc5735 310{
c609b780 311 SendSizeEvent();
3ccc5735
SC
312 // we have to inform some controls that have to reset things
313 // relative to the toplevel window (e.g. OpenGL buffers)
314 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
315}
316
0faf03bf 317void wxNonOwnedWindow::HandleResizing( double WXUNUSED(timestampsec), wxRect* rect )
3ccc5735
SC
318{
319 wxRect r = *rect ;
320
321 // this is a EVT_SIZING not a EVT_SIZE type !
322 wxSizeEvent wxevent( r , GetId() ) ;
323 wxevent.SetEventObject( this ) ;
324 if ( HandleWindowEvent(wxevent) )
325 r = wxevent.GetRect() ;
326
327 if ( GetMaxWidth() != -1 && r.GetWidth() > GetMaxWidth() )
328 r.SetWidth( GetMaxWidth() ) ;
329 if ( GetMaxHeight() != -1 && r.GetHeight() > GetMaxHeight() )
330 r.SetHeight( GetMaxHeight() ) ;
331 if ( GetMinWidth() != -1 && r.GetWidth() < GetMinWidth() )
332 r.SetWidth( GetMinWidth() ) ;
333 if ( GetMinHeight() != -1 && r.GetHeight() < GetMinHeight() )
334 r.SetHeight( GetMinHeight() ) ;
335
336 *rect = r;
337 // TODO actuall this is too early, in case the window extents are adjusted
338 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
339}
340
341void wxNonOwnedWindow::HandleMoved( double timestampsec )
342{
343 wxMoveEvent wxevent( GetPosition() , GetId());
344 wxevent.SetTimestamp( (int) (timestampsec * 1000) );
345 wxevent.SetEventObject( this );
346 HandleWindowEvent(wxevent);
347}
348
349void wxNonOwnedWindow::MacDelayedDeactivation(long timestamp)
350{
351 if (s_macDeactivateWindow)
352 {
353 wxLogTrace(TRACE_ACTIVATE,
354 wxT("Doing delayed deactivation of %p"),
355 s_macDeactivateWindow);
356
357 s_macDeactivateWindow->MacActivate(timestamp, false);
358 }
359}
360
361void wxNonOwnedWindow::MacActivate( long timestamp , bool WXUNUSED(inIsActivating) )
362{
363 wxLogTrace(TRACE_ACTIVATE, wxT("TopLevel=%p::MacActivate"), this);
364
365 if (s_macDeactivateWindow == this)
366 s_macDeactivateWindow = NULL;
367
368 MacDelayedDeactivation(timestamp);
369}
370
371bool wxNonOwnedWindow::Show(bool show)
372{
373 if ( !wxWindow::Show(show) )
374 return false;
375
376 if ( m_nowpeer )
377 m_nowpeer->Show(show);
03647350 378
3ccc5735
SC
379 if ( show )
380 {
381 // because apps expect a size event to occur at this moment
c609b780 382 SendSizeEvent();
3ccc5735 383 }
03647350 384
3ccc5735
SC
385 return true ;
386}
387
388bool wxNonOwnedWindow::SetTransparent(wxByte alpha)
389{
390 return m_nowpeer->SetTransparent(alpha);
391}
392
393
394bool wxNonOwnedWindow::CanSetTransparent()
395{
396 return m_nowpeer->CanSetTransparent();
397}
398
399
400void wxNonOwnedWindow::SetExtraStyle(long exStyle)
401{
402 if ( GetExtraStyle() == exStyle )
403 return ;
404
405 wxWindow::SetExtraStyle( exStyle ) ;
406
407 if ( m_nowpeer )
408 m_nowpeer->SetExtraStyle(exStyle);
409}
410
411bool wxNonOwnedWindow::SetBackgroundStyle(wxBackgroundStyle style)
412{
413 if ( !wxWindow::SetBackgroundStyle(style) )
414 return false ;
03647350 415
4d949e7f 416 return m_nowpeer ? m_nowpeer->SetBackgroundStyle(style) : true;
3ccc5735
SC
417}
418
419void wxNonOwnedWindow::DoMoveWindow(int x, int y, int width, int height)
420{
b8afff01
SC
421 if ( m_nowpeer == NULL )
422 return;
423
3ccc5735
SC
424 m_cachedClippedRectValid = false ;
425
426 m_nowpeer->MoveWindow(x, y, width, height);
427 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
428}
429
430void wxNonOwnedWindow::DoGetPosition( int *x, int *y ) const
431{
b8afff01
SC
432 if ( m_nowpeer == NULL )
433 return;
434
3ccc5735
SC
435 int x1,y1 ;
436 m_nowpeer->GetPosition(x1, y1);
437
438 if (x)
439 *x = x1 ;
440 if (y)
441 *y = y1 ;
442}
443
444void wxNonOwnedWindow::DoGetSize( int *width, int *height ) const
445{
b8afff01
SC
446 if ( m_nowpeer == NULL )
447 return;
03647350 448
3ccc5735 449 int w,h;
03647350 450
3ccc5735
SC
451 m_nowpeer->GetSize(w, h);
452
453 if (width)
454 *width = w ;
455 if (height)
456 *height = h ;
457}
458
459void wxNonOwnedWindow::DoGetClientSize( int *width, int *height ) const
460{
b8afff01
SC
461 if ( m_nowpeer == NULL )
462 return;
463
3ccc5735 464 int left, top, w, h;
bacea497
SC
465 // under iphone with a translucent status bar the m_nowpeer returns the
466 // inner area, while the content area extends under the translucent
467 // status bar, therefore we use the content view's area
5397ea53 468#ifdef __WXOSX_IPHONE__
22756322 469 GetPeer()->GetContentArea(left, top, w, h);
5397ea53 470#else
3ccc5735 471 m_nowpeer->GetContentArea(left, top, w, h);
5397ea53
SC
472#endif
473
3ccc5735
SC
474 if (width)
475 *width = w ;
476 if (height)
477 *height = h ;
478}
479
7dc941e3
SC
480void wxNonOwnedWindow::WindowWasPainted()
481{
482 s_lastFlush = clock();
483}
3ccc5735
SC
484
485void wxNonOwnedWindow::Update()
486{
7dc941e3
SC
487 if ( clock() - s_lastFlush > CLOCKS_PER_SEC / 30 )
488 {
489 s_lastFlush = clock();
490 m_nowpeer->Update();
491 }
3ccc5735
SC
492}
493
494WXWindow wxNonOwnedWindow::GetWXWindow() const
495{
496 return m_nowpeer ? m_nowpeer->GetWXWindow() : NULL;
497}
498
499// ---------------------------------------------------------------------------
500// Shape implementation
501// ---------------------------------------------------------------------------
502
46ea442c 503bool wxNonOwnedWindow::DoClearShape()
3ccc5735 504{
46ea442c
VZ
505 m_shape.Clear();
506
507 wxSize sz = GetClientSize();
508 wxRegion region(0, 0, sz.x, sz.y);
509
510 return m_nowpeer->SetShape(region);
511}
3ccc5735 512
46ea442c
VZ
513bool wxNonOwnedWindow::DoSetRegionShape(const wxRegion& region)
514{
b61b8371 515 m_shape = region;
ce00f59b 516
46ea442c
VZ
517 return m_nowpeer->SetShape(region);
518}
519
520#if wxUSE_GRAPHICS_CONTEXT
521
522#include "wx/scopedptr.h"
523
524bool wxNonOwnedWindow::DoSetPathShape(const wxGraphicsPath& path)
525{
526 m_shapePath = path;
527
528 // Convert the path to wxRegion by rendering the path on a window-sized
529 // bitmap, creating a mask from it and finally creating the region from
530 // this mask.
531 wxBitmap bmp(GetSize());
532
3ccc5735 533 {
46ea442c
VZ
534 wxMemoryDC dc(bmp);
535 dc.SetBackground(*wxBLACK);
536 dc.Clear();
537
538 wxScopedPtr<wxGraphicsContext> context(wxGraphicsContext::Create(dc));
539 context->SetBrush(*wxWHITE);
540 context->FillPath(m_shapePath);
3ccc5735
SC
541 }
542
46ea442c
VZ
543 bmp.SetMask(new wxMask(bmp, *wxBLACK));
544
545 return DoSetRegionShape(wxRegion(bmp));
3ccc5735 546}
46ea442c
VZ
547
548#endif // wxUSE_GRAPHICS_CONTEXT