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