]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/settings.cpp
add support for custom controls in file dialog in wxGTK and generic versions; also...
[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/cmndata.h"
18 #include "wx/toplevel.h"
19 #endif
20
21 #include "wx/fontutil.h"
22
23 #include <gtk/gtkversion.h>
24 #if GTK_CHECK_VERSION(2, 9, 0)
25 // gtk_object_sink
26 #undef GTK_DISABLE_DEPRECATED
27 #endif
28 #include <gtk/gtk.h>
29 #include <gdk/gdkx.h>
30
31 #include <X11/Xatom.h>
32
33 // ----------------------------------------------------------------------------
34 // wxSystemObjects
35 // ----------------------------------------------------------------------------
36
37 struct wxSystemObjects
38 {
39 wxColour m_colBtnFace,
40 m_colBtnShadow,
41 m_colBtnHighlight,
42 m_colHighlight,
43 m_colHighlightText,
44 m_colListBox,
45 m_colWindow,
46 m_colWindowText,
47 m_colBtnText,
48 m_colMenuItemHighlight,
49 m_colTooltip,
50 m_colTooltipText,
51 m_colMenubarBg;
52
53 wxFont m_fontSystem;
54 };
55
56 static wxSystemObjects gs_objects;
57
58 void wxClearGtkSystemObjects()
59 {
60 gs_objects.m_colBtnFace = wxColour();
61 gs_objects.m_colBtnShadow = wxColour();
62 gs_objects.m_colBtnHighlight = wxColour();
63 gs_objects.m_colHighlightText = wxColour();
64 gs_objects.m_colListBox = wxColour();
65 gs_objects.m_colWindow = wxColour();
66 gs_objects.m_colWindowText = wxColour();
67 gs_objects.m_colBtnText = wxColour();
68 gs_objects.m_colMenuItemHighlight = wxColour();
69 gs_objects.m_colTooltip = wxColour();
70 gs_objects.m_colTooltipText = wxColour();
71 gs_objects.m_colMenubarBg = wxColour();
72 gs_objects.m_fontSystem = wxNullFont;
73 }
74
75 // ----------------------------------------------------------------------------
76 // wxSystemSettings implementation
77 // ----------------------------------------------------------------------------
78
79 // kind of widget to use in GetColourFromGTKWidget
80 enum wxGtkWidgetType
81 {
82 wxGTK_BUTTON,
83 wxGTK_LIST,
84 wxGTK_MENUITEM,
85 wxGTK_TEXTCTRL,
86 wxGTK_MENUBAR,
87 };
88
89 // the colour we need
90 enum wxGtkColourType
91 {
92 wxGTK_FG,
93 wxGTK_BG,
94 wxGTK_BASE
95 };
96
97 // wxSystemSettings::GetColour() helper: get the colours from a GTK+
98 // widget style, return true if we did get them
99 static bool GetColourFromGTKWidget(GdkColor& gdkColor,
100 wxGtkWidgetType type = wxGTK_BUTTON,
101 GtkStateType state = GTK_STATE_NORMAL,
102 wxGtkColourType colour = wxGTK_BG)
103 {
104 GtkWidget *widget;
105 switch ( type )
106 {
107 default:
108 wxFAIL_MSG( _T("unexpected GTK widget type") );
109 // fall through
110
111 case wxGTK_BUTTON:
112 widget = gtk_button_new();
113 break;
114
115 case wxGTK_TEXTCTRL:
116 widget = gtk_text_view_new();
117 break;
118
119 case wxGTK_LIST:
120 widget = gtk_tree_view_new_with_model(
121 (GtkTreeModel*)gtk_list_store_new(1, G_TYPE_INT));
122 break;
123
124 case wxGTK_MENUITEM:
125 widget = gtk_menu_item_new();
126
127 case wxGTK_MENUBAR:
128 widget = gtk_menu_bar_new();
129 break;
130
131 }
132
133 GtkStyle *def = gtk_rc_get_style( widget );
134 if ( !def )
135 def = gtk_widget_get_default_style();
136
137 const bool ok = def != NULL;
138 if (ok)
139 {
140 switch ( colour )
141 {
142 default:
143 wxFAIL_MSG( _T("unexpected GTK colour type") );
144 // fall through
145
146 case wxGTK_FG:
147 gdkColor = def->fg[state];
148 break;
149
150 case wxGTK_BG:
151 gdkColor = def->bg[state];
152 break;
153
154 case wxGTK_BASE:
155 gdkColor = def->base[state];
156 break;
157 }
158 }
159
160 gtk_object_sink((GtkObject*)widget);
161
162 return ok;
163 }
164
165 static void GetTooltipColors()
166 {
167 GtkWidget* widget = gtk_window_new(GTK_WINDOW_POPUP);
168 const char* name = "gtk-tooltip";
169 if (gtk_check_version(2, 11, 0))
170 name = "gtk-tooltips";
171 gtk_widget_set_name(widget, name);
172 gtk_widget_ensure_style(widget);
173
174 GdkColor c = widget->style->bg[GTK_STATE_NORMAL];
175 gs_objects.m_colTooltip = wxColor(c);
176 c = widget->style->fg[GTK_STATE_NORMAL];
177 gs_objects.m_colTooltipText = wxColor(c);
178
179 gtk_widget_destroy(widget);
180 }
181
182 wxColour wxSystemSettingsNative::GetColour( wxSystemColour index )
183 {
184 wxColor color;
185 GdkColor gdkColor;
186 switch (index)
187 {
188 case wxSYS_COLOUR_SCROLLBAR:
189 case wxSYS_COLOUR_BACKGROUND:
190 case wxSYS_COLOUR_INACTIVECAPTION:
191 case wxSYS_COLOUR_MENU:
192 case wxSYS_COLOUR_WINDOWFRAME:
193 case wxSYS_COLOUR_ACTIVEBORDER:
194 case wxSYS_COLOUR_INACTIVEBORDER:
195 case wxSYS_COLOUR_BTNFACE:
196 case wxSYS_COLOUR_3DLIGHT:
197 if (!gs_objects.m_colBtnFace.Ok())
198 {
199 gdkColor.red =
200 gdkColor.green = 0;
201 gdkColor.blue = 0x9c40;
202 GetColourFromGTKWidget(gdkColor);
203 gs_objects.m_colBtnFace = wxColor(gdkColor);
204 }
205 color = gs_objects.m_colBtnFace;
206 break;
207
208 case wxSYS_COLOUR_WINDOW:
209 if (!gs_objects.m_colWindow.Ok())
210 {
211 gdkColor.red =
212 gdkColor.green =
213 gdkColor.blue = 0xFFFF;
214 GetColourFromGTKWidget(gdkColor, wxGTK_TEXTCTRL, GTK_STATE_NORMAL, wxGTK_BASE);
215 gs_objects.m_colWindow = wxColor(gdkColor);
216 }
217 color = gs_objects.m_colWindow;
218 break;
219
220
221 case wxSYS_COLOUR_MENUBAR:
222 if (!gs_objects.m_colMenubarBg.Ok())
223 {
224 gdkColor.red =
225 gdkColor.green = 0;
226 gdkColor.blue = 0x9c40;
227 GetColourFromGTKWidget(gdkColor,wxGTK_MENUBAR);
228 gs_objects.m_colMenubarBg = wxColor(gdkColor);
229 }
230 color = gs_objects.m_colMenubarBg;
231 break;
232
233 case wxSYS_COLOUR_3DDKSHADOW:
234 color = *wxBLACK;
235 break;
236
237 case wxSYS_COLOUR_GRAYTEXT:
238 case wxSYS_COLOUR_BTNSHADOW:
239 //case wxSYS_COLOUR_3DSHADOW:
240 if (!gs_objects.m_colBtnShadow.Ok())
241 {
242 wxColour faceColour(GetColour(wxSYS_COLOUR_3DFACE));
243 gs_objects.m_colBtnShadow =
244 wxColour((unsigned char) (faceColour.Red() * 2 / 3),
245 (unsigned char) (faceColour.Green() * 2 / 3),
246 (unsigned char) (faceColour.Blue() * 2 / 3));
247 }
248 color = gs_objects.m_colBtnShadow;
249 break;
250
251 case wxSYS_COLOUR_3DHIGHLIGHT:
252 //case wxSYS_COLOUR_BTNHIGHLIGHT:
253 color = *wxWHITE;
254 break;
255
256 case wxSYS_COLOUR_HIGHLIGHT:
257 if (!gs_objects.m_colHighlight.Ok())
258 {
259 gdkColor.red =
260 gdkColor.green = 0;
261 gdkColor.blue = 0x9c40;
262 GetColourFromGTKWidget(
263 gdkColor, wxGTK_BUTTON, GTK_STATE_SELECTED);
264 gs_objects.m_colHighlight = wxColour(gdkColor);
265 }
266 color = gs_objects.m_colHighlight;
267 break;
268
269 case wxSYS_COLOUR_LISTBOX:
270 if (!gs_objects.m_colListBox.Ok())
271 {
272 if ( GetColourFromGTKWidget(gdkColor,
273 wxGTK_LIST,
274 GTK_STATE_NORMAL,
275 wxGTK_BASE) )
276 {
277 gs_objects.m_colListBox = wxColour(gdkColor);
278 }
279 else
280 {
281 gs_objects.m_colListBox = *wxWHITE;
282 }
283 }
284 color = gs_objects.m_colListBox;
285 break;
286
287 case wxSYS_COLOUR_MENUTEXT:
288 case wxSYS_COLOUR_WINDOWTEXT:
289 case wxSYS_COLOUR_CAPTIONTEXT:
290 case wxSYS_COLOUR_INACTIVECAPTIONTEXT:
291 case wxSYS_COLOUR_BTNTEXT:
292 if (!gs_objects.m_colBtnText.Ok())
293 {
294 gdkColor.red =
295 gdkColor.green =
296 gdkColor.blue = 0;
297 GetColourFromGTKWidget(
298 gdkColor, wxGTK_BUTTON, GTK_STATE_NORMAL, wxGTK_FG);
299 gs_objects.m_colBtnText = wxColour(gdkColor);
300 }
301 color = gs_objects.m_colBtnText;
302 break;
303
304 case wxSYS_COLOUR_INFOBK:
305 if (!gs_objects.m_colTooltip.Ok()) {
306 GetTooltipColors();
307 }
308 color = gs_objects.m_colTooltip;
309 break;
310
311 case wxSYS_COLOUR_INFOTEXT:
312 if (!gs_objects.m_colTooltipText.Ok()) {
313 GetTooltipColors();
314 }
315 color = gs_objects.m_colTooltipText;
316 break;
317
318 case wxSYS_COLOUR_HIGHLIGHTTEXT:
319 if (!gs_objects.m_colHighlightText.Ok())
320 {
321 wxColour hclr = GetColour(wxSYS_COLOUR_HIGHLIGHT);
322 if (hclr.Red() > 200 && hclr.Green() > 200 && hclr.Blue() > 200)
323 gs_objects.m_colHighlightText = *wxBLACK;
324 else
325 gs_objects.m_colHighlightText = *wxWHITE;
326 }
327 color = gs_objects.m_colHighlightText;
328 break;
329
330 case wxSYS_COLOUR_APPWORKSPACE:
331 color = *wxWHITE; // ?
332 break;
333
334 case wxSYS_COLOUR_ACTIVECAPTION:
335 case wxSYS_COLOUR_MENUHILIGHT:
336 if (!gs_objects.m_colMenuItemHighlight.Ok())
337 {
338 gdkColor.red =
339 gdkColor.green =
340 gdkColor.blue = 0;
341 GetColourFromGTKWidget(
342 gdkColor, wxGTK_MENUITEM, GTK_STATE_SELECTED, wxGTK_BG);
343 gs_objects.m_colMenuItemHighlight = wxColour(gdkColor);
344 }
345 color = gs_objects.m_colMenuItemHighlight;
346 break;
347
348 case wxSYS_COLOUR_HOTLIGHT:
349 case wxSYS_COLOUR_GRADIENTACTIVECAPTION:
350 case wxSYS_COLOUR_GRADIENTINACTIVECAPTION:
351 // TODO
352 color = *wxBLACK;
353 break;
354
355 case wxSYS_COLOUR_MAX:
356 default:
357 wxFAIL_MSG( _T("unknown system colour index") );
358 color = *wxWHITE;
359 break;
360 }
361
362 return color;
363 }
364
365 wxFont wxSystemSettingsNative::GetFont( wxSystemFont index )
366 {
367 wxFont font;
368 switch (index)
369 {
370 case wxSYS_OEM_FIXED_FONT:
371 case wxSYS_ANSI_FIXED_FONT:
372 case wxSYS_SYSTEM_FIXED_FONT:
373 font = *wxNORMAL_FONT;
374 break;
375
376 case wxSYS_ANSI_VAR_FONT:
377 case wxSYS_SYSTEM_FONT:
378 case wxSYS_DEVICE_DEFAULT_FONT:
379 case wxSYS_DEFAULT_GUI_FONT:
380 if (!gs_objects.m_fontSystem.Ok())
381 {
382 GtkWidget *widget = gtk_button_new();
383 GtkStyle *def = gtk_rc_get_style( widget );
384 if ( !def || !def->font_desc )
385 def = gtk_widget_get_default_style();
386 if ( def && def->font_desc )
387 {
388 wxNativeFontInfo info;
389 info.description =
390 pango_font_description_copy(def->font_desc);
391 gs_objects.m_fontSystem = wxFont(info);
392 }
393 else
394 {
395 GtkSettings *settings = gtk_settings_get_default();
396 gchar *font_name = NULL;
397 g_object_get ( settings,
398 "gtk-font-name",
399 &font_name,
400 NULL);
401 if (!font_name)
402 gs_objects.m_fontSystem = wxFont( 12, wxSWISS, wxNORMAL, wxNORMAL );
403 else
404 gs_objects.m_fontSystem = wxFont(wxString::FromAscii(font_name));
405 g_free (font_name);
406 }
407 gtk_object_sink((GtkObject*)widget);
408 }
409 font = gs_objects.m_fontSystem;
410 break;
411
412 default:
413 break;
414 }
415 return font;
416 }
417
418 static bool GetFrameExtents(GdkWindow* window, int* left, int* right, int* top, int* bottom)
419 {
420 bool success = false;
421 Atom property = 0;
422 if (gdk_x11_screen_supports_net_wm_hint(
423 gdk_drawable_get_screen(window),
424 gdk_atom_intern("_NET_FRAME_EXTENTS", false)))
425 {
426 success = true;
427 property = gdk_x11_get_xatom_by_name_for_display(
428 gdk_drawable_get_display(window),
429 "_NET_FRAME_EXTENTS");
430 }
431
432 if (success)
433 {
434 Atom type;
435 int format;
436 gulong nitems, bytes_after;
437 guchar* data;
438 success = XGetWindowProperty(
439 gdk_x11_drawable_get_xdisplay(window),
440 gdk_x11_drawable_get_xid(window),
441 property,
442 0, 4,
443 false,
444 XA_CARDINAL,
445 &type, &format, &nitems, &bytes_after, &data
446 ) == Success;
447 if (success)
448 {
449 success = data && nitems == 4;
450 if (success)
451 {
452 long* p = (long*)data;
453 if (left) *left = int(p[0]);
454 if (right) *right = int(p[1]);
455 if (top) *top = int(p[2]);
456 if (bottom) *bottom = int(p[3]);
457 }
458 if (data)
459 XFree(data);
460 }
461 }
462 return success;
463 }
464
465 // helper: return the GtkSettings either for the screen the current window is
466 // on or for the default screen if window is NULL
467 static GtkSettings *GetSettingsForWindowScreen(GdkWindow *window)
468 {
469 return window ? gtk_settings_get_for_screen(gdk_drawable_get_screen(window))
470 : gtk_settings_get_default();
471 }
472
473 int wxSystemSettingsNative::GetMetric( wxSystemMetric index, wxWindow* win )
474 {
475 GdkWindow *window = NULL;
476 if(win && GTK_WIDGET_REALIZED(win->GetHandle()))
477 window = win->GetHandle()->window;
478
479 switch (index)
480 {
481 case wxSYS_BORDER_X:
482 case wxSYS_BORDER_Y:
483 case wxSYS_EDGE_X:
484 case wxSYS_EDGE_Y:
485 case wxSYS_FRAMESIZE_X:
486 case wxSYS_FRAMESIZE_Y:
487 // If a window is specified/realized, and it is a toplevel window, we can query from wm.
488 // The returned border thickness is outside the client area in that case.
489 if (window)
490 {
491 wxTopLevelWindow *tlw = wxDynamicCast(win, wxTopLevelWindow);
492 if (!tlw)
493 return -1; // not a tlw, not sure how to approach
494 else
495 {
496 // Get the frame extents from the windowmanager.
497 // In most cases the top extent is the titlebar, so we use the bottom extent
498 // for the heights.
499 int right, bottom;
500 if (GetFrameExtents(window, NULL, &right, NULL, &bottom))
501 {
502 switch (index)
503 {
504 case wxSYS_BORDER_X:
505 case wxSYS_EDGE_X:
506 case wxSYS_FRAMESIZE_X:
507 return right; // width of right extent
508 default:
509 return bottom; // height of bottom extent
510 }
511 }
512 }
513 }
514
515 return -1; // no window specified
516
517 case wxSYS_CURSOR_X:
518 case wxSYS_CURSOR_Y:
519 return gdk_display_get_default_cursor_size(
520 window ? gdk_drawable_get_display(window)
521 : gdk_display_get_default());
522
523 case wxSYS_DCLICK_X:
524 case wxSYS_DCLICK_Y:
525 gint dclick_distance;
526 g_object_get(GetSettingsForWindowScreen(window),
527 "gtk-double-click-distance", &dclick_distance, NULL);
528
529 return dclick_distance * 2;
530
531 case wxSYS_DCLICK_MSEC:
532 gint dclick;
533 g_object_get(GetSettingsForWindowScreen(window),
534 "gtk-double-click-time", &dclick, NULL);
535 return dclick;
536
537 case wxSYS_DRAG_X:
538 case wxSYS_DRAG_Y:
539 gint drag_threshold;
540 g_object_get(GetSettingsForWindowScreen(window),
541 "gtk-dnd-drag-threshold", &drag_threshold, NULL);
542
543 // The correct thing here would be to double the value
544 // since that is what the API wants. But the values
545 // are much bigger under GNOME than under Windows and
546 // just seem to much in many cases to be useful.
547 // drag_threshold *= 2;
548
549 return drag_threshold;
550
551 case wxSYS_ICON_X:
552 case wxSYS_ICON_Y:
553 return 32;
554
555 case wxSYS_SCREEN_X:
556 if (window)
557 return gdk_screen_get_width(gdk_drawable_get_screen(window));
558 else
559 return gdk_screen_width();
560
561 case wxSYS_SCREEN_Y:
562 if (window)
563 return gdk_screen_get_height(gdk_drawable_get_screen(window));
564 else
565 return gdk_screen_height();
566
567 case wxSYS_HSCROLL_Y:
568 case wxSYS_VSCROLL_X:
569 return 15;
570
571 case wxSYS_CAPTION_Y:
572 if (!window)
573 // No realized window specified, and no implementation for that case yet.
574 return -1;
575
576 wxASSERT_MSG( wxDynamicCast(win, wxTopLevelWindow),
577 wxT("Asking for caption height of a non toplevel window") );
578
579 // Get the height of the top windowmanager border.
580 // This is the titlebar in most cases. The titlebar might be elsewhere, and
581 // we could check which is the thickest wm border to decide on which side the
582 // titlebar is, but this might lead to interesting behaviours in used code.
583 // Reconsider when we have a way to report to the user on which side it is.
584 {
585 int top;
586 if (GetFrameExtents(window, NULL, NULL, &top, NULL))
587 {
588 return top; // top frame extent
589 }
590 }
591
592 // Try a default approach without a window pointer, if possible
593 // ...
594
595 return -1;
596
597 case wxSYS_PENWINDOWS_PRESENT:
598 // No MS Windows for Pen computing extension available in X11 based gtk+.
599 return 0;
600
601 default:
602 return -1; // metric is unknown
603 }
604 }
605
606 bool wxSystemSettingsNative::HasFeature(wxSystemFeature index)
607 {
608 switch (index)
609 {
610 case wxSYS_CAN_ICONIZE_FRAME:
611 return false;
612
613 case wxSYS_CAN_DRAW_FRAME_DECORATIONS:
614 return true;
615
616 default:
617 return false;
618 }
619 }