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 GtkWidget
* widget
= gtk_window_new(GTK_WINDOW_POPUP
);
137 #if GTK_CHECK_VERSION(2, 10, 0)
138 if (!gtk_check_version(2, 10, 0))
139 gtk_window_set_type_hint((GtkWindow
*)widget
, GDK_WINDOW_TYPE_HINT_TOOLTIP
);
141 const char* name
= "gtk-tooltip";
142 if (gtk_check_version(2, 11, 0))
143 name
= "gtk-tooltips";
144 gtk_widget_set_name(widget
, name
);
145 gtk_widget_ensure_style(widget
);
147 GdkColor c
= widget
->style
->bg
[GTK_STATE_NORMAL
];
148 gs_objects
.m_colTooltip
= wxColor(c
);
149 c
= widget
->style
->fg
[GTK_STATE_NORMAL
];
150 gs_objects
.m_colTooltipText
= wxColor(c
);
152 gtk_widget_destroy(widget
);
155 wxColour
wxSystemSettingsNative::GetColour( wxSystemColour index
)
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())
175 gdkColor
.blue
= 0x9c40;
176 GetColourFromGTKWidget(gdkColor
);
177 gs_objects
.m_colBtnFace
= wxColor(gdkColor
);
179 color
= gs_objects
.m_colBtnFace
;
182 case wxSYS_COLOUR_WINDOW
:
186 case wxSYS_COLOUR_3DDKSHADOW
:
190 case wxSYS_COLOUR_GRAYTEXT
:
191 case wxSYS_COLOUR_BTNSHADOW
:
192 //case wxSYS_COLOUR_3DSHADOW:
193 if (!gs_objects
.m_colBtnShadow
.Ok())
195 wxColour
faceColour(GetColour(wxSYS_COLOUR_3DFACE
));
196 gs_objects
.m_colBtnShadow
=
197 wxColour((unsigned char) (faceColour
.Red() * 2 / 3),
198 (unsigned char) (faceColour
.Green() * 2 / 3),
199 (unsigned char) (faceColour
.Blue() * 2 / 3));
201 color
= gs_objects
.m_colBtnShadow
;
204 case wxSYS_COLOUR_3DHIGHLIGHT
:
205 //case wxSYS_COLOUR_BTNHIGHLIGHT:
209 case wxSYS_COLOUR_HIGHLIGHT
:
210 if (!gs_objects
.m_colHighlight
.Ok())
214 gdkColor
.blue
= 0x9c40;
215 GetColourFromGTKWidget(
216 gdkColor
, wxGTK_BUTTON
, GTK_STATE_SELECTED
);
217 gs_objects
.m_colHighlight
= wxColour(gdkColor
);
219 color
= gs_objects
.m_colHighlight
;
222 case wxSYS_COLOUR_LISTBOX
:
223 if (!gs_objects
.m_colListBox
.Ok())
225 if ( GetColourFromGTKWidget(gdkColor
,
230 gs_objects
.m_colListBox
= wxColour(gdkColor
);
234 gs_objects
.m_colListBox
= *wxWHITE
;
237 color
= gs_objects
.m_colListBox
;
240 case wxSYS_COLOUR_MENUTEXT
:
241 case wxSYS_COLOUR_WINDOWTEXT
:
242 case wxSYS_COLOUR_CAPTIONTEXT
:
243 case wxSYS_COLOUR_INACTIVECAPTIONTEXT
:
244 case wxSYS_COLOUR_BTNTEXT
:
245 if (!gs_objects
.m_colBtnText
.Ok())
250 GetColourFromGTKWidget(
251 gdkColor
, wxGTK_BUTTON
, GTK_STATE_NORMAL
, wxGTK_FG
);
252 gs_objects
.m_colBtnText
= wxColour(gdkColor
);
254 color
= gs_objects
.m_colBtnText
;
257 case wxSYS_COLOUR_INFOBK
:
258 if (!gs_objects
.m_colTooltip
.Ok()) {
261 color
= gs_objects
.m_colTooltip
;
264 case wxSYS_COLOUR_INFOTEXT
:
265 if (!gs_objects
.m_colTooltipText
.Ok()) {
268 color
= gs_objects
.m_colTooltipText
;
271 case wxSYS_COLOUR_HIGHLIGHTTEXT
:
272 if (!gs_objects
.m_colHighlightText
.Ok())
274 wxColour hclr
= GetColour(wxSYS_COLOUR_HIGHLIGHT
);
275 if (hclr
.Red() > 200 && hclr
.Green() > 200 && hclr
.Blue() > 200)
276 gs_objects
.m_colHighlightText
= *wxBLACK
;
278 gs_objects
.m_colHighlightText
= *wxWHITE
;
280 color
= gs_objects
.m_colHighlightText
;
283 case wxSYS_COLOUR_APPWORKSPACE
:
284 color
= *wxWHITE
; // ?
287 case wxSYS_COLOUR_ACTIVECAPTION
:
288 case wxSYS_COLOUR_MENUHILIGHT
:
289 if (!gs_objects
.m_colMenuItemHighlight
.Ok())
294 GetColourFromGTKWidget(
295 gdkColor
, wxGTK_MENUITEM
, GTK_STATE_SELECTED
, wxGTK_BG
);
296 gs_objects
.m_colMenuItemHighlight
= wxColour(gdkColor
);
298 color
= gs_objects
.m_colMenuItemHighlight
;
301 case wxSYS_COLOUR_HOTLIGHT
:
302 case wxSYS_COLOUR_GRADIENTACTIVECAPTION
:
303 case wxSYS_COLOUR_GRADIENTINACTIVECAPTION
:
308 case wxSYS_COLOUR_MAX
:
310 wxFAIL_MSG( _T("unknown system colour index") );
318 wxFont
wxSystemSettingsNative::GetFont( wxSystemFont index
)
323 case wxSYS_OEM_FIXED_FONT
:
324 case wxSYS_ANSI_FIXED_FONT
:
325 case wxSYS_SYSTEM_FIXED_FONT
:
326 font
= *wxNORMAL_FONT
;
329 case wxSYS_ANSI_VAR_FONT
:
330 case wxSYS_SYSTEM_FONT
:
331 case wxSYS_DEVICE_DEFAULT_FONT
:
332 case wxSYS_DEFAULT_GUI_FONT
:
333 if (!gs_objects
.m_fontSystem
.Ok())
335 GtkWidget
*widget
= gtk_button_new();
336 GtkStyle
*def
= gtk_rc_get_style( widget
);
337 if ( !def
|| !def
->font_desc
)
338 def
= gtk_widget_get_default_style();
339 if ( def
&& def
->font_desc
)
341 wxNativeFontInfo info
;
343 pango_font_description_copy(def
->font_desc
);
344 gs_objects
.m_fontSystem
= wxFont(info
);
348 GtkSettings
*settings
= gtk_settings_get_default();
349 gchar
*font_name
= NULL
;
350 g_object_get ( settings
,
355 gs_objects
.m_fontSystem
= wxFont( 12, wxSWISS
, wxNORMAL
, wxNORMAL
);
357 gs_objects
.m_fontSystem
= wxFont(wxString::FromAscii(font_name
));
360 gtk_object_sink((GtkObject
*)widget
);
362 font
= gs_objects
.m_fontSystem
;
371 static bool wxXGetWindowProperty(GdkWindow
* window
, Atom
& type
, int& format
, gulong
& nitems
, guchar
*& data
)
373 bool success
= false;
374 #if GTK_CHECK_VERSION(2, 2, 0)
375 if (gtk_check_version(2, 2, 0) == NULL
)
378 success
= XGetWindowProperty(
379 GDK_DISPLAY_XDISPLAY(gdk_drawable_get_display(window
)),
380 GDK_WINDOW_XWINDOW(window
),
381 gdk_x11_get_xatom_by_name_for_display(
382 gdk_drawable_get_display(window
),
383 "_NET_FRAME_EXTENTS"),
384 0, // left, right, top, bottom, CARDINAL[4]/32
385 G_MAXLONG
, // size of long
386 false, // do not delete property
387 XA_CARDINAL
, // 32 bit
388 &type
, &format
, &nitems
, &bytes_after
, &data
395 int wxSystemSettingsNative::GetMetric( wxSystemMetric index
, wxWindow
* win
)
401 GdkWindow
*window
= NULL
;
402 if(win
&& GTK_WIDGET_REALIZED(win
->GetHandle()))
403 window
= win
->GetHandle()->window
;
411 case wxSYS_FRAMESIZE_X
:
412 case wxSYS_FRAMESIZE_Y
:
413 // If a window is specified/realized, and it is a toplevel window, we can query from wm.
414 // The returned border thickness is outside the client area in that case.
417 wxTopLevelWindow
*tlw
= wxDynamicCast(win
, wxTopLevelWindow
);
419 return -1; // not a tlw, not sure how to approach
422 // Check if wm supports frame extents - we can't know
423 // the border widths if it does not.
424 #if GTK_CHECK_VERSION(2,2,0)
425 if (!gtk_check_version(2,2,0))
427 if (!gdk_x11_screen_supports_net_wm_hint(
428 gdk_drawable_get_screen(window
),
429 gdk_atom_intern("_NET_FRAME_EXTENTS", false) ) )
435 if (!gdk_net_wm_supports(gdk_atom_intern("_NET_FRAME_EXTENTS", false)))
439 // Get the frame extents from the windowmanager.
440 // In most cases the top extent is the titlebar, so we use the bottom extent
442 if (wxXGetWindowProperty(window
, type
, format
, nitems
, data
))
444 int border_return
= -1;
446 if ((type
== XA_CARDINAL
) && (format
== 32) && (nitems
>= 4) && (data
))
452 case wxSYS_FRAMESIZE_X
:
453 border_return
= int(data
[1]); // width of right extent
456 border_return
= int(data
[3]); // height of bottom extent
464 return border_return
;
469 return -1; // no window specified
474 if (!gtk_check_version(2,4,0))
477 return gdk_display_get_default_cursor_size(gdk_drawable_get_display(window
));
479 return gdk_display_get_default_cursor_size(gdk_display_get_default());
487 gint dclick_distance
;
488 #if GTK_CHECK_VERSION(2,2,0)
489 if (window
&& !gtk_check_version(2,2,0))
490 g_object_get(gtk_settings_get_for_screen(gdk_drawable_get_screen(window
)),
491 "gtk-double-click-distance", &dclick_distance
, NULL
);
494 g_object_get(gtk_settings_get_default(),
495 "gtk-double-click-distance", &dclick_distance
, NULL
);
497 return dclick_distance
* 2;
502 #if GTK_CHECK_VERSION(2,2,0)
503 if (window
&& !gtk_check_version(2,2,0))
506 gtk_settings_get_for_screen(gdk_drawable_get_screen(window
)),
507 "gtk-dnd-drag-threshold",
508 &drag_threshold
, NULL
);
513 g_object_get(gtk_settings_get_default(),
514 "gtk-dnd-drag-threshold", &drag_threshold
, NULL
);
517 // The correct thing here would be to double the value
518 // since that is what the API wants. But the values
519 // are much bigger under GNOME than under Windows and
520 // just seem to much in many cases to be useful.
521 // drag_threshold *= 2;
523 return drag_threshold
;
525 // MBN: ditto for icons
526 case wxSYS_ICON_X
: return 32;
527 case wxSYS_ICON_Y
: return 32;
530 #if GTK_CHECK_VERSION(2,2,0)
531 if (window
&& !gtk_check_version(2,2,0))
532 return gdk_screen_get_width(gdk_drawable_get_screen(window
));
535 return gdk_screen_width();
538 #if GTK_CHECK_VERSION(2,2,0)
539 if (window
&& !gtk_check_version(2,2,0))
540 return gdk_screen_get_height(gdk_drawable_get_screen(window
));
543 return gdk_screen_height();
545 case wxSYS_HSCROLL_Y
: return 15;
546 case wxSYS_VSCROLL_X
: return 15;
548 case wxSYS_CAPTION_Y
:
550 // No realized window specified, and no implementation for that case yet.
553 // Check if wm supports frame extents - we can't know the caption height if it does not.
554 #if GTK_CHECK_VERSION(2,2,0)
555 if (!gtk_check_version(2,2,0))
557 if (!gdk_x11_screen_supports_net_wm_hint(
558 gdk_drawable_get_screen(window
),
559 gdk_atom_intern("_NET_FRAME_EXTENTS", false) ) )
565 if (!gdk_net_wm_supports(gdk_atom_intern("_NET_FRAME_EXTENTS", false)))
569 wxASSERT_MSG( wxDynamicCast(win
, wxTopLevelWindow
),
570 wxT("Asking for caption height of a non toplevel window") );
572 // Get the height of the top windowmanager border.
573 // This is the titlebar in most cases. The titlebar might be elsewhere, and
574 // we could check which is the thickest wm border to decide on which side the
575 // titlebar is, but this might lead to interesting behaviours in used code.
576 // Reconsider when we have a way to report to the user on which side it is.
577 if (wxXGetWindowProperty(window
, type
, format
, nitems
, data
))
579 int caption_height
= -1;
581 if ((type
== XA_CARDINAL
) && (format
== 32) && (nitems
>= 3) && (data
))
583 caption_height
= int(data
[2]); // top frame extent
589 return caption_height
;
592 // Try a default approach without a window pointer, if possible
597 case wxSYS_PENWINDOWS_PRESENT
:
598 // No MS Windows for Pen computing extension available in X11 based gtk+.
602 return -1; // metric is unknown
606 bool wxSystemSettingsNative::HasFeature(wxSystemFeature index
)
610 case wxSYS_CAN_ICONIZE_FRAME
:
613 case wxSYS_CAN_DRAW_FRAME_DECORATIONS
: