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