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