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