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