1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/settings.cpp
4 // Author: Robert Roebling
5 // Modified by: Mart Raudsepp (GetMetric)
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
13 #include "wx/settings.h"
16 #include "wx/toplevel.h"
19 #include "wx/fontutil.h"
20 #include "wx/fontenum.h"
23 #include "wx/gtk/private/win_gtk.h"
24 #include "wx/gtk/private/gtk2-compat.h"
26 bool wxGetFrameExtents(GdkWindow
* window
, int* left
, int* right
, int* top
, int* bottom
);
28 // ----------------------------------------------------------------------------
29 // wxSystemSettings implementation
30 // ----------------------------------------------------------------------------
32 static wxFont gs_fontSystem
;
34 static GtkContainer
* ContainerWidget()
36 static GtkContainer
* s_widget
;
39 s_widget
= GTK_CONTAINER(gtk_fixed_new());
40 GtkWidget
* window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
41 gtk_container_add(GTK_CONTAINER(window
), GTK_WIDGET(s_widget
));
48 static void style_updated(GtkWidget
*, void*)
50 static void style_set(GtkWidget
*, GtkStyle
*, void*)
53 gs_fontSystem
= wxNullFont
;
57 static GtkWidget
* ButtonWidget()
59 static GtkWidget
* s_widget
;
62 s_widget
= gtk_button_new();
63 gtk_container_add(ContainerWidget(), s_widget
);
65 g_signal_connect(s_widget
, "style_updated", G_CALLBACK(style_updated
), NULL
);
67 gtk_widget_ensure_style(s_widget
);
68 g_signal_connect(s_widget
, "style_set", G_CALLBACK(style_set
), NULL
);
74 static GtkWidget
* ListWidget()
76 static GtkWidget
* s_widget
;
79 s_widget
= gtk_tree_view_new_with_model(
80 GTK_TREE_MODEL(gtk_list_store_new(1, G_TYPE_INT
)));
81 gtk_container_add(ContainerWidget(), s_widget
);
83 gtk_widget_ensure_style(s_widget
);
89 static GtkWidget
* TextCtrlWidget()
91 static GtkWidget
* s_widget
;
94 s_widget
= gtk_text_view_new();
95 gtk_container_add(ContainerWidget(), s_widget
);
97 gtk_widget_ensure_style(s_widget
);
103 static GtkWidget
* MenuItemWidget()
105 static GtkWidget
* s_widget
;
106 if (s_widget
== NULL
)
108 s_widget
= gtk_menu_item_new();
109 gtk_container_add(ContainerWidget(), s_widget
);
111 gtk_widget_ensure_style(s_widget
);
117 static GtkWidget
* MenuBarWidget()
119 static GtkWidget
* s_widget
;
120 if (s_widget
== NULL
)
122 s_widget
= gtk_menu_bar_new();
123 gtk_container_add(ContainerWidget(), s_widget
);
125 gtk_widget_ensure_style(s_widget
);
131 static GtkWidget
* ToolTipWidget()
133 static GtkWidget
* s_widget
;
134 if (s_widget
== NULL
)
136 s_widget
= gtk_window_new(GTK_WINDOW_POPUP
);
137 const char* name
= "gtk-tooltip";
139 if (gtk_check_version(2, 11, 0))
140 name
= "gtk-tooltips";
142 gtk_widget_set_name(s_widget
, name
);
144 gtk_widget_ensure_style(s_widget
);
151 static void bg(GtkWidget
* widget
, GtkStateFlags state
, GdkRGBA
& gdkRGBA
)
153 GtkStyleContext
* sc
= gtk_widget_get_style_context(widget
);
154 gtk_style_context_get_background_color(sc
, state
, &gdkRGBA
);
156 static void fg(GtkWidget
* widget
, GtkStateFlags state
, GdkRGBA
& gdkRGBA
)
158 GtkStyleContext
* sc
= gtk_widget_get_style_context(widget
);
159 gtk_style_context_get_color(sc
, state
, &gdkRGBA
);
161 static void border(GtkWidget
* widget
, GtkStateFlags state
, GdkRGBA
& gdkRGBA
)
163 GtkStyleContext
* sc
= gtk_widget_get_style_context(widget
);
164 gtk_style_context_get_border_color(sc
, state
, &gdkRGBA
);
167 wxColour
wxSystemSettingsNative::GetColour(wxSystemColour index
)
169 GdkRGBA gdkRGBA
= { 0, 0, 0, 1 };
172 case wxSYS_COLOUR_3DLIGHT
:
173 case wxSYS_COLOUR_ACTIVEBORDER
:
174 case wxSYS_COLOUR_BTNFACE
:
175 case wxSYS_COLOUR_DESKTOP
:
176 case wxSYS_COLOUR_INACTIVEBORDER
:
177 case wxSYS_COLOUR_INACTIVECAPTION
:
178 case wxSYS_COLOUR_SCROLLBAR
:
179 case wxSYS_COLOUR_WINDOWFRAME
:
180 bg(ButtonWidget(), GTK_STATE_FLAG_NORMAL
, gdkRGBA
);
182 case wxSYS_COLOUR_BTNHIGHLIGHT
:
183 case wxSYS_COLOUR_HIGHLIGHT
:
184 bg(ButtonWidget(), GTK_STATE_FLAG_SELECTED
, gdkRGBA
);
186 case wxSYS_COLOUR_BTNSHADOW
:
187 border(ButtonWidget(), GTK_STATE_FLAG_NORMAL
, gdkRGBA
);
189 case wxSYS_COLOUR_BTNTEXT
:
190 case wxSYS_COLOUR_WINDOWTEXT
:
191 fg(ButtonWidget(), GTK_STATE_FLAG_NORMAL
, gdkRGBA
);
193 case wxSYS_COLOUR_GRAYTEXT
:
194 case wxSYS_COLOUR_INACTIVECAPTIONTEXT
:
195 fg(ButtonWidget(), GTK_STATE_FLAG_INSENSITIVE
, gdkRGBA
);
197 case wxSYS_COLOUR_HIGHLIGHTTEXT
:
198 fg(ButtonWidget(), GTK_STATE_FLAG_SELECTED
, gdkRGBA
);
200 case wxSYS_COLOUR_HOTLIGHT
:
202 static GtkWidget
* s_widget
;
203 if (s_widget
== NULL
)
205 s_widget
= gtk_link_button_new("");
206 gtk_container_add(ContainerWidget(), s_widget
);
208 fg(s_widget
, GTK_STATE_FLAG_NORMAL
, gdkRGBA
);
211 case wxSYS_COLOUR_INFOBK
:
212 bg(ToolTipWidget(), GTK_STATE_FLAG_NORMAL
, gdkRGBA
);
214 case wxSYS_COLOUR_INFOTEXT
:
215 fg(ToolTipWidget(), GTK_STATE_FLAG_NORMAL
, gdkRGBA
);
217 case wxSYS_COLOUR_LISTBOX
:
218 bg(ListWidget(), GTK_STATE_FLAG_NORMAL
, gdkRGBA
);
220 case wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT
:
221 fg(ListWidget(), GTK_STATE_FLAG_SELECTED
, gdkRGBA
);
223 case wxSYS_COLOUR_LISTBOXTEXT
:
224 fg(ListWidget(), GTK_STATE_FLAG_NORMAL
, gdkRGBA
);
226 case wxSYS_COLOUR_MENU
:
227 bg(MenuItemWidget(), GTK_STATE_FLAG_NORMAL
, gdkRGBA
);
229 case wxSYS_COLOUR_MENUBAR
:
230 bg(MenuBarWidget(), GTK_STATE_FLAG_NORMAL
, gdkRGBA
);
232 case wxSYS_COLOUR_ACTIVECAPTION
:
233 case wxSYS_COLOUR_MENUHILIGHT
:
234 bg(MenuItemWidget(), GTK_STATE_FLAG_SELECTED
, gdkRGBA
);
236 case wxSYS_COLOUR_MENUTEXT
:
237 fg(MenuItemWidget(), GTK_STATE_FLAG_NORMAL
, gdkRGBA
);
239 case wxSYS_COLOUR_APPWORKSPACE
:
240 case wxSYS_COLOUR_WINDOW
:
241 bg(TextCtrlWidget(), GTK_STATE_FLAG_NORMAL
, gdkRGBA
);
243 case wxSYS_COLOUR_CAPTIONTEXT
:
245 GdkRGBA c
= { 1, 1, 1, 1 };
250 wxFAIL_MSG("unknown system colour index");
252 case wxSYS_COLOUR_3DDKSHADOW
:
253 case wxSYS_COLOUR_GRADIENTACTIVECAPTION
:
254 case wxSYS_COLOUR_GRADIENTINACTIVECAPTION
:
258 return wxColour(gdkRGBA
);
261 static const GtkStyle
* ButtonStyle()
263 return gtk_widget_get_style(ButtonWidget());
266 static const GtkStyle
* ListStyle()
268 return gtk_widget_get_style(ListWidget());
271 static const GtkStyle
* TextCtrlStyle()
273 return gtk_widget_get_style(TextCtrlWidget());
276 static const GtkStyle
* MenuItemStyle()
278 return gtk_widget_get_style(MenuItemWidget());
281 static const GtkStyle
* MenuBarStyle()
283 return gtk_widget_get_style(MenuBarWidget());
286 static const GtkStyle
* ToolTipStyle()
288 return gtk_widget_get_style(ToolTipWidget());
291 wxColour
wxSystemSettingsNative::GetColour( wxSystemColour index
)
296 case wxSYS_COLOUR_SCROLLBAR
:
297 case wxSYS_COLOUR_BACKGROUND
:
298 //case wxSYS_COLOUR_DESKTOP:
299 case wxSYS_COLOUR_INACTIVECAPTION
:
300 case wxSYS_COLOUR_MENU
:
301 case wxSYS_COLOUR_WINDOWFRAME
:
302 case wxSYS_COLOUR_ACTIVEBORDER
:
303 case wxSYS_COLOUR_INACTIVEBORDER
:
304 case wxSYS_COLOUR_BTNFACE
:
305 //case wxSYS_COLOUR_3DFACE:
306 case wxSYS_COLOUR_3DLIGHT
:
307 color
= wxColor(ButtonStyle()->bg
[GTK_STATE_NORMAL
]);
310 case wxSYS_COLOUR_WINDOW
:
311 color
= wxColor(TextCtrlStyle()->base
[GTK_STATE_NORMAL
]);
314 case wxSYS_COLOUR_MENUBAR
:
315 color
= wxColor(MenuBarStyle()->bg
[GTK_STATE_NORMAL
]);
318 case wxSYS_COLOUR_3DDKSHADOW
:
322 case wxSYS_COLOUR_GRAYTEXT
:
323 case wxSYS_COLOUR_BTNSHADOW
:
324 //case wxSYS_COLOUR_3DSHADOW:
326 wxColour
faceColour(GetColour(wxSYS_COLOUR_3DFACE
));
328 wxColour((unsigned char) (faceColour
.Red() * 2 / 3),
329 (unsigned char) (faceColour
.Green() * 2 / 3),
330 (unsigned char) (faceColour
.Blue() * 2 / 3));
334 case wxSYS_COLOUR_BTNHIGHLIGHT
:
335 //case wxSYS_COLOUR_BTNHILIGHT:
336 //case wxSYS_COLOUR_3DHIGHLIGHT:
337 //case wxSYS_COLOUR_3DHILIGHT:
341 case wxSYS_COLOUR_HIGHLIGHT
:
342 color
= wxColor(ButtonStyle()->bg
[GTK_STATE_SELECTED
]);
345 case wxSYS_COLOUR_LISTBOX
:
346 color
= wxColor(ListStyle()->base
[GTK_STATE_NORMAL
]);
349 case wxSYS_COLOUR_LISTBOXTEXT
:
350 color
= wxColor(ListStyle()->text
[GTK_STATE_NORMAL
]);
353 case wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT
:
354 // This is for the text in a list control (or tree) when the
355 // item is selected, but not focused
356 color
= wxColor(ListStyle()->text
[GTK_STATE_ACTIVE
]);
359 case wxSYS_COLOUR_MENUTEXT
:
360 case wxSYS_COLOUR_WINDOWTEXT
:
361 case wxSYS_COLOUR_CAPTIONTEXT
:
362 case wxSYS_COLOUR_INACTIVECAPTIONTEXT
:
363 case wxSYS_COLOUR_BTNTEXT
:
364 color
= wxColor(ButtonStyle()->fg
[GTK_STATE_NORMAL
]);
367 case wxSYS_COLOUR_INFOBK
:
368 color
= wxColor(ToolTipStyle()->bg
[GTK_STATE_NORMAL
]);
371 case wxSYS_COLOUR_INFOTEXT
:
372 color
= wxColor(ToolTipStyle()->fg
[GTK_STATE_NORMAL
]);
375 case wxSYS_COLOUR_HIGHLIGHTTEXT
:
376 color
= wxColor(ButtonStyle()->fg
[GTK_STATE_SELECTED
]);
379 case wxSYS_COLOUR_APPWORKSPACE
:
380 color
= *wxWHITE
; // ?
383 case wxSYS_COLOUR_ACTIVECAPTION
:
384 case wxSYS_COLOUR_MENUHILIGHT
:
385 color
= wxColor(MenuItemStyle()->bg
[GTK_STATE_SELECTED
]);
388 case wxSYS_COLOUR_HOTLIGHT
:
389 case wxSYS_COLOUR_GRADIENTACTIVECAPTION
:
390 case wxSYS_COLOUR_GRADIENTINACTIVECAPTION
:
395 case wxSYS_COLOUR_MAX
:
397 wxFAIL_MSG( wxT("unknown system colour index") );
402 wxASSERT(color
.IsOk());
407 wxFont
wxSystemSettingsNative::GetFont( wxSystemFont index
)
412 case wxSYS_OEM_FIXED_FONT
:
413 case wxSYS_ANSI_FIXED_FONT
:
414 case wxSYS_SYSTEM_FIXED_FONT
:
415 font
= *wxNORMAL_FONT
;
418 case wxSYS_ANSI_VAR_FONT
:
419 case wxSYS_SYSTEM_FONT
:
420 case wxSYS_DEVICE_DEFAULT_FONT
:
421 case wxSYS_DEFAULT_GUI_FONT
:
422 if (!gs_fontSystem
.IsOk())
424 wxNativeFontInfo info
;
426 GtkStyleContext
* sc
= gtk_widget_get_style_context(ButtonWidget());
427 info
.description
= const_cast<PangoFontDescription
*>(
428 gtk_style_context_get_font(sc
, GTK_STATE_FLAG_NORMAL
));
430 info
.description
= ButtonStyle()->font_desc
;
432 gs_fontSystem
= wxFont(info
);
435 // (try to) heal the default font (on some common systems e.g. Ubuntu
436 // it's "Sans Serif" but the real font is called "Sans"):
437 if (!wxFontEnumerator::IsValidFacename(gs_fontSystem
.GetFaceName()) &&
438 gs_fontSystem
.GetFaceName() == "Sans Serif")
439 gs_fontSystem
.SetFaceName("Sans");
440 #endif // wxUSE_FONTENUM
442 info
.description
= NULL
;
444 font
= gs_fontSystem
;
451 wxASSERT( font
.IsOk() );
456 // helper: return the GtkSettings either for the screen the current window is
457 // on or for the default screen if window is NULL
458 static GtkSettings
*GetSettingsForWindowScreen(GdkWindow
*window
)
460 return window
? gtk_settings_get_for_screen(gdk_window_get_screen(window
))
461 : gtk_settings_get_default();
464 static int GetBorderWidth(wxSystemMetric index
, wxWindow
* win
)
468 wxPizza
* pizza
= WX_PIZZA(win
->m_wxwindow
);
470 pizza
->get_border(border
);
475 case wxSYS_FRAMESIZE_X
:
484 int wxSystemSettingsNative::GetMetric( wxSystemMetric index
, wxWindow
* win
)
486 GdkWindow
*window
= NULL
;
488 window
= gtk_widget_get_window(win
->GetHandle());
496 case wxSYS_FRAMESIZE_X
:
497 case wxSYS_FRAMESIZE_Y
:
500 wxTopLevelWindow
*tlw
= wxDynamicCast(win
, wxTopLevelWindow
);
502 return GetBorderWidth(index
, win
);
505 // Get the frame extents from the windowmanager.
506 // In most cases the top extent is the titlebar, so we use the bottom extent
509 if (wxGetFrameExtents(window
, NULL
, &right
, NULL
, &bottom
))
515 case wxSYS_FRAMESIZE_X
:
516 return right
; // width of right extent
518 return bottom
; // height of bottom extent
524 return -1; // no window specified
528 return gdk_display_get_default_cursor_size(
529 window
? gdk_window_get_display(window
)
530 : gdk_display_get_default());
534 gint dclick_distance
;
535 g_object_get(GetSettingsForWindowScreen(window
),
536 "gtk-double-click-distance", &dclick_distance
, NULL
);
538 return dclick_distance
* 2;
540 case wxSYS_DCLICK_MSEC
:
542 g_object_get(GetSettingsForWindowScreen(window
),
543 "gtk-double-click-time", &dclick
, NULL
);
549 g_object_get(GetSettingsForWindowScreen(window
),
550 "gtk-dnd-drag-threshold", &drag_threshold
, NULL
);
552 // The correct thing here would be to double the value
553 // since that is what the API wants. But the values
554 // are much bigger under GNOME than under Windows and
555 // just seem to much in many cases to be useful.
556 // drag_threshold *= 2;
558 return drag_threshold
;
566 return gdk_screen_get_width(gdk_window_get_screen(window
));
568 return gdk_screen_width();
572 return gdk_screen_get_height(gdk_window_get_screen(window
));
574 return gdk_screen_height();
576 case wxSYS_HSCROLL_Y
:
577 case wxSYS_VSCROLL_X
:
580 case wxSYS_CAPTION_Y
:
582 // No realized window specified, and no implementation for that case yet.
585 wxASSERT_MSG( wxDynamicCast(win
, wxTopLevelWindow
),
586 wxT("Asking for caption height of a non toplevel window") );
588 // Get the height of the top windowmanager border.
589 // This is the titlebar in most cases. The titlebar might be elsewhere, and
590 // we could check which is the thickest wm border to decide on which side the
591 // titlebar is, but this might lead to interesting behaviours in used code.
592 // Reconsider when we have a way to report to the user on which side it is.
595 if (wxGetFrameExtents(window
, NULL
, NULL
, &top
, NULL
))
597 return top
; // top frame extent
601 // Try a default approach without a window pointer, if possible
606 case wxSYS_PENWINDOWS_PRESENT
:
607 // No MS Windows for Pen computing extension available in X11 based gtk+.
611 return -1; // metric is unknown
615 bool wxSystemSettingsNative::HasFeature(wxSystemFeature index
)
619 case wxSYS_CAN_ICONIZE_FRAME
:
622 case wxSYS_CAN_DRAW_FRAME_DECORATIONS
: