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 /////////////////////////////////////////////////////////////////////////////
12 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
13 #pragma implementation "settings.h"
16 // For compilers that support precompilation, includes "wx.h".
17 #include "wx/wxprec.h"
19 #include "wx/settings.h"
21 #include "wx/cmndata.h"
22 #include "wx/fontutil.h"
23 #include "wx/toplevel.h"
27 #include <gdk/gdkprivate.h>
30 #include <X11/Xatom.h>
32 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
34 // ----------------------------------------------------------------------------
36 // ----------------------------------------------------------------------------
38 struct wxSystemObjects
40 wxColour m_colBtnFace
,
47 m_colMenuItemHighlight
,
54 static wxSystemObjects gs_objects
;
56 // ----------------------------------------------------------------------------
57 // wxSystemSettings implementation
58 // ----------------------------------------------------------------------------
60 // kind of widget to use in GetColourFromGTKWidget
76 // wxSystemSettings::GetColour() helper: get the colours from a GTK+
77 // widget style, return true if we did get them, false to use defaults
78 static bool GetColourFromGTKWidget(int& red
, int& green
, int& blue
,
79 wxGtkWidgetType type
= wxGTK_BUTTON
,
80 GtkStateType state
= GTK_STATE_NORMAL
,
81 wxGtkColourType colour
= wxGTK_BG
)
87 wxFAIL_MSG( _T("unexpected GTK widget type") );
91 widget
= gtk_button_new();
95 widget
= gtk_list_new();
99 widget
= gtk_menu_item_new();
102 GtkStyle
*def
= gtk_rc_get_style( widget
);
104 def
= gtk_widget_get_default_style();
113 wxFAIL_MSG( _T("unexpected GTK colour type") );
129 red
= col
[state
].red
;
130 green
= col
[state
].green
;
131 blue
= col
[state
].blue
;
140 gtk_widget_destroy( widget
);
145 static void GetTooltipColors()
147 GtkTooltips
* tooltips
= gtk_tooltips_new();
148 gtk_tooltips_force_window(tooltips
);
149 gtk_widget_ensure_style(tooltips
->tip_window
);
150 GdkColor c
= tooltips
->tip_window
->style
->bg
[GTK_STATE_NORMAL
];
151 gs_objects
.m_colTooltip
= wxColor(c
.red
>> SHIFT
, c
.green
>> SHIFT
, c
.blue
>> SHIFT
);
152 c
= tooltips
->tip_window
->style
->fg
[GTK_STATE_NORMAL
];
153 gs_objects
.m_colTooltipText
= wxColor(c
.red
>> SHIFT
, c
.green
>> SHIFT
, c
.blue
>> SHIFT
);
154 gtk_object_sink(wx_reinterpret_cast(GtkObject
*, tooltips
));
157 wxColour
wxSystemSettingsNative::GetColour( wxSystemColour index
)
161 case wxSYS_COLOUR_SCROLLBAR
:
162 case wxSYS_COLOUR_BACKGROUND
:
163 case wxSYS_COLOUR_INACTIVECAPTION
:
164 case wxSYS_COLOUR_MENU
:
165 case wxSYS_COLOUR_WINDOWFRAME
:
166 case wxSYS_COLOUR_ACTIVEBORDER
:
167 case wxSYS_COLOUR_INACTIVEBORDER
:
168 case wxSYS_COLOUR_BTNFACE
:
169 case wxSYS_COLOUR_MENUBAR
:
170 case wxSYS_COLOUR_3DLIGHT
:
171 if (!gs_objects
.m_colBtnFace
.Ok())
173 int red
, green
, blue
;
174 if ( !GetColourFromGTKWidget(red
, green
, blue
) )
181 gs_objects
.m_colBtnFace
= wxColour( red
>> SHIFT
,
185 return gs_objects
.m_colBtnFace
;
187 case wxSYS_COLOUR_WINDOW
:
190 case wxSYS_COLOUR_3DDKSHADOW
:
193 case wxSYS_COLOUR_GRAYTEXT
:
194 case wxSYS_COLOUR_BTNSHADOW
:
195 //case wxSYS_COLOUR_3DSHADOW:
196 if (!gs_objects
.m_colBtnShadow
.Ok())
198 wxColour
faceColour(GetColour(wxSYS_COLOUR_3DFACE
));
199 gs_objects
.m_colBtnShadow
=
200 wxColour((unsigned char) (faceColour
.Red() * 0.666),
201 (unsigned char) (faceColour
.Green() * 0.666),
202 (unsigned char) (faceColour
.Blue() * 0.666));
205 return gs_objects
.m_colBtnShadow
;
207 case wxSYS_COLOUR_3DHIGHLIGHT
:
208 //case wxSYS_COLOUR_BTNHIGHLIGHT:
211 case wxSYS_COLOUR_HIGHLIGHT
:
212 if (!gs_objects
.m_colHighlight
.Ok())
214 int red
, green
, blue
;
215 if ( !GetColourFromGTKWidget(red
, green
, blue
,
217 GTK_STATE_SELECTED
) )
224 gs_objects
.m_colHighlight
= wxColour( red
>> SHIFT
,
228 return gs_objects
.m_colHighlight
;
230 case wxSYS_COLOUR_LISTBOX
:
231 if (!gs_objects
.m_colListBox
.Ok())
233 int red
, green
, blue
;
234 if ( GetColourFromGTKWidget(red
, green
, blue
,
239 gs_objects
.m_colListBox
= wxColour( red
>> SHIFT
,
245 gs_objects
.m_colListBox
= wxColour(*wxWHITE
);
248 return gs_objects
.m_colListBox
;
250 case wxSYS_COLOUR_MENUTEXT
:
251 case wxSYS_COLOUR_WINDOWTEXT
:
252 case wxSYS_COLOUR_CAPTIONTEXT
:
253 case wxSYS_COLOUR_INACTIVECAPTIONTEXT
:
254 case wxSYS_COLOUR_BTNTEXT
:
255 if (!gs_objects
.m_colBtnText
.Ok())
257 int red
, green
, blue
;
258 if ( !GetColourFromGTKWidget(red
, green
, blue
,
268 gs_objects
.m_colBtnText
= wxColour( red
>> SHIFT
,
272 return gs_objects
.m_colBtnText
;
274 case wxSYS_COLOUR_INFOBK
:
275 if (!gs_objects
.m_colTooltip
.Ok()) {
278 return gs_objects
.m_colTooltip
;
280 case wxSYS_COLOUR_INFOTEXT
:
281 if (!gs_objects
.m_colTooltipText
.Ok()) {
284 return gs_objects
.m_colTooltipText
;
286 case wxSYS_COLOUR_HIGHLIGHTTEXT
:
287 if (!gs_objects
.m_colHighlightText
.Ok())
289 wxColour hclr
= GetColour(wxSYS_COLOUR_HIGHLIGHT
);
290 if (hclr
.Red() > 200 && hclr
.Green() > 200 && hclr
.Blue() > 200)
291 gs_objects
.m_colHighlightText
= wxColour(*wxBLACK
);
293 gs_objects
.m_colHighlightText
= wxColour(*wxWHITE
);
295 return gs_objects
.m_colHighlightText
;
297 case wxSYS_COLOUR_APPWORKSPACE
:
298 return *wxWHITE
; // ?
300 case wxSYS_COLOUR_ACTIVECAPTION
:
301 case wxSYS_COLOUR_MENUHILIGHT
:
302 if (!gs_objects
.m_colMenuItemHighlight
.Ok())
304 int red
, green
, blue
;
305 if ( !GetColourFromGTKWidget(red
, green
, blue
,
315 gs_objects
.m_colMenuItemHighlight
= wxColour( red
>> SHIFT
,
319 return gs_objects
.m_colMenuItemHighlight
;
321 case wxSYS_COLOUR_HOTLIGHT
:
322 case wxSYS_COLOUR_GRADIENTACTIVECAPTION
:
323 case wxSYS_COLOUR_GRADIENTINACTIVECAPTION
:
327 case wxSYS_COLOUR_MAX
:
329 wxFAIL_MSG( _T("unknown system colour index") );
335 wxFont
wxSystemSettingsNative::GetFont( wxSystemFont index
)
339 case wxSYS_OEM_FIXED_FONT
:
340 case wxSYS_ANSI_FIXED_FONT
:
341 case wxSYS_SYSTEM_FIXED_FONT
:
343 return *wxNORMAL_FONT
;
345 case wxSYS_ANSI_VAR_FONT
:
346 case wxSYS_SYSTEM_FONT
:
347 case wxSYS_DEVICE_DEFAULT_FONT
:
348 case wxSYS_DEFAULT_GUI_FONT
:
350 if (!gs_objects
.m_fontSystem
.Ok())
353 GtkWidget
*widget
= gtk_button_new();
354 GtkStyle
*def
= gtk_rc_get_style( widget
);
355 if ( !def
|| !def
->font_desc
)
356 def
= gtk_widget_get_default_style();
357 if ( def
&& def
->font_desc
)
359 wxNativeFontInfo info
;
361 pango_font_description_copy(def
->font_desc
);
362 gs_objects
.m_fontSystem
= wxFont(info
);
366 GtkSettings
*settings
= gtk_settings_get_default();
367 gchar
*font_name
= NULL
;
368 g_object_get ( settings
,
373 gs_objects
.m_fontSystem
= wxFont( 12, wxSWISS
, wxNORMAL
, wxNORMAL
);
375 gs_objects
.m_fontSystem
= wxFont(wxString::FromAscii(font_name
));
378 gtk_widget_destroy( widget
);
380 gs_objects
.m_fontSystem
= wxFont( 12, wxSWISS
, wxNORMAL
, wxNORMAL
);
383 return gs_objects
.m_fontSystem
;
391 int wxSystemSettingsNative::GetMetric( wxSystemMetric index
, wxWindow
* win
)
394 bool success
= false;
397 GdkWindow
*window
= NULL
;
398 if(win
&& GTK_WIDGET_REALIZED(win
->GetHandle()))
399 window
= win
->GetHandle()->window
;
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.
415 wxTopLevelWindow
*tlw
= wxDynamicCast(win
, wxTopLevelWindow
);
417 return -1; // not a tlw, not sure how to approach
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))
425 if (!gdk_x11_screen_supports_net_wm_hint(
426 gdk_drawable_get_screen(window
),
427 gdk_atom_intern("_NET_FRAME_EXTENTS", false) ) )
433 if (!gdk_net_wm_supports(gdk_atom_intern("_NET_FRAME_EXTENTS", false)))
437 // Get the frame extents from the windowmanager.
438 // In most cases the top extent is the titlebar, so we use the bottom extent
445 #if GTK_CHECK_VERSION(2,2,0)
446 if (!gtk_check_version(2,2,0))
449 success
= (XGetWindowProperty (GDK_DISPLAY_XDISPLAY(gdk_drawable_get_display(window
)),
450 GDK_WINDOW_XWINDOW(window
),
451 gdk_x11_get_xatom_by_name_for_display (
452 gdk_drawable_get_display(window
),
453 "_NET_FRAME_EXTENTS" ),
454 0, // left, right, top, bottom, CARDINAL[4]/32
455 G_MAXLONG
, // size of long
456 false, // do not delete property
457 XA_CARDINAL
, // 32 bit
458 &type
, &format
, &nitems
, &bytes_after
, &data
464 int border_return
= -1;
466 if ((type
== XA_CARDINAL
) && (format
== 32) && (nitems
>= 4) && (data
))
469 borders
= (long*)data
;
474 case wxSYS_FRAMESIZE_X
:
475 border_return
= borders
[1]; // width of right extent
478 border_return
= borders
[3]; // height of bottom extent
486 return border_return
;
491 return -1; // no window specified
497 if (!gtk_check_version(2,4,0))
500 return gdk_display_get_default_cursor_size(gdk_drawable_get_display(window
));
502 return gdk_display_get_default_cursor_size(gdk_display_get_default());
511 gint dclick_distance
;
512 #if GTK_CHECK_VERSION(2,2,0)
513 if (window
&& !gtk_check_version(2,2,0))
514 g_object_get(gtk_settings_get_for_screen(gdk_drawable_get_screen(window
)),
515 "gtk-double-click-distance", &dclick_distance
, NULL
);
518 g_object_get(gtk_settings_get_default(),
519 "gtk-double-click-distance", &dclick_distance
, NULL
);
521 return dclick_distance
* 2;
528 #if GTK_CHECK_VERSION(2,2,0)
529 if (window
&& !gtk_check_version(2,2,0))
532 gtk_settings_get_for_screen(gdk_drawable_get_screen(window
)),
533 "gtk-dnd-drag-threshold",
534 &drag_threshold
, NULL
);
539 g_object_get(gtk_settings_get_default(),
540 "gtk-dnd-drag-threshold", &drag_threshold
, NULL
);
543 return drag_threshold
* 2;
546 // MBN: ditto for icons
547 case wxSYS_ICON_X
: return 32;
548 case wxSYS_ICON_Y
: return 32;
551 #if defined(__WXGTK20__) && GTK_CHECK_VERSION(2,2,0)
552 if (window
&& !gtk_check_version(2,2,0))
553 return gdk_screen_get_width(gdk_drawable_get_screen(window
));
556 return gdk_screen_width();
559 #if defined(__WXGTK20__) && GTK_CHECK_VERSION(2,2,0)
560 if (window
&& !gtk_check_version(2,2,0))
561 return gdk_screen_get_height(gdk_drawable_get_screen(window
));
564 return gdk_screen_height();
566 case wxSYS_HSCROLL_Y
: return 15;
567 case wxSYS_VSCROLL_X
: return 15;
569 // a gtk1 implementation should be possible too if gtk2 efficiency/convenience functions aren't used
571 case wxSYS_CAPTION_Y
:
573 // No realized window specified, and no implementation for that case yet.
576 // Check if wm supports frame extents - we can't know the caption height if it does not.
577 #if GTK_CHECK_VERSION(2,2,0)
578 if (!gtk_check_version(2,2,0))
580 if (!gdk_x11_screen_supports_net_wm_hint(
581 gdk_drawable_get_screen(window
),
582 gdk_atom_intern("_NET_FRAME_EXTENTS", false) ) )
588 if (!gdk_net_wm_supports(gdk_atom_intern("_NET_FRAME_EXTENTS", false)))
592 wxASSERT_MSG( wxDynamicCast(win
, wxTopLevelWindow
),
593 wxT("Asking for caption height of a non toplevel window") );
595 // Get the height of the top windowmanager border.
596 // This is the titlebar in most cases. The titlebar might be elsewhere, and
597 // we could check which is the thickest wm border to decide on which side the
598 // titlebar is, but this might lead to interesting behaviours in used code.
599 // Reconsider when we have a way to report to the user on which side it is.
605 #if GTK_CHECK_VERSION(2,2,0)
606 if (!gtk_check_version(2,2,0))
609 success
= (XGetWindowProperty (GDK_DISPLAY_XDISPLAY(gdk_drawable_get_display(window
)),
610 GDK_WINDOW_XWINDOW(window
),
611 gdk_x11_get_xatom_by_name_for_display (
612 gdk_drawable_get_display(window
),
613 "_NET_FRAME_EXTENTS" ),
614 0, // left, right, top, bottom, CARDINAL[4]/32
615 G_MAXLONG
, // size of long
616 false, // do not delete property
617 XA_CARDINAL
, // 32 bit
618 &type
, &format
, &nitems
, &bytes_after
, &data
624 int caption_height
= -1;
626 if ((type
== XA_CARDINAL
) && (format
== 32) && (nitems
>= 3) && (data
))
629 borders
= (long*)data
;
630 caption_height
= borders
[2]; // top frame extent
636 return caption_height
;
639 // Try a default approach without a window pointer, if possible
645 case wxSYS_PENWINDOWS_PRESENT
:
646 // No MS Windows for Pen computing extension available in X11 based gtk+.
650 return -1; // metric is unknown
654 bool wxSystemSettingsNative::HasFeature(wxSystemFeature index
)
658 case wxSYS_CAN_ICONIZE_FRAME
:
661 case wxSYS_CAN_DRAW_FRAME_DECORATIONS
: