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