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