]> Pileus Git - ~andy/gtk/blob - gdk/win32/gdkgc-win32.c
Include the build directory.
[~andy/gtk] / gdk / win32 / gdkgc-win32.c
1 /* GDK - The GIMP Drawing Kit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Modified by the GTK+ Team and others 1997-1999.  See the AUTHORS
22  * file for a list of people on the GTK+ Team.  See the ChangeLog
23  * files for a list of changes.  These files are distributed with
24  * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
25  */
26
27 #include "config.h"
28
29 #include <string.h>
30
31 #include "gdkgc.h"
32 #include "gdkfont.h"
33 #include "gdkpixmap.h"
34 #include "gdkregion-generic.h"
35 #include "gdkinternals.h"
36 #include "gdkprivate-win32.h"
37 #include "gdkdrawable-win32.h"
38 #include "gdkwindow-win32.h"
39 #include "gdkpixmap-win32.h"
40
41 static void gdk_win32_gc_destroy    (GdkGC           *gc);
42 static void gdk_win32_gc_get_values (GdkGC           *gc,
43                                      GdkGCValues     *values);
44 static void gdk_win32_gc_set_values (GdkGC           *gc,
45                                      GdkGCValues     *values,
46                                      GdkGCValuesMask  values_mask);
47 static void gdk_win32_gc_set_dashes (GdkGC           *gc,
48                                      gint             dash_offset,
49                                      gint8            dash_list[],
50                                      gint             n);
51
52 static void gdk_gc_win32_class_init (GdkGCWin32Class *klass);
53 static void gdk_gc_win32_finalize   (GObject         *object);
54
55 static gpointer parent_class = NULL;
56
57 GType
58 gdk_gc_win32_get_type (void)
59 {
60   static GType object_type = 0;
61
62   if (!object_type)
63     {
64       static const GTypeInfo object_info =
65       {
66         sizeof (GdkGCWin32Class),
67         (GBaseInitFunc) NULL,
68         (GBaseFinalizeFunc) NULL,
69         (GClassInitFunc) gdk_gc_win32_class_init,
70         NULL,           /* class_finalize */
71         NULL,           /* class_data */
72         sizeof (GdkGCWin32),
73         0,              /* n_preallocs */
74         (GInstanceInitFunc) NULL,
75       };
76       
77       object_type = g_type_register_static (GDK_TYPE_GC,
78                                             "GdkGCWin32",
79                                             &object_info);
80     }
81   
82   return object_type;
83 }
84
85 static void
86 gdk_gc_win32_class_init (GdkGCWin32Class *klass)
87 {
88   GObjectClass *object_class = G_OBJECT_CLASS (klass);
89   GdkGCClass *gc_class = GDK_GC_CLASS (klass);
90   
91   parent_class = g_type_class_peek_parent (klass);
92
93   object_class->finalize = gdk_gc_win32_finalize;
94
95   gc_class->get_values = gdk_win32_gc_get_values;
96   gc_class->set_values = gdk_win32_gc_set_values;
97   gc_class->set_dashes = gdk_win32_gc_set_dashes;
98 }
99
100 static void
101 gdk_gc_win32_finalize (GObject *object)
102 {
103   GdkGCWin32 *win32_gc = GDK_GC_WIN32 (object);
104   
105   if (win32_gc->clip_region)
106     gdk_region_destroy (win32_gc->clip_region);
107   
108   if (win32_gc->values_mask & GDK_GC_FONT)
109     gdk_font_unref (win32_gc->font);
110   
111   if (win32_gc->values_mask & GDK_GC_TILE)
112     gdk_drawable_unref (win32_gc->tile);
113   
114   if (win32_gc->values_mask & GDK_GC_STIPPLE)
115     gdk_drawable_unref (win32_gc->stipple);
116   
117   G_OBJECT_CLASS (parent_class)->finalize (object);
118 }
119
120 static void
121 gdk_win32_gc_values_to_win32values (GdkGCValues    *values,
122                                     GdkGCValuesMask mask,
123                                     GdkGCWin32     *win32_gc)
124 {                                   
125   char *s = "";
126   gint sw, sh;
127
128   GDK_NOTE (MISC, g_print ("{"));
129
130   if (mask & GDK_GC_FOREGROUND)
131     {
132       win32_gc->foreground = values->foreground.pixel;
133       win32_gc->values_mask |= GDK_GC_FOREGROUND;
134       GDK_NOTE (MISC, (g_print ("fg=%.06x", win32_gc->foreground),
135                        s = ","));
136     }
137   
138   if (mask & GDK_GC_BACKGROUND)
139     {
140       win32_gc->background = values->background.pixel;
141       win32_gc->values_mask |= GDK_GC_BACKGROUND;
142       GDK_NOTE (MISC, (g_print ("%sbg=%.06x", s, win32_gc->background),
143                        s = ","));
144     }
145
146   if ((mask & GDK_GC_FONT) && (values->font->type == GDK_FONT_FONT
147                                || values->font->type == GDK_FONT_FONTSET))
148     {
149       if (win32_gc->font != NULL)
150         gdk_font_unref (win32_gc->font);
151       win32_gc->font = values->font;
152       if (win32_gc->font != NULL)
153         {
154           gchar *xlfd;
155
156           gdk_font_ref (win32_gc->font);
157           win32_gc->values_mask |= GDK_GC_FONT;
158           GDK_NOTE (MISC, (xlfd = gdk_font_full_name_get (win32_gc->font),
159                            g_print ("%sfont=%s", s, xlfd),
160                            s = ",",
161                            gdk_font_full_name_free (xlfd)));
162         }
163       else
164         {
165           win32_gc->values_mask &= ~GDK_GC_FONT;
166           GDK_NOTE (MISC, (g_print ("%sfont=NULL"),
167                            s = ","));
168         }
169     }
170
171   if (mask & GDK_GC_FUNCTION)
172     {
173       GDK_NOTE (MISC, (g_print ("%srop2=", s),
174                        s = ","));
175       switch (values->function)
176         {
177         case GDK_COPY:
178           win32_gc->rop2 = R2_COPYPEN;
179           GDK_NOTE (MISC, g_print ("COPYPEN"));
180           break;
181         case GDK_INVERT:
182           win32_gc->rop2 = R2_NOT;
183           GDK_NOTE (MISC, g_print ("NOT"));
184           break;
185         case GDK_XOR:
186           win32_gc->rop2 = R2_XORPEN;
187           GDK_NOTE (MISC, g_print ("XORPEN"));
188           break;
189         case GDK_CLEAR:
190           win32_gc->rop2 = R2_BLACK;
191           GDK_NOTE (MISC, g_print ("BLACK"));
192           break;
193         case GDK_AND:
194           win32_gc->rop2 = R2_MASKPEN;
195           GDK_NOTE (MISC, g_print ("MASKPEN"));
196           break;
197         case GDK_AND_REVERSE:
198           win32_gc->rop2 = R2_MASKPENNOT;
199           GDK_NOTE (MISC, g_print ("MASKPENNOT"));
200           break;
201         case GDK_AND_INVERT:
202           win32_gc->rop2 = R2_MASKNOTPEN;
203           GDK_NOTE (MISC, g_print ("MASKNOTPEN"));
204           break;
205         case GDK_NOOP:
206           win32_gc->rop2 = R2_NOP;
207           GDK_NOTE (MISC, g_print ("NOP"));
208           break;
209         case GDK_OR:
210           win32_gc->rop2 = R2_MERGEPEN;
211           GDK_NOTE (MISC, g_print ("MERGEPEN"));
212           break;
213         case GDK_EQUIV:
214           win32_gc->rop2 = R2_NOTXORPEN;
215           GDK_NOTE (MISC, g_print ("NOTXORPEN"));
216           break;
217         case GDK_OR_REVERSE:
218           win32_gc->rop2 = R2_MERGEPENNOT;
219           GDK_NOTE (MISC, g_print ("MERGEPENNOT"));
220           break;
221         case GDK_COPY_INVERT:
222           win32_gc->rop2 = R2_NOTCOPYPEN;
223           GDK_NOTE (MISC, g_print ("NOTCOPYPEN"));
224           break;
225         case GDK_OR_INVERT:
226           win32_gc->rop2 = R2_MERGENOTPEN;
227           GDK_NOTE (MISC, g_print ("MERGENOTPEN"));
228           break;
229         case GDK_NAND:
230           win32_gc->rop2 = R2_NOTMASKPEN;
231           GDK_NOTE (MISC, g_print ("NOTMASKPEN"));
232           break;
233         case GDK_NOR:
234           win32_gc->rop2 = R2_NOTMERGEPEN;
235           GDK_NOTE (MISC, g_print ("NOTMERGEPEN"));
236           break;
237         case GDK_SET:
238           win32_gc->rop2 = R2_WHITE;
239           GDK_NOTE (MISC, g_print ("WHITE"));
240           break;
241         }
242       win32_gc->values_mask |= GDK_GC_FUNCTION;
243     }
244
245   if (mask & GDK_GC_FILL)
246     {
247       win32_gc->fill_style = values->fill;
248       win32_gc->values_mask |= GDK_GC_FILL;
249       GDK_NOTE (MISC, (g_print ("%sfill=%d", s, win32_gc->fill_style),
250                        s = ","));
251     }
252
253   if (mask & GDK_GC_TILE)
254     {
255       if (win32_gc->tile != NULL)
256         gdk_drawable_unref (win32_gc->tile);
257       win32_gc->tile = values->tile;
258       if (win32_gc->tile != NULL)
259         {
260           gdk_drawable_ref (win32_gc->tile);
261           win32_gc->values_mask |= GDK_GC_TILE;
262           GDK_NOTE (MISC, (g_print ("%stile=%#x", s,
263                                     GDK_PIXMAP_HBITMAP (win32_gc->tile)),
264                            s = ","));
265         }
266       else
267         {
268           win32_gc->values_mask &= ~GDK_GC_TILE;
269           GDK_NOTE (MISC, (g_print ("%stile=NULL", s),
270                            s = ","));
271         }
272     }
273
274   if (mask & GDK_GC_STIPPLE)
275     {
276       if (win32_gc->stipple != NULL)
277         gdk_drawable_unref (win32_gc->stipple);
278       win32_gc->stipple = values->stipple;
279       if (win32_gc->stipple != NULL)
280         {
281           gdk_drawable_get_size (win32_gc->stipple, &sw, &sh);
282
283           if (sw != 8 || sh != 8)
284             {
285               /* It seems that it *must* be 8x8, at least on my machine. 
286                * Thus, tile an 8x8 bitmap with the stipple in case it is
287                * smaller, or simply use just the top left 8x8 in case it is
288                * larger.
289                */
290               gchar dummy[8];
291               GdkPixmap *bm = gdk_bitmap_create_from_data (NULL, dummy, 8, 8);
292               GdkGC *gc = gdk_gc_new (bm);
293               gint i, j;
294
295               i = 0;
296               while (i < 8)
297                 {
298                   j = 0;
299                   while (j < 8)
300                     {
301                       gdk_draw_drawable (bm, gc, win32_gc->stipple, 0, 0, i, j, sw, sh);
302                       j += sh;
303                     }
304                   i += sw;
305                 }
306               win32_gc->stipple = bm;
307               gdk_gc_unref (gc);
308             }
309           else
310             gdk_drawable_ref (win32_gc->stipple);
311           win32_gc->values_mask |= GDK_GC_STIPPLE;
312           GDK_NOTE (MISC, (g_print ("%sstipple=%#x", s,
313                                     GDK_PIXMAP_HBITMAP (win32_gc->stipple)),
314                            s = ","));
315         }
316       else
317         {
318           win32_gc->values_mask &= ~GDK_GC_STIPPLE;
319           GDK_NOTE (MISC, (g_print ("%sstipple=NULL", s),
320                            s = ","));
321         }
322     }
323
324   if (mask & GDK_GC_CLIP_MASK)
325     {
326       if (win32_gc->clip_region != NULL)
327         {
328           gdk_region_destroy (win32_gc->clip_region);
329           win32_gc->clip_region = NULL;
330         }
331       if (win32_gc->hcliprgn != NULL)
332         DeleteObject (win32_gc->hcliprgn);
333
334       if (values->clip_mask != NULL)
335         {
336           win32_gc->hcliprgn =
337             BitmapToRegion ((HBITMAP) GDK_PIXMAP_HBITMAP (values->clip_mask));
338           win32_gc->values_mask |= GDK_GC_CLIP_MASK;
339           OffsetRgn (win32_gc->hcliprgn,
340                      win32_gc->parent_instance.clip_x_origin,
341                      win32_gc->parent_instance.clip_y_origin);
342         }
343       else
344         {
345           win32_gc->hcliprgn = NULL;
346           win32_gc->values_mask &= ~GDK_GC_CLIP_MASK;
347         }
348       GDK_NOTE (MISC, (g_print ("%sclip=%#x", s, win32_gc->hcliprgn),
349                        s = ","));
350     }
351
352   if (mask & GDK_GC_SUBWINDOW)
353     {
354       win32_gc->subwindow_mode = values->subwindow_mode;
355       win32_gc->values_mask |= GDK_GC_SUBWINDOW;
356       GDK_NOTE (MISC, (g_print ("%ssubw=%d", s, win32_gc->subwindow_mode),
357                        s = ","));
358     }
359
360   if (mask & GDK_GC_TS_X_ORIGIN)
361     {
362       win32_gc->values_mask |= GDK_GC_TS_X_ORIGIN;
363       GDK_NOTE (MISC, (g_print ("%sts_x=%d", s, values->ts_x_origin),
364                        s = ","));
365     }
366
367   if (mask & GDK_GC_TS_Y_ORIGIN)
368     {
369       win32_gc->values_mask |= GDK_GC_TS_Y_ORIGIN;
370       GDK_NOTE (MISC, (g_print ("%sts_y=%d", s, values->ts_y_origin),
371                        s = ","));
372     }
373
374   if (mask & GDK_GC_CLIP_X_ORIGIN)
375     {
376       win32_gc->values_mask |= GDK_GC_CLIP_X_ORIGIN;
377       GDK_NOTE (MISC, (g_print ("%sclip_x=%d", s, values->clip_x_origin),
378                        s = ","));
379     }
380
381   if (mask & GDK_GC_CLIP_Y_ORIGIN)
382     {
383       win32_gc->values_mask |= GDK_GC_CLIP_Y_ORIGIN;
384       GDK_NOTE (MISC, (g_print ("%sclip_y=%d", s, values->clip_y_origin),
385                        s = ","));
386    }
387  
388   if (mask & GDK_GC_EXPOSURES)
389     {
390       win32_gc->graphics_exposures = values->graphics_exposures;
391       win32_gc->values_mask |= GDK_GC_EXPOSURES;
392       GDK_NOTE (MISC, (g_print ("%sexp=%d", s, win32_gc->graphics_exposures),
393                        s = ","));
394     }
395
396   if (mask & GDK_GC_LINE_WIDTH)
397     {
398       win32_gc->pen_width = values->line_width;
399       win32_gc->values_mask |= GDK_GC_LINE_WIDTH;
400       GDK_NOTE (MISC, (g_print ("%spw=%d", s, win32_gc->pen_width),
401                        s = ","));
402     }
403
404   if (mask & GDK_GC_LINE_STYLE)
405     {
406       win32_gc->pen_style &= ~(PS_STYLE_MASK);
407       GDK_NOTE (MISC, (g_print ("%sps|=", s),
408                        s = ","));
409       switch (values->line_style)
410         {
411         case GDK_LINE_SOLID:
412           GDK_NOTE (MISC, g_print ("LINE_SOLID"));
413           win32_gc->pen_style |= PS_SOLID;
414           break;
415         case GDK_LINE_ON_OFF_DASH:
416         case GDK_LINE_DOUBLE_DASH: /* ??? */
417           GDK_NOTE (MISC, g_print ("DASH"));
418           win32_gc->pen_style |= PS_DASH;
419           break;
420         }
421       win32_gc->values_mask |= GDK_GC_LINE_STYLE;
422     }
423
424   if (mask & GDK_GC_CAP_STYLE)
425     {
426       win32_gc->pen_style &= ~(PS_ENDCAP_MASK);
427       GDK_NOTE (MISC, (g_print ("%sps|=", s),
428                        s = ","));
429       switch (values->cap_style)
430         {
431         case GDK_CAP_NOT_LAST:  /* ??? */
432         case GDK_CAP_BUTT:
433           GDK_NOTE (MISC, g_print ("ENDCAP_FLAT"));
434           win32_gc->pen_style |= PS_ENDCAP_FLAT;
435           break;
436         case GDK_CAP_ROUND:
437           GDK_NOTE (MISC, g_print ("ENDCAP_ROUND"));
438           win32_gc->pen_style |= PS_ENDCAP_ROUND;
439           break;
440         case GDK_CAP_PROJECTING:
441           GDK_NOTE (MISC, g_print ("ENDCAP_SQUARE"));
442           win32_gc->pen_style |= PS_ENDCAP_SQUARE;
443           break;
444         }
445       win32_gc->values_mask |= GDK_GC_CAP_STYLE;
446     }
447
448   if (mask & GDK_GC_JOIN_STYLE)
449     {
450       win32_gc->pen_style &= ~(PS_JOIN_MASK);
451       GDK_NOTE (MISC, (g_print ("%sps|=", s),
452                        s = ","));
453       switch (values->join_style)
454         {
455         case GDK_JOIN_MITER:
456           GDK_NOTE (MISC, g_print ("JOIN_MITER"));
457           win32_gc->pen_style |= PS_JOIN_MITER;
458           break;
459         case GDK_JOIN_ROUND:
460           GDK_NOTE (MISC, g_print ("JOIN_ROUND"));
461           win32_gc->pen_style |= PS_JOIN_ROUND;
462           break;
463         case GDK_JOIN_BEVEL:
464           GDK_NOTE (MISC, g_print ("JOIN_BEVEL"));
465           win32_gc->pen_style |= PS_JOIN_BEVEL;
466           break;
467         }
468       win32_gc->values_mask |= GDK_GC_JOIN_STYLE;
469     }
470   GDK_NOTE (MISC, g_print ("}\n"));
471 }
472
473 GdkGC*
474 _gdk_win32_gc_new (GdkDrawable    *drawable,
475                    GdkGCValues    *values,
476                    GdkGCValuesMask mask)
477 {
478   GdkGC *gc;
479   GdkGCWin32 *win32_gc;
480
481   /* NOTICE that the drawable here has to be the impl drawable,
482    * not the publically-visible drawables.
483    */
484   g_return_val_if_fail (GDK_IS_DRAWABLE_IMPL_WIN32 (drawable), NULL);
485
486   gc = g_object_new (gdk_gc_win32_get_type (), NULL);
487   win32_gc = GDK_GC_WIN32 (gc);
488
489   win32_gc->hdc = NULL;
490   win32_gc->clip_region = NULL;
491   win32_gc->hcliprgn = NULL;
492
493   /* Use the same default values as X11 does, even if they don't make
494    * sense per se. But apps always set fg and bg anyway.
495    */
496   win32_gc->foreground = 0;
497   win32_gc->background = 1;
498   win32_gc->font = NULL;
499   win32_gc->rop2 = R2_COPYPEN;
500   win32_gc->fill_style = GDK_SOLID;
501   win32_gc->tile = NULL;
502   win32_gc->stipple = NULL;
503   win32_gc->pen_style = PS_GEOMETRIC|PS_ENDCAP_FLAT|PS_JOIN_MITER;
504   win32_gc->pen_width = 0;
505
506   win32_gc->values_mask = GDK_GC_FUNCTION | GDK_GC_FILL;
507
508   GDK_NOTE (MISC, g_print ("_gdk_win32_gc_new: "));
509   gdk_win32_gc_values_to_win32values (values, mask, win32_gc);
510
511   win32_gc->hwnd = NULL;
512
513   GDK_NOTE (MISC, g_print (" = %p\n", gc));
514
515   return gc;
516 }
517
518 static void
519 gdk_win32_gc_get_values (GdkGC       *gc,
520                          GdkGCValues *values)
521 {
522   GdkGCWin32 *win32_gc = GDK_GC_WIN32 (gc);
523
524   values->foreground.pixel = win32_gc->foreground;
525   values->background.pixel = win32_gc->background;
526   values->font = win32_gc->font;
527
528   switch (win32_gc->rop2)
529     {
530     case R2_COPYPEN:
531       values->function = GDK_COPY; break;
532     case R2_NOT:
533       values->function = GDK_INVERT; break;
534     case R2_XORPEN:
535       values->function = GDK_XOR; break;
536     case R2_BLACK:
537       values->function = GDK_CLEAR; break;
538     case R2_MASKPEN:
539       values->function = GDK_AND; break;
540     case R2_MASKPENNOT:
541       values->function = GDK_AND_REVERSE; break;
542     case R2_MASKNOTPEN:
543       values->function = GDK_AND_INVERT; break;
544     case R2_NOP:
545       values->function = GDK_NOOP; break;
546     case R2_MERGEPEN:
547       values->function = GDK_OR; break;
548     case R2_NOTXORPEN:
549       values->function = GDK_EQUIV; break;
550     case R2_MERGEPENNOT:
551       values->function = GDK_OR_REVERSE; break;
552     case R2_NOTCOPYPEN:
553       values->function = GDK_COPY_INVERT; break;
554     case R2_MERGENOTPEN:
555       values->function = GDK_OR_INVERT; break;
556     case R2_NOTMASKPEN:
557       values->function = GDK_NAND; break;
558     case R2_NOTMERGEPEN:
559       values->function = GDK_NOR; break;
560     case R2_WHITE:
561       values->function = GDK_SET; break;
562     }
563
564   values->fill = win32_gc->fill_style;
565
566   values->tile = win32_gc->tile;
567   values->stipple = win32_gc->stipple;
568
569   /* Also the X11 backend always returns a NULL clip_mask */
570   values->clip_mask = NULL;
571
572   values->subwindow_mode = win32_gc->subwindow_mode;
573   values->ts_x_origin = win32_gc->parent_instance.ts_x_origin;
574   values->ts_y_origin = win32_gc->parent_instance.ts_y_origin;
575   values->clip_x_origin = win32_gc->parent_instance.clip_x_origin;
576   values->clip_y_origin = win32_gc->parent_instance.clip_y_origin;
577   values->graphics_exposures = win32_gc->graphics_exposures;
578   values->line_width = win32_gc->pen_width;
579   
580   if (win32_gc->pen_style & PS_SOLID)
581     values->line_style = GDK_LINE_SOLID;
582   else if (win32_gc->pen_style & PS_DASH)
583     values->line_style = GDK_LINE_ON_OFF_DASH;
584   else
585     values->line_style = GDK_LINE_SOLID;
586
587   /* PS_ENDCAP_ROUND is zero */
588   if (win32_gc->pen_style & PS_ENDCAP_FLAT)
589     values->cap_style = GDK_CAP_BUTT;
590   else if (win32_gc->pen_style & PS_ENDCAP_SQUARE)
591     values->cap_style = GDK_CAP_PROJECTING;
592   else
593     values->cap_style = GDK_CAP_ROUND;
594     
595   /* PS_JOIN_ROUND is zero */
596   if (win32_gc->pen_style & PS_JOIN_MITER)
597     values->join_style = GDK_JOIN_MITER;
598   else if (win32_gc->pen_style & PS_JOIN_BEVEL)
599     values->join_style = GDK_JOIN_BEVEL;
600   else
601     values->join_style = GDK_JOIN_ROUND;
602 }
603
604 static void
605 gdk_win32_gc_set_values (GdkGC           *gc,
606                          GdkGCValues     *values,
607                          GdkGCValuesMask  mask)
608 {
609   g_return_if_fail (GDK_IS_GC (gc));
610
611   GDK_NOTE (MISC, g_print ("gdk_win32_gc_set_values: "));
612
613   gdk_win32_gc_values_to_win32values (values, mask, GDK_GC_WIN32 (gc));
614 }
615
616 static void
617 gdk_win32_gc_set_dashes (GdkGC *gc,
618                          gint   dash_offset,
619                          gint8  dash_list[],
620                          gint   n)
621 {
622   GdkGCWin32 *win32_gc;
623
624   g_return_if_fail (GDK_IS_GC (gc));
625   g_return_if_fail (dash_list != NULL);
626
627   win32_gc = GDK_GC_WIN32 (gc);
628
629   win32_gc->pen_style &= ~(PS_STYLE_MASK);
630   win32_gc->pen_style |= PS_DASH;
631
632   /* 
633    * Set the extended line style. This could be done by 
634    * PS_USERSTYLE and ExtCreatePen; but ONLY on WinNT, 
635    * so let's make a guess (based on the implementation 
636    * in DIA). On Win9x this does only work for lines
637    * with width one ...
638    *
639    * More workarounds for Win9x descibed at:
640    * http://www.codeguru.com/gdi/dashed.shtml
641    */
642   if (!IS_WIN_NT () && win32_gc->pen_width > 1)
643     {
644       GDK_NOTE (MISC, g_print ("gdk_win32_gc_set_dashes: not fully supported\n"));
645       win32_gc->pen_style |= PS_SOLID;
646       return;
647     }
648   
649   /* win32_gc->pen_style = PS_COSMETIC; ??? */
650   if (2 == n)
651     {
652       if ((dash_list[0] == dash_list[1]) && (dash_list[0] > 2))
653         {
654           win32_gc->pen_style |= PS_DASH;
655           GDK_NOTE (MISC, g_print("gdk_win32_gc_set_dashes: PS_DASH (%d,%d)\n", 
656                                   dash_list[0], dash_list[1]));
657         }
658       else
659         {
660           win32_gc->pen_style |= PS_DOT;
661           GDK_NOTE (MISC, g_print("gdk_win32_gc_set_dashes: PS_DOT (%d,%d)\n", 
662                                   dash_list[0], dash_list[1]));
663         }
664     }
665   else if (4 == n)
666     {
667       win32_gc->pen_style |= PS_DASHDOT; 
668       GDK_NOTE (MISC, g_print("gdk_win32_gc_set_dashes: PS_DASHDOT (%d,%d,%d,%d)\n", 
669                               dash_list[0], dash_list[1],
670                               dash_list[2], dash_list[3]));
671     }
672   else if (6 == n)
673     {
674       win32_gc->pen_style |= PS_DASHDOTDOT; 
675       GDK_NOTE (MISC, g_print("gdk_win32_gc_set_dashes: PS_DASHDOTDOT (%d,%d,%d,%d,%d,%d)\n", 
676                               dash_list[0], dash_list[1],
677                               dash_list[2], dash_list[3],
678                               dash_list[4], dash_list[5]));
679     }
680   else
681     {
682       win32_gc->pen_style |= PS_DASH;
683       GDK_NOTE (MISC, g_print("gdk_win32_gc_set_dashes: no guess for %d dashes\n", n));
684     }
685 }
686
687 void
688 gdk_gc_set_clip_rectangle (GdkGC        *gc,
689                            GdkRectangle *rectangle)
690 {
691   GdkGCWin32 *win32_gc;
692
693   g_return_if_fail (GDK_IS_GC (gc));
694
695   win32_gc = GDK_GC_WIN32 (gc);
696
697   if (win32_gc->clip_region)
698     gdk_region_destroy (win32_gc->clip_region);
699
700   if (rectangle)
701     {
702       GDK_NOTE (MISC,
703                 g_print ("gdk_gc_set_clip_rectangle: (%d) %dx%d@+%d+%d\n",
704                          win32_gc,
705                          rectangle->width, rectangle->height,
706                          rectangle->x, rectangle->y));
707       win32_gc->clip_region = gdk_region_rectangle (rectangle);
708       win32_gc->values_mask |= GDK_GC_CLIP_MASK;
709     }
710   else
711     {
712       GDK_NOTE (MISC, g_print ("gdk_gc_set_clip_rectangle: (%d) NULL\n",
713                                win32_gc));
714       win32_gc->clip_region = NULL;
715       win32_gc->values_mask &= ~GDK_GC_CLIP_MASK;
716     }
717     win32_gc->values_mask &= ~(GDK_GC_CLIP_X_ORIGIN | GDK_GC_CLIP_Y_ORIGIN);
718
719
720 void
721 gdk_gc_set_clip_region (GdkGC            *gc,
722                         GdkRegion        *region)
723 {
724   GdkGCWin32 *win32_gc;
725
726   g_return_if_fail (GDK_IS_GC (gc));
727
728   win32_gc = GDK_GC_WIN32 (gc);
729
730   if (win32_gc->clip_region)
731     gdk_region_destroy (win32_gc->clip_region);
732
733   if (region)
734     {
735       GDK_NOTE (MISC, g_print ("gdk_gc_set_clip_region: %d %dx%d+%d+%d\n",
736                                win32_gc,
737                                region->extents.x2 - region->extents.x1,
738                                region->extents.y2 - region->extents.y1,
739                                region->extents.x1, region->extents.y1));
740       win32_gc->clip_region = gdk_region_copy (region);
741       win32_gc->values_mask |= GDK_GC_CLIP_MASK;
742     }
743   else
744     {
745       GDK_NOTE (MISC, g_print ("gdk_gc_set_clip_region: %d NULL\n",
746                                win32_gc));
747       win32_gc->clip_region = NULL;
748       win32_gc->values_mask &= ~GDK_GC_CLIP_MASK;
749     }
750
751   gc->clip_x_origin = 0;
752   gc->clip_y_origin = 0;
753
754   win32_gc->values_mask &= ~(GDK_GC_CLIP_X_ORIGIN | GDK_GC_CLIP_Y_ORIGIN);
755 }
756
757 void
758 gdk_gc_copy (GdkGC *dst_gc,
759              GdkGC *src_gc)
760 {
761   GdkGCWin32 *dst_win32_gc;
762   GdkGCWin32 *src_win32_gc;
763   DWORD nbytes;
764   LPRGNDATA rgn;
765
766   g_return_if_fail (GDK_IS_GC_WIN32 (dst_gc));
767   g_return_if_fail (GDK_IS_GC_WIN32 (src_gc));
768   
769   dst_win32_gc = GDK_GC_WIN32 (dst_gc);
770   src_win32_gc = GDK_GC_WIN32 (src_gc);
771
772   if (dst_win32_gc->font != NULL)
773     gdk_font_unref (dst_win32_gc->font);
774   if (dst_win32_gc->tile != NULL)
775     gdk_drawable_unref (dst_win32_gc->tile);
776   if (dst_win32_gc->stipple != NULL)
777     gdk_drawable_unref (dst_win32_gc->stipple);
778   if (dst_win32_gc->clip_region != NULL)
779     gdk_region_destroy (dst_win32_gc->clip_region);
780   
781   *dst_win32_gc = *src_win32_gc;
782
783   if (dst_win32_gc->clip_region != NULL)
784     dst_win32_gc->clip_region = gdk_region_copy (dst_win32_gc->clip_region);
785   if (dst_win32_gc->font != NULL)
786     gdk_font_ref (dst_win32_gc->font);
787   if (dst_win32_gc->tile != NULL)
788     gdk_drawable_ref (dst_win32_gc->tile);
789   if (dst_win32_gc->stipple != NULL)
790     gdk_drawable_ref (dst_win32_gc->stipple);
791 }
792
793 static guint bitmask[9] = { 0, 1, 3, 7, 15, 31, 63, 127, 255 };
794
795 COLORREF
796 gdk_colormap_color (GdkColormap *colormap,
797                     gulong       pixel)
798 {
799   const GdkVisual *visual;
800   GdkColormapPrivateWin32 *colormap_private;
801   guchar r, g, b;
802
803   if (colormap == NULL)
804     return PALETTEINDEX (pixel);
805
806   colormap_private = GDK_COLORMAP_PRIVATE_DATA (colormap);
807
808   if (colormap_private->xcolormap->rc_palette)
809     return PALETTEINDEX (pixel);
810
811   visual = colormap->visual;
812   r = (pixel & visual->red_mask) >> visual->red_shift;
813   r = (r * 255) / bitmask[visual->red_prec];
814   g = (pixel & visual->green_mask) >> visual->green_shift;
815   g = (g * 255) / bitmask[visual->green_prec];
816   b = (pixel & visual->blue_mask) >> visual->blue_shift;
817   b = (b * 255) / bitmask[visual->blue_prec];
818   
819   return RGB (r, g, b);
820 }
821
822 static void
823 predraw_set_foreground (GdkGC       *gc,
824                         GdkColormap *colormap,
825                         gboolean    *ok)
826 {
827   GdkGCWin32 *win32_gc = (GdkGCWin32 *) gc;
828   GdkColormapPrivateWin32 *colormap_private;
829   COLORREF fg;
830   LOGBRUSH logbrush;
831   HPEN hpen;
832   HBRUSH hbr;
833
834   if (colormap == NULL)
835     {
836       /* A 1 bit deep bitmap */
837       struct
838       {
839         WORD palVersion;
840         WORD palNumEntries;
841         PALETTEENTRY palPalEntry[2];
842       } logpal;
843       static HPALETTE hpal = NULL;
844
845       if (hpal == NULL)
846         {
847           /* Create a b&w palette */
848           logpal.palVersion = 0x300;
849           logpal.palNumEntries = 2;
850           logpal.palPalEntry[0].peRed = 
851             logpal.palPalEntry[0].peGreen = 
852             logpal.palPalEntry[0].peBlue = 0x00;
853           logpal.palPalEntry[0].peFlags = 0x00;
854           logpal.palPalEntry[1].peRed = 
855             logpal.palPalEntry[1].peGreen = 
856             logpal.palPalEntry[1].peBlue = 0xFF;
857           logpal.palPalEntry[1].peFlags = 0x00;
858           if ((hpal = CreatePalette ((LOGPALETTE *) &logpal)) == NULL)
859             WIN32_GDI_FAILED ("CreatePalette"), *ok = FALSE;
860         }
861       SelectPalette (win32_gc->hdc, hpal, FALSE);
862       RealizePalette (win32_gc->hdc);
863       fg = PALETTEINDEX (win32_gc->foreground);
864     }
865   else
866     {
867       colormap_private = GDK_COLORMAP_PRIVATE_DATA (colormap);
868       if (colormap_private->xcolormap->rc_palette)
869         {
870           int k;
871           if (SelectPalette (win32_gc->hdc, colormap_private->xcolormap->palette,
872                              FALSE) == NULL)
873             WIN32_GDI_FAILED ("SelectPalette"), *ok = FALSE;
874           if (TRUE || colormap_private->xcolormap->stale)
875             {
876               if ((k = RealizePalette (win32_gc->hdc)) == GDI_ERROR)
877                 WIN32_GDI_FAILED ("RealizePalette"), *ok = FALSE;
878               colormap_private->xcolormap->stale = FALSE;
879             }
880 #if 0
881           g_print ("Selected palette %#x for gc %#x, realized %d colors\n",
882                    colormap_private->xcolormap->palette, win32_gc->hdc, k);
883 #endif
884         }
885     }
886
887   fg = gdk_colormap_color (colormap, win32_gc->foreground);
888
889   if (SetTextColor (win32_gc->hdc, fg) == CLR_INVALID)
890     WIN32_GDI_FAILED ("SetTextColor"), *ok = FALSE;
891
892   /* Create and select pen and brush. */
893
894   logbrush.lbStyle = BS_SOLID;
895   logbrush.lbColor = fg;
896
897   if (*ok && (hpen = ExtCreatePen (win32_gc->pen_style,
898                                    (win32_gc->pen_width > 0 ? win32_gc->pen_width : 1),
899                                    &logbrush, 0, NULL)) == NULL)
900     WIN32_GDI_FAILED ("ExtCreatePen");
901   
902   if (SelectObject (win32_gc->hdc, hpen) == NULL)
903     WIN32_GDI_FAILED ("SelectObject"), *ok = FALSE;
904
905   switch (win32_gc->fill_style)
906     {
907     case GDK_OPAQUE_STIPPLED:
908       if (*ok && (hbr = CreatePatternBrush (GDK_PIXMAP_HBITMAP (win32_gc->stipple))) == NULL)
909         WIN32_GDI_FAILED ("CreatePatternBrush"), *ok = FALSE;
910         
911       if (*ok && !SetBrushOrgEx(win32_gc->hdc, gc->ts_x_origin,
912                                 gc->ts_y_origin, NULL))
913         WIN32_GDI_FAILED ("SetBrushOrgEx"), *ok = FALSE;
914       break;
915
916     case GDK_SOLID:
917     default:
918       if (*ok && (hbr = CreateSolidBrush (fg)) == NULL)
919         WIN32_GDI_FAILED ("CreateSolidBrush"), *ok = FALSE;
920       break;
921   }
922   if (*ok && SelectObject (win32_gc->hdc, hbr) == NULL)
923     WIN32_GDI_FAILED ("SelectObject"), *ok = FALSE;
924 }  
925
926 void
927 predraw_set_background (GdkGC       *gc,
928                         GdkColormap *colormap,
929                         gboolean    *ok)
930 {
931   GdkGCWin32 *win32_gc = (GdkGCWin32 *) gc;
932   COLORREF bg = gdk_colormap_color (colormap, win32_gc->background);
933
934   if (SetBkColor (win32_gc->hdc, bg) == CLR_INVALID)
935     WIN32_GDI_FAILED ("SetBkColor"), *ok = FALSE;
936 }
937
938 HDC
939 gdk_win32_hdc_get (GdkDrawable    *drawable,
940                    GdkGC          *gc,
941                    GdkGCValuesMask usage)
942 {
943   GdkGCWin32 *win32_gc = (GdkGCWin32 *) gc;
944   GdkColormapPrivateWin32 *colormap_private =
945     (GdkColormapPrivateWin32 *) GDK_DRAWABLE_IMPL_WIN32 (drawable)->colormap;
946   gboolean ok = TRUE;
947   int flag;
948
949   g_assert (win32_gc->hdc == NULL);
950
951   win32_gc->hwnd = GDK_DRAWABLE_HANDLE (drawable);
952   
953   if (GDK_IS_PIXMAP (drawable))
954     {
955       if ((win32_gc->hdc = CreateCompatibleDC (NULL)) == NULL)
956         WIN32_GDI_FAILED ("CreateCompatibleDC"), ok = FALSE;
957
958       if (ok && (win32_gc->saved_dc = SaveDC (win32_gc->hdc)) == 0)
959         WIN32_GDI_FAILED ("SaveDC"), ok = FALSE;
960       
961       if (ok && SelectObject (win32_gc->hdc, win32_gc->hwnd) == NULL)
962         WIN32_GDI_FAILED ("SelectObject"), ok = FALSE;
963     }
964   else
965     {
966       if ((win32_gc->hdc = GetDC (win32_gc->hwnd)) == NULL)
967         WIN32_GDI_FAILED ("GetDC");
968       
969       if (ok && (win32_gc->saved_dc = SaveDC (win32_gc->hdc)) == 0)
970         WIN32_GDI_FAILED ("SaveDC");
971     }
972   
973   if (ok && (usage & GDK_GC_FOREGROUND))
974     predraw_set_foreground (gc, GDK_DRAWABLE_IMPL_WIN32 (drawable)->colormap, &ok);
975
976   if (ok
977       && (usage & GDK_GC_BACKGROUND)
978       && (win32_gc->values_mask & GDK_GC_BACKGROUND))
979     predraw_set_background (gc, GDK_DRAWABLE_IMPL_WIN32 (drawable)->colormap, &ok);
980   
981   if (ok && (usage & GDK_GC_FONT))
982     {
983       if (SetBkMode (win32_gc->hdc, TRANSPARENT) == 0)
984         WIN32_GDI_FAILED ("SetBkMode"), ok = FALSE;
985   
986       if (ok && SetTextAlign (win32_gc->hdc, TA_BASELINE) == GDI_ERROR)
987         WIN32_GDI_FAILED ("SetTextAlign"), ok = FALSE;
988     }
989   
990   if (ok && (win32_gc->values_mask & GDK_GC_FUNCTION))
991     if (SetROP2 (win32_gc->hdc, win32_gc->rop2) == 0)
992       WIN32_GDI_FAILED ("SetROP2"), ok = FALSE;
993
994   if (win32_gc->values_mask & GDK_GC_CLIP_MASK)
995     g_assert ((win32_gc->clip_region != NULL) != (win32_gc->hcliprgn != NULL));
996
997   if (ok
998       && (win32_gc->values_mask & GDK_GC_CLIP_MASK)
999       && win32_gc->clip_region != NULL)
1000     {
1001       HRGN hrgn;
1002       RGNDATA *rgndata;
1003       RECT *rect;
1004       GdkRegionBox *boxes = win32_gc->clip_region->rects;
1005       guint nbytes =
1006         sizeof (RGNDATAHEADER) + (sizeof (RECT) * win32_gc->clip_region->numRects);
1007       int i;
1008
1009       rgndata = g_malloc (nbytes);
1010       rgndata->rdh.dwSize = sizeof (RGNDATAHEADER);
1011       rgndata->rdh.iType = RDH_RECTANGLES;
1012       rgndata->rdh.nCount = rgndata->rdh.nRgnSize = 0;
1013       SetRect (&rgndata->rdh.rcBound,
1014                G_MAXSHORT, G_MAXSHORT, G_MINSHORT, G_MINSHORT);
1015
1016       for (i = 0; i < win32_gc->clip_region->numRects; i++)
1017         {
1018           rect = ((RECT *) rgndata->Buffer) + rgndata->rdh.nCount++;
1019
1020           rect->left = CLAMP (boxes[i].x1 + gc->clip_x_origin,
1021                               G_MINSHORT, G_MAXSHORT);
1022           rect->right = CLAMP (boxes[i].x2 + gc->clip_x_origin,
1023                                G_MINSHORT, G_MAXSHORT);
1024           rect->top = CLAMP (boxes[i].y1 + gc->clip_y_origin,
1025                              G_MINSHORT, G_MAXSHORT);
1026           rect->bottom = CLAMP (boxes[i].y2 + gc->clip_y_origin,
1027                                 G_MINSHORT, G_MAXSHORT);
1028
1029           GDK_NOTE (MISC, g_print ("clip rgn box %d: %dx%d@+%d+%d\n",
1030                                    i,
1031                                    rect->right-rect->left,
1032                                    rect->bottom-rect->top,
1033                                    rect->left, rect->top));
1034
1035           if (rect->left < rgndata->rdh.rcBound.left)
1036             rgndata->rdh.rcBound.left = rect->left;
1037           if (rect->right > rgndata->rdh.rcBound.right)
1038             rgndata->rdh.rcBound.right = rect->right;
1039           if (rect->top < rgndata->rdh.rcBound.top)
1040             rgndata->rdh.rcBound.top = rect->top;
1041           if (rect->bottom > rgndata->rdh.rcBound.bottom)
1042             rgndata->rdh.rcBound.bottom = rect->bottom;
1043         }
1044       if ((hrgn = ExtCreateRegion (NULL, nbytes, rgndata)) == NULL)
1045         WIN32_API_FAILED ("ExtCreateRegion"), ok = FALSE;
1046
1047       if (ok && SelectClipRgn (win32_gc->hdc, hrgn) == ERROR)
1048         WIN32_API_FAILED ("SelectClipRgn"), ok = FALSE;
1049
1050       if (hrgn != NULL)
1051         DeleteObject (hrgn);
1052     }
1053   else if (ok
1054            && (win32_gc->values_mask & GDK_GC_CLIP_MASK)
1055            && win32_gc->hcliprgn != NULL)
1056     {
1057       if (SelectClipRgn (win32_gc->hdc, win32_gc->hcliprgn) == ERROR)
1058         WIN32_API_FAILED ("SelectClipRgn"), ok = FALSE;
1059     }
1060
1061   if (gdk_debug_flags & GDK_DEBUG_MISC)
1062     {
1063       HGDIOBJ obj;
1064       LOGBRUSH logbrush;
1065       EXTLOGPEN extlogpen;
1066       HRGN hrgn;
1067       RECT rect;
1068
1069       g_print ("gdk_win32_hdc_get: %d: %#x\n", win32_gc, win32_gc->hdc);
1070       obj = GetCurrentObject (win32_gc->hdc, OBJ_BRUSH);
1071       GetObject (obj, sizeof (LOGBRUSH), &logbrush);
1072       g_print ("brush: style: %s color: %.06x hatch: %#x\n",
1073                (logbrush.lbStyle == BS_HOLLOW ? "HOLLOW" :
1074                 (logbrush.lbStyle == BS_PATTERN ? "PATTERN" :
1075                  (logbrush.lbStyle == BS_SOLID ? "SOLID" :
1076                   "???"))),
1077                logbrush.lbColor,
1078                logbrush.lbHatch);
1079       obj = GetCurrentObject (win32_gc->hdc, OBJ_PEN);
1080       GetObject (obj, sizeof (EXTLOGPEN), &extlogpen);
1081       g_print ("pen: type: %s style: %s endcap: %s join: %s width: %d brush: %s\n",
1082                ((extlogpen.elpPenStyle & PS_TYPE_MASK) ==
1083                 PS_GEOMETRIC ? "GEOMETRIC" : "COSMETIC"),
1084                ((extlogpen.elpPenStyle & PS_STYLE_MASK) ==
1085                 PS_NULL ? "NULL" :
1086                 ((extlogpen.elpPenStyle & PS_STYLE_MASK) ==
1087                  PS_SOLID ? "SOLID" : "???")),
1088                ((extlogpen.elpPenStyle & PS_ENDCAP_MASK) ==
1089                 PS_ENDCAP_FLAT ? "FLAT" :
1090                 ((extlogpen.elpPenStyle & PS_ENDCAP_MASK) ==
1091                  PS_ENDCAP_ROUND ? "ROUND" :
1092                  ((extlogpen.elpPenStyle & PS_ENDCAP_MASK) ==
1093                   PS_ENDCAP_SQUARE ? "ROUND" :
1094                   ((extlogpen.elpPenStyle & PS_ENDCAP_MASK) ==
1095                    PS_ENDCAP_SQUARE ? "ROUND" : "???")))),
1096                ((extlogpen.elpPenStyle & PS_JOIN_MASK) ==
1097                 PS_JOIN_BEVEL ? "BEVEL" :
1098                 ((extlogpen.elpPenStyle & PS_JOIN_MASK) ==
1099                  PS_JOIN_MITER ? "MITER" :
1100                  ((extlogpen.elpPenStyle & PS_JOIN_MASK) ==
1101                   PS_JOIN_ROUND ? "ROUND" : "???"))),
1102                extlogpen.elpWidth,
1103                (extlogpen.elpBrushStyle == BS_DIBPATTERN ? "DIBPATTERN" :
1104                 (extlogpen.elpBrushStyle == BS_DIBPATTERNPT ? "DIBPATTERNPT" :
1105                  (extlogpen.elpBrushStyle == BS_HATCHED ? "HATCHED" :
1106                   (extlogpen.elpBrushStyle == BS_HOLLOW ? "HOLLOW" :
1107                    (extlogpen.elpBrushStyle == BS_PATTERN ? "PATTERN" :
1108                     (extlogpen.elpBrushStyle == BS_SOLID ? "SOLID" : "???")))))));
1109       hrgn = CreateRectRgn (0, 0, 0, 0);
1110       if ((flag = GetClipRgn (win32_gc->hdc, hrgn)) == -1)
1111         WIN32_API_FAILED ("GetClipRgn");
1112       else if (flag == 0)
1113         g_print ("no clip region\n");
1114       else if (flag == 1)
1115         {
1116           GetRgnBox (hrgn, &rect);
1117           g_print ("clip region bbox: %dx%d@+%d+%d\n",
1118                    rect.right - rect.left,
1119                    rect.bottom - rect.top,
1120                    rect.left, rect.top);
1121         }
1122     }
1123
1124   return win32_gc->hdc;
1125 }
1126
1127 void
1128 gdk_win32_hdc_release (GdkDrawable    *drawable,
1129                        GdkGC          *gc,
1130                        GdkGCValuesMask usage)
1131 {
1132   GdkGCWin32 *win32_gc = (GdkGCWin32 *) gc;
1133   HGDIOBJ hpen = NULL;
1134   HGDIOBJ hbr = NULL;
1135
1136   if (usage & GDK_GC_FOREGROUND)
1137     {
1138       if ((hpen = GetCurrentObject (win32_gc->hdc, OBJ_PEN)) == NULL)
1139         WIN32_GDI_FAILED ("GetCurrentObject");
1140
1141       if ((hbr = GetCurrentObject (win32_gc->hdc, OBJ_BRUSH)) == NULL)
1142         WIN32_GDI_FAILED ("GetCurrentObject");
1143     }
1144
1145   if (!RestoreDC (win32_gc->hdc, win32_gc->saved_dc))
1146     WIN32_GDI_FAILED ("RestoreDC");
1147 #if 0
1148   if (colormap_private != NULL
1149       && colormap_private->xcolormap->rc_palette
1150       && colormap_private->xcolormap->stale)
1151     {
1152       SelectPalette (win32_gc->hdc, GetStockObject (DEFAULT_PALETTE), FALSE);
1153       if (!UnrealizeObject (colormap_private->xcolormap->palette))
1154         WIN32_GDI_FAILED ("UnrealizeObject");
1155     }
1156 #endif
1157   if (GDK_IS_PIXMAP (drawable))
1158     {
1159       if (!DeleteDC (win32_gc->hdc))
1160         WIN32_GDI_FAILED ("DeleteDC");
1161     }
1162   else
1163     {
1164       ReleaseDC (win32_gc->hwnd, win32_gc->hdc);
1165     }
1166
1167   if (hpen != NULL)
1168     if (!DeleteObject (hpen))
1169       WIN32_GDI_FAILED ("DeleteObject");
1170   
1171   if (hbr != NULL)
1172     if (!DeleteObject (hbr))
1173       WIN32_GDI_FAILED ("DeleteObject");
1174
1175   win32_gc->hdc = NULL;
1176 }
1177
1178 /* This function originally from Jean-Edouard Lachand-Robert, and
1179  * available at www.codeguru.com. Simplified for our needs, now
1180  * handles just one-bit deep bitmaps (in Window parlance, ie those
1181  * that GDK calls bitmaps (and not pixmaps), with zero pixels being
1182  * transparent.
1183  */
1184
1185 /*
1186  *  BitmapToRegion :  Create a region from the "non-transparent" pixels of
1187  *  a bitmap
1188  *  Author :      Jean-Edouard Lachand-Robert
1189  *  (http://www.geocities.com/Paris/LeftBank/1160/resume.htm), June 1998.
1190  */
1191
1192 HRGN
1193 BitmapToRegion (HBITMAP hBmp)
1194 {
1195   HRGN hRgn = NULL;
1196   HDC hMemDC;
1197   BITMAP bm;
1198
1199   struct
1200   {
1201     BITMAPINFOHEADER bmiHeader;
1202 #if 1
1203     WORD bmiColors[2];
1204 #else
1205     RGBQUAD bmiColors[2];
1206 #endif
1207   } bmi;
1208   VOID *pbits8; 
1209   HBITMAP hbm8;
1210   struct
1211   {
1212     WORD palVersion;
1213     WORD palNumEntries;
1214     PALETTEENTRY palPalEntry[2];
1215   } logpal;
1216   static HPALETTE bwPalette = NULL;
1217
1218   HBITMAP holdBmp;
1219   HDC hDC;
1220
1221   BITMAP bm8;
1222   HBITMAP holdBmp2;
1223   DWORD maxRects;
1224   RGNDATA *pData;
1225   BYTE *p8;
1226   int x, y;
1227   HRGN h;
1228
1229   /* Create a B&W palette */
1230   if (bwPalette == NULL)
1231     {
1232       /* Create a b&w palette */
1233       logpal.palVersion = 0x300;
1234       logpal.palNumEntries = 2;
1235       logpal.palPalEntry[0].peRed = 
1236         logpal.palPalEntry[0].peGreen = 
1237         logpal.palPalEntry[0].peBlue = 0;
1238       logpal.palPalEntry[0].peFlags = 0;
1239       logpal.palPalEntry[1].peRed = 
1240         logpal.palPalEntry[1].peGreen = 
1241         logpal.palPalEntry[1].peBlue = 0xFF;
1242       logpal.palPalEntry[1].peFlags = 0;
1243       if ((bwPalette = CreatePalette ((LOGPALETTE *) &logpal)) == NULL)
1244         WIN32_GDI_FAILED ("CreatePalette");
1245     }
1246
1247   /* Create a memory DC inside which we will scan the bitmap content */
1248   hMemDC = CreateCompatibleDC (NULL);
1249   if (!hMemDC)
1250     {
1251       WIN32_GDI_FAILED ("CreateCompatibleDC");
1252       return NULL;
1253     }
1254
1255   SelectPalette (hMemDC, bwPalette, FALSE);
1256   RealizePalette (hMemDC);
1257
1258   /* Get bitmap size */
1259   GetObject(hBmp, sizeof(bm), &bm);
1260   
1261   /* Create a 8 bits depth bitmap and select it into the memory DC */
1262   bmi.bmiHeader.biSize = sizeof (BITMAPINFOHEADER);
1263   bmi.bmiHeader.biWidth = bm.bmWidth;
1264   bmi.bmiHeader.biHeight = bm.bmHeight;
1265   bmi.bmiHeader.biPlanes = 1;
1266   bmi.bmiHeader.biBitCount = 8;
1267   bmi.bmiHeader.biCompression = BI_RGB;
1268   bmi.bmiHeader.biSizeImage = 0;
1269   bmi.bmiHeader.biXPelsPerMeter = 0;
1270   bmi.bmiHeader.biYPelsPerMeter = 0;
1271   bmi.bmiHeader.biClrUsed = 2;
1272   bmi.bmiHeader.biClrImportant = 2;
1273 #if 1
1274   bmi.bmiColors[0] = 0;
1275   bmi.bmiColors[1] = 1;
1276   hbm8 = CreateDIBSection (hMemDC, (BITMAPINFO *)&bmi,
1277                             DIB_PAL_COLORS, &pbits8, NULL, 0);
1278 #else
1279   bmi.bmiColors[0].rgbBlue =
1280     bmi.bmiColors[0].rgbGreen =
1281     bmi.bmiColors[0].rgbRed = 0x00;
1282   bmi.bmiColors[0].rgbReserved = 0x00;
1283
1284   bmi.bmiColors[1].rgbBlue =
1285     bmi.bmiColors[1].rgbGreen =
1286     bmi.bmiColors[1].rgbRed = 0xFF;
1287   bmi.bmiColors[0].rgbReserved = 0x00;
1288
1289   hbm8 = CreateDIBSection (hMemDC, (BITMAPINFO *)&bmi,
1290                             DIB_RGB_COLORS, &pbits8, NULL, 0);
1291 #endif
1292   if (!hbm8)
1293     {
1294       WIN32_GDI_FAILED ("CreateDIBSection");
1295       DeleteDC (hMemDC);
1296       return NULL;
1297     }
1298
1299   holdBmp = (HBITMAP) SelectObject (hMemDC, hbm8);
1300
1301   /* Create a DC just to copy the bitmap into the memory DC*/
1302   hDC = CreateCompatibleDC (hMemDC);
1303   if (!hDC)
1304     {
1305       WIN32_GDI_FAILED ("CreateCompatibleDC");
1306       SelectObject (hMemDC, holdBmp);
1307       DeleteObject (hbm8);
1308       DeleteDC (hMemDC);
1309       return NULL;
1310     }
1311
1312   /* Get how many bytes per row we have for the bitmap bits */
1313   GetObject (hbm8, sizeof (bm8), &bm8);
1314
1315   /* Hans Breuer found a fix to the long-standing erroneous behaviour
1316    * on NT 4.0: There seems to be a bug in Win NT 4.0 GDI: scanlines
1317    * in bitmaps are dword aligned on both Win95 and NT. In the case of
1318    * a bitmap with 22 bytes worth of width, GetObject above returns
1319    * with bmWidth == 22. On Win95 bmWidthBytes == 24, as it should be,
1320    * but on NT is it 22. We need to correct this here.
1321    */
1322   bm8.bmWidthBytes = (((bm8.bmWidthBytes-1)/4)+1)*4; /* dword aligned!! */
1323
1324   /* Copy the bitmap into the memory DC*/
1325   holdBmp2 = (HBITMAP) SelectObject (hDC, hBmp);
1326
1327   if (!BitBlt (hMemDC, 0, 0, bm.bmWidth, bm.bmHeight, hDC, 0, 0, SRCCOPY))
1328     {
1329       WIN32_GDI_FAILED ("BitBlt");
1330       SelectObject (hDC, holdBmp2);
1331       SelectObject (hMemDC, holdBmp);
1332       DeleteObject (hbm8);
1333       DeleteDC (hMemDC);
1334       return NULL;
1335     }
1336   SelectObject (hDC, holdBmp2);
1337   DeleteDC (hDC);
1338
1339   /* For better performances, we will use the ExtCreateRegion()
1340    * function to create the region. This function take a RGNDATA
1341    * structure on entry. We will add rectangles by amount of
1342    * ALLOC_UNIT number in this structure.
1343    */
1344   #define ALLOC_UNIT  100
1345   maxRects = ALLOC_UNIT;
1346
1347   pData = g_malloc (sizeof (RGNDATAHEADER) + (sizeof (RECT) * maxRects));
1348   pData->rdh.dwSize = sizeof (RGNDATAHEADER);
1349   pData->rdh.iType = RDH_RECTANGLES;
1350   pData->rdh.nCount = pData->rdh.nRgnSize = 0;
1351   SetRect (&pData->rdh.rcBound, MAXLONG, MAXLONG, 0, 0);
1352
1353   /* Scan each bitmap from bottom to top (the bitmap is inverted vertically)*/
1354   p8 = (BYTE *) pbits8 + (bm8.bmHeight - 1) * bm8.bmWidthBytes;
1355   for (y = 0; y < bm.bmHeight; y++)
1356     {
1357       /* Scan each bitmap row from left to right*/
1358       for (x = 0; x < bm.bmWidth; x++)
1359         {
1360           /* Search for a continuous range of "non transparent pixels"*/
1361           int x0 = x;
1362           BYTE *p = p8 + x;
1363           while (x < bm.bmWidth)
1364             {
1365               if (*p == 0)
1366                 /* This pixel is "transparent"*/
1367                 break;
1368               p++;
1369               x++;
1370             }
1371           
1372           if (x > x0)
1373             {
1374               RECT *pr;
1375               /* Add the pixels (x0, y) to (x, y+1) as a new rectangle
1376                * in the region
1377                */
1378               if (pData->rdh.nCount >= maxRects)
1379                 {
1380                   maxRects += ALLOC_UNIT;
1381                   pData = g_realloc (pData, sizeof(RGNDATAHEADER)
1382                                      + (sizeof(RECT) * maxRects));
1383                 }
1384               pr = (RECT *) &pData->Buffer;
1385               SetRect (&pr[pData->rdh.nCount], x0, y, x, y+1);
1386               if (x0 < pData->rdh.rcBound.left)
1387                 pData->rdh.rcBound.left = x0;
1388               if (y < pData->rdh.rcBound.top)
1389                 pData->rdh.rcBound.top = y;
1390               if (x > pData->rdh.rcBound.right)
1391                 pData->rdh.rcBound.right = x;
1392               if (y+1 > pData->rdh.rcBound.bottom)
1393                 pData->rdh.rcBound.bottom = y+1;
1394               pData->rdh.nCount++;
1395               
1396               /* On Windows98, ExtCreateRegion() may fail if the
1397                * number of rectangles is too large (ie: >
1398                * 4000). Therefore, we have to create the region by
1399                * multiple steps.
1400                */
1401               if (pData->rdh.nCount == 2000)
1402                 {
1403                   HRGN h = ExtCreateRegion (NULL, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRects), pData);
1404                   if (hRgn)
1405                     {
1406                       CombineRgn(hRgn, hRgn, h, RGN_OR);
1407                       DeleteObject(h);
1408                     }
1409                   else
1410                     hRgn = h;
1411                   pData->rdh.nCount = 0;
1412                   SetRect (&pData->rdh.rcBound, MAXLONG, MAXLONG, 0, 0);
1413                 }
1414             }
1415         }
1416       
1417       /* Go to next row (remember, the bitmap is inverted vertically)*/
1418       p8 -= bm8.bmWidthBytes;
1419     }
1420   
1421   /* Create or extend the region with the remaining rectangles*/
1422   h = ExtCreateRegion (NULL, sizeof (RGNDATAHEADER)
1423                        + (sizeof (RECT) * maxRects), pData);
1424   if (hRgn)
1425     {
1426       CombineRgn (hRgn, hRgn, h, RGN_OR);
1427       DeleteObject (h);
1428     }
1429   else
1430     hRgn = h;
1431
1432   /* Clean up*/
1433   SelectObject(hMemDC, holdBmp);
1434   DeleteObject (hbm8);
1435   DeleteDC (hMemDC);
1436
1437   return hRgn;
1438 }