]> git.saurik.com Git - wxWidgets.git/blame - src/osx/nonownedwnd_osx.cpp
Remove the native toolbar from the frame in Destroy() rather than the destructor...
[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
6// RCS-ID: $Id: nonownedwnd.cpp 50329 2007-11-29 17:00:58Z VS $
7// Copyright: (c) Stefan Csomor 2008
8// Licence: wxWindows licence
9/////////////////////////////////////////////////////////////////////////////
10
11// For compilers that support precompilation, includes "wx.h".
12#include "wx/wxprec.h"
13
14#ifndef WX_PRECOMP
15 #include "wx/app.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
41// unified title and toolbar constant - not in Tiger headers, so we duplicate it here
42#define kWindowUnifiedTitleAndToolbarAttribute (1 << 7)
43
44// ---------------------------------------------------------------------------
45// wxWindowMac utility functions
46// ---------------------------------------------------------------------------
47
48// Find an item given the Macintosh Window Reference
49
50WX_DECLARE_HASH_MAP(WXWindow, wxNonOwnedWindow*, wxPointerHash, wxPointerEqual, MacWindowMap);
51
52static MacWindowMap wxWinMacWindowList;
53
54wxNonOwnedWindow *wxFindWindowFromWXWindow(WXWindow inWindowRef)
55{
56 MacWindowMap::iterator node = wxWinMacWindowList.find(inWindowRef);
57
58 return (node == wxWinMacWindowList.end()) ? NULL : node->second;
59}
60
61void wxAssociateWindowWithWXWindow(WXWindow inWindowRef, wxNonOwnedWindow *win) ;
62void wxAssociateWindowWithWXWindow(WXWindow inWindowRef, wxNonOwnedWindow *win)
63{
64 // adding NULL WindowRef is (first) surely a result of an error and
65 // nothing else :-)
66 wxCHECK_RET( inWindowRef != (WXWindow) NULL, wxT("attempt to add a NULL WindowRef to window list") );
67
68 wxWinMacWindowList[inWindowRef] = win;
69}
70
71void wxRemoveWXWindowAssociation(wxNonOwnedWindow *win) ;
72void wxRemoveWXWindowAssociation(wxNonOwnedWindow *win)
73{
74 MacWindowMap::iterator it;
75 for ( it = wxWinMacWindowList.begin(); it != wxWinMacWindowList.end(); ++it )
76 {
77 if ( it->second == win )
78 {
79 wxWinMacWindowList.erase(it);
80 break;
81 }
82 }
83}
84
85wxNonOwnedWindow* wxNonOwnedWindow::GetFromWXWindow( WXWindow win )
86{
87 return wxFindWindowFromWXWindow( win );
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;
101}
102
103bool wxNonOwnedWindow::Create(wxWindow *parent,
104 wxWindowID id,
105 const wxPoint& pos,
106 const wxSize& size,
107 long style,
108 const wxString& name)
109{
110 // init our fields
111 Init();
112
113 m_windowStyle = style;
114
115 SetName( name );
116
117 m_windowId = id == -1 ? NewControlId() : id;
118 m_windowStyle = style;
119 m_isShown = false;
120
121 // create frame.
122 int x = (int)pos.x;
123 int y = (int)pos.y;
124
125 wxRect display = wxGetClientDisplayRect() ;
126
127 if ( x == wxDefaultPosition.x )
128 x = display.x ;
129
130 if ( y == wxDefaultPosition.y )
131 y = display.y ;
132
133 int w = WidthDefault(size.x);
134 int h = HeightDefault(size.y);
135
524c47aa 136 m_nowpeer = wxNonOwnedWindowImpl::CreateNonOwnedWindow(this, parent, wxPoint(x,y) , wxSize(w,h) , style , GetExtraStyle(), name );
3ccc5735 137 wxAssociateWindowWithWXWindow( m_nowpeer->GetWXWindow() , this ) ;
3ccc5735 138 m_peer = wxWidgetImpl::CreateContentView(this);
3ccc5735
SC
139
140 DoSetWindowVariant( m_windowVariant ) ;
141
142 wxWindowCreateEvent event(this);
143 HandleWindowEvent(event);
144
145 SetBackgroundColour(wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE ));
146
147 if ( parent )
148 parent->AddChild(this);
149
0aaa6ace
SC
150 wxBIND_OR_CONNECT_HACK(this, wxEVT_DESTROY, wxWindowDestroyEventHandler,
151 wxNonOwnedWindow::OnWindowDestroy, this);
152
3ccc5735
SC
153 return true;
154}
155
156wxNonOwnedWindow::~wxNonOwnedWindow()
157{
c6212a0c 158 SendDestroyEvent();
03647350 159
3ccc5735 160 wxRemoveWXWindowAssociation( this ) ;
03647350 161
524c47aa 162 DestroyChildren();
03647350 163
524c47aa 164 delete m_nowpeer;
0aaa6ace 165 m_nowpeer = NULL;
3ccc5735
SC
166
167 // avoid dangling refs
168 if ( s_macDeactivateWindow == this )
169 s_macDeactivateWindow = NULL;
170}
171
0aaa6ace
SC
172void wxNonOwnedWindow::OnWindowDestroy( wxWindowDestroyEvent &event)
173{
174 event.Skip();
175
176 if ( m_nowpeer )
177 m_nowpeer->WillBeDestroyed();
178}
179
3ccc5735
SC
180// ----------------------------------------------------------------------------
181// wxNonOwnedWindow misc
182// ----------------------------------------------------------------------------
183
ab9a0b84
VZ
184bool wxNonOwnedWindow::OSXShowWithEffect(bool show,
185 wxShowEffect effect,
186 unsigned timeout)
03647350 187{
ab9a0b84
VZ
188 // Cocoa code needs to manage window visibility on its own and so calls
189 // wxWindow::Show() as needed but if we already changed the internal
190 // visibility flag here, Show() would do nothing, so avoid doing it
191#if wxOSX_USE_CARBON
192 if ( !wxWindow::Show(show) )
3ccc5735 193 return false;
ab9a0b84 194#endif // Carbon
3ccc5735 195
ab9a0b84
VZ
196 if ( effect == wxSHOW_EFFECT_NONE ||
197 !m_nowpeer || !m_nowpeer->ShowWithEffect(show, effect, timeout) )
198 return Show(show);
3ccc5735 199
ab9a0b84
VZ
200 if ( show )
201 {
202 // as apps expect a size event to occur when the window is shown,
203 // generate one when it is shown with effect too
204 wxSizeEvent event(GetSize(), m_windowId);
205 event.SetEventObject(this);
206 HandleWindowEvent(event);
207 }
3ccc5735 208
ab9a0b84 209 return true;
3ccc5735
SC
210}
211
212wxPoint wxNonOwnedWindow::GetClientAreaOrigin() const
213{
214 int left, top, width, height;
215 m_nowpeer->GetContentArea(left, top, width, height);
216 return wxPoint(left, top);
217}
218
219bool wxNonOwnedWindow::SetBackgroundColour(const wxColour& c )
03647350 220{
3ccc5735
SC
221 if ( !wxWindow::SetBackgroundColour(c) && m_hasBgCol )
222 return false ;
03647350 223
3ccc5735
SC
224 if ( GetBackgroundStyle() != wxBG_STYLE_CUSTOM )
225 {
524c47aa
SC
226 if ( m_nowpeer )
227 return m_nowpeer->SetBackgroundColour(c);
3ccc5735
SC
228 }
229 return true;
03647350 230}
3ccc5735 231
b6dc21e7
KO
232void wxNonOwnedWindow::SetWindowStyleFlag(long flags)
233{
234 if (flags == GetWindowStyleFlag())
235 return;
236
237 wxWindow::SetWindowStyleFlag(flags);
238
239 if (m_nowpeer)
240 m_nowpeer->SetWindowStyleFlag(flags);
241}
242
3ccc5735
SC
243// Raise the window to the top of the Z order
244void wxNonOwnedWindow::Raise()
245{
246 m_nowpeer->Raise();
247}
248
249// Lower the window to the bottom of the Z order
250void wxNonOwnedWindow::Lower()
251{
252 m_nowpeer->Lower();
253}
254
255void wxNonOwnedWindow::HandleActivated( double timestampsec, bool didActivate )
256{
257 MacActivate( (int) (timestampsec * 1000) , didActivate);
258 wxActivateEvent wxevent(wxEVT_ACTIVATE, didActivate , GetId());
259 wxevent.SetTimestamp( (int) (timestampsec * 1000) );
260 wxevent.SetEventObject(this);
261 HandleWindowEvent(wxevent);
262}
263
264void wxNonOwnedWindow::HandleResized( double timestampsec )
265{
266 wxSizeEvent wxevent( GetSize() , GetId());
267 wxevent.SetTimestamp( (int) (timestampsec * 1000) );
268 wxevent.SetEventObject( this );
269 HandleWindowEvent(wxevent);
270 // we have to inform some controls that have to reset things
271 // relative to the toplevel window (e.g. OpenGL buffers)
272 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
273}
274
0faf03bf 275void wxNonOwnedWindow::HandleResizing( double WXUNUSED(timestampsec), wxRect* rect )
3ccc5735
SC
276{
277 wxRect r = *rect ;
278
279 // this is a EVT_SIZING not a EVT_SIZE type !
280 wxSizeEvent wxevent( r , GetId() ) ;
281 wxevent.SetEventObject( this ) ;
282 if ( HandleWindowEvent(wxevent) )
283 r = wxevent.GetRect() ;
284
285 if ( GetMaxWidth() != -1 && r.GetWidth() > GetMaxWidth() )
286 r.SetWidth( GetMaxWidth() ) ;
287 if ( GetMaxHeight() != -1 && r.GetHeight() > GetMaxHeight() )
288 r.SetHeight( GetMaxHeight() ) ;
289 if ( GetMinWidth() != -1 && r.GetWidth() < GetMinWidth() )
290 r.SetWidth( GetMinWidth() ) ;
291 if ( GetMinHeight() != -1 && r.GetHeight() < GetMinHeight() )
292 r.SetHeight( GetMinHeight() ) ;
293
294 *rect = r;
295 // TODO actuall this is too early, in case the window extents are adjusted
296 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
297}
298
299void wxNonOwnedWindow::HandleMoved( double timestampsec )
300{
301 wxMoveEvent wxevent( GetPosition() , GetId());
302 wxevent.SetTimestamp( (int) (timestampsec * 1000) );
303 wxevent.SetEventObject( this );
304 HandleWindowEvent(wxevent);
305}
306
307void wxNonOwnedWindow::MacDelayedDeactivation(long timestamp)
308{
309 if (s_macDeactivateWindow)
310 {
311 wxLogTrace(TRACE_ACTIVATE,
312 wxT("Doing delayed deactivation of %p"),
313 s_macDeactivateWindow);
314
315 s_macDeactivateWindow->MacActivate(timestamp, false);
316 }
317}
318
319void wxNonOwnedWindow::MacActivate( long timestamp , bool WXUNUSED(inIsActivating) )
320{
321 wxLogTrace(TRACE_ACTIVATE, wxT("TopLevel=%p::MacActivate"), this);
322
323 if (s_macDeactivateWindow == this)
324 s_macDeactivateWindow = NULL;
325
326 MacDelayedDeactivation(timestamp);
327}
328
329bool wxNonOwnedWindow::Show(bool show)
330{
331 if ( !wxWindow::Show(show) )
332 return false;
333
334 if ( m_nowpeer )
335 m_nowpeer->Show(show);
03647350 336
3ccc5735
SC
337 if ( show )
338 {
339 // because apps expect a size event to occur at this moment
340 wxSizeEvent event(GetSize() , m_windowId);
341 event.SetEventObject(this);
342 HandleWindowEvent(event);
343 }
03647350 344
3ccc5735
SC
345 return true ;
346}
347
348bool wxNonOwnedWindow::SetTransparent(wxByte alpha)
349{
350 return m_nowpeer->SetTransparent(alpha);
351}
352
353
354bool wxNonOwnedWindow::CanSetTransparent()
355{
356 return m_nowpeer->CanSetTransparent();
357}
358
359
360void wxNonOwnedWindow::SetExtraStyle(long exStyle)
361{
362 if ( GetExtraStyle() == exStyle )
363 return ;
364
365 wxWindow::SetExtraStyle( exStyle ) ;
366
367 if ( m_nowpeer )
368 m_nowpeer->SetExtraStyle(exStyle);
369}
370
371bool wxNonOwnedWindow::SetBackgroundStyle(wxBackgroundStyle style)
372{
373 if ( !wxWindow::SetBackgroundStyle(style) )
374 return false ;
03647350 375
4d949e7f 376 return m_nowpeer ? m_nowpeer->SetBackgroundStyle(style) : true;
3ccc5735
SC
377}
378
379void wxNonOwnedWindow::DoMoveWindow(int x, int y, int width, int height)
380{
b8afff01
SC
381 if ( m_nowpeer == NULL )
382 return;
383
3ccc5735
SC
384 m_cachedClippedRectValid = false ;
385
386 m_nowpeer->MoveWindow(x, y, width, height);
387 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
388}
389
390void wxNonOwnedWindow::DoGetPosition( int *x, int *y ) const
391{
b8afff01
SC
392 if ( m_nowpeer == NULL )
393 return;
394
3ccc5735
SC
395 int x1,y1 ;
396 m_nowpeer->GetPosition(x1, y1);
397
398 if (x)
399 *x = x1 ;
400 if (y)
401 *y = y1 ;
402}
403
404void wxNonOwnedWindow::DoGetSize( int *width, int *height ) const
405{
b8afff01
SC
406 if ( m_nowpeer == NULL )
407 return;
03647350 408
3ccc5735 409 int w,h;
03647350 410
3ccc5735
SC
411 m_nowpeer->GetSize(w, h);
412
413 if (width)
414 *width = w ;
415 if (height)
416 *height = h ;
417}
418
419void wxNonOwnedWindow::DoGetClientSize( int *width, int *height ) const
420{
b8afff01
SC
421 if ( m_nowpeer == NULL )
422 return;
423
3ccc5735 424 int left, top, w, h;
65536c92
SC
425 // perhaps we should do this for all ?
426#ifdef __WXOSX_IPHONE__
427 m_peer->GetContentArea(left, top, w, h);
428#else
3ccc5735 429 m_nowpeer->GetContentArea(left, top, w, h);
65536c92
SC
430#endif
431
3ccc5735
SC
432 if (width)
433 *width = w ;
434 if (height)
435 *height = h ;
436}
437
438
439void wxNonOwnedWindow::Update()
440{
441 m_nowpeer->Update();
442}
443
444WXWindow wxNonOwnedWindow::GetWXWindow() const
445{
446 return m_nowpeer ? m_nowpeer->GetWXWindow() : NULL;
447}
448
449// ---------------------------------------------------------------------------
450// Shape implementation
451// ---------------------------------------------------------------------------
452
453
8e88e984 454bool wxNonOwnedWindow::DoSetShape(const wxRegion& region)
3ccc5735
SC
455{
456 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED), false,
9a83f860 457 wxT("Shaped windows must be created with the wxFRAME_SHAPED style."));
3ccc5735
SC
458
459 // The empty region signifies that the shape
460 // should be removed from the window.
461 if ( region.IsEmpty() )
462 {
463 wxSize sz = GetClientSize();
464 wxRegion rgn(0, 0, sz.x, sz.y);
465 if ( rgn.IsEmpty() )
466 return false ;
467 else
8e88e984 468 return DoSetShape(rgn);
3ccc5735
SC
469 }
470
471 return m_nowpeer->SetShape(region);
472}