]>
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
5 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Vadim Zeitlin
6 // Licence: wxWindows licence
7 /////////////////////////////////////////////////////////////////////////////
9 // For compilers that support precompilation, includes "wx.h".
10 #include "wx/wxprec.h"
14 #include "wx/control.h"
18 #include "wx/settings.h"
21 #include "wx/fontutil.h"
23 #include "wx/sysopt.h"
26 #include "wx/gtk/private.h"
27 #include "wx/gtk/private/mnemonics.h"
29 // ============================================================================
30 // wxControl implementation
31 // ============================================================================
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
37 IMPLEMENT_DYNAMIC_CLASS(wxControl
, wxWindow
)
39 wxControl::wxControl()
43 bool wxControl::Create( wxWindow
*parent
,
48 const wxValidator
& validator
,
49 const wxString
&name
)
51 bool ret
= wxWindow::Create(parent
, id
, pos
, size
, style
, name
);
54 SetValidator(validator
);
61 bool wxControl::SetFont(const wxFont
& font
)
63 const bool changed
= base_type::SetFont(font
);
64 if (changed
&& !gtk_widget_get_realized(m_widget
))
66 // GTK defers sending "style-updated" until widget is realized, but
67 // GetBestSize() won't compute correct result until the signal is sent,
68 // so we have to do it now
69 g_signal_emit_by_name(m_widget
, "style-updated");
75 wxSize
wxControl::DoGetBestSize() const
77 // Do not return any arbitrary default value...
78 wxASSERT_MSG( m_widget
, wxT("DoGetBestSize called before creation") );
83 // this is not a native control, size_request is likely to be (0,0)
84 best
= wxControlBase::DoGetBestSize();
88 best
= GTKGetPreferredSize(m_widget
);
94 void wxControl::PostCreation(const wxSize
& size
)
96 wxWindow::PostCreation();
99 // NB: GetBestSize needs to know the style, otherwise it will assume
100 // default font and if the user uses a different font, determined
101 // best size will be different (typically, smaller) than the desired
102 // size. This call ensure that a style is available at the time
103 // GetBestSize is called.
104 gtk_widget_ensure_style(m_widget
);
107 SetInitialSize(size
);
110 // ----------------------------------------------------------------------------
111 // Work around a GTK+ bug whereby button is insensitive after being
113 // ----------------------------------------------------------------------------
115 // Fix sensitivity due to bug in GTK+ < 2.14
116 void wxControl::GTKFixSensitivity(bool WXUNUSED_IN_GTK3(onlyIfUnderMouse
))
119 if (gtk_check_version(2,14,0)
120 #if wxUSE_SYSTEM_OPTIONS
121 && (wxSystemOptions::GetOptionInt(wxT("gtk.control.disable-sensitivity-fix")) != 1)
125 if (!onlyIfUnderMouse
|| GetScreenRect().Contains(wxGetMousePosition()))
134 // ----------------------------------------------------------------------------
135 // wxControl dealing with labels
136 // ----------------------------------------------------------------------------
138 void wxControl::GTKSetLabelForLabel(GtkLabel
*w
, const wxString
& label
)
140 const wxString labelGTK
= GTKConvertMnemonics(label
);
141 gtk_label_set_text_with_mnemonic(w
, wxGTK_CONV(labelGTK
));
146 void wxControl::GTKSetLabelWithMarkupForLabel(GtkLabel
*w
, const wxString
& label
)
148 const wxString labelGTK
= GTKConvertMnemonicsWithMarkup(label
);
149 gtk_label_set_markup_with_mnemonic(w
, wxGTK_CONV(labelGTK
));
152 #endif // wxUSE_MARKUP
154 // ----------------------------------------------------------------------------
157 // GtkFrames do in fact support mnemonics in GTK2+ but not through
158 // gtk_frame_set_label, rather you need to use a custom label widget
159 // instead (idea gleaned from the native gtk font dialog code in GTK)
160 // ----------------------------------------------------------------------------
162 GtkWidget
* wxControl::GTKCreateFrame(const wxString
& label
)
164 const wxString labelGTK
= GTKConvertMnemonics(label
);
165 GtkWidget
* labelwidget
= gtk_label_new_with_mnemonic(wxGTK_CONV(labelGTK
));
166 gtk_widget_show(labelwidget
); // without this it won't show...
168 GtkWidget
* framewidget
= gtk_frame_new(NULL
);
169 gtk_frame_set_label_widget(GTK_FRAME(framewidget
), labelwidget
);
171 return framewidget
; // note that the label is already set so you'll
172 // only need to call wxControl::SetLabel afterwards
175 void wxControl::GTKSetLabelForFrame(GtkFrame
*w
, const wxString
& label
)
177 wxControlBase::SetLabel(label
);
179 GtkLabel
* labelwidget
= GTK_LABEL(gtk_frame_get_label_widget(w
));
180 GTKSetLabelForLabel(labelwidget
, label
);
183 void wxControl::GTKFrameApplyWidgetStyle(GtkFrame
* w
, GtkRcStyle
* style
)
185 GTKApplyStyle(GTK_WIDGET(w
), style
);
186 GTKApplyStyle(gtk_frame_get_label_widget(w
), style
);
189 void wxControl::GTKFrameSetMnemonicWidget(GtkFrame
* w
, GtkWidget
* widget
)
191 GtkLabel
* labelwidget
= GTK_LABEL(gtk_frame_get_label_widget(w
));
193 gtk_label_set_mnemonic_widget(labelwidget
, widget
);
196 // ----------------------------------------------------------------------------
197 // worker function implementing GTK*Mnemonics() functions
198 // ----------------------------------------------------------------------------
201 wxString
wxControl::GTKRemoveMnemonics(const wxString
& label
)
203 return wxGTKRemoveMnemonics(label
);
207 wxString
wxControl::GTKConvertMnemonics(const wxString
& label
)
209 return wxConvertMnemonicsToGTK(label
);
213 wxString
wxControl::GTKConvertMnemonicsWithMarkup(const wxString
& label
)
215 return wxConvertMnemonicsToGTKMarkup(label
);
218 // ----------------------------------------------------------------------------
219 // wxControl styles (a.k.a. attributes)
220 // ----------------------------------------------------------------------------
222 wxVisualAttributes
wxControl::GetDefaultAttributes() const
224 return GetDefaultAttributesFromGTKWidget(m_widget
,
230 wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget
* widget
,
231 bool WXUNUSED_IN_GTK3(useBase
),
234 wxVisualAttributes attr
;
236 GtkWidget
* tlw
= NULL
;
237 if (gtk_widget_get_parent(widget
) == NULL
)
239 tlw
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
240 gtk_container_add(GTK_CONTAINER(tlw
), widget
);
244 GtkStateFlags stateFlag
= GTK_STATE_FLAG_NORMAL
;
247 wxASSERT(state
== GTK_STATE_ACTIVE
);
248 stateFlag
= GTK_STATE_FLAG_ACTIVE
;
250 GtkStyleContext
* sc
= gtk_widget_get_style_context(widget
);
252 gtk_style_context_get_color(sc
, stateFlag
, &c
);
253 attr
.colFg
= wxColour(c
);
254 gtk_style_context_get_background_color(sc
, stateFlag
, &c
);
255 attr
.colBg
= wxColour(c
);
256 wxNativeFontInfo info
;
257 info
.description
= const_cast<PangoFontDescription
*>(gtk_style_context_get_font(sc
, stateFlag
));
258 attr
.font
= wxFont(info
);
259 info
.description
= NULL
;
263 style
= gtk_rc_get_style(widget
);
265 style
= gtk_widget_get_default_style();
269 // get the style's colours
270 attr
.colFg
= wxColour(style
->fg
[state
]);
272 attr
.colBg
= wxColour(style
->base
[state
]);
274 attr
.colBg
= wxColour(style
->bg
[state
]);
276 // get the style's font
277 if (!style
->font_desc
)
278 style
= gtk_widget_get_default_style();
279 if (style
&& style
->font_desc
)
281 wxNativeFontInfo info
;
282 info
.description
= style
->font_desc
;
283 attr
.font
= wxFont(info
);
284 info
.description
= NULL
;
288 attr
= wxWindow::GetClassDefaultAttributes(wxWINDOW_VARIANT_NORMAL
);
291 if (!attr
.font
.IsOk())
293 GtkSettings
*settings
= gtk_settings_get_default();
294 gchar
*font_name
= NULL
;
295 g_object_get ( settings
,
300 attr
.font
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
302 attr
.font
= wxFont(wxString::FromAscii(font_name
));
307 gtk_widget_destroy(tlw
);
312 // This is not the same as GetBestSize() because that size may have
313 // been recalculated and cached by us. We want GTK+ information.
314 wxSize
wxControl::GTKGetPreferredSize(GtkWidget
* widget
) const
318 gtk_widget_get_preferred_size(widget
, NULL
, &req
);
320 GTK_WIDGET_GET_CLASS(widget
)->size_request(widget
, &req
);
323 return wxSize(req
.width
, req
.height
);
326 wxPoint
wxControl::GTKGetEntryMargins(GtkEntry
* entry
) const
331 #if GTK_CHECK_VERSION(2,10,0)
332 // The margins we have previously set
333 const GtkBorder
* border
= gtk_entry_get_inner_border(entry
);
336 marg
.x
= border
->left
+ border
->right
;
337 marg
.y
= border
->top
+ border
->bottom
;
341 // Gtk3 does not use inner border, but StyleContext and CSS
342 // TODO: implement it, starting with wxTextEntry::DoSetMargins()
346 gtk_entry_get_layout_offsets(entry
, &x
, &y
);
347 // inner borders are included. Substract them so we can get other margins
357 #endif // wxUSE_CONTROLS