added support for GTK2 label mnemonics (patch #689573)
[wxWidgets.git] / src / gtk / control.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: control.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Vadim Zeitlin
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 #ifdef __GNUG__
11 #pragma implementation "control.h"
12 #endif
13
14 #include "wx/defs.h"
15
16 #if wxUSE_CONTROLS
17
18 #include "wx/control.h"
19
20 #include <gtk/gtk.h>
21
22 //-----------------------------------------------------------------------------
23 // wxControl
24 //-----------------------------------------------------------------------------
25
26 IMPLEMENT_DYNAMIC_CLASS(wxControl, wxWindow)
27
28 wxControl::wxControl()
29 {
30 m_needParent = TRUE;
31 }
32
33 bool wxControl::Create( wxWindow *parent,
34 wxWindowID id,
35 const wxPoint &pos,
36 const wxSize &size,
37 long style,
38 const wxValidator& validator,
39 const wxString &name )
40 {
41 bool ret = wxWindow::Create(parent, id, pos, size, style, name);
42
43 #if wxUSE_VALIDATORS
44 SetValidator(validator);
45 #endif
46
47 return ret;
48 }
49
50 void wxControl::SetLabel( const wxString &label )
51 {
52 m_label.Empty();
53 for ( const wxChar *pc = label; *pc != wxT('\0'); pc++ )
54 {
55 if ( *pc == wxT('&') )
56 {
57 pc++; // skip it
58 #if 0 // it would be unused anyhow for now - kbd interface not done yet
59 if ( *pc != wxT('&') ) m_chAccel = *pc;
60 #endif
61 }
62 m_label << *pc;
63 }
64 }
65
66 wxString wxControl::GetLabel() const
67 {
68 return m_label;
69 }
70
71
72 wxSize wxControl::DoGetBestSize() const
73 {
74 // Do not return any arbitrary default value...
75 wxASSERT_MSG( m_widget, wxT("DoGetBestSize called before creation") );
76
77 GtkRequisition req;
78 req.width = 2;
79 req.height = 2;
80 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request )
81 (m_widget, &req );
82
83 return wxSize(req.width, req.height);
84 }
85
86 wxString wxControl::PrepareLabelMnemonics( const wxString &label ) const
87 {
88 //Format mnemonics properly for GTK2. This can be called from GTK1.x, but
89 //it's not very useful because mnemonics don't exist prior to GTK2.
90 wxString label2;
91 for (size_t i = 0; i < label.Len(); i++)
92 {
93 if (label.GetChar(i) == wxT('&'))
94 {
95 //Mnemonic escape sequence "&&" is a literal "&" in the output.
96 if (label.GetChar(i + 1) == wxT('&'))
97 {
98 label2 << wxT('&');
99 i++;
100 }
101 //Handle special case of "&_" (i.e. "_" is the mnemonic).
102 //FIXME - Is it possible to use "_" as a GTK mnemonic? Just use a
103 //dash for now.
104 else if (label.GetChar(i + 1) == wxT('_'))
105 {
106 label2 << wxT("_-");
107 i++;
108 }
109 //Replace WX mnemonic indicator "&" with GTK indicator "_".
110 else
111 {
112 label2 << wxT('_');
113 }
114 }
115 else if (label.GetChar(i) == wxT('_'))
116 {
117 //Escape any underlines in the string so GTK doesn't use them.
118 label2 << wxT("__");
119 }
120 else
121 {
122 label2 << label.GetChar(i);
123 }
124 }
125 return label2;
126 }
127
128 #endif // wxUSE_CONTROLS
129