]> git.saurik.com Git - wxWidgets.git/blob - src/osx/nonownedwnd_osx.cpp
Make GTK callbacks passed to GTKConnectWidget() extern "C".
[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/dcmemory.h"
17 #include "wx/log.h"
18 #endif // WX_PRECOMP
19
20 #include "wx/hashmap.h"
21 #include "wx/evtloop.h"
22 #include "wx/tooltip.h"
23 #include "wx/nonownedwnd.h"
24
25 #include "wx/osx/private.h"
26 #include "wx/settings.h"
27 #include "wx/frame.h"
28
29 #if wxUSE_SYSTEM_OPTIONS
30 #include "wx/sysopt.h"
31 #endif
32
33 // ----------------------------------------------------------------------------
34 // constants
35 // ----------------------------------------------------------------------------
36
37 // trace mask for activation tracing messages
38 #define TRACE_ACTIVATE "activation"
39
40 wxWindow* g_MacLastWindow = NULL ;
41
42 // unified title and toolbar constant - not in Tiger headers, so we duplicate it here
43 #define kWindowUnifiedTitleAndToolbarAttribute (1 << 7)
44
45 // ---------------------------------------------------------------------------
46 // wxWindowMac utility functions
47 // ---------------------------------------------------------------------------
48
49 WX_DECLARE_HASH_MAP(WXWindow, wxNonOwnedWindowImpl*, wxPointerHash, wxPointerEqual, MacWindowMap);
50
51 static MacWindowMap wxWinMacWindowList;
52
53 wxNonOwnedWindow* wxNonOwnedWindow::GetFromWXWindow( WXWindow win )
54 {
55 wxNonOwnedWindowImpl* impl = wxNonOwnedWindowImpl::FindFromWXWindow(win);
56
57 return ( impl != NULL ? impl->GetWXPeer() : NULL ) ;
58 }
59
60 wxNonOwnedWindowImpl* wxNonOwnedWindowImpl::FindFromWXWindow (WXWindow window)
61 {
62 MacWindowMap::iterator node = wxWinMacWindowList.find(window);
63
64 return (node == wxWinMacWindowList.end()) ? NULL : node->second;
65 }
66
67 void wxNonOwnedWindowImpl::RemoveAssociations( wxNonOwnedWindowImpl* impl)
68 {
69 MacWindowMap::iterator it;
70 for ( it = wxWinMacWindowList.begin(); it != wxWinMacWindowList.end(); ++it )
71 {
72 if ( it->second == impl )
73 {
74 wxWinMacWindowList.erase(it);
75 break;
76 }
77 }
78 }
79
80 void wxNonOwnedWindowImpl::Associate( WXWindow window, wxNonOwnedWindowImpl *impl )
81 {
82 // adding NULL WindowRef is (first) surely a result of an error and
83 // nothing else :-)
84 wxCHECK_RET( window != (WXWindow) NULL, wxT("attempt to add a NULL WindowRef to window list") );
85
86 wxWinMacWindowList[window] = impl;
87 }
88
89 // ----------------------------------------------------------------------------
90 // wxNonOwnedWindow creation
91 // ----------------------------------------------------------------------------
92
93 IMPLEMENT_ABSTRACT_CLASS( wxNonOwnedWindowImpl , wxObject )
94
95 wxNonOwnedWindow *wxNonOwnedWindow::s_macDeactivateWindow = NULL;
96
97 void wxNonOwnedWindow::Init()
98 {
99 m_nowpeer = NULL;
100 m_isNativeWindowWrapper = false;
101 }
102
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 SetBackgroundColour(wxSystemSettings::GetColour( wxSYS_COLOUR_3DFACE ));
156
157 if ( parent )
158 parent->AddChild(this);
159
160 return true;
161 }
162
163 bool wxNonOwnedWindow::Create(wxWindow *parent, WXWindow nativeWindow)
164 {
165 if ( parent )
166 parent->AddChild(this);
167
168 SubclassWin(nativeWindow);
169
170 return true;
171 }
172
173 void wxNonOwnedWindow::SubclassWin(WXWindow nativeWindow)
174 {
175 wxASSERT_MSG( !m_isNativeWindowWrapper, wxT("subclassing window twice?") );
176 wxASSERT_MSG( m_nowpeer == NULL, wxT("window already was created") );
177
178 m_nowpeer = wxNonOwnedWindowImpl::CreateNonOwnedWindow(this, GetParent(), nativeWindow );
179 m_isNativeWindowWrapper = true;
180 wxNonOwnedWindowImpl::Associate( m_nowpeer->GetWXWindow() , m_nowpeer ) ;
181 SetPeer(wxWidgetImpl::CreateContentView(this));
182 }
183
184 void wxNonOwnedWindow::UnsubclassWin()
185 {
186 wxASSERT_MSG( m_isNativeWindowWrapper, wxT("window was not subclassed") );
187
188 if ( GetParent() )
189 GetParent()->RemoveChild(this);
190
191 wxNonOwnedWindowImpl::RemoveAssociations(m_nowpeer) ;
192 wxDELETE(m_nowpeer);
193 SetPeer(NULL);
194 m_isNativeWindowWrapper = false;
195 }
196
197
198 wxNonOwnedWindow::~wxNonOwnedWindow()
199 {
200 SendDestroyEvent();
201
202 wxNonOwnedWindowImpl::RemoveAssociations(m_nowpeer) ;
203
204 DestroyChildren();
205
206 wxDELETE(m_nowpeer);
207
208 // avoid dangling refs
209 if ( s_macDeactivateWindow == this )
210 s_macDeactivateWindow = NULL;
211 }
212
213 bool wxNonOwnedWindow::Destroy()
214 {
215 WillBeDestroyed();
216
217 return wxWindow::Destroy();
218 }
219
220 void wxNonOwnedWindow::WillBeDestroyed()
221 {
222 if ( m_nowpeer )
223 m_nowpeer->WillBeDestroyed();
224 }
225
226 // ----------------------------------------------------------------------------
227 // wxNonOwnedWindow misc
228 // ----------------------------------------------------------------------------
229
230 bool wxNonOwnedWindow::OSXShowWithEffect(bool show,
231 wxShowEffect effect,
232 unsigned timeout)
233 {
234 // Cocoa code needs to manage window visibility on its own and so calls
235 // wxWindow::Show() as needed but if we already changed the internal
236 // visibility flag here, Show() would do nothing, so avoid doing it
237 #if wxOSX_USE_CARBON
238 if ( !wxWindow::Show(show) )
239 return false;
240 #endif // Carbon
241
242 if ( effect == wxSHOW_EFFECT_NONE ||
243 !m_nowpeer || !m_nowpeer->ShowWithEffect(show, effect, timeout) )
244 return Show(show);
245
246 if ( show )
247 {
248 // as apps expect a size event to occur when the window is shown,
249 // generate one when it is shown with effect too
250 wxSizeEvent event(GetSize(), m_windowId);
251 event.SetEventObject(this);
252 HandleWindowEvent(event);
253 }
254
255 return true;
256 }
257
258 wxPoint wxNonOwnedWindow::GetClientAreaOrigin() const
259 {
260 int left, top, width, height;
261 m_nowpeer->GetContentArea(left, top, width, height);
262 return wxPoint(left, top);
263 }
264
265 bool wxNonOwnedWindow::SetBackgroundColour(const wxColour& c )
266 {
267 if ( !wxWindow::SetBackgroundColour(c) && m_hasBgCol )
268 return false ;
269
270 if ( GetBackgroundStyle() != wxBG_STYLE_CUSTOM )
271 {
272 if ( m_nowpeer )
273 return m_nowpeer->SetBackgroundColour(c);
274 }
275 return true;
276 }
277
278 void wxNonOwnedWindow::SetWindowStyleFlag(long flags)
279 {
280 if (flags == GetWindowStyleFlag())
281 return;
282
283 wxWindow::SetWindowStyleFlag(flags);
284
285 if (m_nowpeer)
286 m_nowpeer->SetWindowStyleFlag(flags);
287 }
288
289 // Raise the window to the top of the Z order
290 void wxNonOwnedWindow::Raise()
291 {
292 m_nowpeer->Raise();
293 }
294
295 // Lower the window to the bottom of the Z order
296 void wxNonOwnedWindow::Lower()
297 {
298 m_nowpeer->Lower();
299 }
300
301 void wxNonOwnedWindow::HandleActivated( double timestampsec, bool didActivate )
302 {
303 MacActivate( (int) (timestampsec * 1000) , didActivate);
304 wxActivateEvent wxevent(wxEVT_ACTIVATE, didActivate , GetId());
305 wxevent.SetTimestamp( (int) (timestampsec * 1000) );
306 wxevent.SetEventObject(this);
307 HandleWindowEvent(wxevent);
308 }
309
310 void wxNonOwnedWindow::HandleResized( double timestampsec )
311 {
312 wxSizeEvent wxevent( GetSize() , GetId());
313 wxevent.SetTimestamp( (int) (timestampsec * 1000) );
314 wxevent.SetEventObject( this );
315 HandleWindowEvent(wxevent);
316 // we have to inform some controls that have to reset things
317 // relative to the toplevel window (e.g. OpenGL buffers)
318 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
319 }
320
321 void wxNonOwnedWindow::HandleResizing( double WXUNUSED(timestampsec), wxRect* rect )
322 {
323 wxRect r = *rect ;
324
325 // this is a EVT_SIZING not a EVT_SIZE type !
326 wxSizeEvent wxevent( r , GetId() ) ;
327 wxevent.SetEventObject( this ) ;
328 if ( HandleWindowEvent(wxevent) )
329 r = wxevent.GetRect() ;
330
331 if ( GetMaxWidth() != -1 && r.GetWidth() > GetMaxWidth() )
332 r.SetWidth( GetMaxWidth() ) ;
333 if ( GetMaxHeight() != -1 && r.GetHeight() > GetMaxHeight() )
334 r.SetHeight( GetMaxHeight() ) ;
335 if ( GetMinWidth() != -1 && r.GetWidth() < GetMinWidth() )
336 r.SetWidth( GetMinWidth() ) ;
337 if ( GetMinHeight() != -1 && r.GetHeight() < GetMinHeight() )
338 r.SetHeight( GetMinHeight() ) ;
339
340 *rect = r;
341 // TODO actuall this is too early, in case the window extents are adjusted
342 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
343 }
344
345 void wxNonOwnedWindow::HandleMoved( double timestampsec )
346 {
347 wxMoveEvent wxevent( GetPosition() , GetId());
348 wxevent.SetTimestamp( (int) (timestampsec * 1000) );
349 wxevent.SetEventObject( this );
350 HandleWindowEvent(wxevent);
351 }
352
353 void wxNonOwnedWindow::MacDelayedDeactivation(long timestamp)
354 {
355 if (s_macDeactivateWindow)
356 {
357 wxLogTrace(TRACE_ACTIVATE,
358 wxT("Doing delayed deactivation of %p"),
359 s_macDeactivateWindow);
360
361 s_macDeactivateWindow->MacActivate(timestamp, false);
362 }
363 }
364
365 void wxNonOwnedWindow::MacActivate( long timestamp , bool WXUNUSED(inIsActivating) )
366 {
367 wxLogTrace(TRACE_ACTIVATE, wxT("TopLevel=%p::MacActivate"), this);
368
369 if (s_macDeactivateWindow == this)
370 s_macDeactivateWindow = NULL;
371
372 MacDelayedDeactivation(timestamp);
373 }
374
375 bool wxNonOwnedWindow::Show(bool show)
376 {
377 if ( !wxWindow::Show(show) )
378 return false;
379
380 if ( m_nowpeer )
381 m_nowpeer->Show(show);
382
383 if ( show )
384 {
385 // because apps expect a size event to occur at this moment
386 wxSizeEvent event(GetSize() , m_windowId);
387 event.SetEventObject(this);
388 HandleWindowEvent(event);
389 }
390
391 return true ;
392 }
393
394 bool wxNonOwnedWindow::SetTransparent(wxByte alpha)
395 {
396 return m_nowpeer->SetTransparent(alpha);
397 }
398
399
400 bool wxNonOwnedWindow::CanSetTransparent()
401 {
402 return m_nowpeer->CanSetTransparent();
403 }
404
405
406 void wxNonOwnedWindow::SetExtraStyle(long exStyle)
407 {
408 if ( GetExtraStyle() == exStyle )
409 return ;
410
411 wxWindow::SetExtraStyle( exStyle ) ;
412
413 if ( m_nowpeer )
414 m_nowpeer->SetExtraStyle(exStyle);
415 }
416
417 bool wxNonOwnedWindow::SetBackgroundStyle(wxBackgroundStyle style)
418 {
419 if ( !wxWindow::SetBackgroundStyle(style) )
420 return false ;
421
422 return m_nowpeer ? m_nowpeer->SetBackgroundStyle(style) : true;
423 }
424
425 void wxNonOwnedWindow::DoMoveWindow(int x, int y, int width, int height)
426 {
427 if ( m_nowpeer == NULL )
428 return;
429
430 m_cachedClippedRectValid = false ;
431
432 m_nowpeer->MoveWindow(x, y, width, height);
433 wxWindowMac::MacSuperChangedPosition() ; // like this only children will be notified
434 }
435
436 void wxNonOwnedWindow::DoGetPosition( int *x, int *y ) const
437 {
438 if ( m_nowpeer == NULL )
439 return;
440
441 int x1,y1 ;
442 m_nowpeer->GetPosition(x1, y1);
443
444 if (x)
445 *x = x1 ;
446 if (y)
447 *y = y1 ;
448 }
449
450 void wxNonOwnedWindow::DoGetSize( int *width, int *height ) const
451 {
452 if ( m_nowpeer == NULL )
453 return;
454
455 int w,h;
456
457 m_nowpeer->GetSize(w, h);
458
459 if (width)
460 *width = w ;
461 if (height)
462 *height = h ;
463 }
464
465 void wxNonOwnedWindow::DoGetClientSize( int *width, int *height ) const
466 {
467 if ( m_nowpeer == NULL )
468 return;
469
470 int left, top, w, h;
471 // under iphone with a translucent status bar the m_nowpeer returns the
472 // inner area, while the content area extends under the translucent
473 // status bar, therefore we use the content view's area
474 #ifdef __WXOSX_IPHONE__
475 GetPeer()->GetContentArea(left, top, w, h);
476 #else
477 m_nowpeer->GetContentArea(left, top, w, h);
478 #endif
479
480 if (width)
481 *width = w ;
482 if (height)
483 *height = h ;
484 }
485
486
487 void wxNonOwnedWindow::Update()
488 {
489 m_nowpeer->Update();
490 }
491
492 WXWindow wxNonOwnedWindow::GetWXWindow() const
493 {
494 return m_nowpeer ? m_nowpeer->GetWXWindow() : NULL;
495 }
496
497 // ---------------------------------------------------------------------------
498 // Shape implementation
499 // ---------------------------------------------------------------------------
500
501 bool wxNonOwnedWindow::DoClearShape()
502 {
503 m_shape.Clear();
504
505 wxSize sz = GetClientSize();
506 wxRegion region(0, 0, sz.x, sz.y);
507
508 return m_nowpeer->SetShape(region);
509 }
510
511 bool wxNonOwnedWindow::DoSetRegionShape(const wxRegion& region)
512 {
513 m_shape = region;
514
515 return m_nowpeer->SetShape(region);
516 }
517
518 #if wxUSE_GRAPHICS_CONTEXT
519
520 #include "wx/scopedptr.h"
521
522 bool wxNonOwnedWindow::DoSetPathShape(const wxGraphicsPath& path)
523 {
524 m_shapePath = path;
525
526 // Convert the path to wxRegion by rendering the path on a window-sized
527 // bitmap, creating a mask from it and finally creating the region from
528 // this mask.
529 wxBitmap bmp(GetSize());
530
531 {
532 wxMemoryDC dc(bmp);
533 dc.SetBackground(*wxBLACK);
534 dc.Clear();
535
536 wxScopedPtr<wxGraphicsContext> context(wxGraphicsContext::Create(dc));
537 context->SetBrush(*wxWHITE);
538 context->FillPath(m_shapePath);
539 }
540
541 bmp.SetMask(new wxMask(bmp, *wxBLACK));
542
543 return DoSetRegionShape(wxRegion(bmp));
544 }
545
546 #endif // wxUSE_GRAPHICS_CONTEXT