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
);
42 // need this to initialize style for window
43 gtk_widget_get_style_context(GTK_WIDGET(window
));
45 gtk_container_add(GTK_CONTAINER(window
), GTK_WIDGET(s_widget
));
52 static void style_updated(GtkWidget
*, void*)
54 static void style_set(GtkWidget
*, GtkStyle
*, void*)
57 gs_fontSystem
= wxNullFont
;
61 static GtkWidget
* ButtonWidget()
63 static GtkWidget
* s_widget
;
66 s_widget
= gtk_button_new();
67 gtk_container_add(ContainerWidget(), s_widget
);
69 g_signal_connect(s_widget
, "style_updated", G_CALLBACK(style_updated
), NULL
);
71 gtk_widget_ensure_style(s_widget
);
72 g_signal_connect(s_widget
, "style_set", G_CALLBACK(style_set
), NULL
);
78 static GtkWidget
* ListWidget()
80 static GtkWidget
* s_widget
;
83 s_widget
= gtk_tree_view_new_with_model(
84 GTK_TREE_MODEL(gtk_list_store_new(1, G_TYPE_INT
)));
85 gtk_container_add(ContainerWidget(), s_widget
);
87 gtk_widget_ensure_style(s_widget
);
93 static GtkWidget
* TextCtrlWidget()
95 static GtkWidget
* s_widget
;
98 s_widget
= gtk_text_view_new();
99 gtk_container_add(ContainerWidget(), s_widget
);
101 gtk_widget_ensure_style(s_widget
);
107 static GtkWidget
* MenuItemWidget()
109 static GtkWidget
* s_widget
;
110 if (s_widget
== NULL
)
112 s_widget
= gtk_menu_item_new();
113 gtk_container_add(ContainerWidget(), s_widget
);
115 gtk_widget_ensure_style(s_widget
);
121 static GtkWidget
* MenuBarWidget()
123 static GtkWidget
* s_widget
;
124 if (s_widget
== NULL
)
126 s_widget
= gtk_menu_bar_new();
127 gtk_container_add(ContainerWidget(), s_widget
);
129 gtk_widget_ensure_style(s_widget
);
135 static GtkWidget
* ToolTipWidget()
137 static GtkWidget
* s_widget
;
138 if (s_widget
== NULL
)
140 s_widget
= gtk_window_new(GTK_WINDOW_POPUP
);
141 const char* name
= "gtk-tooltip";
143 if (gtk_check_version(2, 11, 0))
144 name
= "gtk-tooltips";
146 gtk_widget_set_name(s_widget
, name
);
148 gtk_widget_ensure_style(s_widget
);
155 static void bg(GtkWidget
* widget
, GtkStateFlags state
, GdkRGBA
& gdkRGBA
)
157 GtkStyleContext
* sc
= gtk_widget_get_style_context(widget
);
158 gtk_style_context_get_background_color(sc
, state
, &gdkRGBA
);
160 static void fg(GtkWidget
* widget
, GtkStateFlags state
, GdkRGBA
& gdkRGBA
)
162 GtkStyleContext
* sc
= gtk_widget_get_style_context(widget
);
163 gtk_style_context_get_color(sc
, state
, &gdkRGBA
);
165 static void border(GtkWidget
* widget
, GtkStateFlags state
, GdkRGBA
& gdkRGBA
)
167 GtkStyleContext
* sc
= gtk_widget_get_style_context(widget
);
168 gtk_style_context_get_border_color(sc
, state
, &gdkRGBA
);
171 wxColour
wxSystemSettingsNative::GetColour(wxSystemColour index
)
173 GdkRGBA gdkRGBA
= { 0, 0, 0, 1 };
176 case wxSYS_COLOUR_3DLIGHT
:
177 case wxSYS_COLOUR_ACTIVEBORDER
:
178 case wxSYS_COLOUR_BTNFACE
:
179 case wxSYS_COLOUR_DESKTOP
:
180 case wxSYS_COLOUR_INACTIVEBORDER
:
181 case wxSYS_COLOUR_INACTIVECAPTION
:
182 case wxSYS_COLOUR_SCROLLBAR
:
183 case wxSYS_COLOUR_WINDOWFRAME
:
184 bg(ButtonWidget(), GTK_STATE_FLAG_NORMAL
, gdkRGBA
);
186 case wxSYS_COLOUR_BTNHIGHLIGHT
:
187 case wxSYS_COLOUR_HIGHLIGHT
:
188 bg(ButtonWidget(), GTK_STATE_FLAG_SELECTED
, gdkRGBA
);
190 case wxSYS_COLOUR_BTNSHADOW
:
191 border(ButtonWidget(), GTK_STATE_FLAG_NORMAL
, gdkRGBA
);
193 case wxSYS_COLOUR_BTNTEXT
:
194 case wxSYS_COLOUR_WINDOWTEXT
:
195 fg(ButtonWidget(), GTK_STATE_FLAG_NORMAL
, gdkRGBA
);
197 case wxSYS_COLOUR_GRAYTEXT
:
198 case wxSYS_COLOUR_INACTIVECAPTIONTEXT
:
199 fg(ButtonWidget(), GTK_STATE_FLAG_INSENSITIVE
, gdkRGBA
);
201 case wxSYS_COLOUR_HIGHLIGHTTEXT
:
202 fg(ButtonWidget(), GTK_STATE_FLAG_SELECTED
, gdkRGBA
);
204 case wxSYS_COLOUR_HOTLIGHT
:
206 static GtkWidget
* s_widget
;
207 if (s_widget
== NULL
)
209 s_widget
= gtk_link_button_new("");
210 gtk_container_add(ContainerWidget(), s_widget
);
212 fg(s_widget
, GTK_STATE_FLAG_NORMAL
, gdkRGBA
);
215 case wxSYS_COLOUR_INFOBK
:
216 bg(ToolTipWidget(), GTK_STATE_FLAG_NORMAL
, gdkRGBA
);
218 case wxSYS_COLOUR_INFOTEXT
:
219 fg(ToolTipWidget(), GTK_STATE_FLAG_NORMAL
, gdkRGBA
);
221 case wxSYS_COLOUR_LISTBOX
:
222 bg(ListWidget(), GTK_STATE_FLAG_NORMAL
, gdkRGBA
);
224 case wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT
:
225 fg(ListWidget(), GTK_STATE_FLAG_SELECTED
, gdkRGBA
);
227 case wxSYS_COLOUR_LISTBOXTEXT
:
228 fg(ListWidget(), GTK_STATE_FLAG_NORMAL
, gdkRGBA
);
230 case wxSYS_COLOUR_MENU
:
231 bg(MenuItemWidget(), GTK_STATE_FLAG_NORMAL
, gdkRGBA
);
233 case wxSYS_COLOUR_MENUBAR
:
234 bg(MenuBarWidget(), GTK_STATE_FLAG_NORMAL
, gdkRGBA
);
236 case wxSYS_COLOUR_ACTIVECAPTION
:
237 case wxSYS_COLOUR_MENUHILIGHT
:
238 bg(MenuItemWidget(), GTK_STATE_FLAG_SELECTED
, gdkRGBA
);
240 case wxSYS_COLOUR_MENUTEXT
:
241 fg(MenuItemWidget(), GTK_STATE_FLAG_NORMAL
, gdkRGBA
);
243 case wxSYS_COLOUR_APPWORKSPACE
:
244 case wxSYS_COLOUR_WINDOW
:
245 bg(TextCtrlWidget(), GTK_STATE_FLAG_NORMAL
, gdkRGBA
);
247 case wxSYS_COLOUR_CAPTIONTEXT
:
249 GdkRGBA c
= { 1, 1, 1, 1 };
254 wxFAIL_MSG("unknown system colour index");
256 case wxSYS_COLOUR_3DDKSHADOW
:
257 case wxSYS_COLOUR_GRADIENTACTIVECAPTION
:
258 case wxSYS_COLOUR_GRADIENTINACTIVECAPTION
:
262 return wxColour(gdkRGBA
);
265 static const GtkStyle
* ButtonStyle()
267 return gtk_widget_get_style(ButtonWidget());
270 static const GtkStyle
* ListStyle()
272 return gtk_widget_get_style(ListWidget());
275 static const GtkStyle
* TextCtrlStyle()
277 return gtk_widget_get_style(TextCtrlWidget());
280 static const GtkStyle
* MenuItemStyle()
282 return gtk_widget_get_style(MenuItemWidget());
285 static const GtkStyle
* MenuBarStyle()
287 return gtk_widget_get_style(MenuBarWidget());
290 static const GtkStyle
* ToolTipStyle()
292 return gtk_widget_get_style(ToolTipWidget());
295 wxColour
wxSystemSettingsNative::GetColour( wxSystemColour index
)
300 case wxSYS_COLOUR_SCROLLBAR
:
301 case wxSYS_COLOUR_BACKGROUND
:
302 //case wxSYS_COLOUR_DESKTOP:
303 case wxSYS_COLOUR_INACTIVECAPTION
:
304 case wxSYS_COLOUR_MENU
:
305 case wxSYS_COLOUR_WINDOWFRAME
:
306 case wxSYS_COLOUR_ACTIVEBORDER
:
307 case wxSYS_COLOUR_INACTIVEBORDER
:
308 case wxSYS_COLOUR_BTNFACE
:
309 //case wxSYS_COLOUR_3DFACE:
310 case wxSYS_COLOUR_3DLIGHT
:
311 color
= wxColor(ButtonStyle()->bg
[GTK_STATE_NORMAL
]);
314 case wxSYS_COLOUR_WINDOW
:
315 color
= wxColor(TextCtrlStyle()->base
[GTK_STATE_NORMAL
]);
318 case wxSYS_COLOUR_MENUBAR
:
319 color
= wxColor(MenuBarStyle()->bg
[GTK_STATE_NORMAL
]);
322 case wxSYS_COLOUR_3DDKSHADOW
:
326 case wxSYS_COLOUR_GRAYTEXT
:
327 case wxSYS_COLOUR_BTNSHADOW
:
328 //case wxSYS_COLOUR_3DSHADOW:
330 wxColour
faceColour(GetColour(wxSYS_COLOUR_3DFACE
));
332 wxColour((unsigned char) (faceColour
.Red() * 2 / 3),
333 (unsigned char) (faceColour
.Green() * 2 / 3),
334 (unsigned char) (faceColour
.Blue() * 2 / 3));
338 case wxSYS_COLOUR_BTNHIGHLIGHT
:
339 //case wxSYS_COLOUR_BTNHILIGHT:
340 //case wxSYS_COLOUR_3DHIGHLIGHT:
341 //case wxSYS_COLOUR_3DHILIGHT:
345 case wxSYS_COLOUR_HIGHLIGHT
:
346 color
= wxColor(ButtonStyle()->bg
[GTK_STATE_SELECTED
]);
349 case wxSYS_COLOUR_LISTBOX
:
350 color
= wxColor(ListStyle()->base
[GTK_STATE_NORMAL
]);
353 case wxSYS_COLOUR_LISTBOXTEXT
:
354 color
= wxColor(ListStyle()->text
[GTK_STATE_NORMAL
]);
357 case wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT
:
358 // This is for the text in a list control (or tree) when the
359 // item is selected, but not focused
360 color
= wxColor(ListStyle()->text
[GTK_STATE_ACTIVE
]);
363 case wxSYS_COLOUR_MENUTEXT
:
364 case wxSYS_COLOUR_WINDOWTEXT
:
365 case wxSYS_COLOUR_CAPTIONTEXT
:
366 case wxSYS_COLOUR_INACTIVECAPTIONTEXT
:
367 case wxSYS_COLOUR_BTNTEXT
:
368 color
= wxColor(ButtonStyle()->fg
[GTK_STATE_NORMAL
]);
371 case wxSYS_COLOUR_INFOBK
:
372 color
= wxColor(ToolTipStyle()->bg
[GTK_STATE_NORMAL
]);
375 case wxSYS_COLOUR_INFOTEXT
:
376 color
= wxColor(ToolTipStyle()->fg
[GTK_STATE_NORMAL
]);
379 case wxSYS_COLOUR_HIGHLIGHTTEXT
:
380 color
= wxColor(ButtonStyle()->fg
[GTK_STATE_SELECTED
]);
383 case wxSYS_COLOUR_APPWORKSPACE
:
384 color
= *wxWHITE
; // ?
387 case wxSYS_COLOUR_ACTIVECAPTION
:
388 case wxSYS_COLOUR_MENUHILIGHT
:
389 color
= wxColor(MenuItemStyle()->bg
[GTK_STATE_SELECTED
]);
392 case wxSYS_COLOUR_HOTLIGHT
:
393 case wxSYS_COLOUR_GRADIENTACTIVECAPTION
:
394 case wxSYS_COLOUR_GRADIENTINACTIVECAPTION
:
399 case wxSYS_COLOUR_MAX
:
401 wxFAIL_MSG( wxT("unknown system colour index") );
406 wxASSERT(color
.IsOk());
411 wxFont
wxSystemSettingsNative::GetFont( wxSystemFont index
)
416 case wxSYS_OEM_FIXED_FONT
:
417 case wxSYS_ANSI_FIXED_FONT
:
418 case wxSYS_SYSTEM_FIXED_FONT
:
419 font
= *wxNORMAL_FONT
;
422 case wxSYS_ANSI_VAR_FONT
:
423 case wxSYS_SYSTEM_FONT
:
424 case wxSYS_DEVICE_DEFAULT_FONT
:
425 case wxSYS_DEFAULT_GUI_FONT
:
426 if (!gs_fontSystem
.IsOk())
428 wxNativeFontInfo info
;
430 GtkStyleContext
* sc
= gtk_widget_get_style_context(ButtonWidget());
431 info
.description
= const_cast<PangoFontDescription
*>(
432 gtk_style_context_get_font(sc
, GTK_STATE_FLAG_NORMAL
));
434 info
.description
= ButtonStyle()->font_desc
;
436 gs_fontSystem
= wxFont(info
);
439 // (try to) heal the default font (on some common systems e.g. Ubuntu
440 // it's "Sans Serif" but the real font is called "Sans"):
441 if (!wxFontEnumerator::IsValidFacename(gs_fontSystem
.GetFaceName()) &&
442 gs_fontSystem
.GetFaceName() == "Sans Serif")
443 gs_fontSystem
.SetFaceName("Sans");
444 #endif // wxUSE_FONTENUM
446 info
.description
= NULL
;
448 font
= gs_fontSystem
;
455 wxASSERT( font
.IsOk() );
460 // helper: return the GtkSettings either for the screen the current window is
461 // on or for the default screen if window is NULL
462 static GtkSettings
*GetSettingsForWindowScreen(GdkWindow
*window
)
464 return window
? gtk_settings_get_for_screen(gdk_window_get_screen(window
))
465 : gtk_settings_get_default();
468 static int GetBorderWidth(wxSystemMetric index
, wxWindow
* win
)
472 wxPizza
* pizza
= WX_PIZZA(win
->m_wxwindow
);
474 pizza
->get_border(border
);
479 case wxSYS_FRAMESIZE_X
:
488 int wxSystemSettingsNative::GetMetric( wxSystemMetric index
, wxWindow
* win
)
490 GdkWindow
*window
= NULL
;
492 window
= gtk_widget_get_window(win
->GetHandle());
500 case wxSYS_FRAMESIZE_X
:
501 case wxSYS_FRAMESIZE_Y
:
504 wxTopLevelWindow
*tlw
= wxDynamicCast(win
, wxTopLevelWindow
);
506 return GetBorderWidth(index
, win
);
509 // Get the frame extents from the windowmanager.
510 // In most cases the top extent is the titlebar, so we use the bottom extent
513 if (wxGetFrameExtents(window
, NULL
, &right
, NULL
, &bottom
))
519 case wxSYS_FRAMESIZE_X
:
520 return right
; // width of right extent
522 return bottom
; // height of bottom extent
528 return -1; // no window specified
532 return gdk_display_get_default_cursor_size(
533 window
? gdk_window_get_display(window
)
534 : gdk_display_get_default());
538 gint dclick_distance
;
539 g_object_get(GetSettingsForWindowScreen(window
),
540 "gtk-double-click-distance", &dclick_distance
, NULL
);
542 return dclick_distance
* 2;
544 case wxSYS_DCLICK_MSEC
:
546 g_object_get(GetSettingsForWindowScreen(window
),
547 "gtk-double-click-time", &dclick
, NULL
);
553 g_object_get(GetSettingsForWindowScreen(window
),
554 "gtk-dnd-drag-threshold", &drag_threshold
, NULL
);
556 // The correct thing here would be to double the value
557 // since that is what the API wants. But the values
558 // are much bigger under GNOME than under Windows and
559 // just seem to much in many cases to be useful.
560 // drag_threshold *= 2;
562 return drag_threshold
;
570 return gdk_screen_get_width(gdk_window_get_screen(window
));
572 return gdk_screen_width();
576 return gdk_screen_get_height(gdk_window_get_screen(window
));
578 return gdk_screen_height();
580 case wxSYS_HSCROLL_Y
:
581 case wxSYS_VSCROLL_X
:
584 case wxSYS_CAPTION_Y
:
586 // No realized window specified, and no implementation for that case yet.
589 wxASSERT_MSG( wxDynamicCast(win
, wxTopLevelWindow
),
590 wxT("Asking for caption height of a non toplevel window") );
592 // Get the height of the top windowmanager border.
593 // This is the titlebar in most cases. The titlebar might be elsewhere, and
594 // we could check which is the thickest wm border to decide on which side the
595 // titlebar is, but this might lead to interesting behaviours in used code.
596 // Reconsider when we have a way to report to the user on which side it is.
599 if (wxGetFrameExtents(window
, NULL
, NULL
, &top
, NULL
))
601 return top
; // top frame extent
605 // Try a default approach without a window pointer, if possible
610 case wxSYS_PENWINDOWS_PRESENT
:
611 // No MS Windows for Pen computing extension available in X11 based gtk+.
615 return -1; // metric is unknown
619 bool wxSystemSettingsNative::HasFeature(wxSystemFeature index
)
623 case wxSYS_CAN_ICONIZE_FRAME
:
626 case wxSYS_CAN_DRAW_FRAME_DECORATIONS
: