]>
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 SetInitialSize(size
);
111 // ----------------------------------------------------------------------------
112 // Work around a GTK+ bug whereby button is insensitive after being
114 // ----------------------------------------------------------------------------
116 // Fix sensitivity due to bug in GTK+ < 2.14
117 void wxControl::GTKFixSensitivity(bool WXUNUSED_IN_GTK3(onlyIfUnderMouse
))
120 if (gtk_check_version(2,14,0)
121 #if wxUSE_SYSTEM_OPTIONS
122 && (wxSystemOptions::GetOptionInt(wxT("gtk.control.disable-sensitivity-fix")) != 1)
126 if (!onlyIfUnderMouse
|| GetScreenRect().Contains(wxGetMousePosition()))
135 // ----------------------------------------------------------------------------
136 // wxControl dealing with labels
137 // ----------------------------------------------------------------------------
139 void wxControl::GTKSetLabelForLabel(GtkLabel
*w
, const wxString
& label
)
141 const wxString labelGTK
= GTKConvertMnemonics(label
);
142 gtk_label_set_text_with_mnemonic(w
, wxGTK_CONV(labelGTK
));
147 void wxControl::GTKSetLabelWithMarkupForLabel(GtkLabel
*w
, const wxString
& label
)
149 const wxString labelGTK
= GTKConvertMnemonicsWithMarkup(label
);
150 gtk_label_set_markup_with_mnemonic(w
, wxGTK_CONV(labelGTK
));
153 #endif // wxUSE_MARKUP
155 // ----------------------------------------------------------------------------
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 // ----------------------------------------------------------------------------
163 GtkWidget
* wxControl::GTKCreateFrame(const wxString
& label
)
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...
169 GtkWidget
* framewidget
= gtk_frame_new(NULL
);
170 gtk_frame_set_label_widget(GTK_FRAME(framewidget
), labelwidget
);
172 return framewidget
; // note that the label is already set so you'll
173 // only need to call wxControl::SetLabel afterwards
176 void wxControl::GTKSetLabelForFrame(GtkFrame
*w
, const wxString
& label
)
178 wxControlBase::SetLabel(label
);
180 GtkLabel
* labelwidget
= GTK_LABEL(gtk_frame_get_label_widget(w
));
181 GTKSetLabelForLabel(labelwidget
, label
);
184 void wxControl::GTKFrameApplyWidgetStyle(GtkFrame
* w
, GtkRcStyle
* style
)
186 GTKApplyStyle(GTK_WIDGET(w
), style
);
187 GTKApplyStyle(gtk_frame_get_label_widget(w
), style
);
190 void wxControl::GTKFrameSetMnemonicWidget(GtkFrame
* w
, GtkWidget
* widget
)
192 GtkLabel
* labelwidget
= GTK_LABEL(gtk_frame_get_label_widget(w
));
194 gtk_label_set_mnemonic_widget(labelwidget
, widget
);
197 // ----------------------------------------------------------------------------
198 // worker function implementing GTK*Mnemonics() functions
199 // ----------------------------------------------------------------------------
202 wxString
wxControl::GTKRemoveMnemonics(const wxString
& label
)
204 return wxGTKRemoveMnemonics(label
);
208 wxString
wxControl::GTKConvertMnemonics(const wxString
& label
)
210 return wxConvertMnemonicsToGTK(label
);
214 wxString
wxControl::GTKConvertMnemonicsWithMarkup(const wxString
& label
)
216 return wxConvertMnemonicsToGTKMarkup(label
);
219 // ----------------------------------------------------------------------------
220 // wxControl styles (a.k.a. attributes)
221 // ----------------------------------------------------------------------------
223 wxVisualAttributes
wxControl::GetDefaultAttributes() const
225 return GetDefaultAttributesFromGTKWidget(m_widget
,
231 wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget
* widget
,
232 bool WXUNUSED_IN_GTK3(useBase
),
235 wxVisualAttributes attr
;
237 GtkWidget
* tlw
= NULL
;
238 if (gtk_widget_get_parent(widget
) == NULL
)
240 tlw
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
241 gtk_container_add(GTK_CONTAINER(tlw
), widget
);
245 GtkStateFlags stateFlag
= GTK_STATE_FLAG_NORMAL
;
248 wxASSERT(state
== GTK_STATE_ACTIVE
);
249 stateFlag
= GTK_STATE_FLAG_ACTIVE
;
251 GtkStyleContext
* sc
= gtk_widget_get_style_context(widget
);
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
;
264 style
= gtk_rc_get_style(widget
);
266 style
= gtk_widget_get_default_style();
270 // get the style's colours
271 attr
.colFg
= wxColour(style
->fg
[state
]);
273 attr
.colBg
= wxColour(style
->base
[state
]);
275 attr
.colBg
= wxColour(style
->bg
[state
]);
277 // get the style's font
278 if (!style
->font_desc
)
279 style
= gtk_widget_get_default_style();
280 if (style
&& style
->font_desc
)
282 wxNativeFontInfo info
;
283 info
.description
= style
->font_desc
;
284 attr
.font
= wxFont(info
);
285 info
.description
= NULL
;
289 attr
= wxWindow::GetClassDefaultAttributes(wxWINDOW_VARIANT_NORMAL
);
292 if (!attr
.font
.IsOk())
294 GtkSettings
*settings
= gtk_settings_get_default();
295 gchar
*font_name
= NULL
;
296 g_object_get ( settings
,
301 attr
.font
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
303 attr
.font
= wxFont(wxString::FromAscii(font_name
));
308 gtk_widget_destroy(tlw
);
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
319 gtk_widget_get_preferred_size(widget
, NULL
, &req
);
321 GTK_WIDGET_GET_CLASS(widget
)->size_request(widget
, &req
);
324 return wxSize(req
.width
, req
.height
);
327 wxPoint
wxControl::GTKGetEntryMargins(GtkEntry
* entry
) const
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
);
337 marg
.x
= border
->left
+ border
->right
;
338 marg
.y
= border
->top
+ border
->bottom
;
342 // Gtk3 does not use inner border, but StyleContext and CSS
343 // TODO: implement it, starting with wxTextEntry::DoSetMargins()
347 gtk_entry_get_layout_offsets(entry
, &x
, &y
);
348 // inner borders are included. Substract them so we can get other margins
358 #endif // wxUSE_CONTROLS