1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/settings.cpp
4 // Author: Robert Roebling
5 // Modified by: Mart Raudsepp (GetMetric)
7 // Copyright: (c) 1998 Robert Roebling
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // For compilers that support precompilation, includes "wx.h".
12 #include "wx/wxprec.h"
14 #include "wx/settings.h"
16 #include "wx/cmndata.h"
17 #include "wx/fontutil.h"
18 #include "wx/toplevel.h"
22 #include <gdk/gdkprivate.h>
25 #include <X11/Xatom.h>
27 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
29 // ----------------------------------------------------------------------------
31 // ----------------------------------------------------------------------------
33 struct wxSystemObjects
35 wxColour m_colBtnFace
,
42 m_colMenuItemHighlight
,
49 static wxSystemObjects gs_objects
;
51 // ----------------------------------------------------------------------------
52 // wxSystemSettings implementation
53 // ----------------------------------------------------------------------------
55 // kind of widget to use in GetColourFromGTKWidget
71 // wxSystemSettings::GetColour() helper: get the colours from a GTK+
72 // widget style, return true if we did get them, false to use defaults
73 static bool GetColourFromGTKWidget(int& red
, int& green
, int& blue
,
74 wxGtkWidgetType type
= wxGTK_BUTTON
,
75 GtkStateType state
= GTK_STATE_NORMAL
,
76 wxGtkColourType colour
= wxGTK_BG
)
82 wxFAIL_MSG( _T("unexpected GTK widget type") );
86 widget
= gtk_button_new();
90 widget
= gtk_list_new();
94 widget
= gtk_menu_item_new();
97 GtkStyle
*def
= gtk_rc_get_style( widget
);
99 def
= gtk_widget_get_default_style();
108 wxFAIL_MSG( _T("unexpected GTK colour type") );
124 red
= col
[state
].red
;
125 green
= col
[state
].green
;
126 blue
= col
[state
].blue
;
135 gtk_widget_destroy( widget
);
140 static void GetTooltipColors()
142 GtkTooltips
* tooltips
= gtk_tooltips_new();
143 gtk_tooltips_force_window(tooltips
);
144 gtk_widget_ensure_style(tooltips
->tip_window
);
145 GdkColor c
= tooltips
->tip_window
->style
->bg
[GTK_STATE_NORMAL
];
146 gs_objects
.m_colTooltip
= wxColor(c
.red
>> SHIFT
, c
.green
>> SHIFT
, c
.blue
>> SHIFT
);
147 c
= tooltips
->tip_window
->style
->fg
[GTK_STATE_NORMAL
];
148 gs_objects
.m_colTooltipText
= wxColor(c
.red
>> SHIFT
, c
.green
>> SHIFT
, c
.blue
>> SHIFT
);
149 gtk_object_sink(wx_reinterpret_cast(GtkObject
*, tooltips
));
152 wxColour
wxSystemSettingsNative::GetColour( wxSystemColour index
)
156 case wxSYS_COLOUR_SCROLLBAR
:
157 case wxSYS_COLOUR_BACKGROUND
:
158 case wxSYS_COLOUR_INACTIVECAPTION
:
159 case wxSYS_COLOUR_MENU
:
160 case wxSYS_COLOUR_WINDOWFRAME
:
161 case wxSYS_COLOUR_ACTIVEBORDER
:
162 case wxSYS_COLOUR_INACTIVEBORDER
:
163 case wxSYS_COLOUR_BTNFACE
:
164 case wxSYS_COLOUR_MENUBAR
:
165 case wxSYS_COLOUR_3DLIGHT
:
166 if (!gs_objects
.m_colBtnFace
.Ok())
168 int red
, green
, blue
;
169 if ( !GetColourFromGTKWidget(red
, green
, blue
) )
176 gs_objects
.m_colBtnFace
= wxColour( red
>> SHIFT
,
180 return gs_objects
.m_colBtnFace
;
182 case wxSYS_COLOUR_WINDOW
:
185 case wxSYS_COLOUR_3DDKSHADOW
:
188 case wxSYS_COLOUR_GRAYTEXT
:
189 case wxSYS_COLOUR_BTNSHADOW
:
190 //case wxSYS_COLOUR_3DSHADOW:
191 if (!gs_objects
.m_colBtnShadow
.Ok())
193 wxColour
faceColour(GetColour(wxSYS_COLOUR_3DFACE
));
194 gs_objects
.m_colBtnShadow
=
195 wxColour((unsigned char) (faceColour
.Red() * 0.666),
196 (unsigned char) (faceColour
.Green() * 0.666),
197 (unsigned char) (faceColour
.Blue() * 0.666));
200 return gs_objects
.m_colBtnShadow
;
202 case wxSYS_COLOUR_3DHIGHLIGHT
:
203 //case wxSYS_COLOUR_BTNHIGHLIGHT:
206 case wxSYS_COLOUR_HIGHLIGHT
:
207 if (!gs_objects
.m_colHighlight
.Ok())
209 int red
, green
, blue
;
210 if ( !GetColourFromGTKWidget(red
, green
, blue
,
212 GTK_STATE_SELECTED
) )
219 gs_objects
.m_colHighlight
= wxColour( red
>> SHIFT
,
223 return gs_objects
.m_colHighlight
;
225 case wxSYS_COLOUR_LISTBOX
:
226 if (!gs_objects
.m_colListBox
.Ok())
228 int red
, green
, blue
;
229 if ( GetColourFromGTKWidget(red
, green
, blue
,
234 gs_objects
.m_colListBox
= wxColour( red
>> SHIFT
,
240 gs_objects
.m_colListBox
= wxColour(*wxWHITE
);
243 return gs_objects
.m_colListBox
;
245 case wxSYS_COLOUR_MENUTEXT
:
246 case wxSYS_COLOUR_WINDOWTEXT
:
247 case wxSYS_COLOUR_CAPTIONTEXT
:
248 case wxSYS_COLOUR_INACTIVECAPTIONTEXT
:
249 case wxSYS_COLOUR_BTNTEXT
:
250 if (!gs_objects
.m_colBtnText
.Ok())
252 int red
, green
, blue
;
253 if ( !GetColourFromGTKWidget(red
, green
, blue
,
263 gs_objects
.m_colBtnText
= wxColour( red
>> SHIFT
,
267 return gs_objects
.m_colBtnText
;
269 case wxSYS_COLOUR_INFOBK
:
270 if (!gs_objects
.m_colTooltip
.Ok()) {
273 return gs_objects
.m_colTooltip
;
275 case wxSYS_COLOUR_INFOTEXT
:
276 if (!gs_objects
.m_colTooltipText
.Ok()) {
279 return gs_objects
.m_colTooltipText
;
281 case wxSYS_COLOUR_HIGHLIGHTTEXT
:
282 if (!gs_objects
.m_colHighlightText
.Ok())
284 wxColour hclr
= GetColour(wxSYS_COLOUR_HIGHLIGHT
);
285 if (hclr
.Red() > 200 && hclr
.Green() > 200 && hclr
.Blue() > 200)
286 gs_objects
.m_colHighlightText
= wxColour(*wxBLACK
);
288 gs_objects
.m_colHighlightText
= wxColour(*wxWHITE
);
290 return gs_objects
.m_colHighlightText
;
292 case wxSYS_COLOUR_APPWORKSPACE
:
293 return *wxWHITE
; // ?
295 case wxSYS_COLOUR_ACTIVECAPTION
:
296 case wxSYS_COLOUR_MENUHILIGHT
:
297 if (!gs_objects
.m_colMenuItemHighlight
.Ok())
299 int red
, green
, blue
;
300 if ( !GetColourFromGTKWidget(red
, green
, blue
,
310 gs_objects
.m_colMenuItemHighlight
= wxColour( red
>> SHIFT
,
314 return gs_objects
.m_colMenuItemHighlight
;
316 case wxSYS_COLOUR_HOTLIGHT
:
317 case wxSYS_COLOUR_GRADIENTACTIVECAPTION
:
318 case wxSYS_COLOUR_GRADIENTINACTIVECAPTION
:
322 case wxSYS_COLOUR_MAX
:
324 wxFAIL_MSG( _T("unknown system colour index") );
330 wxFont
wxSystemSettingsNative::GetFont( wxSystemFont index
)
334 case wxSYS_OEM_FIXED_FONT
:
335 case wxSYS_ANSI_FIXED_FONT
:
336 case wxSYS_SYSTEM_FIXED_FONT
:
338 return *wxNORMAL_FONT
;
340 case wxSYS_ANSI_VAR_FONT
:
341 case wxSYS_SYSTEM_FONT
:
342 case wxSYS_DEVICE_DEFAULT_FONT
:
343 case wxSYS_DEFAULT_GUI_FONT
:
345 if (!gs_objects
.m_fontSystem
.Ok())
348 GtkWidget
*widget
= gtk_button_new();
349 GtkStyle
*def
= gtk_rc_get_style( widget
);
350 if ( !def
|| !def
->font_desc
)
351 def
= gtk_widget_get_default_style();
352 if ( def
&& def
->font_desc
)
354 wxNativeFontInfo info
;
356 pango_font_description_copy(def
->font_desc
);
357 gs_objects
.m_fontSystem
= wxFont(info
);
361 GtkSettings
*settings
= gtk_settings_get_default();
362 gchar
*font_name
= NULL
;
363 g_object_get ( settings
,
368 gs_objects
.m_fontSystem
= wxFont( 12, wxSWISS
, wxNORMAL
, wxNORMAL
);
370 gs_objects
.m_fontSystem
= wxFont(wxString::FromAscii(font_name
));
373 gtk_widget_destroy( widget
);
375 gs_objects
.m_fontSystem
= wxFont( 12, wxSWISS
, wxNORMAL
, wxNORMAL
);
378 return gs_objects
.m_fontSystem
;
386 int wxSystemSettingsNative::GetMetric( wxSystemMetric index
, wxWindow
* win
)
389 bool success
= false;
392 GdkWindow
*window
= NULL
;
393 if(win
&& GTK_WIDGET_REALIZED(win
->GetHandle()))
394 window
= win
->GetHandle()->window
;
404 case wxSYS_FRAMESIZE_X
:
405 case wxSYS_FRAMESIZE_Y
:
406 // If a window is specified/realized, and it is a toplevel window, we can query from wm.
407 // The returned border thickness is outside the client area in that case.
410 wxTopLevelWindow
*tlw
= wxDynamicCast(win
, wxTopLevelWindow
);
412 return -1; // not a tlw, not sure how to approach
415 // Check if wm supports frame extents - we can't know
416 // the border widths if it does not.
417 #if GTK_CHECK_VERSION(2,2,0)
418 if (!gtk_check_version(2,2,0))
420 if (!gdk_x11_screen_supports_net_wm_hint(
421 gdk_drawable_get_screen(window
),
422 gdk_atom_intern("_NET_FRAME_EXTENTS", false) ) )
428 if (!gdk_net_wm_supports(gdk_atom_intern("_NET_FRAME_EXTENTS", false)))
432 // Get the frame extents from the windowmanager.
433 // In most cases the top extent is the titlebar, so we use the bottom extent
440 #if GTK_CHECK_VERSION(2,2,0)
441 if (!gtk_check_version(2,2,0))
444 success
= (XGetWindowProperty (GDK_DISPLAY_XDISPLAY(gdk_drawable_get_display(window
)),
445 GDK_WINDOW_XWINDOW(window
),
446 gdk_x11_get_xatom_by_name_for_display (
447 gdk_drawable_get_display(window
),
448 "_NET_FRAME_EXTENTS" ),
449 0, // left, right, top, bottom, CARDINAL[4]/32
450 G_MAXLONG
, // size of long
451 false, // do not delete property
452 XA_CARDINAL
, // 32 bit
453 &type
, &format
, &nitems
, &bytes_after
, &data
459 int border_return
= -1;
461 if ((type
== XA_CARDINAL
) && (format
== 32) && (nitems
>= 4) && (data
))
464 borders
= (long*)data
;
469 case wxSYS_FRAMESIZE_X
:
470 border_return
= borders
[1]; // width of right extent
473 border_return
= borders
[3]; // height of bottom extent
481 return border_return
;
486 return -1; // no window specified
492 if (!gtk_check_version(2,4,0))
495 return gdk_display_get_default_cursor_size(gdk_drawable_get_display(window
));
497 return gdk_display_get_default_cursor_size(gdk_display_get_default());
506 gint dclick_distance
;
507 #if GTK_CHECK_VERSION(2,2,0)
508 if (window
&& !gtk_check_version(2,2,0))
509 g_object_get(gtk_settings_get_for_screen(gdk_drawable_get_screen(window
)),
510 "gtk-double-click-distance", &dclick_distance
, NULL
);
513 g_object_get(gtk_settings_get_default(),
514 "gtk-double-click-distance", &dclick_distance
, NULL
);
516 return dclick_distance
* 2;
523 #if GTK_CHECK_VERSION(2,2,0)
524 if (window
&& !gtk_check_version(2,2,0))
527 gtk_settings_get_for_screen(gdk_drawable_get_screen(window
)),
528 "gtk-dnd-drag-threshold",
529 &drag_threshold
, NULL
);
534 g_object_get(gtk_settings_get_default(),
535 "gtk-dnd-drag-threshold", &drag_threshold
, NULL
);
538 return drag_threshold
* 2;
541 // MBN: ditto for icons
542 case wxSYS_ICON_X
: return 32;
543 case wxSYS_ICON_Y
: return 32;
546 #if defined(__WXGTK20__) && GTK_CHECK_VERSION(2,2,0)
547 if (window
&& !gtk_check_version(2,2,0))
548 return gdk_screen_get_width(gdk_drawable_get_screen(window
));
551 return gdk_screen_width();
554 #if defined(__WXGTK20__) && GTK_CHECK_VERSION(2,2,0)
555 if (window
&& !gtk_check_version(2,2,0))
556 return gdk_screen_get_height(gdk_drawable_get_screen(window
));
559 return gdk_screen_height();
561 case wxSYS_HSCROLL_Y
: return 15;
562 case wxSYS_VSCROLL_X
: return 15;
564 // a gtk1 implementation should be possible too if gtk2 efficiency/convenience functions aren't used
566 case wxSYS_CAPTION_Y
:
568 // No realized window specified, and no implementation for that case yet.
571 // Check if wm supports frame extents - we can't know the caption height if it does not.
572 #if GTK_CHECK_VERSION(2,2,0)
573 if (!gtk_check_version(2,2,0))
575 if (!gdk_x11_screen_supports_net_wm_hint(
576 gdk_drawable_get_screen(window
),
577 gdk_atom_intern("_NET_FRAME_EXTENTS", false) ) )
583 if (!gdk_net_wm_supports(gdk_atom_intern("_NET_FRAME_EXTENTS", false)))
587 wxASSERT_MSG( wxDynamicCast(win
, wxTopLevelWindow
),
588 wxT("Asking for caption height of a non toplevel window") );
590 // Get the height of the top windowmanager border.
591 // This is the titlebar in most cases. The titlebar might be elsewhere, and
592 // we could check which is the thickest wm border to decide on which side the
593 // titlebar is, but this might lead to interesting behaviours in used code.
594 // Reconsider when we have a way to report to the user on which side it is.
600 #if GTK_CHECK_VERSION(2,2,0)
601 if (!gtk_check_version(2,2,0))
604 success
= (XGetWindowProperty (GDK_DISPLAY_XDISPLAY(gdk_drawable_get_display(window
)),
605 GDK_WINDOW_XWINDOW(window
),
606 gdk_x11_get_xatom_by_name_for_display (
607 gdk_drawable_get_display(window
),
608 "_NET_FRAME_EXTENTS" ),
609 0, // left, right, top, bottom, CARDINAL[4]/32
610 G_MAXLONG
, // size of long
611 false, // do not delete property
612 XA_CARDINAL
, // 32 bit
613 &type
, &format
, &nitems
, &bytes_after
, &data
619 int caption_height
= -1;
621 if ((type
== XA_CARDINAL
) && (format
== 32) && (nitems
>= 3) && (data
))
624 borders
= (long*)data
;
625 caption_height
= borders
[2]; // top frame extent
631 return caption_height
;
634 // Try a default approach without a window pointer, if possible
640 case wxSYS_PENWINDOWS_PRESENT
:
641 // No MS Windows for Pen computing extension available in X11 based gtk+.
645 return -1; // metric is unknown
649 bool wxSystemSettingsNative::HasFeature(wxSystemFeature index
)
653 case wxSYS_CAN_ICONIZE_FRAME
:
656 case wxSYS_CAN_DRAW_FRAME_DECORATIONS
: