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