Debian Codegraph @

gdk/win32/gdkvisual-win32.c

raw
1 /* GDK - The GIMP Drawing Kit
2 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3 * Copyright (C) 1998-2002 Tor Lillqvist
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
19 */
20
21 /*
22 * Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
23 * file for a list of people on the GTK+ Team. See the ChangeLog
24 * files for a list of changes. These files are distributed with
25 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
26 */
27
28 #include "config.h"
29 #include <stdlib.h>
30
31 #include "gdkvisual.h"
32 #include "gdkscreen.h" /* gdk_screen_get_default() */
33 #include "gdkprivate-win32.h"
34
35 static void gdk_visual_decompose_mask (gulong mask,
36 gint *shift,
37 gint *prec);
38
39 static GdkVisual *system_visual = NULL;
40
41 static gint available_depths[1];
42
43 static GdkVisualType available_types[1];
44
45 static void
46 gdk_visual_finalize (GObject *object)
47 {
48 g_error ("A GdkVisual object was finalized. This should not happen");
49 }
50
51 static void
52 gdk_visual_class_init (GObjectClass *class)
53 {
54 class->finalize = gdk_visual_finalize;
55 }
56
57 GType
58 gdk_visual_get_type (void)
59 {
60 static GType object_type = 0;
61
62 if (!object_type)
63 {
64 const GTypeInfo object_info =
65 {
66 sizeof (GdkVisualClass),
67 (GBaseInitFunc) NULL,
68 (GBaseFinalizeFunc) NULL,
69 (GClassInitFunc) gdk_visual_class_init,
70 NULL, /* class_finalize */
71 NULL, /* class_data */
72 sizeof (GdkVisual),
73 0, /* n_preallocs */
74 (GInstanceInitFunc) NULL,
75 };
76
77 object_type = g_type_register_static (G_TYPE_OBJECT,
78 "GdkVisual",
79 &object_info, 0);
80 }
81
82 return object_type;
83 }
84
85 void
86 _gdk_visual_init (void)
87 {
88 struct
89 {
90 BITMAPINFOHEADER bi;
91 union
92 {
93 RGBQUAD colors[256];
94 DWORD fields[256];
95 } u;
96 } bmi;
97 HBITMAP hbm;
98
99 const gint rastercaps = GetDeviceCaps (_gdk_display_hdc, RASTERCAPS);
100 const int numcolors = GetDeviceCaps (_gdk_display_hdc, NUMCOLORS);
101 gint bitspixel = GetDeviceCaps (_gdk_display_hdc, BITSPIXEL);
102 gint map_entries = 0;
103
104 system_visual = g_object_new (GDK_TYPE_VISUAL, NULL);
105
106 GDK_NOTE (COLORMAP, g_print ("BITSPIXEL=%d NUMCOLORS=%d\n",
107 bitspixel, numcolors));
108
109 if (rastercaps & RC_PALETTE)
110 {
111 const int sizepalette = GetDeviceCaps (_gdk_display_hdc, SIZEPALETTE);
112 gchar *max_colors = getenv ("GDK_WIN32_MAX_COLORS");
113 system_visual->type = GDK_VISUAL_PSEUDO_COLOR;
114
115 GDK_NOTE (COLORMAP, g_print ("SIZEPALETTE=%d\n", sizepalette));
116 g_assert (sizepalette == 256);
117
118 if (max_colors != NULL)
119 _gdk_max_colors = atoi (max_colors);
120
121 map_entries = _gdk_max_colors;
122
123 if (map_entries >= 16 && map_entries < sizepalette)
124 {
125 /* The calls to gdk_rgb_set_min_colors() here have knowledge
126 * of what color cubes gdk_rgb_do_colormaps() will try, and
127 * of the static system palette colors... XXX
128 */
129 if (map_entries < 32)
130 {
131 map_entries = 16;
132 system_visual->type = GDK_VISUAL_STATIC_COLOR;
133 bitspixel = 4;
134 gdk_rgb_set_min_colors (2*2*2);
135 }
136 else if (map_entries < 64)
137 {
138 map_entries = 32;
139 bitspixel = 5;
140 gdk_rgb_set_min_colors (3*3*3);
141 }
142 else if (map_entries < 128)
143 {
144 map_entries = 64;
145 bitspixel = 6;
146 gdk_rgb_set_min_colors (3*3*3);
147 }
148 else if (map_entries < 256)
149 {
150 map_entries = 128;
151 bitspixel = 7;
152 gdk_rgb_set_min_colors (5*5*4);
153 }
154 else
155 g_assert_not_reached ();
156 }
157 else
158 map_entries = sizepalette;
159 }
160 else if (bitspixel == 1 && numcolors == 16)
161 {
162 bitspixel = 4;
163 system_visual->type = GDK_VISUAL_STATIC_COLOR;
164 map_entries = 16;
165 }
166 else if (bitspixel == 1)
167 {
168 system_visual->type = GDK_VISUAL_STATIC_GRAY;
169 map_entries = 2;
170 }
171 else if (bitspixel == 4)
172 {
173 system_visual->type = GDK_VISUAL_STATIC_COLOR;
174 map_entries = 16;
175 }
176 else if (bitspixel == 8)
177 {
178 system_visual->type = GDK_VISUAL_STATIC_COLOR;
179 map_entries = 256;
180 }
181 else if (bitspixel == 16)
182 {
183 system_visual->type = GDK_VISUAL_TRUE_COLOR;
184 #if 1
185 /* This code by Mike Enright,
186 * see http://www.users.cts.com/sd/m/menright/display.html
187 */
188 memset (&bmi, 0, sizeof (bmi));
189 bmi.bi.biSize = sizeof (bmi.bi);
190
191 hbm = CreateCompatibleBitmap (_gdk_display_hdc, 1, 1);
192 GetDIBits (_gdk_display_hdc, hbm, 0, 1, NULL,
193 (BITMAPINFO *) &bmi, DIB_RGB_COLORS);
194 GetDIBits (_gdk_display_hdc, hbm, 0, 1, NULL,
195 (BITMAPINFO *) &bmi, DIB_RGB_COLORS);
196 DeleteObject (hbm);
197
198 if (bmi.bi.biCompression != BI_BITFIELDS)
199 {
200 /* Either BI_RGB or BI_RLE_something
201 * .... or perhaps (!!) something else.
202 * Theoretically biCompression might be
203 * mmioFourCC('c','v','i','d') but I doubt it.
204 */
205 if (bmi.bi.biCompression == BI_RGB)
206 {
207 /* It's 555 */
208 bitspixel = 15;
209 system_visual->red_mask = 0x00007C00;
210 system_visual->green_mask = 0x000003E0;
211 system_visual->blue_mask = 0x0000001F;
212 }
213 else
214 {
215 g_assert_not_reached ();
216 }
217 }
218 else
219 {
220 DWORD allmasks =
221 bmi.u.fields[0] | bmi.u.fields[1] | bmi.u.fields[2];
222 int k = 0;
223 while (allmasks)
224 {
225 if (allmasks&1)
226 k++;
227 allmasks/=2;
228 }
229 bitspixel = k;
230 system_visual->red_mask = bmi.u.fields[0];
231 system_visual->green_mask = bmi.u.fields[1];
232 system_visual->blue_mask = bmi.u.fields[2];
233 }
234 #else
235 /* Old, incorrect (but still working) code. */
236 #if 0
237 system_visual->red_mask = 0x0000F800;
238 system_visual->green_mask = 0x000007E0;
239 system_visual->blue_mask = 0x0000001F;
240 #else
241 system_visual->red_mask = 0x00007C00;
242 system_visual->green_mask = 0x000003E0;
243 system_visual->blue_mask = 0x0000001F;
244 #endif
245 #endif
246 }
247 else if (bitspixel == 24 || bitspixel == 32)
248 {
249 bitspixel = 24;
250 system_visual->type = GDK_VISUAL_TRUE_COLOR;
251 system_visual->red_mask = 0x00FF0000;
252 system_visual->green_mask = 0x0000FF00;
253 system_visual->blue_mask = 0x000000FF;
254 }
255 else
256 g_error ("_gdk_visual_init: unsupported BITSPIXEL: %d\n", bitspixel);
257
258 system_visual->depth = bitspixel;
259 system_visual->byte_order = GDK_LSB_FIRST;
260 system_visual->bits_per_rgb = 42; /* Not used? */
261
262 if ((system_visual->type == GDK_VISUAL_TRUE_COLOR) ||
263 (system_visual->type == GDK_VISUAL_DIRECT_COLOR))
264 {
265 gdk_visual_decompose_mask (system_visual->red_mask,
266 &system_visual->red_shift,
267 &system_visual->red_prec);
268
269 gdk_visual_decompose_mask (system_visual->green_mask,
270 &system_visual->green_shift,
271 &system_visual->green_prec);
272
273 gdk_visual_decompose_mask (system_visual->blue_mask,
274 &system_visual->blue_shift,
275 &system_visual->blue_prec);
276 map_entries = 1 << (MAX (system_visual->red_prec,
277 MAX (system_visual->green_prec,
278 system_visual->blue_prec)));
279 }
280 else
281 {
282 system_visual->red_mask = 0;
283 system_visual->red_shift = 0;
284 system_visual->red_prec = 0;
285
286 system_visual->green_mask = 0;
287 system_visual->green_shift = 0;
288 system_visual->green_prec = 0;
289
290 system_visual->blue_mask = 0;
291 system_visual->blue_shift = 0;
292 system_visual->blue_prec = 0;
293 }
294 system_visual->colormap_size = map_entries;
295
296 available_depths[0] = system_visual->depth;
297 available_types[0] = system_visual->type;
298 }
299
300 gint
301 gdk_visual_get_best_depth (void)
302 {
303 return available_depths[0];
304 }
305
306 GdkVisualType
307 gdk_visual_get_best_type (void)
308 {
309 return available_types[0];
310 }
311
312 GdkVisual*
313 gdk_screen_get_system_visual (GdkScreen *screen)
314 {
315 return system_visual;
316 }
317
318 GdkVisual*
319 gdk_visual_get_best (void)
320 {
321 return ((GdkVisual*) system_visual);
322 }
323
324 GdkVisual*
325 gdk_visual_get_best_with_depth (gint depth)
326 {
327 if (depth == system_visual->depth)
328 return (GdkVisual*) system_visual;
329 else
330 return NULL;
331 }
332
333 GdkVisual*
334 gdk_visual_get_best_with_type (GdkVisualType visual_type)
335 {
336 if (visual_type == system_visual->type)
337 return system_visual;
338 else
339 return NULL;
340 }
341
342 GdkVisual*
343 gdk_visual_get_best_with_both (gint depth,
344 GdkVisualType visual_type)
345 {
346 if ((depth == system_visual->depth) && (visual_type == system_visual->type))
347 return system_visual;
348 else
349 return NULL;
350 }
351
352 void
353 gdk_query_depths (gint **depths,
354 gint *count)
355 {
356 *count = 1;
357 *depths = available_depths;
358 }
359
360 void
361 gdk_query_visual_types (GdkVisualType **visual_types,
362 gint *count)
363 {
364 *count = 1;
365 *visual_types = available_types;
366 }
367
368 GList*
369 gdk_screen_list_visuals (GdkScreen *screen)
370 {
371 return g_list_append (NULL, (gpointer) system_visual);
372 }
373
374 GdkScreen *
375 gdk_visual_get_screen (GdkVisual *visual)
376 {
377 g_return_val_if_fail (GDK_IS_VISUAL (visual), NULL);
378
379 return gdk_screen_get_default ();
380 }
381
382 static void
383 gdk_visual_decompose_mask (gulong mask,
384 gint *shift,
385 gint *prec)
386 {
387 *shift = 0;
388 *prec = 0;
389
390 while (!(mask & 0x1))
391 {
392 (*shift)++;
393 mask >>= 1;
394 }
395
396 while (mask & 0x1)
397 {
398 (*prec)++;
399 mask >>= 1;
400 }
401 }