]> Pileus Git - ~andy/gtk/blob - gdk/win32/gdkvisual-win32.c
Fix win32 build
[~andy/gtk] / gdk / win32 / gdkvisual-win32.c
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 #include "gdkvisualprivate.h"
35
36 static void  gdk_visual_decompose_mask (gulong     mask,
37                                         gint      *shift,
38                                         gint      *prec);
39
40 static GdkVisual *system_visual = NULL;
41
42 static gint available_depths[1];
43
44 static GdkVisualType available_types[1];
45
46 void
47 _gdk_visual_init (void)
48 {
49   struct
50   {
51     BITMAPINFOHEADER bi;
52     union
53     {
54       RGBQUAD colors[256];
55       DWORD fields[256];
56     } u;
57   } bmi;
58   HBITMAP hbm;
59
60   const gint rastercaps = GetDeviceCaps (_gdk_display_hdc, RASTERCAPS);
61   const int numcolors = GetDeviceCaps (_gdk_display_hdc, NUMCOLORS);
62   gint bitspixel = GetDeviceCaps (_gdk_display_hdc, BITSPIXEL);
63   gint map_entries = 0;
64
65   system_visual = g_object_new (GDK_TYPE_VISUAL, NULL);
66   system_visual->screen = gdk_screen_get_default();
67
68   GDK_NOTE (COLORMAP, g_print ("BITSPIXEL=%d NUMCOLORS=%d\n",
69                                bitspixel, numcolors));
70
71   if (rastercaps & RC_PALETTE)
72     {
73       const int sizepalette = GetDeviceCaps (_gdk_display_hdc, SIZEPALETTE);
74       gchar *max_colors = getenv ("GDK_WIN32_MAX_COLORS");
75       system_visual->type = GDK_VISUAL_PSEUDO_COLOR;
76
77       GDK_NOTE (COLORMAP, g_print ("SIZEPALETTE=%d\n", sizepalette));
78       g_assert (sizepalette == 256);
79
80       if (max_colors != NULL)
81         _gdk_max_colors = atoi (max_colors);
82       
83       map_entries = _gdk_max_colors;
84
85       if (map_entries >= 16 && map_entries < sizepalette)
86         {
87           if (map_entries < 32)
88             {
89               map_entries = 16;
90               system_visual->type = GDK_VISUAL_STATIC_COLOR;
91               bitspixel = 4;
92             }
93           else if (map_entries < 64)
94             {
95               map_entries = 32;
96               bitspixel = 5;
97             }
98           else if (map_entries < 128)
99             {
100               map_entries = 64;
101               bitspixel = 6;
102             }
103           else if (map_entries < 256)
104             {
105               map_entries = 128;
106               bitspixel = 7;
107             }
108           else
109             g_assert_not_reached ();
110         }
111       else
112         map_entries = sizepalette;
113     }
114   else if (bitspixel == 1 && numcolors == 16)
115     {
116       bitspixel = 4;
117       system_visual->type = GDK_VISUAL_STATIC_COLOR;
118       map_entries = 16;
119     }
120   else if (bitspixel == 1)
121     {
122       system_visual->type = GDK_VISUAL_STATIC_GRAY;
123       map_entries = 2;
124     }
125   else if (bitspixel == 4)
126     {
127       system_visual->type = GDK_VISUAL_STATIC_COLOR;
128       map_entries = 16;
129     }
130   else if (bitspixel == 8)
131     {
132       system_visual->type = GDK_VISUAL_STATIC_COLOR;
133       map_entries = 256;
134     }
135   else if (bitspixel == 16)
136     {
137       system_visual->type = GDK_VISUAL_TRUE_COLOR;
138 #if 1
139       /* This code by Mike Enright,
140        * see http://www.users.cts.com/sd/m/menright/display.html
141        */
142       memset (&bmi, 0, sizeof (bmi));
143       bmi.bi.biSize = sizeof (bmi.bi);
144
145       hbm = CreateCompatibleBitmap (_gdk_display_hdc, 1, 1);
146       GetDIBits (_gdk_display_hdc, hbm, 0, 1, NULL,
147                  (BITMAPINFO *) &bmi, DIB_RGB_COLORS);
148       GetDIBits (_gdk_display_hdc, hbm, 0, 1, NULL,
149                  (BITMAPINFO *) &bmi, DIB_RGB_COLORS);
150       DeleteObject (hbm);
151
152       if (bmi.bi.biCompression != BI_BITFIELDS)
153         {
154           /* Either BI_RGB or BI_RLE_something
155            * .... or perhaps (!!) something else.
156            * Theoretically biCompression might be
157            * mmioFourCC('c','v','i','d') but I doubt it.
158            */
159           if (bmi.bi.biCompression == BI_RGB)
160             {
161               /* It's 555 */
162               bitspixel = 15;
163               system_visual->red_mask   = 0x00007C00;
164               system_visual->green_mask = 0x000003E0;
165               system_visual->blue_mask  = 0x0000001F;
166             }
167           else
168             {
169               g_assert_not_reached ();
170             }
171         }
172       else
173         {
174           DWORD allmasks =
175             bmi.u.fields[0] | bmi.u.fields[1] | bmi.u.fields[2];
176           int k = 0;
177           while (allmasks)
178             {
179               if (allmasks&1)
180                 k++;
181               allmasks/=2;
182             }
183           bitspixel = k;
184           system_visual->red_mask = bmi.u.fields[0];
185           system_visual->green_mask = bmi.u.fields[1];
186           system_visual->blue_mask  = bmi.u.fields[2];
187         }
188 #else
189       /* Old, incorrect (but still working) code. */
190 #if 0
191       system_visual->red_mask   = 0x0000F800;
192       system_visual->green_mask = 0x000007E0;
193       system_visual->blue_mask  = 0x0000001F;
194 #else
195       system_visual->red_mask   = 0x00007C00;
196       system_visual->green_mask = 0x000003E0;
197       system_visual->blue_mask  = 0x0000001F;
198 #endif
199 #endif
200     }
201   else if (bitspixel == 24 || bitspixel == 32)
202     {
203       bitspixel = 24;
204       system_visual->type = GDK_VISUAL_TRUE_COLOR;
205       system_visual->red_mask   = 0x00FF0000;
206       system_visual->green_mask = 0x0000FF00;
207       system_visual->blue_mask  = 0x000000FF;
208     }
209   else
210     g_error ("_gdk_visual_init: unsupported BITSPIXEL: %d\n", bitspixel);
211
212   system_visual->depth = bitspixel;
213   system_visual->byte_order = GDK_LSB_FIRST;
214   system_visual->bits_per_rgb = 42; /* Not used? */
215
216   if ((system_visual->type == GDK_VISUAL_TRUE_COLOR) ||
217       (system_visual->type == GDK_VISUAL_DIRECT_COLOR))
218     {
219       gdk_visual_decompose_mask (system_visual->red_mask,
220                                  &system_visual->red_shift,
221                                  &system_visual->red_prec);
222
223       gdk_visual_decompose_mask (system_visual->green_mask,
224                                  &system_visual->green_shift,
225                                  &system_visual->green_prec);
226
227       gdk_visual_decompose_mask (system_visual->blue_mask,
228                                  &system_visual->blue_shift,
229                                  &system_visual->blue_prec);
230       map_entries = 1 << (MAX (system_visual->red_prec,
231                                MAX (system_visual->green_prec,
232                                     system_visual->blue_prec)));
233     }
234   else
235     {
236       system_visual->red_mask = 0;
237       system_visual->red_shift = 0;
238       system_visual->red_prec = 0;
239
240       system_visual->green_mask = 0;
241       system_visual->green_shift = 0;
242       system_visual->green_prec = 0;
243
244       system_visual->blue_mask = 0;
245       system_visual->blue_shift = 0;
246       system_visual->blue_prec = 0;
247     }
248   system_visual->colormap_size = map_entries;
249
250   available_depths[0] = system_visual->depth;
251   available_types[0] = system_visual->type;
252 }
253
254 gint
255 _gdk_win32_screen_visual_get_best_depth (GdkScreen *screen)
256 {
257   return available_depths[0];
258 }
259
260 GdkVisualType
261 _gdk_win32_screen_visual_get_best_type (GdkScreen *screen)
262 {
263   return available_types[0];
264 }
265
266 GdkVisual*
267 _gdk_win32_screen_get_system_visual (GdkScreen *screen)
268 {
269   return system_visual;
270 }
271
272 GdkVisual*
273 _gdk_win32_screen_visual_get_best (GdkScreen *screen)
274 {
275   return ((GdkVisual*) system_visual);
276 }
277
278 GdkVisual*
279 _gdk_win32_screen_visual_get_best_with_depth (GdkScreen *screen, gint depth)
280 {
281   if (depth == system_visual->depth)
282     return (GdkVisual*) system_visual;
283   else
284     return NULL;
285 }
286
287 GdkVisual*
288 _gdk_win32_screen_visual_get_best_with_type (GdkScreen *screen, GdkVisualType visual_type)
289 {
290   if (visual_type == system_visual->type)
291     return system_visual;
292   else
293     return NULL;
294 }
295
296 GdkVisual*
297 _gdk_win32_screen_visual_get_best_with_both (GdkScreen    *screen,
298                                              gint          depth,
299                                              GdkVisualType visual_type)
300 {
301   if ((depth == system_visual->depth) && (visual_type == system_visual->type))
302     return system_visual;
303   else
304     return NULL;
305 }
306
307 void
308 _gdk_win32_screen_query_depths  (GdkScreen *screen,
309                                  gint **depths,
310                                  gint  *count)
311 {
312   *count = 1;
313   *depths = available_depths;
314 }
315
316 void
317 _gdk_win32_screen_query_visual_types (GdkScreen      *screen,
318                                       GdkVisualType **visual_types,
319                                       gint           *count)
320 {
321   *count = 1;
322   *visual_types = available_types;
323 }
324
325 GList*
326 _gdk_win32_screen_list_visuals (GdkScreen *screen)
327 {
328   return g_list_append (NULL, (gpointer) system_visual);
329 }
330
331 static void
332 gdk_visual_decompose_mask (gulong  mask,
333                            gint   *shift,
334                            gint   *prec)
335 {
336   *shift = 0;
337   *prec = 0;
338
339   while (!(mask & 0x1))
340     {
341       (*shift)++;
342       mask >>= 1;
343     }
344
345   while (mask & 0x1)
346     {
347       (*prec)++;
348       mask >>= 1;
349     }
350 }