| 1 | /////////////////////////////////////////////////////////////////////////////// |
| 2 | // Name: common/menucmn.cpp |
| 3 | // Purpose: wxMenu and wxMenuBar methods common to all ports |
| 4 | // Author: Vadim Zeitlin |
| 5 | // Modified by: |
| 6 | // Created: 26.10.99 |
| 7 | // RCS-ID: $Id$ |
| 8 | // Copyright: (c) wxWindows team |
| 9 | // Licence: wxWindows license |
| 10 | /////////////////////////////////////////////////////////////////////////////// |
| 11 | |
| 12 | // ============================================================================ |
| 13 | // declarations |
| 14 | // ============================================================================ |
| 15 | |
| 16 | // ---------------------------------------------------------------------------- |
| 17 | // headers |
| 18 | // ---------------------------------------------------------------------------- |
| 19 | |
| 20 | #ifdef __GNUG__ |
| 21 | #pragma implementation "menubase.h" |
| 22 | #endif |
| 23 | |
| 24 | // For compilers that support precompilation, includes "wx.h". |
| 25 | #include "wx/wxprec.h" |
| 26 | |
| 27 | #ifdef __BORLANDC__ |
| 28 | #pragma hdrstop |
| 29 | #endif |
| 30 | |
| 31 | #if wxUSE_MENUS |
| 32 | |
| 33 | #include <ctype.h> |
| 34 | |
| 35 | #ifndef WX_PRECOMP |
| 36 | #include "wx/intl.h" |
| 37 | #include "wx/log.h" |
| 38 | #include "wx/menu.h" |
| 39 | #endif |
| 40 | |
| 41 | // ---------------------------------------------------------------------------- |
| 42 | // template lists |
| 43 | // ---------------------------------------------------------------------------- |
| 44 | |
| 45 | #include "wx/listimpl.cpp" |
| 46 | |
| 47 | WX_DEFINE_LIST(wxMenuList); |
| 48 | WX_DEFINE_LIST(wxMenuItemList); |
| 49 | |
| 50 | // ============================================================================ |
| 51 | // implementation |
| 52 | // ============================================================================ |
| 53 | |
| 54 | // ---------------------------------------------------------------------------- |
| 55 | // wxMenuItem |
| 56 | // ---------------------------------------------------------------------------- |
| 57 | |
| 58 | wxMenuItemBase::~wxMenuItemBase() |
| 59 | { |
| 60 | delete m_subMenu; |
| 61 | } |
| 62 | |
| 63 | #if wxUSE_ACCEL |
| 64 | |
| 65 | // return wxAcceleratorEntry for the given menu string or NULL if none |
| 66 | // specified |
| 67 | wxAcceleratorEntry *wxGetAccelFromString(const wxString& label) |
| 68 | { |
| 69 | // check for accelerators: they are given after '\t' |
| 70 | int posTab = label.Find(wxT('\t')); |
| 71 | if ( posTab != wxNOT_FOUND ) { |
| 72 | // parse the accelerator string |
| 73 | int keyCode = 0; |
| 74 | int accelFlags = wxACCEL_NORMAL; |
| 75 | wxString current; |
| 76 | for ( size_t n = (size_t)posTab + 1; n < label.Len(); n++ ) { |
| 77 | if ( (label[n] == '+') || (label[n] == '-') ) { |
| 78 | if ( current == _("ctrl") ) |
| 79 | accelFlags |= wxACCEL_CTRL; |
| 80 | else if ( current == _("alt") ) |
| 81 | accelFlags |= wxACCEL_ALT; |
| 82 | else if ( current == _("shift") ) |
| 83 | accelFlags |= wxACCEL_SHIFT; |
| 84 | else { |
| 85 | wxLogDebug(wxT("Unknown accel modifier: '%s'"), |
| 86 | current.c_str()); |
| 87 | } |
| 88 | |
| 89 | current.Empty(); |
| 90 | } |
| 91 | else { |
| 92 | current += wxTolower(label[n]); |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | if ( current.IsEmpty() ) { |
| 97 | wxLogDebug(wxT("No accel key found, accel string ignored.")); |
| 98 | } |
| 99 | else { |
| 100 | if ( current.Len() == 1 ) { |
| 101 | // it's a letter |
| 102 | keyCode = wxToupper(current[0U]); |
| 103 | } |
| 104 | else { |
| 105 | // is it a function key? |
| 106 | if ( current[0U] == 'f' && isdigit(current[1U]) && |
| 107 | (current.Len() == 2 || |
| 108 | (current.Len() == 3 && isdigit(current[2U]))) ) { |
| 109 | int n; |
| 110 | wxSscanf(current.c_str() + 1, wxT("%d"), &n); |
| 111 | |
| 112 | keyCode = WXK_F1 + n - 1; |
| 113 | } |
| 114 | else { |
| 115 | // several special cases |
| 116 | current.MakeUpper(); |
| 117 | if ( current == wxT("DEL") ) { |
| 118 | keyCode = WXK_DELETE; |
| 119 | } |
| 120 | else if ( current == wxT("DELETE") ) { |
| 121 | keyCode = WXK_DELETE; |
| 122 | } |
| 123 | else if ( current == wxT("INS") ) { |
| 124 | keyCode = WXK_INSERT; |
| 125 | } |
| 126 | else if ( current == wxT("INSERT") ) { |
| 127 | keyCode = WXK_INSERT; |
| 128 | } |
| 129 | #if 0 |
| 130 | else if ( current == wxT("PGUP") ) { |
| 131 | keyCode = VK_PRIOR; |
| 132 | } |
| 133 | else if ( current == wxT("PGDN") ) { |
| 134 | keyCode = VK_NEXT; |
| 135 | } |
| 136 | #endif |
| 137 | else |
| 138 | { |
| 139 | wxLogDebug(wxT("Unrecognized accel key '%s', accel string ignored."), |
| 140 | current.c_str()); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | if ( keyCode ) { |
| 147 | // we do have something |
| 148 | return new wxAcceleratorEntry(accelFlags, keyCode); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | return (wxAcceleratorEntry *)NULL; |
| 153 | } |
| 154 | |
| 155 | wxAcceleratorEntry *wxMenuItemBase::GetAccel() const |
| 156 | { |
| 157 | return wxGetAccelFromString(GetText()); |
| 158 | } |
| 159 | |
| 160 | void wxMenuItemBase::SetAccel(wxAcceleratorEntry *accel) |
| 161 | { |
| 162 | wxString text = m_text.BeforeFirst(wxT('\t')); |
| 163 | if ( accel ) |
| 164 | { |
| 165 | text += wxT('\t'); |
| 166 | |
| 167 | int flags = accel->GetFlags(); |
| 168 | if ( flags & wxACCEL_ALT ) |
| 169 | text += wxT("Alt-"); |
| 170 | if ( flags & wxACCEL_CTRL ) |
| 171 | text += wxT("Ctrl-"); |
| 172 | if ( flags & wxACCEL_SHIFT ) |
| 173 | text += wxT("Shift-"); |
| 174 | |
| 175 | int code = accel->GetKeyCode(); |
| 176 | switch ( code ) |
| 177 | { |
| 178 | case WXK_F1: |
| 179 | case WXK_F2: |
| 180 | case WXK_F3: |
| 181 | case WXK_F4: |
| 182 | case WXK_F5: |
| 183 | case WXK_F6: |
| 184 | case WXK_F7: |
| 185 | case WXK_F8: |
| 186 | case WXK_F9: |
| 187 | case WXK_F10: |
| 188 | case WXK_F11: |
| 189 | case WXK_F12: |
| 190 | text << wxT('F') << code - WXK_F1 + 1; |
| 191 | break; |
| 192 | |
| 193 | // if there are any other keys wxGetAccelFromString() may return, |
| 194 | // we should process them here |
| 195 | |
| 196 | default: |
| 197 | if ( wxIsalnum(code) ) |
| 198 | { |
| 199 | text << (wxChar)code; |
| 200 | |
| 201 | break; |
| 202 | } |
| 203 | |
| 204 | wxFAIL_MSG( wxT("unknown keyboard accel") ); |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | SetText(text); |
| 209 | } |
| 210 | |
| 211 | #endif // wxUSE_ACCEL |
| 212 | |
| 213 | // ---------------------------------------------------------------------------- |
| 214 | // wxMenu ctor and dtor |
| 215 | // ---------------------------------------------------------------------------- |
| 216 | |
| 217 | void wxMenuBase::Init(long style) |
| 218 | { |
| 219 | m_items.DeleteContents(TRUE); |
| 220 | |
| 221 | m_menuBar = (wxMenuBar *)NULL; |
| 222 | m_menuParent = (wxMenu *)NULL; |
| 223 | |
| 224 | m_invokingWindow = (wxWindow *)NULL; |
| 225 | m_style = style; |
| 226 | m_clientData = (void *)NULL; |
| 227 | m_eventHandler = this; |
| 228 | |
| 229 | #if wxUSE_MENU_CALLBACK |
| 230 | m_callback = (wxFunction) NULL; |
| 231 | #endif // wxUSE_MENU_CALLBACK |
| 232 | } |
| 233 | |
| 234 | wxMenuBase::~wxMenuBase() |
| 235 | { |
| 236 | // nothing to do, wxMenuItemList dtor will delete the menu items. |
| 237 | |
| 238 | // Actually, in GTK, the submenus have to get deleted first. |
| 239 | } |
| 240 | |
| 241 | // ---------------------------------------------------------------------------- |
| 242 | // wxMenu item adding/removing |
| 243 | // ---------------------------------------------------------------------------- |
| 244 | |
| 245 | void wxMenuBase::AddSubMenu(wxMenu *submenu) |
| 246 | { |
| 247 | wxCHECK_RET( submenu, _T("can't add a NULL submenu") ); |
| 248 | |
| 249 | if ( m_menuBar ) |
| 250 | { |
| 251 | submenu->Attach(m_menuBar); |
| 252 | } |
| 253 | |
| 254 | submenu->SetParent((wxMenu *)this); |
| 255 | } |
| 256 | |
| 257 | bool wxMenuBase::DoAppend(wxMenuItem *item) |
| 258 | { |
| 259 | wxCHECK_MSG( item, FALSE, wxT("invalid item in wxMenu::Append()") ); |
| 260 | |
| 261 | m_items.Append(item); |
| 262 | if ( item->IsSubMenu() ) |
| 263 | { |
| 264 | AddSubMenu(item->GetSubMenu()); |
| 265 | } |
| 266 | |
| 267 | return TRUE; |
| 268 | } |
| 269 | |
| 270 | bool wxMenuBase::Insert(size_t pos, wxMenuItem *item) |
| 271 | { |
| 272 | wxCHECK_MSG( item, FALSE, wxT("invalid item in wxMenu::Insert") ); |
| 273 | |
| 274 | if ( pos == GetMenuItemCount() ) |
| 275 | { |
| 276 | return DoAppend(item); |
| 277 | } |
| 278 | else |
| 279 | { |
| 280 | wxCHECK_MSG( pos < GetMenuItemCount(), FALSE, |
| 281 | wxT("invalid index in wxMenu::Insert") ); |
| 282 | |
| 283 | return DoInsert(pos, item); |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | bool wxMenuBase::DoInsert(size_t pos, wxMenuItem *item) |
| 288 | { |
| 289 | wxCHECK_MSG( item, FALSE, wxT("invalid item in wxMenu::Insert()") ); |
| 290 | |
| 291 | wxMenuItemList::Node *node = m_items.Item(pos); |
| 292 | wxCHECK_MSG( node, FALSE, wxT("invalid index in wxMenu::Insert()") ); |
| 293 | |
| 294 | m_items.Insert(node, item); |
| 295 | if ( item->IsSubMenu() ) |
| 296 | { |
| 297 | AddSubMenu(item->GetSubMenu()); |
| 298 | } |
| 299 | |
| 300 | return TRUE; |
| 301 | } |
| 302 | |
| 303 | wxMenuItem *wxMenuBase::Remove(wxMenuItem *item) |
| 304 | { |
| 305 | wxCHECK_MSG( item, NULL, wxT("invalid item in wxMenu::Remove") ); |
| 306 | |
| 307 | return DoRemove(item); |
| 308 | } |
| 309 | |
| 310 | wxMenuItem *wxMenuBase::DoRemove(wxMenuItem *item) |
| 311 | { |
| 312 | wxMenuItemList::Node *node = m_items.Find(item); |
| 313 | |
| 314 | // if we get here, the item is valid or one of Remove() functions is broken |
| 315 | wxCHECK_MSG( node, NULL, wxT("bug in wxMenu::Remove logic") ); |
| 316 | |
| 317 | // we detach the item, but we do delete the list node (i.e. don't call |
| 318 | // DetachNode() here!) |
| 319 | node->SetData((wxMenuItem *)NULL); // to prevent it from deleting the item |
| 320 | m_items.DeleteNode(node); |
| 321 | |
| 322 | // item isn't attached to anything any more |
| 323 | wxMenu *submenu = item->GetSubMenu(); |
| 324 | if ( submenu ) |
| 325 | { |
| 326 | submenu->SetParent((wxMenu *)NULL); |
| 327 | } |
| 328 | |
| 329 | return item; |
| 330 | } |
| 331 | |
| 332 | bool wxMenuBase::Delete(wxMenuItem *item) |
| 333 | { |
| 334 | wxCHECK_MSG( item, FALSE, wxT("invalid item in wxMenu::Delete") ); |
| 335 | |
| 336 | return DoDelete(item); |
| 337 | } |
| 338 | |
| 339 | bool wxMenuBase::DoDelete(wxMenuItem *item) |
| 340 | { |
| 341 | wxMenuItem *item2 = DoRemove(item); |
| 342 | wxCHECK_MSG( item2, FALSE, wxT("failed to delete menu item") ); |
| 343 | |
| 344 | // don't delete the submenu |
| 345 | item2->SetSubMenu((wxMenu *)NULL); |
| 346 | |
| 347 | delete item2; |
| 348 | |
| 349 | return TRUE; |
| 350 | } |
| 351 | |
| 352 | bool wxMenuBase::Destroy(wxMenuItem *item) |
| 353 | { |
| 354 | wxCHECK_MSG( item, FALSE, wxT("invalid item in wxMenu::Destroy") ); |
| 355 | |
| 356 | return DoDestroy(item); |
| 357 | } |
| 358 | |
| 359 | bool wxMenuBase::DoDestroy(wxMenuItem *item) |
| 360 | { |
| 361 | wxMenuItem *item2 = DoRemove(item); |
| 362 | wxCHECK_MSG( item2, FALSE, wxT("failed to delete menu item") ); |
| 363 | |
| 364 | delete item2; |
| 365 | |
| 366 | return TRUE; |
| 367 | } |
| 368 | |
| 369 | // ---------------------------------------------------------------------------- |
| 370 | // wxMenu searching for items |
| 371 | // ---------------------------------------------------------------------------- |
| 372 | |
| 373 | // Finds the item id matching the given string, -1 if not found. |
| 374 | int wxMenuBase::FindItem(const wxString& text) const |
| 375 | { |
| 376 | wxString label = wxMenuItem::GetLabelFromText(text); |
| 377 | for ( wxMenuItemList::Node *node = m_items.GetFirst(); |
| 378 | node; |
| 379 | node = node->GetNext() ) |
| 380 | { |
| 381 | wxMenuItem *item = node->GetData(); |
| 382 | if ( item->IsSubMenu() ) |
| 383 | { |
| 384 | int rc = item->GetSubMenu()->FindItem(label); |
| 385 | if ( rc != wxNOT_FOUND ) |
| 386 | return rc; |
| 387 | } |
| 388 | |
| 389 | // we execute this code for submenus as well to alllow finding them by |
| 390 | // name just like the ordinary items |
| 391 | if ( !item->IsSeparator() ) |
| 392 | { |
| 393 | if ( item->GetLabel() == label ) |
| 394 | return item->GetId(); |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | return wxNOT_FOUND; |
| 399 | } |
| 400 | |
| 401 | // recursive search for item by id |
| 402 | wxMenuItem *wxMenuBase::FindItem(int itemId, wxMenu **itemMenu) const |
| 403 | { |
| 404 | if ( itemMenu ) |
| 405 | *itemMenu = NULL; |
| 406 | |
| 407 | wxMenuItem *item = NULL; |
| 408 | for ( wxMenuItemList::Node *node = m_items.GetFirst(); |
| 409 | node && !item; |
| 410 | node = node->GetNext() ) |
| 411 | { |
| 412 | item = node->GetData(); |
| 413 | |
| 414 | if ( item->GetId() == itemId ) |
| 415 | { |
| 416 | if ( itemMenu ) |
| 417 | *itemMenu = (wxMenu *)this; |
| 418 | } |
| 419 | else if ( item->IsSubMenu() ) |
| 420 | { |
| 421 | item = item->GetSubMenu()->FindItem(itemId, itemMenu); |
| 422 | } |
| 423 | else |
| 424 | { |
| 425 | // don't exit the loop |
| 426 | item = NULL; |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | return item; |
| 431 | } |
| 432 | |
| 433 | // non recursive search |
| 434 | wxMenuItem *wxMenuBase::FindChildItem(int id, size_t *ppos) const |
| 435 | { |
| 436 | wxMenuItem *item = (wxMenuItem *)NULL; |
| 437 | wxMenuItemList::Node *node = GetMenuItems().GetFirst(); |
| 438 | |
| 439 | size_t pos; |
| 440 | for ( pos = 0; node; pos++ ) |
| 441 | { |
| 442 | if ( node->GetData()->GetId() == id ) |
| 443 | { |
| 444 | item = node->GetData(); |
| 445 | |
| 446 | break; |
| 447 | } |
| 448 | |
| 449 | node = node->GetNext(); |
| 450 | } |
| 451 | |
| 452 | if ( ppos ) |
| 453 | { |
| 454 | *ppos = item ? pos : (size_t)wxNOT_FOUND; |
| 455 | } |
| 456 | |
| 457 | return item; |
| 458 | } |
| 459 | |
| 460 | // ---------------------------------------------------------------------------- |
| 461 | // wxMenu helpers used by derived classes |
| 462 | // ---------------------------------------------------------------------------- |
| 463 | |
| 464 | // Update a menu and all submenus recursively. source is the object that has |
| 465 | // the update event handlers defined for it. If NULL, the menu or associated |
| 466 | // window will be used. |
| 467 | void wxMenuBase::UpdateUI(wxEvtHandler* source) |
| 468 | { |
| 469 | if ( !source && GetInvokingWindow() ) |
| 470 | source = GetInvokingWindow()->GetEventHandler(); |
| 471 | if ( !source ) |
| 472 | source = GetEventHandler(); |
| 473 | if ( !source ) |
| 474 | source = this; |
| 475 | |
| 476 | wxMenuItemList::Node* node = GetMenuItems().GetFirst(); |
| 477 | while ( node ) |
| 478 | { |
| 479 | wxMenuItem* item = node->GetData(); |
| 480 | if ( !item->IsSeparator() ) |
| 481 | { |
| 482 | wxWindowID id = item->GetId(); |
| 483 | wxUpdateUIEvent event(id); |
| 484 | event.SetEventObject( source ); |
| 485 | |
| 486 | if ( source->ProcessEvent(event) ) |
| 487 | { |
| 488 | // if anything changed, update the chanegd attribute |
| 489 | if (event.GetSetText()) |
| 490 | SetLabel(id, event.GetText()); |
| 491 | if (event.GetSetChecked()) |
| 492 | Check(id, event.GetChecked()); |
| 493 | if (event.GetSetEnabled()) |
| 494 | Enable(id, event.GetEnabled()); |
| 495 | } |
| 496 | |
| 497 | // recurse to the submenus |
| 498 | if ( item->GetSubMenu() ) |
| 499 | item->GetSubMenu()->UpdateUI(source); |
| 500 | } |
| 501 | //else: item is a separator (which don't process update UI events) |
| 502 | |
| 503 | node = node->GetNext(); |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | bool wxMenuBase::SendEvent(int id, int checked) |
| 508 | { |
| 509 | wxCommandEvent event(wxEVT_COMMAND_MENU_SELECTED, id); |
| 510 | event.SetEventObject(this); |
| 511 | event.SetInt(checked); |
| 512 | |
| 513 | bool processed = FALSE; |
| 514 | |
| 515 | #if wxUSE_MENU_CALLBACK |
| 516 | // Try a callback |
| 517 | if (m_callback) |
| 518 | { |
| 519 | (void)(*(m_callback))(*this, event); |
| 520 | processed = TRUE; |
| 521 | } |
| 522 | #endif // wxUSE_MENU_CALLBACK |
| 523 | |
| 524 | // Try the menu's event handler |
| 525 | if ( !processed ) |
| 526 | { |
| 527 | wxEvtHandler *handler = GetEventHandler(); |
| 528 | if ( handler ) |
| 529 | processed = handler->ProcessEvent(event); |
| 530 | } |
| 531 | |
| 532 | // Try the window the menu was popped up from (and up through the |
| 533 | // hierarchy) |
| 534 | if ( !processed ) |
| 535 | { |
| 536 | const wxMenuBase *menu = this; |
| 537 | while ( menu ) |
| 538 | { |
| 539 | wxWindow *win = menu->GetInvokingWindow(); |
| 540 | if ( win ) |
| 541 | { |
| 542 | processed = win->GetEventHandler()->ProcessEvent(event); |
| 543 | break; |
| 544 | } |
| 545 | |
| 546 | menu = menu->GetParent(); |
| 547 | } |
| 548 | } |
| 549 | |
| 550 | return processed; |
| 551 | } |
| 552 | |
| 553 | // ---------------------------------------------------------------------------- |
| 554 | // wxMenu attaching/detaching to/from menu bar |
| 555 | // ---------------------------------------------------------------------------- |
| 556 | |
| 557 | void wxMenuBase::Attach(wxMenuBarBase *menubar) |
| 558 | { |
| 559 | // use Detach() instead! |
| 560 | wxASSERT_MSG( menubar, _T("menu can't be attached to NULL menubar") ); |
| 561 | |
| 562 | // use IsAttached() to prevent this from happening |
| 563 | wxASSERT_MSG( !m_menuBar, _T("attaching menu twice?") ); |
| 564 | |
| 565 | m_menuBar = (wxMenuBar *)menubar; |
| 566 | } |
| 567 | |
| 568 | void wxMenuBase::Detach() |
| 569 | { |
| 570 | // use IsAttached() to prevent this from happening |
| 571 | wxASSERT_MSG( m_menuBar, _T("detaching unattached menu?") ); |
| 572 | |
| 573 | m_menuBar = NULL; |
| 574 | } |
| 575 | |
| 576 | // ---------------------------------------------------------------------------- |
| 577 | // wxMenu functions forwarded to wxMenuItem |
| 578 | // ---------------------------------------------------------------------------- |
| 579 | |
| 580 | void wxMenuBase::Enable( int id, bool enable ) |
| 581 | { |
| 582 | wxMenuItem *item = FindItem(id); |
| 583 | |
| 584 | wxCHECK_RET( item, wxT("wxMenu::Enable: no such item") ); |
| 585 | |
| 586 | item->Enable(enable); |
| 587 | } |
| 588 | |
| 589 | bool wxMenuBase::IsEnabled( int id ) const |
| 590 | { |
| 591 | wxMenuItem *item = FindItem(id); |
| 592 | |
| 593 | wxCHECK_MSG( item, FALSE, wxT("wxMenu::IsEnabled: no such item") ); |
| 594 | |
| 595 | return item->IsEnabled(); |
| 596 | } |
| 597 | |
| 598 | void wxMenuBase::Check( int id, bool enable ) |
| 599 | { |
| 600 | wxMenuItem *item = FindItem(id); |
| 601 | |
| 602 | wxCHECK_RET( item, wxT("wxMenu::Check: no such item") ); |
| 603 | |
| 604 | item->Check(enable); |
| 605 | } |
| 606 | |
| 607 | bool wxMenuBase::IsChecked( int id ) const |
| 608 | { |
| 609 | wxMenuItem *item = FindItem(id); |
| 610 | |
| 611 | wxCHECK_MSG( item, FALSE, wxT("wxMenu::IsChecked: no such item") ); |
| 612 | |
| 613 | return item->IsChecked(); |
| 614 | } |
| 615 | |
| 616 | void wxMenuBase::SetLabel( int id, const wxString &label ) |
| 617 | { |
| 618 | wxMenuItem *item = FindItem(id); |
| 619 | |
| 620 | wxCHECK_RET( item, wxT("wxMenu::SetLabel: no such item") ); |
| 621 | |
| 622 | item->SetText(label); |
| 623 | } |
| 624 | |
| 625 | wxString wxMenuBase::GetLabel( int id ) const |
| 626 | { |
| 627 | wxMenuItem *item = FindItem(id); |
| 628 | |
| 629 | wxCHECK_MSG( item, wxT(""), wxT("wxMenu::GetLabel: no such item") ); |
| 630 | |
| 631 | return item->GetText(); |
| 632 | } |
| 633 | |
| 634 | void wxMenuBase::SetHelpString( int id, const wxString& helpString ) |
| 635 | { |
| 636 | wxMenuItem *item = FindItem(id); |
| 637 | |
| 638 | wxCHECK_RET( item, wxT("wxMenu::SetHelpString: no such item") ); |
| 639 | |
| 640 | item->SetHelp( helpString ); |
| 641 | } |
| 642 | |
| 643 | wxString wxMenuBase::GetHelpString( int id ) const |
| 644 | { |
| 645 | wxMenuItem *item = FindItem(id); |
| 646 | |
| 647 | wxCHECK_MSG( item, wxT(""), wxT("wxMenu::GetHelpString: no such item") ); |
| 648 | |
| 649 | return item->GetHelp(); |
| 650 | } |
| 651 | |
| 652 | // ---------------------------------------------------------------------------- |
| 653 | // wxMenuBarBase ctor and dtor |
| 654 | // ---------------------------------------------------------------------------- |
| 655 | |
| 656 | wxMenuBarBase::wxMenuBarBase() |
| 657 | { |
| 658 | // we own the menus when we get them |
| 659 | m_menus.DeleteContents(TRUE); |
| 660 | |
| 661 | // not attached yet |
| 662 | m_menuBarFrame = NULL; |
| 663 | } |
| 664 | |
| 665 | wxMenuBarBase::~wxMenuBarBase() |
| 666 | { |
| 667 | // nothing to do, the list will delete the menus because of the call to |
| 668 | // DeleteContents() above |
| 669 | } |
| 670 | |
| 671 | // ---------------------------------------------------------------------------- |
| 672 | // wxMenuBar item access: the base class versions manage m_menus list, the |
| 673 | // derived class should reflect the changes in the real menubar |
| 674 | // ---------------------------------------------------------------------------- |
| 675 | |
| 676 | wxMenu *wxMenuBarBase::GetMenu(size_t pos) const |
| 677 | { |
| 678 | wxMenuList::Node *node = m_menus.Item(pos); |
| 679 | wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::GetMenu()") ); |
| 680 | |
| 681 | return node->GetData(); |
| 682 | } |
| 683 | |
| 684 | bool wxMenuBarBase::Append(wxMenu *menu, const wxString& WXUNUSED(title)) |
| 685 | { |
| 686 | wxCHECK_MSG( menu, FALSE, wxT("can't append NULL menu") ); |
| 687 | |
| 688 | m_menus.Append(menu); |
| 689 | menu->Attach(this); |
| 690 | |
| 691 | return TRUE; |
| 692 | } |
| 693 | |
| 694 | bool wxMenuBarBase::Insert(size_t pos, wxMenu *menu, |
| 695 | const wxString& title) |
| 696 | { |
| 697 | if ( pos == m_menus.GetCount() ) |
| 698 | { |
| 699 | return wxMenuBarBase::Append(menu, title); |
| 700 | } |
| 701 | else // not at the end |
| 702 | { |
| 703 | wxCHECK_MSG( menu, FALSE, wxT("can't insert NULL menu") ); |
| 704 | |
| 705 | wxMenuList::Node *node = m_menus.Item(pos); |
| 706 | wxCHECK_MSG( node, FALSE, wxT("bad index in wxMenuBar::Insert()") ); |
| 707 | |
| 708 | m_menus.Insert(node, menu); |
| 709 | menu->Attach(this); |
| 710 | |
| 711 | return TRUE; |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | wxMenu *wxMenuBarBase::Replace(size_t pos, wxMenu *menu, |
| 716 | const wxString& WXUNUSED(title)) |
| 717 | { |
| 718 | wxCHECK_MSG( menu, NULL, wxT("can't insert NULL menu") ); |
| 719 | |
| 720 | wxMenuList::Node *node = m_menus.Item(pos); |
| 721 | wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::Replace()") ); |
| 722 | |
| 723 | wxMenu *menuOld = node->GetData(); |
| 724 | node->SetData(menu); |
| 725 | |
| 726 | menu->Attach(this); |
| 727 | menuOld->Detach(); |
| 728 | |
| 729 | return menuOld; |
| 730 | } |
| 731 | |
| 732 | wxMenu *wxMenuBarBase::Remove(size_t pos) |
| 733 | { |
| 734 | wxMenuList::Node *node = m_menus.Item(pos); |
| 735 | wxCHECK_MSG( node, NULL, wxT("bad index in wxMenuBar::Remove()") ); |
| 736 | |
| 737 | node = m_menus.DetachNode(node); |
| 738 | wxCHECK( node, NULL ); // unexpected |
| 739 | wxMenu *menu = node->GetData(); |
| 740 | menu->Detach(); |
| 741 | |
| 742 | delete node; |
| 743 | |
| 744 | return menu; |
| 745 | } |
| 746 | |
| 747 | int wxMenuBarBase::FindMenu(const wxString& title) const |
| 748 | { |
| 749 | wxString label = wxMenuItem::GetLabelFromText(title); |
| 750 | |
| 751 | size_t count = GetMenuCount(); |
| 752 | for ( size_t i = 0; i < count; i++ ) |
| 753 | { |
| 754 | wxString title2 = GetLabelTop(i); |
| 755 | if ( (title2 == title) || |
| 756 | (wxMenuItem::GetLabelFromText(title2) == label) ) |
| 757 | { |
| 758 | // found |
| 759 | return (int)i; |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | return wxNOT_FOUND; |
| 764 | |
| 765 | } |
| 766 | |
| 767 | // ---------------------------------------------------------------------------- |
| 768 | // wxMenuBar attaching/detaching to/from the frame |
| 769 | // ---------------------------------------------------------------------------- |
| 770 | |
| 771 | void wxMenuBarBase::Attach(wxFrame *frame) |
| 772 | { |
| 773 | wxASSERT_MSG( !IsAttached(), wxT("menubar already attached!") ); |
| 774 | |
| 775 | m_menuBarFrame = frame; |
| 776 | } |
| 777 | |
| 778 | void wxMenuBarBase::Detach() |
| 779 | { |
| 780 | wxASSERT_MSG( IsAttached(), wxT("detaching unattached menubar") ); |
| 781 | |
| 782 | m_menuBarFrame = NULL; |
| 783 | } |
| 784 | |
| 785 | // ---------------------------------------------------------------------------- |
| 786 | // wxMenuBar searching for items |
| 787 | // ---------------------------------------------------------------------------- |
| 788 | |
| 789 | wxMenuItem *wxMenuBarBase::FindItem(int id, wxMenu **menu) const |
| 790 | { |
| 791 | if ( menu ) |
| 792 | *menu = NULL; |
| 793 | |
| 794 | wxMenuItem *item = NULL; |
| 795 | size_t count = GetMenuCount(); |
| 796 | for ( size_t i = 0; !item && (i < count); i++ ) |
| 797 | { |
| 798 | item = m_menus[i]->FindItem(id, menu); |
| 799 | } |
| 800 | |
| 801 | return item; |
| 802 | } |
| 803 | |
| 804 | int wxMenuBarBase::FindMenuItem(const wxString& menu, const wxString& item) const |
| 805 | { |
| 806 | wxString label = wxMenuItem::GetLabelFromText(menu); |
| 807 | |
| 808 | int i = 0; |
| 809 | wxMenuList::Node *node; |
| 810 | for ( node = m_menus.GetFirst(); node; node = node->GetNext(), i++ ) |
| 811 | { |
| 812 | if ( label == wxMenuItem::GetLabelFromText(GetLabelTop(i)) ) |
| 813 | return node->GetData()->FindItem(item); |
| 814 | } |
| 815 | |
| 816 | return wxNOT_FOUND; |
| 817 | } |
| 818 | |
| 819 | // --------------------------------------------------------------------------- |
| 820 | // wxMenuBar functions forwarded to wxMenuItem |
| 821 | // --------------------------------------------------------------------------- |
| 822 | |
| 823 | void wxMenuBarBase::Enable(int id, bool enable) |
| 824 | { |
| 825 | wxMenuItem *item = FindItem(id); |
| 826 | |
| 827 | wxCHECK_RET( item, wxT("attempt to enable an item which doesn't exist") ); |
| 828 | |
| 829 | item->Enable(enable); |
| 830 | } |
| 831 | |
| 832 | void wxMenuBarBase::Check(int id, bool check) |
| 833 | { |
| 834 | wxMenuItem *item = FindItem(id); |
| 835 | |
| 836 | wxCHECK_RET( item, wxT("attempt to check an item which doesn't exist") ); |
| 837 | wxCHECK_RET( item->IsCheckable(), wxT("attempt to check an uncheckable item") ); |
| 838 | |
| 839 | item->Check(check); |
| 840 | } |
| 841 | |
| 842 | bool wxMenuBarBase::IsChecked(int id) const |
| 843 | { |
| 844 | wxMenuItem *item = FindItem(id); |
| 845 | |
| 846 | wxCHECK_MSG( item, FALSE, wxT("wxMenuBar::IsChecked(): no such item") ); |
| 847 | |
| 848 | return item->IsChecked(); |
| 849 | } |
| 850 | |
| 851 | bool wxMenuBarBase::IsEnabled(int id) const |
| 852 | { |
| 853 | wxMenuItem *item = FindItem(id); |
| 854 | |
| 855 | wxCHECK_MSG( item, FALSE, wxT("wxMenuBar::IsEnabled(): no such item") ); |
| 856 | |
| 857 | return item->IsEnabled(); |
| 858 | } |
| 859 | |
| 860 | void wxMenuBarBase::SetLabel(int id, const wxString& label) |
| 861 | { |
| 862 | wxMenuItem *item = FindItem(id); |
| 863 | |
| 864 | wxCHECK_RET( item, wxT("wxMenuBar::SetLabel(): no such item") ); |
| 865 | |
| 866 | item->SetText(label); |
| 867 | } |
| 868 | |
| 869 | wxString wxMenuBarBase::GetLabel(int id) const |
| 870 | { |
| 871 | wxMenuItem *item = FindItem(id); |
| 872 | |
| 873 | wxCHECK_MSG( item, wxEmptyString, |
| 874 | wxT("wxMenuBar::GetLabel(): no such item") ); |
| 875 | |
| 876 | return item->GetText(); |
| 877 | } |
| 878 | |
| 879 | void wxMenuBarBase::SetHelpString(int id, const wxString& helpString) |
| 880 | { |
| 881 | wxMenuItem *item = FindItem(id); |
| 882 | |
| 883 | wxCHECK_RET( item, wxT("wxMenuBar::SetHelpString(): no such item") ); |
| 884 | |
| 885 | item->SetHelp(helpString); |
| 886 | } |
| 887 | |
| 888 | wxString wxMenuBarBase::GetHelpString(int id) const |
| 889 | { |
| 890 | wxMenuItem *item = FindItem(id); |
| 891 | |
| 892 | wxCHECK_MSG( item, wxEmptyString, |
| 893 | wxT("wxMenuBar::GetHelpString(): no such item") ); |
| 894 | |
| 895 | return item->GetHelp(); |
| 896 | } |
| 897 | |
| 898 | #endif // wxUSE_MENUS |