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