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/toplevel.h"
20 #include "wx/fontutil.h"
21 #include "wx/fontenum.h"
24 #include "wx/gtk/private/win_gtk.h"
25 #include "wx/gtk/private/gtk2-compat.h"
27 bool wxGetFrameExtents(GdkWindow
* window
, int* left
, int* right
, int* top
, int* bottom
);
29 // ----------------------------------------------------------------------------
30 // wxSystemSettings implementation
31 // ----------------------------------------------------------------------------
33 static wxFont gs_fontSystem
;
35 static GtkContainer
* ContainerWidget()
37 static GtkContainer
* s_widget
;
40 s_widget
= GTK_CONTAINER(gtk_fixed_new());
41 GtkWidget
* window
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
42 gtk_container_add(GTK_CONTAINER(window
), GTK_WIDGET(s_widget
));
49 static void style_updated(GtkWidget
*, void*)
51 static void style_set(GtkWidget
*, GtkStyle
*, void*)
54 gs_fontSystem
= wxNullFont
;
58 static GtkWidget
* ButtonWidget()
60 static GtkWidget
* s_widget
;
63 s_widget
= gtk_button_new();
64 gtk_container_add(ContainerWidget(), s_widget
);
66 g_signal_connect(s_widget
, "style_updated", G_CALLBACK(style_updated
), NULL
);
68 gtk_widget_ensure_style(s_widget
);
69 g_signal_connect(s_widget
, "style_set", G_CALLBACK(style_set
), NULL
);
75 static GtkWidget
* ListWidget()
77 static GtkWidget
* s_widget
;
80 s_widget
= gtk_tree_view_new_with_model(
81 GTK_TREE_MODEL(gtk_list_store_new(1, G_TYPE_INT
)));
82 gtk_container_add(ContainerWidget(), s_widget
);
84 gtk_widget_ensure_style(s_widget
);
90 static GtkWidget
* TextCtrlWidget()
92 static GtkWidget
* s_widget
;
95 s_widget
= gtk_text_view_new();
96 gtk_container_add(ContainerWidget(), s_widget
);
98 gtk_widget_ensure_style(s_widget
);
104 static GtkWidget
* MenuItemWidget()
106 static GtkWidget
* s_widget
;
107 if (s_widget
== NULL
)
109 s_widget
= gtk_menu_item_new();
110 gtk_container_add(ContainerWidget(), s_widget
);
112 gtk_widget_ensure_style(s_widget
);
118 static GtkWidget
* MenuBarWidget()
120 static GtkWidget
* s_widget
;
121 if (s_widget
== NULL
)
123 s_widget
= gtk_menu_bar_new();
124 gtk_container_add(ContainerWidget(), s_widget
);
126 gtk_widget_ensure_style(s_widget
);
132 static GtkWidget
* ToolTipWidget()
134 static GtkWidget
* s_widget
;
135 if (s_widget
== NULL
)
137 s_widget
= gtk_window_new(GTK_WINDOW_POPUP
);
138 const char* name
= "gtk-tooltip";
140 if (gtk_check_version(2, 11, 0))
141 name
= "gtk-tooltips";
143 gtk_widget_set_name(s_widget
, name
);
145 gtk_widget_ensure_style(s_widget
);
152 static void bg(GtkWidget
* widget
, GtkStateFlags state
, GdkRGBA
& gdkRGBA
)
154 GtkStyleContext
* sc
= gtk_widget_get_style_context(widget
);
155 gtk_style_context_get_background_color(sc
, state
, &gdkRGBA
);
157 static void fg(GtkWidget
* widget
, GtkStateFlags state
, GdkRGBA
& gdkRGBA
)
159 GtkStyleContext
* sc
= gtk_widget_get_style_context(widget
);
160 gtk_style_context_get_color(sc
, state
, &gdkRGBA
);
162 static void border(GtkWidget
* widget
, GtkStateFlags state
, GdkRGBA
& gdkRGBA
)
164 GtkStyleContext
* sc
= gtk_widget_get_style_context(widget
);
165 gtk_style_context_get_border_color(sc
, state
, &gdkRGBA
);
168 wxColour
wxSystemSettingsNative::GetColour(wxSystemColour index
)
170 GdkRGBA gdkRGBA
= { 0, 0, 0, 1 };
173 case wxSYS_COLOUR_3DLIGHT
:
174 case wxSYS_COLOUR_ACTIVEBORDER
:
175 case wxSYS_COLOUR_BTNFACE
:
176 case wxSYS_COLOUR_DESKTOP
:
177 case wxSYS_COLOUR_INACTIVEBORDER
:
178 case wxSYS_COLOUR_INACTIVECAPTION
:
179 case wxSYS_COLOUR_SCROLLBAR
:
180 case wxSYS_COLOUR_WINDOWFRAME
:
181 bg(ButtonWidget(), GTK_STATE_FLAG_NORMAL
, gdkRGBA
);
183 case wxSYS_COLOUR_BTNHIGHLIGHT
:
184 case wxSYS_COLOUR_HIGHLIGHT
:
185 bg(ButtonWidget(), GTK_STATE_FLAG_SELECTED
, gdkRGBA
);
187 case wxSYS_COLOUR_BTNSHADOW
:
188 border(ButtonWidget(), GTK_STATE_FLAG_NORMAL
, gdkRGBA
);
190 case wxSYS_COLOUR_BTNTEXT
:
191 case wxSYS_COLOUR_WINDOWTEXT
:
192 fg(ButtonWidget(), GTK_STATE_FLAG_NORMAL
, gdkRGBA
);
194 case wxSYS_COLOUR_GRAYTEXT
:
195 case wxSYS_COLOUR_INACTIVECAPTIONTEXT
:
196 fg(ButtonWidget(), GTK_STATE_FLAG_INSENSITIVE
, gdkRGBA
);
198 case wxSYS_COLOUR_HIGHLIGHTTEXT
:
199 fg(ButtonWidget(), GTK_STATE_FLAG_SELECTED
, gdkRGBA
);
201 case wxSYS_COLOUR_HOTLIGHT
:
203 static GtkWidget
* s_widget
;
204 if (s_widget
== NULL
)
206 s_widget
= gtk_link_button_new("");
207 gtk_container_add(ContainerWidget(), s_widget
);
209 fg(s_widget
, GTK_STATE_FLAG_NORMAL
, gdkRGBA
);
212 case wxSYS_COLOUR_INFOBK
:
213 bg(ToolTipWidget(), GTK_STATE_FLAG_NORMAL
, gdkRGBA
);
215 case wxSYS_COLOUR_INFOTEXT
:
216 fg(ToolTipWidget(), GTK_STATE_FLAG_NORMAL
, gdkRGBA
);
218 case wxSYS_COLOUR_LISTBOX
:
219 bg(ListWidget(), GTK_STATE_FLAG_NORMAL
, gdkRGBA
);
221 case wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT
:
222 fg(ListWidget(), GTK_STATE_FLAG_SELECTED
, gdkRGBA
);
224 case wxSYS_COLOUR_LISTBOXTEXT
:
225 fg(ListWidget(), GTK_STATE_FLAG_NORMAL
, gdkRGBA
);
227 case wxSYS_COLOUR_MENU
:
228 bg(MenuItemWidget(), GTK_STATE_FLAG_NORMAL
, gdkRGBA
);
230 case wxSYS_COLOUR_MENUBAR
:
231 bg(MenuBarWidget(), GTK_STATE_FLAG_NORMAL
, gdkRGBA
);
233 case wxSYS_COLOUR_ACTIVECAPTION
:
234 case wxSYS_COLOUR_MENUHILIGHT
:
235 bg(MenuItemWidget(), GTK_STATE_FLAG_SELECTED
, gdkRGBA
);
237 case wxSYS_COLOUR_MENUTEXT
:
238 fg(MenuItemWidget(), GTK_STATE_FLAG_NORMAL
, gdkRGBA
);
240 case wxSYS_COLOUR_APPWORKSPACE
:
241 case wxSYS_COLOUR_WINDOW
:
242 bg(TextCtrlWidget(), GTK_STATE_FLAG_NORMAL
, gdkRGBA
);
244 case wxSYS_COLOUR_CAPTIONTEXT
:
246 GdkRGBA c
= { 1, 1, 1, 1 };
251 wxFAIL_MSG("unknown system colour index");
253 case wxSYS_COLOUR_3DDKSHADOW
:
254 case wxSYS_COLOUR_GRADIENTACTIVECAPTION
:
255 case wxSYS_COLOUR_GRADIENTINACTIVECAPTION
:
259 return wxColour(gdkRGBA
);
262 static const GtkStyle
* ButtonStyle()
264 return gtk_widget_get_style(ButtonWidget());
267 static const GtkStyle
* ListStyle()
269 return gtk_widget_get_style(ListWidget());
272 static const GtkStyle
* TextCtrlStyle()
274 return gtk_widget_get_style(TextCtrlWidget());
277 static const GtkStyle
* MenuItemStyle()
279 return gtk_widget_get_style(MenuItemWidget());
282 static const GtkStyle
* MenuBarStyle()
284 return gtk_widget_get_style(MenuBarWidget());
287 static const GtkStyle
* ToolTipStyle()
289 return gtk_widget_get_style(ToolTipWidget());
292 wxColour
wxSystemSettingsNative::GetColour( wxSystemColour index
)
297 case wxSYS_COLOUR_SCROLLBAR
:
298 case wxSYS_COLOUR_BACKGROUND
:
299 //case wxSYS_COLOUR_DESKTOP:
300 case wxSYS_COLOUR_INACTIVECAPTION
:
301 case wxSYS_COLOUR_MENU
:
302 case wxSYS_COLOUR_WINDOWFRAME
:
303 case wxSYS_COLOUR_ACTIVEBORDER
:
304 case wxSYS_COLOUR_INACTIVEBORDER
:
305 case wxSYS_COLOUR_BTNFACE
:
306 //case wxSYS_COLOUR_3DFACE:
307 case wxSYS_COLOUR_3DLIGHT
:
308 color
= wxColor(ButtonStyle()->bg
[GTK_STATE_NORMAL
]);
311 case wxSYS_COLOUR_WINDOW
:
312 color
= wxColor(TextCtrlStyle()->base
[GTK_STATE_NORMAL
]);
315 case wxSYS_COLOUR_MENUBAR
:
316 color
= wxColor(MenuBarStyle()->bg
[GTK_STATE_NORMAL
]);
319 case wxSYS_COLOUR_3DDKSHADOW
:
323 case wxSYS_COLOUR_GRAYTEXT
:
324 case wxSYS_COLOUR_BTNSHADOW
:
325 //case wxSYS_COLOUR_3DSHADOW:
327 wxColour
faceColour(GetColour(wxSYS_COLOUR_3DFACE
));
329 wxColour((unsigned char) (faceColour
.Red() * 2 / 3),
330 (unsigned char) (faceColour
.Green() * 2 / 3),
331 (unsigned char) (faceColour
.Blue() * 2 / 3));
335 case wxSYS_COLOUR_BTNHIGHLIGHT
:
336 //case wxSYS_COLOUR_BTNHILIGHT:
337 //case wxSYS_COLOUR_3DHIGHLIGHT:
338 //case wxSYS_COLOUR_3DHILIGHT:
342 case wxSYS_COLOUR_HIGHLIGHT
:
343 color
= wxColor(ButtonStyle()->bg
[GTK_STATE_SELECTED
]);
346 case wxSYS_COLOUR_LISTBOX
:
347 color
= wxColor(ListStyle()->base
[GTK_STATE_NORMAL
]);
350 case wxSYS_COLOUR_LISTBOXTEXT
:
351 color
= wxColor(ListStyle()->text
[GTK_STATE_NORMAL
]);
354 case wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT
:
355 // This is for the text in a list control (or tree) when the
356 // item is selected, but not focused
357 color
= wxColor(ListStyle()->text
[GTK_STATE_ACTIVE
]);
360 case wxSYS_COLOUR_MENUTEXT
:
361 case wxSYS_COLOUR_WINDOWTEXT
:
362 case wxSYS_COLOUR_CAPTIONTEXT
:
363 case wxSYS_COLOUR_INACTIVECAPTIONTEXT
:
364 case wxSYS_COLOUR_BTNTEXT
:
365 color
= wxColor(ButtonStyle()->fg
[GTK_STATE_NORMAL
]);
368 case wxSYS_COLOUR_INFOBK
:
369 color
= wxColor(ToolTipStyle()->bg
[GTK_STATE_NORMAL
]);
372 case wxSYS_COLOUR_INFOTEXT
:
373 color
= wxColor(ToolTipStyle()->fg
[GTK_STATE_NORMAL
]);
376 case wxSYS_COLOUR_HIGHLIGHTTEXT
:
377 color
= wxColor(ButtonStyle()->fg
[GTK_STATE_SELECTED
]);
380 case wxSYS_COLOUR_APPWORKSPACE
:
381 color
= *wxWHITE
; // ?
384 case wxSYS_COLOUR_ACTIVECAPTION
:
385 case wxSYS_COLOUR_MENUHILIGHT
:
386 color
= wxColor(MenuItemStyle()->bg
[GTK_STATE_SELECTED
]);
389 case wxSYS_COLOUR_HOTLIGHT
:
390 case wxSYS_COLOUR_GRADIENTACTIVECAPTION
:
391 case wxSYS_COLOUR_GRADIENTINACTIVECAPTION
:
396 case wxSYS_COLOUR_MAX
:
398 wxFAIL_MSG( wxT("unknown system colour index") );
403 wxASSERT(color
.IsOk());
408 wxFont
wxSystemSettingsNative::GetFont( wxSystemFont index
)
413 case wxSYS_OEM_FIXED_FONT
:
414 case wxSYS_ANSI_FIXED_FONT
:
415 case wxSYS_SYSTEM_FIXED_FONT
:
416 font
= *wxNORMAL_FONT
;
419 case wxSYS_ANSI_VAR_FONT
:
420 case wxSYS_SYSTEM_FONT
:
421 case wxSYS_DEVICE_DEFAULT_FONT
:
422 case wxSYS_DEFAULT_GUI_FONT
:
423 if (!gs_fontSystem
.IsOk())
425 wxNativeFontInfo info
;
427 GtkStyleContext
* sc
= gtk_widget_get_style_context(ButtonWidget());
428 info
.description
= const_cast<PangoFontDescription
*>(
429 gtk_style_context_get_font(sc
, GTK_STATE_FLAG_NORMAL
));
431 info
.description
= ButtonStyle()->font_desc
;
433 gs_fontSystem
= wxFont(info
);
436 // (try to) heal the default font (on some common systems e.g. Ubuntu
437 // it's "Sans Serif" but the real font is called "Sans"):
438 if (!wxFontEnumerator::IsValidFacename(gs_fontSystem
.GetFaceName()) &&
439 gs_fontSystem
.GetFaceName() == "Sans Serif")
440 gs_fontSystem
.SetFaceName("Sans");
441 #endif // wxUSE_FONTENUM
443 info
.description
= NULL
;
445 font
= gs_fontSystem
;
452 wxASSERT( font
.IsOk() );
457 // helper: return the GtkSettings either for the screen the current window is
458 // on or for the default screen if window is NULL
459 static GtkSettings
*GetSettingsForWindowScreen(GdkWindow
*window
)
461 return window
? gtk_settings_get_for_screen(gdk_window_get_screen(window
))
462 : gtk_settings_get_default();
465 static int GetBorderWidth(wxSystemMetric index
, wxWindow
* win
)
469 wxPizza
* pizza
= WX_PIZZA(win
->m_wxwindow
);
471 pizza
->get_border(border
);
476 case wxSYS_FRAMESIZE_X
:
485 int wxSystemSettingsNative::GetMetric( wxSystemMetric index
, wxWindow
* win
)
487 GdkWindow
*window
= NULL
;
489 window
= gtk_widget_get_window(win
->GetHandle());
497 case wxSYS_FRAMESIZE_X
:
498 case wxSYS_FRAMESIZE_Y
:
501 wxTopLevelWindow
*tlw
= wxDynamicCast(win
, wxTopLevelWindow
);
503 return GetBorderWidth(index
, win
);
506 // Get the frame extents from the windowmanager.
507 // In most cases the top extent is the titlebar, so we use the bottom extent
510 if (wxGetFrameExtents(window
, NULL
, &right
, NULL
, &bottom
))
516 case wxSYS_FRAMESIZE_X
:
517 return right
; // width of right extent
519 return bottom
; // height of bottom extent
525 return -1; // no window specified
529 return gdk_display_get_default_cursor_size(
530 window
? gdk_window_get_display(window
)
531 : gdk_display_get_default());
535 gint dclick_distance
;
536 g_object_get(GetSettingsForWindowScreen(window
),
537 "gtk-double-click-distance", &dclick_distance
, NULL
);
539 return dclick_distance
* 2;
541 case wxSYS_DCLICK_MSEC
:
543 g_object_get(GetSettingsForWindowScreen(window
),
544 "gtk-double-click-time", &dclick
, NULL
);
550 g_object_get(GetSettingsForWindowScreen(window
),
551 "gtk-dnd-drag-threshold", &drag_threshold
, NULL
);
553 // The correct thing here would be to double the value
554 // since that is what the API wants. But the values
555 // are much bigger under GNOME than under Windows and
556 // just seem to much in many cases to be useful.
557 // drag_threshold *= 2;
559 return drag_threshold
;
567 return gdk_screen_get_width(gdk_window_get_screen(window
));
569 return gdk_screen_width();
573 return gdk_screen_get_height(gdk_window_get_screen(window
));
575 return gdk_screen_height();
577 case wxSYS_HSCROLL_Y
:
578 case wxSYS_VSCROLL_X
:
581 case wxSYS_CAPTION_Y
:
583 // No realized window specified, and no implementation for that case yet.
586 wxASSERT_MSG( wxDynamicCast(win
, wxTopLevelWindow
),
587 wxT("Asking for caption height of a non toplevel window") );
589 // Get the height of the top windowmanager border.
590 // This is the titlebar in most cases. The titlebar might be elsewhere, and
591 // we could check which is the thickest wm border to decide on which side the
592 // titlebar is, but this might lead to interesting behaviours in used code.
593 // Reconsider when we have a way to report to the user on which side it is.
596 if (wxGetFrameExtents(window
, NULL
, NULL
, &top
, NULL
))
598 return top
; // top frame extent
602 // Try a default approach without a window pointer, if possible
607 case wxSYS_PENWINDOWS_PRESENT
:
608 // No MS Windows for Pen computing extension available in X11 based gtk+.
612 return -1; // metric is unknown
616 bool wxSystemSettingsNative::HasFeature(wxSystemFeature index
)
620 case wxSYS_CAN_ICONIZE_FRAME
:
623 case wxSYS_CAN_DRAW_FRAME_DECORATIONS
: