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
,
55 static wxSystemObjects gs_objects
;
57 void wxClearGtkSystemObjects()
59 gs_objects
.m_colBtnFace
= wxColour();
60 gs_objects
.m_colBtnShadow
= wxColour();
61 gs_objects
.m_colBtnHighlight
= wxColour();
62 gs_objects
.m_colHighlightText
= wxColour();
63 gs_objects
.m_colListBox
= wxColour();
64 gs_objects
.m_colWindow
= wxColour();
65 gs_objects
.m_colWindowText
= wxColour();
66 gs_objects
.m_colBtnText
= wxColour();
67 gs_objects
.m_colMenuItemHighlight
= wxColour();
68 gs_objects
.m_colTooltip
= wxColour();
69 gs_objects
.m_colTooltipText
= wxColour();
70 gs_objects
.m_fontSystem
= wxNullFont
;
73 // ----------------------------------------------------------------------------
74 // wxSystemSettings implementation
75 // ----------------------------------------------------------------------------
77 // kind of widget to use in GetColourFromGTKWidget
94 // wxSystemSettings::GetColour() helper: get the colours from a GTK+
95 // widget style, return true if we did get them
96 static bool GetColourFromGTKWidget(GdkColor
& gdkColor
,
97 wxGtkWidgetType type
= wxGTK_BUTTON
,
98 GtkStateType state
= GTK_STATE_NORMAL
,
99 wxGtkColourType colour
= wxGTK_BG
)
105 wxFAIL_MSG( _T("unexpected GTK widget type") );
109 widget
= gtk_button_new();
113 widget
= gtk_text_view_new();
117 widget
= gtk_tree_view_new_with_model(
118 (GtkTreeModel
*)gtk_list_store_new(1, G_TYPE_INT
));
122 widget
= gtk_menu_item_new();
125 GtkStyle
*def
= gtk_rc_get_style( widget
);
127 def
= gtk_widget_get_default_style();
129 const bool ok
= def
!= NULL
;
135 wxFAIL_MSG( _T("unexpected GTK colour type") );
139 gdkColor
= def
->fg
[state
];
143 gdkColor
= def
->bg
[state
];
147 gdkColor
= def
->base
[state
];
152 gtk_object_sink((GtkObject
*)widget
);
157 static void GetTooltipColors()
159 GtkWidget
* widget
= gtk_window_new(GTK_WINDOW_POPUP
);
160 const char* name
= "gtk-tooltip";
161 if (gtk_check_version(2, 11, 0))
162 name
= "gtk-tooltips";
163 gtk_widget_set_name(widget
, name
);
164 gtk_widget_ensure_style(widget
);
166 GdkColor c
= widget
->style
->bg
[GTK_STATE_NORMAL
];
167 gs_objects
.m_colTooltip
= wxColor(c
);
168 c
= widget
->style
->fg
[GTK_STATE_NORMAL
];
169 gs_objects
.m_colTooltipText
= wxColor(c
);
171 gtk_widget_destroy(widget
);
174 wxColour
wxSystemSettingsNative::GetColour( wxSystemColour index
)
180 case wxSYS_COLOUR_SCROLLBAR
:
181 case wxSYS_COLOUR_BACKGROUND
:
182 case wxSYS_COLOUR_INACTIVECAPTION
:
183 case wxSYS_COLOUR_MENU
:
184 case wxSYS_COLOUR_WINDOWFRAME
:
185 case wxSYS_COLOUR_ACTIVEBORDER
:
186 case wxSYS_COLOUR_INACTIVEBORDER
:
187 case wxSYS_COLOUR_BTNFACE
:
188 case wxSYS_COLOUR_MENUBAR
:
189 case wxSYS_COLOUR_3DLIGHT
:
190 if (!gs_objects
.m_colBtnFace
.Ok())
194 gdkColor
.blue
= 0x9c40;
195 GetColourFromGTKWidget(gdkColor
);
196 gs_objects
.m_colBtnFace
= wxColor(gdkColor
);
198 color
= gs_objects
.m_colBtnFace
;
201 case wxSYS_COLOUR_WINDOW
:
202 if (!gs_objects
.m_colWindow
.Ok())
206 gdkColor
.blue
= 0xFFFF;
207 GetColourFromGTKWidget(gdkColor
, wxGTK_TEXTCTRL
, GTK_STATE_NORMAL
, wxGTK_BASE
);
208 gs_objects
.m_colWindow
= wxColor(gdkColor
);
210 color
= gs_objects
.m_colWindow
;
213 case wxSYS_COLOUR_3DDKSHADOW
:
217 case wxSYS_COLOUR_GRAYTEXT
:
218 case wxSYS_COLOUR_BTNSHADOW
:
219 //case wxSYS_COLOUR_3DSHADOW:
220 if (!gs_objects
.m_colBtnShadow
.Ok())
222 wxColour
faceColour(GetColour(wxSYS_COLOUR_3DFACE
));
223 gs_objects
.m_colBtnShadow
=
224 wxColour((unsigned char) (faceColour
.Red() * 2 / 3),
225 (unsigned char) (faceColour
.Green() * 2 / 3),
226 (unsigned char) (faceColour
.Blue() * 2 / 3));
228 color
= gs_objects
.m_colBtnShadow
;
231 case wxSYS_COLOUR_3DHIGHLIGHT
:
232 //case wxSYS_COLOUR_BTNHIGHLIGHT:
236 case wxSYS_COLOUR_HIGHLIGHT
:
237 if (!gs_objects
.m_colHighlight
.Ok())
241 gdkColor
.blue
= 0x9c40;
242 GetColourFromGTKWidget(
243 gdkColor
, wxGTK_BUTTON
, GTK_STATE_SELECTED
);
244 gs_objects
.m_colHighlight
= wxColour(gdkColor
);
246 color
= gs_objects
.m_colHighlight
;
249 case wxSYS_COLOUR_LISTBOX
:
250 if (!gs_objects
.m_colListBox
.Ok())
252 if ( GetColourFromGTKWidget(gdkColor
,
257 gs_objects
.m_colListBox
= wxColour(gdkColor
);
261 gs_objects
.m_colListBox
= *wxWHITE
;
264 color
= gs_objects
.m_colListBox
;
267 case wxSYS_COLOUR_MENUTEXT
:
268 case wxSYS_COLOUR_WINDOWTEXT
:
269 case wxSYS_COLOUR_CAPTIONTEXT
:
270 case wxSYS_COLOUR_INACTIVECAPTIONTEXT
:
271 case wxSYS_COLOUR_BTNTEXT
:
272 if (!gs_objects
.m_colBtnText
.Ok())
277 GetColourFromGTKWidget(
278 gdkColor
, wxGTK_BUTTON
, GTK_STATE_NORMAL
, wxGTK_FG
);
279 gs_objects
.m_colBtnText
= wxColour(gdkColor
);
281 color
= gs_objects
.m_colBtnText
;
284 case wxSYS_COLOUR_INFOBK
:
285 if (!gs_objects
.m_colTooltip
.Ok()) {
288 color
= gs_objects
.m_colTooltip
;
291 case wxSYS_COLOUR_INFOTEXT
:
292 if (!gs_objects
.m_colTooltipText
.Ok()) {
295 color
= gs_objects
.m_colTooltipText
;
298 case wxSYS_COLOUR_HIGHLIGHTTEXT
:
299 if (!gs_objects
.m_colHighlightText
.Ok())
301 wxColour hclr
= GetColour(wxSYS_COLOUR_HIGHLIGHT
);
302 if (hclr
.Red() > 200 && hclr
.Green() > 200 && hclr
.Blue() > 200)
303 gs_objects
.m_colHighlightText
= *wxBLACK
;
305 gs_objects
.m_colHighlightText
= *wxWHITE
;
307 color
= gs_objects
.m_colHighlightText
;
310 case wxSYS_COLOUR_APPWORKSPACE
:
311 color
= *wxWHITE
; // ?
314 case wxSYS_COLOUR_ACTIVECAPTION
:
315 case wxSYS_COLOUR_MENUHILIGHT
:
316 if (!gs_objects
.m_colMenuItemHighlight
.Ok())
321 GetColourFromGTKWidget(
322 gdkColor
, wxGTK_MENUITEM
, GTK_STATE_SELECTED
, wxGTK_BG
);
323 gs_objects
.m_colMenuItemHighlight
= wxColour(gdkColor
);
325 color
= gs_objects
.m_colMenuItemHighlight
;
328 case wxSYS_COLOUR_HOTLIGHT
:
329 case wxSYS_COLOUR_GRADIENTACTIVECAPTION
:
330 case wxSYS_COLOUR_GRADIENTINACTIVECAPTION
:
335 case wxSYS_COLOUR_MAX
:
337 wxFAIL_MSG( _T("unknown system colour index") );
345 wxFont
wxSystemSettingsNative::GetFont( wxSystemFont index
)
350 case wxSYS_OEM_FIXED_FONT
:
351 case wxSYS_ANSI_FIXED_FONT
:
352 case wxSYS_SYSTEM_FIXED_FONT
:
353 font
= *wxNORMAL_FONT
;
356 case wxSYS_ANSI_VAR_FONT
:
357 case wxSYS_SYSTEM_FONT
:
358 case wxSYS_DEVICE_DEFAULT_FONT
:
359 case wxSYS_DEFAULT_GUI_FONT
:
360 if (!gs_objects
.m_fontSystem
.Ok())
362 GtkWidget
*widget
= gtk_button_new();
363 GtkStyle
*def
= gtk_rc_get_style( widget
);
364 if ( !def
|| !def
->font_desc
)
365 def
= gtk_widget_get_default_style();
366 if ( def
&& def
->font_desc
)
368 wxNativeFontInfo info
;
370 pango_font_description_copy(def
->font_desc
);
371 gs_objects
.m_fontSystem
= wxFont(info
);
375 GtkSettings
*settings
= gtk_settings_get_default();
376 gchar
*font_name
= NULL
;
377 g_object_get ( settings
,
382 gs_objects
.m_fontSystem
= wxFont( 12, wxSWISS
, wxNORMAL
, wxNORMAL
);
384 gs_objects
.m_fontSystem
= wxFont(wxString::FromAscii(font_name
));
387 gtk_object_sink((GtkObject
*)widget
);
389 font
= gs_objects
.m_fontSystem
;
398 static bool GetFrameExtents(GdkWindow
* window
, int* left
, int* right
, int* top
, int* bottom
)
400 bool success
= false;
402 if (gdk_x11_screen_supports_net_wm_hint(
403 gdk_drawable_get_screen(window
),
404 gdk_atom_intern("_NET_FRAME_EXTENTS", false)))
407 property
= gdk_x11_get_xatom_by_name_for_display(
408 gdk_drawable_get_display(window
),
409 "_NET_FRAME_EXTENTS");
416 gulong nitems
, bytes_after
;
418 success
= XGetWindowProperty(
419 gdk_x11_drawable_get_xdisplay(window
),
420 gdk_x11_drawable_get_xid(window
),
425 &type
, &format
, &nitems
, &bytes_after
, (guchar
**)&data
429 success
= data
&& nitems
== 4;
432 if (left
) *left
= int(data
[0]);
433 if (right
) *right
= int(data
[1]);
434 if (top
) *top
= int(data
[2]);
435 if (bottom
) *bottom
= int(data
[3]);
444 // helper: return the GtkSettings either for the screen the current window is
445 // on or for the default screen if window is NULL
446 static GtkSettings
*GetSettingsForWindowScreen(GdkWindow
*window
)
448 return window
? gtk_settings_get_for_screen(gdk_drawable_get_screen(window
))
449 : gtk_settings_get_default();
452 int wxSystemSettingsNative::GetMetric( wxSystemMetric index
, wxWindow
* win
)
454 GdkWindow
*window
= NULL
;
455 if(win
&& GTK_WIDGET_REALIZED(win
->GetHandle()))
456 window
= win
->GetHandle()->window
;
464 case wxSYS_FRAMESIZE_X
:
465 case wxSYS_FRAMESIZE_Y
:
466 // If a window is specified/realized, and it is a toplevel window, we can query from wm.
467 // The returned border thickness is outside the client area in that case.
470 wxTopLevelWindow
*tlw
= wxDynamicCast(win
, wxTopLevelWindow
);
472 return -1; // not a tlw, not sure how to approach
475 // Get the frame extents from the windowmanager.
476 // In most cases the top extent is the titlebar, so we use the bottom extent
479 if (GetFrameExtents(window
, NULL
, &right
, NULL
, &bottom
))
485 case wxSYS_FRAMESIZE_X
:
486 return right
; // width of right extent
488 return bottom
; // height of bottom extent
494 return -1; // no window specified
498 return gdk_display_get_default_cursor_size(
499 window
? gdk_drawable_get_display(window
)
500 : gdk_display_get_default());
504 gint dclick_distance
;
505 g_object_get(GetSettingsForWindowScreen(window
),
506 "gtk-double-click-distance", &dclick_distance
, NULL
);
508 return dclick_distance
* 2;
510 case wxSYS_DCLICK_MSEC
:
512 g_object_get(GetSettingsForWindowScreen(window
),
513 "gtk-double-click-time", &dclick
, NULL
);
519 g_object_get(GetSettingsForWindowScreen(window
),
520 "gtk-dnd-drag-threshold", &drag_threshold
, NULL
);
522 // The correct thing here would be to double the value
523 // since that is what the API wants. But the values
524 // are much bigger under GNOME than under Windows and
525 // just seem to much in many cases to be useful.
526 // drag_threshold *= 2;
528 return drag_threshold
;
536 return gdk_screen_get_width(gdk_drawable_get_screen(window
));
538 return gdk_screen_width();
542 return gdk_screen_get_height(gdk_drawable_get_screen(window
));
544 return gdk_screen_height();
546 case wxSYS_HSCROLL_Y
:
547 case wxSYS_VSCROLL_X
:
550 case wxSYS_CAPTION_Y
:
552 // No realized window specified, and no implementation for that case yet.
555 wxASSERT_MSG( wxDynamicCast(win
, wxTopLevelWindow
),
556 wxT("Asking for caption height of a non toplevel window") );
558 // Get the height of the top windowmanager border.
559 // This is the titlebar in most cases. The titlebar might be elsewhere, and
560 // we could check which is the thickest wm border to decide on which side the
561 // titlebar is, but this might lead to interesting behaviours in used code.
562 // Reconsider when we have a way to report to the user on which side it is.
565 if (GetFrameExtents(window
, NULL
, NULL
, &top
, NULL
))
567 return top
; // top frame extent
571 // Try a default approach without a window pointer, if possible
576 case wxSYS_PENWINDOWS_PRESENT
:
577 // No MS Windows for Pen computing extension available in X11 based gtk+.
581 return -1; // metric is unknown
585 bool wxSystemSettingsNative::HasFeature(wxSystemFeature index
)
589 case wxSYS_CAN_ICONIZE_FRAME
:
592 case wxSYS_CAN_DRAW_FRAME_DECORATIONS
: