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