fix for compilation without PCH
[wxWidgets.git] / src / gtk / control.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/control.cpp
3 // Purpose: wxControl implementation for wxGTK
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 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #if wxUSE_CONTROLS
14
15 #include "wx/log.h"
16 #include "wx/control.h"
17 #include "wx/fontutil.h"
18 #include "wx/settings.h"
19 #include "wx/gtk/private.h"
20
21 // ============================================================================
22 // wxControl implementation
23 // ============================================================================
24
25 // ----------------------------------------------------------------------------
26 // wxControl creation
27 // ----------------------------------------------------------------------------
28
29 IMPLEMENT_DYNAMIC_CLASS(wxControl, wxWindow)
30
31 wxControl::wxControl()
32 {
33 m_needParent = TRUE;
34 }
35
36 bool wxControl::Create( wxWindow *parent,
37 wxWindowID id,
38 const wxPoint &pos,
39 const wxSize &size,
40 long style,
41 const wxValidator& validator,
42 const wxString &name )
43 {
44 bool ret = wxWindow::Create(parent, id, pos, size, style, name);
45
46 #if wxUSE_VALIDATORS
47 SetValidator(validator);
48 #endif
49
50 return ret;
51 }
52
53 wxSize wxControl::DoGetBestSize() const
54 {
55 // Do not return any arbitrary default value...
56 wxASSERT_MSG( m_widget, wxT("DoGetBestSize called before creation") );
57
58 GtkRequisition req;
59 req.width = 2;
60 req.height = 2;
61 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request )
62 (m_widget, &req );
63
64 wxSize best(req.width, req.height);
65 CacheBestSize(best);
66 return best;
67 }
68
69
70 void wxControl::PostCreation(const wxSize& size)
71 {
72 wxWindow::PostCreation();
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);
80
81 ApplyWidgetStyle();
82 SetInitialBestSize(size);
83 }
84
85 // ----------------------------------------------------------------------------
86 // wxControl dealing with labels
87 // ----------------------------------------------------------------------------
88
89 void 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
98 wxString wxControl::GetLabel() const
99 {
100 return m_label;
101 }
102
103 void 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);
109
110 #ifdef __WXGTK20__
111 gtk_label_set_text_with_mnemonic(w, wxGTK_CONV(labelGTK));
112 #else
113 gtk_label_set(w, wxGTK_CONV(labelGTK));
114 #endif
115 }
116
117 void wxControl::GTKSetLabelForFrame(GtkFrame *w, const wxString& label)
118 {
119 wxControl::SetLabel(label);
120
121 // frames don't support mnemonics even under GTK+ 2
122 const wxString labelGTK = GTKRemoveMnemonics(label);
123
124 gtk_frame_set_label(w, labelGTK.empty() ? (char *)NULL
125 : wxGTK_CONV(labelGTK));
126 }
127
128 // worker function implementing both GTKConvert/RemoveMnemonics()
129 //
130 // notice that under GTK+ 1 we only really need to support MNEMONICS_REMOVE as
131 // it doesn't support mnemonics anyhow but this would make the code so ugly
132 // that we do the same thing for GKT+ 1 and 2
133 enum MnemonicsFlag
134 {
135 MNEMONICS_REMOVE,
136 MNEMONICS_CONVERT
137 };
138
139 static wxString GTKProcessMnemonics(const wxString& label, MnemonicsFlag flag)
140 {
141 const size_t len = label.length();
142 wxString labelGTK;
143 labelGTK.reserve(len);
144 for ( size_t i = 0; i < len; i++ )
145 {
146 wxChar ch = label[i];
147
148 switch ( ch )
149 {
150 case wxT('&'):
151 if ( i == len - 1 )
152 {
153 // "&" at the end of string is an error
154 wxLogDebug(wxT("Invalid label \"%s\"."), label.c_str());
155 break;
156 }
157
158 ch = label[++i]; // skip '&' itself
159 switch ( ch )
160 {
161 case wxT('&'):
162 // special case: "&&" is not a mnemonic at all but just
163 // an escaped "&"
164 labelGTK += wxT('&');
165 break;
166
167 case wxT('_'):
168 if ( flag == MNEMONICS_CONVERT )
169 {
170 // '_' can't be a GTK mnemonic apparently so
171 // replace it with something similar
172 labelGTK += wxT("_-");
173 break;
174 }
175 //else: fall through
176
177 default:
178 if ( flag == MNEMONICS_CONVERT )
179 labelGTK += wxT('_');
180 labelGTK += ch;
181 }
182 break;
183
184 case wxT('_'):
185 if ( flag == MNEMONICS_CONVERT )
186 {
187 // escape any existing underlines in the string so that
188 // they don't become mnemonics accidentally
189 labelGTK += wxT("__");
190 break;
191 }
192 //else: fall through
193
194 default:
195 labelGTK += ch;
196 }
197 }
198
199 return labelGTK;
200 }
201
202 /* static */
203 wxString wxControl::GTKRemoveMnemonics(const wxString& label)
204 {
205 return GTKProcessMnemonics(label, MNEMONICS_REMOVE);
206 }
207
208 /* static */
209 wxString wxControl::GTKConvertMnemonics(const wxString& label)
210 {
211 return GTKProcessMnemonics(label, MNEMONICS_CONVERT);
212 }
213
214 // ----------------------------------------------------------------------------
215 // wxControl styles (a.k.a. attributes)
216 // ----------------------------------------------------------------------------
217
218 wxVisualAttributes wxControl::GetDefaultAttributes() const
219 {
220 return GetDefaultAttributesFromGTKWidget(m_widget,
221 UseGTKStyleBase());
222 }
223
224
225 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
226
227 // static
228 wxVisualAttributes
229 wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget* widget,
230 bool useBase,
231 int state)
232 {
233 GtkStyle* style;
234 wxVisualAttributes attr;
235
236 style = gtk_rc_get_style(widget);
237 if (!style)
238 style = gtk_widget_get_default_style();
239
240 if (!style)
241 {
242 return wxWindow::GetClassDefaultAttributes(wxWINDOW_VARIANT_NORMAL);
243 }
244
245 if (state == -1)
246 state = GTK_STATE_NORMAL;
247
248 // get the style's colours
249 attr.colFg = wxColour(style->fg[state].red >> SHIFT,
250 style->fg[state].green >> SHIFT,
251 style->fg[state].blue >> SHIFT);
252 if (useBase)
253 attr.colBg = wxColour(style->base[state].red >> SHIFT,
254 style->base[state].green >> SHIFT,
255 style->base[state].blue >> SHIFT);
256 else
257 attr.colBg = wxColour(style->bg[state].red >> SHIFT,
258 style->bg[state].green >> SHIFT,
259 style->bg[state].blue >> SHIFT);
260
261 // get the style's font
262 #ifdef __WXGTK20__
263 if ( !style->font_desc )
264 style = gtk_widget_get_default_style();
265 if ( style && style->font_desc )
266 {
267 wxNativeFontInfo info;
268 info.description = pango_font_description_copy(style->font_desc);
269 attr.font = wxFont(info);
270 }
271 else
272 {
273 GtkSettings *settings = gtk_settings_get_default();
274 gchar *font_name = NULL;
275 g_object_get ( settings,
276 "gtk-font-name",
277 &font_name,
278 NULL);
279 if (!font_name)
280 attr.font = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT );
281 else
282 attr.font = wxFont(wxString::FromAscii(font_name));
283 g_free (font_name);
284 }
285 #else
286 // TODO: isn't there a way to get a standard gtk 1.2 font?
287 attr.font = wxFont( 12, wxSWISS, wxNORMAL, wxNORMAL );
288 #endif
289
290 return attr;
291 }
292
293
294 //static
295 wxVisualAttributes
296 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNew_t widget_new,
297 bool useBase,
298 int state)
299 {
300 wxVisualAttributes attr;
301 // NB: we need toplevel window so that GTK+ can find the right style
302 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
303 GtkWidget* widget = widget_new();
304 gtk_container_add(GTK_CONTAINER(wnd), widget);
305 attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state);
306 gtk_widget_destroy(wnd);
307 return attr;
308 }
309
310 //static
311 wxVisualAttributes
312 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromStr_t widget_new,
313 bool useBase,
314 int state)
315 {
316 wxVisualAttributes attr;
317 // NB: we need toplevel window so that GTK+ can find the right style
318 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
319 GtkWidget* widget = widget_new("");
320 gtk_container_add(GTK_CONTAINER(wnd), widget);
321 attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state);
322 gtk_widget_destroy(wnd);
323 return attr;
324 }
325
326
327 //static
328 wxVisualAttributes
329 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromAdj_t widget_new,
330 bool useBase,
331 int state)
332 {
333 wxVisualAttributes attr;
334 // NB: we need toplevel window so that GTK+ can find the right style
335 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
336 GtkWidget* widget = widget_new(NULL);
337 gtk_container_add(GTK_CONTAINER(wnd), widget);
338 attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state);
339 gtk_widget_destroy(wnd);
340 return attr;
341 }
342
343 #endif // wxUSE_CONTROLS
344