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