From 74639764d0076ce477ed3d69827061063f4b5e9a Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 25 Jul 2006 18:40:04 +0000 Subject: [PATCH] added possibility to strip only mnemonics, not accels, in wxStripMenuCodes(); added wxControl::GetLabelText() git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@40329 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/latex/wx/control.tex | 15 +++++++++++++-- docs/latex/wx/function.tex | 20 +++++++++++--------- include/wx/control.h | 5 +++++ include/wx/utils.h | 25 +++++++++++++++++++++++-- src/common/ctrlcmn.cpp | 8 ++++++++ src/common/utilscmn.cpp | 8 +++++--- 6 files changed, 65 insertions(+), 16 deletions(-) diff --git a/docs/latex/wx/control.tex b/docs/latex/wx/control.tex index 317ab1f7e6..eade0d1ddd 100644 --- a/docs/latex/wx/control.tex +++ b/docs/latex/wx/control.tex @@ -31,12 +31,23 @@ Simulates the effect of the user issuing a command to the item. See \helpref{wxC \membersection{wxControl::GetLabel}\label{wxcontrolgetlabel} -\func{wxString\&}{GetLabel}{\void} +\constfunc{const wxString\&}{GetLabel}{\void} Returns the control's text. Note that the returned string contains the mnemonics (\texttt{\&} characters) if -any. +any, use \helpref{wxControl::GetLabelText}{wxcontrolgetlabeltext} if they are +undesired. + + +\membersection{wxControl::GetLabelText}\label{wxcontrolgetlabeltext} + +\constfunc{const wxString\&}{GetLabelText}{\void} + +\func{static const wxString\&}{GetLabelText}{\param{const wxString\& }{label}} + +Returns the control's label or the given \arg{label} string for the static +version without the mnemonics characters. \membersection{wxControl::SetLabel}\label{wxcontrolsetlabel} diff --git a/docs/latex/wx/function.tex b/docs/latex/wx/function.tex index 74b7136158..4a8f89a7a1 100644 --- a/docs/latex/wx/function.tex +++ b/docs/latex/wx/function.tex @@ -3222,18 +3222,20 @@ See also \helpref{wxGetDisplayName}{wxgetdisplayname}. \membersection{::wxStripMenuCodes}\label{wxstripmenucodes} -\func{wxString}{wxStripMenuCodes}{\param{const wxString\& }{in}} +\func{wxString}{wxStripMenuCodes}{\param{const wxString\& }{str}, \param{int }{flags = wxStrip\_All}} -\func{void}{wxStripMenuCodes}{\param{char *}{in}, \param{char *}{out}} +Strips any menu codes from \arg{str} and returns the result. -{\bf NB:} This function is obsolete, please use -\helpref{wxMenuItem::GetLabelFromText}{wxmenuitemgetlabelfromtext} instead. - -Strips any menu codes from {\it in} and places the result -in {\it out} (or returns the new string, in the first form). +By default, the functions strips both the mnemonics character (\texttt{'\&'}) +which is used to indicate a keyboard shortkey, and the accelerators, which are +used only in the menu items and are separated from the main text by the +\texttt{$\backslash$t} (TAB) character. By using \arg{flags} of +\texttt{wxStrip\_Mnemonics} or \texttt{wxStrip\_Accel} to strip only the former +or the latter part, respectively. -Menu codes include \& (mark the next character with an underline -as a keyboard shortkey in Windows and Motif) and $\backslash$t (tab in Windows). +Notice that in most cases +\helpref{wxMenuItem::GetLabelFromText}{wxmenuitemgetlabelfromtext} or +\helpref{wxControl::GetLabelText}{wxcontrolgetlabeltext} can be used instead. \wxheading{Include files} diff --git a/include/wx/control.h b/include/wx/control.h index 1432d67e5c..aed113f803 100644 --- a/include/wx/control.h +++ b/include/wx/control.h @@ -46,6 +46,11 @@ public: // get the control alignment (left/right/centre, top/bottom/centre) int GetAlignment() const { return m_windowStyle & wxALIGN_MASK; } + // get the string without mnemonic characters ('&') + static wxString GetLabelText(const wxString& label); + + // get just the text of the label, without mnemonic characters ('&') + wxString GetLabelText() const { return GetLabelText(GetLabel()); } // controls by default inherit the colours of their parents, if a // particular control class doesn't want to do it, it can override diff --git a/include/wx/utils.h b/include/wx/utils.h index 1141a3004e..237793b05d 100644 --- a/include/wx/utils.h +++ b/include/wx/utils.h @@ -532,8 +532,29 @@ WXDLLIMPEXP_BASE bool wxGetDiskSpace(const wxString& path, // Menu accelerators related things // ---------------------------------------------------------------------------- -WXDLLEXPORT wxChar* wxStripMenuCodes(const wxChar *in, wxChar *out = (wxChar *) NULL); -WXDLLEXPORT wxString wxStripMenuCodes(const wxString& str); +// flags for wxStripMenuCodes +enum +{ + // strip '&' characters + wxStrip_Mnemonics = 1, + + // strip everything after '\t' + wxStrip_Accel = 2, + + // strip everything (this is the default) + wxStrip_All = wxStrip_Mnemonics | wxStrip_Accel +}; + +// strip mnemonics and/or accelerators from the label +WXDLLEXPORT wxString +wxStripMenuCodes(const wxString& str, int flags = wxStrip_All); + +// obsolete and deprecated version, do not use +#if WXWIN_COMPATIBILITY_2_6 +wxDEPRECATED( + WXDLLEXPORT wxChar* wxStripMenuCodes(const wxChar *in, wxChar *out = NULL) +); +#endif #if wxUSE_ACCEL class WXDLLEXPORT wxAcceleratorEntry; diff --git a/src/common/ctrlcmn.cpp b/src/common/ctrlcmn.cpp index 46532a9cda..2bb9ab4520 100644 --- a/src/common/ctrlcmn.cpp +++ b/src/common/ctrlcmn.cpp @@ -33,6 +33,7 @@ #include "wx/radiobut.h" #include "wx/statbmp.h" #include "wx/bitmap.h" + #include "wx/utils.h" // for wxStripMenuCodes() #endif const wxChar wxControlNameStr[] = wxT("control"); @@ -85,6 +86,13 @@ bool wxControlBase::CreateControl(wxWindowBase *parent, return true; } +/* static */ +wxString wxControlBase::GetLabelText(const wxString& label) +{ + // we don't want strip the TABs here, just the mnemonics + return wxStripMenuCodes(label, wxStrip_Mnemonics); +} + void wxControlBase::Command(wxCommandEvent& event) { (void)GetEventHandler()->ProcessEvent(event); diff --git a/src/common/utilscmn.cpp b/src/common/utilscmn.cpp index eadd15283b..303e31881f 100644 --- a/src/common/utilscmn.cpp +++ b/src/common/utilscmn.cpp @@ -941,8 +941,10 @@ wxChar *wxStripMenuCodes(const wxChar *in, wxChar *out) return out; } -wxString wxStripMenuCodes(const wxString& in) +wxString wxStripMenuCodes(const wxString& in, int flags) { + wxASSERT_MSG( flags, _T("this is useless to call without any flags") ); + wxString out; size_t len = in.length(); @@ -951,7 +953,7 @@ wxString wxStripMenuCodes(const wxString& in) for ( size_t n = 0; n < len; n++ ) { wxChar ch = in[n]; - if ( ch == _T('&') ) + if ( (flags & wxStrip_Mnemonics) && ch == _T('&') ) { // skip it, it is used to introduce the accel char (or to quote // itself in which case it should still be skipped): note that it @@ -966,7 +968,7 @@ wxString wxStripMenuCodes(const wxString& in) ch = in[n]; } } - else if ( ch == _T('\t') ) + else if ( (flags & wxStrip_Accel) && ch == _T('\t') ) { // everything after TAB is accel string, exit the loop break; -- 2.45.2