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
;
52 static wxSystemObjects gs_objects
;
54 // ----------------------------------------------------------------------------
55 // wxSystemSettings implementation
56 // ----------------------------------------------------------------------------
58 // kind of widget to use in GetColourFromGTKWidget
74 // wxSystemSettings::GetColour() helper: get the colours from a GTK+
75 // widget style, return true if we did get them, false to use defaults
76 static bool GetColourFromGTKWidget(int& red
, int& green
, int& blue
,
77 wxGtkWidgetType type
= wxGTK_BUTTON
,
78 GtkStateType state
= GTK_STATE_NORMAL
,
79 wxGtkColourType colour
= wxGTK_BG
)
85 wxFAIL_MSG( _T("unexpected GTK widget type") );
89 widget
= gtk_button_new();
93 widget
= gtk_list_new();
96 widget
= gtk_menu_item_new();
99 GtkStyle
*def
= gtk_rc_get_style( widget
);
101 def
= gtk_widget_get_default_style();
110 wxFAIL_MSG( _T("unexpected GTK colour type") );
126 red
= col
[state
].red
;
127 green
= col
[state
].green
;
128 blue
= col
[state
].blue
;
137 gtk_widget_destroy( widget
);
142 wxColour
wxSystemSettingsNative::GetColour( wxSystemColour index
)
146 case wxSYS_COLOUR_SCROLLBAR
:
147 case wxSYS_COLOUR_BACKGROUND
:
148 case wxSYS_COLOUR_INACTIVECAPTION
:
149 case wxSYS_COLOUR_MENU
:
150 case wxSYS_COLOUR_WINDOWFRAME
:
151 case wxSYS_COLOUR_ACTIVEBORDER
:
152 case wxSYS_COLOUR_INACTIVEBORDER
:
153 case wxSYS_COLOUR_BTNFACE
:
154 case wxSYS_COLOUR_MENUBAR
:
155 case wxSYS_COLOUR_3DLIGHT
:
156 if (!gs_objects
.m_colBtnFace
.Ok())
158 int red
, green
, blue
;
159 if ( !GetColourFromGTKWidget(red
, green
, blue
) )
166 gs_objects
.m_colBtnFace
= wxColour( red
>> SHIFT
,
170 return gs_objects
.m_colBtnFace
;
172 case wxSYS_COLOUR_WINDOW
:
175 case wxSYS_COLOUR_3DDKSHADOW
:
178 case wxSYS_COLOUR_GRAYTEXT
:
179 case wxSYS_COLOUR_BTNSHADOW
:
180 //case wxSYS_COLOUR_3DSHADOW:
181 if (!gs_objects
.m_colBtnShadow
.Ok())
183 wxColour
faceColour(GetColour(wxSYS_COLOUR_3DFACE
));
184 gs_objects
.m_colBtnShadow
=
185 wxColour((unsigned char) (faceColour
.Red() * 0.666),
186 (unsigned char) (faceColour
.Green() * 0.666),
187 (unsigned char) (faceColour
.Blue() * 0.666));
190 return gs_objects
.m_colBtnShadow
;
192 case wxSYS_COLOUR_3DHIGHLIGHT
:
193 //case wxSYS_COLOUR_BTNHIGHLIGHT:
196 case wxSYS_COLOUR_HIGHLIGHT
:
197 if (!gs_objects
.m_colHighlight
.Ok())
199 int red
, green
, blue
;
200 if ( !GetColourFromGTKWidget(red
, green
, blue
,
202 GTK_STATE_SELECTED
) )
209 gs_objects
.m_colHighlight
= wxColour( red
>> SHIFT
,
213 return gs_objects
.m_colHighlight
;
215 case wxSYS_COLOUR_LISTBOX
:
216 if (!gs_objects
.m_colListBox
.Ok())
218 int red
, green
, blue
;
219 if ( GetColourFromGTKWidget(red
, green
, blue
,
224 gs_objects
.m_colListBox
= wxColour( red
>> SHIFT
,
230 gs_objects
.m_colListBox
= wxColour(*wxWHITE
);
233 return gs_objects
.m_colListBox
;
235 case wxSYS_COLOUR_MENUTEXT
:
236 case wxSYS_COLOUR_WINDOWTEXT
:
237 case wxSYS_COLOUR_CAPTIONTEXT
:
238 case wxSYS_COLOUR_INACTIVECAPTIONTEXT
:
239 case wxSYS_COLOUR_BTNTEXT
:
240 case wxSYS_COLOUR_INFOTEXT
:
241 if (!gs_objects
.m_colBtnText
.Ok())
243 int red
, green
, blue
;
244 if ( !GetColourFromGTKWidget(red
, green
, blue
,
254 gs_objects
.m_colBtnText
= wxColour( red
>> SHIFT
,
258 return gs_objects
.m_colBtnText
;
260 // this (as well as wxSYS_COLOUR_INFOTEXT above) is used for
261 // tooltip windows - Robert, please change this code to use the
262 // real GTK tooltips when/if you can (TODO)
263 case wxSYS_COLOUR_INFOBK
:
264 return wxColour(255, 255, 225);
266 case wxSYS_COLOUR_HIGHLIGHTTEXT
:
267 if (!gs_objects
.m_colHighlightText
.Ok())
269 wxColour hclr
= GetColour(wxSYS_COLOUR_HIGHLIGHT
);
270 if (hclr
.Red() > 200 && hclr
.Green() > 200 && hclr
.Blue() > 200)
271 gs_objects
.m_colHighlightText
= wxColour(*wxBLACK
);
273 gs_objects
.m_colHighlightText
= wxColour(*wxWHITE
);
275 return gs_objects
.m_colHighlightText
;
277 case wxSYS_COLOUR_APPWORKSPACE
:
278 return *wxWHITE
; // ?
280 case wxSYS_COLOUR_ACTIVECAPTION
:
281 case wxSYS_COLOUR_MENUHILIGHT
:
282 if (!gs_objects
.m_colMenuItemHighlight
.Ok())
284 int red
, green
, blue
;
285 if ( !GetColourFromGTKWidget(red
, green
, blue
,
295 gs_objects
.m_colMenuItemHighlight
= wxColour( red
>> SHIFT
,
299 return gs_objects
.m_colMenuItemHighlight
;
301 case wxSYS_COLOUR_HOTLIGHT
:
302 case wxSYS_COLOUR_GRADIENTACTIVECAPTION
:
303 case wxSYS_COLOUR_GRADIENTINACTIVECAPTION
:
307 case wxSYS_COLOUR_MAX
:
309 wxFAIL_MSG( _T("unknown system colour index") );
315 wxFont
wxSystemSettingsNative::GetFont( wxSystemFont index
)
319 case wxSYS_OEM_FIXED_FONT
:
320 case wxSYS_ANSI_FIXED_FONT
:
321 case wxSYS_SYSTEM_FIXED_FONT
:
323 return *wxNORMAL_FONT
;
325 case wxSYS_ANSI_VAR_FONT
:
326 case wxSYS_SYSTEM_FONT
:
327 case wxSYS_DEVICE_DEFAULT_FONT
:
328 case wxSYS_DEFAULT_GUI_FONT
:
330 if (!gs_objects
.m_fontSystem
.Ok())
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
)
339 wxNativeFontInfo info
;
341 pango_font_description_copy(def
->font_desc
);
342 gs_objects
.m_fontSystem
= wxFont(info
);
346 GtkSettings
*settings
= gtk_settings_get_default();
347 gchar
*font_name
= NULL
;
348 g_object_get ( settings
,
353 gs_objects
.m_fontSystem
= wxFont( 12, wxSWISS
, wxNORMAL
, wxNORMAL
);
355 gs_objects
.m_fontSystem
= wxFont(wxString::FromAscii(font_name
));
358 gtk_widget_destroy( widget
);
360 gs_objects
.m_fontSystem
= wxFont( 12, wxSWISS
, wxNORMAL
, wxNORMAL
);
363 return gs_objects
.m_fontSystem
;
371 int wxSystemSettingsNative::GetMetric( wxSystemMetric index
, wxWindow
* win
)
374 bool success
= false;
377 GdkWindow
*window
= NULL
;
378 if(win
&& GTK_WIDGET_REALIZED(win
->GetHandle()))
379 window
= win
->GetHandle()->window
;
389 case wxSYS_FRAMESIZE_X
:
390 case wxSYS_FRAMESIZE_Y
:
391 // If a window is specified/realized, and it is a toplevel window, we can query from wm.
392 // The returned border thickness is outside the client area in that case.
395 wxTopLevelWindow
*tlw
= wxDynamicCast(win
, wxTopLevelWindow
);
397 return -1; // not a tlw, not sure how to approach
400 // Check if wm supports frame extents - we can't know
401 // the border widths if it does not.
402 #if GTK_CHECK_VERSION(2,2,0)
403 if (!gtk_check_version(2,2,0))
405 if (!gdk_x11_screen_supports_net_wm_hint(
406 gdk_drawable_get_screen(window
),
407 gdk_atom_intern("_NET_FRAME_EXTENTS", false) ) )
413 if (!gdk_net_wm_supports(gdk_atom_intern("_NET_FRAME_EXTENTS", false)))
417 // Get the frame extents from the windowmanager.
418 // In most cases the top extent is the titlebar, so we use the bottom extent
425 #if GTK_CHECK_VERSION(2,2,0)
426 if (!gtk_check_version(2,2,0))
429 success
= (XGetWindowProperty (GDK_DISPLAY_XDISPLAY(gdk_drawable_get_display(window
)),
430 GDK_WINDOW_XWINDOW(window
),
431 gdk_x11_get_xatom_by_name_for_display (
432 gdk_drawable_get_display(window
),
433 "_NET_FRAME_EXTENTS" ),
434 0, // left, right, top, bottom, CARDINAL[4]/32
435 G_MAXLONG
, // size of long
436 false, // do not delete property
437 XA_CARDINAL
, // 32 bit
438 &type
, &format
, &nitems
, &bytes_after
, &data
444 int border_return
= -1;
446 if ((type
== XA_CARDINAL
) && (format
== 32) && (nitems
>= 4) && (data
))
449 borders
= (long*)data
;
454 case wxSYS_FRAMESIZE_X
:
455 border_return
= borders
[1]; // width of right extent
458 border_return
= borders
[3]; // height of bottom extent
466 return border_return
;
471 return -1; // no window specified
477 if (!gtk_check_version(2,4,0))
480 return gdk_display_get_default_cursor_size(gdk_drawable_get_display(window
));
482 return gdk_display_get_default_cursor_size(gdk_display_get_default());
491 gint dclick_distance
;
492 #if GTK_CHECK_VERSION(2,2,0)
493 if (window
&& !gtk_check_version(2,2,0))
494 g_object_get(gtk_settings_get_for_screen(gdk_drawable_get_screen(window
)),
495 "gtk-double-click-distance", &dclick_distance
, NULL
);
498 g_object_get(gtk_settings_get_default(),
499 "gtk-double-click-distance", &dclick_distance
, NULL
);
501 return dclick_distance
* 2;
508 #if GTK_CHECK_VERSION(2,2,0)
509 if (window
&& !gtk_check_version(2,2,0))
512 gtk_settings_get_for_screen(gdk_drawable_get_screen(window
)),
513 "gtk-dnd-drag-threshold",
514 &drag_threshold
, NULL
);
519 g_object_get(gtk_settings_get_default(),
520 "gtk-dnd-drag-threshold", &drag_threshold
, NULL
);
523 return drag_threshold
* 2;
526 // MBN: ditto for icons
527 case wxSYS_ICON_X
: return 32;
528 case wxSYS_ICON_Y
: return 32;
531 #if defined(__WXGTK20__) && GTK_CHECK_VERSION(2,2,0)
532 if (window
&& !gtk_check_version(2,2,0))
533 return gdk_screen_get_width(gdk_drawable_get_screen(window
));
536 return gdk_screen_width();
539 #if defined(__WXGTK20__) && GTK_CHECK_VERSION(2,2,0)
540 if (window
&& !gtk_check_version(2,2,0))
541 return gdk_screen_get_height(gdk_drawable_get_screen(window
));
544 return gdk_screen_height();
546 case wxSYS_HSCROLL_Y
: return 15;
547 case wxSYS_VSCROLL_X
: return 15;
549 // a gtk1 implementation should be possible too if gtk2 efficiency/convenience functions aren't used
551 case wxSYS_CAPTION_Y
:
553 // No realized window specified, and no implementation for that case yet.
556 // Check if wm supports frame extents - we can't know the caption height if it does not.
557 #if GTK_CHECK_VERSION(2,2,0)
558 if (!gtk_check_version(2,2,0))
560 if (!gdk_x11_screen_supports_net_wm_hint(
561 gdk_drawable_get_screen(window
),
562 gdk_atom_intern("_NET_FRAME_EXTENTS", false) ) )
568 if (!gdk_net_wm_supports(gdk_atom_intern("_NET_FRAME_EXTENTS", false)))
572 wxASSERT_MSG( wxDynamicCast(win
, wxTopLevelWindow
),
573 wxT("Asking for caption height of a non toplevel window") );
575 // Get the height of the top windowmanager border.
576 // This is the titlebar in most cases. The titlebar might be elsewhere, and
577 // we could check which is the thickest wm border to decide on which side the
578 // titlebar is, but this might lead to interesting behaviours in used code.
579 // Reconsider when we have a way to report to the user on which side it is.
585 #if GTK_CHECK_VERSION(2,2,0)
586 if (!gtk_check_version(2,2,0))
589 success
= (XGetWindowProperty (GDK_DISPLAY_XDISPLAY(gdk_drawable_get_display(window
)),
590 GDK_WINDOW_XWINDOW(window
),
591 gdk_x11_get_xatom_by_name_for_display (
592 gdk_drawable_get_display(window
),
593 "_NET_FRAME_EXTENTS" ),
594 0, // left, right, top, bottom, CARDINAL[4]/32
595 G_MAXLONG
, // size of long
596 false, // do not delete property
597 XA_CARDINAL
, // 32 bit
598 &type
, &format
, &nitems
, &bytes_after
, &data
604 int caption_height
= -1;
606 if ((type
== XA_CARDINAL
) && (format
== 32) && (nitems
>= 3) && (data
))
609 borders
= (long*)data
;
610 caption_height
= borders
[2]; // top frame extent
616 return caption_height
;
619 // Try a default approach without a window pointer, if possible
625 case wxSYS_PENWINDOWS_PRESENT
:
626 // No MS Windows for Pen computing extension available in X11 based gtk+.
630 return -1; // metric is unknown
634 bool wxSystemSettingsNative::HasFeature(wxSystemFeature index
)
638 case wxSYS_CAN_ICONIZE_FRAME
:
641 case wxSYS_CAN_DRAW_FRAME_DECORATIONS
: