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