1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/settings.cpp
4 // Author: Robert Roebling
5 // Modified by: Mart Raudsepp (GetMetric)
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
14 #include "wx/settings.h"
17 #include "wx/cmndata.h"
18 #include "wx/toplevel.h"
21 #include "wx/fontutil.h"
23 #include <gtk/gtkversion.h>
24 #if GTK_CHECK_VERSION(2, 9, 0)
26 #undef GTK_DISABLE_DEPRECATED
31 #include <X11/Xatom.h>
33 // ----------------------------------------------------------------------------
35 // ----------------------------------------------------------------------------
37 struct wxSystemObjects
39 wxColour m_colBtnFace
,
46 m_colMenuItemHighlight
,
53 static wxSystemObjects gs_objects
;
55 // ----------------------------------------------------------------------------
56 // wxSystemSettings implementation
57 // ----------------------------------------------------------------------------
59 // kind of widget to use in GetColourFromGTKWidget
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
)
86 wxFAIL_MSG( _T("unexpected GTK widget type") );
90 widget
= gtk_button_new();
94 widget
= gtk_tree_view_new_with_model(
95 (GtkTreeModel
*)gtk_list_store_new(1, G_TYPE_INT
));
99 widget
= gtk_menu_item_new();
102 GtkStyle
*def
= gtk_rc_get_style( widget
);
104 def
= gtk_widget_get_default_style();
106 const bool ok
= def
!= NULL
;
112 wxFAIL_MSG( _T("unexpected GTK colour type") );
116 gdkColor
= def
->fg
[state
];
120 gdkColor
= def
->bg
[state
];
124 gdkColor
= def
->base
[state
];
129 gtk_object_sink((GtkObject
*)widget
);
134 static void GetTooltipColors()
136 GtkTooltips
* tooltips
= gtk_tooltips_new();
137 gtk_tooltips_force_window(tooltips
);
139 // FIXME: In 2.11.6 tip_window is private and always NULL so
140 // we need to do something different if there is no window.
141 // See https://sourceforge.net/tracker/index.php?func=detail&aid=1767485&group_id=9863&atid=109863
142 if (tooltips
->tip_window
)
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
);
147 c
= tooltips
->tip_window
->style
->fg
[GTK_STATE_NORMAL
];
148 gs_objects
.m_colTooltipText
= wxColor(c
);
152 gs_objects
.m_colTooltipText
= wxColour(255, 255, 128); // FIXME
155 gtk_object_sink((GtkObject
*)tooltips
);
158 wxColour
wxSystemSettingsNative::GetColour( wxSystemColour index
)
164 case wxSYS_COLOUR_SCROLLBAR
:
165 case wxSYS_COLOUR_BACKGROUND
:
166 case wxSYS_COLOUR_INACTIVECAPTION
:
167 case wxSYS_COLOUR_MENU
:
168 case wxSYS_COLOUR_WINDOWFRAME
:
169 case wxSYS_COLOUR_ACTIVEBORDER
:
170 case wxSYS_COLOUR_INACTIVEBORDER
:
171 case wxSYS_COLOUR_BTNFACE
:
172 case wxSYS_COLOUR_MENUBAR
:
173 case wxSYS_COLOUR_3DLIGHT
:
174 if (!gs_objects
.m_colBtnFace
.Ok())
178 gdkColor
.blue
= 0x9c40;
179 GetColourFromGTKWidget(gdkColor
);
180 gs_objects
.m_colBtnFace
= wxColor(gdkColor
);
182 color
= gs_objects
.m_colBtnFace
;
185 case wxSYS_COLOUR_WINDOW
:
189 case wxSYS_COLOUR_3DDKSHADOW
:
193 case wxSYS_COLOUR_GRAYTEXT
:
194 case wxSYS_COLOUR_BTNSHADOW
:
195 //case wxSYS_COLOUR_3DSHADOW:
196 if (!gs_objects
.m_colBtnShadow
.Ok())
198 wxColour
faceColour(GetColour(wxSYS_COLOUR_3DFACE
));
199 gs_objects
.m_colBtnShadow
=
200 wxColour((unsigned char) (faceColour
.Red() * 2 / 3),
201 (unsigned char) (faceColour
.Green() * 2 / 3),
202 (unsigned char) (faceColour
.Blue() * 2 / 3));
204 color
= gs_objects
.m_colBtnShadow
;
207 case wxSYS_COLOUR_3DHIGHLIGHT
:
208 //case wxSYS_COLOUR_BTNHIGHLIGHT:
212 case wxSYS_COLOUR_HIGHLIGHT
:
213 if (!gs_objects
.m_colHighlight
.Ok())
217 gdkColor
.blue
= 0x9c40;
218 GetColourFromGTKWidget(
219 gdkColor
, wxGTK_BUTTON
, GTK_STATE_SELECTED
);
220 gs_objects
.m_colHighlight
= wxColour(gdkColor
);
222 color
= gs_objects
.m_colHighlight
;
225 case wxSYS_COLOUR_LISTBOX
:
226 if (!gs_objects
.m_colListBox
.Ok())
228 if ( GetColourFromGTKWidget(gdkColor
,
233 gs_objects
.m_colListBox
= wxColour(gdkColor
);
237 gs_objects
.m_colListBox
= *wxWHITE
;
240 color
= gs_objects
.m_colListBox
;
243 case wxSYS_COLOUR_MENUTEXT
:
244 case wxSYS_COLOUR_WINDOWTEXT
:
245 case wxSYS_COLOUR_CAPTIONTEXT
:
246 case wxSYS_COLOUR_INACTIVECAPTIONTEXT
:
247 case wxSYS_COLOUR_BTNTEXT
:
248 if (!gs_objects
.m_colBtnText
.Ok())
253 GetColourFromGTKWidget(
254 gdkColor
, wxGTK_BUTTON
, GTK_STATE_NORMAL
, wxGTK_FG
);
255 gs_objects
.m_colBtnText
= wxColour(gdkColor
);
257 color
= gs_objects
.m_colBtnText
;
260 case wxSYS_COLOUR_INFOBK
:
261 if (!gs_objects
.m_colTooltip
.Ok()) {
264 color
= gs_objects
.m_colTooltip
;
267 case wxSYS_COLOUR_INFOTEXT
:
268 if (!gs_objects
.m_colTooltipText
.Ok()) {
271 color
= gs_objects
.m_colTooltipText
;
274 case wxSYS_COLOUR_HIGHLIGHTTEXT
:
275 if (!gs_objects
.m_colHighlightText
.Ok())
277 wxColour hclr
= GetColour(wxSYS_COLOUR_HIGHLIGHT
);
278 if (hclr
.Red() > 200 && hclr
.Green() > 200 && hclr
.Blue() > 200)
279 gs_objects
.m_colHighlightText
= *wxBLACK
;
281 gs_objects
.m_colHighlightText
= *wxWHITE
;
283 color
= gs_objects
.m_colHighlightText
;
286 case wxSYS_COLOUR_APPWORKSPACE
:
287 color
= *wxWHITE
; // ?
290 case wxSYS_COLOUR_ACTIVECAPTION
:
291 case wxSYS_COLOUR_MENUHILIGHT
:
292 if (!gs_objects
.m_colMenuItemHighlight
.Ok())
297 GetColourFromGTKWidget(
298 gdkColor
, wxGTK_MENUITEM
, GTK_STATE_SELECTED
, wxGTK_BG
);
299 gs_objects
.m_colMenuItemHighlight
= wxColour(gdkColor
);
301 color
= gs_objects
.m_colMenuItemHighlight
;
304 case wxSYS_COLOUR_HOTLIGHT
:
305 case wxSYS_COLOUR_GRADIENTACTIVECAPTION
:
306 case wxSYS_COLOUR_GRADIENTINACTIVECAPTION
:
311 case wxSYS_COLOUR_MAX
:
313 wxFAIL_MSG( _T("unknown system colour index") );
321 wxFont
wxSystemSettingsNative::GetFont( wxSystemFont index
)
326 case wxSYS_OEM_FIXED_FONT
:
327 case wxSYS_ANSI_FIXED_FONT
:
328 case wxSYS_SYSTEM_FIXED_FONT
:
329 font
= *wxNORMAL_FONT
;
332 case wxSYS_ANSI_VAR_FONT
:
333 case wxSYS_SYSTEM_FONT
:
334 case wxSYS_DEVICE_DEFAULT_FONT
:
335 case wxSYS_DEFAULT_GUI_FONT
:
336 if (!gs_objects
.m_fontSystem
.Ok())
338 GtkWidget
*widget
= gtk_button_new();
339 GtkStyle
*def
= gtk_rc_get_style( widget
);
340 if ( !def
|| !def
->font_desc
)
341 def
= gtk_widget_get_default_style();
342 if ( def
&& def
->font_desc
)
344 wxNativeFontInfo info
;
346 pango_font_description_copy(def
->font_desc
);
347 gs_objects
.m_fontSystem
= wxFont(info
);
351 GtkSettings
*settings
= gtk_settings_get_default();
352 gchar
*font_name
= NULL
;
353 g_object_get ( settings
,
358 gs_objects
.m_fontSystem
= wxFont( 12, wxSWISS
, wxNORMAL
, wxNORMAL
);
360 gs_objects
.m_fontSystem
= wxFont(wxString::FromAscii(font_name
));
363 gtk_object_sink((GtkObject
*)widget
);
365 font
= gs_objects
.m_fontSystem
;
374 static bool wxXGetWindowProperty(GdkWindow
* window
, Atom
& type
, int& format
, gulong
& nitems
, guchar
*& data
)
376 bool success
= false;
377 #if GTK_CHECK_VERSION(2, 2, 0)
378 if (gtk_check_version(2, 2, 0) == NULL
)
381 success
= XGetWindowProperty(
382 GDK_DISPLAY_XDISPLAY(gdk_drawable_get_display(window
)),
383 GDK_WINDOW_XWINDOW(window
),
384 gdk_x11_get_xatom_by_name_for_display(
385 gdk_drawable_get_display(window
),
386 "_NET_FRAME_EXTENTS"),
387 0, // left, right, top, bottom, CARDINAL[4]/32
388 G_MAXLONG
, // size of long
389 false, // do not delete property
390 XA_CARDINAL
, // 32 bit
391 &type
, &format
, &nitems
, &bytes_after
, &data
398 int wxSystemSettingsNative::GetMetric( wxSystemMetric index
, wxWindow
* win
)
404 GdkWindow
*window
= NULL
;
405 if(win
&& GTK_WIDGET_REALIZED(win
->GetHandle()))
406 window
= win
->GetHandle()->window
;
414 case wxSYS_FRAMESIZE_X
:
415 case wxSYS_FRAMESIZE_Y
:
416 // If a window is specified/realized, and it is a toplevel window, we can query from wm.
417 // The returned border thickness is outside the client area in that case.
420 wxTopLevelWindow
*tlw
= wxDynamicCast(win
, wxTopLevelWindow
);
422 return -1; // not a tlw, not sure how to approach
425 // Check if wm supports frame extents - we can't know
426 // the border widths if it does not.
427 #if GTK_CHECK_VERSION(2,2,0)
428 if (!gtk_check_version(2,2,0))
430 if (!gdk_x11_screen_supports_net_wm_hint(
431 gdk_drawable_get_screen(window
),
432 gdk_atom_intern("_NET_FRAME_EXTENTS", false) ) )
438 if (!gdk_net_wm_supports(gdk_atom_intern("_NET_FRAME_EXTENTS", false)))
442 // Get the frame extents from the windowmanager.
443 // In most cases the top extent is the titlebar, so we use the bottom extent
445 if (wxXGetWindowProperty(window
, type
, format
, nitems
, data
))
447 int border_return
= -1;
449 if ((type
== XA_CARDINAL
) && (format
== 32) && (nitems
>= 4) && (data
))
455 case wxSYS_FRAMESIZE_X
:
456 border_return
= int(data
[1]); // width of right extent
459 border_return
= int(data
[3]); // height of bottom extent
467 return border_return
;
472 return -1; // no window specified
477 if (!gtk_check_version(2,4,0))
480 return gdk_display_get_default_cursor_size(gdk_drawable_get_display(window
));
482 return gdk_display_get_default_cursor_size(gdk_display_get_default());
490 gint dclick_distance
;
491 #if GTK_CHECK_VERSION(2,2,0)
492 if (window
&& !gtk_check_version(2,2,0))
493 g_object_get(gtk_settings_get_for_screen(gdk_drawable_get_screen(window
)),
494 "gtk-double-click-distance", &dclick_distance
, NULL
);
497 g_object_get(gtk_settings_get_default(),
498 "gtk-double-click-distance", &dclick_distance
, NULL
);
500 return dclick_distance
* 2;
505 #if GTK_CHECK_VERSION(2,2,0)
506 if (window
&& !gtk_check_version(2,2,0))
509 gtk_settings_get_for_screen(gdk_drawable_get_screen(window
)),
510 "gtk-dnd-drag-threshold",
511 &drag_threshold
, NULL
);
516 g_object_get(gtk_settings_get_default(),
517 "gtk-dnd-drag-threshold", &drag_threshold
, NULL
);
520 // The correct thing here would be to double the value
521 // since that is what the API wants. But the values
522 // are much bigger under GNOME than under Windows and
523 // just seem to much in many cases to be useful.
524 // drag_threshold *= 2;
526 return drag_threshold
;
528 // MBN: ditto for icons
529 case wxSYS_ICON_X
: return 32;
530 case wxSYS_ICON_Y
: return 32;
533 #if GTK_CHECK_VERSION(2,2,0)
534 if (window
&& !gtk_check_version(2,2,0))
535 return gdk_screen_get_width(gdk_drawable_get_screen(window
));
538 return gdk_screen_width();
541 #if GTK_CHECK_VERSION(2,2,0)
542 if (window
&& !gtk_check_version(2,2,0))
543 return gdk_screen_get_height(gdk_drawable_get_screen(window
));
546 return gdk_screen_height();
548 case wxSYS_HSCROLL_Y
: return 15;
549 case wxSYS_VSCROLL_X
: return 15;
551 case wxSYS_CAPTION_Y
:
553 // No realized window specified, and no implementation for that case yet.
556 // Check if wm supports frame extents - we can't know the caption height if it does not.
557 #if GTK_CHECK_VERSION(2,2,0)
558 if (!gtk_check_version(2,2,0))
560 if (!gdk_x11_screen_supports_net_wm_hint(
561 gdk_drawable_get_screen(window
),
562 gdk_atom_intern("_NET_FRAME_EXTENTS", false) ) )
568 if (!gdk_net_wm_supports(gdk_atom_intern("_NET_FRAME_EXTENTS", false)))
572 wxASSERT_MSG( wxDynamicCast(win
, wxTopLevelWindow
),
573 wxT("Asking for caption height of a non toplevel window") );
575 // Get the height of the top windowmanager border.
576 // This is the titlebar in most cases. The titlebar might be elsewhere, and
577 // we could check which is the thickest wm border to decide on which side the
578 // titlebar is, but this might lead to interesting behaviours in used code.
579 // Reconsider when we have a way to report to the user on which side it is.
580 if (wxXGetWindowProperty(window
, type
, format
, nitems
, data
))
582 int caption_height
= -1;
584 if ((type
== XA_CARDINAL
) && (format
== 32) && (nitems
>= 3) && (data
))
586 caption_height
= int(data
[2]); // top frame extent
592 return caption_height
;
595 // Try a default approach without a window pointer, if possible
600 case wxSYS_PENWINDOWS_PRESENT
:
601 // No MS Windows for Pen computing extension available in X11 based gtk+.
605 return -1; // metric is unknown
609 bool wxSystemSettingsNative::HasFeature(wxSystemFeature index
)
613 case wxSYS_CAN_ICONIZE_FRAME
:
616 case wxSYS_CAN_DRAW_FRAME_DECORATIONS
: