X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/ee0a94cfc2f71e8b770eedda5197a1f4bd62b5cb..8168167976dc3cc5f5223a21e1a62ba91a4f77b3:/src/common/menucmn.cpp?ds=sidebyside diff --git a/src/common/menucmn.cpp b/src/common/menucmn.cpp index 09251fcb4d..49f3a00d9e 100644 --- a/src/common/menucmn.cpp +++ b/src/common/menucmn.cpp @@ -34,6 +34,8 @@ #include "wx/menu.h" #endif +#include "wx/stockitem.h" + // ---------------------------------------------------------------------------- // template lists // ---------------------------------------------------------------------------- @@ -170,19 +172,20 @@ static int return prefixCode + num - first; } -bool wxAcceleratorEntry::FromString(const wxString& text) +/* static */ +bool +wxAcceleratorEntry::ParseAccel(const wxString& text, int *flagsOut, int *keyOut) { - // the parser won't like leading/trailing spaces - wxString label = text.Strip(wxString::both); - - // set to invalid state: - m_flags = 0; - m_keyCode = 0; + // the parser won't like trailing spaces + wxString label = text; + label.Trim(true); // the initial \t must be preserved so don't strip leading whitespaces // check for accelerators: they are given after '\t' int posTab = label.Find(wxT('\t')); if ( posTab == wxNOT_FOUND ) + { return false; + } // parse the accelerator string int accelFlags = wxACCEL_NORMAL; @@ -278,11 +281,30 @@ bool wxAcceleratorEntry::FromString(const wxString& text) wxASSERT_MSG( keyCode, _T("logic error: should have key code here") ); - m_flags = accelFlags; - m_keyCode = keyCode; + if ( flagsOut ) + *flagsOut = accelFlags; + if ( keyOut ) + *keyOut = keyCode; + return true; } +/* static */ +wxAcceleratorEntry *wxAcceleratorEntry::Create(const wxString& str) +{ + int flags, + keyCode; + if ( !ParseAccel(str, &flags, &keyCode) ) + return NULL; + + return new wxAcceleratorEntry(flags, keyCode); +} + +bool wxAcceleratorEntry::FromString(const wxString& str) +{ + return ParseAccel(str, &m_flags, &m_keyCode); +} + wxString wxAcceleratorEntry::ToString() const { wxString text; @@ -327,15 +349,10 @@ wxString wxAcceleratorEntry::ToString() const wxAcceleratorEntry *wxGetAccelFromString(const wxString& label) { - wxAcceleratorEntry *ret = new wxAcceleratorEntry(); - if (ret->FromString(label)) - return ret; - - wxDELETE(ret); - return NULL; + return wxAcceleratorEntry::Create(label); } -#endif // wxUSE_ACCEL +#endif // wxUSE_ACCEL // ---------------------------------------------------------------------------- @@ -348,8 +365,6 @@ wxMenuItemBase::wxMenuItemBase(wxMenu *parentMenu, const wxString& help, wxItemKind kind, wxMenu *subMenu) - : m_text(text), - m_help(help) { wxASSERT_MSG( parentMenu != NULL, wxT("menuitem should have a menu") ); @@ -363,6 +378,9 @@ wxMenuItemBase::wxMenuItemBase(wxMenu *parentMenu, m_id = wxNewId(); if (m_id == wxID_SEPARATOR) m_kind = wxITEM_SEPARATOR; + + SetText(text); + SetHelp(help); } wxMenuItemBase::~wxMenuItemBase() @@ -374,7 +392,7 @@ wxMenuItemBase::~wxMenuItemBase() wxAcceleratorEntry *wxMenuItemBase::GetAccel() const { - return wxGetAccelFromString(GetText()); + return wxAcceleratorEntry::Create(GetText()); } void wxMenuItemBase::SetAccel(wxAcceleratorEntry *accel) @@ -391,6 +409,30 @@ void wxMenuItemBase::SetAccel(wxAcceleratorEntry *accel) #endif // wxUSE_ACCEL +void wxMenuItemBase::SetText(const wxString& str) +{ + m_text = str; + + if ( m_text.empty() && !IsSeparator() ) + { + wxASSERT_MSG( wxIsStockID(GetId()), + wxT("A non-stock menu item with an empty label?") ); + m_text = wxGetStockLabel(GetId(), wxSTOCK_WITH_ACCELERATOR | + wxSTOCK_WITH_MNEMONIC); + } +} + +void wxMenuItemBase::SetHelp(const wxString& str) +{ + m_help = str; + + if ( m_help.empty() && !IsSeparator() && wxIsStockID(GetId()) ) + { + // get a stock help string + m_help = wxGetStockHelpString(GetId()); + } +} + bool wxMenuBase::ms_locked = true; // ----------------------------------------------------------------------------