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