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