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