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