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