]> git.saurik.com Git - wxWidgets.git/blob - src/osx/nonownedwnd_osx.cpp
Safer access of member variables
[wxWidgets.git] / src / osx / nonownedwnd_osx.cpp
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"
16 #include "wx/log.h"
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
39 wxWindow* 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
50 WX_DECLARE_HASH_MAP(WXWindow, wxNonOwnedWindow*, wxPointerHash, wxPointerEqual, MacWindowMap);
51
52 static MacWindowMap wxWinMacWindowList;
53
54 wxNonOwnedWindow *wxFindWindowFromWXWindow(WXWindow inWindowRef)
55 {
56 MacWindowMap::iterator node = wxWinMacWindowList.find(inWindowRef);
57
58 return (node == wxWinMacWindowList.end()) ? NULL : node->second;
59 }
60
61 void wxAssociateWindowWithWXWindow(WXWindow inWindowRef, wxNonOwnedWindow *win) ;
62 void 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
71 void wxRemoveWXWindowAssociation(wxNonOwnedWindow *win) ;
72 void 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
85 wxNonOwnedWindow* wxNonOwnedWindow::GetFromWXWindow( WXWindow win )
86 {
87 return wxFindWindowFromWXWindow( win );
88 }
89
90 // ----------------------------------------------------------------------------
91 // wxNonOwnedWindow creation
92 // ----------------------------------------------------------------------------
93
94 IMPLEMENT_ABSTRACT_CLASS( wxNonOwnedWindowImpl , wxObject )
95
96 wxNonOwnedWindow *wxNonOwnedWindow::s_macDeactivateWindow = NULL;
97
98 void wxNonOwnedWindow::Init()
99 {
100 m_nowpeer = NULL;
101 }
102
103 bool 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
136 m_nowpeer = wxNonOwnedWindowImpl::CreateNonOwnedWindow(this, parent, wxPoint(x,y) , wxSize(w,h) , style , GetExtraStyle(), name );
137 wxAssociateWindowWithWXWindow( m_nowpeer->GetWXWindow() , this ) ;
138 m_peer = wxWidgetImpl::CreateContentView(this);
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
150 return true;
151 }
152
153 wxNonOwnedWindow::~wxNonOwnedWindow()
154 {
155 SendDestroyEvent();
156
157 wxRemoveWXWindowAssociation( this ) ;
158
159 DestroyChildren();
160
161 delete m_nowpeer;
162
163 // avoid dangling refs
164 if ( s_macDeactivateWindow == this )
165 s_macDeactivateWindow = NULL;
166 }
167
168 // ----------------------------------------------------------------------------
169 // wxNonOwnedWindow misc
170 // ----------------------------------------------------------------------------
171
172 bool wxNonOwnedWindow::OSXShowWithEffect(bool show,
173 wxShowEffect effect,
174 unsigned timeout)
175 {
176 // Cocoa code needs to manage window visibility on its own and so calls
177 // wxWindow::Show() as needed but if we already changed the internal
178 // visibility flag here, Show() would do nothing, so avoid doing it
179 #if wxOSX_USE_CARBON
180 if ( !wxWindow::Show(show) )
181 return false;
182 #endif // Carbon
183
184 if ( effect == wxSHOW_EFFECT_NONE ||
185 !m_nowpeer || !m_nowpeer->ShowWithEffect(show, effect, timeout) )
186 return Show(show);
187
188 if ( show )
189 {
190 // as apps expect a size event to occur when the window is shown,
191 // generate one when it is shown with effect too
192 wxSizeEvent event(GetSize(), m_windowId);
193 event.SetEventObject(this);
194 HandleWindowEvent(event);
195 }
196
197 return true;
198 }
199
200 wxPoint wxNonOwnedWindow::GetClientAreaOrigin() const
201 {
202 int left, top, width, height;
203 m_nowpeer->GetContentArea(left, top, width, height);
204 return wxPoint(left, top);
205 }
206
207 bool wxNonOwnedWindow::SetBackgroundColour(const wxColour& c )
208 {
209 if ( !wxWindow::SetBackgroundColour(c) && m_hasBgCol )
210 return false ;
211
212 if ( GetBackgroundStyle() != wxBG_STYLE_CUSTOM )
213 {
214 if ( m_nowpeer )
215 return m_nowpeer->SetBackgroundColour(c);
216 }
217 return true;
218 }
219
220 void wxNonOwnedWindow::SetWindowStyleFlag(long flags)
221 {
222 if (flags == GetWindowStyleFlag())
223 return;
224
225 wxWindow::SetWindowStyleFlag(flags);
226
227 if (m_nowpeer)
228 m_nowpeer->SetWindowStyleFlag(flags);
229 }
230
231 // Raise the window to the top of the Z order
232 void wxNonOwnedWindow::Raise()
233 {
234 m_nowpeer->Raise();
235 }
236
237 // Lower the window to the bottom of the Z order
238 void wxNonOwnedWindow::Lower()
239 {
240 m_nowpeer->Lower();
241 }
242
243 void wxNonOwnedWindow::HandleActivated( double timestampsec, bool didActivate )
244 {
245 MacActivate( (int) (timestampsec * 1000) , didActivate);
246 wxActivateEvent wxevent(wxEVT_ACTIVATE, didActivate , GetId());
247 wxevent.SetTimestamp( (int) (timestampsec * 1000) );
248 wxevent.SetEventObject(this);
249 HandleWindowEvent(wxevent);
250 }
251
252 void wxNonOwnedWindow::HandleResized( double timestampsec )
253 {
254 wxSizeEvent wxevent( GetSize() , GetId());
255 wxevent.SetTimestamp( (int) (timestampsec * 1000) );
256 wxevent.SetEventObject( this );
257 HandleWindowEvent(wxevent);
258 // we have to inform some controls that have to reset things
259 // relative to the toplevel window (e.g. OpenGL buffers)
260 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
261 }
262
263 void wxNonOwnedWindow::HandleResizing( double WXUNUSED(timestampsec), wxRect* rect )
264 {
265 wxRect r = *rect ;
266
267 // this is a EVT_SIZING not a EVT_SIZE type !
268 wxSizeEvent wxevent( r , GetId() ) ;
269 wxevent.SetEventObject( this ) ;
270 if ( HandleWindowEvent(wxevent) )
271 r = wxevent.GetRect() ;
272
273 if ( GetMaxWidth() != -1 && r.GetWidth() > GetMaxWidth() )
274 r.SetWidth( GetMaxWidth() ) ;
275 if ( GetMaxHeight() != -1 && r.GetHeight() > GetMaxHeight() )
276 r.SetHeight( GetMaxHeight() ) ;
277 if ( GetMinWidth() != -1 && r.GetWidth() < GetMinWidth() )
278 r.SetWidth( GetMinWidth() ) ;
279 if ( GetMinHeight() != -1 && r.GetHeight() < GetMinHeight() )
280 r.SetHeight( GetMinHeight() ) ;
281
282 *rect = r;
283 // TODO actuall this is too early, in case the window extents are adjusted
284 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
285 }
286
287 void wxNonOwnedWindow::HandleMoved( double timestampsec )
288 {
289 wxMoveEvent wxevent( GetPosition() , GetId());
290 wxevent.SetTimestamp( (int) (timestampsec * 1000) );
291 wxevent.SetEventObject( this );
292 HandleWindowEvent(wxevent);
293 }
294
295 void wxNonOwnedWindow::MacDelayedDeactivation(long timestamp)
296 {
297 if (s_macDeactivateWindow)
298 {
299 wxLogTrace(TRACE_ACTIVATE,
300 wxT("Doing delayed deactivation of %p"),
301 s_macDeactivateWindow);
302
303 s_macDeactivateWindow->MacActivate(timestamp, false);
304 }
305 }
306
307 void wxNonOwnedWindow::MacActivate( long timestamp , bool WXUNUSED(inIsActivating) )
308 {
309 wxLogTrace(TRACE_ACTIVATE, wxT("TopLevel=%p::MacActivate"), this);
310
311 if (s_macDeactivateWindow == this)
312 s_macDeactivateWindow = NULL;
313
314 MacDelayedDeactivation(timestamp);
315 }
316
317 bool wxNonOwnedWindow::Show(bool show)
318 {
319 if ( !wxWindow::Show(show) )
320 return false;
321
322 if ( m_nowpeer )
323 m_nowpeer->Show(show);
324
325 if ( show )
326 {
327 // because apps expect a size event to occur at this moment
328 wxSizeEvent event(GetSize() , m_windowId);
329 event.SetEventObject(this);
330 HandleWindowEvent(event);
331 }
332
333 return true ;
334 }
335
336 bool wxNonOwnedWindow::SetTransparent(wxByte alpha)
337 {
338 return m_nowpeer->SetTransparent(alpha);
339 }
340
341
342 bool wxNonOwnedWindow::CanSetTransparent()
343 {
344 return m_nowpeer->CanSetTransparent();
345 }
346
347
348 void wxNonOwnedWindow::SetExtraStyle(long exStyle)
349 {
350 if ( GetExtraStyle() == exStyle )
351 return ;
352
353 wxWindow::SetExtraStyle( exStyle ) ;
354
355 if ( m_nowpeer )
356 m_nowpeer->SetExtraStyle(exStyle);
357 }
358
359 bool wxNonOwnedWindow::SetBackgroundStyle(wxBackgroundStyle style)
360 {
361 if ( !wxWindow::SetBackgroundStyle(style) )
362 return false ;
363
364 return m_nowpeer ? m_nowpeer->SetBackgroundStyle(style) : true;
365 }
366
367 void wxNonOwnedWindow::DoMoveWindow(int x, int y, int width, int height)
368 {
369 if ( m_nowpeer == NULL )
370 return;
371
372 m_cachedClippedRectValid = false ;
373
374 m_nowpeer->MoveWindow(x, y, width, height);
375 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
376 }
377
378 void wxNonOwnedWindow::DoGetPosition( int *x, int *y ) const
379 {
380 if ( m_nowpeer == NULL )
381 return;
382
383 int x1,y1 ;
384 m_nowpeer->GetPosition(x1, y1);
385
386 if (x)
387 *x = x1 ;
388 if (y)
389 *y = y1 ;
390 }
391
392 void wxNonOwnedWindow::DoGetSize( int *width, int *height ) const
393 {
394 if ( m_nowpeer == NULL )
395 return;
396
397 int w,h;
398
399 m_nowpeer->GetSize(w, h);
400
401 if (width)
402 *width = w ;
403 if (height)
404 *height = h ;
405 }
406
407 void wxNonOwnedWindow::DoGetClientSize( int *width, int *height ) const
408 {
409 if ( m_nowpeer == NULL )
410 return;
411
412 int left, top, w, h;
413 // perhaps we should do this for all ?
414 #ifdef __WXOSX_IPHONE__
415 m_peer->GetContentArea(left, top, w, h);
416 #else
417 m_nowpeer->GetContentArea(left, top, w, h);
418 #endif
419
420 if (width)
421 *width = w ;
422 if (height)
423 *height = h ;
424 }
425
426
427 void wxNonOwnedWindow::Update()
428 {
429 m_nowpeer->Update();
430 }
431
432 WXWindow wxNonOwnedWindow::GetWXWindow() const
433 {
434 return m_nowpeer ? m_nowpeer->GetWXWindow() : NULL;
435 }
436
437 // ---------------------------------------------------------------------------
438 // Shape implementation
439 // ---------------------------------------------------------------------------
440
441
442 bool wxNonOwnedWindow::DoSetShape(const wxRegion& region)
443 {
444 wxCHECK_MSG( HasFlag(wxFRAME_SHAPED), false,
445 wxT("Shaped windows must be created with the wxFRAME_SHAPED style."));
446
447 // The empty region signifies that the shape
448 // should be removed from the window.
449 if ( region.IsEmpty() )
450 {
451 wxSize sz = GetClientSize();
452 wxRegion rgn(0, 0, sz.x, sz.y);
453 if ( rgn.IsEmpty() )
454 return false ;
455 else
456 return DoSetShape(rgn);
457 }
458
459 return m_nowpeer->SetShape(region);
460 }