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"
23 #include "wx/gtk/private.h"
25 #include "wx/gtk/private/mnemonics.h"
27 // ============================================================================
28 // wxControl implementation
29 // ============================================================================
31 // ----------------------------------------------------------------------------
33 // ----------------------------------------------------------------------------
35 IMPLEMENT_DYNAMIC_CLASS(wxControl
, wxWindow
)
37 wxControl::wxControl()
41 bool wxControl::Create( wxWindow
*parent
,
46 const wxValidator
& validator
,
47 const wxString
&name
)
49 bool ret
= wxWindow::Create(parent
, id
, pos
, size
, style
, name
);
52 SetValidator(validator
);
58 wxSize
wxControl::DoGetBestSize() const
60 // Do not return any arbitrary default value...
61 wxASSERT_MSG( m_widget
, wxT("DoGetBestSize called before creation") );
66 // this is not a native control, size_request is likely to be (0,0)
67 best
= wxControlBase::DoGetBestSize();
72 GTK_WIDGET_GET_CLASS(m_widget
)->size_request(m_widget
, &req
);
73 best
.Set(req
.width
, req
.height
);
79 void wxControl::PostCreation(const wxSize
& size
)
81 wxWindow::PostCreation();
83 // NB: GetBestSize needs to know the style, otherwise it will assume
84 // default font and if the user uses a different font, determined
85 // best size will be different (typically, smaller) than the desired
86 // size. This call ensure that a style is available at the time
87 // GetBestSize is called.
88 gtk_widget_ensure_style(m_widget
);
94 // ----------------------------------------------------------------------------
95 // wxControl dealing with labels
96 // ----------------------------------------------------------------------------
98 void wxControl::GTKSetLabelForLabel(GtkLabel
*w
, const wxString
& label
)
100 // save the original label
101 wxControlBase::SetLabel(label
);
103 const wxString labelGTK
= GTKConvertMnemonics(label
);
104 gtk_label_set_text_with_mnemonic(w
, wxGTK_CONV(labelGTK
));
107 void wxControl::GTKSetLabelWithMarkupForLabel(GtkLabel
*w
, const wxString
& label
)
109 const wxString labelGTK
= GTKConvertMnemonicsWithMarkup(label
);
110 gtk_label_set_markup_with_mnemonic(w
, wxGTK_CONV(labelGTK
));
114 // ----------------------------------------------------------------------------
117 // GtkFrames do in fact support mnemonics in GTK2+ but not through
118 // gtk_frame_set_label, rather you need to use a custom label widget
119 // instead (idea gleaned from the native gtk font dialog code in GTK)
120 // ----------------------------------------------------------------------------
122 GtkWidget
* wxControl::GTKCreateFrame(const wxString
& label
)
124 const wxString labelGTK
= GTKConvertMnemonics(label
);
125 GtkWidget
* labelwidget
= gtk_label_new_with_mnemonic(wxGTK_CONV(labelGTK
));
126 gtk_widget_show(labelwidget
); // without this it won't show...
128 GtkWidget
* framewidget
= gtk_frame_new(NULL
);
129 gtk_frame_set_label_widget(GTK_FRAME(framewidget
), labelwidget
);
131 return framewidget
; // note that the label is already set so you'll
132 // only need to call wxControl::SetLabel afterwards
135 void wxControl::GTKSetLabelForFrame(GtkFrame
*w
, const wxString
& label
)
137 GtkLabel
* labelwidget
= GTK_LABEL(gtk_frame_get_label_widget(w
));
138 GTKSetLabelForLabel(labelwidget
, label
);
141 void wxControl::GTKFrameApplyWidgetStyle(GtkFrame
* w
, GtkRcStyle
* style
)
143 gtk_widget_modify_style(GTK_WIDGET(w
), style
);
144 gtk_widget_modify_style(gtk_frame_get_label_widget (w
), style
);
147 void wxControl::GTKFrameSetMnemonicWidget(GtkFrame
* w
, GtkWidget
* widget
)
149 GtkLabel
* labelwidget
= GTK_LABEL(gtk_frame_get_label_widget(w
));
151 gtk_label_set_mnemonic_widget(labelwidget
, widget
);
154 // ----------------------------------------------------------------------------
155 // worker function implementing GTK*Mnemonics() functions
156 // ----------------------------------------------------------------------------
159 wxString
wxControl::GTKRemoveMnemonics(const wxString
& label
)
161 return wxGTKRemoveMnemonics(label
);
165 wxString
wxControl::GTKConvertMnemonics(const wxString
& label
)
167 return wxConvertMnemonicsToGTK(label
);
171 wxString
wxControl::GTKConvertMnemonicsWithMarkup(const wxString
& label
)
173 return wxConvertMnemonicsToGTKMarkup(label
);
176 // ----------------------------------------------------------------------------
177 // wxControl styles (a.k.a. attributes)
178 // ----------------------------------------------------------------------------
180 wxVisualAttributes
wxControl::GetDefaultAttributes() const
182 return GetDefaultAttributesFromGTKWidget(m_widget
,
188 wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget
* widget
,
193 wxVisualAttributes attr
;
195 style
= gtk_rc_get_style(widget
);
197 style
= gtk_widget_get_default_style();
201 return wxWindow::GetClassDefaultAttributes(wxWINDOW_VARIANT_NORMAL
);
205 state
= GTK_STATE_NORMAL
;
207 // get the style's colours
208 attr
.colFg
= wxColour(style
->fg
[state
]);
210 attr
.colBg
= wxColour(style
->base
[state
]);
212 attr
.colBg
= wxColour(style
->bg
[state
]);
214 // get the style's font
215 if ( !style
->font_desc
)
216 style
= gtk_widget_get_default_style();
217 if ( style
&& style
->font_desc
)
219 wxNativeFontInfo info
;
220 info
.description
= pango_font_description_copy(style
->font_desc
);
221 attr
.font
= wxFont(info
);
225 GtkSettings
*settings
= gtk_settings_get_default();
226 gchar
*font_name
= NULL
;
227 g_object_get ( settings
,
232 attr
.font
= wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT
);
234 attr
.font
= wxFont(wxString::FromAscii(font_name
));
244 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNew_t widget_new
,
248 wxVisualAttributes attr
;
249 // NB: we need toplevel window so that GTK+ can find the right style
250 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
251 GtkWidget
* widget
= widget_new();
252 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
253 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
254 gtk_widget_destroy(wnd
);
260 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromStr_t widget_new
,
264 wxVisualAttributes attr
;
265 // NB: we need toplevel window so that GTK+ can find the right style
266 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
267 GtkWidget
* widget
= widget_new("");
268 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
269 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
270 gtk_widget_destroy(wnd
);
277 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromAdj_t widget_new
,
281 wxVisualAttributes attr
;
282 // NB: we need toplevel window so that GTK+ can find the right style
283 GtkWidget
*wnd
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
284 GtkWidget
* widget
= widget_new(NULL
);
285 gtk_container_add(GTK_CONTAINER(wnd
), widget
);
286 attr
= GetDefaultAttributesFromGTKWidget(widget
, useBase
, state
);
287 gtk_widget_destroy(wnd
);
291 // ----------------------------------------------------------------------------
293 // ----------------------------------------------------------------------------
295 void wxControl::OnInternalIdle()
297 if ( GtkShowFromOnIdle() )
300 if ( GTK_WIDGET_REALIZED(m_widget
) )
305 if ( wxUpdateUIEvent::CanUpdate(this) && IsShownOnScreen() )
306 UpdateWindowUI(wxUPDATE_UI_FROMIDLE
);
309 #endif // wxUSE_CONTROLS