]> Pileus Git - ~andy/gtk/blob - gdk/win32/gdkgc-win32.c
3dbaaf013a0562edd68b1971e6d4df47f5c41c16
[~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  * 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 #define LINE_ATTRIBUTES (GDK_GC_LINE_WIDTH|GDK_GC_LINE_STYLE| \
29                          GDK_GC_CAP_STYLE|GDK_GC_JOIN_STYLE)
30
31 #include <config.h>
32 #include <string.h>
33
34 #include "gdkgc.h"
35 #include "gdkfont.h"
36 #include "gdkpixmap.h"
37 #include "gdkregion-generic.h"
38 #include "gdkprivate-win32.h"
39
40 static void gdk_win32_gc_get_values (GdkGC           *gc,
41                                      GdkGCValues     *values);
42 static void gdk_win32_gc_set_values (GdkGC           *gc,
43                                      GdkGCValues     *values,
44                                      GdkGCValuesMask  values_mask);
45 static void gdk_win32_gc_set_dashes (GdkGC           *gc,
46                                      gint             dash_offset,
47                                      gint8            dash_list[],
48                                      gint             n);
49
50 static void gdk_gc_win32_class_init (GdkGCWin32Class *klass);
51 static void gdk_gc_win32_finalize   (GObject         *object);
52
53 static gpointer parent_class = NULL;
54
55 GType
56 _gdk_gc_win32_get_type (void)
57 {
58   static GType object_type = 0;
59
60   if (!object_type)
61     {
62       static const GTypeInfo object_info =
63       {
64         sizeof (GdkGCWin32Class),
65         (GBaseInitFunc) NULL,
66         (GBaseFinalizeFunc) NULL,
67         (GClassInitFunc) gdk_gc_win32_class_init,
68         NULL,           /* class_finalize */
69         NULL,           /* class_data */
70         sizeof (GdkGCWin32),
71         0,              /* n_preallocs */
72         (GInstanceInitFunc) NULL,
73       };
74       
75       object_type = g_type_register_static (GDK_TYPE_GC,
76                                             "GdkGCWin32",
77                                             &object_info, 0);
78     }
79   
80   return object_type;
81 }
82
83 static void
84 gdk_gc_win32_class_init (GdkGCWin32Class *klass)
85 {
86   GObjectClass *object_class = G_OBJECT_CLASS (klass);
87   GdkGCClass *gc_class = GDK_GC_CLASS (klass);
88   
89   parent_class = g_type_class_peek_parent (klass);
90
91   object_class->finalize = gdk_gc_win32_finalize;
92
93   gc_class->get_values = gdk_win32_gc_get_values;
94   gc_class->set_values = gdk_win32_gc_set_values;
95   gc_class->set_dashes = gdk_win32_gc_set_dashes;
96 }
97
98 static void
99 gdk_gc_win32_finalize (GObject *object)
100 {
101   GdkGCWin32 *win32_gc = GDK_GC_WIN32 (object);
102   
103   if (win32_gc->hcliprgn != NULL)
104     DeleteObject (win32_gc->hcliprgn);
105   
106   if (win32_gc->values_mask & GDK_GC_FONT)
107     gdk_font_unref (win32_gc->font);
108   
109   if (win32_gc->values_mask & GDK_GC_TILE)
110     g_object_unref (win32_gc->tile);
111   
112   if (win32_gc->values_mask & GDK_GC_STIPPLE)
113     g_object_unref (win32_gc->stipple);
114
115   if (win32_gc->pen_dashes)
116     g_free (win32_gc->pen_dashes);
117   
118   G_OBJECT_CLASS (parent_class)->finalize (object);
119 }
120
121 static void
122 gdk_win32_gc_values_to_win32values (GdkGCValues    *values,
123                                     GdkGCValuesMask mask,
124                                     GdkGCWin32     *win32_gc)
125 {                                   
126   char *s = "";
127   gint sw, sh;
128
129   GDK_NOTE (GC, g_print ("{"));
130
131   if (mask & GDK_GC_FOREGROUND)
132     {
133       win32_gc->foreground = values->foreground.pixel;
134       win32_gc->values_mask |= GDK_GC_FOREGROUND;
135       GDK_NOTE (GC, (g_print ("fg=%.06lx", win32_gc->foreground),
136                      s = ","));
137     }
138   
139   if (mask & GDK_GC_BACKGROUND)
140     {
141       win32_gc->background = values->background.pixel;
142       win32_gc->values_mask |= GDK_GC_BACKGROUND;
143       GDK_NOTE (GC, (g_print ("%sbg=%.06lx", s, win32_gc->background),
144                      s = ","));
145     }
146
147   if ((mask & GDK_GC_FONT) && (values->font->type == GDK_FONT_FONT
148                                || values->font->type == GDK_FONT_FONTSET))
149     {
150       if (win32_gc->font != NULL)
151         gdk_font_unref (win32_gc->font);
152       win32_gc->font = values->font;
153       if (win32_gc->font != NULL)
154         {
155           gdk_font_ref (win32_gc->font);
156           win32_gc->values_mask |= GDK_GC_FONT;
157           GDK_NOTE (GC, (g_print ("%sfont=%p", s, win32_gc->font),
158                          s = ","));
159         }
160       else
161         {
162           win32_gc->values_mask &= ~GDK_GC_FONT;
163           GDK_NOTE (GC, (g_print ("%sfont=NULL", s),
164                          s = ","));
165         }
166     }
167
168   if (mask & GDK_GC_FUNCTION)
169     {
170       GDK_NOTE (GC, (g_print ("%srop2=", s),
171                      s = ","));
172       switch (values->function)
173         {
174 #define CASE(x,y) case GDK_##x: win32_gc->rop2 = R2_##y; GDK_NOTE (GC, g_print (#y)); break
175         CASE (COPY, COPYPEN);
176         CASE (INVERT, NOT);
177         CASE (XOR, XORPEN);
178         CASE (CLEAR, BLACK);
179         CASE (AND, MASKPEN);
180         CASE (AND_REVERSE, MASKPENNOT);
181         CASE (AND_INVERT, MASKNOTPEN);
182         CASE (NOOP, NOP);
183         CASE (OR, MERGEPEN);
184         CASE (EQUIV, NOTXORPEN);
185         CASE (OR_REVERSE, MERGEPENNOT);
186         CASE (COPY_INVERT, NOTCOPYPEN);
187         CASE (OR_INVERT, MERGENOTPEN);
188         CASE (NAND, NOTMASKPEN);
189         CASE (NOR, NOTMERGEPEN);
190         CASE (SET, WHITE);
191 #undef CASE
192         }
193       win32_gc->values_mask |= GDK_GC_FUNCTION;
194     }
195
196   if (mask & GDK_GC_FILL)
197     {
198       win32_gc->fill_style = values->fill;
199       win32_gc->values_mask |= GDK_GC_FILL;
200       GDK_NOTE (GC, (g_print ("%sfill=%s", s,
201                               _gdk_win32_fill_style_to_string (win32_gc->fill_style)),
202                      s = ","));
203     }
204
205   if (mask & GDK_GC_TILE)
206     {
207       if (win32_gc->tile != NULL)
208         g_object_unref (win32_gc->tile);
209       win32_gc->tile = values->tile;
210       if (win32_gc->tile != NULL)
211         {
212           g_object_ref (win32_gc->tile);
213           win32_gc->values_mask |= GDK_GC_TILE;
214           GDK_NOTE (GC,
215                     (g_print ("%stile=%p", s,
216                               GDK_PIXMAP_HBITMAP (win32_gc->tile)),
217                      s = ","));
218         }
219       else
220         {
221           win32_gc->values_mask &= ~GDK_GC_TILE;
222           GDK_NOTE (GC, (g_print ("%stile=NULL", s),
223                          s = ","));
224         }
225     }
226
227   if (mask & GDK_GC_STIPPLE)
228     {
229       if (win32_gc->stipple != NULL)
230         g_object_unref (win32_gc->stipple);
231       win32_gc->stipple = values->stipple;
232       if (win32_gc->stipple != NULL)
233         {
234           gdk_drawable_get_size (win32_gc->stipple, &sw, &sh);
235
236 #if 0 /* HB: this size limitation is disabled to make radio and check
237        * buttons work. I got the impression from the API docs, that
238        * it shouldn't be necessary at all, but win9x would do the clipping
239        */
240           if (   (sw != 8 || sh != 8)
241               && !IS_WIN_NT ()) /* HB: the MSDN says it's a Win95 limitation */
242             {
243               /* It seems that it *must* be 8x8, at least on my machine. 
244                * Thus, tile an 8x8 bitmap with the stipple in case it is
245                * smaller, or simply use just the top left 8x8 in case it is
246                * larger.
247                */
248               gchar dummy[8];
249               GdkPixmap *bm = gdk_bitmap_create_from_data (NULL, dummy, 8, 8);
250               GdkGC *gc = gdk_gc_new (bm);
251               gint i, j;
252
253               i = 0;
254               while (i < 8)
255                 {
256                   j = 0;
257                   while (j < 8)
258                     {
259                       gdk_draw_drawable (bm, gc, win32_gc->stipple, 0, 0, i, j, sw, sh);
260                       j += sh;
261                     }
262                   i += sw;
263                 }
264               win32_gc->stipple = bm;
265               gdk_gc_unref (gc);
266             }
267           else
268 #endif
269             g_object_ref (win32_gc->stipple);
270           win32_gc->values_mask |= GDK_GC_STIPPLE;
271           GDK_NOTE (GC,
272                     (g_print ("%sstipple=%p", s,
273                               GDK_PIXMAP_HBITMAP (win32_gc->stipple)),
274                      s = ","));
275         }
276       else
277         {
278           win32_gc->values_mask &= ~GDK_GC_STIPPLE;
279           GDK_NOTE (GC, (g_print ("%sstipple=NULL", s),
280                          s = ","));
281         }
282     }
283
284   if (mask & GDK_GC_CLIP_MASK)
285     {
286       if (win32_gc->hcliprgn != NULL)
287         DeleteObject (win32_gc->hcliprgn);
288
289       if (values->clip_mask != NULL)
290         {
291           win32_gc->hcliprgn = _gdk_win32_bitmap_to_hrgn (values->clip_mask);
292           win32_gc->values_mask |= GDK_GC_CLIP_MASK;
293         }
294       else
295         {
296           win32_gc->hcliprgn = NULL;
297           win32_gc->values_mask &= ~GDK_GC_CLIP_MASK;
298         }
299       GDK_NOTE (GC, (g_print ("%sclip=%p", s, win32_gc->hcliprgn),
300                      s = ","));
301     }
302
303   if (mask & GDK_GC_SUBWINDOW)
304     {
305       win32_gc->subwindow_mode = values->subwindow_mode;
306       win32_gc->values_mask |= GDK_GC_SUBWINDOW;
307       GDK_NOTE (GC, (g_print ("%ssubw=%d", s, win32_gc->subwindow_mode),
308                      s = ","));
309     }
310
311   if (mask & GDK_GC_TS_X_ORIGIN)
312     {
313       win32_gc->values_mask |= GDK_GC_TS_X_ORIGIN;
314       GDK_NOTE (GC, (g_print ("%sts_x=%d", s, values->ts_x_origin),
315                      s = ","));
316     }
317
318   if (mask & GDK_GC_TS_Y_ORIGIN)
319     {
320       win32_gc->values_mask |= GDK_GC_TS_Y_ORIGIN;
321       GDK_NOTE (GC, (g_print ("%sts_y=%d", s, values->ts_y_origin),
322                      s = ","));
323     }
324
325   if (mask & GDK_GC_CLIP_X_ORIGIN)
326     {
327       win32_gc->values_mask |= GDK_GC_CLIP_X_ORIGIN;
328       GDK_NOTE (GC, (g_print ("%sclip_x=%d", s, values->clip_x_origin),
329                      s = ","));
330     }
331
332   if (mask & GDK_GC_CLIP_Y_ORIGIN)
333     {
334       win32_gc->values_mask |= GDK_GC_CLIP_Y_ORIGIN;
335       GDK_NOTE (GC, (g_print ("%sclip_y=%d", s, values->clip_y_origin),
336                      s = ","));
337     }
338
339   if (mask & GDK_GC_EXPOSURES)
340     {
341       win32_gc->graphics_exposures = values->graphics_exposures;
342       win32_gc->values_mask |= GDK_GC_EXPOSURES;
343       GDK_NOTE (GC, (g_print ("%sexp=%d", s, win32_gc->graphics_exposures),
344                      s = ","));
345     }
346
347   if (mask & GDK_GC_LINE_WIDTH)
348     {
349       win32_gc->pen_width = values->line_width;
350       win32_gc->values_mask |= GDK_GC_LINE_WIDTH;
351       GDK_NOTE (GC, (g_print ("%spw=%d", s, win32_gc->pen_width),
352                      s = ","));
353     }
354
355   if (mask & GDK_GC_LINE_STYLE)
356     {
357       switch (values->line_style)
358         {
359         case GDK_LINE_SOLID:
360           if (win32_gc->pen_dashes)
361             {
362               g_free (win32_gc->pen_dashes);
363               win32_gc->pen_dashes = NULL;
364               win32_gc->pen_num_dashes = 0;
365             }
366           win32_gc->pen_style &= ~(PS_STYLE_MASK);
367           win32_gc->pen_style |= PS_SOLID;
368           break;
369         case GDK_LINE_ON_OFF_DASH:
370         case GDK_LINE_DOUBLE_DASH: 
371           if (!win32_gc->pen_dashes)
372             {
373             /* setting to PS_DASH probably isn't correct. If I understand the
374              * xlib docs correctly it should influence the handling of
375              * line endings ? --hb
376              */
377             win32_gc->pen_style &= ~(PS_STYLE_MASK);
378             win32_gc->pen_style |= PS_DASH;
379           }
380           break;
381         }
382       GDK_NOTE (GC, (g_print ("%sps|=PS_STYLE_%s", s, _gdk_win32_psstyle_to_string (win32_gc->pen_style)),
383                      s = ","));
384       win32_gc->values_mask |= GDK_GC_LINE_STYLE;
385     }
386
387   if (mask & GDK_GC_CAP_STYLE)
388     {
389       win32_gc->pen_style &= ~(PS_ENDCAP_MASK);
390       switch (values->cap_style)
391         {
392         case GDK_CAP_NOT_LAST:  /* ??? */
393         case GDK_CAP_BUTT:
394           win32_gc->pen_style |= PS_ENDCAP_FLAT;
395           break;
396         case GDK_CAP_ROUND:
397           win32_gc->pen_style |= PS_ENDCAP_ROUND;
398           break;
399         case GDK_CAP_PROJECTING:
400           win32_gc->pen_style |= PS_ENDCAP_SQUARE;
401           break;
402         }
403       GDK_NOTE (GC, (g_print ("%sps|=PS_ENDCAP_%s", s, _gdk_win32_psendcap_to_string (win32_gc->pen_style)),
404                      s = ","));
405       win32_gc->values_mask |= GDK_GC_CAP_STYLE;
406     }
407
408   if (mask & GDK_GC_JOIN_STYLE)
409     {
410       win32_gc->pen_style &= ~(PS_JOIN_MASK);
411       switch (values->join_style)
412         {
413         case GDK_JOIN_MITER:
414           win32_gc->pen_style |= PS_JOIN_MITER;
415           break;
416         case GDK_JOIN_ROUND:
417           win32_gc->pen_style |= PS_JOIN_ROUND;
418           break;
419         case GDK_JOIN_BEVEL:
420           win32_gc->pen_style |= PS_JOIN_BEVEL;
421           break;
422         }
423       GDK_NOTE (GC, (g_print ("%sps|=PS_JOIN_%s", s, _gdk_win32_psjoin_to_string (win32_gc->pen_style)),
424                      s = ","));
425       win32_gc->values_mask |= GDK_GC_JOIN_STYLE;
426     }
427   GDK_NOTE (GC, g_print ("} mask=(%s)", _gdk_win32_gcvalues_mask_to_string (win32_gc->values_mask)));
428 }
429
430 GdkGC*
431 _gdk_win32_gc_new (GdkDrawable    *drawable,
432                    GdkGCValues    *values,
433                    GdkGCValuesMask mask)
434 {
435   GdkGC *gc;
436   GdkGCWin32 *win32_gc;
437
438   /* NOTICE that the drawable here has to be the impl drawable,
439    * not the publically-visible drawables.
440    */
441   g_return_val_if_fail (GDK_IS_DRAWABLE_IMPL_WIN32 (drawable), NULL);
442
443   gc = g_object_new (_gdk_gc_win32_get_type (), NULL);
444   win32_gc = GDK_GC_WIN32 (gc);
445
446   win32_gc->hcliprgn = NULL;
447
448   /* Use the same default values as X11 does, even if they don't make
449    * sense per se. But apps always set fg and bg anyway.
450    */
451   win32_gc->foreground = 0;
452   win32_gc->background = 1;
453   win32_gc->font = NULL;
454   win32_gc->rop2 = R2_COPYPEN;
455   win32_gc->fill_style = GDK_SOLID;
456   win32_gc->tile = NULL;
457   win32_gc->stipple = NULL;
458   win32_gc->subwindow_mode = GDK_CLIP_BY_CHILDREN;
459   win32_gc->graphics_exposures = TRUE;
460   win32_gc->pen_width = 0;
461   win32_gc->pen_style = PS_GEOMETRIC|PS_ENDCAP_FLAT|PS_JOIN_MITER;
462   win32_gc->pen_dashes = NULL;
463   win32_gc->pen_num_dashes = 0;
464
465   win32_gc->values_mask = GDK_GC_FUNCTION | GDK_GC_FILL;
466
467   GDK_NOTE (GC, g_print ("_gdk_win32_gc_new: %p: ", win32_gc));
468   gdk_win32_gc_values_to_win32values (values, mask, win32_gc);
469   GDK_NOTE (GC, g_print ("\n"));
470
471   win32_gc->hdc = NULL;
472   win32_gc->hwnd = NULL;
473
474   return gc;
475 }
476
477 static void
478 gdk_win32_gc_get_values (GdkGC       *gc,
479                          GdkGCValues *values)
480 {
481   GdkGCWin32 *win32_gc = GDK_GC_WIN32 (gc);
482
483   values->foreground.pixel = win32_gc->foreground;
484   values->background.pixel = win32_gc->background;
485   values->font = win32_gc->font;
486
487   switch (win32_gc->rop2)
488     {
489     case R2_COPYPEN:
490       values->function = GDK_COPY; break;
491     case R2_NOT:
492       values->function = GDK_INVERT; break;
493     case R2_XORPEN:
494       values->function = GDK_XOR; break;
495     case R2_BLACK:
496       values->function = GDK_CLEAR; break;
497     case R2_MASKPEN:
498       values->function = GDK_AND; break;
499     case R2_MASKPENNOT:
500       values->function = GDK_AND_REVERSE; break;
501     case R2_MASKNOTPEN:
502       values->function = GDK_AND_INVERT; break;
503     case R2_NOP:
504       values->function = GDK_NOOP; break;
505     case R2_MERGEPEN:
506       values->function = GDK_OR; break;
507     case R2_NOTXORPEN:
508       values->function = GDK_EQUIV; break;
509     case R2_MERGEPENNOT:
510       values->function = GDK_OR_REVERSE; break;
511     case R2_NOTCOPYPEN:
512       values->function = GDK_COPY_INVERT; break;
513     case R2_MERGENOTPEN:
514       values->function = GDK_OR_INVERT; break;
515     case R2_NOTMASKPEN:
516       values->function = GDK_NAND; break;
517     case R2_NOTMERGEPEN:
518       values->function = GDK_NOR; break;
519     case R2_WHITE:
520       values->function = GDK_SET; break;
521     }
522
523   values->fill = win32_gc->fill_style;
524
525   values->tile = win32_gc->tile;
526   values->stipple = win32_gc->stipple;
527
528   /* Also the X11 backend always returns a NULL clip_mask */
529   values->clip_mask = NULL;
530
531   values->subwindow_mode = win32_gc->subwindow_mode;
532   values->ts_x_origin = win32_gc->parent_instance.ts_x_origin;
533   values->ts_y_origin = win32_gc->parent_instance.ts_y_origin;
534   values->clip_x_origin = win32_gc->parent_instance.clip_x_origin;
535   values->clip_y_origin = win32_gc->parent_instance.clip_y_origin;
536   values->graphics_exposures = win32_gc->graphics_exposures;
537   values->line_width = win32_gc->pen_width;
538   
539   if (win32_gc->pen_style & PS_SOLID)
540     values->line_style = GDK_LINE_SOLID;
541   else if (win32_gc->pen_style & PS_DASH)
542     values->line_style = GDK_LINE_ON_OFF_DASH;
543   else
544     values->line_style = GDK_LINE_SOLID;
545
546   /* PS_ENDCAP_ROUND is zero */
547   if (win32_gc->pen_style & PS_ENDCAP_FLAT)
548     values->cap_style = GDK_CAP_BUTT;
549   else if (win32_gc->pen_style & PS_ENDCAP_SQUARE)
550     values->cap_style = GDK_CAP_PROJECTING;
551   else
552     values->cap_style = GDK_CAP_ROUND;
553     
554   /* PS_JOIN_ROUND is zero */
555   if (win32_gc->pen_style & PS_JOIN_MITER)
556     values->join_style = GDK_JOIN_MITER;
557   else if (win32_gc->pen_style & PS_JOIN_BEVEL)
558     values->join_style = GDK_JOIN_BEVEL;
559   else
560     values->join_style = GDK_JOIN_ROUND;
561 }
562
563 static void
564 gdk_win32_gc_set_values (GdkGC           *gc,
565                          GdkGCValues     *values,
566                          GdkGCValuesMask  mask)
567 {
568   g_return_if_fail (GDK_IS_GC (gc));
569
570   GDK_NOTE (GC, g_print ("gdk_win32_gc_set_values: %p: ", GDK_GC_WIN32 (gc)));
571   gdk_win32_gc_values_to_win32values (values, mask, GDK_GC_WIN32 (gc));
572   GDK_NOTE (GC, g_print ("\n"));
573 }
574
575 static void
576 gdk_win32_gc_set_dashes (GdkGC *gc,
577                          gint   dash_offset,
578                          gint8  dash_list[],
579                          gint   n)
580 {
581   GdkGCWin32 *win32_gc;
582   int i;
583
584   g_return_if_fail (GDK_IS_GC (gc));
585   g_return_if_fail (dash_list != NULL);
586
587   win32_gc = GDK_GC_WIN32 (gc);
588
589   /* mark as set, see gdk_win32_gc_values_to_win32values () for the reason */
590   win32_gc->values_mask |= GDK_GC_LINE_STYLE;
591
592   win32_gc->pen_style &= ~(PS_STYLE_MASK);
593
594   win32_gc->pen_style |= (PS_GEOMETRIC | PS_USERSTYLE);
595   win32_gc->pen_num_dashes = n;
596   if (win32_gc->pen_dashes != NULL)
597     g_free (win32_gc->pen_dashes);
598   win32_gc->pen_dashes = g_new (DWORD, n);
599   for (i = 0; i < n; i++)
600     win32_gc->pen_dashes[i] = dash_list[i];
601 }
602
603 void
604 gdk_gc_set_clip_rectangle (GdkGC        *gc,
605                            GdkRectangle *rectangle)
606 {
607   GdkGCWin32 *win32_gc;
608
609   g_return_if_fail (GDK_IS_GC (gc));
610
611   win32_gc = GDK_GC_WIN32 (gc);
612
613   if (win32_gc->hcliprgn)
614     DeleteObject (win32_gc->hcliprgn);
615
616   if (rectangle)
617     {
618       GDK_NOTE (GC, g_print ("gdk_gc_set_clip_rectangle: %p: %s\n",
619                              win32_gc,
620                              _gdk_win32_gdkrectangle_to_string (rectangle)));
621       win32_gc->hcliprgn = CreateRectRgn (rectangle->x, rectangle->y,
622                                           rectangle->x + rectangle->width,
623                                           rectangle->y + rectangle->height);
624       win32_gc->values_mask |= GDK_GC_CLIP_MASK;
625     }
626   else
627     {
628       GDK_NOTE (GC, g_print ("gdk_gc_set_clip_rectangle: NULL\n"));
629
630       win32_gc->hcliprgn = NULL;
631       win32_gc->values_mask &= ~GDK_GC_CLIP_MASK;
632     }
633
634   gc->clip_x_origin = 0;
635   gc->clip_y_origin = 0;
636   
637   win32_gc->values_mask &= ~(GDK_GC_CLIP_X_ORIGIN | GDK_GC_CLIP_Y_ORIGIN);
638
639
640 void
641 gdk_gc_set_clip_region (GdkGC     *gc,
642                         GdkRegion *region)
643 {
644   GdkGCWin32 *win32_gc;
645
646   g_return_if_fail (GDK_IS_GC (gc));
647
648   win32_gc = GDK_GC_WIN32 (gc);
649
650   if (win32_gc->hcliprgn)
651     DeleteObject (win32_gc->hcliprgn);
652
653   if (region)
654     {
655       GDK_NOTE (GC, g_print ("gdk_gc_set_clip_region: %p: %s\n",
656                              win32_gc,
657                              _gdk_win32_gdkregion_to_string (region)));
658
659       win32_gc->hcliprgn = _gdk_win32_gdkregion_to_hrgn (region, 0, 0);
660       win32_gc->values_mask |= GDK_GC_CLIP_MASK;
661     }
662   else
663     {
664       GDK_NOTE (GC, g_print ("gdk_gc_set_clip_region: NULL\n"));
665
666       win32_gc->hcliprgn = NULL;
667       win32_gc->values_mask &= ~GDK_GC_CLIP_MASK;
668     }
669
670   gc->clip_x_origin = 0;
671   gc->clip_y_origin = 0;
672   
673   win32_gc->values_mask &= ~(GDK_GC_CLIP_X_ORIGIN | GDK_GC_CLIP_Y_ORIGIN);
674 }
675
676 void
677 gdk_gc_copy (GdkGC *dst_gc,
678              GdkGC *src_gc)
679 {
680   GdkGCWin32 *dst_win32_gc;
681   GdkGCWin32 *src_win32_gc;
682
683   g_return_if_fail (GDK_IS_GC_WIN32 (dst_gc));
684   g_return_if_fail (GDK_IS_GC_WIN32 (src_gc));
685   
686   dst_win32_gc = GDK_GC_WIN32 (dst_gc);
687   src_win32_gc = GDK_GC_WIN32 (src_gc);
688
689   GDK_NOTE (GC, g_print ("gdk_gc_copy: %p := %p\n", dst_win32_gc, src_win32_gc));
690
691   if (dst_gc->colormap)
692     g_object_unref (G_OBJECT (dst_gc->colormap));
693
694   if (dst_win32_gc->hcliprgn != NULL)
695     DeleteObject (dst_win32_gc->hcliprgn);
696
697   if (dst_win32_gc->font != NULL)
698     gdk_font_unref (dst_win32_gc->font);
699
700   if (dst_win32_gc->tile != NULL)
701     g_object_unref (dst_win32_gc->tile);
702
703   if (dst_win32_gc->stipple != NULL)
704     g_object_unref (dst_win32_gc->stipple);
705
706   if (dst_win32_gc->pen_dashes)
707     g_free (dst_win32_gc->pen_dashes);
708   
709   dst_gc->clip_x_origin = src_gc->clip_x_origin;
710   dst_gc->clip_y_origin = src_gc->clip_y_origin;
711   dst_gc->ts_x_origin = src_gc->ts_x_origin;
712   dst_gc->ts_y_origin = src_gc->ts_y_origin;
713   dst_gc->colormap = src_gc->colormap;
714   if (dst_gc->colormap)
715     g_object_ref (G_OBJECT (dst_gc->colormap));
716
717   dst_win32_gc->hcliprgn = src_win32_gc->hcliprgn;
718   if (dst_win32_gc->hcliprgn)
719     {
720       /* create a new region, to copy to */
721       dst_win32_gc->hcliprgn = CreateRectRgn (0,0,1,1);
722       /* overwrite from source */
723       CombineRgn (dst_win32_gc->hcliprgn, src_win32_gc->hcliprgn,
724                   NULL, RGN_COPY);
725     }
726
727   dst_win32_gc->values_mask = src_win32_gc->values_mask; 
728   dst_win32_gc->foreground = src_win32_gc->foreground;
729   dst_win32_gc->background = src_win32_gc->background;
730   dst_win32_gc->font = src_win32_gc->font;
731   if (dst_win32_gc->font != NULL)
732     gdk_font_ref (dst_win32_gc->font);
733
734   dst_win32_gc->rop2 = src_win32_gc->rop2;
735   dst_win32_gc->fill_style = src_win32_gc->fill_style;
736   dst_win32_gc->tile = src_win32_gc->tile;
737   if (dst_win32_gc->tile != NULL)
738     g_object_ref (dst_win32_gc->tile);
739
740   dst_win32_gc->stipple = src_win32_gc->stipple;
741   if (dst_win32_gc->stipple != NULL)
742     g_object_ref (dst_win32_gc->stipple);
743
744   dst_win32_gc->subwindow_mode = src_win32_gc->subwindow_mode;
745   dst_win32_gc->graphics_exposures = src_win32_gc->graphics_exposures;
746   dst_win32_gc->pen_width = src_win32_gc->pen_width;
747   dst_win32_gc->pen_style = src_win32_gc->pen_style;
748   dst_win32_gc->pen_dashes = src_win32_gc->pen_dashes;
749   if (dst_win32_gc->pen_dashes)
750     dst_win32_gc->pen_dashes = g_memdup (src_win32_gc->pen_dashes, 
751                                          sizeof (DWORD) * src_win32_gc->pen_num_dashes);
752   dst_win32_gc->pen_num_dashes = src_win32_gc->pen_num_dashes;
753
754
755   dst_win32_gc->hdc = NULL;
756   dst_win32_gc->saved_dc = FALSE;
757   dst_win32_gc->hwnd = NULL;
758   dst_win32_gc->holdpal = NULL;
759 }
760
761 GdkScreen *  
762 gdk_gc_get_screen (GdkGC *gc)
763 {
764   g_return_val_if_fail (GDK_IS_GC_WIN32 (gc), NULL);
765   
766   return _gdk_screen;
767 }
768
769 static guint bitmask[9] = { 0, 1, 3, 7, 15, 31, 63, 127, 255 };
770
771 COLORREF
772 _gdk_win32_colormap_color (GdkColormap *colormap,
773                            gulong       pixel)
774 {
775   const GdkVisual *visual;
776   GdkColormapPrivateWin32 *colormap_private;
777   guchar r, g, b;
778
779   if (colormap == NULL)
780     return DIBINDEX (pixel & 1);
781
782   colormap_private = GDK_WIN32_COLORMAP_DATA (colormap);
783
784   g_assert (colormap_private != NULL);
785
786   visual = colormap->visual;
787   switch (visual->type)
788     {
789     case GDK_VISUAL_GRAYSCALE:
790     case GDK_VISUAL_PSEUDO_COLOR:
791     case GDK_VISUAL_STATIC_COLOR:
792       return PALETTEINDEX (pixel);
793
794     case GDK_VISUAL_TRUE_COLOR:
795       r = (pixel & visual->red_mask) >> visual->red_shift;
796       r = (r * 255) / bitmask[visual->red_prec];
797       g = (pixel & visual->green_mask) >> visual->green_shift;
798       g = (g * 255) / bitmask[visual->green_prec];
799       b = (pixel & visual->blue_mask) >> visual->blue_shift;
800       b = (b * 255) / bitmask[visual->blue_prec];
801       return RGB (r, g, b);
802
803     default:
804       g_assert_not_reached ();
805       return 0;
806     }
807 }
808
809 static COLORREF
810 predraw_set_foreground (GdkGC       *gc,
811                         GdkColormap *colormap,
812                         gboolean    *ok)
813 {
814   COLORREF fg;
815   GdkGCWin32 *win32_gc = (GdkGCWin32 *) gc;
816   GdkColormapPrivateWin32 *colormap_private;
817   gint k;
818
819   if (colormap &&
820       (colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR ||
821        colormap->visual->type == GDK_VISUAL_STATIC_COLOR))
822     {
823       colormap_private = GDK_WIN32_COLORMAP_DATA (colormap);
824
825       g_assert (colormap_private != NULL);
826
827       if (!(win32_gc->holdpal = SelectPalette (win32_gc->hdc, colormap_private->hpal, FALSE)))
828         WIN32_GDI_FAILED ("SelectPalette"), *ok = FALSE;
829       else if ((k = RealizePalette (win32_gc->hdc)) == GDI_ERROR)
830         WIN32_GDI_FAILED ("RealizePalette"), *ok = FALSE;
831       else if (k > 0)
832         GDK_NOTE (COLORMAP, g_print ("predraw_set_foreground: realized %p: %d colors\n",
833                                      colormap_private->hpal, k));
834     }
835
836   fg = _gdk_win32_colormap_color (colormap, win32_gc->foreground);
837
838   GDK_NOTE (GC, g_print ("predraw_set_foreground: fg=%06lx\n", fg));
839   return fg;
840 }
841
842 /**
843  * gdk_win32_hdc_get:
844  * @drawable: destination #GdkDrawable
845  * @gc: #GdkGC to use for drawing on @drawable
846  * @usage: mask indicating what properties needs to be set up
847  *
848  * Allocates a Windows device context handle (HDC) for drawing into
849  * @drawable, and sets it up appropriately according to @usage.
850  *
851  * Each #GdkGC can at one time have only one HDC associated with it.
852  *
853  * The following flags in @mask are handled:
854  *
855  * If %GDK_GC_FOREGROUND is set in @mask, a solid brush of the
856  * foreground color in @gc is selected into the HDC. The text color of
857  * the HDC is also set. If the @drawable has a palette (256-color
858  * mode), the palette is selected and realized.
859  *
860  * If any of the line attribute flags (%GDK_GC_LINE_WIDTH,
861  * %GDK_GC_LINE_STYLE, %GDK_GC_CAP_STYLE and %GDK_GC_JOIN_STYLE) is
862  * set in @mask, a solid pen of the foreground color and appropriate
863  * width and stule is created and selected into the HDC. Note that the
864  * dash properties are not completely implemented.
865  *
866  * If the %GDK_GC_FONT flag is set, the background mix mode is set to
867  * %TRANSPARENT. and the text alignment is set to
868  * %TA_BASELINE|%TA_LEFT. Note that no font gets selected into the HDC
869  * by this function.
870  *
871  * Some things are done regardless of @mask: If the function in @gc is
872  * any other than %GDK_COPY, the raster operation of the HDC is
873  * set. If @gc has a clip mask, the clip region of the HDC is set.
874  *
875  * Note that the fill style, tile, stipple, and tile and stipple
876  * origins in the @gc are ignored by this function. (In general, tiles
877  * and stipples can't be implemented directly on Win32; you need to do
878  * multiple pass drawing and blitting to implement tiles or
879  * stipples. GDK does just that when you call the GDK drawing
880  * functions with a GC that asks for tiles or stipples.)
881  *
882  * When the HDC is no longer used, it should be released by calling
883  * <function>gdk_win32_hdc_release()</function> with the same
884  * parameters.
885  *
886  * If you modify the HDC by calling <function>SelectObject</function>
887  * you should undo those modifications before calling
888  * <function>gdk_win32_hdc_release()</function>.
889  *
890  * Return value: The HDC.
891  **/
892 HDC
893 gdk_win32_hdc_get (GdkDrawable    *drawable,
894                    GdkGC          *gc,
895                    GdkGCValuesMask usage)
896 {
897   GdkGCWin32 *win32_gc = (GdkGCWin32 *) gc;
898   GdkDrawableImplWin32 *impl = NULL;
899   gboolean ok = TRUE;
900   COLORREF fg = RGB (0, 0, 0);
901   LOGBRUSH logbrush;
902   HPEN hpen;
903   HBRUSH hbr;
904
905   g_assert (win32_gc->hdc == NULL);
906
907   if (GDK_IS_DRAWABLE_IMPL_WIN32 (drawable))
908     impl = GDK_DRAWABLE_IMPL_WIN32(drawable);
909   else if (GDK_IS_WINDOW (drawable))
910     impl = GDK_DRAWABLE_IMPL_WIN32 ((GDK_WINDOW_OBJECT (drawable))->impl);
911   else if (GDK_IS_PIXMAP (drawable))
912     impl = GDK_DRAWABLE_IMPL_WIN32 ((GDK_PIXMAP_OBJECT (drawable))->impl);
913   else
914     g_assert_not_reached ();
915
916   win32_gc->hwnd = impl->handle;
917
918   if (GDK_IS_PIXMAP_IMPL_WIN32 (impl))
919     {
920       if ((win32_gc->hdc = CreateCompatibleDC (NULL)) == NULL)
921         WIN32_GDI_FAILED ("CreateCompatibleDC"), ok = FALSE;
922
923       if (ok && (win32_gc->saved_dc = SaveDC (win32_gc->hdc)) == 0)
924         WIN32_GDI_FAILED ("SaveDC"), ok = FALSE;
925       
926       if (ok && SelectObject (win32_gc->hdc, win32_gc->hwnd) == NULL)
927         WIN32_GDI_FAILED ("SelectObject"), ok = FALSE;
928     }
929   else
930     {
931       if ((win32_gc->hdc = GetDC (win32_gc->hwnd)) == NULL)
932         WIN32_GDI_FAILED ("GetDC");
933       
934       if (ok && (win32_gc->saved_dc = SaveDC (win32_gc->hdc)) == 0)
935         WIN32_GDI_FAILED ("SaveDC");
936     }
937   
938   if (ok && (usage & GDK_GC_FOREGROUND))
939     {
940       fg = predraw_set_foreground (gc, impl->colormap, &ok);
941       if (ok && (hbr = CreateSolidBrush (fg)) == NULL)
942         WIN32_GDI_FAILED ("CreateSolidBrush"), ok = FALSE;
943
944       if (ok && SelectObject (win32_gc->hdc, hbr) == NULL)
945         WIN32_GDI_FAILED ("SelectObject"), ok = FALSE;
946
947       if (ok && SetTextColor (win32_gc->hdc, fg) == CLR_INVALID)
948         WIN32_GDI_FAILED ("SetTextColor"), ok = FALSE;
949     }
950
951   if (ok && (usage & LINE_ATTRIBUTES))
952     {
953       /* Create and select pen */
954       logbrush.lbStyle = BS_SOLID;
955       logbrush.lbColor = fg;
956       logbrush.lbHatch = 0;
957       
958       if (win32_gc->pen_num_dashes > 0 && !IS_WIN_NT ())
959         {
960           /* The Win9x GDI is rather limited so we either draw dashed
961            * lines ourselves (only horizontal and vertical) or let them be
962            * drawn solid to avoid implementing a whole line renderer.
963            */
964           if ((hpen = ExtCreatePen (
965                                     (win32_gc->pen_style & ~(PS_STYLE_MASK)) | PS_SOLID,
966                                     MAX (win32_gc->pen_width, 1),
967                                     &logbrush, 
968                                     0, NULL)) == NULL)
969             WIN32_GDI_FAILED ("ExtCreatePen"), ok = FALSE;
970         }
971       else
972         {
973           if ((hpen = ExtCreatePen (win32_gc->pen_style,
974                                     MAX (win32_gc->pen_width, 1),
975                                     &logbrush, 
976                                     win32_gc->pen_num_dashes,
977                                     win32_gc->pen_dashes)) == NULL)
978             WIN32_GDI_FAILED ("ExtCreatePen"), ok = FALSE;
979         }
980       
981       if (ok && SelectObject (win32_gc->hdc, hpen) == NULL)
982         WIN32_GDI_FAILED ("SelectObject"), ok = FALSE;
983     }
984
985   if (ok && (usage & GDK_GC_FONT))
986     {
987       if (SetBkMode (win32_gc->hdc, TRANSPARENT) == 0)
988         WIN32_GDI_FAILED ("SetBkMode"), ok = FALSE;
989   
990       if (ok && SetTextAlign (win32_gc->hdc, TA_BASELINE|TA_LEFT|TA_NOUPDATECP) == GDI_ERROR)
991         WIN32_GDI_FAILED ("SetTextAlign"), ok = FALSE;
992     }
993   
994   if (ok && win32_gc->rop2 != R2_COPYPEN)
995     if (SetROP2 (win32_gc->hdc, win32_gc->rop2) == 0)
996       WIN32_GDI_FAILED ("SetROP2"), ok = FALSE;
997
998   if (ok &&
999       (win32_gc->values_mask & GDK_GC_CLIP_MASK) &&
1000       win32_gc->hcliprgn != NULL)
1001     {
1002       if (SelectClipRgn (win32_gc->hdc, win32_gc->hcliprgn) == ERROR)
1003         WIN32_API_FAILED ("SelectClipRgn"), ok = FALSE;
1004
1005       if (ok && win32_gc->values_mask & (GDK_GC_CLIP_X_ORIGIN | GDK_GC_CLIP_Y_ORIGIN) &&
1006           OffsetClipRgn (win32_gc->hdc,
1007             win32_gc->values_mask & GDK_GC_CLIP_X_ORIGIN ? gc->clip_x_origin : 0,
1008             win32_gc->values_mask & GDK_GC_CLIP_Y_ORIGIN ? gc->clip_y_origin : 0) == ERROR)
1009         WIN32_API_FAILED ("OffsetClipRgn"), ok = FALSE;
1010     }
1011
1012   GDK_NOTE (GC, (g_print ("gdk_win32_hdc_get: %p (%s): ",
1013                           win32_gc, _gdk_win32_gcvalues_mask_to_string (usage)),
1014                  _gdk_win32_print_dc (win32_gc->hdc)));
1015
1016   return win32_gc->hdc;
1017 }
1018
1019 /**
1020  * gdk_win32_hdc_release:
1021  * @drawable: destination #GdkDrawable
1022  * @gc: #GdkGC to use for drawing on @drawable
1023  * @usage: mask indicating what properties were set up
1024  *
1025  * This function deallocates the Windows device context allocated by
1026  * <funcion>gdk_win32_hdc_get()</function>. It should be called with
1027  * the same parameters.
1028  **/
1029 void
1030 gdk_win32_hdc_release (GdkDrawable    *drawable,
1031                        GdkGC          *gc,
1032                        GdkGCValuesMask usage)
1033 {
1034   GdkGCWin32 *win32_gc = (GdkGCWin32 *) gc;
1035   GdkDrawableImplWin32 *impl = NULL;
1036   HGDIOBJ hpen = NULL;
1037   HGDIOBJ hbr = NULL;
1038
1039   GDK_NOTE (GC, g_print ("gdk_win32_hdc_release: %p: %p (%s)\n",
1040                          win32_gc, win32_gc->hdc,
1041                          _gdk_win32_gcvalues_mask_to_string (usage)));
1042
1043   if (GDK_IS_DRAWABLE_IMPL_WIN32 (drawable))
1044     impl = GDK_DRAWABLE_IMPL_WIN32(drawable);
1045   else if (GDK_IS_WINDOW (drawable))
1046     impl = GDK_DRAWABLE_IMPL_WIN32 ((GDK_WINDOW_OBJECT (drawable))->impl);
1047   else if (GDK_IS_PIXMAP (drawable))
1048     impl = GDK_DRAWABLE_IMPL_WIN32 ((GDK_PIXMAP_OBJECT (drawable))->impl);
1049   else
1050     g_assert_not_reached ();
1051
1052   if (win32_gc->holdpal != NULL)
1053     {
1054       gint k;
1055       
1056       if (!SelectPalette (win32_gc->hdc, win32_gc->holdpal, FALSE))
1057         WIN32_GDI_FAILED ("SelectPalette");
1058       else if ((k = RealizePalette (win32_gc->hdc)) == GDI_ERROR)
1059         WIN32_GDI_FAILED ("RealizePalette");
1060       else if (k > 0)
1061         GDK_NOTE (COLORMAP, g_print ("gdk_win32_hdc_release: realized %p: %d colors\n",
1062                                      win32_gc->holdpal, k));
1063       win32_gc->holdpal = NULL;
1064     }
1065
1066   if (usage & LINE_ATTRIBUTES)
1067     if ((hpen = GetCurrentObject (win32_gc->hdc, OBJ_PEN)) == NULL)
1068       WIN32_GDI_FAILED ("GetCurrentObject");
1069   
1070   if (usage & GDK_GC_FOREGROUND)
1071     if ((hbr = GetCurrentObject (win32_gc->hdc, OBJ_BRUSH)) == NULL)
1072       WIN32_GDI_FAILED ("GetCurrentObject");
1073
1074   GDI_CALL (RestoreDC, (win32_gc->hdc, win32_gc->saved_dc));
1075
1076   if (GDK_IS_PIXMAP_IMPL_WIN32 (impl))
1077     GDI_CALL (DeleteDC, (win32_gc->hdc));
1078   else
1079     GDI_CALL (ReleaseDC, (win32_gc->hwnd, win32_gc->hdc));
1080
1081   if (hpen != NULL)
1082     GDI_CALL (DeleteObject, (hpen));
1083   
1084   if (hbr != NULL)
1085     GDI_CALL (DeleteObject, (hbr));
1086
1087   win32_gc->hdc = NULL;
1088 }
1089
1090 /* This function originally from Jean-Edouard Lachand-Robert, and
1091  * available at www.codeguru.com. Simplified for our needs, not sure
1092  * how much of the original code left any longer. Now handles just
1093  * one-bit deep bitmaps (in Window parlance, ie those that GDK calls
1094  * bitmaps (and not pixmaps), with zero pixels being transparent.
1095  */
1096
1097 /* _gdk_win32_bitmap_to_hrgn : Create a region from the
1098  * "non-transparent" pixels of a bitmap.
1099  */
1100
1101 HRGN
1102 _gdk_win32_bitmap_to_hrgn (GdkPixmap *pixmap)
1103 {
1104   HRGN hRgn = NULL;
1105   HRGN h;
1106   DWORD maxRects;
1107   RGNDATA *pData;
1108   guchar *bits;
1109   gint width, height, bpl;
1110   guchar *p;
1111   gint x, y;
1112
1113   g_assert (GDK_PIXMAP_OBJECT(pixmap)->depth == 1);
1114
1115   bits = GDK_PIXMAP_IMPL_WIN32 (GDK_PIXMAP_OBJECT (pixmap)->impl)->bits;
1116   width = GDK_PIXMAP_IMPL_WIN32 (GDK_PIXMAP_OBJECT (pixmap)->impl)->width;
1117   height = GDK_PIXMAP_IMPL_WIN32 (GDK_PIXMAP_OBJECT (pixmap)->impl)->height;
1118   bpl = ((width - 1)/32 + 1)*4;
1119
1120   /* For better performances, we will use the ExtCreateRegion()
1121    * function to create the region. This function take a RGNDATA
1122    * structure on entry. We will add rectangles by amount of
1123    * ALLOC_UNIT number in this structure.
1124    */
1125   #define ALLOC_UNIT  100
1126   maxRects = ALLOC_UNIT;
1127
1128   pData = g_malloc (sizeof (RGNDATAHEADER) + (sizeof (RECT) * maxRects));
1129   pData->rdh.dwSize = sizeof (RGNDATAHEADER);
1130   pData->rdh.iType = RDH_RECTANGLES;
1131   pData->rdh.nCount = pData->rdh.nRgnSize = 0;
1132   SetRect (&pData->rdh.rcBound, MAXLONG, MAXLONG, 0, 0);
1133
1134   for (y = 0; y < height; y++)
1135     {
1136       /* Scan each bitmap row from left to right*/
1137       p = (guchar *) bits + y * bpl;
1138       for (x = 0; x < width; x++)
1139         {
1140           /* Search for a continuous range of "non transparent pixels"*/
1141           gint x0 = x;
1142           while (x < width)
1143             {
1144               if ((((p[x/8])>>(7-(x%8)))&1) == 0)
1145                 /* This pixel is "transparent"*/
1146                 break;
1147               x++;
1148             }
1149           
1150           if (x > x0)
1151             {
1152               RECT *pr;
1153               /* Add the pixels (x0, y) to (x, y+1) as a new rectangle
1154                * in the region
1155                */
1156               if (pData->rdh.nCount >= maxRects)
1157                 {
1158                   maxRects += ALLOC_UNIT;
1159                   pData = g_realloc (pData, sizeof(RGNDATAHEADER)
1160                                      + (sizeof(RECT) * maxRects));
1161                 }
1162               pr = (RECT *) &pData->Buffer;
1163               SetRect (&pr[pData->rdh.nCount], x0, y, x, y+1);
1164               if (x0 < pData->rdh.rcBound.left)
1165                 pData->rdh.rcBound.left = x0;
1166               if (y < pData->rdh.rcBound.top)
1167                 pData->rdh.rcBound.top = y;
1168               if (x > pData->rdh.rcBound.right)
1169                 pData->rdh.rcBound.right = x;
1170               if (y+1 > pData->rdh.rcBound.bottom)
1171                 pData->rdh.rcBound.bottom = y+1;
1172               pData->rdh.nCount++;
1173               
1174               /* On Windows98, ExtCreateRegion() may fail if the
1175                * number of rectangles is too large (ie: >
1176                * 4000). Therefore, we have to create the region by
1177                * multiple steps.
1178                */
1179               if (pData->rdh.nCount == 2000)
1180                 {
1181                   HRGN h = ExtCreateRegion (NULL, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRects), pData);
1182                   if (hRgn)
1183                     {
1184                       CombineRgn(hRgn, hRgn, h, RGN_OR);
1185                       DeleteObject(h);
1186                     }
1187                   else
1188                     hRgn = h;
1189                   pData->rdh.nCount = 0;
1190                   SetRect (&pData->rdh.rcBound, MAXLONG, MAXLONG, 0, 0);
1191                 }
1192             }
1193         }
1194     }
1195   
1196   /* Create or extend the region with the remaining rectangles*/
1197   h = ExtCreateRegion (NULL, sizeof (RGNDATAHEADER)
1198                        + (sizeof (RECT) * maxRects), pData);
1199   if (hRgn)
1200     {
1201       CombineRgn (hRgn, hRgn, h, RGN_OR);
1202       DeleteObject (h);
1203     }
1204   else
1205     hRgn = h;
1206
1207   /* Clean up*/
1208   g_free (pData);
1209
1210   return hRgn;
1211 }
1212
1213 HRGN
1214 _gdk_win32_gdkregion_to_hrgn (GdkRegion *region,
1215                               gint       x_origin,
1216                               gint       y_origin)
1217 {
1218   HRGN hrgn;
1219   RGNDATA *rgndata;
1220   RECT *rect;
1221   GdkRegionBox *boxes = region->rects;
1222   guint nbytes =
1223     sizeof (RGNDATAHEADER) + (sizeof (RECT) * region->numRects);
1224   int i;
1225
1226   rgndata = g_malloc (nbytes);
1227   rgndata->rdh.dwSize = sizeof (RGNDATAHEADER);
1228   rgndata->rdh.iType = RDH_RECTANGLES;
1229   rgndata->rdh.nCount = rgndata->rdh.nRgnSize = 0;
1230   SetRect (&rgndata->rdh.rcBound,
1231            G_MAXSHORT, G_MAXSHORT, G_MINSHORT, G_MINSHORT);
1232
1233   for (i = 0; i < region->numRects; i++)
1234     {
1235       rect = ((RECT *) rgndata->Buffer) + rgndata->rdh.nCount++;
1236
1237       rect->left = CLAMP (boxes[i].x1 + x_origin,
1238                           G_MINSHORT, G_MAXSHORT);
1239       rect->right = CLAMP (boxes[i].x2 + x_origin,
1240                            G_MINSHORT, G_MAXSHORT);
1241       rect->top = CLAMP (boxes[i].y1 + y_origin,
1242                          G_MINSHORT, G_MAXSHORT);
1243       rect->bottom = CLAMP (boxes[i].y2 + y_origin,
1244                             G_MINSHORT, G_MAXSHORT);
1245
1246       if (rect->left < rgndata->rdh.rcBound.left)
1247         rgndata->rdh.rcBound.left = rect->left;
1248       if (rect->right > rgndata->rdh.rcBound.right)
1249         rgndata->rdh.rcBound.right = rect->right;
1250       if (rect->top < rgndata->rdh.rcBound.top)
1251         rgndata->rdh.rcBound.top = rect->top;
1252       if (rect->bottom > rgndata->rdh.rcBound.bottom)
1253         rgndata->rdh.rcBound.bottom = rect->bottom;
1254     }
1255   if ((hrgn = ExtCreateRegion (NULL, nbytes, rgndata)) == NULL)
1256     WIN32_API_FAILED ("ExtCreateRegion");
1257
1258   g_free (rgndata);
1259
1260   return (hrgn);
1261 }