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