]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/control.cpp
no real changes, just move wxMask::GetBitmap() to wxMask section
[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
170acdc9 108 SetInitialSize(size);
abdeb9e7
RD
109}
110
ad60f9e7
JS
111// ----------------------------------------------------------------------------
112// Work around a GTK+ bug whereby button is insensitive after being
113// enabled
114// ----------------------------------------------------------------------------
115
116// Fix sensitivity due to bug in GTK+ < 2.14
9dc44eff 117void wxControl::GTKFixSensitivity(bool WXUNUSED_IN_GTK3(onlyIfUnderMouse))
ad60f9e7 118{
9dc44eff 119#ifndef __WXGTK3__
ad60f9e7
JS
120 if (gtk_check_version(2,14,0)
121#if wxUSE_SYSTEM_OPTIONS
122 && (wxSystemOptions::GetOptionInt(wxT("gtk.control.disable-sensitivity-fix")) != 1)
123#endif
124 )
125 {
8ab75332 126 if (!onlyIfUnderMouse || GetScreenRect().Contains(wxGetMousePosition()))
ad60f9e7
JS
127 {
128 Hide();
129 Show();
130 }
131 }
9dc44eff 132#endif
ad60f9e7
JS
133}
134
b2ff89d6
VZ
135// ----------------------------------------------------------------------------
136// wxControl dealing with labels
137// ----------------------------------------------------------------------------
138
39bc0347 139void wxControl::GTKSetLabelForLabel(GtkLabel *w, const wxString& label)
b2ff89d6 140{
39bc0347
VZ
141 const wxString labelGTK = GTKConvertMnemonics(label);
142 gtk_label_set_text_with_mnemonic(w, wxGTK_CONV(labelGTK));
b2ff89d6
VZ
143}
144
f5bdfc69
VZ
145#if wxUSE_MARKUP
146
39bc0347 147void wxControl::GTKSetLabelWithMarkupForLabel(GtkLabel *w, const wxString& label)
b2ff89d6 148{
39bc0347
VZ
149 const wxString labelGTK = GTKConvertMnemonicsWithMarkup(label);
150 gtk_label_set_markup_with_mnemonic(w, wxGTK_CONV(labelGTK));
b2ff89d6
VZ
151}
152
f5bdfc69 153#endif // wxUSE_MARKUP
b2ff89d6 154
2e1f5012
VZ
155// ----------------------------------------------------------------------------
156// GtkFrame helpers
157//
158// GtkFrames do in fact support mnemonics in GTK2+ but not through
159// gtk_frame_set_label, rather you need to use a custom label widget
160// instead (idea gleaned from the native gtk font dialog code in GTK)
161// ----------------------------------------------------------------------------
162
163GtkWidget* wxControl::GTKCreateFrame(const wxString& label)
164{
165 const wxString labelGTK = GTKConvertMnemonics(label);
166 GtkWidget* labelwidget = gtk_label_new_with_mnemonic(wxGTK_CONV(labelGTK));
167 gtk_widget_show(labelwidget); // without this it won't show...
168
169 GtkWidget* framewidget = gtk_frame_new(NULL);
170 gtk_frame_set_label_widget(GTK_FRAME(framewidget), labelwidget);
171
39bc0347
VZ
172 return framewidget; // note that the label is already set so you'll
173 // only need to call wxControl::SetLabel afterwards
2e1f5012
VZ
174}
175
b2ff89d6
VZ
176void wxControl::GTKSetLabelForFrame(GtkFrame *w, const wxString& label)
177{
6ea2bc50
VZ
178 wxControlBase::SetLabel(label);
179
2e1f5012
VZ
180 GtkLabel* labelwidget = GTK_LABEL(gtk_frame_get_label_widget(w));
181 GTKSetLabelForLabel(labelwidget, label);
182}
183
184void wxControl::GTKFrameApplyWidgetStyle(GtkFrame* w, GtkRcStyle* style)
185{
9dc44eff
PC
186 GTKApplyStyle(GTK_WIDGET(w), style);
187 GTKApplyStyle(gtk_frame_get_label_widget(w), style);
2e1f5012 188}
b2ff89d6 189
2e1f5012
VZ
190void wxControl::GTKFrameSetMnemonicWidget(GtkFrame* w, GtkWidget* widget)
191{
192 GtkLabel* labelwidget = GTK_LABEL(gtk_frame_get_label_widget(w));
b2ff89d6 193
2e1f5012 194 gtk_label_set_mnemonic_widget(labelwidget, widget);
b2ff89d6
VZ
195}
196
2e1f5012 197// ----------------------------------------------------------------------------
39bc0347 198// worker function implementing GTK*Mnemonics() functions
2e1f5012
VZ
199// ----------------------------------------------------------------------------
200
b2ff89d6
VZ
201/* static */
202wxString wxControl::GTKRemoveMnemonics(const wxString& label)
203{
b1f17bf0 204 return wxGTKRemoveMnemonics(label);
b2ff89d6
VZ
205}
206
207/* static */
208wxString wxControl::GTKConvertMnemonics(const wxString& label)
209{
b1f17bf0 210 return wxConvertMnemonicsToGTK(label);
eaafd2f8
VS
211}
212
39bc0347
VZ
213/* static */
214wxString wxControl::GTKConvertMnemonicsWithMarkup(const wxString& label)
215{
b1f17bf0 216 return wxConvertMnemonicsToGTKMarkup(label);
39bc0347
VZ
217}
218
b2ff89d6
VZ
219// ----------------------------------------------------------------------------
220// wxControl styles (a.k.a. attributes)
221// ----------------------------------------------------------------------------
9d522606
RD
222
223wxVisualAttributes wxControl::GetDefaultAttributes() const
224{
225 return GetDefaultAttributesFromGTKWidget(m_widget,
226 UseGTKStyleBase());
227}
228
9d522606
RD
229// static
230wxVisualAttributes
231wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget* widget,
9dc44eff 232 bool WXUNUSED_IN_GTK3(useBase),
9d522606
RD
233 int state)
234{
9d522606 235 wxVisualAttributes attr;
7fff16b8
PC
236
237 GtkWidget* tlw = NULL;
238 if (gtk_widget_get_parent(widget) == NULL)
239 {
240 tlw = gtk_window_new(GTK_WINDOW_TOPLEVEL);
241 gtk_container_add(GTK_CONTAINER(tlw), widget);
242 }
243
9dc44eff
PC
244#ifdef __WXGTK3__
245 GtkStateFlags stateFlag = GTK_STATE_FLAG_NORMAL;
246 if (state)
247 {
248 wxASSERT(state == GTK_STATE_ACTIVE);
249 stateFlag = GTK_STATE_FLAG_ACTIVE;
250 }
251 GtkStyleContext* sc = gtk_widget_get_style_context(widget);
252 GdkRGBA c;
253 gtk_style_context_get_color(sc, stateFlag, &c);
254 attr.colFg = wxColour(c);
255 gtk_style_context_get_background_color(sc, stateFlag, &c);
256 attr.colBg = wxColour(c);
257 wxNativeFontInfo info;
258 info.description = const_cast<PangoFontDescription*>(gtk_style_context_get_font(sc, stateFlag));
259 attr.font = wxFont(info);
260 info.description = NULL;
261#else
262 GtkStyle* style;
9d522606
RD
263
264 style = gtk_rc_get_style(widget);
265 if (!style)
266 style = gtk_widget_get_default_style();
267
7fff16b8 268 if (style)
9d522606 269 {
7fff16b8
PC
270 // get the style's colours
271 attr.colFg = wxColour(style->fg[state]);
272 if (useBase)
273 attr.colBg = wxColour(style->base[state]);
274 else
275 attr.colBg = wxColour(style->bg[state]);
9d522606 276
7fff16b8
PC
277 // get the style's font
278 if (!style->font_desc)
279 style = gtk_widget_get_default_style();
280 if (style && style->font_desc)
281 {
282 wxNativeFontInfo info;
283 info.description = style->font_desc;
284 attr.font = wxFont(info);
285 info.description = NULL;
286 }
b2ff89d6 287 }
7fff16b8
PC
288 else
289 attr = wxWindow::GetClassDefaultAttributes(wxWINDOW_VARIANT_NORMAL);
9dc44eff 290#endif
7fff16b8 291
9dc44eff 292 if (!attr.font.IsOk())
b2ff89d6 293 {
9d522606
RD
294 GtkSettings *settings = gtk_settings_get_default();
295 gchar *font_name = NULL;
296 g_object_get ( settings,
b2ff89d6 297 "gtk-font-name",
9d522606
RD
298 &font_name,
299 NULL);
300 if (!font_name)
301 attr.font = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT );
302 else
303 attr.font = wxFont(wxString::FromAscii(font_name));
304 g_free (font_name);
b2ff89d6 305 }
b2ff89d6 306
7fff16b8
PC
307 if (tlw)
308 gtk_widget_destroy(tlw);
9d522606 309
9d522606
RD
310 return attr;
311}
312
7a78a937
VZ
313// This is not the same as GetBestSize() because that size may have
314// been recalculated and cached by us. We want GTK+ information.
315wxSize wxControl::GTKGetPreferredSize(GtkWidget* widget) const
316{
317 GtkRequisition req;
318#ifdef __WXGTK3__
1897abe1 319 gtk_widget_get_preferred_size(widget, NULL, &req);
7a78a937
VZ
320#else
321 GTK_WIDGET_GET_CLASS(widget)->size_request(widget, &req);
322#endif
323
324 return wxSize(req.width, req.height);
325}
326
327wxPoint wxControl::GTKGetEntryMargins(GtkEntry* entry) const
328{
329 wxPoint marg(0, 0);
330
331#ifndef __WXGTK3__
332#if GTK_CHECK_VERSION(2,10,0)
333 // The margins we have previously set
334 const GtkBorder* border = gtk_entry_get_inner_border(entry);
335 if ( border )
336 {
337 marg.x = border->left + border->right;
338 marg.y = border->top + border->bottom;
339 }
340#endif // GTK+ 2.10+
341#else // GTK+ 3
342 // Gtk3 does not use inner border, but StyleContext and CSS
343 // TODO: implement it, starting with wxTextEntry::DoSetMargins()
344#endif // GTK+ 2/3
345
346 int x, y;
347 gtk_entry_get_layout_offsets(entry, &x, &y);
348 // inner borders are included. Substract them so we can get other margins
349 x -= marg.x;
350 y -= marg.y;
351 marg.x += 2 * x + 2;
352 marg.y += 2 * y + 2;
353
354 return marg;
355}
356
357
1e6feb95 358#endif // wxUSE_CONTROLS