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