]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/control.cpp
PRIOR/NEXT build fixes.
[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 void wxControl::PostCreation(const wxSize& size)
70 {
71 wxWindow::PostCreation();
72
73 // NB: GetBestSize needs to know the style, otherwise it will assume
74 // default font and if the user uses a different font, determined
75 // best size will be different (typically, smaller) than the desired
76 // size. This call ensure that a style is available at the time
77 // GetBestSize is called.
78 gtk_widget_ensure_style(m_widget);
79
80 ApplyWidgetStyle();
81 SetInitialBestSize(size);
82 }
83
84 // ----------------------------------------------------------------------------
85 // wxControl dealing with labels
86 // ----------------------------------------------------------------------------
87
88 void wxControl::SetLabel( const wxString &label )
89 {
90 // keep the original string internally to be able to return it later (for
91 // consistency with the other ports)
92 m_label = label;
93
94 InvalidateBestSize();
95 }
96
97 wxString wxControl::GetLabel() const
98 {
99 return m_label;
100 }
101
102 void wxControl::GTKSetLabelForLabel(GtkLabel *w, const wxString& label)
103 {
104 // don't call the virtual function which might call this one back again
105 wxControl::SetLabel(label);
106
107 const wxString labelGTK = GTKConvertMnemonics(label);
108
109 gtk_label_set_text_with_mnemonic(w, wxGTK_CONV(labelGTK));
110 }
111
112 // ----------------------------------------------------------------------------
113 // GtkFrame helpers
114 //
115 // GtkFrames do in fact support mnemonics in GTK2+ but not through
116 // gtk_frame_set_label, rather you need to use a custom label widget
117 // instead (idea gleaned from the native gtk font dialog code in GTK)
118 // ----------------------------------------------------------------------------
119
120 GtkWidget* wxControl::GTKCreateFrame(const wxString& label)
121 {
122 const wxString labelGTK = GTKConvertMnemonics(label);
123 GtkWidget* labelwidget = gtk_label_new_with_mnemonic(wxGTK_CONV(labelGTK));
124 gtk_widget_show(labelwidget); // without this it won't show...
125
126 GtkWidget* framewidget = gtk_frame_new(NULL);
127 gtk_frame_set_label_widget(GTK_FRAME(framewidget), labelwidget);
128
129 return framewidget; //note that the label is already set so you'll
130 //only need to call wxControl::SetLabel afterwards
131 }
132
133 void wxControl::GTKSetLabelForFrame(GtkFrame *w, const wxString& label)
134 {
135 GtkLabel* labelwidget = GTK_LABEL(gtk_frame_get_label_widget(w));
136 GTKSetLabelForLabel(labelwidget, label);
137 }
138
139 void wxControl::GTKFrameApplyWidgetStyle(GtkFrame* w, GtkRcStyle* style)
140 {
141 gtk_widget_modify_style(GTK_WIDGET(w), style);
142 gtk_widget_modify_style(gtk_frame_get_label_widget (w), style);
143 }
144
145 void wxControl::GTKFrameSetMnemonicWidget(GtkFrame* w, GtkWidget* widget)
146 {
147 GtkLabel* labelwidget = GTK_LABEL(gtk_frame_get_label_widget(w));
148
149 gtk_label_set_mnemonic_widget(labelwidget, widget);
150 }
151
152 // ----------------------------------------------------------------------------
153 // worker function implementing both GTKConvert/RemoveMnemonics()
154 //
155 // notice that under GTK+ 1 we only really need to support MNEMONICS_REMOVE as
156 // it doesn't support mnemonics anyhow but this would make the code so ugly
157 // that we do the same thing for GKT+ 1 and 2
158 // ----------------------------------------------------------------------------
159
160 enum MnemonicsFlag
161 {
162 MNEMONICS_REMOVE,
163 MNEMONICS_CONVERT
164 };
165
166 static wxString GTKProcessMnemonics(const wxString& label, MnemonicsFlag flag)
167 {
168 const size_t len = label.length();
169 wxString labelGTK;
170 labelGTK.reserve(len);
171 for ( size_t i = 0; i < len; i++ )
172 {
173 wxChar ch = label[i];
174
175 switch ( ch )
176 {
177 case wxT('&'):
178 if ( i == len - 1 )
179 {
180 // "&" at the end of string is an error
181 wxLogDebug(wxT("Invalid label \"%s\"."), label.c_str());
182 break;
183 }
184
185 ch = label[++i]; // skip '&' itself
186 switch ( ch )
187 {
188 case wxT('&'):
189 // special case: "&&" is not a mnemonic at all but just
190 // an escaped "&"
191 labelGTK += wxT('&');
192 break;
193
194 case wxT('_'):
195 if ( flag == MNEMONICS_CONVERT )
196 {
197 // '_' can't be a GTK mnemonic apparently so
198 // replace it with something similar
199 labelGTK += wxT("_-");
200 break;
201 }
202 //else: fall through
203
204 default:
205 if ( flag == MNEMONICS_CONVERT )
206 labelGTK += wxT('_');
207 labelGTK += ch;
208 }
209 break;
210
211 case wxT('_'):
212 if ( flag == MNEMONICS_CONVERT )
213 {
214 // escape any existing underlines in the string so that
215 // they don't become mnemonics accidentally
216 labelGTK += wxT("__");
217 break;
218 }
219 //else: fall through
220
221 default:
222 labelGTK += ch;
223 }
224 }
225
226 return labelGTK;
227 }
228
229 /* static */
230 wxString wxControl::GTKRemoveMnemonics(const wxString& label)
231 {
232 return GTKProcessMnemonics(label, MNEMONICS_REMOVE);
233 }
234
235 /* static */
236 wxString wxControl::GTKConvertMnemonics(const wxString& label)
237 {
238 return GTKProcessMnemonics(label, MNEMONICS_CONVERT);
239 }
240
241 // ----------------------------------------------------------------------------
242 // wxControl styles (a.k.a. attributes)
243 // ----------------------------------------------------------------------------
244
245 wxVisualAttributes wxControl::GetDefaultAttributes() const
246 {
247 return GetDefaultAttributesFromGTKWidget(m_widget,
248 UseGTKStyleBase());
249 }
250
251
252 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
253
254 // static
255 wxVisualAttributes
256 wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget* widget,
257 bool useBase,
258 int state)
259 {
260 GtkStyle* style;
261 wxVisualAttributes attr;
262
263 style = gtk_rc_get_style(widget);
264 if (!style)
265 style = gtk_widget_get_default_style();
266
267 if (!style)
268 {
269 return wxWindow::GetClassDefaultAttributes(wxWINDOW_VARIANT_NORMAL);
270 }
271
272 if (state == -1)
273 state = GTK_STATE_NORMAL;
274
275 // get the style's colours
276 attr.colFg = wxColour(style->fg[state].red >> SHIFT,
277 style->fg[state].green >> SHIFT,
278 style->fg[state].blue >> SHIFT);
279 if (useBase)
280 attr.colBg = wxColour(style->base[state].red >> SHIFT,
281 style->base[state].green >> SHIFT,
282 style->base[state].blue >> SHIFT);
283 else
284 attr.colBg = wxColour(style->bg[state].red >> SHIFT,
285 style->bg[state].green >> SHIFT,
286 style->bg[state].blue >> SHIFT);
287
288 // get the style's font
289 if ( !style->font_desc )
290 style = gtk_widget_get_default_style();
291 if ( style && style->font_desc )
292 {
293 wxNativeFontInfo info;
294 info.description = pango_font_description_copy(style->font_desc);
295 attr.font = wxFont(info);
296 }
297 else
298 {
299 GtkSettings *settings = gtk_settings_get_default();
300 gchar *font_name = NULL;
301 g_object_get ( settings,
302 "gtk-font-name",
303 &font_name,
304 NULL);
305 if (!font_name)
306 attr.font = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT );
307 else
308 attr.font = wxFont(wxString::FromAscii(font_name));
309 g_free (font_name);
310 }
311
312 return attr;
313 }
314
315
316 //static
317 wxVisualAttributes
318 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNew_t widget_new,
319 bool useBase,
320 int state)
321 {
322 wxVisualAttributes attr;
323 // NB: we need toplevel window so that GTK+ can find the right style
324 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
325 GtkWidget* widget = widget_new();
326 gtk_container_add(GTK_CONTAINER(wnd), widget);
327 attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state);
328 gtk_widget_destroy(wnd);
329 return attr;
330 }
331
332 //static
333 wxVisualAttributes
334 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromStr_t widget_new,
335 bool useBase,
336 int state)
337 {
338 wxVisualAttributes attr;
339 // NB: we need toplevel window so that GTK+ can find the right style
340 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
341 GtkWidget* widget = widget_new("");
342 gtk_container_add(GTK_CONTAINER(wnd), widget);
343 attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state);
344 gtk_widget_destroy(wnd);
345 return attr;
346 }
347
348
349 //static
350 wxVisualAttributes
351 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromAdj_t widget_new,
352 bool useBase,
353 int state)
354 {
355 wxVisualAttributes attr;
356 // NB: we need toplevel window so that GTK+ can find the right style
357 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
358 GtkWidget* widget = widget_new(NULL);
359 gtk_container_add(GTK_CONTAINER(wnd), widget);
360 attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state);
361 gtk_widget_destroy(wnd);
362 return attr;
363 }
364
365 #endif // wxUSE_CONTROLS
366