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