]> git.saurik.com Git - wxWidgets.git/blame - src/gtk1/control.cpp
app.h must be included for the non RAEL implementation, as universal binaries command...
[wxWidgets.git] / src / gtk1 / control.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
3cbab641 2// Name: src/gtk1/control.cpp
634fb750 3// Purpose: wxControl implementation for wxGTK
c801d85f 4// Author: Robert Roebling
dbf858b5 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling, Julian Smart and Vadim Zeitlin
65571936 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
1e6feb95
VZ
13#if wxUSE_CONTROLS
14
634fb750 15#include "wx/log.h"
c801d85f 16#include "wx/control.h"
9d522606
RD
17#include "wx/fontutil.h"
18#include "wx/settings.h"
3cbab641 19#include "wx/gtk1/private.h"
034be888 20
634fb750
VZ
21// ============================================================================
22// wxControl implementation
23// ============================================================================
24
25// ----------------------------------------------------------------------------
26// wxControl creation
27// ----------------------------------------------------------------------------
c801d85f 28
9abe166a 29IMPLEMENT_DYNAMIC_CLASS(wxControl, wxWindow)
c801d85f 30
31528cd3 31wxControl::wxControl()
c801d85f 32{
b292e2f5 33 m_needParent = TRUE;
6de97a3b 34}
c801d85f 35
04165bec 36bool wxControl::Create( wxWindow *parent,
31528cd3
VZ
37 wxWindowID id,
38 const wxPoint &pos,
39 const wxSize &size,
40 long style,
8d772832 41 const wxValidator& validator,
04165bec 42 const wxString &name )
8d772832 43{
04165bec 44 bool ret = wxWindow::Create(parent, id, pos, size, style, name);
b2ff89d6 45
04165bec 46#if wxUSE_VALIDATORS
8d772832 47 SetValidator(validator);
8d772832
RD
48#endif
49
04165bec
RR
50 return ret;
51}
52
f68586e5
VZ
53wxSize wxControl::DoGetBestSize() const
54{
0279e844
RR
55 // Do not return any arbitrary default value...
56 wxASSERT_MSG( m_widget, wxT("DoGetBestSize called before creation") );
57
f68586e5 58 GtkRequisition req;
33720b2d
RR
59 req.width = 2;
60 req.height = 2;
2afa14f2 61 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request )
f68586e5
VZ
62 (m_widget, &req );
63
9f884528
RD
64 wxSize best(req.width, req.height);
65 CacheBestSize(best);
66 return best;
f68586e5
VZ
67}
68
abdeb9e7
RD
69
70void wxControl::PostCreation(const wxSize& size)
71{
72 wxWindow::PostCreation();
f40fdaa3
VS
73
74 // NB: GetBestSize needs to know the style, otherwise it will assume
75 // default font and if the user uses a different font, determined
76 // best size will be different (typically, smaller) than the desired
77 // size. This call ensure that a style is available at the time
78 // GetBestSize is called.
79 gtk_widget_ensure_style(m_widget);
b2ff89d6 80
abdeb9e7
RD
81 ApplyWidgetStyle();
82 SetInitialBestSize(size);
83}
84
b2ff89d6
VZ
85// ----------------------------------------------------------------------------
86// wxControl dealing with labels
87// ----------------------------------------------------------------------------
88
89void wxControl::SetLabel( const wxString &label )
90{
91 // keep the original string internally to be able to return it later (for
92 // consistency with the other ports)
93 m_label = label;
94
95 InvalidateBestSize();
96}
97
98wxString wxControl::GetLabel() const
99{
100 return m_label;
101}
102
103void wxControl::GTKSetLabelForLabel(GtkLabel *w, const wxString& label)
104{
105 // don't call the virtual function which might call this one back again
106 wxControl::SetLabel(label);
107
108 const wxString labelGTK = GTKConvertMnemonics(label);
abdeb9e7 109
b2ff89d6 110 gtk_label_set(w, wxGTK_CONV(labelGTK));
b2ff89d6
VZ
111}
112
113void wxControl::GTKSetLabelForFrame(GtkFrame *w, const wxString& label)
114{
115 wxControl::SetLabel(label);
116
117 // frames don't support mnemonics even under GTK+ 2
118 const wxString labelGTK = GTKRemoveMnemonics(label);
119
120 gtk_frame_set_label(w, labelGTK.empty() ? (char *)NULL
121 : wxGTK_CONV(labelGTK));
122}
123
124// worker function implementing both GTKConvert/RemoveMnemonics()
125//
126// notice that under GTK+ 1 we only really need to support MNEMONICS_REMOVE as
127// it doesn't support mnemonics anyhow but this would make the code so ugly
128// that we do the same thing for GKT+ 1 and 2
129enum MnemonicsFlag
eaafd2f8 130{
b2ff89d6
VZ
131 MNEMONICS_REMOVE,
132 MNEMONICS_CONVERT
133};
134
135static wxString GTKProcessMnemonics(const wxString& label, MnemonicsFlag flag)
136{
137 const size_t len = label.length();
138 wxString labelGTK;
139 labelGTK.reserve(len);
140 for ( size_t i = 0; i < len; i++ )
eaafd2f8 141 {
b2ff89d6
VZ
142 wxChar ch = label[i];
143
144 switch ( ch )
eaafd2f8 145 {
b2ff89d6
VZ
146 case wxT('&'):
147 if ( i == len - 1 )
148 {
149 // "&" at the end of string is an error
150 wxLogDebug(wxT("Invalid label \"%s\"."), label.c_str());
151 break;
152 }
153
154 ch = label[++i]; // skip '&' itself
155 switch ( ch )
156 {
157 case wxT('&'):
158 // special case: "&&" is not a mnemonic at all but just
159 // an escaped "&"
160 labelGTK += wxT('&');
161 break;
162
163 case wxT('_'):
164 if ( flag == MNEMONICS_CONVERT )
165 {
166 // '_' can't be a GTK mnemonic apparently so
167 // replace it with something similar
168 labelGTK += wxT("_-");
169 break;
170 }
171 //else: fall through
172
173 default:
174 if ( flag == MNEMONICS_CONVERT )
175 labelGTK += wxT('_');
176 labelGTK += ch;
177 }
178 break;
179
180 case wxT('_'):
181 if ( flag == MNEMONICS_CONVERT )
182 {
183 // escape any existing underlines in the string so that
184 // they don't become mnemonics accidentally
185 labelGTK += wxT("__");
186 break;
187 }
188 //else: fall through
189
190 default:
191 labelGTK += ch;
eaafd2f8
VS
192 }
193 }
b2ff89d6
VZ
194
195 return labelGTK;
196}
197
198/* static */
199wxString wxControl::GTKRemoveMnemonics(const wxString& label)
200{
201 return GTKProcessMnemonics(label, MNEMONICS_REMOVE);
202}
203
204/* static */
205wxString wxControl::GTKConvertMnemonics(const wxString& label)
206{
d411c5d6 207 return GTKRemoveMnemonics(label);
eaafd2f8
VS
208}
209
b2ff89d6
VZ
210// ----------------------------------------------------------------------------
211// wxControl styles (a.k.a. attributes)
212// ----------------------------------------------------------------------------
9d522606
RD
213
214wxVisualAttributes wxControl::GetDefaultAttributes() const
215{
216 return GetDefaultAttributesFromGTKWidget(m_widget,
217 UseGTKStyleBase());
218}
219
220
221#define SHIFT (8*(sizeof(short int)-sizeof(char)))
222
223// static
224wxVisualAttributes
225wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget* widget,
226 bool useBase,
227 int state)
228{
229 GtkStyle* style;
230 wxVisualAttributes attr;
231
232 style = gtk_rc_get_style(widget);
233 if (!style)
234 style = gtk_widget_get_default_style();
235
236 if (!style)
237 {
238 return wxWindow::GetClassDefaultAttributes(wxWINDOW_VARIANT_NORMAL);
239 }
240
241 if (state == -1)
242 state = GTK_STATE_NORMAL;
b2ff89d6 243
9d522606
RD
244 // get the style's colours
245 attr.colFg = wxColour(style->fg[state].red >> SHIFT,
246 style->fg[state].green >> SHIFT,
247 style->fg[state].blue >> SHIFT);
248 if (useBase)
249 attr.colBg = wxColour(style->base[state].red >> SHIFT,
250 style->base[state].green >> SHIFT,
251 style->base[state].blue >> SHIFT);
252 else
253 attr.colBg = wxColour(style->bg[state].red >> SHIFT,
254 style->bg[state].green >> SHIFT,
255 style->bg[state].blue >> SHIFT);
256
257 // get the style's font
9d522606
RD
258 // TODO: isn't there a way to get a standard gtk 1.2 font?
259 attr.font = wxFont( 12, wxSWISS, wxNORMAL, wxNORMAL );
b2ff89d6 260
9d522606
RD
261 return attr;
262}
263
264
265//static
266wxVisualAttributes
865bb325 267wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNew_t widget_new,
9d522606
RD
268 bool useBase,
269 int state)
270{
271 wxVisualAttributes attr;
66d8fe77
VS
272 // NB: we need toplevel window so that GTK+ can find the right style
273 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
9d522606 274 GtkWidget* widget = widget_new();
66d8fe77 275 gtk_container_add(GTK_CONTAINER(wnd), widget);
9d522606 276 attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state);
66d8fe77 277 gtk_widget_destroy(wnd);
9d522606
RD
278 return attr;
279}
280
281//static
282wxVisualAttributes
865bb325 283wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromStr_t widget_new,
9d522606
RD
284 bool useBase,
285 int state)
286{
287 wxVisualAttributes attr;
66d8fe77
VS
288 // NB: we need toplevel window so that GTK+ can find the right style
289 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
9d522606 290 GtkWidget* widget = widget_new("");
66d8fe77 291 gtk_container_add(GTK_CONTAINER(wnd), widget);
9d522606 292 attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state);
66d8fe77 293 gtk_widget_destroy(wnd);
9d522606
RD
294 return attr;
295}
296
297
298//static
299wxVisualAttributes
865bb325 300wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromAdj_t widget_new,
9d522606
RD
301 bool useBase,
302 int state)
303{
304 wxVisualAttributes attr;
66d8fe77
VS
305 // NB: we need toplevel window so that GTK+ can find the right style
306 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
9d522606 307 GtkWidget* widget = widget_new(NULL);
66d8fe77 308 gtk_container_add(GTK_CONTAINER(wnd), widget);
9d522606 309 attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state);
66d8fe77 310 gtk_widget_destroy(wnd);
9d522606
RD
311 return attr;
312}
313
1e6feb95
VZ
314#endif // wxUSE_CONTROLS
315