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