]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/settings.cpp
For consistency with the docs and with wxMSW the keycode for
[wxWidgets.git] / src / gtk / settings.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #include "wx/debug.h"
16 #include "wx/cmndata.h"
17 #include "wx/fontutil.h"
18 #include "wx/toplevel.h"
19
20 #include <gdk/gdk.h>
21 #include <gdk/gdkx.h>
22 #include <gdk/gdkprivate.h>
23 #include <gtk/gtk.h>
24
25 #include <X11/Xatom.h>
26
27 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
28
29 // ----------------------------------------------------------------------------
30 // wxSystemObjects
31 // ----------------------------------------------------------------------------
32
33 struct wxSystemObjects
34 {
35 wxColour m_colBtnFace,
36 m_colBtnShadow,
37 m_colBtnHighlight,
38 m_colHighlight,
39 m_colHighlightText,
40 m_colListBox,
41 m_colBtnText,
42 m_colMenuItemHighlight,
43 m_colTooltip,
44 m_colTooltipText;
45
46 wxFont m_fontSystem;
47 };
48
49 static wxSystemObjects gs_objects;
50
51 // ----------------------------------------------------------------------------
52 // wxSystemSettings implementation
53 // ----------------------------------------------------------------------------
54
55 // kind of widget to use in GetColourFromGTKWidget
56 enum wxGtkWidgetType
57 {
58 wxGTK_BUTTON,
59 wxGTK_LIST,
60 wxGTK_MENUITEM
61 };
62
63 // the colour we need
64 enum wxGtkColourType
65 {
66 wxGTK_FG,
67 wxGTK_BG,
68 wxGTK_BASE
69 };
70
71 // wxSystemSettings::GetColour() helper: get the colours from a GTK+
72 // widget style, return true if we did get them, false to use defaults
73 static bool GetColourFromGTKWidget(int& red, int& green, int& blue,
74 wxGtkWidgetType type = wxGTK_BUTTON,
75 GtkStateType state = GTK_STATE_NORMAL,
76 wxGtkColourType colour = wxGTK_BG)
77 {
78 GtkWidget *widget;
79 switch ( type )
80 {
81 default:
82 wxFAIL_MSG( _T("unexpected GTK widget type") );
83 // fall through
84
85 case wxGTK_BUTTON:
86 widget = gtk_button_new();
87 break;
88
89 case wxGTK_LIST:
90 widget = gtk_list_new();
91 break;
92
93 case wxGTK_MENUITEM:
94 widget = gtk_menu_item_new();
95 }
96
97 GtkStyle *def = gtk_rc_get_style( widget );
98 if ( !def )
99 def = gtk_widget_get_default_style();
100
101 bool ok;
102 if ( def )
103 {
104 GdkColor *col;
105 switch ( colour )
106 {
107 default:
108 wxFAIL_MSG( _T("unexpected GTK colour type") );
109 // fall through
110
111 case wxGTK_FG:
112 col = def->fg;
113 break;
114
115 case wxGTK_BG:
116 col = def->bg;
117 break;
118
119 case wxGTK_BASE:
120 col = def->base;
121 break;
122 }
123
124 red = col[state].red;
125 green = col[state].green;
126 blue = col[state].blue;
127
128 ok = TRUE;
129 }
130 else
131 {
132 ok = FALSE;
133 }
134
135 gtk_widget_destroy( widget );
136
137 return ok;
138 }
139
140 static void GetTooltipColors()
141 {
142 GtkTooltips* tooltips = gtk_tooltips_new();
143 gtk_tooltips_force_window(tooltips);
144 gtk_widget_ensure_style(tooltips->tip_window);
145 GdkColor c = tooltips->tip_window->style->bg[GTK_STATE_NORMAL];
146 gs_objects.m_colTooltip = wxColor(c.red >> SHIFT, c.green >> SHIFT, c.blue >> SHIFT);
147 c = tooltips->tip_window->style->fg[GTK_STATE_NORMAL];
148 gs_objects.m_colTooltipText = wxColor(c.red >> SHIFT, c.green >> SHIFT, c.blue >> SHIFT);
149 gtk_object_sink(wx_reinterpret_cast(GtkObject*, tooltips));
150 }
151
152 wxColour wxSystemSettingsNative::GetColour( wxSystemColour index )
153 {
154 switch (index)
155 {
156 case wxSYS_COLOUR_SCROLLBAR:
157 case wxSYS_COLOUR_BACKGROUND:
158 case wxSYS_COLOUR_INACTIVECAPTION:
159 case wxSYS_COLOUR_MENU:
160 case wxSYS_COLOUR_WINDOWFRAME:
161 case wxSYS_COLOUR_ACTIVEBORDER:
162 case wxSYS_COLOUR_INACTIVEBORDER:
163 case wxSYS_COLOUR_BTNFACE:
164 case wxSYS_COLOUR_MENUBAR:
165 case wxSYS_COLOUR_3DLIGHT:
166 if (!gs_objects.m_colBtnFace.Ok())
167 {
168 int red, green, blue;
169 if ( !GetColourFromGTKWidget(red, green, blue) )
170 {
171 red =
172 green = 0;
173 blue = 0x9c40;
174 }
175
176 gs_objects.m_colBtnFace = wxColour( red >> SHIFT,
177 green >> SHIFT,
178 blue >> SHIFT );
179 }
180 return gs_objects.m_colBtnFace;
181
182 case wxSYS_COLOUR_WINDOW:
183 return *wxWHITE;
184
185 case wxSYS_COLOUR_3DDKSHADOW:
186 return *wxBLACK;
187
188 case wxSYS_COLOUR_GRAYTEXT:
189 case wxSYS_COLOUR_BTNSHADOW:
190 //case wxSYS_COLOUR_3DSHADOW:
191 if (!gs_objects.m_colBtnShadow.Ok())
192 {
193 wxColour faceColour(GetColour(wxSYS_COLOUR_3DFACE));
194 gs_objects.m_colBtnShadow =
195 wxColour((unsigned char) (faceColour.Red() * 0.666),
196 (unsigned char) (faceColour.Green() * 0.666),
197 (unsigned char) (faceColour.Blue() * 0.666));
198 }
199
200 return gs_objects.m_colBtnShadow;
201
202 case wxSYS_COLOUR_3DHIGHLIGHT:
203 //case wxSYS_COLOUR_BTNHIGHLIGHT:
204 return * wxWHITE;
205
206 case wxSYS_COLOUR_HIGHLIGHT:
207 if (!gs_objects.m_colHighlight.Ok())
208 {
209 int red, green, blue;
210 if ( !GetColourFromGTKWidget(red, green, blue,
211 wxGTK_BUTTON,
212 GTK_STATE_SELECTED) )
213 {
214 red =
215 green = 0;
216 blue = 0x9c40;
217 }
218
219 gs_objects.m_colHighlight = wxColour( red >> SHIFT,
220 green >> SHIFT,
221 blue >> SHIFT );
222 }
223 return gs_objects.m_colHighlight;
224
225 case wxSYS_COLOUR_LISTBOX:
226 if (!gs_objects.m_colListBox.Ok())
227 {
228 int red, green, blue;
229 if ( GetColourFromGTKWidget(red, green, blue,
230 wxGTK_LIST,
231 GTK_STATE_NORMAL,
232 wxGTK_BASE) )
233 {
234 gs_objects.m_colListBox = wxColour( red >> SHIFT,
235 green >> SHIFT,
236 blue >> SHIFT );
237 }
238 else
239 {
240 gs_objects.m_colListBox = wxColour(*wxWHITE);
241 }
242 }
243 return gs_objects.m_colListBox;
244
245 case wxSYS_COLOUR_MENUTEXT:
246 case wxSYS_COLOUR_WINDOWTEXT:
247 case wxSYS_COLOUR_CAPTIONTEXT:
248 case wxSYS_COLOUR_INACTIVECAPTIONTEXT:
249 case wxSYS_COLOUR_BTNTEXT:
250 if (!gs_objects.m_colBtnText.Ok())
251 {
252 int red, green, blue;
253 if ( !GetColourFromGTKWidget(red, green, blue,
254 wxGTK_BUTTON,
255 GTK_STATE_NORMAL,
256 wxGTK_FG) )
257 {
258 red =
259 green =
260 blue = 0;
261 }
262
263 gs_objects.m_colBtnText = wxColour( red >> SHIFT,
264 green >> SHIFT,
265 blue >> SHIFT );
266 }
267 return gs_objects.m_colBtnText;
268
269 case wxSYS_COLOUR_INFOBK:
270 if (!gs_objects.m_colTooltip.Ok()) {
271 GetTooltipColors();
272 }
273 return gs_objects.m_colTooltip;
274
275 case wxSYS_COLOUR_INFOTEXT:
276 if (!gs_objects.m_colTooltipText.Ok()) {
277 GetTooltipColors();
278 }
279 return gs_objects.m_colTooltipText;
280
281 case wxSYS_COLOUR_HIGHLIGHTTEXT:
282 if (!gs_objects.m_colHighlightText.Ok())
283 {
284 wxColour hclr = GetColour(wxSYS_COLOUR_HIGHLIGHT);
285 if (hclr.Red() > 200 && hclr.Green() > 200 && hclr.Blue() > 200)
286 gs_objects.m_colHighlightText = wxColour(*wxBLACK);
287 else
288 gs_objects.m_colHighlightText = wxColour(*wxWHITE);
289 }
290 return gs_objects.m_colHighlightText;
291
292 case wxSYS_COLOUR_APPWORKSPACE:
293 return *wxWHITE; // ?
294
295 case wxSYS_COLOUR_ACTIVECAPTION:
296 case wxSYS_COLOUR_MENUHILIGHT:
297 if (!gs_objects.m_colMenuItemHighlight.Ok())
298 {
299 int red, green, blue;
300 if ( !GetColourFromGTKWidget(red, green, blue,
301 wxGTK_MENUITEM,
302 GTK_STATE_SELECTED,
303 wxGTK_BG) )
304 {
305 red =
306 green =
307 blue = 0;
308 }
309
310 gs_objects.m_colMenuItemHighlight = wxColour( red >> SHIFT,
311 green >> SHIFT,
312 blue >> SHIFT );
313 }
314 return gs_objects.m_colMenuItemHighlight;
315
316 case wxSYS_COLOUR_HOTLIGHT:
317 case wxSYS_COLOUR_GRADIENTACTIVECAPTION:
318 case wxSYS_COLOUR_GRADIENTINACTIVECAPTION:
319 // TODO
320 return *wxBLACK;
321
322 case wxSYS_COLOUR_MAX:
323 default:
324 wxFAIL_MSG( _T("unknown system colour index") );
325 }
326
327 return *wxWHITE;
328 }
329
330 wxFont wxSystemSettingsNative::GetFont( wxSystemFont index )
331 {
332 switch (index)
333 {
334 case wxSYS_OEM_FIXED_FONT:
335 case wxSYS_ANSI_FIXED_FONT:
336 case wxSYS_SYSTEM_FIXED_FONT:
337 {
338 return *wxNORMAL_FONT;
339 }
340 case wxSYS_ANSI_VAR_FONT:
341 case wxSYS_SYSTEM_FONT:
342 case wxSYS_DEVICE_DEFAULT_FONT:
343 case wxSYS_DEFAULT_GUI_FONT:
344 {
345 if (!gs_objects.m_fontSystem.Ok())
346 {
347 GtkWidget *widget = gtk_button_new();
348 GtkStyle *def = gtk_rc_get_style( widget );
349 if ( !def || !def->font_desc )
350 def = gtk_widget_get_default_style();
351 if ( def && def->font_desc )
352 {
353 wxNativeFontInfo info;
354 info.description =
355 pango_font_description_copy(def->font_desc);
356 gs_objects.m_fontSystem = wxFont(info);
357 }
358 else
359 {
360 GtkSettings *settings = gtk_settings_get_default();
361 gchar *font_name = NULL;
362 g_object_get ( settings,
363 "gtk-font-name",
364 &font_name,
365 NULL);
366 if (!font_name)
367 gs_objects.m_fontSystem = wxFont( 12, wxSWISS, wxNORMAL, wxNORMAL );
368 else
369 gs_objects.m_fontSystem = wxFont(wxString::FromAscii(font_name));
370 g_free (font_name);
371 }
372 gtk_widget_destroy( widget );
373 }
374 return gs_objects.m_fontSystem;
375 }
376
377 default:
378 return wxNullFont;
379 }
380 }
381
382 int wxSystemSettingsNative::GetMetric( wxSystemMetric index, wxWindow* win )
383 {
384 bool success = false;
385
386 guchar *data = NULL;
387 GdkWindow *window = NULL;
388 if(win && GTK_WIDGET_REALIZED(win->GetHandle()))
389 window = win->GetHandle()->window;
390
391 switch (index)
392 {
393 case wxSYS_BORDER_X:
394 case wxSYS_BORDER_Y:
395 case wxSYS_EDGE_X:
396 case wxSYS_EDGE_Y:
397 case wxSYS_FRAMESIZE_X:
398 case wxSYS_FRAMESIZE_Y:
399 // If a window is specified/realized, and it is a toplevel window, we can query from wm.
400 // The returned border thickness is outside the client area in that case.
401 if (window)
402 {
403 wxTopLevelWindow *tlw = wxDynamicCast(win, wxTopLevelWindow);
404 if (!tlw)
405 return -1; // not a tlw, not sure how to approach
406 else
407 {
408 // Check if wm supports frame extents - we can't know
409 // the border widths if it does not.
410 #if GTK_CHECK_VERSION(2,2,0)
411 if (!gtk_check_version(2,2,0))
412 {
413 if (!gdk_x11_screen_supports_net_wm_hint(
414 gdk_drawable_get_screen(window),
415 gdk_atom_intern("_NET_FRAME_EXTENTS", false) ) )
416 return -1;
417 }
418 else
419 #endif
420 {
421 if (!gdk_net_wm_supports(gdk_atom_intern("_NET_FRAME_EXTENTS", false)))
422 return -1;
423 }
424
425 // Get the frame extents from the windowmanager.
426 // In most cases the top extent is the titlebar, so we use the bottom extent
427 // for the heights.
428
429 Atom type;
430 gint format;
431 gulong nitems;
432
433 #if GTK_CHECK_VERSION(2,2,0)
434 if (!gtk_check_version(2,2,0))
435 {
436 gulong bytes_after;
437 success = (XGetWindowProperty (GDK_DISPLAY_XDISPLAY(gdk_drawable_get_display(window)),
438 GDK_WINDOW_XWINDOW(window),
439 gdk_x11_get_xatom_by_name_for_display (
440 gdk_drawable_get_display(window),
441 "_NET_FRAME_EXTENTS" ),
442 0, // left, right, top, bottom, CARDINAL[4]/32
443 G_MAXLONG, // size of long
444 false, // do not delete property
445 XA_CARDINAL, // 32 bit
446 &type, &format, &nitems, &bytes_after, &data
447 ) == Success);
448 }
449 #endif
450 if (success)
451 {
452 int border_return = -1;
453
454 if ((type == XA_CARDINAL) && (format == 32) && (nitems >= 4) && (data))
455 {
456 long *borders;
457 borders = (long*)data;
458 switch(index)
459 {
460 case wxSYS_BORDER_X:
461 case wxSYS_EDGE_X:
462 case wxSYS_FRAMESIZE_X:
463 border_return = borders[1]; // width of right extent
464 break;
465 default:
466 border_return = borders[3]; // height of bottom extent
467 break;
468 }
469 }
470
471 if (data)
472 XFree(data);
473
474 return border_return;
475 }
476 }
477 }
478
479 return -1; // no window specified
480
481 case wxSYS_CURSOR_X:
482 case wxSYS_CURSOR_Y:
483 #ifdef __WXGTK24__
484 if (!gtk_check_version(2,4,0))
485 {
486 if (window)
487 return gdk_display_get_default_cursor_size(gdk_drawable_get_display(window));
488 else
489 return gdk_display_get_default_cursor_size(gdk_display_get_default());
490 }
491 else
492 #endif
493 return 16;
494
495 case wxSYS_DCLICK_X:
496 case wxSYS_DCLICK_Y:
497 gint dclick_distance;
498 #if GTK_CHECK_VERSION(2,2,0)
499 if (window && !gtk_check_version(2,2,0))
500 g_object_get(gtk_settings_get_for_screen(gdk_drawable_get_screen(window)),
501 "gtk-double-click-distance", &dclick_distance, NULL);
502 else
503 #endif
504 g_object_get(gtk_settings_get_default(),
505 "gtk-double-click-distance", &dclick_distance, NULL);
506
507 return dclick_distance * 2;
508
509 case wxSYS_DRAG_X:
510 case wxSYS_DRAG_Y:
511 gint drag_threshold;
512 #if GTK_CHECK_VERSION(2,2,0)
513 if (window && !gtk_check_version(2,2,0))
514 {
515 g_object_get(
516 gtk_settings_get_for_screen(gdk_drawable_get_screen(window)),
517 "gtk-dnd-drag-threshold",
518 &drag_threshold, NULL);
519 }
520 else
521 #endif
522 {
523 g_object_get(gtk_settings_get_default(),
524 "gtk-dnd-drag-threshold", &drag_threshold, NULL);
525 }
526
527 return drag_threshold * 2;
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 defined(__WXGTK20__) && 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 defined(__WXGTK20__) && 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 // Check if wm supports frame extents - we can't know the caption height if it does not.
558 #if GTK_CHECK_VERSION(2,2,0)
559 if (!gtk_check_version(2,2,0))
560 {
561 if (!gdk_x11_screen_supports_net_wm_hint(
562 gdk_drawable_get_screen(window),
563 gdk_atom_intern("_NET_FRAME_EXTENTS", false) ) )
564 return -1;
565 }
566 else
567 #endif
568 {
569 if (!gdk_net_wm_supports(gdk_atom_intern("_NET_FRAME_EXTENTS", false)))
570 return -1;
571 }
572
573 wxASSERT_MSG( wxDynamicCast(win, wxTopLevelWindow),
574 wxT("Asking for caption height of a non toplevel window") );
575
576 // Get the height of the top windowmanager border.
577 // This is the titlebar in most cases. The titlebar might be elsewhere, and
578 // we could check which is the thickest wm border to decide on which side the
579 // titlebar is, but this might lead to interesting behaviours in used code.
580 // Reconsider when we have a way to report to the user on which side it is.
581
582 Atom type;
583 gint format;
584 gulong nitems;
585
586 #if GTK_CHECK_VERSION(2,2,0)
587 if (!gtk_check_version(2,2,0))
588 {
589 gulong bytes_after;
590 success = (XGetWindowProperty (GDK_DISPLAY_XDISPLAY(gdk_drawable_get_display(window)),
591 GDK_WINDOW_XWINDOW(window),
592 gdk_x11_get_xatom_by_name_for_display (
593 gdk_drawable_get_display(window),
594 "_NET_FRAME_EXTENTS" ),
595 0, // left, right, top, bottom, CARDINAL[4]/32
596 G_MAXLONG, // size of long
597 false, // do not delete property
598 XA_CARDINAL, // 32 bit
599 &type, &format, &nitems, &bytes_after, &data
600 ) == Success);
601 }
602 #endif
603 if (success)
604 {
605 int caption_height = -1;
606
607 if ((type == XA_CARDINAL) && (format == 32) && (nitems >= 3) && (data))
608 {
609 long *borders;
610 borders = (long*)data;
611 caption_height = borders[2]; // top frame extent
612 }
613
614 if (data)
615 XFree(data);
616
617 return caption_height;
618 }
619
620 // Try a default approach without a window pointer, if possible
621 // ...
622
623 return -1;
624
625 case wxSYS_PENWINDOWS_PRESENT:
626 // No MS Windows for Pen computing extension available in X11 based gtk+.
627 return 0;
628
629 default:
630 return -1; // metric is unknown
631 }
632 }
633
634 bool wxSystemSettingsNative::HasFeature(wxSystemFeature index)
635 {
636 switch (index)
637 {
638 case wxSYS_CAN_ICONIZE_FRAME:
639 return FALSE;
640
641 case wxSYS_CAN_DRAW_FRAME_DECORATIONS:
642 return TRUE;
643
644 default:
645 return FALSE;
646 }
647 }