1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/control.cpp
3 // Purpose: wxControl implementation for wxGTK
4 // Author: Robert Roebling
6 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Vadim Zeitlin
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
15 #include "wx/control.h"
19 #include "wx/settings.h"
22 #include "wx/fontutil.h"
24 #include "wx/sysopt.h"
27 #include "wx/gtk/private.h"
28 #include "wx/gtk/private/mnemonics.h"
30 // ============================================================================
31 // wxControl implementation
32 // ============================================================================
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 IMPLEMENT_DYNAMIC_CLASS(wxControl
, wxWindow
)
40 wxControl::wxControl()
44 bool wxControl::Create( wxWindow
*parent
,
49 const wxValidator
& validator
,
50 const wxString
&name
)
52 bool ret
= wxWindow::Create(parent
, id
, pos
, size
, style
, name
);
55 SetValidator(validator
);
62 bool wxControl::SetFont(const wxFont
& font
)
64 const bool changed
= base_type::SetFont(font
);
65 if (changed
&& !gtk_widget_get_realized(m_widget
))
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");
76 wxSize
wxControl::DoGetBestSize() const
78 // Do not return any arbitrary default value...
79 wxASSERT_MSG( m_widget
, wxT("DoGetBestSize called before creation") );
84 // this is not a native control, size_request is likely to be (0,0)
85 best
= wxControlBase::DoGetBestSize();
91 if (gtk_widget_get_request_mode(m_widget
) != GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH
)
93 gtk_widget_get_preferred_height(m_widget
, NULL
, &req
.height
);
94 gtk_widget_get_preferred_width_for_height(m_widget
, req
.height
, NULL
, &req
.width
);
98 gtk_widget_get_preferred_width(m_widget
, NULL
, &req
.width
);
99 gtk_widget_get_preferred_height_for_width(m_widget
, req
.width
, NULL
, &req
.height
);
102 GTK_WIDGET_GET_CLASS(m_widget
)->size_request(m_widget
, &req
);
104 best
.Set(req
.width
, req
.height
);
110 void wxControl::PostCreation(const wxSize
& size
)
112 wxWindow::PostCreation();
115 // NB: GetBestSize needs to know the style, otherwise it will assume
116 // default font and if the user uses a different font, determined
117 // best size will be different (typically, smaller) than the desired
118 // size. This call ensure that a style is available at the time
119 // GetBestSize is called.
120 gtk_widget_ensure_style(m_widget
);
123 GTKApplyWidgetStyle();
124 SetInitialSize(size
);
127 // ----------------------------------------------------------------------------
128 // Work around a GTK+ bug whereby button is insensitive after being
130 // ----------------------------------------------------------------------------
132 // Fix sensitivity due to bug in GTK+ < 2.14
133 void wxControl::GTKFixSensitivity(bool WXUNUSED_IN_GTK3(onlyIfUnderMouse
))
136 if (gtk_check_version(2,14,0)
137 #if wxUSE_SYSTEM_OPTIONS
138 && (wxSystemOptions::GetOptionInt(wxT("gtk.control.disable-sensitivity-fix")) != 1)
142 wxPoint pt
= wxGetMousePosition();
143 wxRect
rect(ClientToScreen(wxPoint(0, 0)), GetSize());
144 if (!onlyIfUnderMouse
|| rect
.Contains(pt
))
153 // ----------------------------------------------------------------------------
154 // wxControl dealing with labels
155 // ----------------------------------------------------------------------------
157 void wxControl::GTKSetLabelForLabel(GtkLabel
*w
, const wxString
& label
)
159 const wxString labelGTK
= GTKConvertMnemonics(label
);
160 gtk_label_set_text_with_mnemonic(w
, wxGTK_CONV(labelGTK
));
165 void wxControl::GTKSetLabelWithMarkupForLabel(GtkLabel
*w
, const wxString
& label
)
167 const wxString labelGTK
= GTKConvertMnemonicsWithMarkup(label
);
168 gtk_label_set_markup_with_mnemonic(w
, wxGTK_CONV(labelGTK
));
171 #endif // wxUSE_MARKUP
173 // ----------------------------------------------------------------------------
176 // GtkFrames do in fact support mnemonics in GTK2+ but not through
177 // gtk_frame_set_label, rather you need to use a custom label widget
178 // instead (idea gleaned from the native gtk font dialog code in GTK)
179 // ----------------------------------------------------------------------------
181 GtkWidget
* wxControl::GTKCreateFrame(const wxString
& label
)
183 const wxString labelGTK
= GTKConvertMnemonics(label
);
184 GtkWidget
* labelwidget
= gtk_label_new_with_mnemonic(wxGTK_CONV(labelGTK
));
185 gtk_widget_show(labelwidget
); // without this it won't show...
187 GtkWidget
* framewidget
= gtk_frame_new(NULL
);
188 gtk_frame_set_label_widget(GTK_FRAME(framewidget
), labelwidget
);
190 return framewidget
; // note that the label is already set so you'll
191 // only need to call wxControl::SetLabel afterwards
194 void wxControl::GTKSetLabelForFrame(GtkFrame
*w
, const wxString
& label
)
196 wxControlBase::SetLabel(label
);
198 GtkLabel
* labelwidget
= GTK_LABEL(gtk_frame_get_label_widget(w
));
199 GTKSetLabelForLabel(labelwidget
, label
);
202 void wxControl::GTKFrameApplyWidgetStyle(GtkFrame
* w
, GtkRcStyle
* style
)
204 GTKApplyStyle(GTK_WIDGET(w
), style
);
205 GTKApplyStyle(gtk_frame_get_label_widget(w
), style
);
208 void wxControl::GTKFrameSetMnemonicWidget(GtkFrame
* w
, GtkWidget
* widget
)
210 GtkLabel
* labelwidget
= GTK_LABEL(gtk_frame_get_label_widget(w
));
212 gtk_label_set_mnemonic_widget(labelwidget
, widget
);
215 // ----------------------------------------------------------------------------
216 // worker function implementing GTK*Mnemonics() functions
217 // ----------------------------------------------------------------------------
220 wxString
wxControl::GTKRemoveMnemonics(const wxString
& label
)
222 return wxGTKRemoveMnemonics(label
);
226 wxString
wxControl::GTKConvertMnemonics(const wxString
& label
)
228 return wxConvertMnemonicsToGTK(label
);
232 wxString
wxControl::GTKConvertMnemonicsWithMarkup(const wxString
& label
)
234 return wxConvertMnemonicsToGTKMarkup(label
);
237 // ----------------------------------------------------------------------------
238 // wxControl styles (a.k.a. attributes)
239 // ----------------------------------------------------------------------------
241 wxVisualAttributes
wxControl::GetDefaultAttributes() const
243 return GetDefaultAttributesFromGTKWidget(m_widget
,
249 wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget
* widget
,
250 bool WXUNUSED_IN_GTK3(useBase
),
253 wxVisualAttributes attr
;
255 GtkStateFlags stateFlag
= GTK_STATE_FLAG_NORMAL
;
258 wxASSERT(state
== GTK_STATE_ACTIVE
);
259 stateFlag
= GTK_STATE_FLAG_ACTIVE
;
261 GtkStyleContext
* sc
= gtk_widget_get_style_context(widget
);
263 gtk_style_context_get_color(sc
, stateFlag
, &c
);
264 attr
.colFg
= wxColour(c
);
265 gtk_style_context_get_background_color(sc
, stateFlag
, &c
);
266 attr
.colBg
= wxColour(c
);
267 wxNativeFontInfo info
;
268 info
.description
= const_cast<PangoFontDescription
*>(gtk_style_context_get_font(sc
, stateFlag
));
269 attr
.font
= wxFont(info
);
270 info
.description
= NULL
;
274 style
= gtk_rc_get_style(widget
);
276 style
= gtk_widget_get_default_style();
280 return wxWindow::GetClassDefaultAttributes(wxWINDOW_VARIANT_NORMAL
);
283 // get the style's colours
284 attr
.colFg
= wxColour(style
->fg
[state
]);
286 attr
.colBg
= wxColour(style
->base
[state
]);
288 attr
.colBg
= wxColour(style
->bg
[state
]);
290 // get the style's font
291 if ( !style
->font_desc
)
292 style
= gtk_widget_get_default_style();
293 if ( style
&& style
->font_desc
)
295 wxNativeFontInfo info
;
296 info
.description
= style
->font_desc
;
297 attr
.font
= wxFont(info
);
298 info
.description
= NULL
;
301 if (!attr
.font
.IsOk())
303 GtkSettings
*settings
= gtk_settings_get_default();
304 gchar
*font_name
= NULL
;
305 g_object_get ( settings
,
310 attr
.font
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
312 attr
.font
= wxFont(wxString::FromAscii(font_name
));
322 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNew_t widget_new
,
326 wxVisualAttributes attr
;
327 // NB: we need toplevel window so that GTK+ can find the right style
328 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
329 GtkWidget
* widget
= widget_new();
330 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
331 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
332 gtk_widget_destroy(wnd
);
338 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromStr_t widget_new
,
342 wxVisualAttributes attr
;
343 // NB: we need toplevel window so that GTK+ can find the right style
344 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
345 GtkWidget
* widget
= widget_new("");
346 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
347 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
348 gtk_widget_destroy(wnd
);
355 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromAdj_t widget_new
,
359 wxVisualAttributes attr
;
360 // NB: we need toplevel window so that GTK+ can find the right style
361 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
362 GtkWidget
* widget
= widget_new(NULL
);
363 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
364 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
365 gtk_widget_destroy(wnd
);
369 #endif // wxUSE_CONTROLS