]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/control.cpp
simplify code to return from the end of the function
[wxWidgets.git] / src / gtk / control.cpp
CommitLineData
c801d85f 1/////////////////////////////////////////////////////////////////////////////
634fb750
VZ
2// Name: src/gtk/control.cpp
3// Purpose: wxControl implementation for wxGTK
c801d85f 4// Author: Robert Roebling
dbf858b5 5// Id: $Id$
01111366 6// Copyright: (c) 1998 Robert Roebling, Julian Smart and Vadim Zeitlin
65571936 7// Licence: wxWindows licence
c801d85f
KB
8/////////////////////////////////////////////////////////////////////////////
9
14f355c2
VS
10// For compilers that support precompilation, includes "wx.h".
11#include "wx/wxprec.h"
12
1e6feb95
VZ
13#if wxUSE_CONTROLS
14
c801d85f 15#include "wx/control.h"
e4db172a
WS
16
17#ifndef WX_PRECOMP
18 #include "wx/log.h"
9eddec69 19 #include "wx/settings.h"
e4db172a
WS
20#endif
21
9d522606 22#include "wx/fontutil.h"
ad60f9e7
JS
23#include "wx/utils.h"
24#include "wx/sysopt.h"
034be888 25
9dc44eff
PC
26#include <gtk/gtk.h>
27#include "wx/gtk/private.h"
b1f17bf0 28#include "wx/gtk/private/mnemonics.h"
39bc0347 29
634fb750
VZ
30// ============================================================================
31// wxControl implementation
32// ============================================================================
33
34// ----------------------------------------------------------------------------
35// wxControl creation
36// ----------------------------------------------------------------------------
c801d85f 37
9abe166a 38IMPLEMENT_DYNAMIC_CLASS(wxControl, wxWindow)
c801d85f 39
31528cd3 40wxControl::wxControl()
c801d85f 41{
6de97a3b 42}
c801d85f 43
04165bec 44bool wxControl::Create( wxWindow *parent,
31528cd3
VZ
45 wxWindowID id,
46 const wxPoint &pos,
47 const wxSize &size,
48 long style,
8d772832 49 const wxValidator& validator,
04165bec 50 const wxString &name )
8d772832 51{
04165bec 52 bool ret = wxWindow::Create(parent, id, pos, size, style, name);
b2ff89d6 53
04165bec 54#if wxUSE_VALIDATORS
8d772832 55 SetValidator(validator);
8d772832
RD
56#endif
57
04165bec
RR
58 return ret;
59}
60
9dc44eff
PC
61#ifdef __WXGTK3__
62bool wxControl::SetFont(const wxFont& font)
63{
64 const bool changed = base_type::SetFont(font);
65 if (changed && !gtk_widget_get_realized(m_widget))
66 {
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");
71 }
72 return changed;
73}
74#endif
75
f68586e5
VZ
76wxSize wxControl::DoGetBestSize() const
77{
0279e844
RR
78 // Do not return any arbitrary default value...
79 wxASSERT_MSG( m_widget, wxT("DoGetBestSize called before creation") );
80
50bedb7b
PC
81 wxSize best;
82 if (m_wxwindow)
83 {
84 // this is not a native control, size_request is likely to be (0,0)
85 best = wxControlBase::DoGetBestSize();
86 }
87 else
88 {
7a78a937 89 best = GTKGetPreferredSize(m_widget);
50bedb7b 90 }
7a78a937 91
9f884528 92 return best;
f68586e5
VZ
93}
94
abdeb9e7
RD
95void wxControl::PostCreation(const wxSize& size)
96{
97 wxWindow::PostCreation();
f40fdaa3 98
9dc44eff 99#ifndef __WXGTK3__
f40fdaa3
VS
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);
9dc44eff 106#endif
b2ff89d6 107
496e7ec6 108 GTKApplyWidgetStyle();
170acdc9 109 SetInitialSize(size);
abdeb9e7
RD
110}
111
ad60f9e7
JS
112// ----------------------------------------------------------------------------
113// Work around a GTK+ bug whereby button is insensitive after being
114// enabled
115// ----------------------------------------------------------------------------
116
117// Fix sensitivity due to bug in GTK+ < 2.14
9dc44eff 118void wxControl::GTKFixSensitivity(bool WXUNUSED_IN_GTK3(onlyIfUnderMouse))
ad60f9e7 119{
9dc44eff 120#ifndef __WXGTK3__
ad60f9e7
JS
121 if (gtk_check_version(2,14,0)
122#if wxUSE_SYSTEM_OPTIONS
123 && (wxSystemOptions::GetOptionInt(wxT("gtk.control.disable-sensitivity-fix")) != 1)
124#endif
125 )
126 {
8ab75332 127 if (!onlyIfUnderMouse || GetScreenRect().Contains(wxGetMousePosition()))
ad60f9e7
JS
128 {
129 Hide();
130 Show();
131 }
132 }
9dc44eff 133#endif
ad60f9e7
JS
134}
135
b2ff89d6
VZ
136// ----------------------------------------------------------------------------
137// wxControl dealing with labels
138// ----------------------------------------------------------------------------
139
39bc0347 140void wxControl::GTKSetLabelForLabel(GtkLabel *w, const wxString& label)
b2ff89d6 141{
39bc0347
VZ
142 const wxString labelGTK = GTKConvertMnemonics(label);
143 gtk_label_set_text_with_mnemonic(w, wxGTK_CONV(labelGTK));
b2ff89d6
VZ
144}
145
f5bdfc69
VZ
146#if wxUSE_MARKUP
147
39bc0347 148void wxControl::GTKSetLabelWithMarkupForLabel(GtkLabel *w, const wxString& label)
b2ff89d6 149{
39bc0347
VZ
150 const wxString labelGTK = GTKConvertMnemonicsWithMarkup(label);
151 gtk_label_set_markup_with_mnemonic(w, wxGTK_CONV(labelGTK));
b2ff89d6
VZ
152}
153
f5bdfc69 154#endif // wxUSE_MARKUP
b2ff89d6 155
2e1f5012
VZ
156// ----------------------------------------------------------------------------
157// GtkFrame helpers
158//
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// ----------------------------------------------------------------------------
163
164GtkWidget* wxControl::GTKCreateFrame(const wxString& label)
165{
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...
169
170 GtkWidget* framewidget = gtk_frame_new(NULL);
171 gtk_frame_set_label_widget(GTK_FRAME(framewidget), labelwidget);
172
39bc0347
VZ
173 return framewidget; // note that the label is already set so you'll
174 // only need to call wxControl::SetLabel afterwards
2e1f5012
VZ
175}
176
b2ff89d6
VZ
177void wxControl::GTKSetLabelForFrame(GtkFrame *w, const wxString& label)
178{
6ea2bc50
VZ
179 wxControlBase::SetLabel(label);
180
2e1f5012
VZ
181 GtkLabel* labelwidget = GTK_LABEL(gtk_frame_get_label_widget(w));
182 GTKSetLabelForLabel(labelwidget, label);
183}
184
185void wxControl::GTKFrameApplyWidgetStyle(GtkFrame* w, GtkRcStyle* style)
186{
9dc44eff
PC
187 GTKApplyStyle(GTK_WIDGET(w), style);
188 GTKApplyStyle(gtk_frame_get_label_widget(w), style);
2e1f5012 189}
b2ff89d6 190
2e1f5012
VZ
191void wxControl::GTKFrameSetMnemonicWidget(GtkFrame* w, GtkWidget* widget)
192{
193 GtkLabel* labelwidget = GTK_LABEL(gtk_frame_get_label_widget(w));
b2ff89d6 194
2e1f5012 195 gtk_label_set_mnemonic_widget(labelwidget, widget);
b2ff89d6
VZ
196}
197
2e1f5012 198// ----------------------------------------------------------------------------
39bc0347 199// worker function implementing GTK*Mnemonics() functions
2e1f5012
VZ
200// ----------------------------------------------------------------------------
201
b2ff89d6
VZ
202/* static */
203wxString wxControl::GTKRemoveMnemonics(const wxString& label)
204{
b1f17bf0 205 return wxGTKRemoveMnemonics(label);
b2ff89d6
VZ
206}
207
208/* static */
209wxString wxControl::GTKConvertMnemonics(const wxString& label)
210{
b1f17bf0 211 return wxConvertMnemonicsToGTK(label);
eaafd2f8
VS
212}
213
39bc0347
VZ
214/* static */
215wxString wxControl::GTKConvertMnemonicsWithMarkup(const wxString& label)
216{
b1f17bf0 217 return wxConvertMnemonicsToGTKMarkup(label);
39bc0347
VZ
218}
219
b2ff89d6
VZ
220// ----------------------------------------------------------------------------
221// wxControl styles (a.k.a. attributes)
222// ----------------------------------------------------------------------------
9d522606
RD
223
224wxVisualAttributes wxControl::GetDefaultAttributes() const
225{
226 return GetDefaultAttributesFromGTKWidget(m_widget,
227 UseGTKStyleBase());
228}
229
9d522606
RD
230// static
231wxVisualAttributes
232wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget* widget,
9dc44eff 233 bool WXUNUSED_IN_GTK3(useBase),
9d522606
RD
234 int state)
235{
9d522606 236 wxVisualAttributes attr;
7fff16b8
PC
237
238 GtkWidget* tlw = NULL;
239 if (gtk_widget_get_parent(widget) == NULL)
240 {
241 tlw = gtk_window_new(GTK_WINDOW_TOPLEVEL);
242 gtk_container_add(GTK_CONTAINER(tlw), widget);
243 }
244
9dc44eff
PC
245#ifdef __WXGTK3__
246 GtkStateFlags stateFlag = GTK_STATE_FLAG_NORMAL;
247 if (state)
248 {
249 wxASSERT(state == GTK_STATE_ACTIVE);
250 stateFlag = GTK_STATE_FLAG_ACTIVE;
251 }
252 GtkStyleContext* sc = gtk_widget_get_style_context(widget);
253 GdkRGBA c;
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;
262#else
263 GtkStyle* style;
9d522606
RD
264
265 style = gtk_rc_get_style(widget);
266 if (!style)
267 style = gtk_widget_get_default_style();
268
7fff16b8 269 if (style)
9d522606 270 {
7fff16b8
PC
271 // get the style's colours
272 attr.colFg = wxColour(style->fg[state]);
273 if (useBase)
274 attr.colBg = wxColour(style->base[state]);
275 else
276 attr.colBg = wxColour(style->bg[state]);
9d522606 277
7fff16b8
PC
278 // get the style's font
279 if (!style->font_desc)
280 style = gtk_widget_get_default_style();
281 if (style && style->font_desc)
282 {
283 wxNativeFontInfo info;
284 info.description = style->font_desc;
285 attr.font = wxFont(info);
286 info.description = NULL;
287 }
b2ff89d6 288 }
7fff16b8
PC
289 else
290 attr = wxWindow::GetClassDefaultAttributes(wxWINDOW_VARIANT_NORMAL);
9dc44eff 291#endif
7fff16b8 292
9dc44eff 293 if (!attr.font.IsOk())
b2ff89d6 294 {
9d522606
RD
295 GtkSettings *settings = gtk_settings_get_default();
296 gchar *font_name = NULL;
297 g_object_get ( settings,
b2ff89d6 298 "gtk-font-name",
9d522606
RD
299 &font_name,
300 NULL);
301 if (!font_name)
302 attr.font = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT );
303 else
304 attr.font = wxFont(wxString::FromAscii(font_name));
305 g_free (font_name);
b2ff89d6 306 }
b2ff89d6 307
7fff16b8
PC
308 if (tlw)
309 gtk_widget_destroy(tlw);
9d522606 310
9d522606
RD
311 return attr;
312}
313
7a78a937
VZ
314// This is not the same as GetBestSize() because that size may have
315// been recalculated and cached by us. We want GTK+ information.
316wxSize wxControl::GTKGetPreferredSize(GtkWidget* widget) const
317{
318 GtkRequisition req;
319#ifdef __WXGTK3__
1897abe1 320 gtk_widget_get_preferred_size(widget, NULL, &req);
7a78a937
VZ
321#else
322 GTK_WIDGET_GET_CLASS(widget)->size_request(widget, &req);
323#endif
324
325 return wxSize(req.width, req.height);
326}
327
328wxPoint wxControl::GTKGetEntryMargins(GtkEntry* entry) const
329{
330 wxPoint marg(0, 0);
331
332#ifndef __WXGTK3__
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);
336 if ( border )
337 {
338 marg.x = border->left + border->right;
339 marg.y = border->top + border->bottom;
340 }
341#endif // GTK+ 2.10+
342#else // GTK+ 3
343 // Gtk3 does not use inner border, but StyleContext and CSS
344 // TODO: implement it, starting with wxTextEntry::DoSetMargins()
345#endif // GTK+ 2/3
346
347 int x, y;
348 gtk_entry_get_layout_offsets(entry, &x, &y);
349 // inner borders are included. Substract them so we can get other margins
350 x -= marg.x;
351 y -= marg.y;
352 marg.x += 2 * x + 2;
353 marg.y += 2 * y + 2;
354
355 return marg;
356}
357
358
1e6feb95 359#endif // wxUSE_CONTROLS