]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/control.cpp
attempt to get the 'new focus' window parameter of a focus kill event set correctly
[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/control.h"
16
17 #ifndef WX_PRECOMP
18 #include "wx/log.h"
19 #include "wx/settings.h"
20 #endif
21
22 #include "wx/fontutil.h"
23 #include "wx/utils.h"
24 #include "wx/sysopt.h"
25
26 #include <gtk/gtk.h>
27 #include "wx/gtk/private.h"
28 #include "wx/gtk/private/mnemonics.h"
29
30 // ============================================================================
31 // wxControl implementation
32 // ============================================================================
33
34 // ----------------------------------------------------------------------------
35 // wxControl creation
36 // ----------------------------------------------------------------------------
37
38 IMPLEMENT_DYNAMIC_CLASS(wxControl, wxWindow)
39
40 wxControl::wxControl()
41 {
42 }
43
44 bool wxControl::Create( wxWindow *parent,
45 wxWindowID id,
46 const wxPoint &pos,
47 const wxSize &size,
48 long style,
49 const wxValidator& validator,
50 const wxString &name )
51 {
52 bool ret = wxWindow::Create(parent, id, pos, size, style, name);
53
54 #if wxUSE_VALIDATORS
55 SetValidator(validator);
56 #endif
57
58 return ret;
59 }
60
61 #ifdef __WXGTK3__
62 bool wxControl::SetFont(const wxFont& font)
63 {
64 const bool changed = base_type::SetFont(font);
65 if (changed && !gtk_widget_get_realized(m_widget))
66 {
67 // GTK defers sending "style-updated" until widget is realized, but
68 // GetBestSize() won't compute correct result until the signal is sent,
69 // so we have to do it now
70 g_signal_emit_by_name(m_widget, "style-updated");
71 }
72 return changed;
73 }
74 #endif
75
76 wxSize wxControl::DoGetBestSize() const
77 {
78 // Do not return any arbitrary default value...
79 wxASSERT_MSG( m_widget, wxT("DoGetBestSize called before creation") );
80
81 wxSize best;
82 if (m_wxwindow)
83 {
84 // this is not a native control, size_request is likely to be (0,0)
85 best = wxControlBase::DoGetBestSize();
86 }
87 else
88 {
89 best = GTKGetPreferredSize(m_widget);
90 }
91
92 return best;
93 }
94
95 void wxControl::PostCreation(const wxSize& size)
96 {
97 wxWindow::PostCreation();
98
99 #ifndef __WXGTK3__
100 // NB: GetBestSize needs to know the style, otherwise it will assume
101 // default font and if the user uses a different font, determined
102 // best size will be different (typically, smaller) than the desired
103 // size. This call ensure that a style is available at the time
104 // GetBestSize is called.
105 gtk_widget_ensure_style(m_widget);
106 #endif
107
108 SetInitialSize(size);
109 }
110
111 // ----------------------------------------------------------------------------
112 // Work around a GTK+ bug whereby button is insensitive after being
113 // enabled
114 // ----------------------------------------------------------------------------
115
116 // Fix sensitivity due to bug in GTK+ < 2.14
117 void wxControl::GTKFixSensitivity(bool WXUNUSED_IN_GTK3(onlyIfUnderMouse))
118 {
119 #ifndef __WXGTK3__
120 if (gtk_check_version(2,14,0)
121 #if wxUSE_SYSTEM_OPTIONS
122 && (wxSystemOptions::GetOptionInt(wxT("gtk.control.disable-sensitivity-fix")) != 1)
123 #endif
124 )
125 {
126 if (!onlyIfUnderMouse || GetScreenRect().Contains(wxGetMousePosition()))
127 {
128 Hide();
129 Show();
130 }
131 }
132 #endif
133 }
134
135 // ----------------------------------------------------------------------------
136 // wxControl dealing with labels
137 // ----------------------------------------------------------------------------
138
139 void wxControl::GTKSetLabelForLabel(GtkLabel *w, const wxString& label)
140 {
141 const wxString labelGTK = GTKConvertMnemonics(label);
142 gtk_label_set_text_with_mnemonic(w, wxGTK_CONV(labelGTK));
143 }
144
145 #if wxUSE_MARKUP
146
147 void wxControl::GTKSetLabelWithMarkupForLabel(GtkLabel *w, const wxString& label)
148 {
149 const wxString labelGTK = GTKConvertMnemonicsWithMarkup(label);
150 gtk_label_set_markup_with_mnemonic(w, wxGTK_CONV(labelGTK));
151 }
152
153 #endif // wxUSE_MARKUP
154
155 // ----------------------------------------------------------------------------
156 // GtkFrame helpers
157 //
158 // GtkFrames do in fact support mnemonics in GTK2+ but not through
159 // gtk_frame_set_label, rather you need to use a custom label widget
160 // instead (idea gleaned from the native gtk font dialog code in GTK)
161 // ----------------------------------------------------------------------------
162
163 GtkWidget* wxControl::GTKCreateFrame(const wxString& label)
164 {
165 const wxString labelGTK = GTKConvertMnemonics(label);
166 GtkWidget* labelwidget = gtk_label_new_with_mnemonic(wxGTK_CONV(labelGTK));
167 gtk_widget_show(labelwidget); // without this it won't show...
168
169 GtkWidget* framewidget = gtk_frame_new(NULL);
170 gtk_frame_set_label_widget(GTK_FRAME(framewidget), labelwidget);
171
172 return framewidget; // note that the label is already set so you'll
173 // only need to call wxControl::SetLabel afterwards
174 }
175
176 void wxControl::GTKSetLabelForFrame(GtkFrame *w, const wxString& label)
177 {
178 wxControlBase::SetLabel(label);
179
180 GtkLabel* labelwidget = GTK_LABEL(gtk_frame_get_label_widget(w));
181 GTKSetLabelForLabel(labelwidget, label);
182 }
183
184 void wxControl::GTKFrameApplyWidgetStyle(GtkFrame* w, GtkRcStyle* style)
185 {
186 GTKApplyStyle(GTK_WIDGET(w), style);
187 GTKApplyStyle(gtk_frame_get_label_widget(w), style);
188 }
189
190 void wxControl::GTKFrameSetMnemonicWidget(GtkFrame* w, GtkWidget* widget)
191 {
192 GtkLabel* labelwidget = GTK_LABEL(gtk_frame_get_label_widget(w));
193
194 gtk_label_set_mnemonic_widget(labelwidget, widget);
195 }
196
197 // ----------------------------------------------------------------------------
198 // worker function implementing GTK*Mnemonics() functions
199 // ----------------------------------------------------------------------------
200
201 /* static */
202 wxString wxControl::GTKRemoveMnemonics(const wxString& label)
203 {
204 return wxGTKRemoveMnemonics(label);
205 }
206
207 /* static */
208 wxString wxControl::GTKConvertMnemonics(const wxString& label)
209 {
210 return wxConvertMnemonicsToGTK(label);
211 }
212
213 /* static */
214 wxString wxControl::GTKConvertMnemonicsWithMarkup(const wxString& label)
215 {
216 return wxConvertMnemonicsToGTKMarkup(label);
217 }
218
219 // ----------------------------------------------------------------------------
220 // wxControl styles (a.k.a. attributes)
221 // ----------------------------------------------------------------------------
222
223 wxVisualAttributes wxControl::GetDefaultAttributes() const
224 {
225 return GetDefaultAttributesFromGTKWidget(m_widget,
226 UseGTKStyleBase());
227 }
228
229 // static
230 wxVisualAttributes
231 wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget* widget,
232 bool WXUNUSED_IN_GTK3(useBase),
233 int state)
234 {
235 wxVisualAttributes attr;
236
237 GtkWidget* tlw = NULL;
238 if (gtk_widget_get_parent(widget) == NULL)
239 {
240 tlw = gtk_window_new(GTK_WINDOW_TOPLEVEL);
241 gtk_container_add(GTK_CONTAINER(tlw), widget);
242 }
243
244 #ifdef __WXGTK3__
245 GtkStateFlags stateFlag = GTK_STATE_FLAG_NORMAL;
246 if (state)
247 {
248 wxASSERT(state == GTK_STATE_ACTIVE);
249 stateFlag = GTK_STATE_FLAG_ACTIVE;
250 }
251 GtkStyleContext* sc = gtk_widget_get_style_context(widget);
252 GdkRGBA c;
253 gtk_style_context_get_color(sc, stateFlag, &c);
254 attr.colFg = wxColour(c);
255 gtk_style_context_get_background_color(sc, stateFlag, &c);
256 attr.colBg = wxColour(c);
257 wxNativeFontInfo info;
258 info.description = const_cast<PangoFontDescription*>(gtk_style_context_get_font(sc, stateFlag));
259 attr.font = wxFont(info);
260 info.description = NULL;
261 #else
262 GtkStyle* style;
263
264 style = gtk_rc_get_style(widget);
265 if (!style)
266 style = gtk_widget_get_default_style();
267
268 if (style)
269 {
270 // get the style's colours
271 attr.colFg = wxColour(style->fg[state]);
272 if (useBase)
273 attr.colBg = wxColour(style->base[state]);
274 else
275 attr.colBg = wxColour(style->bg[state]);
276
277 // get the style's font
278 if (!style->font_desc)
279 style = gtk_widget_get_default_style();
280 if (style && style->font_desc)
281 {
282 wxNativeFontInfo info;
283 info.description = style->font_desc;
284 attr.font = wxFont(info);
285 info.description = NULL;
286 }
287 }
288 else
289 attr = wxWindow::GetClassDefaultAttributes(wxWINDOW_VARIANT_NORMAL);
290 #endif
291
292 if (!attr.font.IsOk())
293 {
294 GtkSettings *settings = gtk_settings_get_default();
295 gchar *font_name = NULL;
296 g_object_get ( settings,
297 "gtk-font-name",
298 &font_name,
299 NULL);
300 if (!font_name)
301 attr.font = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT );
302 else
303 attr.font = wxFont(wxString::FromAscii(font_name));
304 g_free (font_name);
305 }
306
307 if (tlw)
308 gtk_widget_destroy(tlw);
309
310 return attr;
311 }
312
313 // This is not the same as GetBestSize() because that size may have
314 // been recalculated and cached by us. We want GTK+ information.
315 wxSize wxControl::GTKGetPreferredSize(GtkWidget* widget) const
316 {
317 GtkRequisition req;
318 #ifdef __WXGTK3__
319 gtk_widget_get_preferred_size(widget, NULL, &req);
320 #else
321 GTK_WIDGET_GET_CLASS(widget)->size_request(widget, &req);
322 #endif
323
324 return wxSize(req.width, req.height);
325 }
326
327 wxPoint wxControl::GTKGetEntryMargins(GtkEntry* entry) const
328 {
329 wxPoint marg(0, 0);
330
331 #ifndef __WXGTK3__
332 #if GTK_CHECK_VERSION(2,10,0)
333 // The margins we have previously set
334 const GtkBorder* border = gtk_entry_get_inner_border(entry);
335 if ( border )
336 {
337 marg.x = border->left + border->right;
338 marg.y = border->top + border->bottom;
339 }
340 #endif // GTK+ 2.10+
341 #else // GTK+ 3
342 // Gtk3 does not use inner border, but StyleContext and CSS
343 // TODO: implement it, starting with wxTextEntry::DoSetMargins()
344 #endif // GTK+ 2/3
345
346 int x, y;
347 gtk_entry_get_layout_offsets(entry, &x, &y);
348 // inner borders are included. Substract them so we can get other margins
349 x -= marg.x;
350 y -= marg.y;
351 marg.x += 2 * x + 2;
352 marg.y += 2 * y + 2;
353
354 return marg;
355 }
356
357
358 #endif // wxUSE_CONTROLS