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