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