]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/control.cpp
fixes #10358
[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"
034be888 24
b1f17bf0 25#include "wx/gtk/private/mnemonics.h"
39bc0347 26
634fb750
VZ
27// ============================================================================
28// wxControl implementation
29// ============================================================================
30
31// ----------------------------------------------------------------------------
32// wxControl creation
33// ----------------------------------------------------------------------------
c801d85f 34
9abe166a 35IMPLEMENT_DYNAMIC_CLASS(wxControl, wxWindow)
c801d85f 36
31528cd3 37wxControl::wxControl()
c801d85f 38{
6de97a3b 39}
c801d85f 40
04165bec 41bool wxControl::Create( wxWindow *parent,
31528cd3
VZ
42 wxWindowID id,
43 const wxPoint &pos,
44 const wxSize &size,
45 long style,
8d772832 46 const wxValidator& validator,
04165bec 47 const wxString &name )
8d772832 48{
04165bec 49 bool ret = wxWindow::Create(parent, id, pos, size, style, name);
b2ff89d6 50
04165bec 51#if wxUSE_VALIDATORS
8d772832 52 SetValidator(validator);
8d772832
RD
53#endif
54
04165bec
RR
55 return ret;
56}
57
f68586e5
VZ
58wxSize wxControl::DoGetBestSize() const
59{
0279e844
RR
60 // Do not return any arbitrary default value...
61 wxASSERT_MSG( m_widget, wxT("DoGetBestSize called before creation") );
62
50bedb7b
PC
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 }
9f884528
RD
75 CacheBestSize(best);
76 return best;
f68586e5
VZ
77}
78
abdeb9e7
RD
79void wxControl::PostCreation(const wxSize& size)
80{
81 wxWindow::PostCreation();
f40fdaa3
VS
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);
b2ff89d6 89
abdeb9e7 90 ApplyWidgetStyle();
170acdc9 91 SetInitialSize(size);
abdeb9e7
RD
92}
93
b2ff89d6
VZ
94// ----------------------------------------------------------------------------
95// wxControl dealing with labels
96// ----------------------------------------------------------------------------
97
39bc0347 98void wxControl::GTKSetLabelForLabel(GtkLabel *w, const wxString& label)
b2ff89d6 99{
39bc0347
VZ
100 // save the original label
101 wxControlBase::SetLabel(label);
b2ff89d6 102
39bc0347
VZ
103 const wxString labelGTK = GTKConvertMnemonics(label);
104 gtk_label_set_text_with_mnemonic(w, wxGTK_CONV(labelGTK));
b2ff89d6
VZ
105}
106
39bc0347 107void wxControl::GTKSetLabelWithMarkupForLabel(GtkLabel *w, const wxString& label)
b2ff89d6 108{
39bc0347
VZ
109 const wxString labelGTK = GTKConvertMnemonicsWithMarkup(label);
110 gtk_label_set_markup_with_mnemonic(w, wxGTK_CONV(labelGTK));
b2ff89d6
VZ
111}
112
b2ff89d6 113
2e1f5012
VZ
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
122GtkWidget* 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
39bc0347
VZ
131 return framewidget; // note that the label is already set so you'll
132 // only need to call wxControl::SetLabel afterwards
2e1f5012
VZ
133}
134
b2ff89d6
VZ
135void wxControl::GTKSetLabelForFrame(GtkFrame *w, const wxString& label)
136{
2e1f5012
VZ
137 GtkLabel* labelwidget = GTK_LABEL(gtk_frame_get_label_widget(w));
138 GTKSetLabelForLabel(labelwidget, label);
139}
140
141void 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}
b2ff89d6 146
2e1f5012
VZ
147void wxControl::GTKFrameSetMnemonicWidget(GtkFrame* w, GtkWidget* widget)
148{
149 GtkLabel* labelwidget = GTK_LABEL(gtk_frame_get_label_widget(w));
b2ff89d6 150
2e1f5012 151 gtk_label_set_mnemonic_widget(labelwidget, widget);
b2ff89d6
VZ
152}
153
2e1f5012 154// ----------------------------------------------------------------------------
39bc0347 155// worker function implementing GTK*Mnemonics() functions
2e1f5012
VZ
156// ----------------------------------------------------------------------------
157
b2ff89d6
VZ
158/* static */
159wxString wxControl::GTKRemoveMnemonics(const wxString& label)
160{
b1f17bf0 161 return wxGTKRemoveMnemonics(label);
b2ff89d6
VZ
162}
163
164/* static */
165wxString wxControl::GTKConvertMnemonics(const wxString& label)
166{
b1f17bf0 167 return wxConvertMnemonicsToGTK(label);
eaafd2f8
VS
168}
169
39bc0347
VZ
170/* static */
171wxString wxControl::GTKConvertMnemonicsWithMarkup(const wxString& label)
172{
b1f17bf0 173 return wxConvertMnemonicsToGTKMarkup(label);
39bc0347
VZ
174}
175
b2ff89d6
VZ
176// ----------------------------------------------------------------------------
177// wxControl styles (a.k.a. attributes)
178// ----------------------------------------------------------------------------
9d522606
RD
179
180wxVisualAttributes wxControl::GetDefaultAttributes() const
181{
182 return GetDefaultAttributesFromGTKWidget(m_widget,
183 UseGTKStyleBase());
184}
185
9d522606
RD
186// static
187wxVisualAttributes
188wxControl::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;
b2ff89d6 206
9d522606 207 // get the style's colours
cdf068a4 208 attr.colFg = wxColour(style->fg[state]);
9d522606 209 if (useBase)
cdf068a4 210 attr.colBg = wxColour(style->base[state]);
9d522606 211 else
cdf068a4 212 attr.colBg = wxColour(style->bg[state]);
9d522606
RD
213
214 // get the style's font
9d522606 215 if ( !style->font_desc )
b2ff89d6 216 style = gtk_widget_get_default_style();
9d522606 217 if ( style && style->font_desc )
b2ff89d6
VZ
218 {
219 wxNativeFontInfo info;
fdf7514a 220 info.description = pango_font_description_copy(style->font_desc);
b2ff89d6
VZ
221 attr.font = wxFont(info);
222 }
223 else
224 {
9d522606
RD
225 GtkSettings *settings = gtk_settings_get_default();
226 gchar *font_name = NULL;
227 g_object_get ( settings,
b2ff89d6 228 "gtk-font-name",
9d522606
RD
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);
b2ff89d6 236 }
b2ff89d6 237
9d522606
RD
238 return attr;
239}
240
241
242//static
243wxVisualAttributes
865bb325 244wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNew_t widget_new,
9d522606
RD
245 bool useBase,
246 int state)
247{
248 wxVisualAttributes attr;
66d8fe77
VS
249 // NB: we need toplevel window so that GTK+ can find the right style
250 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
9d522606 251 GtkWidget* widget = widget_new();
66d8fe77 252 gtk_container_add(GTK_CONTAINER(wnd), widget);
9d522606 253 attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state);
66d8fe77 254 gtk_widget_destroy(wnd);
9d522606
RD
255 return attr;
256}
257
258//static
259wxVisualAttributes
865bb325 260wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromStr_t widget_new,
9d522606
RD
261 bool useBase,
262 int state)
263{
264 wxVisualAttributes attr;
66d8fe77
VS
265 // NB: we need toplevel window so that GTK+ can find the right style
266 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
9d522606 267 GtkWidget* widget = widget_new("");
66d8fe77 268 gtk_container_add(GTK_CONTAINER(wnd), widget);
9d522606 269 attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state);
66d8fe77 270 gtk_widget_destroy(wnd);
9d522606
RD
271 return attr;
272}
273
274
275//static
276wxVisualAttributes
865bb325 277wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromAdj_t widget_new,
9d522606
RD
278 bool useBase,
279 int state)
280{
281 wxVisualAttributes attr;
66d8fe77
VS
282 // NB: we need toplevel window so that GTK+ can find the right style
283 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
9d522606 284 GtkWidget* widget = widget_new(NULL);
66d8fe77 285 gtk_container_add(GTK_CONTAINER(wnd), widget);
9d522606 286 attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state);
66d8fe77 287 gtk_widget_destroy(wnd);
9d522606
RD
288 return attr;
289}
290
ef5c70f9
VZ
291// ----------------------------------------------------------------------------
292// idle handling
293// ----------------------------------------------------------------------------
294
295void wxControl::OnInternalIdle()
296{
297 if ( GtkShowFromOnIdle() )
298 return;
ef5c70f9 299
1f5cf9cc 300 if ( GTK_WIDGET_REALIZED(m_widget) )
6c6a3e8a
VZ
301 {
302 GTKUpdateCursor();
6c6a3e8a 303 }
06a7419e 304
e1ac8c99 305 if ( wxUpdateUIEvent::CanUpdate(this) && IsShownOnScreen() )
ef5c70f9
VZ
306 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
307}
308
1e6feb95 309#endif // wxUSE_CONTROLS