]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/settings.cpp
Fix wxHtmlHelpData::SetTempDir() to behave correctly without trailing slash.
[wxWidgets.git] / src / gtk / settings.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk/settings.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Modified by: Mart Raudsepp (GetMetric)
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10 // For compilers that support precompilation, includes "wx.h".
11 #include "wx/wxprec.h"
12
13 #include "wx/settings.h"
14
15 #ifndef WX_PRECOMP
16 #include "wx/toplevel.h"
17 #endif
18
19 #include "wx/fontutil.h"
20 #include "wx/fontenum.h"
21
22 #include <gtk/gtk.h>
23 #include "wx/gtk/private/win_gtk.h"
24 #include "wx/gtk/private/gtk2-compat.h"
25
26 bool wxGetFrameExtents(GdkWindow* window, int* left, int* right, int* top, int* bottom);
27
28 // ----------------------------------------------------------------------------
29 // wxSystemSettings implementation
30 // ----------------------------------------------------------------------------
31
32 static wxFont gs_fontSystem;
33
34 static GtkContainer* ContainerWidget()
35 {
36 static GtkContainer* s_widget;
37 if (s_widget == NULL)
38 {
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));
42 }
43 return s_widget;
44 }
45
46 extern "C" {
47 #ifdef __WXGTK3__
48 static void style_updated(GtkWidget*, void*)
49 #else
50 static void style_set(GtkWidget*, GtkStyle*, void*)
51 #endif
52 {
53 gs_fontSystem = wxNullFont;
54 }
55 }
56
57 static GtkWidget* ButtonWidget()
58 {
59 static GtkWidget* s_widget;
60 if (s_widget == NULL)
61 {
62 s_widget = gtk_button_new();
63 gtk_container_add(ContainerWidget(), s_widget);
64 #ifdef __WXGTK3__
65 g_signal_connect(s_widget, "style_updated", G_CALLBACK(style_updated), NULL);
66 #else
67 gtk_widget_ensure_style(s_widget);
68 g_signal_connect(s_widget, "style_set", G_CALLBACK(style_set), NULL);
69 #endif
70 }
71 return s_widget;
72 }
73
74 static GtkWidget* ListWidget()
75 {
76 static GtkWidget* s_widget;
77 if (s_widget == NULL)
78 {
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);
82 #ifndef __WXGTK3__
83 gtk_widget_ensure_style(s_widget);
84 #endif
85 }
86 return s_widget;
87 }
88
89 static GtkWidget* TextCtrlWidget()
90 {
91 static GtkWidget* s_widget;
92 if (s_widget == NULL)
93 {
94 s_widget = gtk_text_view_new();
95 gtk_container_add(ContainerWidget(), s_widget);
96 #ifndef __WXGTK3__
97 gtk_widget_ensure_style(s_widget);
98 #endif
99 }
100 return s_widget;
101 }
102
103 static GtkWidget* MenuItemWidget()
104 {
105 static GtkWidget* s_widget;
106 if (s_widget == NULL)
107 {
108 s_widget = gtk_menu_item_new();
109 gtk_container_add(ContainerWidget(), s_widget);
110 #ifndef __WXGTK3__
111 gtk_widget_ensure_style(s_widget);
112 #endif
113 }
114 return s_widget;
115 }
116
117 static GtkWidget* MenuBarWidget()
118 {
119 static GtkWidget* s_widget;
120 if (s_widget == NULL)
121 {
122 s_widget = gtk_menu_bar_new();
123 gtk_container_add(ContainerWidget(), s_widget);
124 #ifndef __WXGTK3__
125 gtk_widget_ensure_style(s_widget);
126 #endif
127 }
128 return s_widget;
129 }
130
131 static GtkWidget* ToolTipWidget()
132 {
133 static GtkWidget* s_widget;
134 if (s_widget == NULL)
135 {
136 s_widget = gtk_window_new(GTK_WINDOW_POPUP);
137 const char* name = "gtk-tooltip";
138 #ifndef __WXGTK3__
139 if (gtk_check_version(2, 11, 0))
140 name = "gtk-tooltips";
141 #endif
142 gtk_widget_set_name(s_widget, name);
143 #ifndef __WXGTK3__
144 gtk_widget_ensure_style(s_widget);
145 #endif
146 }
147 return s_widget;
148 }
149
150 #ifdef __WXGTK3__
151 static void bg(GtkWidget* widget, GtkStateFlags state, GdkRGBA& gdkRGBA)
152 {
153 GtkStyleContext* sc = gtk_widget_get_style_context(widget);
154 gtk_style_context_get_background_color(sc, state, &gdkRGBA);
155 }
156 static void fg(GtkWidget* widget, GtkStateFlags state, GdkRGBA& gdkRGBA)
157 {
158 GtkStyleContext* sc = gtk_widget_get_style_context(widget);
159 gtk_style_context_get_color(sc, state, &gdkRGBA);
160 }
161 static void border(GtkWidget* widget, GtkStateFlags state, GdkRGBA& gdkRGBA)
162 {
163 GtkStyleContext* sc = gtk_widget_get_style_context(widget);
164 gtk_style_context_get_border_color(sc, state, &gdkRGBA);
165 }
166
167 wxColour wxSystemSettingsNative::GetColour(wxSystemColour index)
168 {
169 GdkRGBA gdkRGBA = { 0, 0, 0, 1 };
170 switch (index)
171 {
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);
181 break;
182 case wxSYS_COLOUR_BTNHIGHLIGHT:
183 case wxSYS_COLOUR_HIGHLIGHT:
184 bg(ButtonWidget(), GTK_STATE_FLAG_SELECTED, gdkRGBA);
185 break;
186 case wxSYS_COLOUR_BTNSHADOW:
187 border(ButtonWidget(), GTK_STATE_FLAG_NORMAL, gdkRGBA);
188 break;
189 case wxSYS_COLOUR_BTNTEXT:
190 case wxSYS_COLOUR_WINDOWTEXT:
191 fg(ButtonWidget(), GTK_STATE_FLAG_NORMAL, gdkRGBA);
192 break;
193 case wxSYS_COLOUR_GRAYTEXT:
194 case wxSYS_COLOUR_INACTIVECAPTIONTEXT:
195 fg(ButtonWidget(), GTK_STATE_FLAG_INSENSITIVE, gdkRGBA);
196 break;
197 case wxSYS_COLOUR_HIGHLIGHTTEXT:
198 fg(ButtonWidget(), GTK_STATE_FLAG_SELECTED, gdkRGBA);
199 break;
200 case wxSYS_COLOUR_HOTLIGHT:
201 {
202 static GtkWidget* s_widget;
203 if (s_widget == NULL)
204 {
205 s_widget = gtk_link_button_new("");
206 gtk_container_add(ContainerWidget(), s_widget);
207 }
208 fg(s_widget, GTK_STATE_FLAG_NORMAL, gdkRGBA);
209 }
210 break;
211 case wxSYS_COLOUR_INFOBK:
212 bg(ToolTipWidget(), GTK_STATE_FLAG_NORMAL, gdkRGBA);
213 break;
214 case wxSYS_COLOUR_INFOTEXT:
215 fg(ToolTipWidget(), GTK_STATE_FLAG_NORMAL, gdkRGBA);
216 break;
217 case wxSYS_COLOUR_LISTBOX:
218 bg(ListWidget(), GTK_STATE_FLAG_NORMAL, gdkRGBA);
219 break;
220 case wxSYS_COLOUR_LISTBOXHIGHLIGHTTEXT:
221 fg(ListWidget(), GTK_STATE_FLAG_SELECTED, gdkRGBA);
222 break;
223 case wxSYS_COLOUR_LISTBOXTEXT:
224 fg(ListWidget(), GTK_STATE_FLAG_NORMAL, gdkRGBA);
225 break;
226 case wxSYS_COLOUR_MENU:
227 bg(MenuItemWidget(), GTK_STATE_FLAG_NORMAL, gdkRGBA);
228 break;
229 case wxSYS_COLOUR_MENUBAR:
230 bg(MenuBarWidget(), GTK_STATE_FLAG_NORMAL, gdkRGBA);
231 break;
232 case wxSYS_COLOUR_ACTIVECAPTION:
233 case wxSYS_COLOUR_MENUHILIGHT:
234 bg(MenuItemWidget(), GTK_STATE_FLAG_SELECTED, gdkRGBA);
235 break;
236 case wxSYS_COLOUR_MENUTEXT:
237 fg(MenuItemWidget(), GTK_STATE_FLAG_NORMAL, gdkRGBA);
238 break;
239 case wxSYS_COLOUR_APPWORKSPACE:
240 case wxSYS_COLOUR_WINDOW:
241 bg(TextCtrlWidget(), GTK_STATE_FLAG_NORMAL, gdkRGBA);
242 break;
243 case wxSYS_COLOUR_CAPTIONTEXT:
244 {
245 GdkRGBA c = { 1, 1, 1, 1 };
246 gdkRGBA = c;
247 }
248 break;
249 default:
250 wxFAIL_MSG("unknown system colour index");
251 // fallthrough
252 case wxSYS_COLOUR_3DDKSHADOW:
253 case wxSYS_COLOUR_GRADIENTACTIVECAPTION:
254 case wxSYS_COLOUR_GRADIENTINACTIVECAPTION:
255 // black
256 break;
257 }
258 return wxColour(gdkRGBA);
259 }
260 #else
261 static const GtkStyle* ButtonStyle()
262 {
263 return gtk_widget_get_style(ButtonWidget());
264 }
265
266 static const GtkStyle* ListStyle()
267 {
268 return gtk_widget_get_style(ListWidget());
269 }
270
271 static const GtkStyle* TextCtrlStyle()
272 {
273 return gtk_widget_get_style(TextCtrlWidget());
274 }
275
276 static const GtkStyle* MenuItemStyle()
277 {
278 return gtk_widget_get_style(MenuItemWidget());
279 }
280
281 static const GtkStyle* MenuBarStyle()
282 {
283 return gtk_widget_get_style(MenuBarWidget());
284 }
285
286 static const GtkStyle* ToolTipStyle()
287 {
288 return gtk_widget_get_style(ToolTipWidget());
289 }
290
291 wxColour wxSystemSettingsNative::GetColour( wxSystemColour index )
292 {
293 wxColor color;
294 switch (index)
295 {
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]);
308 break;
309
310 case wxSYS_COLOUR_WINDOW:
311 color = wxColor(TextCtrlStyle()->base[GTK_STATE_NORMAL]);
312 break;
313
314 case wxSYS_COLOUR_MENUBAR:
315 color = wxColor(MenuBarStyle()->bg[GTK_STATE_NORMAL]);
316 break;
317
318 case wxSYS_COLOUR_3DDKSHADOW:
319 color = *wxBLACK;
320 break;
321
322 case wxSYS_COLOUR_GRAYTEXT:
323 case wxSYS_COLOUR_BTNSHADOW:
324 //case wxSYS_COLOUR_3DSHADOW:
325 {
326 wxColour faceColour(GetColour(wxSYS_COLOUR_3DFACE));
327 color =
328 wxColour((unsigned char) (faceColour.Red() * 2 / 3),
329 (unsigned char) (faceColour.Green() * 2 / 3),
330 (unsigned char) (faceColour.Blue() * 2 / 3));
331 }
332 break;
333
334 case wxSYS_COLOUR_BTNHIGHLIGHT:
335 //case wxSYS_COLOUR_BTNHILIGHT:
336 //case wxSYS_COLOUR_3DHIGHLIGHT:
337 //case wxSYS_COLOUR_3DHILIGHT:
338 color = *wxWHITE;
339 break;
340
341 case wxSYS_COLOUR_HIGHLIGHT:
342 color = wxColor(ButtonStyle()->bg[GTK_STATE_SELECTED]);
343 break;
344
345 case wxSYS_COLOUR_LISTBOX:
346 color = wxColor(ListStyle()->base[GTK_STATE_NORMAL]);
347 break;
348
349 case wxSYS_COLOUR_LISTBOXTEXT:
350 color = wxColor(ListStyle()->text[GTK_STATE_NORMAL]);
351 break;
352
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]);
357 break;
358
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]);
365 break;
366
367 case wxSYS_COLOUR_INFOBK:
368 color = wxColor(ToolTipStyle()->bg[GTK_STATE_NORMAL]);
369 break;
370
371 case wxSYS_COLOUR_INFOTEXT:
372 color = wxColor(ToolTipStyle()->fg[GTK_STATE_NORMAL]);
373 break;
374
375 case wxSYS_COLOUR_HIGHLIGHTTEXT:
376 color = wxColor(ButtonStyle()->fg[GTK_STATE_SELECTED]);
377 break;
378
379 case wxSYS_COLOUR_APPWORKSPACE:
380 color = *wxWHITE; // ?
381 break;
382
383 case wxSYS_COLOUR_ACTIVECAPTION:
384 case wxSYS_COLOUR_MENUHILIGHT:
385 color = wxColor(MenuItemStyle()->bg[GTK_STATE_SELECTED]);
386 break;
387
388 case wxSYS_COLOUR_HOTLIGHT:
389 case wxSYS_COLOUR_GRADIENTACTIVECAPTION:
390 case wxSYS_COLOUR_GRADIENTINACTIVECAPTION:
391 // TODO
392 color = *wxBLACK;
393 break;
394
395 case wxSYS_COLOUR_MAX:
396 default:
397 wxFAIL_MSG( wxT("unknown system colour index") );
398 color = *wxWHITE;
399 break;
400 }
401
402 wxASSERT(color.IsOk());
403 return color;
404 }
405 #endif
406
407 wxFont wxSystemSettingsNative::GetFont( wxSystemFont index )
408 {
409 wxFont font;
410 switch (index)
411 {
412 case wxSYS_OEM_FIXED_FONT:
413 case wxSYS_ANSI_FIXED_FONT:
414 case wxSYS_SYSTEM_FIXED_FONT:
415 font = *wxNORMAL_FONT;
416 break;
417
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())
423 {
424 wxNativeFontInfo info;
425 #ifdef __WXGTK3__
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));
429 #else
430 info.description = ButtonStyle()->font_desc;
431 #endif
432 gs_fontSystem = wxFont(info);
433
434 #if wxUSE_FONTENUM
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
441
442 info.description = NULL;
443 }
444 font = gs_fontSystem;
445 break;
446
447 default:
448 break;
449 }
450
451 wxASSERT( font.IsOk() );
452
453 return font;
454 }
455
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)
459 {
460 return window ? gtk_settings_get_for_screen(gdk_window_get_screen(window))
461 : gtk_settings_get_default();
462 }
463
464 static int GetBorderWidth(wxSystemMetric index, wxWindow* win)
465 {
466 if (win->m_wxwindow)
467 {
468 wxPizza* pizza = WX_PIZZA(win->m_wxwindow);
469 GtkBorder border;
470 pizza->get_border(border);
471 switch (index)
472 {
473 case wxSYS_BORDER_X:
474 case wxSYS_EDGE_X:
475 case wxSYS_FRAMESIZE_X:
476 return border.left;
477 default:
478 return border.top;
479 }
480 }
481 return -1;
482 }
483
484 int wxSystemSettingsNative::GetMetric( wxSystemMetric index, wxWindow* win )
485 {
486 GdkWindow *window = NULL;
487 if (win)
488 window = gtk_widget_get_window(win->GetHandle());
489
490 switch (index)
491 {
492 case wxSYS_BORDER_X:
493 case wxSYS_BORDER_Y:
494 case wxSYS_EDGE_X:
495 case wxSYS_EDGE_Y:
496 case wxSYS_FRAMESIZE_X:
497 case wxSYS_FRAMESIZE_Y:
498 if (win)
499 {
500 wxTopLevelWindow *tlw = wxDynamicCast(win, wxTopLevelWindow);
501 if (!tlw)
502 return GetBorderWidth(index, win);
503 else if (window)
504 {
505 // Get the frame extents from the windowmanager.
506 // In most cases the top extent is the titlebar, so we use the bottom extent
507 // for the heights.
508 int right, bottom;
509 if (wxGetFrameExtents(window, NULL, &right, NULL, &bottom))
510 {
511 switch (index)
512 {
513 case wxSYS_BORDER_X:
514 case wxSYS_EDGE_X:
515 case wxSYS_FRAMESIZE_X:
516 return right; // width of right extent
517 default:
518 return bottom; // height of bottom extent
519 }
520 }
521 }
522 }
523
524 return -1; // no window specified
525
526 case wxSYS_CURSOR_X:
527 case wxSYS_CURSOR_Y:
528 return gdk_display_get_default_cursor_size(
529 window ? gdk_window_get_display(window)
530 : gdk_display_get_default());
531
532 case wxSYS_DCLICK_X:
533 case wxSYS_DCLICK_Y:
534 gint dclick_distance;
535 g_object_get(GetSettingsForWindowScreen(window),
536 "gtk-double-click-distance", &dclick_distance, NULL);
537
538 return dclick_distance * 2;
539
540 case wxSYS_DCLICK_MSEC:
541 gint dclick;
542 g_object_get(GetSettingsForWindowScreen(window),
543 "gtk-double-click-time", &dclick, NULL);
544 return dclick;
545
546 case wxSYS_DRAG_X:
547 case wxSYS_DRAG_Y:
548 gint drag_threshold;
549 g_object_get(GetSettingsForWindowScreen(window),
550 "gtk-dnd-drag-threshold", &drag_threshold, NULL);
551
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;
557
558 return drag_threshold;
559
560 case wxSYS_ICON_X:
561 case wxSYS_ICON_Y:
562 return 32;
563
564 case wxSYS_SCREEN_X:
565 if (window)
566 return gdk_screen_get_width(gdk_window_get_screen(window));
567 else
568 return gdk_screen_width();
569
570 case wxSYS_SCREEN_Y:
571 if (window)
572 return gdk_screen_get_height(gdk_window_get_screen(window));
573 else
574 return gdk_screen_height();
575
576 case wxSYS_HSCROLL_Y:
577 case wxSYS_VSCROLL_X:
578 return 15;
579
580 case wxSYS_CAPTION_Y:
581 if (!window)
582 // No realized window specified, and no implementation for that case yet.
583 return -1;
584
585 wxASSERT_MSG( wxDynamicCast(win, wxTopLevelWindow),
586 wxT("Asking for caption height of a non toplevel window") );
587
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.
593 {
594 int top;
595 if (wxGetFrameExtents(window, NULL, NULL, &top, NULL))
596 {
597 return top; // top frame extent
598 }
599 }
600
601 // Try a default approach without a window pointer, if possible
602 // ...
603
604 return -1;
605
606 case wxSYS_PENWINDOWS_PRESENT:
607 // No MS Windows for Pen computing extension available in X11 based gtk+.
608 return 0;
609
610 default:
611 return -1; // metric is unknown
612 }
613 }
614
615 bool wxSystemSettingsNative::HasFeature(wxSystemFeature index)
616 {
617 switch (index)
618 {
619 case wxSYS_CAN_ICONIZE_FRAME:
620 return false;
621
622 case wxSYS_CAN_DRAW_FRAME_DECORATIONS:
623 return true;
624
625 default:
626 return false;
627 }
628 }