]>
git.saurik.com Git - wxWidgets.git/blob - src/gtk/control.cpp
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();
89 best
= GTKGetPreferredSize(m_widget
);
95 void wxControl::PostCreation(const wxSize
& size
)
97 wxWindow::PostCreation();
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
);
108 GTKApplyWidgetStyle();
109 SetInitialSize(size
);
112 // ----------------------------------------------------------------------------
113 // Work around a GTK+ bug whereby button is insensitive after being
115 // ----------------------------------------------------------------------------
117 // Fix sensitivity due to bug in GTK+ < 2.14
118 void wxControl::GTKFixSensitivity(bool WXUNUSED_IN_GTK3(onlyIfUnderMouse
))
121 if (gtk_check_version(2,14,0)
122 #if wxUSE_SYSTEM_OPTIONS
123 && (wxSystemOptions::GetOptionInt(wxT("gtk.control.disable-sensitivity-fix")) != 1)
127 if (!onlyIfUnderMouse
|| GetScreenRect().Contains(wxGetMousePosition()))
136 // ----------------------------------------------------------------------------
137 // wxControl dealing with labels
138 // ----------------------------------------------------------------------------
140 void wxControl::GTKSetLabelForLabel(GtkLabel
*w
, const wxString
& label
)
142 const wxString labelGTK
= GTKConvertMnemonics(label
);
143 gtk_label_set_text_with_mnemonic(w
, wxGTK_CONV(labelGTK
));
148 void wxControl::GTKSetLabelWithMarkupForLabel(GtkLabel
*w
, const wxString
& label
)
150 const wxString labelGTK
= GTKConvertMnemonicsWithMarkup(label
);
151 gtk_label_set_markup_with_mnemonic(w
, wxGTK_CONV(labelGTK
));
154 #endif // wxUSE_MARKUP
156 // ----------------------------------------------------------------------------
159 // GtkFrames do in fact support mnemonics in GTK2+ but not through
160 // gtk_frame_set_label, rather you need to use a custom label widget
161 // instead (idea gleaned from the native gtk font dialog code in GTK)
162 // ----------------------------------------------------------------------------
164 GtkWidget
* wxControl::GTKCreateFrame(const wxString
& label
)
166 const wxString labelGTK
= GTKConvertMnemonics(label
);
167 GtkWidget
* labelwidget
= gtk_label_new_with_mnemonic(wxGTK_CONV(labelGTK
));
168 gtk_widget_show(labelwidget
); // without this it won't show...
170 GtkWidget
* framewidget
= gtk_frame_new(NULL
);
171 gtk_frame_set_label_widget(GTK_FRAME(framewidget
), labelwidget
);
173 return framewidget
; // note that the label is already set so you'll
174 // only need to call wxControl::SetLabel afterwards
177 void wxControl::GTKSetLabelForFrame(GtkFrame
*w
, const wxString
& label
)
179 wxControlBase::SetLabel(label
);
181 GtkLabel
* labelwidget
= GTK_LABEL(gtk_frame_get_label_widget(w
));
182 GTKSetLabelForLabel(labelwidget
, label
);
185 void wxControl::GTKFrameApplyWidgetStyle(GtkFrame
* w
, GtkRcStyle
* style
)
187 GTKApplyStyle(GTK_WIDGET(w
), style
);
188 GTKApplyStyle(gtk_frame_get_label_widget(w
), style
);
191 void wxControl::GTKFrameSetMnemonicWidget(GtkFrame
* w
, GtkWidget
* widget
)
193 GtkLabel
* labelwidget
= GTK_LABEL(gtk_frame_get_label_widget(w
));
195 gtk_label_set_mnemonic_widget(labelwidget
, widget
);
198 // ----------------------------------------------------------------------------
199 // worker function implementing GTK*Mnemonics() functions
200 // ----------------------------------------------------------------------------
203 wxString
wxControl::GTKRemoveMnemonics(const wxString
& label
)
205 return wxGTKRemoveMnemonics(label
);
209 wxString
wxControl::GTKConvertMnemonics(const wxString
& label
)
211 return wxConvertMnemonicsToGTK(label
);
215 wxString
wxControl::GTKConvertMnemonicsWithMarkup(const wxString
& label
)
217 return wxConvertMnemonicsToGTKMarkup(label
);
220 // ----------------------------------------------------------------------------
221 // wxControl styles (a.k.a. attributes)
222 // ----------------------------------------------------------------------------
224 wxVisualAttributes
wxControl::GetDefaultAttributes() const
226 return GetDefaultAttributesFromGTKWidget(m_widget
,
232 wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget
* widget
,
233 bool WXUNUSED_IN_GTK3(useBase
),
236 wxVisualAttributes attr
;
238 GtkWidget
* tlw
= NULL
;
239 if (gtk_widget_get_parent(widget
) == NULL
)
241 tlw
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
242 gtk_container_add(GTK_CONTAINER(tlw
), widget
);
246 GtkStateFlags stateFlag
= GTK_STATE_FLAG_NORMAL
;
249 wxASSERT(state
== GTK_STATE_ACTIVE
);
250 stateFlag
= GTK_STATE_FLAG_ACTIVE
;
252 GtkStyleContext
* sc
= gtk_widget_get_style_context(widget
);
254 gtk_style_context_get_color(sc
, stateFlag
, &c
);
255 attr
.colFg
= wxColour(c
);
256 gtk_style_context_get_background_color(sc
, stateFlag
, &c
);
257 attr
.colBg
= wxColour(c
);
258 wxNativeFontInfo info
;
259 info
.description
= const_cast<PangoFontDescription
*>(gtk_style_context_get_font(sc
, stateFlag
));
260 attr
.font
= wxFont(info
);
261 info
.description
= NULL
;
265 style
= gtk_rc_get_style(widget
);
267 style
= gtk_widget_get_default_style();
271 // get the style's colours
272 attr
.colFg
= wxColour(style
->fg
[state
]);
274 attr
.colBg
= wxColour(style
->base
[state
]);
276 attr
.colBg
= wxColour(style
->bg
[state
]);
278 // get the style's font
279 if (!style
->font_desc
)
280 style
= gtk_widget_get_default_style();
281 if (style
&& style
->font_desc
)
283 wxNativeFontInfo info
;
284 info
.description
= style
->font_desc
;
285 attr
.font
= wxFont(info
);
286 info
.description
= NULL
;
290 attr
= wxWindow::GetClassDefaultAttributes(wxWINDOW_VARIANT_NORMAL
);
293 if (!attr
.font
.IsOk())
295 GtkSettings
*settings
= gtk_settings_get_default();
296 gchar
*font_name
= NULL
;
297 g_object_get ( settings
,
302 attr
.font
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
304 attr
.font
= wxFont(wxString::FromAscii(font_name
));
309 gtk_widget_destroy(tlw
);
314 // This is not the same as GetBestSize() because that size may have
315 // been recalculated and cached by us. We want GTK+ information.
316 wxSize
wxControl::GTKGetPreferredSize(GtkWidget
* widget
) const
320 gtk_widget_get_preferred_size(widget
, NULL
, &req
);
322 GTK_WIDGET_GET_CLASS(widget
)->size_request(widget
, &req
);
325 return wxSize(req
.width
, req
.height
);
328 wxPoint
wxControl::GTKGetEntryMargins(GtkEntry
* entry
) const
333 #if GTK_CHECK_VERSION(2,10,0)
334 // The margins we have previously set
335 const GtkBorder
* border
= gtk_entry_get_inner_border(entry
);
338 marg
.x
= border
->left
+ border
->right
;
339 marg
.y
= border
->top
+ border
->bottom
;
343 // Gtk3 does not use inner border, but StyleContext and CSS
344 // TODO: implement it, starting with wxTextEntry::DoSetMargins()
348 gtk_entry_get_layout_offsets(entry
, &x
, &y
);
349 // inner borders are included. Substract them so we can get other margins
359 #endif // wxUSE_CONTROLS