]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/settings.cpp
Ensure that validators work even in presence of pushed event handlers.
[wxWidgets.git] / src / gtk / settings.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/settings.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Modified by: Mart Raudsepp (GetMetric)
6 // Id: $Id$
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
13
14 #include "wx/settings.h"
15
16 #ifndef WX_PRECOMP
17 #include "wx/cmndata.h"
18 #include "wx/toplevel.h"
19 #endif
20
21 #include "wx/fontutil.h"
22 #include "wx/fontenum.h"
23
24 #include <gtk/gtk.h>
25 #include "wx/gtk/private/win_gtk.h"
26
27 bool wxGetFrameExtents(GdkWindow* window, int* left, int* right, int* top, int* bottom);
28
29 // ----------------------------------------------------------------------------
30 // wxSystemSettings implementation
31 // ----------------------------------------------------------------------------
32
33 static wxFont gs_fontSystem;
34
35 static GtkContainer* ContainerWidget()
36 {
37 static GtkContainer* s_widget;
38 if (s_widget == NULL)
39 {
40 s_widget = GTK_CONTAINER(gtk_fixed_new());
41 GtkWidget* window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
42 gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(s_widget));
43 }
44 return s_widget;
45 }
46
47 extern "C" {
48 static void style_set(GtkWidget*, GtkStyle*, void*)
49 {
50 gs_fontSystem = wxNullFont;
51 }
52 }
53
54 static const GtkStyle* ButtonStyle()
55 {
56 static GtkWidget* s_widget;
57 if (s_widget == NULL)
58 {
59 s_widget = gtk_button_new();
60 gtk_container_add(ContainerWidget(), s_widget);
61 gtk_widget_ensure_style(s_widget);
62 g_signal_connect(s_widget, "style_set", G_CALLBACK(style_set), NULL);
63 }
64 return s_widget->style;
65 }
66
67 static const GtkStyle* ListStyle()
68 {
69 static GtkWidget* s_widget;
70 if (s_widget == NULL)
71 {
72 s_widget = gtk_tree_view_new_with_model(
73 GTK_TREE_MODEL(gtk_list_store_new(1, G_TYPE_INT)));
74 gtk_container_add(ContainerWidget(), s_widget);
75 gtk_widget_ensure_style(s_widget);
76 }
77 return s_widget->style;
78 }
79
80 static const GtkStyle* TextCtrlStyle()
81 {
82 static GtkWidget* s_widget;
83 if (s_widget == NULL)
84 {
85 s_widget = gtk_text_view_new();
86 gtk_container_add(ContainerWidget(), s_widget);
87 gtk_widget_ensure_style(s_widget);
88 }
89 return s_widget->style;
90 }
91
92 static const GtkStyle* MenuItemStyle()
93 {
94 static GtkWidget* s_widget;
95 if (s_widget == NULL)
96 {
97 s_widget = gtk_menu_item_new();
98 gtk_container_add(ContainerWidget(), s_widget);
99 gtk_widget_ensure_style(s_widget);
100 }
101 return s_widget->style;
102 }
103
104 static const GtkStyle* MenuBarStyle()
105 {
106 static GtkWidget* s_widget;
107 if (s_widget == NULL)
108 {
109 s_widget = gtk_menu_bar_new();
110 gtk_container_add(ContainerWidget(), s_widget);
111 gtk_widget_ensure_style(s_widget);
112 }
113 return s_widget->style;
114 }
115
116 static const GtkStyle* ToolTipStyle()
117 {
118 static GtkWidget* s_widget;
119 if (s_widget == NULL)
120 {
121 s_widget = gtk_window_new(GTK_WINDOW_POPUP);
122 const char* name = "gtk-tooltip";
123 if (gtk_check_version(2, 11, 0))
124 name = "gtk-tooltips";
125 gtk_widget_set_name(s_widget, name);
126 gtk_widget_ensure_style(s_widget);
127 }
128 return s_widget->style;
129 }
130
131 wxColour wxSystemSettingsNative::GetColour( wxSystemColour index )
132 {
133 wxColor color;
134 switch (index)
135 {
136 case wxSYS_COLOUR_SCROLLBAR:
137 case wxSYS_COLOUR_BACKGROUND:
138 //case wxSYS_COLOUR_DESKTOP:
139 case wxSYS_COLOUR_INACTIVECAPTION:
140 case wxSYS_COLOUR_MENU:
141 case wxSYS_COLOUR_WINDOWFRAME:
142 case wxSYS_COLOUR_ACTIVEBORDER:
143 case wxSYS_COLOUR_INACTIVEBORDER:
144 case wxSYS_COLOUR_BTNFACE:
145 //case wxSYS_COLOUR_3DFACE:
146 case wxSYS_COLOUR_3DLIGHT:
147 color = wxColor(ButtonStyle()->bg[GTK_STATE_NORMAL]);
148 break;
149
150 case wxSYS_COLOUR_WINDOW:
151 color = wxColor(TextCtrlStyle()->base[GTK_STATE_NORMAL]);
152 break;
153
154 case wxSYS_COLOUR_MENUBAR:
155 color = wxColor(MenuBarStyle()->bg[GTK_STATE_NORMAL]);
156 break;
157
158 case wxSYS_COLOUR_3DDKSHADOW:
159 color = *wxBLACK;
160 break;
161
162 case wxSYS_COLOUR_GRAYTEXT:
163 case wxSYS_COLOUR_BTNSHADOW:
164 //case wxSYS_COLOUR_3DSHADOW:
165 {
166 wxColour faceColour(GetColour(wxSYS_COLOUR_3DFACE));
167 color =
168 wxColour((unsigned char) (faceColour.Red() * 2 / 3),
169 (unsigned char) (faceColour.Green() * 2 / 3),
170 (unsigned char) (faceColour.Blue() * 2 / 3));
171 }
172 break;
173
174 case wxSYS_COLOUR_BTNHIGHLIGHT:
175 //case wxSYS_COLOUR_BTNHILIGHT:
176 //case wxSYS_COLOUR_3DHIGHLIGHT:
177 //case wxSYS_COLOUR_3DHILIGHT:
178 color = *wxWHITE;
179 break;
180
181 case wxSYS_COLOUR_HIGHLIGHT:
182 color = wxColor(ButtonStyle()->bg[GTK_STATE_SELECTED]);
183 break;
184
185 case wxSYS_COLOUR_LISTBOX:
186 color = wxColor(ListStyle()->base[GTK_STATE_NORMAL]);
187 break;
188
189 case wxSYS_COLOUR_LISTBOXTEXT:
190 color = wxColor(ListStyle()->text[GTK_STATE_NORMAL]);
191 break;
192
193 case wxSYS_COLOUR_MENUTEXT:
194 case wxSYS_COLOUR_WINDOWTEXT:
195 case wxSYS_COLOUR_CAPTIONTEXT:
196 case wxSYS_COLOUR_INACTIVECAPTIONTEXT:
197 case wxSYS_COLOUR_BTNTEXT:
198 color = wxColor(ButtonStyle()->fg[GTK_STATE_NORMAL]);
199 break;
200
201 case wxSYS_COLOUR_INFOBK:
202 color = wxColor(ToolTipStyle()->bg[GTK_STATE_NORMAL]);
203 break;
204
205 case wxSYS_COLOUR_INFOTEXT:
206 color = wxColor(ToolTipStyle()->fg[GTK_STATE_NORMAL]);
207 break;
208
209 case wxSYS_COLOUR_HIGHLIGHTTEXT:
210 color = wxColor(ButtonStyle()->fg[GTK_STATE_SELECTED]);
211 break;
212
213 case wxSYS_COLOUR_APPWORKSPACE:
214 color = *wxWHITE; // ?
215 break;
216
217 case wxSYS_COLOUR_ACTIVECAPTION:
218 case wxSYS_COLOUR_MENUHILIGHT:
219 color = wxColor(MenuItemStyle()->bg[GTK_STATE_SELECTED]);
220 break;
221
222 case wxSYS_COLOUR_HOTLIGHT:
223 case wxSYS_COLOUR_GRADIENTACTIVECAPTION:
224 case wxSYS_COLOUR_GRADIENTINACTIVECAPTION:
225 // TODO
226 color = *wxBLACK;
227 break;
228
229 case wxSYS_COLOUR_MAX:
230 default:
231 wxFAIL_MSG( wxT("unknown system colour index") );
232 color = *wxWHITE;
233 break;
234 }
235
236 wxASSERT(color.IsOk());
237 return color;
238 }
239
240 wxFont wxSystemSettingsNative::GetFont( wxSystemFont index )
241 {
242 wxFont font;
243 switch (index)
244 {
245 case wxSYS_OEM_FIXED_FONT:
246 case wxSYS_ANSI_FIXED_FONT:
247 case wxSYS_SYSTEM_FIXED_FONT:
248 font = *wxNORMAL_FONT;
249 break;
250
251 case wxSYS_ANSI_VAR_FONT:
252 case wxSYS_SYSTEM_FONT:
253 case wxSYS_DEVICE_DEFAULT_FONT:
254 case wxSYS_DEFAULT_GUI_FONT:
255 if (!gs_fontSystem.Ok())
256 {
257 wxNativeFontInfo info;
258 info.description = ButtonStyle()->font_desc;
259 gs_fontSystem = wxFont(info);
260
261 #if wxUSE_FONTENUM
262 // (try to) heal the default font (on some common systems e.g. Ubuntu
263 // it's "Sans Serif" but the real font is called "Sans"):
264 if (!wxFontEnumerator::IsValidFacename(gs_fontSystem.GetFaceName()) &&
265 gs_fontSystem.GetFaceName() == "Sans Serif")
266 gs_fontSystem.SetFaceName("Sans");
267 #endif // wxUSE_FONTENUM
268
269 info.description = NULL;
270 }
271 font = gs_fontSystem;
272 break;
273
274 default:
275 break;
276 }
277
278 wxASSERT( font.IsOk() );
279
280 return font;
281 }
282
283 // helper: return the GtkSettings either for the screen the current window is
284 // on or for the default screen if window is NULL
285 static GtkSettings *GetSettingsForWindowScreen(GdkWindow *window)
286 {
287 return window ? gtk_settings_get_for_screen(gdk_drawable_get_screen(window))
288 : gtk_settings_get_default();
289 }
290
291 static int GetBorderWidth(wxSystemMetric index, wxWindow* win)
292 {
293 if (win->m_wxwindow)
294 {
295 wxPizza* pizza = WX_PIZZA(win->m_wxwindow);
296 int x, y;
297 pizza->get_border_widths(x, y);
298 switch (index)
299 {
300 case wxSYS_BORDER_X:
301 case wxSYS_EDGE_X:
302 case wxSYS_FRAMESIZE_X:
303 return x;
304 default:
305 return y;
306 }
307 }
308 return -1;
309 }
310
311 int wxSystemSettingsNative::GetMetric( wxSystemMetric index, wxWindow* win )
312 {
313 GdkWindow *window = NULL;
314 if(win && GTK_WIDGET_REALIZED(win->GetHandle()))
315 window = win->GetHandle()->window;
316
317 switch (index)
318 {
319 case wxSYS_BORDER_X:
320 case wxSYS_BORDER_Y:
321 case wxSYS_EDGE_X:
322 case wxSYS_EDGE_Y:
323 case wxSYS_FRAMESIZE_X:
324 case wxSYS_FRAMESIZE_Y:
325 if (win)
326 {
327 wxTopLevelWindow *tlw = wxDynamicCast(win, wxTopLevelWindow);
328 if (!tlw)
329 return GetBorderWidth(index, win);
330 else if (window)
331 {
332 // Get the frame extents from the windowmanager.
333 // In most cases the top extent is the titlebar, so we use the bottom extent
334 // for the heights.
335 int right, bottom;
336 if (wxGetFrameExtents(window, NULL, &right, NULL, &bottom))
337 {
338 switch (index)
339 {
340 case wxSYS_BORDER_X:
341 case wxSYS_EDGE_X:
342 case wxSYS_FRAMESIZE_X:
343 return right; // width of right extent
344 default:
345 return bottom; // height of bottom extent
346 }
347 }
348 }
349 }
350
351 return -1; // no window specified
352
353 case wxSYS_CURSOR_X:
354 case wxSYS_CURSOR_Y:
355 return gdk_display_get_default_cursor_size(
356 window ? gdk_drawable_get_display(window)
357 : gdk_display_get_default());
358
359 case wxSYS_DCLICK_X:
360 case wxSYS_DCLICK_Y:
361 gint dclick_distance;
362 g_object_get(GetSettingsForWindowScreen(window),
363 "gtk-double-click-distance", &dclick_distance, NULL);
364
365 return dclick_distance * 2;
366
367 case wxSYS_DCLICK_MSEC:
368 gint dclick;
369 g_object_get(GetSettingsForWindowScreen(window),
370 "gtk-double-click-time", &dclick, NULL);
371 return dclick;
372
373 case wxSYS_DRAG_X:
374 case wxSYS_DRAG_Y:
375 gint drag_threshold;
376 g_object_get(GetSettingsForWindowScreen(window),
377 "gtk-dnd-drag-threshold", &drag_threshold, NULL);
378
379 // The correct thing here would be to double the value
380 // since that is what the API wants. But the values
381 // are much bigger under GNOME than under Windows and
382 // just seem to much in many cases to be useful.
383 // drag_threshold *= 2;
384
385 return drag_threshold;
386
387 case wxSYS_ICON_X:
388 case wxSYS_ICON_Y:
389 return 32;
390
391 case wxSYS_SCREEN_X:
392 if (window)
393 return gdk_screen_get_width(gdk_drawable_get_screen(window));
394 else
395 return gdk_screen_width();
396
397 case wxSYS_SCREEN_Y:
398 if (window)
399 return gdk_screen_get_height(gdk_drawable_get_screen(window));
400 else
401 return gdk_screen_height();
402
403 case wxSYS_HSCROLL_Y:
404 case wxSYS_VSCROLL_X:
405 return 15;
406
407 case wxSYS_CAPTION_Y:
408 if (!window)
409 // No realized window specified, and no implementation for that case yet.
410 return -1;
411
412 wxASSERT_MSG( wxDynamicCast(win, wxTopLevelWindow),
413 wxT("Asking for caption height of a non toplevel window") );
414
415 // Get the height of the top windowmanager border.
416 // This is the titlebar in most cases. The titlebar might be elsewhere, and
417 // we could check which is the thickest wm border to decide on which side the
418 // titlebar is, but this might lead to interesting behaviours in used code.
419 // Reconsider when we have a way to report to the user on which side it is.
420 {
421 int top;
422 if (wxGetFrameExtents(window, NULL, NULL, &top, NULL))
423 {
424 return top; // top frame extent
425 }
426 }
427
428 // Try a default approach without a window pointer, if possible
429 // ...
430
431 return -1;
432
433 case wxSYS_PENWINDOWS_PRESENT:
434 // No MS Windows for Pen computing extension available in X11 based gtk+.
435 return 0;
436
437 default:
438 return -1; // metric is unknown
439 }
440 }
441
442 bool wxSystemSettingsNative::HasFeature(wxSystemFeature index)
443 {
444 switch (index)
445 {
446 case wxSYS_CAN_ICONIZE_FRAME:
447 return false;
448
449 case wxSYS_CAN_DRAW_FRAME_DECORATIONS:
450 return true;
451
452 default:
453 return false;
454 }
455 }