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
This commit is contained in:
Vadim Zeitlin 2006-07-25 18:40:04 +00:00
parent 60fa553ce7
commit 74639764d0
6 changed files with 65 additions and 16 deletions

View File

@ -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}

View File

@ -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.
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.
Strips any menu codes from {\it in} and places the result
in {\it out} (or returns the new string, in the first form).
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}

View File

@ -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

View File

@ -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;

View File

@ -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);

View File

@ -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;