put frame extents XGetWindowProperty code in one place
[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/gtkversion.h>
24 #if GTK_CHECK_VERSION(2, 9, 0)
25 // gtk_object_sink
26 #undef GTK_DISABLE_DEPRECATED
27 #endif
28 #include <gtk/gtk.h>
29
30 bool wxGetFrameExtents(GdkWindow* window, int* left, int* right, int* top, int* bottom);
31
32 // ----------------------------------------------------------------------------
33 // wxSystemObjects
34 // ----------------------------------------------------------------------------
35
36 struct wxSystemObjects
37 {
38 wxColour m_colBtnFace,
39 m_colBtnShadow,
40 m_colBtnHighlight,
41 m_colHighlight,
42 m_colHighlightText,
43 m_colListBox,
44 m_colWindow,
45 m_colWindowText,
46 m_colBtnText,
47 m_colMenuItemHighlight,
48 m_colTooltip,
49 m_colTooltipText,
50 m_colMenubarBg;
51
52 wxFont m_fontSystem;
53 };
54
55 static wxSystemObjects gs_objects;
56
57 void wxClearGtkSystemObjects()
58 {
59 gs_objects.m_colBtnFace = wxColour();
60 gs_objects.m_colBtnShadow = wxColour();
61 gs_objects.m_colBtnHighlight = wxColour();
62 gs_objects.m_colHighlightText = wxColour();
63 gs_objects.m_colListBox = wxColour();
64 gs_objects.m_colWindow = wxColour();
65 gs_objects.m_colWindowText = wxColour();
66 gs_objects.m_colBtnText = wxColour();
67 gs_objects.m_colMenuItemHighlight = wxColour();
68 gs_objects.m_colTooltip = wxColour();
69 gs_objects.m_colTooltipText = wxColour();
70 gs_objects.m_colMenubarBg = wxColour();
71 gs_objects.m_fontSystem = wxNullFont;
72 }
73
74 // ----------------------------------------------------------------------------
75 // wxSystemSettings implementation
76 // ----------------------------------------------------------------------------
77
78 // kind of widget to use in GetColourFromGTKWidget
79 enum wxGtkWidgetType
80 {
81 wxGTK_BUTTON,
82 wxGTK_LIST,
83 wxGTK_MENUITEM,
84 wxGTK_TEXTCTRL,
85 wxGTK_MENUBAR,
86 };
87
88 // the colour we need
89 enum wxGtkColourType
90 {
91 wxGTK_FG,
92 wxGTK_BG,
93 wxGTK_BASE
94 };
95
96 // wxSystemSettings::GetColour() helper: get the colours from a GTK+
97 // widget style, return true if we did get them
98 static bool GetColourFromGTKWidget(GdkColor& gdkColor,
99 wxGtkWidgetType type = wxGTK_BUTTON,
100 GtkStateType state = GTK_STATE_NORMAL,
101 wxGtkColourType colour = wxGTK_BG)
102 {
103 GtkWidget *widget;
104 switch ( type )
105 {
106 default:
107 wxFAIL_MSG( _T("unexpected GTK widget type") );
108 // fall through
109
110 case wxGTK_BUTTON:
111 widget = gtk_button_new();
112 break;
113
114 case wxGTK_TEXTCTRL:
115 widget = gtk_text_view_new();
116 break;
117
118 case wxGTK_LIST:
119 widget = gtk_tree_view_new_with_model(
120 (GtkTreeModel*)gtk_list_store_new(1, G_TYPE_INT));
121 break;
122
123 case wxGTK_MENUITEM:
124 widget = gtk_menu_item_new();
125
126 case wxGTK_MENUBAR:
127 widget = gtk_menu_bar_new();
128 break;
129
130 }
131
132 GtkStyle *def = gtk_rc_get_style( widget );
133 if ( !def )
134 def = gtk_widget_get_default_style();
135
136 const bool ok = def != NULL;
137 if (ok)
138 {
139 switch ( colour )
140 {
141 default:
142 wxFAIL_MSG( _T("unexpected GTK colour type") );
143 // fall through
144
145 case wxGTK_FG:
146 gdkColor = def->fg[state];
147 break;
148
149 case wxGTK_BG:
150 gdkColor = def->bg[state];
151 break;
152
153 case wxGTK_BASE:
154 gdkColor = def->base[state];
155 break;
156 }
157 }
158
159 gtk_object_sink((GtkObject*)widget);
160
161 return ok;
162 }
163
164 static void GetTooltipColors()
165 {
166 GtkWidget* widget = gtk_window_new(GTK_WINDOW_POPUP);
167 const char* name = "gtk-tooltip";
168 if (gtk_check_version(2, 11, 0))
169 name = "gtk-tooltips";
170 gtk_widget_set_name(widget, name);
171 gtk_widget_ensure_style(widget);
172
173 GdkColor c = widget->style->bg[GTK_STATE_NORMAL];
174 gs_objects.m_colTooltip = wxColor(c);
175 c = widget->style->fg[GTK_STATE_NORMAL];
176 gs_objects.m_colTooltipText = wxColor(c);
177
178 gtk_widget_destroy(widget);
179 }
180
181 wxColour wxSystemSettingsNative::GetColour( wxSystemColour index )
182 {
183 wxColor color;
184 GdkColor gdkColor;
185 switch (index)
186 {
187 case wxSYS_COLOUR_SCROLLBAR:
188 case wxSYS_COLOUR_BACKGROUND:
189 case wxSYS_COLOUR_INACTIVECAPTION:
190 case wxSYS_COLOUR_MENU:
191 case wxSYS_COLOUR_WINDOWFRAME:
192 case wxSYS_COLOUR_ACTIVEBORDER:
193 case wxSYS_COLOUR_INACTIVEBORDER:
194 case wxSYS_COLOUR_BTNFACE:
195 case wxSYS_COLOUR_3DLIGHT:
196 if (!gs_objects.m_colBtnFace.Ok())
197 {
198 gdkColor.red =
199 gdkColor.green = 0;
200 gdkColor.blue = 0x9c40;
201 GetColourFromGTKWidget(gdkColor);
202 gs_objects.m_colBtnFace = wxColor(gdkColor);
203 }
204 color = gs_objects.m_colBtnFace;
205 break;
206
207 case wxSYS_COLOUR_WINDOW:
208 if (!gs_objects.m_colWindow.Ok())
209 {
210 gdkColor.red =
211 gdkColor.green =
212 gdkColor.blue = 0xFFFF;
213 GetColourFromGTKWidget(gdkColor, wxGTK_TEXTCTRL, GTK_STATE_NORMAL, wxGTK_BASE);
214 gs_objects.m_colWindow = wxColor(gdkColor);
215 }
216 color = gs_objects.m_colWindow;
217 break;
218
219
220 case wxSYS_COLOUR_MENUBAR:
221 if (!gs_objects.m_colMenubarBg.Ok())
222 {
223 gdkColor.red =
224 gdkColor.green = 0;
225 gdkColor.blue = 0x9c40;
226 GetColourFromGTKWidget(gdkColor,wxGTK_MENUBAR);
227 gs_objects.m_colMenubarBg = wxColor(gdkColor);
228 }
229 color = gs_objects.m_colMenubarBg;
230 break;
231
232 case wxSYS_COLOUR_3DDKSHADOW:
233 color = *wxBLACK;
234 break;
235
236 case wxSYS_COLOUR_GRAYTEXT:
237 case wxSYS_COLOUR_BTNSHADOW:
238 //case wxSYS_COLOUR_3DSHADOW:
239 if (!gs_objects.m_colBtnShadow.Ok())
240 {
241 wxColour faceColour(GetColour(wxSYS_COLOUR_3DFACE));
242 gs_objects.m_colBtnShadow =
243 wxColour((unsigned char) (faceColour.Red() * 2 / 3),
244 (unsigned char) (faceColour.Green() * 2 / 3),
245 (unsigned char) (faceColour.Blue() * 2 / 3));
246 }
247 color = gs_objects.m_colBtnShadow;
248 break;
249
250 case wxSYS_COLOUR_3DHIGHLIGHT:
251 //case wxSYS_COLOUR_BTNHIGHLIGHT:
252 color = *wxWHITE;
253 break;
254
255 case wxSYS_COLOUR_HIGHLIGHT:
256 if (!gs_objects.m_colHighlight.Ok())
257 {
258 gdkColor.red =
259 gdkColor.green = 0;
260 gdkColor.blue = 0x9c40;
261 GetColourFromGTKWidget(
262 gdkColor, wxGTK_BUTTON, GTK_STATE_SELECTED);
263 gs_objects.m_colHighlight = wxColour(gdkColor);
264 }
265 color = gs_objects.m_colHighlight;
266 break;
267
268 case wxSYS_COLOUR_LISTBOX:
269 if (!gs_objects.m_colListBox.Ok())
270 {
271 if ( GetColourFromGTKWidget(gdkColor,
272 wxGTK_LIST,
273 GTK_STATE_NORMAL,
274 wxGTK_BASE) )
275 {
276 gs_objects.m_colListBox = wxColour(gdkColor);
277 }
278 else
279 {
280 gs_objects.m_colListBox = *wxWHITE;
281 }
282 }
283 color = gs_objects.m_colListBox;
284 break;
285
286 case wxSYS_COLOUR_MENUTEXT:
287 case wxSYS_COLOUR_WINDOWTEXT:
288 case wxSYS_COLOUR_CAPTIONTEXT:
289 case wxSYS_COLOUR_INACTIVECAPTIONTEXT:
290 case wxSYS_COLOUR_BTNTEXT:
291 if (!gs_objects.m_colBtnText.Ok())
292 {
293 gdkColor.red =
294 gdkColor.green =
295 gdkColor.blue = 0;
296 GetColourFromGTKWidget(
297 gdkColor, wxGTK_BUTTON, GTK_STATE_NORMAL, wxGTK_FG);
298 gs_objects.m_colBtnText = wxColour(gdkColor);
299 }
300 color = gs_objects.m_colBtnText;
301 break;
302
303 case wxSYS_COLOUR_INFOBK:
304 if (!gs_objects.m_colTooltip.Ok()) {
305 GetTooltipColors();
306 }
307 color = gs_objects.m_colTooltip;
308 break;
309
310 case wxSYS_COLOUR_INFOTEXT:
311 if (!gs_objects.m_colTooltipText.Ok()) {
312 GetTooltipColors();
313 }
314 color = gs_objects.m_colTooltipText;
315 break;
316
317 case wxSYS_COLOUR_HIGHLIGHTTEXT:
318 if (!gs_objects.m_colHighlightText.Ok())
319 {
320 wxColour hclr = GetColour(wxSYS_COLOUR_HIGHLIGHT);
321 if (hclr.Red() > 200 && hclr.Green() > 200 && hclr.Blue() > 200)
322 gs_objects.m_colHighlightText = *wxBLACK;
323 else
324 gs_objects.m_colHighlightText = *wxWHITE;
325 }
326 color = gs_objects.m_colHighlightText;
327 break;
328
329 case wxSYS_COLOUR_APPWORKSPACE:
330 color = *wxWHITE; // ?
331 break;
332
333 case wxSYS_COLOUR_ACTIVECAPTION:
334 case wxSYS_COLOUR_MENUHILIGHT:
335 if (!gs_objects.m_colMenuItemHighlight.Ok())
336 {
337 gdkColor.red =
338 gdkColor.green =
339 gdkColor.blue = 0;
340 GetColourFromGTKWidget(
341 gdkColor, wxGTK_MENUITEM, GTK_STATE_SELECTED, wxGTK_BG);
342 gs_objects.m_colMenuItemHighlight = wxColour(gdkColor);
343 }
344 color = gs_objects.m_colMenuItemHighlight;
345 break;
346
347 case wxSYS_COLOUR_HOTLIGHT:
348 case wxSYS_COLOUR_GRADIENTACTIVECAPTION:
349 case wxSYS_COLOUR_GRADIENTINACTIVECAPTION:
350 // TODO
351 color = *wxBLACK;
352 break;
353
354 case wxSYS_COLOUR_MAX:
355 default:
356 wxFAIL_MSG( _T("unknown system colour index") );
357 color = *wxWHITE;
358 break;
359 }
360
361 return color;
362 }
363
364 wxFont wxSystemSettingsNative::GetFont( wxSystemFont index )
365 {
366 wxFont font;
367 switch (index)
368 {
369 case wxSYS_OEM_FIXED_FONT:
370 case wxSYS_ANSI_FIXED_FONT:
371 case wxSYS_SYSTEM_FIXED_FONT:
372 font = *wxNORMAL_FONT;
373 break;
374
375 case wxSYS_ANSI_VAR_FONT:
376 case wxSYS_SYSTEM_FONT:
377 case wxSYS_DEVICE_DEFAULT_FONT:
378 case wxSYS_DEFAULT_GUI_FONT:
379 if (!gs_objects.m_fontSystem.Ok())
380 {
381 GtkWidget *widget = gtk_button_new();
382 GtkStyle *def = gtk_rc_get_style( widget );
383 if ( !def || !def->font_desc )
384 def = gtk_widget_get_default_style();
385 if ( def && def->font_desc )
386 {
387 wxNativeFontInfo info;
388 info.description =
389 pango_font_description_copy(def->font_desc);
390 gs_objects.m_fontSystem = wxFont(info);
391 }
392 else
393 {
394 GtkSettings *settings = gtk_settings_get_default();
395 gchar *font_name = NULL;
396 g_object_get ( settings,
397 "gtk-font-name",
398 &font_name,
399 NULL);
400 if (!font_name)
401 gs_objects.m_fontSystem = wxFont( 12, wxSWISS, wxNORMAL, wxNORMAL );
402 else
403 gs_objects.m_fontSystem = wxFont(wxString::FromAscii(font_name));
404 g_free (font_name);
405 }
406 gtk_object_sink((GtkObject*)widget);
407 }
408 font = gs_objects.m_fontSystem;
409 break;
410
411 default:
412 break;
413 }
414 return font;
415 }
416
417 // helper: return the GtkSettings either for the screen the current window is
418 // on or for the default screen if window is NULL
419 static GtkSettings *GetSettingsForWindowScreen(GdkWindow *window)
420 {
421 return window ? gtk_settings_get_for_screen(gdk_drawable_get_screen(window))
422 : gtk_settings_get_default();
423 }
424
425 int wxSystemSettingsNative::GetMetric( wxSystemMetric index, wxWindow* win )
426 {
427 GdkWindow *window = NULL;
428 if(win && GTK_WIDGET_REALIZED(win->GetHandle()))
429 window = win->GetHandle()->window;
430
431 switch (index)
432 {
433 case wxSYS_BORDER_X:
434 case wxSYS_BORDER_Y:
435 case wxSYS_EDGE_X:
436 case wxSYS_EDGE_Y:
437 case wxSYS_FRAMESIZE_X:
438 case wxSYS_FRAMESIZE_Y:
439 // If a window is specified/realized, and it is a toplevel window, we can query from wm.
440 // The returned border thickness is outside the client area in that case.
441 if (window)
442 {
443 wxTopLevelWindow *tlw = wxDynamicCast(win, wxTopLevelWindow);
444 if (!tlw)
445 return -1; // not a tlw, not sure how to approach
446 else
447 {
448 // Get the frame extents from the windowmanager.
449 // In most cases the top extent is the titlebar, so we use the bottom extent
450 // for the heights.
451 int right, bottom;
452 if (wxGetFrameExtents(window, NULL, &right, NULL, &bottom))
453 {
454 switch (index)
455 {
456 case wxSYS_BORDER_X:
457 case wxSYS_EDGE_X:
458 case wxSYS_FRAMESIZE_X:
459 return right; // width of right extent
460 default:
461 return bottom; // height of bottom extent
462 }
463 }
464 }
465 }
466
467 return -1; // no window specified
468
469 case wxSYS_CURSOR_X:
470 case wxSYS_CURSOR_Y:
471 return gdk_display_get_default_cursor_size(
472 window ? gdk_drawable_get_display(window)
473 : gdk_display_get_default());
474
475 case wxSYS_DCLICK_X:
476 case wxSYS_DCLICK_Y:
477 gint dclick_distance;
478 g_object_get(GetSettingsForWindowScreen(window),
479 "gtk-double-click-distance", &dclick_distance, NULL);
480
481 return dclick_distance * 2;
482
483 case wxSYS_DCLICK_MSEC:
484 gint dclick;
485 g_object_get(GetSettingsForWindowScreen(window),
486 "gtk-double-click-time", &dclick, NULL);
487 return dclick;
488
489 case wxSYS_DRAG_X:
490 case wxSYS_DRAG_Y:
491 gint drag_threshold;
492 g_object_get(GetSettingsForWindowScreen(window),
493 "gtk-dnd-drag-threshold", &drag_threshold, NULL);
494
495 // The correct thing here would be to double the value
496 // since that is what the API wants. But the values
497 // are much bigger under GNOME than under Windows and
498 // just seem to much in many cases to be useful.
499 // drag_threshold *= 2;
500
501 return drag_threshold;
502
503 case wxSYS_ICON_X:
504 case wxSYS_ICON_Y:
505 return 32;
506
507 case wxSYS_SCREEN_X:
508 if (window)
509 return gdk_screen_get_width(gdk_drawable_get_screen(window));
510 else
511 return gdk_screen_width();
512
513 case wxSYS_SCREEN_Y:
514 if (window)
515 return gdk_screen_get_height(gdk_drawable_get_screen(window));
516 else
517 return gdk_screen_height();
518
519 case wxSYS_HSCROLL_Y:
520 case wxSYS_VSCROLL_X:
521 return 15;
522
523 case wxSYS_CAPTION_Y:
524 if (!window)
525 // No realized window specified, and no implementation for that case yet.
526 return -1;
527
528 wxASSERT_MSG( wxDynamicCast(win, wxTopLevelWindow),
529 wxT("Asking for caption height of a non toplevel window") );
530
531 // Get the height of the top windowmanager border.
532 // This is the titlebar in most cases. The titlebar might be elsewhere, and
533 // we could check which is the thickest wm border to decide on which side the
534 // titlebar is, but this might lead to interesting behaviours in used code.
535 // Reconsider when we have a way to report to the user on which side it is.
536 {
537 int top;
538 if (wxGetFrameExtents(window, NULL, NULL, &top, NULL))
539 {
540 return top; // top frame extent
541 }
542 }
543
544 // Try a default approach without a window pointer, if possible
545 // ...
546
547 return -1;
548
549 case wxSYS_PENWINDOWS_PRESENT:
550 // No MS Windows for Pen computing extension available in X11 based gtk+.
551 return 0;
552
553 default:
554 return -1; // metric is unknown
555 }
556 }
557
558 bool wxSystemSettingsNative::HasFeature(wxSystemFeature index)
559 {
560 switch (index)
561 {
562 case wxSYS_CAN_ICONIZE_FRAME:
563 return false;
564
565 case wxSYS_CAN_DRAW_FRAME_DECORATIONS:
566 return true;
567
568 default:
569 return false;
570 }
571 }