Move wxColourData and wxFontData into separate files.
[wxWidgets.git] / src / gtk1 / settings.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/gtk1/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/toplevel.h"
18 #endif
19
20 #include "wx/fontutil.h"
21
22 #include <gdk/gdk.h>
23 #include <gdk/gdkx.h>
24 #include <gdk/gdkprivate.h>
25 #include <gtk/gtk.h>
26
27 #include <X11/Xatom.h>
28
29 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
30
31 // ----------------------------------------------------------------------------
32 // wxSystemObjects
33 // ----------------------------------------------------------------------------
34
35 struct wxSystemObjects
36 {
37 wxColour m_colBtnFace,
38 m_colBtnShadow,
39 m_colBtnHighlight,
40 m_colHighlight,
41 m_colHighlightText,
42 m_colListBox,
43 m_colBtnText,
44 m_colMenuItemHighlight,
45 m_colTooltip,
46 m_colTooltipText;
47
48 wxFont m_fontSystem;
49 };
50
51 static wxSystemObjects gs_objects;
52
53 // ----------------------------------------------------------------------------
54 // wxSystemSettings implementation
55 // ----------------------------------------------------------------------------
56
57 // kind of widget to use in GetColourFromGTKWidget
58 enum wxGtkWidgetType
59 {
60 wxGTK_BUTTON,
61 wxGTK_LIST,
62 wxGTK_MENUITEM
63 };
64
65 // the colour we need
66 enum wxGtkColourType
67 {
68 wxGTK_FG,
69 wxGTK_BG,
70 wxGTK_BASE
71 };
72
73 // wxSystemSettings::GetColour() helper: get the colours from a GTK+
74 // widget style, return true if we did get them, false to use defaults
75 static bool GetColourFromGTKWidget(int& red, int& green, int& blue,
76 wxGtkWidgetType type = wxGTK_BUTTON,
77 GtkStateType state = GTK_STATE_NORMAL,
78 wxGtkColourType colour = wxGTK_BG)
79 {
80 GtkWidget *widget;
81 switch ( type )
82 {
83 default:
84 wxFAIL_MSG( wxT("unexpected GTK widget type") );
85 // fall through
86
87 case wxGTK_BUTTON:
88 widget = gtk_button_new();
89 break;
90
91 case wxGTK_LIST:
92 widget = gtk_list_new();
93 break;
94
95 case wxGTK_MENUITEM:
96 widget = gtk_menu_item_new();
97 }
98
99 GtkStyle *def = gtk_rc_get_style( widget );
100 if ( !def )
101 def = gtk_widget_get_default_style();
102
103 bool ok;
104 if ( def )
105 {
106 GdkColor *col;
107 switch ( colour )
108 {
109 default:
110 wxFAIL_MSG( wxT("unexpected GTK colour type") );
111 // fall through
112
113 case wxGTK_FG:
114 col = def->fg;
115 break;
116
117 case wxGTK_BG:
118 col = def->bg;
119 break;
120
121 case wxGTK_BASE:
122 col = def->base;
123 break;
124 }
125
126 red = col[state].red;
127 green = col[state].green;
128 blue = col[state].blue;
129
130 ok = true;
131 }
132 else
133 {
134 ok = false;
135 }
136
137 gtk_widget_destroy( widget );
138
139 return ok;
140 }
141
142 static void GetTooltipColors()
143 {
144 GtkTooltips* tooltips = gtk_tooltips_new();
145 gtk_tooltips_force_window(tooltips);
146 gtk_widget_ensure_style(tooltips->tip_window);
147 GdkColor c = tooltips->tip_window->style->bg[GTK_STATE_NORMAL];
148 gs_objects.m_colTooltip = wxColor(c.red >> SHIFT, c.green >> SHIFT, c.blue >> SHIFT);
149 c = tooltips->tip_window->style->fg[GTK_STATE_NORMAL];
150 gs_objects.m_colTooltipText = wxColor(c.red >> SHIFT, c.green >> SHIFT, c.blue >> SHIFT);
151 gtk_object_sink(reinterpret_cast<GtkObject*>(tooltips));
152 }
153
154 wxColour wxSystemSettingsNative::GetColour( wxSystemColour index )
155 {
156 switch (index)
157 {
158 case wxSYS_COLOUR_SCROLLBAR:
159 case wxSYS_COLOUR_BACKGROUND:
160 case wxSYS_COLOUR_INACTIVECAPTION:
161 case wxSYS_COLOUR_MENU:
162 case wxSYS_COLOUR_WINDOWFRAME:
163 case wxSYS_COLOUR_ACTIVEBORDER:
164 case wxSYS_COLOUR_INACTIVEBORDER:
165 case wxSYS_COLOUR_BTNFACE:
166 case wxSYS_COLOUR_MENUBAR:
167 case wxSYS_COLOUR_3DLIGHT:
168 if (!gs_objects.m_colBtnFace.Ok())
169 {
170 int red, green, blue;
171 if ( !GetColourFromGTKWidget(red, green, blue) )
172 {
173 red =
174 green = 0;
175 blue = 0x9c40;
176 }
177
178 gs_objects.m_colBtnFace = wxColour( red >> SHIFT,
179 green >> SHIFT,
180 blue >> SHIFT );
181 }
182 return gs_objects.m_colBtnFace;
183
184 case wxSYS_COLOUR_WINDOW:
185 return *wxWHITE;
186
187 case wxSYS_COLOUR_3DDKSHADOW:
188 return *wxBLACK;
189
190 case wxSYS_COLOUR_GRAYTEXT:
191 case wxSYS_COLOUR_BTNSHADOW:
192 //case wxSYS_COLOUR_3DSHADOW:
193 if (!gs_objects.m_colBtnShadow.Ok())
194 {
195 wxColour faceColour(GetColour(wxSYS_COLOUR_3DFACE));
196 gs_objects.m_colBtnShadow =
197 wxColour((unsigned char) (faceColour.Red() * 0.666),
198 (unsigned char) (faceColour.Green() * 0.666),
199 (unsigned char) (faceColour.Blue() * 0.666));
200 }
201
202 return gs_objects.m_colBtnShadow;
203
204 case wxSYS_COLOUR_3DHIGHLIGHT:
205 //case wxSYS_COLOUR_BTNHIGHLIGHT:
206 return * wxWHITE;
207
208 case wxSYS_COLOUR_HIGHLIGHT:
209 if (!gs_objects.m_colHighlight.Ok())
210 {
211 int red, green, blue;
212 if ( !GetColourFromGTKWidget(red, green, blue,
213 wxGTK_BUTTON,
214 GTK_STATE_SELECTED) )
215 {
216 red =
217 green = 0;
218 blue = 0x9c40;
219 }
220
221 gs_objects.m_colHighlight = wxColour( red >> SHIFT,
222 green >> SHIFT,
223 blue >> SHIFT );
224 }
225 return gs_objects.m_colHighlight;
226
227 case wxSYS_COLOUR_LISTBOX:
228 if (!gs_objects.m_colListBox.Ok())
229 {
230 int red, green, blue;
231 if ( GetColourFromGTKWidget(red, green, blue,
232 wxGTK_LIST,
233 GTK_STATE_NORMAL,
234 wxGTK_BASE) )
235 {
236 gs_objects.m_colListBox = wxColour( red >> SHIFT,
237 green >> SHIFT,
238 blue >> SHIFT );
239 }
240 else
241 {
242 gs_objects.m_colListBox = wxColour(*wxWHITE);
243 }
244 }
245 return gs_objects.m_colListBox;
246
247 case wxSYS_COLOUR_MENUTEXT:
248 case wxSYS_COLOUR_WINDOWTEXT:
249 case wxSYS_COLOUR_CAPTIONTEXT:
250 case wxSYS_COLOUR_INACTIVECAPTIONTEXT:
251 case wxSYS_COLOUR_BTNTEXT:
252 case wxSYS_COLOUR_LISTBOXTEXT:
253 if (!gs_objects.m_colBtnText.Ok())
254 {
255 int red, green, blue;
256 if ( !GetColourFromGTKWidget(red, green, blue,
257 wxGTK_BUTTON,
258 GTK_STATE_NORMAL,
259 wxGTK_FG) )
260 {
261 red =
262 green =
263 blue = 0;
264 }
265
266 gs_objects.m_colBtnText = wxColour( red >> SHIFT,
267 green >> SHIFT,
268 blue >> SHIFT );
269 }
270 return gs_objects.m_colBtnText;
271
272 case wxSYS_COLOUR_INFOBK:
273 if (!gs_objects.m_colTooltip.Ok()) {
274 GetTooltipColors();
275 }
276 return gs_objects.m_colTooltip;
277
278 case wxSYS_COLOUR_INFOTEXT:
279 if (!gs_objects.m_colTooltipText.Ok()) {
280 GetTooltipColors();
281 }
282 return gs_objects.m_colTooltipText;
283
284 case wxSYS_COLOUR_HIGHLIGHTTEXT:
285 if (!gs_objects.m_colHighlightText.Ok())
286 {
287 wxColour hclr = GetColour(wxSYS_COLOUR_HIGHLIGHT);
288 if (hclr.Red() > 200 && hclr.Green() > 200 && hclr.Blue() > 200)
289 gs_objects.m_colHighlightText = wxColour(*wxBLACK);
290 else
291 gs_objects.m_colHighlightText = wxColour(*wxWHITE);
292 }
293 return gs_objects.m_colHighlightText;
294
295 case wxSYS_COLOUR_APPWORKSPACE:
296 return *wxWHITE; // ?
297
298 case wxSYS_COLOUR_ACTIVECAPTION:
299 case wxSYS_COLOUR_MENUHILIGHT:
300 if (!gs_objects.m_colMenuItemHighlight.Ok())
301 {
302 int red, green, blue;
303 if ( !GetColourFromGTKWidget(red, green, blue,
304 wxGTK_MENUITEM,
305 GTK_STATE_SELECTED,
306 wxGTK_BG) )
307 {
308 red =
309 green =
310 blue = 0;
311 }
312
313 gs_objects.m_colMenuItemHighlight = wxColour( red >> SHIFT,
314 green >> SHIFT,
315 blue >> SHIFT );
316 }
317 return gs_objects.m_colMenuItemHighlight;
318
319 case wxSYS_COLOUR_HOTLIGHT:
320 case wxSYS_COLOUR_GRADIENTACTIVECAPTION:
321 case wxSYS_COLOUR_GRADIENTINACTIVECAPTION:
322 // TODO
323 return *wxBLACK;
324
325 case wxSYS_COLOUR_MAX:
326 default:
327 wxFAIL_MSG( wxT("unknown system colour index") );
328 }
329
330 return *wxWHITE;
331 }
332
333 wxFont wxSystemSettingsNative::GetFont( wxSystemFont index )
334 {
335 switch (index)
336 {
337 case wxSYS_OEM_FIXED_FONT:
338 case wxSYS_ANSI_FIXED_FONT:
339 case wxSYS_SYSTEM_FIXED_FONT:
340 {
341 return *wxNORMAL_FONT;
342 }
343 case wxSYS_ANSI_VAR_FONT:
344 case wxSYS_SYSTEM_FONT:
345 case wxSYS_DEVICE_DEFAULT_FONT:
346 case wxSYS_DEFAULT_GUI_FONT:
347 {
348 if (!gs_objects.m_fontSystem.Ok())
349 {
350 gs_objects.m_fontSystem = wxFont( 12, wxSWISS, wxNORMAL, wxNORMAL );
351 }
352 return gs_objects.m_fontSystem;
353 }
354
355 default:
356 return wxNullFont;
357 }
358 }
359
360 int
361 wxSystemSettingsNative::GetMetric(wxSystemMetric index, wxWindow* WXUNUSED(win))
362 {
363 switch (index)
364 {
365 case wxSYS_CURSOR_X:
366 case wxSYS_CURSOR_Y:
367 return 16;
368
369 // MBN: ditto for icons
370 case wxSYS_ICON_X: return 32;
371 case wxSYS_ICON_Y: return 32;
372
373 case wxSYS_SCREEN_X:
374 return gdk_screen_width();
375
376 case wxSYS_SCREEN_Y:
377 return gdk_screen_height();
378
379 case wxSYS_HSCROLL_Y: return 15;
380 case wxSYS_VSCROLL_X: return 15;
381
382 // a gtk1 implementation should be possible too if gtk2 efficiency/convenience functions aren't used
383 #if 0
384 case wxSYS_CAPTION_Y:
385 if (!window)
386 // No realized window specified, and no implementation for that case yet.
387 return -1;
388
389 // Check if wm supports frame extents - we can't know the caption height if it does not.
390 #if GTK_CHECK_VERSION(2,2,0)
391 if (!gtk_check_version(2,2,0))
392 {
393 if (!gdk_x11_screen_supports_net_wm_hint(
394 gdk_drawable_get_screen(window),
395 gdk_atom_intern("_NET_FRAME_EXTENTS", false) ) )
396 return -1;
397 }
398 else
399 #endif
400 {
401 if (!gdk_net_wm_supports(gdk_atom_intern("_NET_FRAME_EXTENTS", false)))
402 return -1;
403 }
404
405 wxASSERT_MSG( wxDynamicCast(win, wxTopLevelWindow),
406 wxT("Asking for caption height of a non toplevel window") );
407
408 // Get the height of the top windowmanager border.
409 // This is the titlebar in most cases. The titlebar might be elsewhere, and
410 // we could check which is the thickest wm border to decide on which side the
411 // titlebar is, but this might lead to interesting behaviours in used code.
412 // Reconsider when we have a way to report to the user on which side it is.
413
414 Atom type;
415 gint format;
416 gulong nitems;
417
418 #if GTK_CHECK_VERSION(2,2,0)
419 if (!gtk_check_version(2,2,0))
420 {
421 gulong bytes_after;
422 success = (XGetWindowProperty (GDK_DISPLAY_XDISPLAY(gdk_drawable_get_display(window)),
423 GDK_WINDOW_XWINDOW(window),
424 gdk_x11_get_xatom_by_name_for_display (
425 gdk_drawable_get_display(window),
426 "_NET_FRAME_EXTENTS" ),
427 0, // left, right, top, bottom, CARDINAL[4]/32
428 G_MAXLONG, // size of long
429 false, // do not delete property
430 XA_CARDINAL, // 32 bit
431 &type, &format, &nitems, &bytes_after, &data
432 ) == Success);
433 }
434 #endif
435 if (success)
436 {
437 int caption_height = -1;
438
439 if ((type == XA_CARDINAL) && (format == 32) && (nitems >= 3) && (data))
440 {
441 long *borders;
442 borders = (long*)data;
443 caption_height = borders[2]; // top frame extent
444 }
445
446 if (data)
447 XFree(data);
448
449 return caption_height;
450 }
451
452 // Try a default approach without a window pointer, if possible
453 // ...
454
455 return -1;
456 #endif // gtk2
457
458 case wxSYS_PENWINDOWS_PRESENT:
459 // No MS Windows for Pen computing extension available in X11 based gtk+.
460 return 0;
461
462 default:
463 return -1; // metric is unknown
464 }
465 }
466
467 bool wxSystemSettingsNative::HasFeature(wxSystemFeature index)
468 {
469 switch (index)
470 {
471 case wxSYS_CAN_ICONIZE_FRAME:
472 return false;
473
474 case wxSYS_CAN_DRAW_FRAME_DECORATIONS:
475 return true;
476
477 default:
478 return false;
479 }
480 }