| 1 | ///////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: src/common/framecmn.cpp |
| 3 | // Purpose: common (for all platforms) wxFrame 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/frame.h" |
| 27 | |
| 28 | #ifndef WX_PRECOMP |
| 29 | #include "wx/menu.h" |
| 30 | #include "wx/menuitem.h" |
| 31 | #include "wx/dcclient.h" |
| 32 | #include "wx/toolbar.h" |
| 33 | #include "wx/statusbr.h" |
| 34 | #endif // WX_PRECOMP |
| 35 | |
| 36 | // ---------------------------------------------------------------------------- |
| 37 | // event table |
| 38 | // ---------------------------------------------------------------------------- |
| 39 | |
| 40 | #if wxUSE_MENUS && wxUSE_STATUSBAR |
| 41 | |
| 42 | BEGIN_EVENT_TABLE(wxFrameBase, wxTopLevelWindow) |
| 43 | EVT_MENU_OPEN(wxFrameBase::OnMenuOpen) |
| 44 | EVT_MENU_CLOSE(wxFrameBase::OnMenuClose) |
| 45 | |
| 46 | EVT_MENU_HIGHLIGHT_ALL(wxFrameBase::OnMenuHighlight) |
| 47 | END_EVENT_TABLE() |
| 48 | |
| 49 | #endif // wxUSE_MENUS && wxUSE_STATUSBAR |
| 50 | |
| 51 | // ============================================================================ |
| 52 | // implementation |
| 53 | // ============================================================================ |
| 54 | |
| 55 | // ---------------------------------------------------------------------------- |
| 56 | // construction/destruction |
| 57 | // ---------------------------------------------------------------------------- |
| 58 | |
| 59 | wxFrameBase::wxFrameBase() |
| 60 | { |
| 61 | #if wxUSE_MENUS |
| 62 | m_frameMenuBar = NULL; |
| 63 | #endif // wxUSE_MENUS |
| 64 | |
| 65 | #if wxUSE_TOOLBAR |
| 66 | m_frameToolBar = NULL; |
| 67 | #endif // wxUSE_TOOLBAR |
| 68 | |
| 69 | #if wxUSE_STATUSBAR |
| 70 | m_frameStatusBar = NULL; |
| 71 | #endif // wxUSE_STATUSBAR |
| 72 | |
| 73 | m_statusBarPane = 0; |
| 74 | } |
| 75 | |
| 76 | wxFrameBase::~wxFrameBase() |
| 77 | { |
| 78 | // this destructor is required for Darwin |
| 79 | } |
| 80 | |
| 81 | wxFrame *wxFrameBase::New(wxWindow *parent, |
| 82 | wxWindowID id, |
| 83 | const wxString& title, |
| 84 | const wxPoint& pos, |
| 85 | const wxSize& size, |
| 86 | long style, |
| 87 | const wxString& name) |
| 88 | { |
| 89 | return new wxFrame(parent, id, title, pos, size, style, name); |
| 90 | } |
| 91 | |
| 92 | void wxFrameBase::DeleteAllBars() |
| 93 | { |
| 94 | #if wxUSE_MENUS |
| 95 | if ( m_frameMenuBar ) |
| 96 | { |
| 97 | delete m_frameMenuBar; |
| 98 | m_frameMenuBar = (wxMenuBar *) NULL; |
| 99 | } |
| 100 | #endif // wxUSE_MENUS |
| 101 | |
| 102 | #if wxUSE_STATUSBAR |
| 103 | if ( m_frameStatusBar ) |
| 104 | { |
| 105 | delete m_frameStatusBar; |
| 106 | m_frameStatusBar = (wxStatusBar *) NULL; |
| 107 | } |
| 108 | #endif // wxUSE_STATUSBAR |
| 109 | |
| 110 | #if wxUSE_TOOLBAR |
| 111 | if ( m_frameToolBar ) |
| 112 | { |
| 113 | delete m_frameToolBar; |
| 114 | m_frameToolBar = (wxToolBar *) NULL; |
| 115 | } |
| 116 | #endif // wxUSE_TOOLBAR |
| 117 | } |
| 118 | |
| 119 | bool wxFrameBase::IsOneOfBars(const wxWindow *win) const |
| 120 | { |
| 121 | #if wxUSE_MENUS |
| 122 | if ( win == GetMenuBar() ) |
| 123 | return true; |
| 124 | #endif // wxUSE_MENUS |
| 125 | |
| 126 | #if wxUSE_STATUSBAR |
| 127 | if ( win == GetStatusBar() ) |
| 128 | return true; |
| 129 | #endif // wxUSE_STATUSBAR |
| 130 | |
| 131 | #if wxUSE_TOOLBAR |
| 132 | if ( win == GetToolBar() ) |
| 133 | return true; |
| 134 | #endif // wxUSE_TOOLBAR |
| 135 | |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | // ---------------------------------------------------------------------------- |
| 140 | // wxFrame size management: we exclude the areas taken by menu/status/toolbars |
| 141 | // from the client area, so the client area is what's really available for the |
| 142 | // frame contents |
| 143 | // ---------------------------------------------------------------------------- |
| 144 | |
| 145 | // get the origin of the client area in the client coordinates |
| 146 | wxPoint wxFrameBase::GetClientAreaOrigin() const |
| 147 | { |
| 148 | wxPoint pt = wxTopLevelWindow::GetClientAreaOrigin(); |
| 149 | |
| 150 | #if wxUSE_TOOLBAR && !defined(__WXUNIVERSAL__) |
| 151 | wxToolBar *toolbar = GetToolBar(); |
| 152 | if ( toolbar && toolbar->IsShown() ) |
| 153 | { |
| 154 | int w, h; |
| 155 | toolbar->GetSize(&w, &h); |
| 156 | |
| 157 | if ( toolbar->GetWindowStyleFlag() & wxTB_VERTICAL ) |
| 158 | { |
| 159 | pt.x += w; |
| 160 | } |
| 161 | else |
| 162 | { |
| 163 | pt.y += h; |
| 164 | } |
| 165 | } |
| 166 | #endif // wxUSE_TOOLBAR |
| 167 | |
| 168 | return pt; |
| 169 | } |
| 170 | |
| 171 | |
| 172 | void wxFrameBase::SendSizeEvent() |
| 173 | { |
| 174 | wxSizeEvent event( GetSize(), GetId() ); |
| 175 | event.SetEventObject( this ); |
| 176 | GetEventHandler()->AddPendingEvent( event ); |
| 177 | } |
| 178 | |
| 179 | |
| 180 | // ---------------------------------------------------------------------------- |
| 181 | // misc |
| 182 | // ---------------------------------------------------------------------------- |
| 183 | |
| 184 | bool wxFrameBase::ProcessCommand(int id) |
| 185 | { |
| 186 | #if wxUSE_MENUS |
| 187 | wxMenuBar *bar = GetMenuBar(); |
| 188 | if ( !bar ) |
| 189 | return false; |
| 190 | |
| 191 | wxCommandEvent commandEvent(wxEVT_COMMAND_MENU_SELECTED, id); |
| 192 | commandEvent.SetEventObject(this); |
| 193 | |
| 194 | wxMenuItem *item = bar->FindItem(id); |
| 195 | if (item) |
| 196 | { |
| 197 | if (!item->IsEnabled()) |
| 198 | return true; |
| 199 | |
| 200 | if ((item->GetKind() == wxITEM_RADIO) && item->IsChecked() ) |
| 201 | return true; |
| 202 | |
| 203 | if (item->IsCheckable()) |
| 204 | { |
| 205 | item->Toggle(); |
| 206 | |
| 207 | // use the new value |
| 208 | commandEvent.SetInt(item->IsChecked()); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | GetEventHandler()->ProcessEvent(commandEvent); |
| 213 | return true; |
| 214 | #else // !wxUSE_MENUS |
| 215 | return false; |
| 216 | #endif // wxUSE_MENUS/!wxUSE_MENUS |
| 217 | } |
| 218 | |
| 219 | // Do the UI update processing for this window. This is |
| 220 | // provided for the application to call if it wants to |
| 221 | // force a UI update, particularly for the menus and toolbar. |
| 222 | void wxFrameBase::UpdateWindowUI(long flags) |
| 223 | { |
| 224 | wxWindowBase::UpdateWindowUI(flags); |
| 225 | |
| 226 | #if wxUSE_TOOLBAR |
| 227 | if (GetToolBar()) |
| 228 | GetToolBar()->UpdateWindowUI(flags); |
| 229 | #endif |
| 230 | |
| 231 | #if wxUSE_MENUS |
| 232 | if (GetMenuBar()) |
| 233 | { |
| 234 | if ((flags & wxUPDATE_UI_FROMIDLE) && !wxUSE_IDLEMENUUPDATES) |
| 235 | { |
| 236 | // If coming from an idle event, we only |
| 237 | // want to update the menus if we're |
| 238 | // in the wxUSE_IDLEMENUUPDATES configuration: |
| 239 | // so if we're not, do nothing |
| 240 | } |
| 241 | else |
| 242 | DoMenuUpdates(); |
| 243 | } |
| 244 | #endif // wxUSE_MENUS |
| 245 | } |
| 246 | |
| 247 | // ---------------------------------------------------------------------------- |
| 248 | // event handlers for status bar updates from menus |
| 249 | // ---------------------------------------------------------------------------- |
| 250 | |
| 251 | #if wxUSE_MENUS && wxUSE_STATUSBAR |
| 252 | |
| 253 | void wxFrameBase::OnMenuHighlight(wxMenuEvent& event) |
| 254 | { |
| 255 | #if wxUSE_STATUSBAR |
| 256 | (void)ShowMenuHelp(GetStatusBar(), event.GetMenuId()); |
| 257 | #endif // wxUSE_STATUSBAR |
| 258 | } |
| 259 | |
| 260 | #if !wxUSE_IDLEMENUUPDATES |
| 261 | void wxFrameBase::OnMenuOpen(wxMenuEvent& event) |
| 262 | #else |
| 263 | void wxFrameBase::OnMenuOpen(wxMenuEvent& WXUNUSED(event)) |
| 264 | #endif |
| 265 | { |
| 266 | #if !wxUSE_IDLEMENUUPDATES |
| 267 | DoMenuUpdates(event.GetMenu()); |
| 268 | #endif // !wxUSE_IDLEMENUUPDATES |
| 269 | } |
| 270 | |
| 271 | void wxFrameBase::OnMenuClose(wxMenuEvent& WXUNUSED(event)) |
| 272 | { |
| 273 | DoGiveHelp(wxEmptyString, false); |
| 274 | } |
| 275 | |
| 276 | #endif // wxUSE_MENUS && wxUSE_STATUSBAR |
| 277 | |
| 278 | // Implement internal behaviour (menu updating on some platforms) |
| 279 | void wxFrameBase::OnInternalIdle() |
| 280 | { |
| 281 | wxTopLevelWindow::OnInternalIdle(); |
| 282 | |
| 283 | #if wxUSE_MENUS && wxUSE_IDLEMENUUPDATES |
| 284 | if (wxUpdateUIEvent::CanUpdate(this)) |
| 285 | DoMenuUpdates(); |
| 286 | #endif |
| 287 | } |
| 288 | |
| 289 | // ---------------------------------------------------------------------------- |
| 290 | // status bar stuff |
| 291 | // ---------------------------------------------------------------------------- |
| 292 | |
| 293 | #if wxUSE_STATUSBAR |
| 294 | |
| 295 | wxStatusBar* wxFrameBase::CreateStatusBar(int number, |
| 296 | long style, |
| 297 | wxWindowID id, |
| 298 | const wxString& name) |
| 299 | { |
| 300 | // the main status bar can only be created once (or else it should be |
| 301 | // deleted before calling CreateStatusBar() again) |
| 302 | wxCHECK_MSG( !m_frameStatusBar, (wxStatusBar *)NULL, |
| 303 | wxT("recreating status bar in wxFrame") ); |
| 304 | |
| 305 | SetStatusBar(OnCreateStatusBar(number, style, id, name)); |
| 306 | |
| 307 | return m_frameStatusBar; |
| 308 | } |
| 309 | |
| 310 | wxStatusBar *wxFrameBase::OnCreateStatusBar(int number, |
| 311 | long style, |
| 312 | wxWindowID id, |
| 313 | const wxString& name) |
| 314 | { |
| 315 | wxStatusBar *statusBar = new wxStatusBar(this, id, style, name); |
| 316 | |
| 317 | statusBar->SetFieldsCount(number); |
| 318 | |
| 319 | return statusBar; |
| 320 | } |
| 321 | |
| 322 | void wxFrameBase::SetStatusText(const wxString& text, int number) |
| 323 | { |
| 324 | wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") ); |
| 325 | |
| 326 | m_frameStatusBar->SetStatusText(text, number); |
| 327 | } |
| 328 | |
| 329 | void wxFrameBase::SetStatusWidths(int n, const int widths_field[] ) |
| 330 | { |
| 331 | wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set widths for") ); |
| 332 | |
| 333 | m_frameStatusBar->SetStatusWidths(n, widths_field); |
| 334 | |
| 335 | PositionStatusBar(); |
| 336 | } |
| 337 | |
| 338 | void wxFrameBase::PushStatusText(const wxString& text, int number) |
| 339 | { |
| 340 | wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") ); |
| 341 | |
| 342 | m_frameStatusBar->PushStatusText(text, number); |
| 343 | } |
| 344 | |
| 345 | void wxFrameBase::PopStatusText(int number) |
| 346 | { |
| 347 | wxCHECK_RET( m_frameStatusBar != NULL, wxT("no statusbar to set text for") ); |
| 348 | |
| 349 | m_frameStatusBar->PopStatusText(number); |
| 350 | } |
| 351 | |
| 352 | bool wxFrameBase::ShowMenuHelp(wxStatusBar *WXUNUSED(statbar), int menuId) |
| 353 | { |
| 354 | #if wxUSE_MENUS |
| 355 | // if no help string found, we will clear the status bar text |
| 356 | wxString helpString; |
| 357 | if ( menuId != wxID_SEPARATOR && menuId != -3 /* wxID_TITLE */ ) |
| 358 | { |
| 359 | wxMenuBar *menuBar = GetMenuBar(); |
| 360 | if ( menuBar ) |
| 361 | { |
| 362 | // it's ok if we don't find the item because it might belong |
| 363 | // to the popup menu |
| 364 | wxMenuItem *item = menuBar->FindItem(menuId); |
| 365 | if ( item ) |
| 366 | helpString = item->GetHelp(); |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | DoGiveHelp(helpString, true); |
| 371 | |
| 372 | return !helpString.empty(); |
| 373 | #else // !wxUSE_MENUS |
| 374 | return false; |
| 375 | #endif // wxUSE_MENUS/!wxUSE_MENUS |
| 376 | } |
| 377 | |
| 378 | void wxFrameBase::SetStatusBar(wxStatusBar *statBar) |
| 379 | { |
| 380 | bool hadBar = m_frameStatusBar != NULL; |
| 381 | m_frameStatusBar = statBar; |
| 382 | |
| 383 | if ( (m_frameStatusBar != NULL) != hadBar ) |
| 384 | { |
| 385 | PositionStatusBar(); |
| 386 | |
| 387 | DoLayout(); |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | #endif // wxUSE_STATUSBAR |
| 392 | |
| 393 | #if wxUSE_MENUS || wxUSE_TOOLBAR |
| 394 | void wxFrameBase::DoGiveHelp(const wxString& help, bool show) |
| 395 | { |
| 396 | #if wxUSE_STATUSBAR |
| 397 | if ( m_statusBarPane < 0 ) |
| 398 | { |
| 399 | // status bar messages disabled |
| 400 | return; |
| 401 | } |
| 402 | |
| 403 | wxStatusBar *statbar = GetStatusBar(); |
| 404 | if ( !statbar ) |
| 405 | return; |
| 406 | |
| 407 | wxString text; |
| 408 | if ( show ) |
| 409 | { |
| 410 | // remember the old status bar text if this is the first time we're |
| 411 | // called since the menu has been opened as we're going to overwrite it |
| 412 | // in our DoGiveHelp() and we want to restore it when the menu is |
| 413 | // closed |
| 414 | // |
| 415 | // note that it would be logical to do this in OnMenuOpen() but under |
| 416 | // MSW we get an EVT_MENU_HIGHLIGHT before EVT_MENU_OPEN, strangely |
| 417 | // enough, and so this doesn't work and instead we use the ugly trick |
| 418 | // with using special m_oldStatusText value as "menu opened" (but it is |
| 419 | // arguably better than adding yet another member variable to wxFrame |
| 420 | // on all platforms) |
| 421 | if ( m_oldStatusText.empty() ) |
| 422 | { |
| 423 | m_oldStatusText = statbar->GetStatusText(m_statusBarPane); |
| 424 | if ( m_oldStatusText.empty() ) |
| 425 | { |
| 426 | // use special value to prevent us from doing this the next time |
| 427 | m_oldStatusText += _T('\0'); |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | text = help; |
| 432 | } |
| 433 | else // hide help, restore the original text |
| 434 | { |
| 435 | text = m_oldStatusText; |
| 436 | m_oldStatusText.clear(); |
| 437 | } |
| 438 | |
| 439 | statbar->SetStatusText(text, m_statusBarPane); |
| 440 | #else |
| 441 | wxUnusedVar(text); |
| 442 | wxUnusedVar(show); |
| 443 | #endif // wxUSE_STATUSBAR |
| 444 | } |
| 445 | #endif // wxUSE_MENUS || wxUSE_TOOLBAR |
| 446 | |
| 447 | |
| 448 | // ---------------------------------------------------------------------------- |
| 449 | // toolbar stuff |
| 450 | // ---------------------------------------------------------------------------- |
| 451 | |
| 452 | #if wxUSE_TOOLBAR |
| 453 | |
| 454 | wxToolBar* wxFrameBase::CreateToolBar(long style, |
| 455 | wxWindowID id, |
| 456 | const wxString& name) |
| 457 | { |
| 458 | // the main toolbar can't be recreated (unless it was explicitly deleted |
| 459 | // before) |
| 460 | wxCHECK_MSG( !m_frameToolBar, (wxToolBar *)NULL, |
| 461 | wxT("recreating toolbar in wxFrame") ); |
| 462 | |
| 463 | if ( style == -1 ) |
| 464 | { |
| 465 | // use default style |
| 466 | // |
| 467 | // NB: we don't specify the default value in the method declaration |
| 468 | // because |
| 469 | // a) this allows us to have different defaults for different |
| 470 | // platforms (even if we don't have them right now) |
| 471 | // b) we don't need to include wx/toolbar.h in the header then |
| 472 | style = wxBORDER_NONE | wxTB_HORIZONTAL | wxTB_FLAT; |
| 473 | } |
| 474 | |
| 475 | SetToolBar(OnCreateToolBar(style, id, name)); |
| 476 | |
| 477 | return m_frameToolBar; |
| 478 | } |
| 479 | |
| 480 | wxToolBar* wxFrameBase::OnCreateToolBar(long style, |
| 481 | wxWindowID id, |
| 482 | const wxString& name) |
| 483 | { |
| 484 | #if defined(__WXWINCE__) && defined(__POCKETPC__) |
| 485 | return new wxToolMenuBar(this, id, |
| 486 | wxDefaultPosition, wxDefaultSize, |
| 487 | style, name); |
| 488 | #else |
| 489 | return new wxToolBar(this, id, |
| 490 | wxDefaultPosition, wxDefaultSize, |
| 491 | style, name); |
| 492 | #endif |
| 493 | } |
| 494 | |
| 495 | void wxFrameBase::SetToolBar(wxToolBar *toolbar) |
| 496 | { |
| 497 | bool hadBar = m_frameToolBar != NULL; |
| 498 | m_frameToolBar = toolbar; |
| 499 | |
| 500 | if ( (m_frameToolBar != NULL) != hadBar ) |
| 501 | { |
| 502 | PositionToolBar(); |
| 503 | |
| 504 | DoLayout(); |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | #endif // wxUSE_TOOLBAR |
| 509 | |
| 510 | // ---------------------------------------------------------------------------- |
| 511 | // menus |
| 512 | // ---------------------------------------------------------------------------- |
| 513 | |
| 514 | #if wxUSE_MENUS |
| 515 | |
| 516 | // update all menus |
| 517 | void wxFrameBase::DoMenuUpdates(wxMenu* menu) |
| 518 | { |
| 519 | if (menu) |
| 520 | { |
| 521 | wxEvtHandler* source = GetEventHandler(); |
| 522 | menu->UpdateUI(source); |
| 523 | } |
| 524 | else |
| 525 | { |
| 526 | wxMenuBar* bar = GetMenuBar(); |
| 527 | if (bar != NULL) |
| 528 | bar->UpdateMenus(); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | void wxFrameBase::DetachMenuBar() |
| 533 | { |
| 534 | if ( m_frameMenuBar ) |
| 535 | { |
| 536 | m_frameMenuBar->Detach(); |
| 537 | m_frameMenuBar = NULL; |
| 538 | } |
| 539 | } |
| 540 | |
| 541 | void wxFrameBase::AttachMenuBar(wxMenuBar *menubar) |
| 542 | { |
| 543 | if ( menubar ) |
| 544 | { |
| 545 | menubar->Attach((wxFrame *)this); |
| 546 | m_frameMenuBar = menubar; |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | void wxFrameBase::SetMenuBar(wxMenuBar *menubar) |
| 551 | { |
| 552 | if ( menubar == GetMenuBar() ) |
| 553 | { |
| 554 | // nothing to do |
| 555 | return; |
| 556 | } |
| 557 | |
| 558 | DetachMenuBar(); |
| 559 | |
| 560 | this->AttachMenuBar(menubar); |
| 561 | } |
| 562 | |
| 563 | #endif // wxUSE_MENUS |