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_widget_destroy( widget
);
134 static void GetTooltipColors()
136 GtkTooltips
* tooltips
= gtk_tooltips_new();
137 gtk_tooltips_force_window(tooltips
);
138 gtk_widget_ensure_style(tooltips
->tip_window
);
139 GdkColor c
= tooltips
->tip_window
->style
->bg
[GTK_STATE_NORMAL
];
140 gs_objects
.m_colTooltip
= wxColor(c
);
141 c
= tooltips
->tip_window
->style
->fg
[GTK_STATE_NORMAL
];
142 gs_objects
.m_colTooltipText
= wxColor(c
);
143 #if GTK_CHECK_VERSION(2, 9, 0)
144 if (gtk_check_version(2, 9, 0) == NULL
)
145 g_object_ref_sink(tooltips
);
149 gtk_object_sink((GtkObject
*)tooltips
);
153 wxColour
wxSystemSettingsNative::GetColour( wxSystemColour index
)
159 case wxSYS_COLOUR_SCROLLBAR
:
160 case wxSYS_COLOUR_BACKGROUND
:
161 case wxSYS_COLOUR_INACTIVECAPTION
:
162 case wxSYS_COLOUR_MENU
:
163 case wxSYS_COLOUR_WINDOWFRAME
:
164 case wxSYS_COLOUR_ACTIVEBORDER
:
165 case wxSYS_COLOUR_INACTIVEBORDER
:
166 case wxSYS_COLOUR_BTNFACE
:
167 case wxSYS_COLOUR_MENUBAR
:
168 case wxSYS_COLOUR_3DLIGHT
:
169 if (!gs_objects
.m_colBtnFace
.Ok())
173 gdkColor
.blue
= 0x9c40;
174 GetColourFromGTKWidget(gdkColor
);
175 gs_objects
.m_colBtnFace
= wxColor(gdkColor
);
177 color
= gs_objects
.m_colBtnFace
;
180 case wxSYS_COLOUR_WINDOW
:
184 case wxSYS_COLOUR_3DDKSHADOW
:
188 case wxSYS_COLOUR_GRAYTEXT
:
189 case wxSYS_COLOUR_BTNSHADOW
:
190 //case wxSYS_COLOUR_3DSHADOW:
191 if (!gs_objects
.m_colBtnShadow
.Ok())
193 wxColour
faceColour(GetColour(wxSYS_COLOUR_3DFACE
));
194 gs_objects
.m_colBtnShadow
=
195 wxColour((unsigned char) (faceColour
.Red() * 2 / 3),
196 (unsigned char) (faceColour
.Green() * 2 / 3),
197 (unsigned char) (faceColour
.Blue() * 2 / 3));
199 color
= gs_objects
.m_colBtnShadow
;
202 case wxSYS_COLOUR_3DHIGHLIGHT
:
203 //case wxSYS_COLOUR_BTNHIGHLIGHT:
207 case wxSYS_COLOUR_HIGHLIGHT
:
208 if (!gs_objects
.m_colHighlight
.Ok())
212 gdkColor
.blue
= 0x9c40;
213 GetColourFromGTKWidget(
214 gdkColor
, wxGTK_BUTTON
, GTK_STATE_SELECTED
);
215 gs_objects
.m_colHighlight
= wxColour(gdkColor
);
217 color
= gs_objects
.m_colHighlight
;
220 case wxSYS_COLOUR_LISTBOX
:
221 if (!gs_objects
.m_colListBox
.Ok())
223 if ( GetColourFromGTKWidget(gdkColor
,
228 gs_objects
.m_colListBox
= wxColour(gdkColor
);
232 gs_objects
.m_colListBox
= *wxWHITE
;
235 color
= gs_objects
.m_colListBox
;
238 case wxSYS_COLOUR_MENUTEXT
:
239 case wxSYS_COLOUR_WINDOWTEXT
:
240 case wxSYS_COLOUR_CAPTIONTEXT
:
241 case wxSYS_COLOUR_INACTIVECAPTIONTEXT
:
242 case wxSYS_COLOUR_BTNTEXT
:
243 if (!gs_objects
.m_colBtnText
.Ok())
248 GetColourFromGTKWidget(
249 gdkColor
, wxGTK_BUTTON
, GTK_STATE_NORMAL
, wxGTK_FG
);
250 gs_objects
.m_colBtnText
= wxColour(gdkColor
);
252 color
= gs_objects
.m_colBtnText
;
255 case wxSYS_COLOUR_INFOBK
:
256 if (!gs_objects
.m_colTooltip
.Ok()) {
259 color
= gs_objects
.m_colTooltip
;
262 case wxSYS_COLOUR_INFOTEXT
:
263 if (!gs_objects
.m_colTooltipText
.Ok()) {
266 color
= gs_objects
.m_colTooltipText
;
269 case wxSYS_COLOUR_HIGHLIGHTTEXT
:
270 if (!gs_objects
.m_colHighlightText
.Ok())
272 wxColour hclr
= GetColour(wxSYS_COLOUR_HIGHLIGHT
);
273 if (hclr
.Red() > 200 && hclr
.Green() > 200 && hclr
.Blue() > 200)
274 gs_objects
.m_colHighlightText
= *wxBLACK
;
276 gs_objects
.m_colHighlightText
= *wxWHITE
;
278 color
= gs_objects
.m_colHighlightText
;
281 case wxSYS_COLOUR_APPWORKSPACE
:
282 color
= *wxWHITE
; // ?
285 case wxSYS_COLOUR_ACTIVECAPTION
:
286 case wxSYS_COLOUR_MENUHILIGHT
:
287 if (!gs_objects
.m_colMenuItemHighlight
.Ok())
292 GetColourFromGTKWidget(
293 gdkColor
, wxGTK_MENUITEM
, GTK_STATE_SELECTED
, wxGTK_BG
);
294 gs_objects
.m_colMenuItemHighlight
= wxColour(gdkColor
);
296 color
= gs_objects
.m_colMenuItemHighlight
;
299 case wxSYS_COLOUR_HOTLIGHT
:
300 case wxSYS_COLOUR_GRADIENTACTIVECAPTION
:
301 case wxSYS_COLOUR_GRADIENTINACTIVECAPTION
:
306 case wxSYS_COLOUR_MAX
:
308 wxFAIL_MSG( _T("unknown system colour index") );
316 wxFont
wxSystemSettingsNative::GetFont( wxSystemFont index
)
321 case wxSYS_OEM_FIXED_FONT
:
322 case wxSYS_ANSI_FIXED_FONT
:
323 case wxSYS_SYSTEM_FIXED_FONT
:
324 font
= *wxNORMAL_FONT
;
327 case wxSYS_ANSI_VAR_FONT
:
328 case wxSYS_SYSTEM_FONT
:
329 case wxSYS_DEVICE_DEFAULT_FONT
:
330 case wxSYS_DEFAULT_GUI_FONT
:
331 if (!gs_objects
.m_fontSystem
.Ok())
333 GtkWidget
*widget
= gtk_button_new();
334 GtkStyle
*def
= gtk_rc_get_style( widget
);
335 if ( !def
|| !def
->font_desc
)
336 def
= gtk_widget_get_default_style();
337 if ( def
&& def
->font_desc
)
339 wxNativeFontInfo info
;
341 pango_font_description_copy(def
->font_desc
);
342 gs_objects
.m_fontSystem
= wxFont(info
);
346 GtkSettings
*settings
= gtk_settings_get_default();
347 gchar
*font_name
= NULL
;
348 g_object_get ( settings
,
353 gs_objects
.m_fontSystem
= wxFont( 12, wxSWISS
, wxNORMAL
, wxNORMAL
);
355 gs_objects
.m_fontSystem
= wxFont(wxString::FromAscii(font_name
));
358 gtk_widget_destroy( widget
);
360 font
= gs_objects
.m_fontSystem
;
369 static bool wxXGetWindowProperty(GdkWindow
* window
, Atom
& type
, int& format
, gulong
& nitems
, guchar
*& data
)
371 bool success
= false;
372 #if GTK_CHECK_VERSION(2, 2, 0)
373 if (gtk_check_version(2, 2, 0) == NULL
)
376 success
= XGetWindowProperty(
377 GDK_DISPLAY_XDISPLAY(gdk_drawable_get_display(window
)),
378 GDK_WINDOW_XWINDOW(window
),
379 gdk_x11_get_xatom_by_name_for_display(
380 gdk_drawable_get_display(window
),
381 "_NET_FRAME_EXTENTS"),
382 0, // left, right, top, bottom, CARDINAL[4]/32
383 G_MAXLONG
, // size of long
384 false, // do not delete property
385 XA_CARDINAL
, // 32 bit
386 &type
, &format
, &nitems
, &bytes_after
, &data
393 int wxSystemSettingsNative::GetMetric( wxSystemMetric index
, wxWindow
* win
)
399 GdkWindow
*window
= NULL
;
400 if(win
&& GTK_WIDGET_REALIZED(win
->GetHandle()))
401 window
= win
->GetHandle()->window
;
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.
415 wxTopLevelWindow
*tlw
= wxDynamicCast(win
, wxTopLevelWindow
);
417 return -1; // not a tlw, not sure how to approach
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))
425 if (!gdk_x11_screen_supports_net_wm_hint(
426 gdk_drawable_get_screen(window
),
427 gdk_atom_intern("_NET_FRAME_EXTENTS", false) ) )
433 if (!gdk_net_wm_supports(gdk_atom_intern("_NET_FRAME_EXTENTS", false)))
437 // Get the frame extents from the windowmanager.
438 // In most cases the top extent is the titlebar, so we use the bottom extent
440 if (wxXGetWindowProperty(window
, type
, format
, nitems
, data
))
442 int border_return
= -1;
444 if ((type
== XA_CARDINAL
) && (format
== 32) && (nitems
>= 4) && (data
))
450 case wxSYS_FRAMESIZE_X
:
451 border_return
= int(data
[1]); // width of right extent
454 border_return
= int(data
[3]); // height of bottom extent
462 return border_return
;
467 return -1; // no window specified
472 if (!gtk_check_version(2,4,0))
475 return gdk_display_get_default_cursor_size(gdk_drawable_get_display(window
));
477 return gdk_display_get_default_cursor_size(gdk_display_get_default());
485 gint dclick_distance
;
486 #if GTK_CHECK_VERSION(2,2,0)
487 if (window
&& !gtk_check_version(2,2,0))
488 g_object_get(gtk_settings_get_for_screen(gdk_drawable_get_screen(window
)),
489 "gtk-double-click-distance", &dclick_distance
, NULL
);
492 g_object_get(gtk_settings_get_default(),
493 "gtk-double-click-distance", &dclick_distance
, NULL
);
495 return dclick_distance
* 2;
500 #if GTK_CHECK_VERSION(2,2,0)
501 if (window
&& !gtk_check_version(2,2,0))
504 gtk_settings_get_for_screen(gdk_drawable_get_screen(window
)),
505 "gtk-dnd-drag-threshold",
506 &drag_threshold
, NULL
);
511 g_object_get(gtk_settings_get_default(),
512 "gtk-dnd-drag-threshold", &drag_threshold
, NULL
);
515 // The correct thing here would be to double the value
516 // since that is what the API wants. But the values
517 // are much bigger under GNOME than under Windows and
518 // just seem to much in many cases to be useful.
519 // drag_threshold *= 2;
521 return drag_threshold
;
523 // MBN: ditto for icons
524 case wxSYS_ICON_X
: return 32;
525 case wxSYS_ICON_Y
: return 32;
528 #if GTK_CHECK_VERSION(2,2,0)
529 if (window
&& !gtk_check_version(2,2,0))
530 return gdk_screen_get_width(gdk_drawable_get_screen(window
));
533 return gdk_screen_width();
536 #if GTK_CHECK_VERSION(2,2,0)
537 if (window
&& !gtk_check_version(2,2,0))
538 return gdk_screen_get_height(gdk_drawable_get_screen(window
));
541 return gdk_screen_height();
543 case wxSYS_HSCROLL_Y
: return 15;
544 case wxSYS_VSCROLL_X
: return 15;
546 case wxSYS_CAPTION_Y
:
548 // No realized window specified, and no implementation for that case yet.
551 // Check if wm supports frame extents - we can't know the caption height if it does not.
552 #if GTK_CHECK_VERSION(2,2,0)
553 if (!gtk_check_version(2,2,0))
555 if (!gdk_x11_screen_supports_net_wm_hint(
556 gdk_drawable_get_screen(window
),
557 gdk_atom_intern("_NET_FRAME_EXTENTS", false) ) )
563 if (!gdk_net_wm_supports(gdk_atom_intern("_NET_FRAME_EXTENTS", false)))
567 wxASSERT_MSG( wxDynamicCast(win
, wxTopLevelWindow
),
568 wxT("Asking for caption height of a non toplevel window") );
570 // Get the height of the top windowmanager border.
571 // This is the titlebar in most cases. The titlebar might be elsewhere, and
572 // we could check which is the thickest wm border to decide on which side the
573 // titlebar is, but this might lead to interesting behaviours in used code.
574 // Reconsider when we have a way to report to the user on which side it is.
575 if (wxXGetWindowProperty(window
, type
, format
, nitems
, data
))
577 int caption_height
= -1;
579 if ((type
== XA_CARDINAL
) && (format
== 32) && (nitems
>= 3) && (data
))
581 caption_height
= int(data
[2]); // top frame extent
587 return caption_height
;
590 // Try a default approach without a window pointer, if possible
595 case wxSYS_PENWINDOWS_PRESENT
:
596 // No MS Windows for Pen computing extension available in X11 based gtk+.
600 return -1; // metric is unknown
604 bool wxSystemSettingsNative::HasFeature(wxSystemFeature index
)
608 case wxSYS_CAN_ICONIZE_FRAME
:
611 case wxSYS_CAN_DRAW_FRAME_DECORATIONS
: