| 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 | delete m_nowpeer; |
| 171 | m_nowpeer = NULL; |
| 172 | |
| 173 | // avoid dangling refs |
| 174 | if ( s_macDeactivateWindow == this ) |
| 175 | s_macDeactivateWindow = NULL; |
| 176 | } |
| 177 | |
| 178 | bool wxNonOwnedWindow::Destroy() |
| 179 | { |
| 180 | WillBeDestroyed(); |
| 181 | |
| 182 | return wxWindow::Destroy(); |
| 183 | } |
| 184 | |
| 185 | void wxNonOwnedWindow::WillBeDestroyed() |
| 186 | { |
| 187 | if ( m_nowpeer ) |
| 188 | m_nowpeer->WillBeDestroyed(); |
| 189 | } |
| 190 | |
| 191 | // ---------------------------------------------------------------------------- |
| 192 | // wxNonOwnedWindow misc |
| 193 | // ---------------------------------------------------------------------------- |
| 194 | |
| 195 | bool wxNonOwnedWindow::OSXShowWithEffect(bool show, |
| 196 | wxShowEffect effect, |
| 197 | unsigned timeout) |
| 198 | { |
| 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) ) |
| 204 | return false; |
| 205 | #endif // Carbon |
| 206 | |
| 207 | if ( effect == wxSHOW_EFFECT_NONE || |
| 208 | !m_nowpeer || !m_nowpeer->ShowWithEffect(show, effect, timeout) ) |
| 209 | return Show(show); |
| 210 | |
| 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 | } |
| 219 | |
| 220 | return true; |
| 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 ) |
| 231 | { |
| 232 | if ( !wxWindow::SetBackgroundColour(c) && m_hasBgCol ) |
| 233 | return false ; |
| 234 | |
| 235 | if ( GetBackgroundStyle() != wxBG_STYLE_CUSTOM ) |
| 236 | { |
| 237 | if ( m_nowpeer ) |
| 238 | return m_nowpeer->SetBackgroundColour(c); |
| 239 | } |
| 240 | return true; |
| 241 | } |
| 242 | |
| 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 | |
| 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 | |
| 286 | void wxNonOwnedWindow::HandleResizing( double WXUNUSED(timestampsec), wxRect* rect ) |
| 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); |
| 347 | |
| 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 | } |
| 355 | |
| 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 ; |
| 386 | |
| 387 | return m_nowpeer ? m_nowpeer->SetBackgroundStyle(style) : true; |
| 388 | } |
| 389 | |
| 390 | void wxNonOwnedWindow::DoMoveWindow(int x, int y, int width, int height) |
| 391 | { |
| 392 | if ( m_nowpeer == NULL ) |
| 393 | return; |
| 394 | |
| 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 | { |
| 403 | if ( m_nowpeer == NULL ) |
| 404 | return; |
| 405 | |
| 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 | { |
| 417 | if ( m_nowpeer == NULL ) |
| 418 | return; |
| 419 | |
| 420 | int w,h; |
| 421 | |
| 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 | { |
| 432 | if ( m_nowpeer == NULL ) |
| 433 | return; |
| 434 | |
| 435 | int left, top, w, h; |
| 436 | m_nowpeer->GetContentArea(left, top, w, h); |
| 437 | |
| 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 | |
| 460 | bool wxNonOwnedWindow::DoSetShape(const wxRegion& region) |
| 461 | { |
| 462 | wxCHECK_MSG( HasFlag(wxFRAME_SHAPED), false, |
| 463 | wxT("Shaped windows must be created with the wxFRAME_SHAPED style.")); |
| 464 | |
| 465 | m_shape = region; |
| 466 | |
| 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 |
| 476 | return DoSetShape(rgn); |
| 477 | } |
| 478 | |
| 479 | return m_nowpeer->SetShape(region); |
| 480 | } |