fix for wxSYS_COLOUR_LISTBOX colour detection
[wxWidgets.git] / src / gtk / settings.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: gtk/settings.cpp
3 // Purpose:
4 // Author: Robert Roebling
5 // Id: $Id$
6 // Copyright: (c) 1998 Robert Roebling
7 // Licence: wxWindows licence
8 /////////////////////////////////////////////////////////////////////////////
9
10
11 #ifdef __GNUG__
12 #pragma implementation "settings.h"
13 #endif
14
15 #include "wx/settings.h"
16 #include "wx/debug.h"
17
18 #include "wx/cmndata.h"
19
20 #include <gdk/gdk.h>
21 #include <gdk/gdkprivate.h>
22 #include <gtk/gtk.h>
23
24 extern GdkFont *GtkGetDefaultGuiFont();
25
26
27 /*
28 #define wxSYS_COLOUR_SCROLLBAR 0
29 #define wxSYS_COLOUR_BACKGROUND 1
30 #define wxSYS_COLOUR_ACTIVECAPTION 2
31 #define wxSYS_COLOUR_INACTIVECAPTION 3
32 #define wxSYS_COLOUR_MENU 4
33 #define wxSYS_COLOUR_WINDOW 5
34 #define wxSYS_COLOUR_WINDOWFRAME 6
35 #define wxSYS_COLOUR_MENUTEXT 7
36 #define wxSYS_COLOUR_WINDOWTEXT 8
37 #define wxSYS_COLOUR_CAPTIONTEXT 9
38 #define wxSYS_COLOUR_ACTIVEBORDER 10
39 #define wxSYS_COLOUR_INACTIVEBORDER 11
40 #define wxSYS_COLOUR_APPWORKSPACE 12
41 #define wxSYS_COLOUR_HIGHLIGHT 13
42 #define wxSYS_COLOUR_HIGHLIGHTTEXT 14
43 #define wxSYS_COLOUR_BTNFACE 15
44 #define wxSYS_COLOUR_BTNSHADOW 16
45 #define wxSYS_COLOUR_GRAYTEXT 17
46 #define wxSYS_COLOUR_BTNTEXT 18
47 #define wxSYS_COLOUR_INACTIVECAPTIONTEXT 19
48 #define wxSYS_COLOUR_BTNHIGHLIGHT 20
49
50 #define wxSYS_COLOUR_3DDKSHADOW 21
51 #define wxSYS_COLOUR_3DLIGHT 22
52 #define wxSYS_COLOUR_INFOTEXT 23
53 #define wxSYS_COLOUR_INFOBK 24
54
55 #define wxSYS_COLOUR_DESKTOP wxSYS_COLOUR_BACKGROUND
56 #define wxSYS_COLOUR_3DFACE wxSYS_COLOUR_BTNFACE
57 #define wxSYS_COLOUR_3DSHADOW wxSYS_COLOUR_BTNSHADOW
58 #define wxSYS_COLOUR_3DHIGHLIGHT wxSYS_COLOUR_BTNHIGHLIGHT
59 #define wxSYS_COLOUR_3DHILIGHT wxSYS_COLOUR_BTNHIGHLIGHT
60 #define wxSYS_COLOUR_BTNHILIGHT wxSYS_COLOUR_BTNHIGHLIGHT
61 */
62
63 #define SHIFT (8*(sizeof(short int)-sizeof(char)))
64
65 //wxColour *g_systemWinColour = (wxColour *) NULL;
66 wxColour *g_systemBtnFaceColour = (wxColour *) NULL;
67 wxColour *g_systemBtnShadowColour = (wxColour *) NULL;
68 wxColour *g_systemBtnHighlightColour = (wxColour *) NULL;
69 wxColour *g_systemHighlightColour = (wxColour *) NULL;
70 wxColour *g_systemHighlightTextColour = (wxColour *) NULL;
71 wxColour *g_systemListBoxColour = (wxColour *) NULL;
72 wxColour *g_systemBtnTextColour = (wxColour *) NULL;
73
74 wxFont *g_systemFont = (wxFont *) NULL;
75
76 void wxSystemSettings::Done()
77 {
78 // delete g_systemWinColour;
79 delete g_systemBtnFaceColour;
80 delete g_systemBtnShadowColour;
81 delete g_systemBtnHighlightColour;
82 delete g_systemHighlightColour;
83 delete g_systemHighlightTextColour;
84 delete g_systemListBoxColour;
85 delete g_systemFont;
86 delete g_systemBtnTextColour;
87 }
88
89 // kind of widget to use in GetColourFromGTKWidget
90 enum GtkWidgetType
91 {
92 GTK_BUTTON,
93 GTK_LIST
94 };
95
96 // wxSystemSettings::GetSystemColour() helper: get the colours from a GTK+
97 // widget style, return true if we did get them, false to use defaults
98 static bool GetColourFromGTKWidget(GtkWidgetType type, GtkStateType state,
99 int& red, int& green, int& blue)
100 {
101 GtkWidget *widget = type == GTK_BUTTON ? gtk_button_new() : gtk_list_new();
102 GtkStyle *def = gtk_rc_get_style( widget );
103 if ( !def )
104 def = gtk_widget_get_default_style();
105
106 bool ok;
107 if ( def )
108 {
109 // ok, it's a hack: we really should have different functions to
110 // access GtkStyle::bg and ::base but as we only use base for listbox
111 // for now, this code works too
112 GdkColor *col = type == GTK_BUTTON ? def->bg : def->base;
113 red = col[state].red;
114 green = col[state].green;
115 blue = col[state].blue;
116
117 ok = TRUE;
118 }
119 else
120 {
121 ok = FALSE;
122 }
123
124 gtk_widget_destroy( widget );
125
126 return ok;
127 }
128
129 wxColour wxSystemSettings::GetSystemColour( int index )
130 {
131 switch (index)
132 {
133 case wxSYS_COLOUR_SCROLLBAR:
134 case wxSYS_COLOUR_BACKGROUND:
135 case wxSYS_COLOUR_ACTIVECAPTION:
136 case wxSYS_COLOUR_INACTIVECAPTION:
137 case wxSYS_COLOUR_MENU:
138 case wxSYS_COLOUR_WINDOWFRAME:
139 case wxSYS_COLOUR_ACTIVEBORDER:
140 case wxSYS_COLOUR_INACTIVEBORDER:
141 case wxSYS_COLOUR_BTNFACE:
142 case wxSYS_COLOUR_3DLIGHT:
143 if (!g_systemBtnFaceColour)
144 {
145 int red, green, blue;
146 if ( !GetColourFromGTKWidget(GTK_BUTTON, GTK_STATE_NORMAL,
147 red, green, blue) )
148 {
149 red =
150 green = 0;
151 blue = 0x9c40;
152 }
153
154 g_systemBtnFaceColour = new wxColour( red >> SHIFT,
155 green >> SHIFT,
156 blue >> SHIFT );
157 }
158 return *g_systemBtnFaceColour;
159
160 case wxSYS_COLOUR_WINDOW:
161 return *wxWHITE;
162
163 case wxSYS_COLOUR_3DDKSHADOW:
164 return *wxBLACK;
165
166 case wxSYS_COLOUR_GRAYTEXT:
167 case wxSYS_COLOUR_BTNSHADOW:
168 //case wxSYS_COLOUR_3DSHADOW:
169 if (!g_systemBtnShadowColour)
170 {
171 wxColour faceColour(GetSystemColour(wxSYS_COLOUR_3DFACE));
172 g_systemBtnShadowColour =
173 new wxColour((unsigned char) (faceColour.Red() * 0.666),
174 (unsigned char) (faceColour.Green() * 0.666),
175 (unsigned char) (faceColour.Blue() * 0.666));
176 }
177
178 return *g_systemBtnShadowColour;
179
180 case wxSYS_COLOUR_3DHIGHLIGHT:
181 //case wxSYS_COLOUR_BTNHIGHLIGHT:
182 return * wxWHITE;
183 /* I think this should normally be white (JACS 8/2000)
184
185 Hmm, I'm quite sure it shouldn't ... (VZ 20.08.01)
186 if (!g_systemBtnHighlightColour)
187 {
188 g_systemBtnHighlightColour =
189 new wxColour( 0xea60 >> SHIFT,
190 0xea60 >> SHIFT,
191 0xea60 >> SHIFT );
192 }
193 return *g_systemBtnHighlightColour;
194 */
195
196 case wxSYS_COLOUR_HIGHLIGHT:
197 if (!g_systemHighlightColour)
198 {
199 int red, green, blue;
200 if ( !GetColourFromGTKWidget(GTK_BUTTON, GTK_STATE_SELECTED,
201 red, green, blue) )
202 {
203 red =
204 green = 0;
205 blue = 0x9c40;
206 }
207
208 g_systemHighlightColour = new wxColour( red >> SHIFT,
209 green >> SHIFT,
210 blue >> SHIFT );
211 }
212 return *g_systemHighlightColour;
213
214 case wxSYS_COLOUR_LISTBOX:
215 if (!g_systemListBoxColour)
216 {
217 int red, green, blue;
218 if ( GetColourFromGTKWidget(GTK_LIST, GTK_STATE_NORMAL,
219 red, green, blue) )
220 {
221 g_systemListBoxColour = new wxColour( red >> SHIFT,
222 green >> SHIFT,
223 blue >> SHIFT );
224 }
225 else
226 {
227 g_systemListBoxColour = new wxColour(*wxWHITE);
228 }
229 }
230 return *g_systemListBoxColour;
231
232 case wxSYS_COLOUR_MENUTEXT:
233 case wxSYS_COLOUR_WINDOWTEXT:
234 case wxSYS_COLOUR_CAPTIONTEXT:
235 case wxSYS_COLOUR_INACTIVECAPTIONTEXT:
236 case wxSYS_COLOUR_BTNTEXT:
237 case wxSYS_COLOUR_INFOTEXT:
238 if (!g_systemBtnTextColour)
239 {
240 GtkWidget *widget = gtk_button_new();
241 GtkStyle *def = gtk_rc_get_style( widget );
242 if (!def)
243 def = gtk_widget_get_default_style();
244 if (def)
245 {
246 int red = def->fg[GTK_STATE_NORMAL].red;
247 int green = def->fg[GTK_STATE_NORMAL].green;
248 int blue = def->fg[GTK_STATE_NORMAL].blue;
249 g_systemBtnTextColour =
250 new wxColour( red >> SHIFT,
251 green >> SHIFT,
252 blue >> SHIFT );
253 }
254 else
255 {
256 g_systemBtnTextColour =
257 new wxColour(0, 0, 0);
258 }
259 gtk_widget_destroy( widget );
260 }
261 return *g_systemBtnTextColour;
262
263 case wxSYS_COLOUR_HIGHLIGHTTEXT:
264 if (!g_systemHighlightTextColour)
265 {
266 wxColour hclr = GetSystemColour(wxSYS_COLOUR_HIGHLIGHT);
267 if (hclr.Red() > 200 && hclr.Green() > 200 && hclr.Blue() > 200)
268 g_systemHighlightTextColour = new wxColour(*wxBLACK);
269 else
270 g_systemHighlightTextColour = new wxColour(*wxWHITE);
271 }
272 return *g_systemHighlightTextColour;
273
274 case wxSYS_COLOUR_INFOBK:
275 case wxSYS_COLOUR_APPWORKSPACE:
276 return *wxWHITE; // ?
277 }
278
279 return *wxWHITE;
280 }
281
282 wxFont wxSystemSettings::GetSystemFont( int index )
283 {
284 switch (index)
285 {
286 case wxSYS_OEM_FIXED_FONT:
287 case wxSYS_ANSI_FIXED_FONT:
288 case wxSYS_SYSTEM_FIXED_FONT:
289 {
290 return *wxNORMAL_FONT;
291 }
292 case wxSYS_ANSI_VAR_FONT:
293 case wxSYS_SYSTEM_FONT:
294 case wxSYS_DEVICE_DEFAULT_FONT:
295 case wxSYS_DEFAULT_GUI_FONT:
296 {
297 if (!g_systemFont)
298 {
299 #if 0
300 GdkFont *gdk_font = GtkGetDefaultGuiFont();
301 if (gdk_font)
302 {
303 GSList *font_list = ((GdkFontPrivate*)gdk_font)->names;
304 char *name = (char*)font_list->data;
305 wxString font_string( name );
306 wxFontData font_data;
307 g_systemFont = new wxFont( font_string, font_data );
308 }
309 gtk_widget_destroy( widget );
310 #endif
311
312 g_systemFont = new wxFont( 12, wxSWISS, wxNORMAL, wxNORMAL );
313
314 }
315 return *g_systemFont;
316 }
317 }
318
319 return wxNullFont;
320 }
321
322 int wxSystemSettings::GetSystemMetric( int index )
323 {
324 switch (index)
325 {
326 case wxSYS_SCREEN_X: return gdk_screen_width();
327 case wxSYS_SCREEN_Y: return gdk_screen_height();
328 case wxSYS_HSCROLL_Y: return 15;
329 case wxSYS_VSCROLL_X: return 15;
330 }
331
332 wxCHECK_MSG( index, 0, wxT("wxSystemSettings::GetSystemMetric not fully implemented") );
333
334 return 0;
335 }