]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/control.cpp
add support for custom controls in file dialog in wxGTK and generic versions; also...
[wxWidgets.git] / src / gtk / control.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/control.cpp
3 // Purpose: wxControl implementation for wxGTK
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 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #if wxUSE_CONTROLS
14
15 #include "wx/control.h"
16
17 #ifndef WX_PRECOMP
18 #include "wx/log.h"
19 #include "wx/settings.h"
20 #endif
21
22 #include "wx/fontutil.h"
23 #include "wx/gtk/private.h"
24
25 #include "wx/gtk/private/mnemonics.h"
26
27 // ============================================================================
28 // wxControl implementation
29 // ============================================================================
30
31 // ----------------------------------------------------------------------------
32 // wxControl creation
33 // ----------------------------------------------------------------------------
34
35 IMPLEMENT_DYNAMIC_CLASS(wxControl, wxWindow)
36
37 wxControl::wxControl()
38 {
39 }
40
41 bool wxControl::Create( wxWindow *parent,
42 wxWindowID id,
43 const wxPoint &pos,
44 const wxSize &size,
45 long style,
46 const wxValidator& validator,
47 const wxString &name )
48 {
49 bool ret = wxWindow::Create(parent, id, pos, size, style, name);
50
51 #if wxUSE_VALIDATORS
52 SetValidator(validator);
53 #endif
54
55 return ret;
56 }
57
58 wxSize wxControl::DoGetBestSize() const
59 {
60 // Do not return any arbitrary default value...
61 wxASSERT_MSG( m_widget, wxT("DoGetBestSize called before creation") );
62
63 GtkRequisition req;
64 req.width = 2;
65 req.height = 2;
66 (* GTK_WIDGET_CLASS( GTK_OBJECT_GET_CLASS(m_widget) )->size_request )
67 (m_widget, &req );
68
69 wxSize best(req.width, req.height);
70 CacheBestSize(best);
71 return best;
72 }
73
74 void wxControl::PostCreation(const wxSize& size)
75 {
76 wxWindow::PostCreation();
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);
84
85 ApplyWidgetStyle();
86 SetInitialSize(size);
87 }
88
89 // ----------------------------------------------------------------------------
90 // wxControl dealing with labels
91 // ----------------------------------------------------------------------------
92
93 void wxControl::GTKSetLabelForLabel(GtkLabel *w, const wxString& label)
94 {
95 // save the original label
96 wxControlBase::SetLabel(label);
97
98 const wxString labelGTK = GTKConvertMnemonics(label);
99 gtk_label_set_text_with_mnemonic(w, wxGTK_CONV(labelGTK));
100 }
101
102 void wxControl::GTKSetLabelWithMarkupForLabel(GtkLabel *w, const wxString& label)
103 {
104 const wxString labelGTK = GTKConvertMnemonicsWithMarkup(label);
105 gtk_label_set_markup_with_mnemonic(w, wxGTK_CONV(labelGTK));
106 }
107
108
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
117 GtkWidget* 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
126 return framewidget; // note that the label is already set so you'll
127 // only need to call wxControl::SetLabel afterwards
128 }
129
130 void wxControl::GTKSetLabelForFrame(GtkFrame *w, const wxString& label)
131 {
132 GtkLabel* labelwidget = GTK_LABEL(gtk_frame_get_label_widget(w));
133 GTKSetLabelForLabel(labelwidget, label);
134 }
135
136 void 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 }
141
142 void wxControl::GTKFrameSetMnemonicWidget(GtkFrame* w, GtkWidget* widget)
143 {
144 GtkLabel* labelwidget = GTK_LABEL(gtk_frame_get_label_widget(w));
145
146 gtk_label_set_mnemonic_widget(labelwidget, widget);
147 }
148
149 // ----------------------------------------------------------------------------
150 // worker function implementing GTK*Mnemonics() functions
151 // ----------------------------------------------------------------------------
152
153 /* static */
154 wxString wxControl::GTKRemoveMnemonics(const wxString& label)
155 {
156 return wxGTKRemoveMnemonics(label);
157 }
158
159 /* static */
160 wxString wxControl::GTKConvertMnemonics(const wxString& label)
161 {
162 return wxConvertMnemonicsToGTK(label);
163 }
164
165 /* static */
166 wxString wxControl::GTKConvertMnemonicsWithMarkup(const wxString& label)
167 {
168 return wxConvertMnemonicsToGTKMarkup(label);
169 }
170
171 // ----------------------------------------------------------------------------
172 // wxControl styles (a.k.a. attributes)
173 // ----------------------------------------------------------------------------
174
175 wxVisualAttributes wxControl::GetDefaultAttributes() const
176 {
177 return GetDefaultAttributesFromGTKWidget(m_widget,
178 UseGTKStyleBase());
179 }
180
181 // static
182 wxVisualAttributes
183 wxControl::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;
201
202 // get the style's colours
203 attr.colFg = wxColour(style->fg[state]);
204 if (useBase)
205 attr.colBg = wxColour(style->base[state]);
206 else
207 attr.colBg = wxColour(style->bg[state]);
208
209 // get the style's font
210 if ( !style->font_desc )
211 style = gtk_widget_get_default_style();
212 if ( style && style->font_desc )
213 {
214 wxNativeFontInfo info;
215 info.description = pango_font_description_copy(style->font_desc);
216 attr.font = wxFont(info);
217 }
218 else
219 {
220 GtkSettings *settings = gtk_settings_get_default();
221 gchar *font_name = NULL;
222 g_object_get ( settings,
223 "gtk-font-name",
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);
231 }
232
233 return attr;
234 }
235
236
237 //static
238 wxVisualAttributes
239 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNew_t widget_new,
240 bool useBase,
241 int state)
242 {
243 wxVisualAttributes attr;
244 // NB: we need toplevel window so that GTK+ can find the right style
245 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
246 GtkWidget* widget = widget_new();
247 gtk_container_add(GTK_CONTAINER(wnd), widget);
248 attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state);
249 gtk_widget_destroy(wnd);
250 return attr;
251 }
252
253 //static
254 wxVisualAttributes
255 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromStr_t widget_new,
256 bool useBase,
257 int state)
258 {
259 wxVisualAttributes attr;
260 // NB: we need toplevel window so that GTK+ can find the right style
261 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
262 GtkWidget* widget = widget_new("");
263 gtk_container_add(GTK_CONTAINER(wnd), widget);
264 attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state);
265 gtk_widget_destroy(wnd);
266 return attr;
267 }
268
269
270 //static
271 wxVisualAttributes
272 wxControl::GetDefaultAttributesFromGTKWidget(wxGtkWidgetNewFromAdj_t widget_new,
273 bool useBase,
274 int state)
275 {
276 wxVisualAttributes attr;
277 // NB: we need toplevel window so that GTK+ can find the right style
278 GtkWidget *wnd = gtk_window_new(GTK_WINDOW_TOPLEVEL);
279 GtkWidget* widget = widget_new(NULL);
280 gtk_container_add(GTK_CONTAINER(wnd), widget);
281 attr = GetDefaultAttributesFromGTKWidget(widget, useBase, state);
282 gtk_widget_destroy(wnd);
283 return attr;
284 }
285
286 // ----------------------------------------------------------------------------
287 // idle handling
288 // ----------------------------------------------------------------------------
289
290 void wxControl::OnInternalIdle()
291 {
292 if ( GtkShowFromOnIdle() )
293 return;
294
295 if ( GTK_WIDGET_REALIZED(m_widget) )
296 {
297 GTKUpdateCursor();
298
299 GTKSetDelayedFocusIfNeeded();
300 }
301
302 if ( wxUpdateUIEvent::CanUpdate(this) )
303 UpdateWindowUI(wxUPDATE_UI_FROMIDLE);
304 }
305
306 #endif // wxUSE_CONTROLS