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
,
48 m_colMenuItemHighlight
,
56 static wxSystemObjects gs_objects
;
58 void wxClearGtkSystemObjects()
60 gs_objects
.m_colBtnFace
= wxColour();
61 gs_objects
.m_colBtnShadow
= wxColour();
62 gs_objects
.m_colBtnHighlight
= wxColour();
63 gs_objects
.m_colHighlightText
= wxColour();
64 gs_objects
.m_colListBox
= wxColour();
65 gs_objects
.m_colWindow
= wxColour();
66 gs_objects
.m_colWindowText
= wxColour();
67 gs_objects
.m_colBtnText
= wxColour();
68 gs_objects
.m_colMenuItemHighlight
= wxColour();
69 gs_objects
.m_colTooltip
= wxColour();
70 gs_objects
.m_colTooltipText
= wxColour();
71 gs_objects
.m_colMenubarBg
= wxColour();
72 gs_objects
.m_fontSystem
= wxNullFont
;
75 // ----------------------------------------------------------------------------
76 // wxSystemSettings implementation
77 // ----------------------------------------------------------------------------
79 // kind of widget to use in GetColourFromGTKWidget
97 // wxSystemSettings::GetColour() helper: get the colours from a GTK+
98 // widget style, return true if we did get them
99 static bool GetColourFromGTKWidget(GdkColor
& gdkColor
,
100 wxGtkWidgetType type
= wxGTK_BUTTON
,
101 GtkStateType state
= GTK_STATE_NORMAL
,
102 wxGtkColourType colour
= wxGTK_BG
)
108 wxFAIL_MSG( _T("unexpected GTK widget type") );
112 widget
= gtk_button_new();
116 widget
= gtk_text_view_new();
120 widget
= gtk_tree_view_new_with_model(
121 (GtkTreeModel
*)gtk_list_store_new(1, G_TYPE_INT
));
125 widget
= gtk_menu_item_new();
128 widget
= gtk_menu_bar_new();
133 GtkStyle
*def
= gtk_rc_get_style( widget
);
135 def
= gtk_widget_get_default_style();
137 const bool ok
= def
!= NULL
;
143 wxFAIL_MSG( _T("unexpected GTK colour type") );
147 gdkColor
= def
->fg
[state
];
151 gdkColor
= def
->bg
[state
];
155 gdkColor
= def
->base
[state
];
160 gtk_object_sink((GtkObject
*)widget
);
165 static void GetTooltipColors()
167 GtkWidget
* widget
= gtk_window_new(GTK_WINDOW_POPUP
);
168 const char* name
= "gtk-tooltip";
169 if (gtk_check_version(2, 11, 0))
170 name
= "gtk-tooltips";
171 gtk_widget_set_name(widget
, name
);
172 gtk_widget_ensure_style(widget
);
174 GdkColor c
= widget
->style
->bg
[GTK_STATE_NORMAL
];
175 gs_objects
.m_colTooltip
= wxColor(c
);
176 c
= widget
->style
->fg
[GTK_STATE_NORMAL
];
177 gs_objects
.m_colTooltipText
= wxColor(c
);
179 gtk_widget_destroy(widget
);
182 wxColour
wxSystemSettingsNative::GetColour( wxSystemColour index
)
188 case wxSYS_COLOUR_SCROLLBAR
:
189 case wxSYS_COLOUR_BACKGROUND
:
190 case wxSYS_COLOUR_INACTIVECAPTION
:
191 case wxSYS_COLOUR_MENU
:
192 case wxSYS_COLOUR_WINDOWFRAME
:
193 case wxSYS_COLOUR_ACTIVEBORDER
:
194 case wxSYS_COLOUR_INACTIVEBORDER
:
195 case wxSYS_COLOUR_BTNFACE
:
196 case wxSYS_COLOUR_3DLIGHT
:
197 if (!gs_objects
.m_colBtnFace
.Ok())
201 gdkColor
.blue
= 0x9c40;
202 GetColourFromGTKWidget(gdkColor
);
203 gs_objects
.m_colBtnFace
= wxColor(gdkColor
);
205 color
= gs_objects
.m_colBtnFace
;
208 case wxSYS_COLOUR_WINDOW
:
209 if (!gs_objects
.m_colWindow
.Ok())
213 gdkColor
.blue
= 0xFFFF;
214 GetColourFromGTKWidget(gdkColor
, wxGTK_TEXTCTRL
, GTK_STATE_NORMAL
, wxGTK_BASE
);
215 gs_objects
.m_colWindow
= wxColor(gdkColor
);
217 color
= gs_objects
.m_colWindow
;
221 case wxSYS_COLOUR_MENUBAR
:
222 if (!gs_objects
.m_colMenubarBg
.Ok())
226 gdkColor
.blue
= 0x9c40;
227 GetColourFromGTKWidget(gdkColor
,wxGTK_MENUBAR
);
228 gs_objects
.m_colMenubarBg
= wxColor(gdkColor
);
230 color
= gs_objects
.m_colMenubarBg
;
233 case wxSYS_COLOUR_3DDKSHADOW
:
237 case wxSYS_COLOUR_GRAYTEXT
:
238 case wxSYS_COLOUR_BTNSHADOW
:
239 //case wxSYS_COLOUR_3DSHADOW:
240 if (!gs_objects
.m_colBtnShadow
.Ok())
242 wxColour
faceColour(GetColour(wxSYS_COLOUR_3DFACE
));
243 gs_objects
.m_colBtnShadow
=
244 wxColour((unsigned char) (faceColour
.Red() * 2 / 3),
245 (unsigned char) (faceColour
.Green() * 2 / 3),
246 (unsigned char) (faceColour
.Blue() * 2 / 3));
248 color
= gs_objects
.m_colBtnShadow
;
251 case wxSYS_COLOUR_3DHIGHLIGHT
:
252 //case wxSYS_COLOUR_BTNHIGHLIGHT:
256 case wxSYS_COLOUR_HIGHLIGHT
:
257 if (!gs_objects
.m_colHighlight
.Ok())
261 gdkColor
.blue
= 0x9c40;
262 GetColourFromGTKWidget(
263 gdkColor
, wxGTK_BUTTON
, GTK_STATE_SELECTED
);
264 gs_objects
.m_colHighlight
= wxColour(gdkColor
);
266 color
= gs_objects
.m_colHighlight
;
269 case wxSYS_COLOUR_LISTBOX
:
270 if (!gs_objects
.m_colListBox
.Ok())
272 if ( GetColourFromGTKWidget(gdkColor
,
277 gs_objects
.m_colListBox
= wxColour(gdkColor
);
281 gs_objects
.m_colListBox
= *wxWHITE
;
284 color
= gs_objects
.m_colListBox
;
287 case wxSYS_COLOUR_MENUTEXT
:
288 case wxSYS_COLOUR_WINDOWTEXT
:
289 case wxSYS_COLOUR_CAPTIONTEXT
:
290 case wxSYS_COLOUR_INACTIVECAPTIONTEXT
:
291 case wxSYS_COLOUR_BTNTEXT
:
292 if (!gs_objects
.m_colBtnText
.Ok())
297 GetColourFromGTKWidget(
298 gdkColor
, wxGTK_BUTTON
, GTK_STATE_NORMAL
, wxGTK_FG
);
299 gs_objects
.m_colBtnText
= wxColour(gdkColor
);
301 color
= gs_objects
.m_colBtnText
;
304 case wxSYS_COLOUR_INFOBK
:
305 if (!gs_objects
.m_colTooltip
.Ok()) {
308 color
= gs_objects
.m_colTooltip
;
311 case wxSYS_COLOUR_INFOTEXT
:
312 if (!gs_objects
.m_colTooltipText
.Ok()) {
315 color
= gs_objects
.m_colTooltipText
;
318 case wxSYS_COLOUR_HIGHLIGHTTEXT
:
319 if (!gs_objects
.m_colHighlightText
.Ok())
321 wxColour hclr
= GetColour(wxSYS_COLOUR_HIGHLIGHT
);
322 if (hclr
.Red() > 200 && hclr
.Green() > 200 && hclr
.Blue() > 200)
323 gs_objects
.m_colHighlightText
= *wxBLACK
;
325 gs_objects
.m_colHighlightText
= *wxWHITE
;
327 color
= gs_objects
.m_colHighlightText
;
330 case wxSYS_COLOUR_APPWORKSPACE
:
331 color
= *wxWHITE
; // ?
334 case wxSYS_COLOUR_ACTIVECAPTION
:
335 case wxSYS_COLOUR_MENUHILIGHT
:
336 if (!gs_objects
.m_colMenuItemHighlight
.Ok())
341 GetColourFromGTKWidget(
342 gdkColor
, wxGTK_MENUITEM
, GTK_STATE_SELECTED
, wxGTK_BG
);
343 gs_objects
.m_colMenuItemHighlight
= wxColour(gdkColor
);
345 color
= gs_objects
.m_colMenuItemHighlight
;
348 case wxSYS_COLOUR_HOTLIGHT
:
349 case wxSYS_COLOUR_GRADIENTACTIVECAPTION
:
350 case wxSYS_COLOUR_GRADIENTINACTIVECAPTION
:
355 case wxSYS_COLOUR_MAX
:
357 wxFAIL_MSG( _T("unknown system colour index") );
365 wxFont
wxSystemSettingsNative::GetFont( wxSystemFont index
)
370 case wxSYS_OEM_FIXED_FONT
:
371 case wxSYS_ANSI_FIXED_FONT
:
372 case wxSYS_SYSTEM_FIXED_FONT
:
373 font
= *wxNORMAL_FONT
;
376 case wxSYS_ANSI_VAR_FONT
:
377 case wxSYS_SYSTEM_FONT
:
378 case wxSYS_DEVICE_DEFAULT_FONT
:
379 case wxSYS_DEFAULT_GUI_FONT
:
380 if (!gs_objects
.m_fontSystem
.Ok())
382 GtkWidget
*widget
= gtk_button_new();
383 GtkStyle
*def
= gtk_rc_get_style( widget
);
384 if ( !def
|| !def
->font_desc
)
385 def
= gtk_widget_get_default_style();
386 if ( def
&& def
->font_desc
)
388 wxNativeFontInfo info
;
390 pango_font_description_copy(def
->font_desc
);
391 gs_objects
.m_fontSystem
= wxFont(info
);
395 GtkSettings
*settings
= gtk_settings_get_default();
396 gchar
*font_name
= NULL
;
397 g_object_get ( settings
,
402 gs_objects
.m_fontSystem
= wxFont( 12, wxSWISS
, wxNORMAL
, wxNORMAL
);
404 gs_objects
.m_fontSystem
= wxFont(wxString::FromAscii(font_name
));
407 gtk_object_sink((GtkObject
*)widget
);
409 font
= gs_objects
.m_fontSystem
;
418 static bool GetFrameExtents(GdkWindow
* window
, int* left
, int* right
, int* top
, int* bottom
)
420 bool success
= false;
422 if (gdk_x11_screen_supports_net_wm_hint(
423 gdk_drawable_get_screen(window
),
424 gdk_atom_intern("_NET_FRAME_EXTENTS", false)))
427 property
= gdk_x11_get_xatom_by_name_for_display(
428 gdk_drawable_get_display(window
),
429 "_NET_FRAME_EXTENTS");
436 gulong nitems
, bytes_after
;
438 success
= XGetWindowProperty(
439 gdk_x11_drawable_get_xdisplay(window
),
440 gdk_x11_drawable_get_xid(window
),
445 &type
, &format
, &nitems
, &bytes_after
, &data
449 success
= data
&& nitems
== 4;
452 long* p
= (long*)data
;
453 if (left
) *left
= int(p
[0]);
454 if (right
) *right
= int(p
[1]);
455 if (top
) *top
= int(p
[2]);
456 if (bottom
) *bottom
= int(p
[3]);
465 // helper: return the GtkSettings either for the screen the current window is
466 // on or for the default screen if window is NULL
467 static GtkSettings
*GetSettingsForWindowScreen(GdkWindow
*window
)
469 return window
? gtk_settings_get_for_screen(gdk_drawable_get_screen(window
))
470 : gtk_settings_get_default();
473 int wxSystemSettingsNative::GetMetric( wxSystemMetric index
, wxWindow
* win
)
475 GdkWindow
*window
= NULL
;
476 if(win
&& GTK_WIDGET_REALIZED(win
->GetHandle()))
477 window
= win
->GetHandle()->window
;
485 case wxSYS_FRAMESIZE_X
:
486 case wxSYS_FRAMESIZE_Y
:
487 // If a window is specified/realized, and it is a toplevel window, we can query from wm.
488 // The returned border thickness is outside the client area in that case.
491 wxTopLevelWindow
*tlw
= wxDynamicCast(win
, wxTopLevelWindow
);
493 return -1; // not a tlw, not sure how to approach
496 // Get the frame extents from the windowmanager.
497 // In most cases the top extent is the titlebar, so we use the bottom extent
500 if (GetFrameExtents(window
, NULL
, &right
, NULL
, &bottom
))
506 case wxSYS_FRAMESIZE_X
:
507 return right
; // width of right extent
509 return bottom
; // height of bottom extent
515 return -1; // no window specified
519 return gdk_display_get_default_cursor_size(
520 window
? gdk_drawable_get_display(window
)
521 : gdk_display_get_default());
525 gint dclick_distance
;
526 g_object_get(GetSettingsForWindowScreen(window
),
527 "gtk-double-click-distance", &dclick_distance
, NULL
);
529 return dclick_distance
* 2;
531 case wxSYS_DCLICK_MSEC
:
533 g_object_get(GetSettingsForWindowScreen(window
),
534 "gtk-double-click-time", &dclick
, NULL
);
540 g_object_get(GetSettingsForWindowScreen(window
),
541 "gtk-dnd-drag-threshold", &drag_threshold
, NULL
);
543 // The correct thing here would be to double the value
544 // since that is what the API wants. But the values
545 // are much bigger under GNOME than under Windows and
546 // just seem to much in many cases to be useful.
547 // drag_threshold *= 2;
549 return drag_threshold
;
557 return gdk_screen_get_width(gdk_drawable_get_screen(window
));
559 return gdk_screen_width();
563 return gdk_screen_get_height(gdk_drawable_get_screen(window
));
565 return gdk_screen_height();
567 case wxSYS_HSCROLL_Y
:
568 case wxSYS_VSCROLL_X
:
571 case wxSYS_CAPTION_Y
:
573 // No realized window specified, and no implementation for that case yet.
576 wxASSERT_MSG( wxDynamicCast(win
, wxTopLevelWindow
),
577 wxT("Asking for caption height of a non toplevel window") );
579 // Get the height of the top windowmanager border.
580 // This is the titlebar in most cases. The titlebar might be elsewhere, and
581 // we could check which is the thickest wm border to decide on which side the
582 // titlebar is, but this might lead to interesting behaviours in used code.
583 // Reconsider when we have a way to report to the user on which side it is.
586 if (GetFrameExtents(window
, NULL
, NULL
, &top
, NULL
))
588 return top
; // top frame extent
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
: