Debian Codegraph @

gdk/win32/gdkgc-win32.c

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