| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/common/toplvcmn.cpp |
| 3 | // Purpose: common (for all platforms) wxTopLevelWindow functions |
| 4 | // Author: Julian Smart, Vadim Zeitlin |
| 5 | // Created: 01/02/97 |
| 6 | // Id: $Id$ |
| 7 | // Copyright: (c) 1998 Robert Roebling and Julian Smart |
| 8 | // Licence: wxWindows licence |
| 9 | ///////////////////////////////////////////////////////////////////////////// |
| 10 | |
| 11 | // ============================================================================ |
| 12 | // declarations |
| 13 | // ============================================================================ |
| 14 | |
| 15 | // ---------------------------------------------------------------------------- |
| 16 | // headers |
| 17 | // ---------------------------------------------------------------------------- |
| 18 | |
| 19 | // For compilers that support precompilation, includes "wx.h". |
| 20 | #include "wx/wxprec.h" |
| 21 | |
| 22 | #ifdef __BORLANDC__ |
| 23 | #pragma hdrstop |
| 24 | #endif |
| 25 | |
| 26 | #include "wx/toplevel.h" |
| 27 | |
| 28 | #ifndef WX_PRECOMP |
| 29 | #include "wx/dcclient.h" |
| 30 | #include "wx/app.h" |
| 31 | #endif // WX_PRECOMP |
| 32 | |
| 33 | #include "wx/display.h" |
| 34 | |
| 35 | // ---------------------------------------------------------------------------- |
| 36 | // event table |
| 37 | // ---------------------------------------------------------------------------- |
| 38 | |
| 39 | BEGIN_EVENT_TABLE(wxTopLevelWindowBase, wxWindow) |
| 40 | EVT_CLOSE(wxTopLevelWindowBase::OnCloseWindow) |
| 41 | EVT_SIZE(wxTopLevelWindowBase::OnSize) |
| 42 | END_EVENT_TABLE() |
| 43 | |
| 44 | // ============================================================================ |
| 45 | // implementation |
| 46 | // ============================================================================ |
| 47 | |
| 48 | IMPLEMENT_ABSTRACT_CLASS(wxTopLevelWindow, wxWindow) |
| 49 | |
| 50 | // ---------------------------------------------------------------------------- |
| 51 | // construction/destruction |
| 52 | // ---------------------------------------------------------------------------- |
| 53 | |
| 54 | wxTopLevelWindowBase::wxTopLevelWindowBase() |
| 55 | { |
| 56 | // Unlike windows, top level windows are created hidden by default. |
| 57 | m_isShown = false; |
| 58 | m_winDefault = NULL; |
| 59 | m_winTmpDefault = NULL; |
| 60 | } |
| 61 | |
| 62 | wxTopLevelWindowBase::~wxTopLevelWindowBase() |
| 63 | { |
| 64 | // don't let wxTheApp keep any stale pointers to us |
| 65 | if ( wxTheApp && wxTheApp->GetTopWindow() == this ) |
| 66 | wxTheApp->SetTopWindow(NULL); |
| 67 | |
| 68 | wxTopLevelWindows.DeleteObject(this); |
| 69 | |
| 70 | if ( IsLastBeforeExit() ) |
| 71 | { |
| 72 | // no other (important) windows left, quit the app |
| 73 | wxTheApp->ExitMainLoop(); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | bool wxTopLevelWindowBase::Destroy() |
| 78 | { |
| 79 | // delayed destruction: the frame will be deleted during the next idle |
| 80 | // loop iteration |
| 81 | if ( !wxPendingDelete.Member(this) ) |
| 82 | wxPendingDelete.Append(this); |
| 83 | |
| 84 | if (wxTopLevelWindows.GetCount() > 1) |
| 85 | { |
| 86 | // Hide it immediately. This should |
| 87 | // not be done if this TLW is the |
| 88 | // only one left since we then would |
| 89 | // risk not to get any idle events |
| 90 | // at all anymore during which we |
| 91 | // could delete any pending events. |
| 92 | Hide(); |
| 93 | } |
| 94 | |
| 95 | return true; |
| 96 | } |
| 97 | |
| 98 | bool wxTopLevelWindowBase::IsLastBeforeExit() const |
| 99 | { |
| 100 | // first of all, automatically exiting the app on last window close can be |
| 101 | // completely disabled at wxTheApp level |
| 102 | if ( !wxTheApp || !wxTheApp->GetExitOnFrameDelete() ) |
| 103 | return false; |
| 104 | |
| 105 | wxWindowList::const_iterator i; |
| 106 | const wxWindowList::const_iterator end = wxTopLevelWindows.end(); |
| 107 | |
| 108 | // then decide whether we should exit at all |
| 109 | for ( i = wxTopLevelWindows.begin(); i != end; ++i ) |
| 110 | { |
| 111 | wxTopLevelWindow * const win = wx_static_cast(wxTopLevelWindow *, *i); |
| 112 | if ( win->ShouldPreventAppExit() ) |
| 113 | { |
| 114 | // there remains at least one important TLW, don't exit |
| 115 | return false; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | // if yes, close all the other windows: this could still fail |
| 120 | for ( i = wxTopLevelWindows.begin(); i != end; ++i ) |
| 121 | { |
| 122 | // don't close twice the windows which are already marked for deletion |
| 123 | wxTopLevelWindow * const win = wx_static_cast(wxTopLevelWindow *, *i); |
| 124 | if ( !wxPendingDelete.Member(win) && !win->Close() ) |
| 125 | { |
| 126 | // one of the windows refused to close, don't exit |
| 127 | // |
| 128 | // NB: of course, by now some other windows could have been already |
| 129 | // closed but there is really nothing we can do about it as we |
| 130 | // have no way just to ask the window if it can close without |
| 131 | // forcing it to do it |
| 132 | return false; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | return true; |
| 137 | } |
| 138 | |
| 139 | // ---------------------------------------------------------------------------- |
| 140 | // wxTopLevelWindow geometry |
| 141 | // ---------------------------------------------------------------------------- |
| 142 | |
| 143 | void wxTopLevelWindowBase::SetMinSize(const wxSize& minSize) |
| 144 | { |
| 145 | SetSizeHints( minSize.x, minSize.y, GetMaxWidth(), GetMaxHeight() ); |
| 146 | } |
| 147 | |
| 148 | void wxTopLevelWindowBase::SetMaxSize(const wxSize& maxSize) |
| 149 | { |
| 150 | SetSizeHints( GetMinWidth(), GetMinHeight(), maxSize.x, maxSize.y ); |
| 151 | } |
| 152 | |
| 153 | // set the min/max size of the window |
| 154 | void wxTopLevelWindowBase::DoSetSizeHints(int minW, int minH, |
| 155 | int maxW, int maxH, |
| 156 | int WXUNUSED(incW), int WXUNUSED(incH)) |
| 157 | { |
| 158 | // setting min width greater than max width leads to infinite loops under |
| 159 | // X11 and generally doesn't make any sense, so don't allow it |
| 160 | wxCHECK_RET( (minW == wxDefaultCoord || maxW == wxDefaultCoord || minW <= maxW) && |
| 161 | (minH == wxDefaultCoord || maxH == wxDefaultCoord || minH <= maxH), |
| 162 | _T("min width/height must be less than max width/height!") ); |
| 163 | |
| 164 | m_minWidth = minW; |
| 165 | m_maxWidth = maxW; |
| 166 | m_minHeight = minH; |
| 167 | m_maxHeight = maxH; |
| 168 | } |
| 169 | |
| 170 | void wxTopLevelWindowBase::GetRectForTopLevelChildren(int *x, int *y, int *w, int *h) |
| 171 | { |
| 172 | GetPosition(x,y); |
| 173 | GetSize(w,h); |
| 174 | } |
| 175 | |
| 176 | /* static */ |
| 177 | wxSize wxTopLevelWindowBase::GetDefaultSize() |
| 178 | { |
| 179 | wxSize size = wxGetClientDisplayRect().GetSize(); |
| 180 | |
| 181 | // create proportionally bigger windows on small screens |
| 182 | if ( size.x >= 1024 ) |
| 183 | size.x = 400; |
| 184 | else if ( size.x >= 800 ) |
| 185 | size.x = 300; |
| 186 | else if ( size.x >= 320 ) |
| 187 | size.x = 240; |
| 188 | |
| 189 | if ( size.y >= 768 ) |
| 190 | size.y = 250; |
| 191 | else if ( size.y > 200 ) |
| 192 | { |
| 193 | size.y *= 2; |
| 194 | size.y /= 3; |
| 195 | } |
| 196 | |
| 197 | return size; |
| 198 | } |
| 199 | |
| 200 | void wxTopLevelWindowBase::DoCentre(int dir) |
| 201 | { |
| 202 | // on some platforms centering top level windows is impossible |
| 203 | // because they are always maximized by guidelines or limitations |
| 204 | if(IsAlwaysMaximized()) |
| 205 | return; |
| 206 | |
| 207 | // we need the display rect anyhow so store it first |
| 208 | int nDisplay = wxDisplay::GetFromWindow(this); |
| 209 | wxDisplay dpy(nDisplay == wxNOT_FOUND ? 0 : nDisplay); |
| 210 | const wxRect rectDisplay(dpy.GetClientArea()); |
| 211 | |
| 212 | // what should we centre this window on? |
| 213 | wxRect rectParent; |
| 214 | if ( !(dir & wxCENTRE_ON_SCREEN) && GetParent() ) |
| 215 | { |
| 216 | // centre on parent window: notice that we need screen coordinates for |
| 217 | // positioning this TLW |
| 218 | rectParent = GetParent()->GetScreenRect(); |
| 219 | |
| 220 | // if the parent is entirely off screen (happens at least with MDI |
| 221 | // parent frame under Mac but could happen elsewhere too if the frame |
| 222 | // was hidden/moved away for some reason), don't use it as otherwise |
| 223 | // this window wouldn't be visible at all |
| 224 | if ( !rectDisplay.Contains(rectParent.GetTopLeft()) && |
| 225 | !rectParent.Contains(rectParent.GetBottomRight()) ) |
| 226 | { |
| 227 | // this is enough to make IsEmpty() test below pass |
| 228 | rectParent.width = 0; |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | if ( rectParent.IsEmpty() ) |
| 233 | { |
| 234 | // we were explicitely asked to centre this window on the entire screen |
| 235 | // or if we have no parent anyhow and so can't centre on it |
| 236 | rectParent = rectDisplay; |
| 237 | } |
| 238 | |
| 239 | // centering maximized window on screen is no-op |
| 240 | if((rectParent == rectDisplay) && IsMaximized()) |
| 241 | return; |
| 242 | |
| 243 | // the new window rect candidate |
| 244 | wxRect rect = GetRect().CentreIn(rectParent, dir); |
| 245 | |
| 246 | // we don't want to place the window off screen if Centre() is called as |
| 247 | // this is (almost?) never wanted and it would be very difficult to prevent |
| 248 | // it from happening from the user code if we didn't check for it here |
| 249 | if ( rectDisplay.Contains(rect.GetTopLeft()) ) |
| 250 | { |
| 251 | if ( !rectDisplay.Contains(rect.GetBottomRight()) ) |
| 252 | { |
| 253 | // check if we can move the window so that the bottom right corner |
| 254 | // is visible without hiding the top left one |
| 255 | int dx = rectDisplay.GetRight() - rect.GetRight(); |
| 256 | int dy = rectDisplay.GetBottom() - rect.GetBottom(); |
| 257 | rect.Offset(dx, dy); |
| 258 | } |
| 259 | //else: the window top left and bottom right corner are both visible, |
| 260 | // although the window might still be not entirely on screen (with |
| 261 | // 2 staggered displays for example) we wouldn't be able to |
| 262 | // improve the layout much in such case, so just leave it as is |
| 263 | } |
| 264 | else // make top left corner visible |
| 265 | { |
| 266 | if ( rect.x < rectDisplay.x ) |
| 267 | rect.x = rectDisplay.x; |
| 268 | |
| 269 | if ( rect.y < rectDisplay.y ) |
| 270 | rect.y = rectDisplay.y; |
| 271 | } |
| 272 | |
| 273 | // -1 could be valid coordinate here if there are several displays |
| 274 | SetSize(rect, wxSIZE_ALLOW_MINUS_ONE); |
| 275 | } |
| 276 | |
| 277 | // ---------------------------------------------------------------------------- |
| 278 | // wxTopLevelWindow size management: we exclude the areas taken by |
| 279 | // menu/status/toolbars from the client area, so the client area is what's |
| 280 | // really available for the frame contents |
| 281 | // ---------------------------------------------------------------------------- |
| 282 | |
| 283 | void wxTopLevelWindowBase::DoScreenToClient(int *x, int *y) const |
| 284 | { |
| 285 | wxWindow::DoScreenToClient(x, y); |
| 286 | |
| 287 | // translate the wxWindow client coords to our client coords |
| 288 | wxPoint pt(GetClientAreaOrigin()); |
| 289 | if ( x ) |
| 290 | *x -= pt.x; |
| 291 | if ( y ) |
| 292 | *y -= pt.y; |
| 293 | } |
| 294 | |
| 295 | void wxTopLevelWindowBase::DoClientToScreen(int *x, int *y) const |
| 296 | { |
| 297 | // our client area origin (0, 0) may be really something like (0, 30) for |
| 298 | // wxWindow if we have a toolbar, account for it before translating |
| 299 | wxPoint pt(GetClientAreaOrigin()); |
| 300 | if ( x ) |
| 301 | *x += pt.x; |
| 302 | if ( y ) |
| 303 | *y += pt.y; |
| 304 | |
| 305 | wxWindow::DoClientToScreen(x, y); |
| 306 | } |
| 307 | |
| 308 | bool wxTopLevelWindowBase::IsAlwaysMaximized() const |
| 309 | { |
| 310 | #if defined(__SMARTPHONE__) || defined(__POCKETPC__) |
| 311 | return true; |
| 312 | #else |
| 313 | return false; |
| 314 | #endif |
| 315 | } |
| 316 | |
| 317 | // ---------------------------------------------------------------------------- |
| 318 | // event handlers |
| 319 | // ---------------------------------------------------------------------------- |
| 320 | |
| 321 | // default resizing behaviour - if only ONE subwindow, resize to fill the |
| 322 | // whole client area |
| 323 | void wxTopLevelWindowBase::DoLayout() |
| 324 | { |
| 325 | // if we're using constraints or sizers - do use them |
| 326 | if ( GetAutoLayout() ) |
| 327 | { |
| 328 | Layout(); |
| 329 | } |
| 330 | else |
| 331 | { |
| 332 | // do we have _exactly_ one child? |
| 333 | wxWindow *child = (wxWindow *)NULL; |
| 334 | for ( wxWindowList::compatibility_iterator node = GetChildren().GetFirst(); |
| 335 | node; |
| 336 | node = node->GetNext() ) |
| 337 | { |
| 338 | wxWindow *win = node->GetData(); |
| 339 | |
| 340 | // exclude top level and managed windows (status bar isn't |
| 341 | // currently in the children list except under wxMac anyhow, but |
| 342 | // it makes no harm to test for it) |
| 343 | if ( !win->IsTopLevel() && !IsOneOfBars(win) ) |
| 344 | { |
| 345 | if ( child ) |
| 346 | { |
| 347 | return; // it's our second subwindow - nothing to do |
| 348 | } |
| 349 | |
| 350 | child = win; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | // do we have any children at all? |
| 355 | if ( child && child->IsShown() ) |
| 356 | { |
| 357 | // exactly one child - set it's size to fill the whole frame |
| 358 | int clientW, clientH; |
| 359 | DoGetClientSize(&clientW, &clientH); |
| 360 | |
| 361 | child->SetSize(0, 0, clientW, clientH); |
| 362 | } |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | // The default implementation for the close window event. |
| 367 | void wxTopLevelWindowBase::OnCloseWindow(wxCloseEvent& WXUNUSED(event)) |
| 368 | { |
| 369 | Destroy(); |
| 370 | } |
| 371 | |
| 372 | bool wxTopLevelWindowBase::SendIconizeEvent(bool iconized) |
| 373 | { |
| 374 | wxIconizeEvent event(GetId(), iconized); |
| 375 | event.SetEventObject(this); |
| 376 | |
| 377 | return GetEventHandler()->ProcessEvent(event); |
| 378 | } |
| 379 | |
| 380 | // do the window-specific processing after processing the update event |
| 381 | void wxTopLevelWindowBase::DoUpdateWindowUI(wxUpdateUIEvent& event) |
| 382 | { |
| 383 | // call inherited, but skip the wxControl's version, and call directly the |
| 384 | // wxWindow's one instead, because the only reason why we are overriding this |
| 385 | // function is that we want to use SetTitle() instead of wxControl::SetLabel() |
| 386 | wxWindowBase::DoUpdateWindowUI(event); |
| 387 | |
| 388 | // update title |
| 389 | if ( event.GetSetText() ) |
| 390 | { |
| 391 | if ( event.GetText() != GetTitle() ) |
| 392 | SetTitle(event.GetText()); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | void wxTopLevelWindowBase::RequestUserAttention(int WXUNUSED(flags)) |
| 397 | { |
| 398 | // it's probably better than do nothing, isn't it? |
| 399 | Raise(); |
| 400 | } |
| 401 | |
| 402 | void wxTopLevelWindowBase::RemoveChild(wxWindowBase *child) |
| 403 | { |
| 404 | if ( child == m_winDefault ) |
| 405 | m_winDefault = NULL; |
| 406 | |
| 407 | if ( child == m_winTmpDefault ) |
| 408 | m_winTmpDefault = NULL; |
| 409 | |
| 410 | wxWindow::RemoveChild(child); |
| 411 | } |