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
30 bool wxGetFrameExtents(GdkWindow
* window
, int* left
, int* right
, int* top
, int* bottom
);
32 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
36 struct wxSystemObjects
38 wxColour m_colBtnFace
,
47 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_colMenubarBg
= wxColour();
71 gs_objects
.m_fontSystem
= wxNullFont
;
74 // ----------------------------------------------------------------------------
75 // wxSystemSettings implementation
76 // ----------------------------------------------------------------------------
78 // 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();
129 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
->text
[state
];
159 gdkColor
= def
->base
[state
];
164 gtk_object_sink((GtkObject
*)widget
);
169 static void GetTooltipColors()
171 GtkWidget
* widget
= gtk_window_new(GTK_WINDOW_POPUP
);
172 const char* name
= "gtk-tooltip";
173 if (gtk_check_version(2, 11, 0))
174 name
= "gtk-tooltips";
175 gtk_widget_set_name(widget
, name
);
176 gtk_widget_ensure_style(widget
);
178 GdkColor c
= widget
->style
->bg
[GTK_STATE_NORMAL
];
179 gs_objects
.m_colTooltip
= wxColor(c
);
180 c
= widget
->style
->fg
[GTK_STATE_NORMAL
];
181 gs_objects
.m_colTooltipText
= wxColor(c
);
183 gtk_widget_destroy(widget
);
186 wxColour
wxSystemSettingsNative::GetColour( wxSystemColour index
)
192 case wxSYS_COLOUR_SCROLLBAR
:
193 case wxSYS_COLOUR_BACKGROUND
:
194 case wxSYS_COLOUR_INACTIVECAPTION
:
195 case wxSYS_COLOUR_MENU
:
196 case wxSYS_COLOUR_WINDOWFRAME
:
197 case wxSYS_COLOUR_ACTIVEBORDER
:
198 case wxSYS_COLOUR_INACTIVEBORDER
:
199 case wxSYS_COLOUR_BTNFACE
:
200 case wxSYS_COLOUR_3DLIGHT
:
201 if (!gs_objects
.m_colBtnFace
.Ok())
205 gdkColor
.blue
= 0x9c40;
206 GetColourFromGTKWidget(gdkColor
);
207 gs_objects
.m_colBtnFace
= wxColor(gdkColor
);
209 color
= gs_objects
.m_colBtnFace
;
212 case wxSYS_COLOUR_WINDOW
:
213 if (!gs_objects
.m_colWindow
.Ok())
217 gdkColor
.blue
= 0xFFFF;
218 GetColourFromGTKWidget(gdkColor
, wxGTK_TEXTCTRL
, GTK_STATE_NORMAL
, wxGTK_BASE
);
219 gs_objects
.m_colWindow
= wxColor(gdkColor
);
221 color
= gs_objects
.m_colWindow
;
225 case wxSYS_COLOUR_MENUBAR
:
226 if (!gs_objects
.m_colMenubarBg
.Ok())
230 gdkColor
.blue
= 0x9c40;
231 GetColourFromGTKWidget(gdkColor
,wxGTK_MENUBAR
);
232 gs_objects
.m_colMenubarBg
= wxColor(gdkColor
);
234 color
= gs_objects
.m_colMenubarBg
;
237 case wxSYS_COLOUR_3DDKSHADOW
:
241 case wxSYS_COLOUR_GRAYTEXT
:
242 case wxSYS_COLOUR_BTNSHADOW
:
243 //case wxSYS_COLOUR_3DSHADOW:
244 if (!gs_objects
.m_colBtnShadow
.Ok())
246 wxColour
faceColour(GetColour(wxSYS_COLOUR_3DFACE
));
247 gs_objects
.m_colBtnShadow
=
248 wxColour((unsigned char) (faceColour
.Red() * 2 / 3),
249 (unsigned char) (faceColour
.Green() * 2 / 3),
250 (unsigned char) (faceColour
.Blue() * 2 / 3));
252 color
= gs_objects
.m_colBtnShadow
;
255 case wxSYS_COLOUR_3DHIGHLIGHT
:
256 //case wxSYS_COLOUR_BTNHIGHLIGHT:
260 case wxSYS_COLOUR_HIGHLIGHT
:
261 if (!gs_objects
.m_colHighlight
.Ok())
265 gdkColor
.blue
= 0x9c40;
266 GetColourFromGTKWidget(
267 gdkColor
, wxGTK_BUTTON
, GTK_STATE_SELECTED
);
268 gs_objects
.m_colHighlight
= wxColour(gdkColor
);
270 color
= gs_objects
.m_colHighlight
;
273 case wxSYS_COLOUR_LISTBOX
:
274 if (!gs_objects
.m_colListBox
.Ok())
276 if ( GetColourFromGTKWidget(gdkColor
,
281 gs_objects
.m_colListBox
= wxColour(gdkColor
);
285 gs_objects
.m_colListBox
= *wxWHITE
;
288 color
= gs_objects
.m_colListBox
;
291 case wxSYS_COLOUR_MENUTEXT
:
292 case wxSYS_COLOUR_WINDOWTEXT
:
293 case wxSYS_COLOUR_CAPTIONTEXT
:
294 case wxSYS_COLOUR_INACTIVECAPTIONTEXT
:
295 case wxSYS_COLOUR_BTNTEXT
:
296 if (!gs_objects
.m_colBtnText
.Ok())
301 GetColourFromGTKWidget(
302 gdkColor
, wxGTK_BUTTON
, GTK_STATE_NORMAL
, wxGTK_FG
);
303 gs_objects
.m_colBtnText
= wxColour(gdkColor
);
305 color
= gs_objects
.m_colBtnText
;
308 case wxSYS_COLOUR_INFOBK
:
309 if (!gs_objects
.m_colTooltip
.Ok()) {
312 color
= gs_objects
.m_colTooltip
;
315 case wxSYS_COLOUR_INFOTEXT
:
316 if (!gs_objects
.m_colTooltipText
.Ok()) {
319 color
= gs_objects
.m_colTooltipText
;
322 case wxSYS_COLOUR_HIGHLIGHTTEXT
:
323 if (!gs_objects
.m_colHighlightText
.Ok())
328 GetColourFromGTKWidget(
329 gdkColor
, wxGTK_BUTTON
, GTK_STATE_SELECTED
, wxGTK_TEXT
);
330 gs_objects
.m_colHighlightText
= wxColour(gdkColor
);
332 color
= gs_objects
.m_colHighlightText
;
335 case wxSYS_COLOUR_APPWORKSPACE
:
336 color
= *wxWHITE
; // ?
339 case wxSYS_COLOUR_ACTIVECAPTION
:
340 case wxSYS_COLOUR_MENUHILIGHT
:
341 if (!gs_objects
.m_colMenuItemHighlight
.Ok())
346 GetColourFromGTKWidget(
347 gdkColor
, wxGTK_MENUITEM
, GTK_STATE_SELECTED
, wxGTK_BG
);
348 gs_objects
.m_colMenuItemHighlight
= wxColour(gdkColor
);
350 color
= gs_objects
.m_colMenuItemHighlight
;
353 case wxSYS_COLOUR_HOTLIGHT
:
354 case wxSYS_COLOUR_GRADIENTACTIVECAPTION
:
355 case wxSYS_COLOUR_GRADIENTINACTIVECAPTION
:
360 case wxSYS_COLOUR_MAX
:
362 wxFAIL_MSG( _T("unknown system colour index") );
370 wxFont
wxSystemSettingsNative::GetFont( wxSystemFont index
)
375 case wxSYS_OEM_FIXED_FONT
:
376 case wxSYS_ANSI_FIXED_FONT
:
377 case wxSYS_SYSTEM_FIXED_FONT
:
378 font
= *wxNORMAL_FONT
;
381 case wxSYS_ANSI_VAR_FONT
:
382 case wxSYS_SYSTEM_FONT
:
383 case wxSYS_DEVICE_DEFAULT_FONT
:
384 case wxSYS_DEFAULT_GUI_FONT
:
385 if (!gs_objects
.m_fontSystem
.Ok())
387 GtkWidget
*widget
= gtk_button_new();
388 GtkStyle
*def
= gtk_rc_get_style( widget
);
389 if ( !def
|| !def
->font_desc
)
390 def
= gtk_widget_get_default_style();
391 if ( def
&& def
->font_desc
)
393 wxNativeFontInfo info
;
395 pango_font_description_copy(def
->font_desc
);
396 gs_objects
.m_fontSystem
= wxFont(info
);
400 GtkSettings
*settings
= gtk_settings_get_default();
401 gchar
*font_name
= NULL
;
402 g_object_get ( settings
,
407 gs_objects
.m_fontSystem
= wxFont( 12, wxSWISS
, wxNORMAL
, wxNORMAL
);
409 gs_objects
.m_fontSystem
= wxFont(wxString::FromAscii(font_name
));
412 gtk_object_sink((GtkObject
*)widget
);
414 font
= gs_objects
.m_fontSystem
;
423 // helper: return the GtkSettings either for the screen the current window is
424 // on or for the default screen if window is NULL
425 static GtkSettings
*GetSettingsForWindowScreen(GdkWindow
*window
)
427 return window
? gtk_settings_get_for_screen(gdk_drawable_get_screen(window
))
428 : gtk_settings_get_default();
431 int wxSystemSettingsNative::GetMetric( wxSystemMetric index
, wxWindow
* win
)
433 GdkWindow
*window
= NULL
;
434 if(win
&& GTK_WIDGET_REALIZED(win
->GetHandle()))
435 window
= win
->GetHandle()->window
;
443 case wxSYS_FRAMESIZE_X
:
444 case wxSYS_FRAMESIZE_Y
:
445 // If a window is specified/realized, and it is a toplevel window, we can query from wm.
446 // The returned border thickness is outside the client area in that case.
449 wxTopLevelWindow
*tlw
= wxDynamicCast(win
, wxTopLevelWindow
);
451 return -1; // not a tlw, not sure how to approach
454 // Get the frame extents from the windowmanager.
455 // In most cases the top extent is the titlebar, so we use the bottom extent
458 if (wxGetFrameExtents(window
, NULL
, &right
, NULL
, &bottom
))
464 case wxSYS_FRAMESIZE_X
:
465 return right
; // width of right extent
467 return bottom
; // height of bottom extent
473 return -1; // no window specified
477 return gdk_display_get_default_cursor_size(
478 window
? gdk_drawable_get_display(window
)
479 : gdk_display_get_default());
483 gint dclick_distance
;
484 g_object_get(GetSettingsForWindowScreen(window
),
485 "gtk-double-click-distance", &dclick_distance
, NULL
);
487 return dclick_distance
* 2;
489 case wxSYS_DCLICK_MSEC
:
491 g_object_get(GetSettingsForWindowScreen(window
),
492 "gtk-double-click-time", &dclick
, NULL
);
498 g_object_get(GetSettingsForWindowScreen(window
),
499 "gtk-dnd-drag-threshold", &drag_threshold
, NULL
);
501 // The correct thing here would be to double the value
502 // since that is what the API wants. But the values
503 // are much bigger under GNOME than under Windows and
504 // just seem to much in many cases to be useful.
505 // drag_threshold *= 2;
507 return drag_threshold
;
515 return gdk_screen_get_width(gdk_drawable_get_screen(window
));
517 return gdk_screen_width();
521 return gdk_screen_get_height(gdk_drawable_get_screen(window
));
523 return gdk_screen_height();
525 case wxSYS_HSCROLL_Y
:
526 case wxSYS_VSCROLL_X
:
529 case wxSYS_CAPTION_Y
:
531 // No realized window specified, and no implementation for that case yet.
534 wxASSERT_MSG( wxDynamicCast(win
, wxTopLevelWindow
),
535 wxT("Asking for caption height of a non toplevel window") );
537 // Get the height of the top windowmanager border.
538 // This is the titlebar in most cases. The titlebar might be elsewhere, and
539 // we could check which is the thickest wm border to decide on which side the
540 // titlebar is, but this might lead to interesting behaviours in used code.
541 // Reconsider when we have a way to report to the user on which side it is.
544 if (wxGetFrameExtents(window
, NULL
, NULL
, &top
, NULL
))
546 return top
; // top frame extent
550 // Try a default approach without a window pointer, if possible
555 case wxSYS_PENWINDOWS_PRESENT
:
556 // No MS Windows for Pen computing extension available in X11 based gtk+.
560 return -1; // metric is unknown
564 bool wxSystemSettingsNative::HasFeature(wxSystemFeature index
)
568 case wxSYS_CAN_ICONIZE_FRAME
:
571 case wxSYS_CAN_DRAW_FRAME_DECORATIONS
: