]> git.saurik.com Git - wxWidgets.git/blob - src/gtk/settings.cpp
Corrected some wxMac images
[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/gtk.h>
24
25 bool wxGetFrameExtents(GdkWindow* window, int* left, int* right, int* top, int* bottom);
26
27 // ----------------------------------------------------------------------------
28 // wxSystemSettings implementation
29 // ----------------------------------------------------------------------------
30
31 static wxFont gs_fontSystem;
32
33 static GtkContainer* ContainerWidget()
34 {
35 static GtkContainer* s_widget;
36 if (s_widget == NULL)
37 {
38 s_widget = GTK_CONTAINER(gtk_fixed_new());
39 GtkWidget* window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
40 gtk_container_add(GTK_CONTAINER(window), GTK_WIDGET(s_widget));
41 }
42 return s_widget;
43 }
44
45 extern "C" {
46 static void style_set(GtkWidget*, GtkStyle*, void*)
47 {
48 gs_fontSystem = wxNullFont;
49 }
50 }
51
52 static const GtkStyle* ButtonStyle()
53 {
54 static GtkWidget* s_widget;
55 if (s_widget == NULL)
56 {
57 s_widget = gtk_button_new();
58 gtk_container_add(ContainerWidget(), s_widget);
59 gtk_widget_ensure_style(s_widget);
60 g_signal_connect(s_widget, "style_set", G_CALLBACK(style_set), NULL);
61 }
62 return s_widget->style;
63 }
64
65 static const GtkStyle* ListStyle()
66 {
67 static GtkWidget* s_widget;
68 if (s_widget == NULL)
69 {
70 s_widget = gtk_tree_view_new_with_model(
71 GTK_TREE_MODEL(gtk_list_store_new(1, G_TYPE_INT)));
72 gtk_container_add(ContainerWidget(), s_widget);
73 gtk_widget_ensure_style(s_widget);
74 }
75 return s_widget->style;
76 }
77
78 static const GtkStyle* TextCtrlStyle()
79 {
80 static GtkWidget* s_widget;
81 if (s_widget == NULL)
82 {
83 s_widget = gtk_text_view_new();
84 gtk_container_add(ContainerWidget(), s_widget);
85 gtk_widget_ensure_style(s_widget);
86 }
87 return s_widget->style;
88 }
89
90 static const GtkStyle* MenuItemStyle()
91 {
92 static GtkWidget* s_widget;
93 if (s_widget == NULL)
94 {
95 s_widget = gtk_menu_item_new();
96 gtk_container_add(ContainerWidget(), s_widget);
97 gtk_widget_ensure_style(s_widget);
98 }
99 return s_widget->style;
100 }
101
102 static const GtkStyle* MenuBarStyle()
103 {
104 static GtkWidget* s_widget;
105 if (s_widget == NULL)
106 {
107 s_widget = gtk_menu_bar_new();
108 gtk_container_add(ContainerWidget(), s_widget);
109 gtk_widget_ensure_style(s_widget);
110 }
111 return s_widget->style;
112 }
113
114 static const GtkStyle* ToolTipStyle()
115 {
116 static GtkWidget* s_widget;
117 if (s_widget == NULL)
118 {
119 s_widget = gtk_window_new(GTK_WINDOW_POPUP);
120 const char* name = "gtk-tooltip";
121 if (gtk_check_version(2, 11, 0))
122 name = "gtk-tooltips";
123 gtk_widget_set_name(s_widget, name);
124 gtk_widget_ensure_style(s_widget);
125 }
126 return s_widget->style;
127 }
128
129 wxColour wxSystemSettingsNative::GetColour( wxSystemColour index )
130 {
131 wxColor color;
132 switch (index)
133 {
134 case wxSYS_COLOUR_SCROLLBAR:
135 case wxSYS_COLOUR_BACKGROUND:
136 //case wxSYS_COLOUR_DESKTOP:
137 case wxSYS_COLOUR_INACTIVECAPTION:
138 case wxSYS_COLOUR_MENU:
139 case wxSYS_COLOUR_WINDOWFRAME:
140 case wxSYS_COLOUR_ACTIVEBORDER:
141 case wxSYS_COLOUR_INACTIVEBORDER:
142 case wxSYS_COLOUR_BTNFACE:
143 //case wxSYS_COLOUR_3DFACE:
144 case wxSYS_COLOUR_3DLIGHT:
145 color = wxColor(ButtonStyle()->bg[GTK_STATE_NORMAL]);
146 break;
147
148 case wxSYS_COLOUR_WINDOW:
149 color = wxColor(TextCtrlStyle()->base[GTK_STATE_NORMAL]);
150 break;
151
152 case wxSYS_COLOUR_MENUBAR:
153 color = wxColor(MenuBarStyle()->bg[GTK_STATE_NORMAL]);
154 break;
155
156 case wxSYS_COLOUR_3DDKSHADOW:
157 color = *wxBLACK;
158 break;
159
160 case wxSYS_COLOUR_GRAYTEXT:
161 case wxSYS_COLOUR_BTNSHADOW:
162 //case wxSYS_COLOUR_3DSHADOW:
163 {
164 wxColour faceColour(GetColour(wxSYS_COLOUR_3DFACE));
165 color =
166 wxColour((unsigned char) (faceColour.Red() * 2 / 3),
167 (unsigned char) (faceColour.Green() * 2 / 3),
168 (unsigned char) (faceColour.Blue() * 2 / 3));
169 }
170 break;
171
172 case wxSYS_COLOUR_BTNHIGHLIGHT:
173 //case wxSYS_COLOUR_BTNHILIGHT:
174 //case wxSYS_COLOUR_3DHIGHLIGHT:
175 //case wxSYS_COLOUR_3DHILIGHT:
176 color = *wxWHITE;
177 break;
178
179 case wxSYS_COLOUR_HIGHLIGHT:
180 color = wxColor(ButtonStyle()->bg[GTK_STATE_SELECTED]);
181 break;
182
183 case wxSYS_COLOUR_LISTBOX:
184 color = wxColor(ListStyle()->base[GTK_STATE_NORMAL]);
185 break;
186
187 case wxSYS_COLOUR_MENUTEXT:
188 case wxSYS_COLOUR_WINDOWTEXT:
189 case wxSYS_COLOUR_CAPTIONTEXT:
190 case wxSYS_COLOUR_INACTIVECAPTIONTEXT:
191 case wxSYS_COLOUR_BTNTEXT:
192 color = wxColor(ButtonStyle()->fg[GTK_STATE_NORMAL]);
193 break;
194
195 case wxSYS_COLOUR_INFOBK:
196 color = wxColor(ToolTipStyle()->bg[GTK_STATE_NORMAL]);
197 break;
198
199 case wxSYS_COLOUR_INFOTEXT:
200 color = wxColor(ToolTipStyle()->fg[GTK_STATE_NORMAL]);
201 break;
202
203 case wxSYS_COLOUR_HIGHLIGHTTEXT:
204 color = wxColor(ButtonStyle()->fg[GTK_STATE_SELECTED]);
205 break;
206
207 case wxSYS_COLOUR_APPWORKSPACE:
208 color = *wxWHITE; // ?
209 break;
210
211 case wxSYS_COLOUR_ACTIVECAPTION:
212 case wxSYS_COLOUR_MENUHILIGHT:
213 color = wxColor(MenuItemStyle()->bg[GTK_STATE_SELECTED]);
214 break;
215
216 case wxSYS_COLOUR_HOTLIGHT:
217 case wxSYS_COLOUR_GRADIENTACTIVECAPTION:
218 case wxSYS_COLOUR_GRADIENTINACTIVECAPTION:
219 // TODO
220 color = *wxBLACK;
221 break;
222
223 case wxSYS_COLOUR_MAX:
224 default:
225 wxFAIL_MSG( _T("unknown system colour index") );
226 color = *wxWHITE;
227 break;
228 }
229
230 return color;
231 }
232
233 wxFont wxSystemSettingsNative::GetFont( wxSystemFont index )
234 {
235 wxFont font;
236 switch (index)
237 {
238 case wxSYS_OEM_FIXED_FONT:
239 case wxSYS_ANSI_FIXED_FONT:
240 case wxSYS_SYSTEM_FIXED_FONT:
241 font = *wxNORMAL_FONT;
242 break;
243
244 case wxSYS_ANSI_VAR_FONT:
245 case wxSYS_SYSTEM_FONT:
246 case wxSYS_DEVICE_DEFAULT_FONT:
247 case wxSYS_DEFAULT_GUI_FONT:
248 if (!gs_fontSystem.Ok())
249 {
250 wxNativeFontInfo info;
251 info.description = ButtonStyle()->font_desc;
252 gs_fontSystem = wxFont(info);
253 info.description = NULL;
254 }
255 font = gs_fontSystem;
256 break;
257
258 default:
259 break;
260 }
261 return font;
262 }
263
264 // helper: return the GtkSettings either for the screen the current window is
265 // on or for the default screen if window is NULL
266 static GtkSettings *GetSettingsForWindowScreen(GdkWindow *window)
267 {
268 return window ? gtk_settings_get_for_screen(gdk_drawable_get_screen(window))
269 : gtk_settings_get_default();
270 }
271
272 int wxSystemSettingsNative::GetMetric( wxSystemMetric index, wxWindow* win )
273 {
274 GdkWindow *window = NULL;
275 if(win && GTK_WIDGET_REALIZED(win->GetHandle()))
276 window = win->GetHandle()->window;
277
278 switch (index)
279 {
280 case wxSYS_BORDER_X:
281 case wxSYS_BORDER_Y:
282 case wxSYS_EDGE_X:
283 case wxSYS_EDGE_Y:
284 case wxSYS_FRAMESIZE_X:
285 case wxSYS_FRAMESIZE_Y:
286 // If a window is specified/realized, and it is a toplevel window, we can query from wm.
287 // The returned border thickness is outside the client area in that case.
288 if (window)
289 {
290 wxTopLevelWindow *tlw = wxDynamicCast(win, wxTopLevelWindow);
291 if (!tlw)
292 return -1; // not a tlw, not sure how to approach
293 else
294 {
295 // Get the frame extents from the windowmanager.
296 // In most cases the top extent is the titlebar, so we use the bottom extent
297 // for the heights.
298 int right, bottom;
299 if (wxGetFrameExtents(window, NULL, &right, NULL, &bottom))
300 {
301 switch (index)
302 {
303 case wxSYS_BORDER_X:
304 case wxSYS_EDGE_X:
305 case wxSYS_FRAMESIZE_X:
306 return right; // width of right extent
307 default:
308 return bottom; // height of bottom extent
309 }
310 }
311 }
312 }
313
314 return -1; // no window specified
315
316 case wxSYS_CURSOR_X:
317 case wxSYS_CURSOR_Y:
318 return gdk_display_get_default_cursor_size(
319 window ? gdk_drawable_get_display(window)
320 : gdk_display_get_default());
321
322 case wxSYS_DCLICK_X:
323 case wxSYS_DCLICK_Y:
324 gint dclick_distance;
325 g_object_get(GetSettingsForWindowScreen(window),
326 "gtk-double-click-distance", &dclick_distance, NULL);
327
328 return dclick_distance * 2;
329
330 case wxSYS_DCLICK_MSEC:
331 gint dclick;
332 g_object_get(GetSettingsForWindowScreen(window),
333 "gtk-double-click-time", &dclick, NULL);
334 return dclick;
335
336 case wxSYS_DRAG_X:
337 case wxSYS_DRAG_Y:
338 gint drag_threshold;
339 g_object_get(GetSettingsForWindowScreen(window),
340 "gtk-dnd-drag-threshold", &drag_threshold, NULL);
341
342 // The correct thing here would be to double the value
343 // since that is what the API wants. But the values
344 // are much bigger under GNOME than under Windows and
345 // just seem to much in many cases to be useful.
346 // drag_threshold *= 2;
347
348 return drag_threshold;
349
350 case wxSYS_ICON_X:
351 case wxSYS_ICON_Y:
352 return 32;
353
354 case wxSYS_SCREEN_X:
355 if (window)
356 return gdk_screen_get_width(gdk_drawable_get_screen(window));
357 else
358 return gdk_screen_width();
359
360 case wxSYS_SCREEN_Y:
361 if (window)
362 return gdk_screen_get_height(gdk_drawable_get_screen(window));
363 else
364 return gdk_screen_height();
365
366 case wxSYS_HSCROLL_Y:
367 case wxSYS_VSCROLL_X:
368 return 15;
369
370 case wxSYS_CAPTION_Y:
371 if (!window)
372 // No realized window specified, and no implementation for that case yet.
373 return -1;
374
375 wxASSERT_MSG( wxDynamicCast(win, wxTopLevelWindow),
376 wxT("Asking for caption height of a non toplevel window") );
377
378 // Get the height of the top windowmanager border.
379 // This is the titlebar in most cases. The titlebar might be elsewhere, and
380 // we could check which is the thickest wm border to decide on which side the
381 // titlebar is, but this might lead to interesting behaviours in used code.
382 // Reconsider when we have a way to report to the user on which side it is.
383 {
384 int top;
385 if (wxGetFrameExtents(window, NULL, NULL, &top, NULL))
386 {
387 return top; // top frame extent
388 }
389 }
390
391 // Try a default approach without a window pointer, if possible
392 // ...
393
394 return -1;
395
396 case wxSYS_PENWINDOWS_PRESENT:
397 // No MS Windows for Pen computing extension available in X11 based gtk+.
398 return 0;
399
400 default:
401 return -1; // metric is unknown
402 }
403 }
404
405 bool wxSystemSettingsNative::HasFeature(wxSystemFeature index)
406 {
407 switch (index)
408 {
409 case wxSYS_CAN_ICONIZE_FRAME:
410 return false;
411
412 case wxSYS_CAN_DRAW_FRAME_DECORATIONS:
413 return true;
414
415 default:
416 return false;
417 }
418 }