don't call size_request to get best size of a non-native control, it will probably...
[wxWidgets.git] / src / gtk / control.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/control.cpp
3 // Purpose: wxControl implementation for wxGTK
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling, Julian Smart and Vadim Zeitlin
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #if wxUSE_CONTROLS
14
15 #include "wx/control.h"
16
17 #ifndef WX_PRECOMP
18 #include "wx/log.h"
19 #include "wx/settings.h"
20 #endif
21
22 #include "wx/fontutil.h"
23 #include "wx/gtk/private.h"
24
25 #include "wx/gtk/private/mnemonics.h"
26
27 // ============================================================================
28 // wxControl implementation
29 // ============================================================================
30
31 // ----------------------------------------------------------------------------
32 // wxControl creation
33 // ----------------------------------------------------------------------------
34
35 IMPLEMENT_DYNAMIC_CLASS(wxControl, wxWindow)
36
37 wxControl::wxControl()
38 {
39 }
40
41 bool wxControl::Create( wxWindow *parent,
42 wxWindowID id,
43 const wxPoint &pos,
44 const wxSize &size,
45 long style,
46 const wxValidator& validator,
47 const wxString &name )
48 {
49 bool ret = wxWindow::Create(parent, id, pos, size, style, name);
50
51 #if wxUSE_VALIDATORS
52 SetValidator(validator);
53 #endif
54
55 return ret;
56 }
57
58 wxSize wxControl::DoGetBestSize() const
59 {
60 // Do not return any arbitrary default value...
61 wxASSERT_MSG( m_widget, wxT("DoGetBestSize called before creation") );
62
63 wxSize best;
64 if (m_wxwindow)
65 {
66 // this is not a native control, size_request is likely to be (0,0)
67 best = wxControlBase::DoGetBestSize();
68 }
69 else
70 {
71 GtkRequisition req;
72 GTK_WIDGET_GET_CLASS(m_widget)->size_request(m_widget, &req);
73 best.Set(req.width, req.height);
74 }
75 CacheBestSize(best);
76 return best;
77 }
78
79 void wxControl::PostCreation(const wxSize& size)
80 {
81 wxWindow::PostCreation();
82
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);
89
90 ApplyWidgetStyle();
91 SetInitialSize(size);
92 }
93
94 // ----------------------------------------------------------------------------
95 // wxControl dealing with labels
96 // ----------------------------------------------------------------------------
97
98 void wxControl::GTKSetLabelForLabel(GtkLabel *w, const wxString& label)
99 {
100 // save the original label
101 wxControlBase::SetLabel(label);
102
103 const wxString labelGTK = GTKConvertMnemonics(label);
104 gtk_label_set_text_with_mnemonic(w, wxGTK_CONV(labelGTK));
105 }
106
107 void wxControl::GTKSetLabelWithMarkupForLabel(GtkLabel *w, const wxString& label)
108 {
109 const wxString labelGTK = GTKConvertMnemonicsWithMarkup(label);
110 gtk_label_set_markup_with_mnemonic(w, wxGTK_CONV(labelGTK));
111 }
112
113
114 // ----------------------------------------------------------------------------
115 // GtkFrame helpers
116 //
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 // ----------------------------------------------------------------------------
121
122 GtkWidget* wxControl::GTKCreateFrame(const wxString& label)
123 {
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...
127
128 GtkWidget* framewidget = gtk_frame_new(NULL);
129 gtk_frame_set_label_widget(GTK_FRAME(framewidget), labelwidget);
130
131 return framewidget; // note that the label is already set so you'll
132 // only need to call wxControl::SetLabel afterwards
133 }
134
135 void wxControl::GTKSetLabelForFrame(GtkFrame *w, const wxString& label)
136 {
137 GtkLabel* labelwidget = GTK_LABEL(gtk_frame_get_label_widget(w));
138 GTKSetLabelForLabel(labelwidget, label);
139 }
140
141 void wxControl::GTKFrameApplyWidgetStyle(GtkFrame* w, GtkRcStyle* style)
142 {
143 gtk_widget_modify_style(GTK_WIDGET(w), style);
144 gtk_widget_modify_style(gtk_frame_get_label_widget (w), style);
145 }
146
147 void wxControl::GTKFrameSetMnemonicWidget(GtkFrame* w, GtkWidget* widget)
148 {
149 GtkLabel* labelwidget = GTK_LABEL(gtk_frame_get_label_widget(w));
150
151 gtk_label_set_mnemonic_widget(labelwidget, widget);
152 }
153
154 // ----------------------------------------------------------------------------
155 // worker function implementing GTK*Mnemonics() functions
156 // ----------------------------------------------------------------------------
157
158 /* static */
159 wxString wxControl::GTKRemoveMnemonics(const wxString& label)
160 {
161 return wxGTKRemoveMnemonics(label);
162 }
163
164 /* static */
165 wxString wxControl::GTKConvertMnemonics(const wxString& label)
166 {
167 return wxConvertMnemonicsToGTK(label);
168 }
169
170 /* static */
171 wxString wxControl::GTKConvertMnemonicsWithMarkup(const wxString& label)
172 {
173 return wxConvertMnemonicsToGTKMarkup(label);
174 }
175
176 // ----------------------------------------------------------------------------
177 // wxControl styles (a.k.a. attributes)
178 // ----------------------------------------------------------------------------
179
180 wxVisualAttributes wxControl::GetDefaultAttributes() const
181 {
182 return GetDefaultAttributesFromGTKWidget(m_widget,
183 UseGTKStyleBase());
184 }
185
186 // static
187 wxVisualAttributes
188 wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget* widget,
189 bool useBase,
190 int state)
191 {
192 GtkStyle* style;
193 wxVisualAttributes attr;
194
195 style = gtk_rc_get_style(widget);
196 if (!style)
197 style = gtk_widget_get_default_style();
198
199 if (!style)
200 {
201 return wxWindow::GetClassDefaultAttributes(wxWINDOW_VARIANT_NORMAL);
202 }
203
204 if (state == -1)
205 state = GTK_STATE_NORMAL;
206
207 // get the style's colours
208 attr.colFg = wxColour(style->fg[state]);
209 if (useBase)
210 attr.colBg = wxColour(style->base[state]);
211 else
212 attr.colBg = wxColour(style->bg[state]);
213
214 // get the style's font
215 if ( !style->font_desc )
216 style = gtk_widget_get_default_style();
217 if ( style && style->font_desc )
218 {
219 wxNativeFontInfo info;
220 info.description = pango_font_description_copy(style->font_desc);
221 attr.font = wxFont(info);
222 }
223 else
224 {
225 GtkSettings *settings = gtk_settings_get_default();
226 gchar *font_name = NULL;
227 g_object_get ( settings,
228 "gtk-font-name",
229 &font_name,
230 NULL);
231 if (!font_name)
232 attr.font = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT );
233 else
234 attr.font = wxFont(wxString::FromAscii(font_name));
235 g_free (font_name);
236 }
237
238 return attr;
239 }
240
241
242 //static
243 wxVisualAttributes
244 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNew_t widget_new,
245 bool useBase,
246 int state)
247 {
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);
255 return attr;
256 }
257
258 //static
259 wxVisualAttributes
260 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromStr_t widget_new,
261 bool useBase,
262 int state)
263 {
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);
271 return attr;
272 }
273
274
275 //static
276 wxVisualAttributes
277 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromAdj_t widget_new,
278 bool useBase,
279 int state)
280 {
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);
288 return attr;
289 }
290
291 // ----------------------------------------------------------------------------
292 // idle handling
293 // ----------------------------------------------------------------------------
294
295 void wxControl::OnInternalIdle()
296 {
297 if ( GtkShowFromOnIdle() )
298 return;
299
300 if ( GTK_WIDGET_REALIZED(m_widget) )
301 {
302 GTKUpdateCursor();
303 }
304
305 if ( wxUpdateUIEvent::CanUpdate(this) && IsShownOnScreen() )
306 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
307 }
308
309 #endif // wxUSE_CONTROLS