]> git.saurik.com Git - wxWidgets.git/blame - src/gtk/control.cpp
X error handler should be extern C (SunCC warning fix)
[wxWidgets.git] / src / gtk / control.cpp
CommitLineData
c801d85f
KB
1/////////////////////////////////////////////////////////////////////////////
2// Name: control.cpp
3// Purpose:
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 10#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
c801d85f
KB
11#pragma implementation "control.h"
12#endif
13
14f355c2
VS
14// For compilers that support precompilation, includes "wx.h".
15#include "wx/wxprec.h"
16
1e6feb95
VZ
17#include "wx/defs.h"
18
19#if wxUSE_CONTROLS
20
c801d85f 21#include "wx/control.h"
9d522606
RD
22#include "wx/fontutil.h"
23#include "wx/settings.h"
c801d85f 24
071a2d78 25#include <gtk/gtk.h>
034be888 26
c801d85f
KB
27//-----------------------------------------------------------------------------
28// wxControl
29//-----------------------------------------------------------------------------
30
9abe166a 31IMPLEMENT_DYNAMIC_CLASS(wxControl, wxWindow)
c801d85f 32
31528cd3 33wxControl::wxControl()
c801d85f 34{
b292e2f5 35 m_needParent = TRUE;
6de97a3b 36}
c801d85f 37
04165bec 38bool wxControl::Create( wxWindow *parent,
31528cd3
VZ
39 wxWindowID id,
40 const wxPoint &pos,
41 const wxSize &size,
42 long style,
8d772832 43 const wxValidator& validator,
04165bec 44 const wxString &name )
8d772832 45{
04165bec
RR
46 bool ret = wxWindow::Create(parent, id, pos, size, style, name);
47
48#if wxUSE_VALIDATORS
8d772832 49 SetValidator(validator);
8d772832
RD
50#endif
51
04165bec
RR
52 return ret;
53}
54
c801d85f
KB
55void wxControl::SetLabel( const wxString &label )
56{
d9ea011f 57 m_label.Empty();
223d09f6 58 for ( const wxChar *pc = label; *pc != wxT('\0'); pc++ )
b292e2f5 59 {
223d09f6 60 if ( *pc == wxT('&') )
31528cd3 61 {
b292e2f5 62 pc++; // skip it
32c77a71 63#if 0 // it would be unused anyhow for now - kbd interface not done yet
223d09f6 64 if ( *pc != wxT('&') ) m_chAccel = *pc;
32c77a71 65#endif
b292e2f5
RR
66 }
67 m_label << *pc;
32c77a71 68 }
9f884528 69 InvalidateBestSize();
6de97a3b 70}
c801d85f 71
9abe166a 72wxString wxControl::GetLabel() const
c801d85f 73{
b292e2f5 74 return m_label;
6de97a3b 75}
c801d85f
KB
76
77
f68586e5
VZ
78wxSize wxControl::DoGetBestSize() const
79{
0279e844
RR
80 // Do not return any arbitrary default value...
81 wxASSERT_MSG( m_widget, wxT("DoGetBestSize called before creation") );
82
f68586e5 83 GtkRequisition req;
33720b2d
RR
84 req.width = 2;
85 req.height = 2;
2afa14f2 86 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request )
f68586e5
VZ
87 (m_widget, &req );
88
9f884528
RD
89 wxSize best(req.width, req.height);
90 CacheBestSize(best);
91 return best;
f68586e5
VZ
92}
93
abdeb9e7
RD
94
95void wxControl::PostCreation(const wxSize& size)
96{
97 wxWindow::PostCreation();
f40fdaa3
VS
98
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);
105
abdeb9e7
RD
106 ApplyWidgetStyle();
107 SetInitialBestSize(size);
108}
109
110
393cbb8f 111#ifdef __WXGTK20__
eaafd2f8
VS
112wxString wxControl::PrepareLabelMnemonics( const wxString &label ) const
113{
114 //Format mnemonics properly for GTK2. This can be called from GTK1.x, but
115 //it's not very useful because mnemonics don't exist prior to GTK2.
116 wxString label2;
117 for (size_t i = 0; i < label.Len(); i++)
118 {
119 if (label.GetChar(i) == wxT('&'))
120 {
121 //Mnemonic escape sequence "&&" is a literal "&" in the output.
122 if (label.GetChar(i + 1) == wxT('&'))
123 {
124 label2 << wxT('&');
125 i++;
126 }
127 //Handle special case of "&_" (i.e. "_" is the mnemonic).
128 //FIXME - Is it possible to use "_" as a GTK mnemonic? Just use a
129 //dash for now.
130 else if (label.GetChar(i + 1) == wxT('_'))
131 {
132 label2 << wxT("_-");
133 i++;
134 }
135 //Replace WX mnemonic indicator "&" with GTK indicator "_".
136 else
137 {
138 label2 << wxT('_');
139 }
140 }
141 else if (label.GetChar(i) == wxT('_'))
142 {
143 //Escape any underlines in the string so GTK doesn't use them.
144 label2 << wxT("__");
145 }
146 else
147 {
148 label2 << label.GetChar(i);
149 }
150 }
151 return label2;
152}
393cbb8f 153#endif
eaafd2f8 154
9d522606
RD
155
156wxVisualAttributes wxControl::GetDefaultAttributes() const
157{
158 return GetDefaultAttributesFromGTKWidget(m_widget,
159 UseGTKStyleBase());
160}
161
162
163#define SHIFT (8*(sizeof(short int)-sizeof(char)))
164
165// static
166wxVisualAttributes
167wxControl::GetDefaultAttributesFromGTKWidget(GtkWidget* widget,
168 bool useBase,
169 int state)
170{
171 GtkStyle* style;
172 wxVisualAttributes attr;
173
174 style = gtk_rc_get_style(widget);
175 if (!style)
176 style = gtk_widget_get_default_style();
177
178 if (!style)
179 {
180 return wxWindow::GetClassDefaultAttributes(wxWINDOW_VARIANT_NORMAL);
181 }
182
183 if (state == -1)
184 state = GTK_STATE_NORMAL;
185
186 // get the style's colours
187 attr.colFg = wxColour(style->fg[state].red >> SHIFT,
188 style->fg[state].green >> SHIFT,
189 style->fg[state].blue >> SHIFT);
190 if (useBase)
191 attr.colBg = wxColour(style->base[state].red >> SHIFT,
192 style->base[state].green >> SHIFT,
193 style->base[state].blue >> SHIFT);
194 else
195 attr.colBg = wxColour(style->bg[state].red >> SHIFT,
196 style->bg[state].green >> SHIFT,
197 style->bg[state].blue >> SHIFT);
198
199 // get the style's font
200#ifdef __WXGTK20__
201 if ( !style->font_desc )
202 style = gtk_widget_get_default_style();
203 if ( style && style->font_desc )
204 {
205 wxNativeFontInfo info;
fdf7514a 206 info.description = pango_font_description_copy(style->font_desc);
9d522606
RD
207 attr.font = wxFont(info);
208 }
209 else
210 {
211 GtkSettings *settings = gtk_settings_get_default();
212 gchar *font_name = NULL;
213 g_object_get ( settings,
214 "gtk-font-name",
215 &font_name,
216 NULL);
217 if (!font_name)
218 attr.font = wxSystemSettings::GetFont( wxSYS_DEFAULT_GUI_FONT );
219 else
220 attr.font = wxFont(wxString::FromAscii(font_name));
221 g_free (font_name);
222 }
223#else
224 // TODO: isn't there a way to get a standard gtk 1.2 font?
225 attr.font = wxFont( 12, wxSWISS, wxNORMAL, wxNORMAL );
226#endif
227
228 return attr;
229}
230
231
232//static
233wxVisualAttributes
865bb325 234wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNew_t widget_new,
9d522606
RD
235 bool useBase,
236 int state)
237{
238 wxVisualAttributes attr;
66d8fe77
VS
239 // NB: we need toplevel window so that GTK+ can find the right style
240 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
9d522606 241 GtkWidget* widget = widget_new();
66d8fe77 242 gtk_container_add(GTK_CONTAINER(wnd), widget);
9d522606 243 attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state);
66d8fe77 244 gtk_widget_destroy(wnd);
9d522606
RD
245 return attr;
246}
247
248//static
249wxVisualAttributes
865bb325 250wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromStr_t widget_new,
9d522606
RD
251 bool useBase,
252 int state)
253{
254 wxVisualAttributes attr;
66d8fe77
VS
255 // NB: we need toplevel window so that GTK+ can find the right style
256 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
9d522606 257 GtkWidget* widget = widget_new("");
66d8fe77 258 gtk_container_add(GTK_CONTAINER(wnd), widget);
9d522606 259 attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state);
66d8fe77 260 gtk_widget_destroy(wnd);
9d522606
RD
261 return attr;
262}
263
264
265//static
266wxVisualAttributes
865bb325 267wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromAdj_t widget_new,
9d522606
RD
268 bool useBase,
269 int state)
270{
271 wxVisualAttributes attr;
66d8fe77
VS
272 // NB: we need toplevel window so that GTK+ can find the right style
273 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
9d522606 274 GtkWidget* widget = widget_new(NULL);
66d8fe77 275 gtk_container_add(GTK_CONTAINER(wnd), widget);
9d522606 276 attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state);
66d8fe77 277 gtk_widget_destroy(wnd);
9d522606
RD
278 return attr;
279}
280
1e6feb95
VZ
281#endif // wxUSE_CONTROLS
282