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 const char* name
= "gtk-tooltip";
138 if (gtk_check_version(2, 11, 0))
139 name
= "gtk-tooltips";
140 gtk_widget_set_name(widget
, name
);
141 gtk_widget_ensure_style(widget
);
143 GdkColor c
= widget
->style
->bg
[GTK_STATE_NORMAL
];
144 gs_objects
.m_colTooltip
= wxColor(c
);
145 c
= widget
->style
->fg
[GTK_STATE_NORMAL
];
146 gs_objects
.m_colTooltipText
= wxColor(c
);
148 gtk_widget_destroy(widget
);
151 wxColour
wxSystemSettingsNative::GetColour( wxSystemColour index
)
157 case wxSYS_COLOUR_SCROLLBAR
:
158 case wxSYS_COLOUR_BACKGROUND
:
159 case wxSYS_COLOUR_INACTIVECAPTION
:
160 case wxSYS_COLOUR_MENU
:
161 case wxSYS_COLOUR_WINDOWFRAME
:
162 case wxSYS_COLOUR_ACTIVEBORDER
:
163 case wxSYS_COLOUR_INACTIVEBORDER
:
164 case wxSYS_COLOUR_BTNFACE
:
165 case wxSYS_COLOUR_MENUBAR
:
166 case wxSYS_COLOUR_3DLIGHT
:
167 if (!gs_objects
.m_colBtnFace
.Ok())
171 gdkColor
.blue
= 0x9c40;
172 GetColourFromGTKWidget(gdkColor
);
173 gs_objects
.m_colBtnFace
= wxColor(gdkColor
);
175 color
= gs_objects
.m_colBtnFace
;
178 case wxSYS_COLOUR_WINDOW
:
182 case wxSYS_COLOUR_3DDKSHADOW
:
186 case wxSYS_COLOUR_GRAYTEXT
:
187 case wxSYS_COLOUR_BTNSHADOW
:
188 //case wxSYS_COLOUR_3DSHADOW:
189 if (!gs_objects
.m_colBtnShadow
.Ok())
191 wxColour
faceColour(GetColour(wxSYS_COLOUR_3DFACE
));
192 gs_objects
.m_colBtnShadow
=
193 wxColour((unsigned char) (faceColour
.Red() * 2 / 3),
194 (unsigned char) (faceColour
.Green() * 2 / 3),
195 (unsigned char) (faceColour
.Blue() * 2 / 3));
197 color
= gs_objects
.m_colBtnShadow
;
200 case wxSYS_COLOUR_3DHIGHLIGHT
:
201 //case wxSYS_COLOUR_BTNHIGHLIGHT:
205 case wxSYS_COLOUR_HIGHLIGHT
:
206 if (!gs_objects
.m_colHighlight
.Ok())
210 gdkColor
.blue
= 0x9c40;
211 GetColourFromGTKWidget(
212 gdkColor
, wxGTK_BUTTON
, GTK_STATE_SELECTED
);
213 gs_objects
.m_colHighlight
= wxColour(gdkColor
);
215 color
= gs_objects
.m_colHighlight
;
218 case wxSYS_COLOUR_LISTBOX
:
219 if (!gs_objects
.m_colListBox
.Ok())
221 if ( GetColourFromGTKWidget(gdkColor
,
226 gs_objects
.m_colListBox
= wxColour(gdkColor
);
230 gs_objects
.m_colListBox
= *wxWHITE
;
233 color
= gs_objects
.m_colListBox
;
236 case wxSYS_COLOUR_MENUTEXT
:
237 case wxSYS_COLOUR_WINDOWTEXT
:
238 case wxSYS_COLOUR_CAPTIONTEXT
:
239 case wxSYS_COLOUR_INACTIVECAPTIONTEXT
:
240 case wxSYS_COLOUR_BTNTEXT
:
241 if (!gs_objects
.m_colBtnText
.Ok())
246 GetColourFromGTKWidget(
247 gdkColor
, wxGTK_BUTTON
, GTK_STATE_NORMAL
, wxGTK_FG
);
248 gs_objects
.m_colBtnText
= wxColour(gdkColor
);
250 color
= gs_objects
.m_colBtnText
;
253 case wxSYS_COLOUR_INFOBK
:
254 if (!gs_objects
.m_colTooltip
.Ok()) {
257 color
= gs_objects
.m_colTooltip
;
260 case wxSYS_COLOUR_INFOTEXT
:
261 if (!gs_objects
.m_colTooltipText
.Ok()) {
264 color
= gs_objects
.m_colTooltipText
;
267 case wxSYS_COLOUR_HIGHLIGHTTEXT
:
268 if (!gs_objects
.m_colHighlightText
.Ok())
270 wxColour hclr
= GetColour(wxSYS_COLOUR_HIGHLIGHT
);
271 if (hclr
.Red() > 200 && hclr
.Green() > 200 && hclr
.Blue() > 200)
272 gs_objects
.m_colHighlightText
= *wxBLACK
;
274 gs_objects
.m_colHighlightText
= *wxWHITE
;
276 color
= gs_objects
.m_colHighlightText
;
279 case wxSYS_COLOUR_APPWORKSPACE
:
280 color
= *wxWHITE
; // ?
283 case wxSYS_COLOUR_ACTIVECAPTION
:
284 case wxSYS_COLOUR_MENUHILIGHT
:
285 if (!gs_objects
.m_colMenuItemHighlight
.Ok())
290 GetColourFromGTKWidget(
291 gdkColor
, wxGTK_MENUITEM
, GTK_STATE_SELECTED
, wxGTK_BG
);
292 gs_objects
.m_colMenuItemHighlight
= wxColour(gdkColor
);
294 color
= gs_objects
.m_colMenuItemHighlight
;
297 case wxSYS_COLOUR_HOTLIGHT
:
298 case wxSYS_COLOUR_GRADIENTACTIVECAPTION
:
299 case wxSYS_COLOUR_GRADIENTINACTIVECAPTION
:
304 case wxSYS_COLOUR_MAX
:
306 wxFAIL_MSG( _T("unknown system colour index") );
314 wxFont
wxSystemSettingsNative::GetFont( wxSystemFont index
)
319 case wxSYS_OEM_FIXED_FONT
:
320 case wxSYS_ANSI_FIXED_FONT
:
321 case wxSYS_SYSTEM_FIXED_FONT
:
322 font
= *wxNORMAL_FONT
;
325 case wxSYS_ANSI_VAR_FONT
:
326 case wxSYS_SYSTEM_FONT
:
327 case wxSYS_DEVICE_DEFAULT_FONT
:
328 case wxSYS_DEFAULT_GUI_FONT
:
329 if (!gs_objects
.m_fontSystem
.Ok())
331 GtkWidget
*widget
= gtk_button_new();
332 GtkStyle
*def
= gtk_rc_get_style( widget
);
333 if ( !def
|| !def
->font_desc
)
334 def
= gtk_widget_get_default_style();
335 if ( def
&& def
->font_desc
)
337 wxNativeFontInfo info
;
339 pango_font_description_copy(def
->font_desc
);
340 gs_objects
.m_fontSystem
= wxFont(info
);
344 GtkSettings
*settings
= gtk_settings_get_default();
345 gchar
*font_name
= NULL
;
346 g_object_get ( settings
,
351 gs_objects
.m_fontSystem
= wxFont( 12, wxSWISS
, wxNORMAL
, wxNORMAL
);
353 gs_objects
.m_fontSystem
= wxFont(wxString::FromAscii(font_name
));
356 gtk_object_sink((GtkObject
*)widget
);
358 font
= gs_objects
.m_fontSystem
;
367 static bool wxXGetWindowProperty(GdkWindow
* window
, Atom
& type
, int& format
, gulong
& nitems
, guchar
*& data
)
369 bool success
= false;
370 #if GTK_CHECK_VERSION(2, 2, 0)
371 if (gtk_check_version(2, 2, 0) == NULL
)
374 success
= XGetWindowProperty(
375 GDK_DISPLAY_XDISPLAY(gdk_drawable_get_display(window
)),
376 GDK_WINDOW_XWINDOW(window
),
377 gdk_x11_get_xatom_by_name_for_display(
378 gdk_drawable_get_display(window
),
379 "_NET_FRAME_EXTENTS"),
380 0, // left, right, top, bottom, CARDINAL[4]/32
381 G_MAXLONG
, // size of long
382 false, // do not delete property
383 XA_CARDINAL
, // 32 bit
384 &type
, &format
, &nitems
, &bytes_after
, &data
391 int wxSystemSettingsNative::GetMetric( wxSystemMetric index
, wxWindow
* win
)
397 GdkWindow
*window
= NULL
;
398 if(win
&& GTK_WIDGET_REALIZED(win
->GetHandle()))
399 window
= win
->GetHandle()->window
;
407 case wxSYS_FRAMESIZE_X
:
408 case wxSYS_FRAMESIZE_Y
:
409 // If a window is specified/realized, and it is a toplevel window, we can query from wm.
410 // The returned border thickness is outside the client area in that case.
413 wxTopLevelWindow
*tlw
= wxDynamicCast(win
, wxTopLevelWindow
);
415 return -1; // not a tlw, not sure how to approach
418 // Check if wm supports frame extents - we can't know
419 // the border widths if it does not.
420 #if GTK_CHECK_VERSION(2,2,0)
421 if (!gtk_check_version(2,2,0))
423 if (!gdk_x11_screen_supports_net_wm_hint(
424 gdk_drawable_get_screen(window
),
425 gdk_atom_intern("_NET_FRAME_EXTENTS", false) ) )
431 if (!gdk_net_wm_supports(gdk_atom_intern("_NET_FRAME_EXTENTS", false)))
435 // Get the frame extents from the windowmanager.
436 // In most cases the top extent is the titlebar, so we use the bottom extent
438 if (wxXGetWindowProperty(window
, type
, format
, nitems
, data
))
440 int border_return
= -1;
442 if ((type
== XA_CARDINAL
) && (format
== 32) && (nitems
>= 4) && (data
))
448 case wxSYS_FRAMESIZE_X
:
449 border_return
= int(data
[1]); // width of right extent
452 border_return
= int(data
[3]); // height of bottom extent
460 return border_return
;
465 return -1; // no window specified
470 if (!gtk_check_version(2,4,0))
473 return gdk_display_get_default_cursor_size(gdk_drawable_get_display(window
));
475 return gdk_display_get_default_cursor_size(gdk_display_get_default());
483 gint dclick_distance
;
484 #if GTK_CHECK_VERSION(2,2,0)
485 if (window
&& !gtk_check_version(2,2,0))
486 g_object_get(gtk_settings_get_for_screen(gdk_drawable_get_screen(window
)),
487 "gtk-double-click-distance", &dclick_distance
, NULL
);
490 g_object_get(gtk_settings_get_default(),
491 "gtk-double-click-distance", &dclick_distance
, NULL
);
493 return dclick_distance
* 2;
498 #if GTK_CHECK_VERSION(2,2,0)
499 if (window
&& !gtk_check_version(2,2,0))
502 gtk_settings_get_for_screen(gdk_drawable_get_screen(window
)),
503 "gtk-dnd-drag-threshold",
504 &drag_threshold
, NULL
);
509 g_object_get(gtk_settings_get_default(),
510 "gtk-dnd-drag-threshold", &drag_threshold
, NULL
);
513 // The correct thing here would be to double the value
514 // since that is what the API wants. But the values
515 // are much bigger under GNOME than under Windows and
516 // just seem to much in many cases to be useful.
517 // drag_threshold *= 2;
519 return drag_threshold
;
521 // MBN: ditto for icons
522 case wxSYS_ICON_X
: return 32;
523 case wxSYS_ICON_Y
: return 32;
526 #if GTK_CHECK_VERSION(2,2,0)
527 if (window
&& !gtk_check_version(2,2,0))
528 return gdk_screen_get_width(gdk_drawable_get_screen(window
));
531 return gdk_screen_width();
534 #if GTK_CHECK_VERSION(2,2,0)
535 if (window
&& !gtk_check_version(2,2,0))
536 return gdk_screen_get_height(gdk_drawable_get_screen(window
));
539 return gdk_screen_height();
541 case wxSYS_HSCROLL_Y
: return 15;
542 case wxSYS_VSCROLL_X
: return 15;
544 case wxSYS_CAPTION_Y
:
546 // No realized window specified, and no implementation for that case yet.
549 // Check if wm supports frame extents - we can't know the caption height if it does not.
550 #if GTK_CHECK_VERSION(2,2,0)
551 if (!gtk_check_version(2,2,0))
553 if (!gdk_x11_screen_supports_net_wm_hint(
554 gdk_drawable_get_screen(window
),
555 gdk_atom_intern("_NET_FRAME_EXTENTS", false) ) )
561 if (!gdk_net_wm_supports(gdk_atom_intern("_NET_FRAME_EXTENTS", false)))
565 wxASSERT_MSG( wxDynamicCast(win
, wxTopLevelWindow
),
566 wxT("Asking for caption height of a non toplevel window") );
568 // Get the height of the top windowmanager border.
569 // This is the titlebar in most cases. The titlebar might be elsewhere, and
570 // we could check which is the thickest wm border to decide on which side the
571 // titlebar is, but this might lead to interesting behaviours in used code.
572 // Reconsider when we have a way to report to the user on which side it is.
573 if (wxXGetWindowProperty(window
, type
, format
, nitems
, data
))
575 int caption_height
= -1;
577 if ((type
== XA_CARDINAL
) && (format
== 32) && (nitems
>= 3) && (data
))
579 caption_height
= int(data
[2]); // top frame extent
585 return caption_height
;
588 // Try a default approach without a window pointer, if possible
593 case wxSYS_PENWINDOWS_PRESENT
:
594 // No MS Windows for Pen computing extension available in X11 based gtk+.
598 return -1; // metric is unknown
602 bool wxSystemSettingsNative::HasFeature(wxSystemFeature index
)
606 case wxSYS_CAN_ICONIZE_FRAME
:
609 case wxSYS_CAN_DRAW_FRAME_DECORATIONS
: