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
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.
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.
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.
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/.
28 #define LINE_ATTRIBUTES (GDK_GC_LINE_WIDTH|GDK_GC_LINE_STYLE| \
29 GDK_GC_CAP_STYLE|GDK_GC_JOIN_STYLE)
36 #include "gdkpixmap.h"
37 #include "gdkregion-generic.h"
38 #include "gdkprivate-win32.h"
40 static void gdk_win32_gc_get_values (GdkGC *gc,
42 static void gdk_win32_gc_set_values (GdkGC *gc,
44 GdkGCValuesMask values_mask);
45 static void gdk_win32_gc_set_dashes (GdkGC *gc,
50 static void gdk_gc_win32_class_init (GdkGCWin32Class *klass);
51 static void gdk_gc_win32_finalize (GObject *object);
53 static gpointer parent_class = NULL;
56 _gdk_gc_win32_get_type (void)
58 static GType object_type = 0;
62 static const GTypeInfo object_info =
64 sizeof (GdkGCWin32Class),
66 (GBaseFinalizeFunc) NULL,
67 (GClassInitFunc) gdk_gc_win32_class_init,
68 NULL, /* class_finalize */
69 NULL, /* class_data */
72 (GInstanceInitFunc) NULL,
75 object_type = g_type_register_static (GDK_TYPE_GC,
84 gdk_gc_win32_class_init (GdkGCWin32Class *klass)
86 GObjectClass *object_class = G_OBJECT_CLASS (klass);
87 GdkGCClass *gc_class = GDK_GC_CLASS (klass);
89 parent_class = g_type_class_peek_parent (klass);
91 object_class->finalize = gdk_gc_win32_finalize;
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;
99 gdk_gc_win32_finalize (GObject *object)
101 GdkGCWin32 *win32_gc = GDK_GC_WIN32 (object);
103 if (win32_gc->hcliprgn != NULL)
104 DeleteObject (win32_gc->hcliprgn);
106 if (win32_gc->values_mask & GDK_GC_FONT)
107 gdk_font_unref (win32_gc->font);
109 if (win32_gc->values_mask & GDK_GC_TILE)
110 g_object_unref (win32_gc->tile);
112 if (win32_gc->values_mask & GDK_GC_STIPPLE)
113 g_object_unref (win32_gc->stipple);
115 if (win32_gc->pen_dashes)
116 g_free (win32_gc->pen_dashes);
118 G_OBJECT_CLASS (parent_class)->finalize (object);
122 gdk_win32_gc_values_to_win32values (GdkGCValues *values,
123 GdkGCValuesMask mask,
124 GdkGCWin32 *win32_gc)
129 GDK_NOTE (GC, g_print ("{"));
131 if (mask & GDK_GC_FOREGROUND)
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),
139 if (mask & GDK_GC_BACKGROUND)
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),
147 if ((mask & GDK_GC_FONT) && (values->font->type == GDK_FONT_FONT
148 || values->font->type == GDK_FONT_FONTSET))
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)
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),
162 win32_gc->values_mask &= ~GDK_GC_FONT;
163 GDK_NOTE (GC, (g_print ("%sfont=NULL", s),
168 if (mask & GDK_GC_FUNCTION)
170 GDK_NOTE (GC, (g_print ("%srop2=", s),
172 switch (values->function)
174 #define CASE(x,y) case GDK_##x: win32_gc->rop2 = R2_##y; GDK_NOTE (GC, g_print (#y)); break
175 CASE (COPY, COPYPEN);
180 CASE (AND_REVERSE, MASKPENNOT);
181 CASE (AND_INVERT, MASKNOTPEN);
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);
193 win32_gc->values_mask |= GDK_GC_FUNCTION;
196 if (mask & GDK_GC_FILL)
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)),
205 if (mask & GDK_GC_TILE)
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)
212 g_object_ref (win32_gc->tile);
213 win32_gc->values_mask |= GDK_GC_TILE;
215 (g_print ("%stile=%p", s,
216 GDK_PIXMAP_HBITMAP (win32_gc->tile)),
221 win32_gc->values_mask &= ~GDK_GC_TILE;
222 GDK_NOTE (GC, (g_print ("%stile=NULL", s),
227 if (mask & GDK_GC_STIPPLE)
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)
234 gdk_drawable_get_size (win32_gc->stipple, &sw, &sh);
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
240 if ( (sw != 8 || sh != 8)
241 && !IS_WIN_NT ()) /* HB: the MSDN says it's a Win95 limitation */
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
249 GdkPixmap *bm = gdk_bitmap_create_from_data (NULL, dummy, 8, 8);
250 GdkGC *gc = gdk_gc_new (bm);
259 gdk_draw_drawable (bm, gc, win32_gc->stipple, 0, 0, i, j, sw, sh);
264 win32_gc->stipple = bm;
269 g_object_ref (win32_gc->stipple);
270 win32_gc->values_mask |= GDK_GC_STIPPLE;
272 (g_print ("%sstipple=%p", s,
273 GDK_PIXMAP_HBITMAP (win32_gc->stipple)),
278 win32_gc->values_mask &= ~GDK_GC_STIPPLE;
279 GDK_NOTE (GC, (g_print ("%sstipple=NULL", s),
284 if (mask & GDK_GC_CLIP_MASK)
286 if (win32_gc->hcliprgn != NULL)
287 DeleteObject (win32_gc->hcliprgn);
289 if (values->clip_mask != NULL)
291 win32_gc->hcliprgn = _gdk_win32_bitmap_to_hrgn (values->clip_mask);
292 win32_gc->values_mask |= GDK_GC_CLIP_MASK;
296 win32_gc->hcliprgn = NULL;
297 win32_gc->values_mask &= ~GDK_GC_CLIP_MASK;
299 GDK_NOTE (GC, (g_print ("%sclip=%p", s, win32_gc->hcliprgn),
303 if (mask & GDK_GC_SUBWINDOW)
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),
311 if (mask & GDK_GC_TS_X_ORIGIN)
313 win32_gc->values_mask |= GDK_GC_TS_X_ORIGIN;
314 GDK_NOTE (GC, (g_print ("%sts_x=%d", s, values->ts_x_origin),
318 if (mask & GDK_GC_TS_Y_ORIGIN)
320 win32_gc->values_mask |= GDK_GC_TS_Y_ORIGIN;
321 GDK_NOTE (GC, (g_print ("%sts_y=%d", s, values->ts_y_origin),
325 if (mask & GDK_GC_CLIP_X_ORIGIN)
327 win32_gc->values_mask |= GDK_GC_CLIP_X_ORIGIN;
328 GDK_NOTE (GC, (g_print ("%sclip_x=%d", s, values->clip_x_origin),
332 if (mask & GDK_GC_CLIP_Y_ORIGIN)
334 win32_gc->values_mask |= GDK_GC_CLIP_Y_ORIGIN;
335 GDK_NOTE (GC, (g_print ("%sclip_y=%d", s, values->clip_y_origin),
339 if (mask & GDK_GC_EXPOSURES)
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),
347 if (mask & GDK_GC_LINE_WIDTH)
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),
355 if (mask & GDK_GC_LINE_STYLE)
357 switch (values->line_style)
360 if (win32_gc->pen_dashes)
362 g_free (win32_gc->pen_dashes);
363 win32_gc->pen_dashes = NULL;
364 win32_gc->pen_num_dashes = 0;
366 win32_gc->pen_style &= ~(PS_STYLE_MASK);
367 win32_gc->pen_style |= PS_SOLID;
369 case GDK_LINE_ON_OFF_DASH:
370 case GDK_LINE_DOUBLE_DASH:
371 if (!win32_gc->pen_dashes)
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
377 win32_gc->pen_style &= ~(PS_STYLE_MASK);
378 win32_gc->pen_style |= PS_DASH;
382 GDK_NOTE (GC, (g_print ("%sps|=PS_STYLE_%s", s, _gdk_win32_psstyle_to_string (win32_gc->pen_style)),
384 win32_gc->values_mask |= GDK_GC_LINE_STYLE;
387 if (mask & GDK_GC_CAP_STYLE)
389 win32_gc->pen_style &= ~(PS_ENDCAP_MASK);
390 switch (values->cap_style)
392 case GDK_CAP_NOT_LAST: /* ??? */
394 win32_gc->pen_style |= PS_ENDCAP_FLAT;
397 win32_gc->pen_style |= PS_ENDCAP_ROUND;
399 case GDK_CAP_PROJECTING:
400 win32_gc->pen_style |= PS_ENDCAP_SQUARE;
403 GDK_NOTE (GC, (g_print ("%sps|=PS_ENDCAP_%s", s, _gdk_win32_psendcap_to_string (win32_gc->pen_style)),
405 win32_gc->values_mask |= GDK_GC_CAP_STYLE;
408 if (mask & GDK_GC_JOIN_STYLE)
410 win32_gc->pen_style &= ~(PS_JOIN_MASK);
411 switch (values->join_style)
414 win32_gc->pen_style |= PS_JOIN_MITER;
417 win32_gc->pen_style |= PS_JOIN_ROUND;
420 win32_gc->pen_style |= PS_JOIN_BEVEL;
423 GDK_NOTE (GC, (g_print ("%sps|=PS_JOIN_%s", s, _gdk_win32_psjoin_to_string (win32_gc->pen_style)),
425 win32_gc->values_mask |= GDK_GC_JOIN_STYLE;
427 GDK_NOTE (GC, g_print ("} mask=(%s)", _gdk_win32_gcvalues_mask_to_string (win32_gc->values_mask)));
431 _gdk_win32_gc_new (GdkDrawable *drawable,
433 GdkGCValuesMask mask)
436 GdkGCWin32 *win32_gc;
438 /* NOTICE that the drawable here has to be the impl drawable,
439 * not the publically-visible drawables.
441 g_return_val_if_fail (GDK_IS_DRAWABLE_IMPL_WIN32 (drawable), NULL);
443 gc = g_object_new (_gdk_gc_win32_get_type (), NULL);
444 win32_gc = GDK_GC_WIN32 (gc);
446 win32_gc->hcliprgn = NULL;
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.
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;
465 win32_gc->values_mask = GDK_GC_FUNCTION | GDK_GC_FILL;
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"));
471 win32_gc->hdc = NULL;
472 win32_gc->hwnd = NULL;
478 gdk_win32_gc_get_values (GdkGC *gc,
481 GdkGCWin32 *win32_gc = GDK_GC_WIN32 (gc);
483 values->foreground.pixel = win32_gc->foreground;
484 values->background.pixel = win32_gc->background;
485 values->font = win32_gc->font;
487 switch (win32_gc->rop2)
490 values->function = GDK_COPY; break;
492 values->function = GDK_INVERT; break;
494 values->function = GDK_XOR; break;
496 values->function = GDK_CLEAR; break;
498 values->function = GDK_AND; break;
500 values->function = GDK_AND_REVERSE; break;
502 values->function = GDK_AND_INVERT; break;
504 values->function = GDK_NOOP; break;
506 values->function = GDK_OR; break;
508 values->function = GDK_EQUIV; break;
510 values->function = GDK_OR_REVERSE; break;
512 values->function = GDK_COPY_INVERT; break;
514 values->function = GDK_OR_INVERT; break;
516 values->function = GDK_NAND; break;
518 values->function = GDK_NOR; break;
520 values->function = GDK_SET; break;
523 values->fill = win32_gc->fill_style;
525 values->tile = win32_gc->tile;
526 values->stipple = win32_gc->stipple;
528 /* Also the X11 backend always returns a NULL clip_mask */
529 values->clip_mask = NULL;
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;
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;
544 values->line_style = GDK_LINE_SOLID;
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;
552 values->cap_style = GDK_CAP_ROUND;
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;
560 values->join_style = GDK_JOIN_ROUND;
564 gdk_win32_gc_set_values (GdkGC *gc,
566 GdkGCValuesMask mask)
568 g_return_if_fail (GDK_IS_GC (gc));
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"));
576 gdk_win32_gc_set_dashes (GdkGC *gc,
581 GdkGCWin32 *win32_gc;
584 g_return_if_fail (GDK_IS_GC (gc));
585 g_return_if_fail (dash_list != NULL);
587 win32_gc = GDK_GC_WIN32 (gc);
589 /* mark as set, see gdk_win32_gc_values_to_win32values () for the reason */
590 win32_gc->values_mask |= GDK_GC_LINE_STYLE;
592 win32_gc->pen_style &= ~(PS_STYLE_MASK);
594 win32_gc->pen_style |= (PS_GEOMETRIC | PS_USERSTYLE);
595 win32_gc->pen_num_dashes = n;
596 win32_gc->pen_dashes = g_new (DWORD, n);
597 for (i = 0; i < n; i++)
598 win32_gc->pen_dashes[i] = dash_list[i];
602 gdk_gc_set_clip_rectangle (GdkGC *gc,
603 GdkRectangle *rectangle)
605 GdkGCWin32 *win32_gc;
607 g_return_if_fail (GDK_IS_GC (gc));
609 win32_gc = GDK_GC_WIN32 (gc);
611 if (win32_gc->hcliprgn)
612 DeleteObject (win32_gc->hcliprgn);
616 GDK_NOTE (GC, g_print ("gdk_gc_set_clip_rectangle: %p: %s\n",
618 _gdk_win32_gdkrectangle_to_string (rectangle)));
619 win32_gc->hcliprgn = CreateRectRgn (rectangle->x, rectangle->y,
620 rectangle->x + rectangle->width,
621 rectangle->y + rectangle->height);
622 win32_gc->values_mask |= GDK_GC_CLIP_MASK;
626 GDK_NOTE (GC, g_print ("gdk_gc_set_clip_rectangle: NULL\n"));
628 win32_gc->hcliprgn = NULL;
629 win32_gc->values_mask &= ~GDK_GC_CLIP_MASK;
632 gc->clip_x_origin = 0;
633 gc->clip_y_origin = 0;
635 win32_gc->values_mask &= ~(GDK_GC_CLIP_X_ORIGIN | GDK_GC_CLIP_Y_ORIGIN);
639 gdk_gc_set_clip_region (GdkGC *gc,
642 GdkGCWin32 *win32_gc;
644 g_return_if_fail (GDK_IS_GC (gc));
646 win32_gc = GDK_GC_WIN32 (gc);
648 if (win32_gc->hcliprgn)
649 DeleteObject (win32_gc->hcliprgn);
653 GDK_NOTE (GC, g_print ("gdk_gc_set_clip_region: %p: %s\n",
655 _gdk_win32_gdkregion_to_string (region)));
657 win32_gc->hcliprgn = _gdk_win32_gdkregion_to_hrgn (region, 0, 0);
658 win32_gc->values_mask |= GDK_GC_CLIP_MASK;
662 GDK_NOTE (GC, g_print ("gdk_gc_set_clip_region: NULL\n"));
664 win32_gc->hcliprgn = NULL;
665 win32_gc->values_mask &= ~GDK_GC_CLIP_MASK;
668 gc->clip_x_origin = 0;
669 gc->clip_y_origin = 0;
671 win32_gc->values_mask &= ~(GDK_GC_CLIP_X_ORIGIN | GDK_GC_CLIP_Y_ORIGIN);
675 gdk_gc_copy (GdkGC *dst_gc,
678 GdkGCWin32 *dst_win32_gc;
679 GdkGCWin32 *src_win32_gc;
681 g_return_if_fail (GDK_IS_GC_WIN32 (dst_gc));
682 g_return_if_fail (GDK_IS_GC_WIN32 (src_gc));
684 dst_win32_gc = GDK_GC_WIN32 (dst_gc);
685 src_win32_gc = GDK_GC_WIN32 (src_gc);
687 GDK_NOTE (GC, g_print ("gdk_gc_copy: %p := %p\n", dst_win32_gc, src_win32_gc));
689 if (dst_gc->colormap)
690 g_object_unref (G_OBJECT (dst_gc->colormap));
692 if (dst_win32_gc->hcliprgn != NULL)
693 DeleteObject (dst_win32_gc->hcliprgn);
695 if (dst_win32_gc->font != NULL)
696 gdk_font_unref (dst_win32_gc->font);
698 if (dst_win32_gc->tile != NULL)
699 g_object_unref (dst_win32_gc->tile);
701 if (dst_win32_gc->stipple != NULL)
702 g_object_unref (dst_win32_gc->stipple);
704 if (dst_win32_gc->pen_dashes)
705 g_free (dst_win32_gc->pen_dashes);
707 dst_gc->clip_x_origin = src_gc->clip_x_origin;
708 dst_gc->clip_y_origin = src_gc->clip_y_origin;
709 dst_gc->ts_x_origin = src_gc->ts_x_origin;
710 dst_gc->ts_y_origin = src_gc->ts_y_origin;
711 dst_gc->colormap = src_gc->colormap;
712 if (dst_gc->colormap)
713 g_object_ref (G_OBJECT (dst_gc->colormap));
715 dst_win32_gc->hcliprgn = src_win32_gc->hcliprgn;
716 if (dst_win32_gc->hcliprgn)
718 /* create a new region, to copy to */
719 dst_win32_gc->hcliprgn = CreateRectRgn (0,0,1,1);
720 /* overwrite from source */
721 CombineRgn (dst_win32_gc->hcliprgn, src_win32_gc->hcliprgn,
725 dst_win32_gc->values_mask = src_win32_gc->values_mask;
726 dst_win32_gc->foreground = src_win32_gc->foreground;
727 dst_win32_gc->background = src_win32_gc->background;
728 dst_win32_gc->font = src_win32_gc->font;
729 if (dst_win32_gc->font != NULL)
730 gdk_font_ref (dst_win32_gc->font);
732 dst_win32_gc->rop2 = src_win32_gc->rop2;
733 dst_win32_gc->fill_style = src_win32_gc->fill_style;
734 dst_win32_gc->tile = src_win32_gc->tile;
735 if (dst_win32_gc->tile != NULL)
736 g_object_ref (dst_win32_gc->tile);
738 dst_win32_gc->stipple = src_win32_gc->stipple;
739 if (dst_win32_gc->stipple != NULL)
740 g_object_ref (dst_win32_gc->stipple);
742 dst_win32_gc->subwindow_mode = src_win32_gc->subwindow_mode;
743 dst_win32_gc->graphics_exposures = src_win32_gc->graphics_exposures;
744 dst_win32_gc->pen_width = src_win32_gc->pen_width;
745 dst_win32_gc->pen_style = src_win32_gc->pen_style;
746 dst_win32_gc->pen_dashes = src_win32_gc->pen_dashes;
747 if (dst_win32_gc->pen_dashes)
748 dst_win32_gc->pen_dashes = g_memdup (src_win32_gc->pen_dashes,
749 sizeof (DWORD) * src_win32_gc->pen_num_dashes);
750 dst_win32_gc->pen_num_dashes = src_win32_gc->pen_num_dashes;
753 dst_win32_gc->hdc = NULL;
754 dst_win32_gc->saved_dc = FALSE;
755 dst_win32_gc->hwnd = NULL;
756 dst_win32_gc->holdpal = NULL;
760 gdk_gc_get_screen (GdkGC *gc)
762 g_return_val_if_fail (GDK_IS_GC_WIN32 (gc), NULL);
767 static guint bitmask[9] = { 0, 1, 3, 7, 15, 31, 63, 127, 255 };
770 _gdk_win32_colormap_color (GdkColormap *colormap,
773 const GdkVisual *visual;
774 GdkColormapPrivateWin32 *colormap_private;
777 if (colormap == NULL)
778 return DIBINDEX (pixel & 1);
780 colormap_private = GDK_WIN32_COLORMAP_DATA (colormap);
782 g_assert (colormap_private != NULL);
784 visual = colormap->visual;
785 switch (visual->type)
787 case GDK_VISUAL_GRAYSCALE:
788 case GDK_VISUAL_PSEUDO_COLOR:
789 case GDK_VISUAL_STATIC_COLOR:
790 return PALETTEINDEX (pixel);
792 case GDK_VISUAL_TRUE_COLOR:
793 r = (pixel & visual->red_mask) >> visual->red_shift;
794 r = (r * 255) / bitmask[visual->red_prec];
795 g = (pixel & visual->green_mask) >> visual->green_shift;
796 g = (g * 255) / bitmask[visual->green_prec];
797 b = (pixel & visual->blue_mask) >> visual->blue_shift;
798 b = (b * 255) / bitmask[visual->blue_prec];
799 return RGB (r, g, b);
802 g_assert_not_reached ();
808 predraw_set_foreground (GdkGC *gc,
809 GdkColormap *colormap,
813 GdkGCWin32 *win32_gc = (GdkGCWin32 *) gc;
814 GdkColormapPrivateWin32 *colormap_private;
818 (colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR ||
819 colormap->visual->type == GDK_VISUAL_STATIC_COLOR))
821 colormap_private = GDK_WIN32_COLORMAP_DATA (colormap);
823 g_assert (colormap_private != NULL);
825 if (!(win32_gc->holdpal = SelectPalette (win32_gc->hdc, colormap_private->hpal, FALSE)))
826 WIN32_GDI_FAILED ("SelectPalette"), *ok = FALSE;
827 else if ((k = RealizePalette (win32_gc->hdc)) == GDI_ERROR)
828 WIN32_GDI_FAILED ("RealizePalette"), *ok = FALSE;
830 GDK_NOTE (COLORMAP, g_print ("predraw_set_foreground: realized %p: %d colors\n",
831 colormap_private->hpal, k));
834 fg = _gdk_win32_colormap_color (colormap, win32_gc->foreground);
836 GDK_NOTE (GC, g_print ("predraw_set_foreground: fg=%06lx\n", fg));
842 * @drawable: destination #GdkDrawable
843 * @gc: #GdkGC to use for drawing on @drawable
844 * @usage: mask indicating what properties needs to be set up
846 * Allocates a Windows device context handle (HDC) for drawing into
847 * @drawable, and sets it up appropriately according to @usage.
849 * Each #GdkGC can at one time have only one HDC associated with it.
851 * The following flags in @mask are handled:
853 * If %GDK_GC_FOREGROUND is set in @mask, a solid brush of the
854 * foreground color in @gc is selected into the HDC. The text color of
855 * the HDC is also set. If the @drawable has a palette (256-color
856 * mode), the palette is selected and realized.
858 * If any of the line attribute flags (%GDK_GC_LINE_WIDTH,
859 * %GDK_GC_LINE_STYLE, %GDK_GC_CAP_STYLE and %GDK_GC_JOIN_STYLE) is
860 * set in @mask, a solid pen of the foreground color and appropriate
861 * width and stule is created and selected into the HDC. Note that the
862 * dash properties are not completely implemented.
864 * If the %GDK_GC_FONT flag is set, the background mix mode is set to
865 * %TRANSPARENT. and the text alignment is set to
866 * %TA_BASELINE|%TA_LEFT. Note that no font gets selected into the HDC
869 * Some things are done regardless of @mask: If the function in @gc is
870 * any other than %GDK_COPY, the raster operation of the HDC is
871 * set. If @gc has a clip mask, the clip region of the HDC is set.
873 * Note that the fill style, tile, stipple, and tile and stipple
874 * origins in the @gc are ignored by this function. (In general, tiles
875 * and stipples can't be implemented directly on Win32; you need to do
876 * multiple pass drawing and blitting to implement tiles or
877 * stipples. GDK does just that when you call the GDK drawing
878 * functions with a GC that asks for tiles or stipples.)
880 * When the HDC is no longer used, it should be released by calling
881 * <function>gdk_win32_hdc_release()</function> with the same
884 * If you modify the HDC by calling <function>SelectObject</function>
885 * you should undo those modifications before calling
886 * <function>gdk_win32_hdc_release()</function>.
888 * Return value: The HDC.
891 gdk_win32_hdc_get (GdkDrawable *drawable,
893 GdkGCValuesMask usage)
895 GdkGCWin32 *win32_gc = (GdkGCWin32 *) gc;
896 GdkDrawableImplWin32 *impl = NULL;
898 COLORREF fg = RGB (0, 0, 0);
903 g_assert (win32_gc->hdc == NULL);
905 if (GDK_IS_DRAWABLE_IMPL_WIN32 (drawable))
906 impl = GDK_DRAWABLE_IMPL_WIN32(drawable);
907 else if (GDK_IS_WINDOW (drawable))
908 impl = GDK_DRAWABLE_IMPL_WIN32 ((GDK_WINDOW_OBJECT (drawable))->impl);
909 else if (GDK_IS_PIXMAP (drawable))
910 impl = GDK_DRAWABLE_IMPL_WIN32 ((GDK_PIXMAP_OBJECT (drawable))->impl);
912 g_assert_not_reached ();
914 win32_gc->hwnd = impl->handle;
916 if (GDK_IS_PIXMAP_IMPL_WIN32 (impl))
918 if ((win32_gc->hdc = CreateCompatibleDC (NULL)) == NULL)
919 WIN32_GDI_FAILED ("CreateCompatibleDC"), ok = FALSE;
921 if (ok && (win32_gc->saved_dc = SaveDC (win32_gc->hdc)) == 0)
922 WIN32_GDI_FAILED ("SaveDC"), ok = FALSE;
924 if (ok && SelectObject (win32_gc->hdc, win32_gc->hwnd) == NULL)
925 WIN32_GDI_FAILED ("SelectObject"), ok = FALSE;
929 if ((win32_gc->hdc = GetDC (win32_gc->hwnd)) == NULL)
930 WIN32_GDI_FAILED ("GetDC");
932 if (ok && (win32_gc->saved_dc = SaveDC (win32_gc->hdc)) == 0)
933 WIN32_GDI_FAILED ("SaveDC");
936 if (ok && (usage & GDK_GC_FOREGROUND))
938 fg = predraw_set_foreground (gc, impl->colormap, &ok);
939 if (ok && (hbr = CreateSolidBrush (fg)) == NULL)
940 WIN32_GDI_FAILED ("CreateSolidBrush"), ok = FALSE;
942 if (ok && SelectObject (win32_gc->hdc, hbr) == NULL)
943 WIN32_GDI_FAILED ("SelectObject"), ok = FALSE;
945 if (ok && SetTextColor (win32_gc->hdc, fg) == CLR_INVALID)
946 WIN32_GDI_FAILED ("SetTextColor"), ok = FALSE;
949 if (ok && (usage & LINE_ATTRIBUTES))
951 /* Create and select pen */
952 logbrush.lbStyle = BS_SOLID;
953 logbrush.lbColor = fg;
954 logbrush.lbHatch = 0;
956 if (win32_gc->pen_num_dashes > 0 && !IS_WIN_NT ())
958 /* The Win9x GDI is rather limited so we either draw dashed
959 * lines ourselves (only horizontal and vertical) or let them be
960 * drawn solid to avoid implementing a whole line renderer.
962 if ((hpen = ExtCreatePen (
963 (win32_gc->pen_style & ~(PS_STYLE_MASK)) | PS_SOLID,
964 MAX (win32_gc->pen_width, 1),
967 WIN32_GDI_FAILED ("ExtCreatePen"), ok = FALSE;
971 if ((hpen = ExtCreatePen (win32_gc->pen_style,
972 MAX (win32_gc->pen_width, 1),
974 win32_gc->pen_num_dashes,
975 win32_gc->pen_dashes)) == NULL)
976 WIN32_GDI_FAILED ("ExtCreatePen"), ok = FALSE;
979 if (ok && SelectObject (win32_gc->hdc, hpen) == NULL)
980 WIN32_GDI_FAILED ("SelectObject"), ok = FALSE;
983 if (ok && (usage & GDK_GC_FONT))
985 if (SetBkMode (win32_gc->hdc, TRANSPARENT) == 0)
986 WIN32_GDI_FAILED ("SetBkMode"), ok = FALSE;
988 if (ok && SetTextAlign (win32_gc->hdc, TA_BASELINE|TA_LEFT|TA_NOUPDATECP) == GDI_ERROR)
989 WIN32_GDI_FAILED ("SetTextAlign"), ok = FALSE;
992 if (ok && win32_gc->rop2 != R2_COPYPEN)
993 if (SetROP2 (win32_gc->hdc, win32_gc->rop2) == 0)
994 WIN32_GDI_FAILED ("SetROP2"), ok = FALSE;
997 (win32_gc->values_mask & GDK_GC_CLIP_MASK) &&
998 win32_gc->hcliprgn != NULL)
1000 if (SelectClipRgn (win32_gc->hdc, win32_gc->hcliprgn) == ERROR)
1001 WIN32_API_FAILED ("SelectClipRgn"), ok = FALSE;
1003 if (ok && win32_gc->values_mask & (GDK_GC_CLIP_X_ORIGIN | GDK_GC_CLIP_Y_ORIGIN) &&
1004 OffsetClipRgn (win32_gc->hdc,
1005 win32_gc->values_mask & GDK_GC_CLIP_X_ORIGIN ? gc->clip_x_origin : 0,
1006 win32_gc->values_mask & GDK_GC_CLIP_Y_ORIGIN ? gc->clip_y_origin : 0) == ERROR)
1007 WIN32_API_FAILED ("OffsetClipRgn"), ok = FALSE;
1010 GDK_NOTE (GC, (g_print ("gdk_win32_hdc_get: %p (%s): ",
1011 win32_gc, _gdk_win32_gcvalues_mask_to_string (usage)),
1012 _gdk_win32_print_dc (win32_gc->hdc)));
1014 return win32_gc->hdc;
1018 * gdk_win32_hdc_release:
1019 * @drawable: destination #GdkDrawable
1020 * @gc: #GdkGC to use for drawing on @drawable
1021 * @usage: mask indicating what properties were set up
1023 * This function deallocates the Windows device context allocated by
1024 * <funcion>gdk_win32_hdc_get()</function>. It should be called with
1025 * the same parameters.
1028 gdk_win32_hdc_release (GdkDrawable *drawable,
1030 GdkGCValuesMask usage)
1032 GdkGCWin32 *win32_gc = (GdkGCWin32 *) gc;
1033 GdkDrawableImplWin32 *impl = NULL;
1034 HGDIOBJ hpen = NULL;
1037 GDK_NOTE (GC, g_print ("gdk_win32_hdc_release: %p: %p (%s)\n",
1038 win32_gc, win32_gc->hdc,
1039 _gdk_win32_gcvalues_mask_to_string (usage)));
1041 if (GDK_IS_DRAWABLE_IMPL_WIN32 (drawable))
1042 impl = GDK_DRAWABLE_IMPL_WIN32(drawable);
1043 else if (GDK_IS_WINDOW (drawable))
1044 impl = GDK_DRAWABLE_IMPL_WIN32 ((GDK_WINDOW_OBJECT (drawable))->impl);
1045 else if (GDK_IS_PIXMAP (drawable))
1046 impl = GDK_DRAWABLE_IMPL_WIN32 ((GDK_PIXMAP_OBJECT (drawable))->impl);
1048 g_assert_not_reached ();
1050 if (win32_gc->holdpal != NULL)
1054 if (!SelectPalette (win32_gc->hdc, win32_gc->holdpal, FALSE))
1055 WIN32_GDI_FAILED ("SelectPalette");
1056 else if ((k = RealizePalette (win32_gc->hdc)) == GDI_ERROR)
1057 WIN32_GDI_FAILED ("RealizePalette");
1059 GDK_NOTE (COLORMAP, g_print ("gdk_win32_hdc_release: realized %p: %d colors\n",
1060 win32_gc->holdpal, k));
1061 win32_gc->holdpal = NULL;
1064 if (usage & LINE_ATTRIBUTES)
1065 if ((hpen = GetCurrentObject (win32_gc->hdc, OBJ_PEN)) == NULL)
1066 WIN32_GDI_FAILED ("GetCurrentObject");
1068 if (usage & GDK_GC_FOREGROUND)
1069 if ((hbr = GetCurrentObject (win32_gc->hdc, OBJ_BRUSH)) == NULL)
1070 WIN32_GDI_FAILED ("GetCurrentObject");
1072 GDI_CALL (RestoreDC, (win32_gc->hdc, win32_gc->saved_dc));
1074 if (GDK_IS_PIXMAP_IMPL_WIN32 (impl))
1075 GDI_CALL (DeleteDC, (win32_gc->hdc));
1077 GDI_CALL (ReleaseDC, (win32_gc->hwnd, win32_gc->hdc));
1080 GDI_CALL (DeleteObject, (hpen));
1083 GDI_CALL (DeleteObject, (hbr));
1085 win32_gc->hdc = NULL;
1088 /* This function originally from Jean-Edouard Lachand-Robert, and
1089 * available at www.codeguru.com. Simplified for our needs, not sure
1090 * how much of the original code left any longer. Now handles just
1091 * one-bit deep bitmaps (in Window parlance, ie those that GDK calls
1092 * bitmaps (and not pixmaps), with zero pixels being transparent.
1095 /* _gdk_win32_bitmap_to_hrgn : Create a region from the
1096 * "non-transparent" pixels of a bitmap.
1100 _gdk_win32_bitmap_to_hrgn (GdkPixmap *pixmap)
1107 gint width, height, bpl;
1111 g_assert (GDK_PIXMAP_OBJECT(pixmap)->depth == 1);
1113 bits = GDK_PIXMAP_IMPL_WIN32 (GDK_PIXMAP_OBJECT (pixmap)->impl)->bits;
1114 width = GDK_PIXMAP_IMPL_WIN32 (GDK_PIXMAP_OBJECT (pixmap)->impl)->width;
1115 height = GDK_PIXMAP_IMPL_WIN32 (GDK_PIXMAP_OBJECT (pixmap)->impl)->height;
1116 bpl = ((width - 1)/32 + 1)*4;
1118 /* For better performances, we will use the ExtCreateRegion()
1119 * function to create the region. This function take a RGNDATA
1120 * structure on entry. We will add rectangles by amount of
1121 * ALLOC_UNIT number in this structure.
1123 #define ALLOC_UNIT 100
1124 maxRects = ALLOC_UNIT;
1126 pData = g_malloc (sizeof (RGNDATAHEADER) + (sizeof (RECT) * maxRects));
1127 pData->rdh.dwSize = sizeof (RGNDATAHEADER);
1128 pData->rdh.iType = RDH_RECTANGLES;
1129 pData->rdh.nCount = pData->rdh.nRgnSize = 0;
1130 SetRect (&pData->rdh.rcBound, MAXLONG, MAXLONG, 0, 0);
1132 for (y = 0; y < height; y++)
1134 /* Scan each bitmap row from left to right*/
1135 p = (guchar *) bits + y * bpl;
1136 for (x = 0; x < width; x++)
1138 /* Search for a continuous range of "non transparent pixels"*/
1142 if ((((p[x/8])>>(7-(x%8)))&1) == 0)
1143 /* This pixel is "transparent"*/
1151 /* Add the pixels (x0, y) to (x, y+1) as a new rectangle
1154 if (pData->rdh.nCount >= maxRects)
1156 maxRects += ALLOC_UNIT;
1157 pData = g_realloc (pData, sizeof(RGNDATAHEADER)
1158 + (sizeof(RECT) * maxRects));
1160 pr = (RECT *) &pData->Buffer;
1161 SetRect (&pr[pData->rdh.nCount], x0, y, x, y+1);
1162 if (x0 < pData->rdh.rcBound.left)
1163 pData->rdh.rcBound.left = x0;
1164 if (y < pData->rdh.rcBound.top)
1165 pData->rdh.rcBound.top = y;
1166 if (x > pData->rdh.rcBound.right)
1167 pData->rdh.rcBound.right = x;
1168 if (y+1 > pData->rdh.rcBound.bottom)
1169 pData->rdh.rcBound.bottom = y+1;
1170 pData->rdh.nCount++;
1172 /* On Windows98, ExtCreateRegion() may fail if the
1173 * number of rectangles is too large (ie: >
1174 * 4000). Therefore, we have to create the region by
1177 if (pData->rdh.nCount == 2000)
1179 HRGN h = ExtCreateRegion (NULL, sizeof(RGNDATAHEADER) + (sizeof(RECT) * maxRects), pData);
1182 CombineRgn(hRgn, hRgn, h, RGN_OR);
1187 pData->rdh.nCount = 0;
1188 SetRect (&pData->rdh.rcBound, MAXLONG, MAXLONG, 0, 0);
1194 /* Create or extend the region with the remaining rectangles*/
1195 h = ExtCreateRegion (NULL, sizeof (RGNDATAHEADER)
1196 + (sizeof (RECT) * maxRects), pData);
1199 CombineRgn (hRgn, hRgn, h, RGN_OR);
1212 _gdk_win32_gdkregion_to_hrgn (GdkRegion *region,
1219 GdkRegionBox *boxes = region->rects;
1221 sizeof (RGNDATAHEADER) + (sizeof (RECT) * region->numRects);
1224 rgndata = g_malloc (nbytes);
1225 rgndata->rdh.dwSize = sizeof (RGNDATAHEADER);
1226 rgndata->rdh.iType = RDH_RECTANGLES;
1227 rgndata->rdh.nCount = rgndata->rdh.nRgnSize = 0;
1228 SetRect (&rgndata->rdh.rcBound,
1229 G_MAXSHORT, G_MAXSHORT, G_MINSHORT, G_MINSHORT);
1231 for (i = 0; i < region->numRects; i++)
1233 rect = ((RECT *) rgndata->Buffer) + rgndata->rdh.nCount++;
1235 rect->left = CLAMP (boxes[i].x1 + x_origin,
1236 G_MINSHORT, G_MAXSHORT);
1237 rect->right = CLAMP (boxes[i].x2 + x_origin,
1238 G_MINSHORT, G_MAXSHORT);
1239 rect->top = CLAMP (boxes[i].y1 + y_origin,
1240 G_MINSHORT, G_MAXSHORT);
1241 rect->bottom = CLAMP (boxes[i].y2 + y_origin,
1242 G_MINSHORT, G_MAXSHORT);
1244 if (rect->left < rgndata->rdh.rcBound.left)
1245 rgndata->rdh.rcBound.left = rect->left;
1246 if (rect->right > rgndata->rdh.rcBound.right)
1247 rgndata->rdh.rcBound.right = rect->right;
1248 if (rect->top < rgndata->rdh.rcBound.top)
1249 rgndata->rdh.rcBound.top = rect->top;
1250 if (rect->bottom > rgndata->rdh.rcBound.bottom)
1251 rgndata->rdh.rcBound.bottom = rect->bottom;
1253 if ((hrgn = ExtCreateRegion (NULL, nbytes, rgndata)) == NULL)
1254 WIN32_API_FAILED ("ExtCreateRegion");