]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/control.cpp
Fix wrong wxEVT_COMMAND_DATAVIEW_COLUMN_HEADER_CLICK name in the docs.
[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"
b2ff89d6 23#include "wx/gtk/private.h"
ad60f9e7
JS
24#include "wx/utils.h"
25#include "wx/sysopt.h"
034be888 26
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
f68586e5
VZ
60wxSize wxControl::DoGetBestSize() const
61{
0279e844
RR
62 // Do not return any arbitrary default value...
63 wxASSERT_MSG( m_widget, wxT("DoGetBestSize called before creation") );
64
50bedb7b
PC
65 wxSize best;
66 if (m_wxwindow)
67 {
68 // this is not a native control, size_request is likely to be (0,0)
69 best = wxControlBase::DoGetBestSize();
70 }
71 else
72 {
73 GtkRequisition req;
74 GTK_WIDGET_GET_CLASS(m_widget)->size_request(m_widget, &req);
75 best.Set(req.width, req.height);
76 }
9f884528
RD
77 CacheBestSize(best);
78 return best;
f68586e5
VZ
79}
80
abdeb9e7
RD
81void wxControl::PostCreation(const wxSize& size)
82{
83 wxWindow::PostCreation();
f40fdaa3
VS
84
85 // NB: GetBestSize needs to know the style, otherwise it will assume
86 // default font and if the user uses a different font, determined
87 // best size will be different (typically, smaller) than the desired
88 // size. This call ensure that a style is available at the time
89 // GetBestSize is called.
90 gtk_widget_ensure_style(m_widget);
b2ff89d6 91
496e7ec6 92 GTKApplyWidgetStyle();
170acdc9 93 SetInitialSize(size);
abdeb9e7
RD
94}
95
ad60f9e7
JS
96// ----------------------------------------------------------------------------
97// Work around a GTK+ bug whereby button is insensitive after being
98// enabled
99// ----------------------------------------------------------------------------
100
101// Fix sensitivity due to bug in GTK+ < 2.14
102void wxControl::GTKFixSensitivity(bool onlyIfUnderMouse)
103{
104 if (gtk_check_version(2,14,0)
105#if wxUSE_SYSTEM_OPTIONS
106 && (wxSystemOptions::GetOptionInt(wxT("gtk.control.disable-sensitivity-fix")) != 1)
107#endif
108 )
109 {
110 wxPoint pt = wxGetMousePosition();
111 wxRect rect(ClientToScreen(wxPoint(0, 0)), GetSize());
112 if (!onlyIfUnderMouse || rect.Contains(pt))
113 {
114 Hide();
115 Show();
116 }
117 }
118}
119
b2ff89d6
VZ
120// ----------------------------------------------------------------------------
121// wxControl dealing with labels
122// ----------------------------------------------------------------------------
123
39bc0347 124void wxControl::GTKSetLabelForLabel(GtkLabel *w, const wxString& label)
b2ff89d6 125{
39bc0347
VZ
126 const wxString labelGTK = GTKConvertMnemonics(label);
127 gtk_label_set_text_with_mnemonic(w, wxGTK_CONV(labelGTK));
b2ff89d6
VZ
128}
129
f5bdfc69
VZ
130#if wxUSE_MARKUP
131
39bc0347 132void wxControl::GTKSetLabelWithMarkupForLabel(GtkLabel *w, const wxString& label)
b2ff89d6 133{
39bc0347
VZ
134 const wxString labelGTK = GTKConvertMnemonicsWithMarkup(label);
135 gtk_label_set_markup_with_mnemonic(w, wxGTK_CONV(labelGTK));
b2ff89d6
VZ
136}
137
f5bdfc69 138#endif // wxUSE_MARKUP
b2ff89d6 139
2e1f5012
VZ
140// ----------------------------------------------------------------------------
141// GtkFrame helpers
142//
143// GtkFrames do in fact support mnemonics in GTK2+ but not through
144// gtk_frame_set_label, rather you need to use a custom label widget
145// instead (idea gleaned from the native gtk font dialog code in GTK)
146// ----------------------------------------------------------------------------
147
148GtkWidget* wxControl::GTKCreateFrame(const wxString& label)
149{
150 const wxString labelGTK = GTKConvertMnemonics(label);
151 GtkWidget* labelwidget = gtk_label_new_with_mnemonic(wxGTK_CONV(labelGTK));
152 gtk_widget_show(labelwidget); // without this it won't show...
153
154 GtkWidget* framewidget = gtk_frame_new(NULL);
155 gtk_frame_set_label_widget(GTK_FRAME(framewidget), labelwidget);
156
39bc0347
VZ
157 return framewidget; // note that the label is already set so you'll
158 // only need to call wxControl::SetLabel afterwards
2e1f5012
VZ
159}
160
b2ff89d6
VZ
161void wxControl::GTKSetLabelForFrame(GtkFrame *w, const wxString& label)
162{
6ea2bc50
VZ
163 wxControlBase::SetLabel(label);
164
2e1f5012
VZ
165 GtkLabel* labelwidget = GTK_LABEL(gtk_frame_get_label_widget(w));
166 GTKSetLabelForLabel(labelwidget, label);
167}
168
169void wxControl::GTKFrameApplyWidgetStyle(GtkFrame* w, GtkRcStyle* style)
170{
171 gtk_widget_modify_style(GTK_WIDGET(w), style);
172 gtk_widget_modify_style(gtk_frame_get_label_widget (w), style);
173}
b2ff89d6 174
2e1f5012
VZ
175void wxControl::GTKFrameSetMnemonicWidget(GtkFrame* w, GtkWidget* widget)
176{
177 GtkLabel* labelwidget = GTK_LABEL(gtk_frame_get_label_widget(w));
b2ff89d6 178
2e1f5012 179 gtk_label_set_mnemonic_widget(labelwidget, widget);
b2ff89d6
VZ
180}
181
2e1f5012 182// ----------------------------------------------------------------------------
39bc0347 183// worker function implementing GTK*Mnemonics() functions
2e1f5012
VZ
184// ----------------------------------------------------------------------------
185
b2ff89d6
VZ
186/* static */
187wxString wxControl::GTKRemoveMnemonics(const wxString& label)
188{
b1f17bf0 189 return wxGTKRemoveMnemonics(label);
b2ff89d6
VZ
190}
191
192/* static */
193wxString wxControl::GTKConvertMnemonics(const wxString& label)
194{
b1f17bf0 195 return wxConvertMnemonicsToGTK(label);
eaafd2f8
VS
196}
197
39bc0347
VZ
198/* static */
199wxString wxControl::GTKConvertMnemonicsWithMarkup(const wxString& label)
200{
b1f17bf0 201 return wxConvertMnemonicsToGTKMarkup(label);
39bc0347
VZ
202}
203
b2ff89d6
VZ
204// ----------------------------------------------------------------------------
205// wxControl styles (a.k.a. attributes)
206// ----------------------------------------------------------------------------
9d522606
RD
207
208wxVisualAttributes wxControl::GetDefaultAttributes() const
209{
210 return GetDefaultAttributesFromGTKWidget(m_widget,
211 UseGTKStyleBase());
212}
213
9d522606
RD
214// static
215wxVisualAttributes
216wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget* widget,
217 bool useBase,
218 int state)
219{
220 GtkStyle* style;
221 wxVisualAttributes attr;
222
223 style = gtk_rc_get_style(widget);
224 if (!style)
225 style = gtk_widget_get_default_style();
226
227 if (!style)
228 {
229 return wxWindow::GetClassDefaultAttributes(wxWINDOW_VARIANT_NORMAL);
230 }
231
9d522606 232 // get the style's colours
cdf068a4 233 attr.colFg = wxColour(style->fg[state]);
9d522606 234 if (useBase)
cdf068a4 235 attr.colBg = wxColour(style->base[state]);
9d522606 236 else
cdf068a4 237 attr.colBg = wxColour(style->bg[state]);
9d522606
RD
238
239 // get the style's font
9d522606 240 if ( !style->font_desc )
b2ff89d6 241 style = gtk_widget_get_default_style();
9d522606 242 if ( style && style->font_desc )
b2ff89d6
VZ
243 {
244 wxNativeFontInfo info;
622226dc 245 info.description = style->font_desc;
b2ff89d6 246 attr.font = wxFont(info);
622226dc 247 info.description = NULL;
b2ff89d6
VZ
248 }
249 else
250 {
9d522606
RD
251 GtkSettings *settings = gtk_settings_get_default();
252 gchar *font_name = NULL;
253 g_object_get ( settings,
b2ff89d6 254 "gtk-font-name",
9d522606
RD
255 &font_name,
256 NULL);
257 if (!font_name)
258 attr.font = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT );
259 else
260 attr.font = wxFont(wxString::FromAscii(font_name));
261 g_free (font_name);
b2ff89d6 262 }
b2ff89d6 263
9d522606
RD
264 return attr;
265}
266
267
268//static
269wxVisualAttributes
865bb325 270wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNew_t widget_new,
9d522606
RD
271 bool useBase,
272 int state)
273{
274 wxVisualAttributes attr;
66d8fe77
VS
275 // NB: we need toplevel window so that GTK+ can find the right style
276 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
9d522606 277 GtkWidget* widget = widget_new();
66d8fe77 278 gtk_container_add(GTK_CONTAINER(wnd), widget);
9d522606 279 attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state);
66d8fe77 280 gtk_widget_destroy(wnd);
9d522606
RD
281 return attr;
282}
283
284//static
285wxVisualAttributes
865bb325 286wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromStr_t widget_new,
9d522606
RD
287 bool useBase,
288 int state)
289{
290 wxVisualAttributes attr;
66d8fe77
VS
291 // NB: we need toplevel window so that GTK+ can find the right style
292 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
9d522606 293 GtkWidget* widget = widget_new("");
66d8fe77 294 gtk_container_add(GTK_CONTAINER(wnd), widget);
9d522606 295 attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state);
66d8fe77 296 gtk_widget_destroy(wnd);
9d522606
RD
297 return attr;
298}
299
300
301//static
302wxVisualAttributes
865bb325 303wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromAdj_t widget_new,
9d522606
RD
304 bool useBase,
305 int state)
306{
307 wxVisualAttributes attr;
66d8fe77
VS
308 // NB: we need toplevel window so that GTK+ can find the right style
309 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
9d522606 310 GtkWidget* widget = widget_new(NULL);
66d8fe77 311 gtk_container_add(GTK_CONTAINER(wnd), widget);
9d522606 312 attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state);
66d8fe77 313 gtk_widget_destroy(wnd);
9d522606
RD
314 return attr;
315}
316
1e6feb95 317#endif // wxUSE_CONTROLS