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