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