]>
Commit | Line | Data |
---|---|---|
c801d85f | 1 | ///////////////////////////////////////////////////////////////////////////// |
634fb750 VZ |
2 | // Name: src/gtk/control.cpp |
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" | |
b2ff89d6 | 19 | #include "wx/gtk/private.h" |
034be888 | 20 | |
634fb750 VZ |
21 | // ============================================================================ |
22 | // wxControl implementation | |
23 | // ============================================================================ | |
24 | ||
25 | // ---------------------------------------------------------------------------- | |
26 | // wxControl creation | |
27 | // ---------------------------------------------------------------------------- | |
c801d85f | 28 | |
9abe166a | 29 | IMPLEMENT_DYNAMIC_CLASS(wxControl, wxWindow) |
c801d85f | 30 | |
31528cd3 | 31 | wxControl::wxControl() |
c801d85f | 32 | { |
b292e2f5 | 33 | m_needParent = TRUE; |
6de97a3b | 34 | } |
c801d85f | 35 | |
04165bec | 36 | bool 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 |
53 | wxSize 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 | void wxControl::PostCreation(const wxSize& size) |
70 | { | |
71 | wxWindow::PostCreation(); | |
f40fdaa3 VS |
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); | |
b2ff89d6 | 79 | |
abdeb9e7 RD |
80 | ApplyWidgetStyle(); |
81 | SetInitialBestSize(size); | |
82 | } | |
83 | ||
b2ff89d6 VZ |
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); | |
abdeb9e7 | 108 | |
b2ff89d6 | 109 | gtk_label_set_text_with_mnemonic(w, wxGTK_CONV(labelGTK)); |
b2ff89d6 VZ |
110 | } |
111 | ||
2e1f5012 VZ |
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 | ||
b2ff89d6 VZ |
133 | void wxControl::GTKSetLabelForFrame(GtkFrame *w, const wxString& label) |
134 | { | |
2e1f5012 VZ |
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 | } | |
b2ff89d6 | 144 | |
2e1f5012 VZ |
145 | void wxControl::GTKFrameSetMnemonicWidget(GtkFrame* w, GtkWidget* widget) |
146 | { | |
147 | GtkLabel* labelwidget = GTK_LABEL(gtk_frame_get_label_widget(w)); | |
b2ff89d6 | 148 | |
2e1f5012 | 149 | gtk_label_set_mnemonic_widget(labelwidget, widget); |
b2ff89d6 VZ |
150 | } |
151 | ||
2e1f5012 | 152 | // ---------------------------------------------------------------------------- |
b2ff89d6 VZ |
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 | |
2e1f5012 VZ |
158 | // ---------------------------------------------------------------------------- |
159 | ||
b2ff89d6 | 160 | enum MnemonicsFlag |
eaafd2f8 | 161 | { |
b2ff89d6 VZ |
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++ ) | |
eaafd2f8 | 172 | { |
b2ff89d6 VZ |
173 | wxChar ch = label[i]; |
174 | ||
175 | switch ( ch ) | |
eaafd2f8 | 176 | { |
b2ff89d6 VZ |
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; | |
eaafd2f8 VS |
223 | } |
224 | } | |
b2ff89d6 VZ |
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); | |
eaafd2f8 VS |
239 | } |
240 | ||
b2ff89d6 VZ |
241 | // ---------------------------------------------------------------------------- |
242 | // wxControl styles (a.k.a. attributes) | |
243 | // ---------------------------------------------------------------------------- | |
9d522606 RD |
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; | |
b2ff89d6 | 274 | |
9d522606 RD |
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 | |
9d522606 | 289 | if ( !style->font_desc ) |
b2ff89d6 | 290 | style = gtk_widget_get_default_style(); |
9d522606 | 291 | if ( style && style->font_desc ) |
b2ff89d6 VZ |
292 | { |
293 | wxNativeFontInfo info; | |
fdf7514a | 294 | info.description = pango_font_description_copy(style->font_desc); |
b2ff89d6 VZ |
295 | attr.font = wxFont(info); |
296 | } | |
297 | else | |
298 | { | |
9d522606 RD |
299 | GtkSettings *settings = gtk_settings_get_default(); |
300 | gchar *font_name = NULL; | |
301 | g_object_get ( settings, | |
b2ff89d6 | 302 | "gtk-font-name", |
9d522606 RD |
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); | |
b2ff89d6 | 310 | } |
b2ff89d6 | 311 | |
9d522606 RD |
312 | return attr; |
313 | } | |
314 | ||
315 | ||
316 | //static | |
317 | wxVisualAttributes | |
865bb325 | 318 | wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNew_t widget_new, |
9d522606 RD |
319 | bool useBase, |
320 | int state) | |
321 | { | |
322 | wxVisualAttributes attr; | |
66d8fe77 VS |
323 | // NB: we need toplevel window so that GTK+ can find the right style |
324 | GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
9d522606 | 325 | GtkWidget* widget = widget_new(); |
66d8fe77 | 326 | gtk_container_add(GTK_CONTAINER(wnd), widget); |
9d522606 | 327 | attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state); |
66d8fe77 | 328 | gtk_widget_destroy(wnd); |
9d522606 RD |
329 | return attr; |
330 | } | |
331 | ||
332 | //static | |
333 | wxVisualAttributes | |
865bb325 | 334 | wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromStr_t widget_new, |
9d522606 RD |
335 | bool useBase, |
336 | int state) | |
337 | { | |
338 | wxVisualAttributes attr; | |
66d8fe77 VS |
339 | // NB: we need toplevel window so that GTK+ can find the right style |
340 | GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
9d522606 | 341 | GtkWidget* widget = widget_new(""); |
66d8fe77 | 342 | gtk_container_add(GTK_CONTAINER(wnd), widget); |
9d522606 | 343 | attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state); |
66d8fe77 | 344 | gtk_widget_destroy(wnd); |
9d522606 RD |
345 | return attr; |
346 | } | |
347 | ||
348 | ||
349 | //static | |
350 | wxVisualAttributes | |
865bb325 | 351 | wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromAdj_t widget_new, |
9d522606 RD |
352 | bool useBase, |
353 | int state) | |
354 | { | |
355 | wxVisualAttributes attr; | |
66d8fe77 VS |
356 | // NB: we need toplevel window so that GTK+ can find the right style |
357 | GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL); | |
9d522606 | 358 | GtkWidget* widget = widget_new(NULL); |
66d8fe77 | 359 | gtk_container_add(GTK_CONTAINER(wnd), widget); |
9d522606 | 360 | attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state); |
66d8fe77 | 361 | gtk_widget_destroy(wnd); |
9d522606 RD |
362 | return attr; |
363 | } | |
364 | ||
1e6feb95 VZ |
365 | #endif // wxUSE_CONTROLS |
366 |