Debian Codegraph @

gdk/win32/gdkdrawable-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) 2001-2005 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 #include "config.h"
30 #include <math.h>
31 #include <stdio.h>
32 #include <glib.h>
33
34 #include <pango/pangowin32.h>
35 #include <cairo-win32.h>
36
37 #include "gdkscreen.h" /* gdk_screen_get_default() */
38 #include "gdkregion-generic.h"
39 #include "gdkprivate-win32.h"
40
41 #define ROP3_D 0x00AA0029
42 #define ROP3_DSna 0x00220326
43 #define ROP3_DSPDxax 0x00E20746
44
45 #define LINE_ATTRIBUTES (GDK_GC_LINE_WIDTH|GDK_GC_LINE_STYLE| \
46 GDK_GC_CAP_STYLE|GDK_GC_JOIN_STYLE)
47
48 #define MUST_RENDER_DASHES_MANUALLY(gcwin32) \
49 (gcwin32->line_style == GDK_LINE_DOUBLE_DASH || \
50 (gcwin32->line_style == GDK_LINE_ON_OFF_DASH && gcwin32->pen_dash_offset))
51
52 static void gdk_win32_draw_rectangle (GdkDrawable *drawable,
53 GdkGC *gc,
54 gboolean filled,
55 gint x,
56 gint y,
57 gint width,
58 gint height);
59 static void gdk_win32_draw_arc (GdkDrawable *drawable,
60 GdkGC *gc,
61 gboolean filled,
62 gint x,
63 gint y,
64 gint width,
65 gint height,
66 gint angle1,
67 gint angle2);
68 static void gdk_win32_draw_polygon (GdkDrawable *drawable,
69 GdkGC *gc,
70 gboolean filled,
71 GdkPoint *points,
72 gint npoints);
73 static void gdk_win32_draw_text (GdkDrawable *drawable,
74 GdkFont *font,
75 GdkGC *gc,
76 gint x,
77 gint y,
78 const gchar *text,
79 gint text_length);
80 static void gdk_win32_draw_text_wc (GdkDrawable *drawable,
81 GdkFont *font,
82 GdkGC *gc,
83 gint x,
84 gint y,
85 const GdkWChar *text,
86 gint text_length);
87 static void gdk_win32_draw_drawable (GdkDrawable *drawable,
88 GdkGC *gc,
89 GdkPixmap *src,
90 gint xsrc,
91 gint ysrc,
92 gint xdest,
93 gint ydest,
94 gint width,
95 gint height,
96 GdkDrawable *original_src);
97 static void gdk_win32_draw_points (GdkDrawable *drawable,
98 GdkGC *gc,
99 GdkPoint *points,
100 gint npoints);
101 static void gdk_win32_draw_segments (GdkDrawable *drawable,
102 GdkGC *gc,
103 GdkSegment *segs,
104 gint nsegs);
105 static void gdk_win32_draw_lines (GdkDrawable *drawable,
106 GdkGC *gc,
107 GdkPoint *points,
108 gint npoints);
109 static void gdk_win32_draw_image (GdkDrawable *drawable,
110 GdkGC *gc,
111 GdkImage *image,
112 gint xsrc,
113 gint ysrc,
114 gint xdest,
115 gint ydest,
116 gint width,
117 gint height);
118 static void gdk_win32_draw_pixbuf (GdkDrawable *drawable,
119 GdkGC *gc,
120 GdkPixbuf *pixbuf,
121 gint src_x,
122 gint src_y,
123 gint dest_x,
124 gint dest_y,
125 gint width,
126 gint height,
127 GdkRgbDither dither,
128 gint x_dither,
129 gint y_dither);
130
131 static cairo_surface_t *gdk_win32_ref_cairo_surface (GdkDrawable *drawable);
132
133 static void gdk_win32_set_colormap (GdkDrawable *drawable,
134 GdkColormap *colormap);
135
136 static GdkColormap* gdk_win32_get_colormap (GdkDrawable *drawable);
137
138 static gint gdk_win32_get_depth (GdkDrawable *drawable);
139
140 static GdkScreen * gdk_win32_get_screen (GdkDrawable *drawable);
141
142 static GdkVisual* gdk_win32_get_visual (GdkDrawable *drawable);
143
144 static void gdk_drawable_impl_win32_finalize (GObject *object);
145
146 static const cairo_user_data_key_t gdk_win32_cairo_key;
147 static const cairo_user_data_key_t gdk_win32_cairo_hdc_key;
148
149 G_DEFINE_TYPE (GdkDrawableImplWin32, _gdk_drawable_impl_win32, GDK_TYPE_DRAWABLE)
150
151
152 static void
153 _gdk_drawable_impl_win32_class_init (GdkDrawableImplWin32Class *klass)
154 {
155 GdkDrawableClass *drawable_class = GDK_DRAWABLE_CLASS (klass);
156 GObjectClass *object_class = G_OBJECT_CLASS (klass);
157
158 object_class->finalize = gdk_drawable_impl_win32_finalize;
159
160 drawable_class->create_gc = _gdk_win32_gc_new;
161 drawable_class->draw_rectangle = gdk_win32_draw_rectangle;
162 drawable_class->draw_arc = gdk_win32_draw_arc;
163 drawable_class->draw_polygon = gdk_win32_draw_polygon;
164 drawable_class->draw_text = gdk_win32_draw_text;
165 drawable_class->draw_text_wc = gdk_win32_draw_text_wc;
166 drawable_class->draw_drawable_with_src = gdk_win32_draw_drawable;
167 drawable_class->draw_points = gdk_win32_draw_points;
168 drawable_class->draw_segments = gdk_win32_draw_segments;
169 drawable_class->draw_lines = gdk_win32_draw_lines;
170 drawable_class->draw_image = gdk_win32_draw_image;
171 drawable_class->draw_pixbuf = gdk_win32_draw_pixbuf;
172
173 drawable_class->ref_cairo_surface = gdk_win32_ref_cairo_surface;
174
175 drawable_class->set_colormap = gdk_win32_set_colormap;
176 drawable_class->get_colormap = gdk_win32_get_colormap;
177
178 drawable_class->get_depth = gdk_win32_get_depth;
179 drawable_class->get_screen = gdk_win32_get_screen;
180 drawable_class->get_visual = gdk_win32_get_visual;
181
182 drawable_class->_copy_to_image = _gdk_win32_copy_to_image;
183 }
184
185 static void
186 _gdk_drawable_impl_win32_init (GdkDrawableImplWin32 *impl)
187 {
188 }
189
190 static void
191 gdk_drawable_impl_win32_finalize (GObject *object)
192 {
193 gdk_drawable_set_colormap (GDK_DRAWABLE (object), NULL);
194
195 G_OBJECT_CLASS (_gdk_drawable_impl_win32_parent_class)->finalize (object);
196 }
197
198 /*****************************************************
199 * Win32 specific implementations of generic functions *
200 *****************************************************/
201
202 static GdkColormap*
203 gdk_win32_get_colormap (GdkDrawable *drawable)
204 {
205 return GDK_DRAWABLE_IMPL_WIN32 (drawable)->colormap;
206 }
207
208 static void
209 gdk_win32_set_colormap (GdkDrawable *drawable,
210 GdkColormap *colormap)
211 {
212 GdkDrawableImplWin32 *impl = GDK_DRAWABLE_IMPL_WIN32 (drawable);
213
214 if (impl->colormap == colormap)
215 return;
216
217 if (impl->colormap)
218 g_object_unref (impl->colormap);
219 impl->colormap = colormap;
220 if (impl->colormap)
221 g_object_ref (impl->colormap);
222 }
223
224 /* Drawing
225 */
226
227 static int
228 rop2_to_rop3 (int rop2)
229 {
230 switch (rop2)
231 {
232 /* Oh, Microsoft's silly names for binary and ternary rops. */
233 #define CASE(rop2,rop3) case R2_##rop2: return rop3
234 CASE (BLACK, BLACKNESS);
235 CASE (NOTMERGEPEN, NOTSRCERASE);
236 CASE (MASKNOTPEN, 0x00220326);
237 CASE (NOTCOPYPEN, NOTSRCCOPY);
238 CASE (MASKPENNOT, SRCERASE);
239 CASE (NOT, DSTINVERT);
240 CASE (XORPEN, SRCINVERT);
241 CASE (NOTMASKPEN, 0x007700E6);
242 CASE (MASKPEN, SRCAND);
243 CASE (NOTXORPEN, 0x00990066);
244 CASE (NOP, 0x00AA0029);
245 CASE (MERGENOTPEN, MERGEPAINT);
246 CASE (COPYPEN, SRCCOPY);
247 CASE (MERGEPENNOT, 0x00DD0228);
248 CASE (MERGEPEN, SRCPAINT);
249 CASE (WHITE, WHITENESS);
250 #undef CASE
251 default: return SRCCOPY;
252 }
253 }
254
255 static int
256 rop2_to_patblt_rop (int rop2)
257 {
258 switch (rop2)
259 {
260 #define CASE(rop2,patblt_rop) case R2_##rop2: return patblt_rop
261 CASE (COPYPEN, PATCOPY);
262 CASE (XORPEN, PATINVERT);
263 CASE (NOT, DSTINVERT);
264 CASE (BLACK, BLACKNESS);
265 CASE (WHITE, WHITENESS);
266 #undef CASE
267 default:
268 g_warning ("Unhandled rop2 in GC to be used in PatBlt: %#x", rop2);
269 return PATCOPY;
270 }
271 }
272
273 static inline int
274 align_with_dash_offset (int a, DWORD *dashes, int num_dashes, GdkGCWin32 *gcwin32)
275 {
276 int n = 0;
277 int len_sum = 0;
278 /*
279 * We can't simply add the dashoffset, it can be an arbitrary larger
280 * or smaller value not even between x1 and x2. It just says use the
281 * dash pattern aligned to the offset. So ensure x1 is smaller _x1
282 * and we start with the appropriate dash.
283 */
284 for (n = 0; n < num_dashes; n++)
285 len_sum += dashes[n];
286 if ( len_sum > 0 /* pathological api usage? */
287 && gcwin32->pen_dash_offset > a)
288 a -= (((gcwin32->pen_dash_offset/len_sum - a/len_sum) + 1) * len_sum);
289 else
290 a = gcwin32->pen_dash_offset;
291
292 return a;
293 }
294
295 /* Render a dashed line 'by hand'. Used for all dashes on Win9x (where
296 * GDI is way too limited), and for double dashes on all Windowses.
297 */
298 static inline gboolean
299 render_line_horizontal (GdkGCWin32 *gcwin32,
300 int x1,
301 int x2,
302 int y)
303 {
304 int n = 0;
305 const int pen_width = MAX (gcwin32->pen_width, 1);
306 const int _x1 = x1;
307
308 g_assert (gcwin32->pen_dashes);
309
310 x1 = align_with_dash_offset (x1, gcwin32->pen_dashes, gcwin32->pen_num_dashes, gcwin32);
311
312 for (n = 0; x1 < x2; n++)
313 {
314 int len = gcwin32->pen_dashes[n % gcwin32->pen_num_dashes];
315 if (x1 + len > x2)
316 len = x2 - x1;
317
318 if (n % 2 == 0 && x1 + len > _x1)
319 if (!GDI_CALL (PatBlt, (gcwin32->hdc,
320 x1 < _x1 ? _x1 : x1,
321 y - pen_width / 2,
322 len, pen_width,
323 rop2_to_patblt_rop (gcwin32->rop2))))
324 return FALSE;
325
326 x1 += gcwin32->pen_dashes[n % gcwin32->pen_num_dashes];
327 }
328
329 if (gcwin32->line_style == GDK_LINE_DOUBLE_DASH)
330 {
331 HBRUSH hbr;
332
333 if ((hbr = SelectObject (gcwin32->hdc, gcwin32->pen_hbrbg)) == HGDI_ERROR)
334 return FALSE;
335 x1 = _x1;
336 x1 += gcwin32->pen_dash_offset;
337 for (n = 0; x1 < x2; n++)
338 {
339 int len = gcwin32->pen_dashes[n % gcwin32->pen_num_dashes];
340 if (x1 + len > x2)
341 len = x2 - x1;
342
343 if (n % 2)
344 if (!GDI_CALL (PatBlt, (gcwin32->hdc, x1, y - pen_width / 2,
345 len, pen_width,
346 rop2_to_patblt_rop (gcwin32->rop2))))
347 return FALSE;
348
349 x1 += gcwin32->pen_dashes[n % gcwin32->pen_num_dashes];
350 }
351 if (SelectObject (gcwin32->hdc, hbr) == HGDI_ERROR)
352 return FALSE;
353 }
354
355 return TRUE;
356 }
357
358 static inline gboolean
359 render_line_vertical (GdkGCWin32 *gcwin32,
360 int x,
361 int y1,
362 int y2)
363 {
364 int n;
365 const int pen_width = MAX (gcwin32->pen_width, 1);
366 const int _y1 = y1;
367
368 g_assert (gcwin32->pen_dashes);
369
370 y1 = align_with_dash_offset (y1, gcwin32->pen_dashes, gcwin32->pen_num_dashes, gcwin32);
371 for (n = 0; y1 < y2; n++)
372 {
373 int len = gcwin32->pen_dashes[n % gcwin32->pen_num_dashes];
374 if (y1 + len > y2)
375 len = y2 - y1;
376 if (n % 2 == 0 && y1 + len > _y1)
377 if (!GDI_CALL (PatBlt, (gcwin32->hdc, x - pen_width / 2,
378 y1 < _y1 ? _y1 : y1,
379 pen_width, len,
380 rop2_to_patblt_rop (gcwin32->rop2))))
381 return FALSE;
382
383 y1 += gcwin32->pen_dashes[n % gcwin32->pen_num_dashes];
384 }
385
386 if (gcwin32->line_style == GDK_LINE_DOUBLE_DASH)
387 {
388 HBRUSH hbr;
389
390 if ((hbr = SelectObject (gcwin32->hdc, gcwin32->pen_hbrbg)) == HGDI_ERROR)
391 return FALSE;
392 y1 = _y1;
393 y1 += gcwin32->pen_dash_offset;
394 for (n = 0; y1 < y2; n++)
395 {
396 int len = gcwin32->pen_dashes[n % gcwin32->pen_num_dashes];
397 if (y1 + len > y2)
398 len = y2 - y1;
399 if (n % 2)
400 if (!GDI_CALL (PatBlt, (gcwin32->hdc, x - pen_width / 2, y1,
401 pen_width, len,
402 rop2_to_patblt_rop (gcwin32->rop2))))
403 return FALSE;
404
405 y1 += gcwin32->pen_dashes[n % gcwin32->pen_num_dashes];
406 }
407 if (SelectObject (gcwin32->hdc, hbr) == HGDI_ERROR)
408 return FALSE;
409 }
410
411 return TRUE;
412 }
413
414 static void
415 draw_tiles_lowlevel (HDC dest,
416 HDC tile,
417 int rop3,
418 gint dest_x,
419 gint dest_y,
420 gint tile_x_origin,
421 gint tile_y_origin,
422 gint width,
423 gint height,
424 gint tile_width,
425 gint tile_height)
426 {
427 gint x, y;
428
429 GDK_NOTE (DRAW, g_print ("draw_tiles_lowlevel: %p %+d%+d tile=%p:%dx%d@%+d%+d %dx%d\n",
430 dest,
431 dest_x, dest_y,
432 tile, tile_width, tile_height,
433 tile_x_origin, tile_y_origin,
434 width, height));
435
436 y = tile_y_origin % tile_height;
437 if (y > 0)
438 y -= tile_height;
439 while (y < dest_y + height)
440 {
441 if (y + tile_height >= dest_y)
442 {
443 x = tile_x_origin % tile_width;
444 if (x > 0)
445 x -= tile_width;
446 while (x < dest_x + width)
447 {
448 if (x + tile_width >= dest_x)
449 {
450 gint src_x = MAX (0, dest_x - x);
451 gint src_y = MAX (0, dest_y - y);
452
453 if (!GDI_CALL (BitBlt, (dest, x + src_x, y + src_y,
454 MIN (tile_width, dest_x + width - (x + src_x)),
455 MIN (tile_height, dest_y + height - (y + src_y)),
456 tile,
457 src_x, src_y,
458 rop3)))
459 return;
460 }
461 x += tile_width;
462 }
463 }
464 y += tile_height;
465 }
466 }
467
468 static void
469 draw_tiles (GdkDrawable *drawable,
470 GdkGC *gc,
471 int rop3,
472 GdkPixmap *tile,
473 gint dest_x,
474 gint dest_y,
475 gint tile_x_origin,
476 gint tile_y_origin,
477 gint width,
478 gint height)
479 {
480 const GdkGCValuesMask mask = GDK_GC_FOREGROUND;
481 gint tile_width, tile_height;
482 GdkGC *gc_copy;
483 HDC dest_hdc, tile_hdc;
484
485 gc_copy = gdk_gc_new (tile);
486 gdk_gc_copy (gc_copy, gc);
487 dest_hdc = gdk_win32_hdc_get (drawable, gc, mask);
488 tile_hdc = gdk_win32_hdc_get (tile, gc_copy, mask);
489
490 gdk_drawable_get_size (tile, &tile_width, &tile_height);
491
492 draw_tiles_lowlevel (dest_hdc, tile_hdc, rop3,
493 dest_x, dest_y, tile_x_origin, tile_y_origin,
494 width, height, tile_width, tile_height);
495
496 gdk_win32_hdc_release (drawable, gc, mask);
497 gdk_win32_hdc_release (tile, gc_copy, mask);
498 g_object_unref (gc_copy);
499 }
500
501 static void
502 generic_draw (GdkDrawable *drawable,
503 GdkGC *gc,
504 GdkGCValuesMask mask,
505 void (*function) (GdkGCWin32 *, HDC, gint, gint, va_list),
506 const GdkRegion *region,
507 ...)
508 {
509 GdkDrawableImplWin32 *impl = GDK_DRAWABLE_IMPL_WIN32 (drawable);
510 GdkGCWin32 *gcwin32 = GDK_GC_WIN32 (gc);
511 HDC hdc;
512 va_list args;
513 GdkFill fill_style = _gdk_gc_get_fill (gc);
514
515 va_start (args, region);
516
517 /* If tiled or stippled, draw to a temp pixmap and do blitting magic.
518 */
519
520 if (gcwin32->values_mask & GDK_GC_FILL &&
521 ((fill_style == GDK_TILED &&
522 gcwin32->values_mask & GDK_GC_TILE &&
523 _gdk_gc_get_tile (gc) != NULL)
524 ||
525 ((fill_style == GDK_OPAQUE_STIPPLED ||
526 fill_style == GDK_STIPPLED) &&
527 gcwin32->values_mask & GDK_GC_STIPPLE &&
528 _gdk_gc_get_stipple (gc) != NULL)))
529 {
530 const GdkGCValuesMask blitting_mask = 0;
531 GdkGCValuesMask drawing_mask = GDK_GC_FOREGROUND;
532 gint ts_x_origin = 0, ts_y_origin = 0;
533
534 gint width = region->extents.x2 - region->extents.x1;
535 gint height = region->extents.y2 - region->extents.y1;
536
537 GdkPixmap *mask_pixmap =
538 gdk_pixmap_new (drawable, width, height, 1);
539 GdkPixmap *tile_pixmap =
540 gdk_pixmap_new (drawable, width, height, -1);
541 GdkPixmap *stipple_bitmap = NULL;
542 GdkColor fg;
543
544 GdkGC *mask_gc = gdk_gc_new (mask_pixmap);
545 GdkGC *tile_gc = gdk_gc_new (tile_pixmap);
546
547 HDC mask_hdc;
548 HDC tile_hdc;
549
550 GdkGCValues gcvalues;
551
552 hdc = gdk_win32_hdc_get (drawable, gc, blitting_mask);
553 tile_hdc = gdk_win32_hdc_get (tile_pixmap, tile_gc, blitting_mask);
554
555 if (gcwin32->values_mask & GDK_GC_TS_X_ORIGIN)
556 ts_x_origin = gc->ts_x_origin;
557 if (gcwin32->values_mask & GDK_GC_TS_Y_ORIGIN)
558 ts_y_origin = gc->ts_y_origin;
559
560 ts_x_origin -= region->extents.x1;
561 ts_y_origin -= region->extents.y1;
562
563 /* Fill mask bitmap with zeros */
564 gdk_gc_set_function (mask_gc, GDK_CLEAR);
565 gdk_draw_rectangle (mask_pixmap, mask_gc, TRUE,
566 0, 0, width, height);
567
568 /* Paint into mask bitmap, drawing ones */
569 gdk_gc_set_function (mask_gc, GDK_COPY);
570 fg.pixel = 1;
571 gdk_gc_set_foreground (mask_gc, &fg);
572
573 /* If the drawing function uses line attributes, set them as in
574 * the real GC.
575 */
576 if (mask & LINE_ATTRIBUTES)
577 {
578 gdk_gc_get_values (gc, &gcvalues);
579 if (gcvalues.line_width != 0 ||
580 gcvalues.line_style != GDK_LINE_SOLID ||
581 gcvalues.cap_style != GDK_CAP_BUTT ||
582 gcvalues.join_style != GDK_JOIN_MITER)
583 gdk_gc_set_line_attributes (mask_gc,
584 gcvalues.line_width,
585 gcvalues.line_style,
586 gcvalues.cap_style,
587 gcvalues.join_style);
588 drawing_mask |= LINE_ATTRIBUTES;
589 }
590
591 /* Ditto, if the drawing function draws text, set up for that. */
592 if (mask & GDK_GC_FONT)
593 drawing_mask |= GDK_GC_FONT;
594
595 mask_hdc = gdk_win32_hdc_get (mask_pixmap, mask_gc, drawing_mask);
596 (*function) (GDK_GC_WIN32 (mask_gc), mask_hdc,
597 region->extents.x1, region->extents.y1, args);
598 gdk_win32_hdc_release (mask_pixmap, mask_gc, drawing_mask);
599
600 if (fill_style == GDK_TILED)
601 {
602 /* Tile pixmap with tile */
603 draw_tiles (tile_pixmap, tile_gc, SRCCOPY,
604 _gdk_gc_get_tile (gc),
605 0, 0, ts_x_origin, ts_y_origin,
606 width, height);
607 }
608 else
609 {
610 /* Tile with stipple */
611 GdkGC *stipple_gc;
612
613 stipple_bitmap = gdk_pixmap_new (NULL, width, height, 1);
614 stipple_gc = gdk_gc_new (stipple_bitmap);
615
616 /* Tile stipple bitmap */
617 draw_tiles (stipple_bitmap, stipple_gc, SRCCOPY,
618 _gdk_gc_get_stipple (gc),
619 0, 0, ts_x_origin, ts_y_origin,
620 width, height);
621
622 if (fill_style == GDK_OPAQUE_STIPPLED)
623 {
624 /* Fill tile pixmap with background */
625 fg.pixel = _gdk_gc_get_bg_pixel (gc);
626 gdk_gc_set_foreground (tile_gc, &fg);
627 gdk_draw_rectangle (tile_pixmap, tile_gc, TRUE,
628 0, 0, width, height);
629 }
630 g_object_unref (stipple_gc);
631 }
632
633 mask_hdc = gdk_win32_hdc_get (mask_pixmap, mask_gc, blitting_mask);
634
635 if (fill_style == GDK_STIPPLED ||
636 fill_style == GDK_OPAQUE_STIPPLED)
637 {
638 HDC stipple_hdc;
639 HBRUSH fg_brush;
640 HGDIOBJ old_tile_brush;
641 GdkGC *stipple_gc;
642
643 stipple_gc = gdk_gc_new (stipple_bitmap);
644 stipple_hdc = gdk_win32_hdc_get (stipple_bitmap, stipple_gc, blitting_mask);
645
646 if ((fg_brush = CreateSolidBrush
647 (_gdk_win32_colormap_color (impl->colormap,
648 _gdk_gc_get_fg_pixel (gc)))) == NULL)
649 WIN32_GDI_FAILED ("CreateSolidBrush");
650
651 if ((old_tile_brush = SelectObject (tile_hdc, fg_brush)) == NULL)
652 WIN32_GDI_FAILED ("SelectObject");
653
654 /* Paint tile with foreround where stipple is one
655 *
656 * Desired ternary ROP: (P=foreground, S=stipple, D=destination)
657 * P S D ?
658 * 0 0 0 0
659 * 0 0 1 1
660 * 0 1 0 0
661 * 0 1 1 0
662 * 1 0 0 0
663 * 1 0 1 1
664 * 1 1 0 1
665 * 1 1 1 1
666 *
667 * Reading bottom-up: 11100010 = 0xE2. PSDK docs say this is
668 * known as DSPDxax, with hex value 0x00E20746.
669 */
670 GDI_CALL (BitBlt, (tile_hdc, 0, 0, width, height,
671 stipple_hdc, 0, 0, ROP3_DSPDxax));
672
673 if (fill_style == GDK_STIPPLED)
674 {
675 /* Punch holes in mask where stipple is zero */
676 GDI_CALL (BitBlt, (mask_hdc, 0, 0, width, height,
677 stipple_hdc, 0, 0, SRCAND));
678 }
679
680 GDI_CALL (SelectObject, (tile_hdc, old_tile_brush));
681 GDI_CALL (DeleteObject, (fg_brush));
682 gdk_win32_hdc_release (stipple_bitmap, stipple_gc, blitting_mask);
683 g_object_unref (stipple_gc);
684 g_object_unref (stipple_bitmap);
685 }
686
687 /* Tile pixmap now contains the pattern that we should paint in
688 * the areas where mask is one. (It is filled with said pattern.)
689 */
690
691 GDI_CALL (MaskBlt, (hdc, region->extents.x1, region->extents.y1,
692 width, height,
693 tile_hdc, 0, 0,
694 GDK_PIXMAP_HBITMAP (mask_pixmap), 0, 0,
695 MAKEROP4 (rop2_to_rop3 (gcwin32->rop2), ROP3_D)));
696
697 /* Cleanup */
698 gdk_win32_hdc_release (mask_pixmap, mask_gc, blitting_mask);
699 g_object_unref (mask_gc);
700 g_object_unref (mask_pixmap);
701 gdk_win32_hdc_release (tile_pixmap, tile_gc, blitting_mask);
702 g_object_unref (tile_gc);
703 g_object_unref (tile_pixmap);
704
705 gdk_win32_hdc_release (drawable, gc, blitting_mask);
706 }
707 else
708 {
709 hdc = gdk_win32_hdc_get (drawable, gc, mask);
710 (*function) (gcwin32, hdc, 0, 0, args);
711 gdk_win32_hdc_release (drawable, gc, mask);
712 }
713 va_end (args);
714 }
715
716 static GdkRegion *
717 widen_bounds (GdkRectangle *bounds,
718 gint pen_width)
719 {
720 if (pen_width == 0)
721 pen_width = 1;
722
723 bounds->x -= pen_width;
724 bounds->y -= pen_width;
725 bounds->width += 2 * pen_width;
726 bounds->height += 2 * pen_width;
727
728 return gdk_region_rectangle (bounds);
729 }
730
731 static void
732 draw_rectangle (GdkGCWin32 *gcwin32,
733 HDC hdc,
734 gint x_offset,
735 gint y_offset,
736 va_list args)
737 {
738 HGDIOBJ old_pen_or_brush;
739 gboolean filled;
740 gint x;
741 gint y;
742 gint width;
743 gint height;
744
745 filled = va_arg (args, gboolean);
746 x = va_arg (args, gint);
747 y = va_arg (args, gint);
748 width = va_arg (args, gint);
749 height = va_arg (args, gint);
750
751 x -= x_offset;
752 y -= y_offset;
753
754 if (!filled && MUST_RENDER_DASHES_MANUALLY (gcwin32))
755 {
756 render_line_vertical (gcwin32, x, y, y+height+1) &&
757 render_line_horizontal (gcwin32, x, x+width+1, y) &&
758 render_line_vertical (gcwin32, x+width+1, y, y+height+1) &&
759 render_line_horizontal (gcwin32, x, x+width+1, y+height+1);
760 }
761 else
762 {
763 if (filled)
764 old_pen_or_brush = SelectObject (hdc, GetStockObject (NULL_PEN));
765 else
766 old_pen_or_brush = SelectObject (hdc, GetStockObject (HOLLOW_BRUSH));
767 if (old_pen_or_brush == NULL)
768 WIN32_GDI_FAILED ("SelectObject");
769 else
770 GDI_CALL (Rectangle, (hdc, x, y, x+width+1, y+height+1));
771
772 if (old_pen_or_brush != NULL)
773 GDI_CALL (SelectObject, (hdc, old_pen_or_brush));
774 }
775 }
776
777 static void
778 gdk_win32_draw_rectangle (GdkDrawable *drawable,
779 GdkGC *gc,
780 gboolean filled,
781 gint x,
782 gint y,
783 gint width,
784 gint height)
785 {
786 GdkRectangle bounds;
787 GdkRegion *region;
788
789 GDK_NOTE (DRAW, g_print ("gdk_win32_draw_rectangle: %s (%p) %s%dx%d@%+d%+d\n",
790 _gdk_win32_drawable_description (drawable),
791 gc,
792 (filled ? "fill " : ""),
793 width, height, x, y));
794
795 bounds.x = x;
796 bounds.y = y;
797 bounds.width = width;
798 bounds.height = height;
799 region = widen_bounds (&bounds, GDK_GC_WIN32 (gc)->pen_width);
800
801 generic_draw (drawable, gc,
802 GDK_GC_FOREGROUND | GDK_GC_BACKGROUND |
803 (filled ? 0 : LINE_ATTRIBUTES),
804 draw_rectangle, region, filled, x, y, width, height);
805
806 gdk_region_destroy (region);
807 }
808
809 static void
810 draw_arc (GdkGCWin32 *gcwin32,
811 HDC hdc,
812 gint x_offset,
813 gint y_offset,
814 va_list args)
815 {
816 HGDIOBJ old_pen;
817 gboolean filled;
818 gint x, y;
819 gint width, height;
820 gint angle1, angle2;
821 int nXStartArc, nYStartArc, nXEndArc, nYEndArc;
822
823 filled = va_arg (args, gboolean);
824 x = va_arg (args, gint);
825 y = va_arg (args, gint);
826 width = va_arg (args, gint);
827 height = va_arg (args, gint);
828 angle1 = va_arg (args, gint);
829 angle2 = va_arg (args, gint);
830
831 x -= x_offset;
832 y -= y_offset;
833
834 if (angle2 >= 360*64)
835 {
836 nXStartArc = nYStartArc = nXEndArc = nYEndArc = 0;
837 }
838 else if (angle2 > 0)
839 {
840 nXStartArc = x + width/2 + width * cos(angle1/64.*2.*G_PI/360.);
841 nYStartArc = y + height/2 + -height * sin(angle1/64.*2.*G_PI/360.);
842 nXEndArc = x + width/2 + width * cos((angle1+angle2)/64.*2.*G_PI/360.);
843 nYEndArc = y + height/2 + -height * sin((angle1+angle2)/64.*2.*G_PI/360.);
844 }
845 else
846 {
847 nXEndArc = x + width/2 + width * cos(angle1/64.*2.*G_PI/360.);
848 nYEndArc = y + height/2 + -height * sin(angle1/64.*2.*G_PI/360.);
849 nXStartArc = x + width/2 + width * cos((angle1+angle2)/64.*2.*G_PI/360.);
850 nYStartArc = y + height/2 + -height * sin((angle1+angle2)/64.*2.*G_PI/360.);
851 }
852
853 if (filled)
854 {
855 old_pen = SelectObject (hdc, GetStockObject (NULL_PEN));
856 GDK_NOTE (DRAW, g_print ("... Pie(%p,%d,%d,%d,%d,%d,%d,%d,%d)\n",
857 hdc, x, y, x+width, y+height,
858 nXStartArc, nYStartArc, nXEndArc, nYEndArc));
859 GDI_CALL (Pie, (hdc, x, y, x+width, y+height,
860 nXStartArc, nYStartArc, nXEndArc, nYEndArc));
861 GDI_CALL (SelectObject, (hdc, old_pen));
862 }
863 else
864 {
865 GDK_NOTE (DRAW, g_print ("... Arc(%p,%d,%d,%d,%d,%d,%d,%d,%d)\n",
866 hdc, x, y, x+width, y+height,
867 nXStartArc, nYStartArc, nXEndArc, nYEndArc));
868 GDI_CALL (Arc, (hdc, x, y, x+width, y+height,
869 nXStartArc, nYStartArc, nXEndArc, nYEndArc));
870 }
871 }
872
873 static void
874 gdk_win32_draw_arc (GdkDrawable *drawable,
875 GdkGC *gc,
876 gboolean filled,
877 gint x,
878 gint y,
879 gint width,
880 gint height,
881 gint angle1,
882 gint angle2)
883 {
884 GdkRectangle bounds;
885 GdkRegion *region;
886
887 GDK_NOTE (DRAW, g_print ("gdk_win32_draw_arc: %s %d,%d,%d,%d %d %d\n",
888 _gdk_win32_drawable_description (drawable),
889 x, y, width, height, angle1, angle2));
890
891 if (width <= 2 || height <= 2 || angle2 == 0)
892 return;
893
894 bounds.x = x;
895 bounds.y = y;
896 bounds.width = width;
897 bounds.height = height;
898 region = widen_bounds (&bounds, GDK_GC_WIN32 (gc)->pen_width);
899
900 generic_draw (drawable, gc,
901 GDK_GC_FOREGROUND | (filled ? 0 : LINE_ATTRIBUTES),
902 draw_arc, region, filled, x, y, width, height, angle1, angle2);
903
904 gdk_region_destroy (region);
905 }
906
907 static void
908 draw_polygon (GdkGCWin32 *gcwin32,
909 HDC hdc,
910 gint x_offset,
911 gint y_offset,
912 va_list args)
913 {
914 gboolean filled;
915 POINT *pts;
916 HGDIOBJ old_pen_or_brush;
917 gint npoints;
918 gint i;
919
920 filled = va_arg (args, gboolean);
921 pts = va_arg (args, POINT *);
922 npoints = va_arg (args, gint);
923
924 if (x_offset != 0 || y_offset != 0)
925 for (i = 0; i < npoints; i++)
926 {
927 pts[i].x -= x_offset;
928 pts[i].y -= y_offset;
929 }
930
931 if (filled)
932 old_pen_or_brush = SelectObject (hdc, GetStockObject (NULL_PEN));
933 else
934 old_pen_or_brush = SelectObject (hdc, GetStockObject (HOLLOW_BRUSH));
935 if (old_pen_or_brush == NULL)
936 WIN32_GDI_FAILED ("SelectObject");
937 GDI_CALL (Polygon, (hdc, pts, npoints));
938 if (old_pen_or_brush != NULL)
939 GDI_CALL (SelectObject, (hdc, old_pen_or_brush));
940 }
941
942 static void
943 gdk_win32_draw_polygon (GdkDrawable *drawable,
944 GdkGC *gc,
945 gboolean filled,
946 GdkPoint *points,
947 gint npoints)
948 {
949 GdkRectangle bounds;
950 GdkRegion *region;
951 POINT *pts;
952 int i;
953
954 GDK_NOTE (DRAW, g_print ("gdk_win32_draw_polygon: %s %d points\n",
955 _gdk_win32_drawable_description (drawable),
956 npoints));
957
958 if (npoints < 2)
959 return;
960
961 bounds.x = G_MAXINT;
962 bounds.y = G_MAXINT;
963 bounds.width = 0;
964 bounds.height = 0;
965
966 pts = g_new (POINT, npoints);
967
968 for (i = 0; i < npoints; i++)
969 {
970 bounds.x = MIN (bounds.x, points[i].x);
971 bounds.y = MIN (bounds.y, points[i].y);
972 pts[i].x = points[i].x;
973 pts[i].y = points[i].y;
974 }
975
976 for (i = 0; i < npoints; i++)
977 {
978 bounds.width = MAX (bounds.width, points[i].x - bounds.x);
979 bounds.height = MAX (bounds.height, points[i].y - bounds.y);
980 }
981
982 region = widen_bounds (&bounds, GDK_GC_WIN32 (gc)->pen_width);
983
984 generic_draw (drawable, gc,
985 GDK_GC_FOREGROUND | (filled ? 0 : LINE_ATTRIBUTES),
986 draw_polygon, region, filled, pts, npoints);
987
988 gdk_region_destroy (region);
989 g_free (pts);
990 }
991
992 typedef struct
993 {
994 gint x, y;
995 HDC hdc;
996 } gdk_draw_text_arg;
997
998 static void
999 gdk_draw_text_handler (GdkWin32SingleFont *singlefont,
1000 const wchar_t *wcstr,
1001 int wclen,
1002 void *arg)
1003 {
1004 HGDIOBJ oldfont;
1005 SIZE size;
1006 gdk_draw_text_arg *argp = (gdk_draw_text_arg *) arg;
1007
1008 if (!singlefont)
1009 return;
1010
1011 if ((oldfont = SelectObject (argp->hdc, singlefont->hfont)) == NULL)
1012 {
1013 WIN32_GDI_FAILED ("SelectObject");
1014 return;
1015 }
1016
1017 if (!TextOutW (argp->hdc, argp->x, argp->y, wcstr, wclen))
1018 WIN32_GDI_FAILED ("TextOutW");
1019 GetTextExtentPoint32W (argp->hdc, wcstr, wclen, &size);
1020 argp->x += size.cx;
1021
1022 SelectObject (argp->hdc, oldfont);
1023 }
1024
1025 static void
1026 gdk_win32_draw_text (GdkDrawable *drawable,
1027 GdkFont *font,
1028 GdkGC *gc,
1029 gint x,
1030 gint y,
1031 const gchar *text,
1032 gint text_length)
1033 {
1034 const GdkGCValuesMask mask = GDK_GC_FOREGROUND|GDK_GC_FONT;
1035 wchar_t *wcstr, wc;
1036 glong wlen;
1037 gdk_draw_text_arg arg;
1038
1039 if (text_length == 0)
1040 return;
1041
1042 g_assert (font->type == GDK_FONT_FONT || font->type == GDK_FONT_FONTSET);
1043
1044 arg.x = x;
1045 arg.y = y;
1046 arg.hdc = gdk_win32_hdc_get (drawable, gc, mask);
1047
1048 GDK_NOTE (DRAW, g_print ("gdk_win32_draw_text: %s (%d,%d) \"%.*s\" (len %d)\n",
1049 _gdk_win32_drawable_description (drawable),
1050 x, y,
1051 (text_length > 10 ? 10 : text_length),
1052 text, text_length));
1053
1054 if (text_length == 1)
1055 {
1056 /* For single characters, don't try to interpret as UTF-8. */
1057 wc = (guchar) text[0];
1058 _gdk_wchar_text_handle (font, &wc, 1, gdk_draw_text_handler, &arg);
1059 }
1060 else
1061 {
1062 wcstr = g_utf8_to_utf16 (text, text_length, NULL, &wlen, NULL);
1063 _gdk_wchar_text_handle (font, wcstr, wlen, gdk_draw_text_handler, &arg);
1064 g_free (wcstr);
1065 }
1066
1067 gdk_win32_hdc_release (drawable, gc, mask);
1068 }
1069
1070 static void
1071 gdk_win32_draw_text_wc (GdkDrawable *drawable,
1072 GdkFont *font,
1073 GdkGC *gc,
1074 gint x,
1075 gint y,
1076 const GdkWChar *text,
1077 gint text_length)
1078 {
1079 const GdkGCValuesMask mask = GDK_GC_FOREGROUND|GDK_GC_FONT;
1080 gint i;
1081 wchar_t *wcstr;
1082 gdk_draw_text_arg arg;
1083
1084 if (text_length == 0)
1085 return;
1086
1087 g_assert (font->type == GDK_FONT_FONT || font->type == GDK_FONT_FONTSET);
1088
1089 arg.x = x;
1090 arg.y = y;
1091 arg.hdc = gdk_win32_hdc_get (drawable, gc, mask);
1092
1093 GDK_NOTE (DRAW, g_print ("gdk_win32_draw_text_wc: %s (%d,%d) len: %d\n",
1094 _gdk_win32_drawable_description (drawable),
1095 x, y, text_length));
1096
1097 if (sizeof (wchar_t) != sizeof (GdkWChar))
1098 {
1099 wcstr = g_new (wchar_t, text_length);
1100 for (i = 0; i < text_length; i++)
1101 wcstr[i] = text[i];
1102 }
1103 else
1104 wcstr = (wchar_t *) text;
1105
1106 _gdk_wchar_text_handle (font, wcstr, text_length,
1107 gdk_draw_text_handler, &arg);
1108
1109 if (sizeof (wchar_t) != sizeof (GdkWChar))
1110 g_free (wcstr);
1111
1112 gdk_win32_hdc_release (drawable, gc, mask);
1113 }
1114
1115 static void
1116 gdk_win32_draw_drawable (GdkDrawable *drawable,
1117 GdkGC *gc,
1118 GdkPixmap *src,
1119 gint xsrc,
1120 gint ysrc,
1121 gint xdest,
1122 gint ydest,
1123 gint width,
1124 gint height,
1125 GdkDrawable *original_src)
1126 {
1127 g_assert (GDK_IS_DRAWABLE_IMPL_WIN32 (drawable));
1128
1129 _gdk_win32_blit (FALSE, (GdkDrawableImplWin32 *) drawable,
1130 gc, src, xsrc, ysrc,
1131 xdest, ydest, width, height);
1132 }
1133
1134 static void
1135 gdk_win32_draw_points (GdkDrawable *drawable,
1136 GdkGC *gc,
1137 GdkPoint *points,
1138 gint npoints)
1139 {
1140 HDC hdc;
1141 HGDIOBJ old_pen;
1142 int i;
1143
1144 hdc = gdk_win32_hdc_get (drawable, gc, GDK_GC_FOREGROUND);
1145
1146 GDK_NOTE (DRAW, g_print ("gdk_win32_draw_points: %s %d points\n",
1147 _gdk_win32_drawable_description (drawable),
1148 npoints));
1149
1150 /* The X11 version uses XDrawPoint(), which doesn't use the fill
1151 * mode, so don't use generic_draw. But we should use the current
1152 * function, so we can't use SetPixel(). Draw single-pixel
1153 * rectangles (sigh).
1154 */
1155
1156 old_pen = SelectObject (hdc, GetStockObject (NULL_PEN));
1157 for (i = 0; i < npoints; i++)
1158 Rectangle (hdc, points[i].x, points[i].y,
1159 points[i].x + 2, points[i].y + 2);
1160
1161 SelectObject (hdc, old_pen);
1162 gdk_win32_hdc_release (drawable, gc, GDK_GC_FOREGROUND);
1163 }
1164
1165 static void
1166 draw_segments (GdkGCWin32 *gcwin32,
1167 HDC hdc,
1168 gint x_offset,
1169 gint y_offset,
1170 va_list args)
1171 {
1172 GdkSegment *segs;
1173 gint nsegs;
1174 gint i;
1175
1176 segs = va_arg (args, GdkSegment *);
1177 nsegs = va_arg (args, gint);
1178
1179 if (x_offset != 0 || y_offset != 0)
1180 {
1181 /* must not modify in place, but could splice in the offset all below */
1182 segs = g_memdup (segs, nsegs * sizeof (GdkSegment));
1183 for (i = 0; i < nsegs; i++)
1184 {
1185 segs[i].x1 -= x_offset;
1186 segs[i].y1 -= y_offset;
1187 segs[i].x2 -= x_offset;
1188 segs[i].y2 -= y_offset;
1189 }
1190 }
1191
1192 if (MUST_RENDER_DASHES_MANUALLY (gcwin32))
1193 {
1194 for (i = 0; i < nsegs; i++)
1195 {
1196 if (segs[i].x1 == segs[i].x2)
1197 {
1198 int y1, y2;
1199
1200 if (segs[i].y1 <= segs[i].y2)
1201 y1 = segs[i].y1, y2 = segs[i].y2;
1202 else
1203 y1 = segs[i].y2, y2 = segs[i].y1;
1204
1205 render_line_vertical (gcwin32, segs[i].x1, y1, y2);
1206 }
1207 else if (segs[i].y1 == segs[i].y2)
1208 {
1209 int x1, x2;
1210
1211 if (segs[i].x1 <= segs[i].x2)
1212 x1 = segs[i].x1, x2 = segs[i].x2;
1213 else
1214 x1 = segs[i].x2, x2 = segs[i].x1;
1215
1216 render_line_horizontal (gcwin32, x1, x2, segs[i].y1);
1217 }
1218 else
1219 GDI_CALL (MoveToEx, (hdc, segs[i].x1, segs[i].y1, NULL)) &&
1220 GDI_CALL (LineTo, (hdc, segs[i].x2, segs[i].y2));
1221 }
1222 }
1223 else
1224 {
1225 for (i = 0; i < nsegs; i++)
1226 {
1227 const GdkSegment *ps = &segs[i];
1228 const int x1 = ps->x1, y1 = ps->y1;
1229 int x2 = ps->x2, y2 = ps->y2;
1230
1231 GDK_NOTE (DRAW, g_print (" +%d+%d..+%d+%d", x1, y1, x2, y2));
1232 GDI_CALL (MoveToEx, (hdc, x1, y1, NULL)) &&
1233 GDI_CALL (LineTo, (hdc, x2, y2));
1234 }
1235
1236 GDK_NOTE (DRAW, g_print ("\n"));
1237 }
1238 if (x_offset != 0 || y_offset != 0)
1239 g_free (segs);
1240 }
1241
1242 static void
1243 gdk_win32_draw_segments (GdkDrawable *drawable,
1244 GdkGC *gc,
1245 GdkSegment *segs,
1246 gint nsegs)
1247 {
1248 GdkRectangle bounds;
1249 GdkRegion *region;
1250 gint i;
1251
1252 GDK_NOTE (DRAW, g_print ("gdk_win32_draw_segments: %s %d segs\n",
1253 _gdk_win32_drawable_description (drawable),
1254 nsegs));
1255
1256 bounds.x = G_MAXINT;
1257 bounds.y = G_MAXINT;
1258 bounds.width = 0;
1259 bounds.height = 0;
1260
1261 for (i = 0; i < nsegs; i++)
1262 {
1263 bounds.x = MIN (bounds.x, segs[i].x1);
1264 bounds.x = MIN (bounds.x, segs[i].x2);
1265 bounds.y = MIN (bounds.y, segs[i].y1);
1266 bounds.y = MIN (bounds.y, segs[i].y2);
1267 }
1268
1269 for (i = 0; i < nsegs; i++)
1270 {
1271 bounds.width = MAX (bounds.width, segs[i].x1 - bounds.x);
1272 bounds.width = MAX (bounds.width, segs[i].x2 - bounds.x);
1273 bounds.height = MAX (bounds.height, segs[i].y1 - bounds.y);
1274 bounds.height = MAX (bounds.height, segs[i].y2 - bounds.y);
1275 }
1276
1277 region = widen_bounds (&bounds, GDK_GC_WIN32 (gc)->pen_width);
1278
1279 generic_draw (drawable, gc, GDK_GC_FOREGROUND | LINE_ATTRIBUTES,
1280 draw_segments, region, segs, nsegs);
1281
1282 gdk_region_destroy (region);
1283 }
1284
1285 static void
1286 draw_lines (GdkGCWin32 *gcwin32,
1287 HDC hdc,
1288 gint x_offset,
1289 gint y_offset,
1290 va_list args)
1291 {
1292 POINT *pts;
1293 gint npoints;
1294 gint i;
1295
1296 pts = va_arg (args, POINT *);
1297 npoints = va_arg (args, gint);
1298
1299 if (x_offset != 0 || y_offset != 0)
1300 for (i = 0; i < npoints; i++)
1301 {
1302 pts[i].x -= x_offset;
1303 pts[i].y -= y_offset;
1304 }
1305
1306 if (MUST_RENDER_DASHES_MANUALLY (gcwin32))
1307 {
1308 for (i = 0; i < npoints - 1; i++)
1309 {
1310 if (pts[i].x == pts[i+1].x)
1311 {
1312 int y1, y2;
1313 if (pts[i].y > pts[i+1].y)
1314 y1 = pts[i+1].y, y2 = pts[i].y;
1315 else
1316 y1 = pts[i].y, y2 = pts[i+1].y;
1317
1318 render_line_vertical (gcwin32, pts[i].x, y1, y2);
1319 }
1320 else if (pts[i].y == pts[i+1].y)
1321 {
1322 int x1, x2;
1323 if (pts[i].x > pts[i+1].x)
1324 x1 = pts[i+1].x, x2 = pts[i].x;
1325 else
1326 x1 = pts[i].x, x2 = pts[i+1].x;
1327
1328 render_line_horizontal (gcwin32, x1, x2, pts[i].y);
1329 }
1330 else
1331 GDI_CALL (MoveToEx, (hdc, pts[i].x, pts[i].y, NULL)) &&
1332 GDI_CALL (LineTo, (hdc, pts[i+1].x, pts[i+1].y));
1333 }
1334 }
1335 else
1336 GDI_CALL (Polyline, (hdc, pts, npoints));
1337 }
1338
1339 static void
1340 gdk_win32_draw_lines (GdkDrawable *drawable,
1341 GdkGC *gc,
1342 GdkPoint *points,
1343 gint npoints)
1344 {
1345 GdkRectangle bounds;
1346 GdkRegion *region;
1347 POINT *pts;
1348 int i;
1349
1350 GDK_NOTE (DRAW, g_print ("gdk_win32_draw_lines: %s %d points\n",
1351 _gdk_win32_drawable_description (drawable),
1352 npoints));
1353
1354 if (npoints < 2)
1355 return;
1356
1357 bounds.x = G_MAXINT;
1358 bounds.y = G_MAXINT;
1359 bounds.width = 0;
1360 bounds.height = 0;
1361
1362 pts = g_new (POINT, npoints);
1363
1364 for (i = 0; i < npoints; i++)
1365 {
1366 bounds.x = MIN (bounds.x, points[i].x);
1367 bounds.y = MIN (bounds.y, points[i].y);
1368 pts[i].x = points[i].x;
1369 pts[i].y = points[i].y;
1370 }
1371
1372 for (i = 0; i < npoints; i++)
1373 {
1374 bounds.width = MAX (bounds.width, points[i].x - bounds.x);
1375 bounds.height = MAX (bounds.height, points[i].y - bounds.y);
1376 }
1377
1378 region = widen_bounds (&bounds, GDK_GC_WIN32 (gc)->pen_width);
1379
1380 generic_draw (drawable, gc, GDK_GC_FOREGROUND | GDK_GC_BACKGROUND |
1381 LINE_ATTRIBUTES,
1382 draw_lines, region, pts, npoints);
1383
1384 gdk_region_destroy (region);
1385 g_free (pts);
1386 }
1387
1388 static void
1389 blit_from_pixmap (gboolean use_fg_bg,
1390 GdkDrawableImplWin32 *dest,
1391 HDC hdc,
1392 GdkPixmapImplWin32 *src,
1393 GdkGC *gc,
1394 gint xsrc,
1395 gint ysrc,
1396 gint xdest,
1397 gint ydest,
1398 gint width,
1399 gint height)
1400 {
1401 GdkGCWin32 *gcwin32 = GDK_GC_WIN32 (gc);
1402 HDC srcdc;
1403 HBITMAP holdbitmap;
1404 RGBQUAD oldtable[256], newtable[256];
1405 COLORREF bg, fg;
1406
1407 gint newtable_size = 0, oldtable_size = 0;
1408 gboolean ok = TRUE;
1409
1410 GDK_NOTE (DRAW, g_print ("blit_from_pixmap\n"));
1411
1412 srcdc = _gdk_win32_drawable_acquire_dc (GDK_DRAWABLE (src));
1413 if (!srcdc)
1414 return;
1415
1416 if (!(holdbitmap = SelectObject (srcdc, ((GdkDrawableImplWin32 *) src)->handle)))
1417 WIN32_GDI_FAILED ("SelectObject");
1418 else
1419 {
1420 if (GDK_PIXMAP_OBJECT (src->parent_instance.wrapper)->depth <= 8)
1421 {
1422 /* Blitting from a 1, 4 or 8-bit pixmap */
1423
1424 if ((oldtable_size = GetDIBColorTable (srcdc, 0, 256, oldtable)) == 0)
1425 WIN32_GDI_FAILED ("GetDIBColorTable");
1426 else if (GDK_PIXMAP_OBJECT (src->parent_instance.wrapper)->depth == 1)
1427 {
1428 /* Blitting from an 1-bit pixmap */
1429
1430 gint bgix, fgix;
1431
1432 if (use_fg_bg)
1433 {
1434 bgix = _gdk_gc_get_bg_pixel (gc);
1435 fgix = _gdk_gc_get_fg_pixel (gc);
1436 }
1437 else
1438 {
1439 bgix = 0;
1440 fgix = 1;
1441 }
1442
1443 if (GDK_IS_PIXMAP_IMPL_WIN32 (dest) &&
1444 GDK_PIXMAP_OBJECT (dest->wrapper)->depth <= 8)
1445 {
1446 /* Destination is also pixmap, get fg and bg from
1447 * its palette. Either use the foreground and
1448 * background pixel values in the GC (only in the
1449 * case of gdk_image_put(), cf. XPutImage()), or 0
1450 * and 1 to index the palette.
1451 */
1452 if (!GDI_CALL (GetDIBColorTable, (hdc, bgix, 1, newtable)) ||
1453 !GDI_CALL (GetDIBColorTable, (hdc, fgix, 1, newtable+1)))
1454 ok = FALSE;
1455 }
1456 else
1457 {
1458 /* Destination is a window, get fg and bg from its
1459 * colormap
1460 */
1461
1462 bg = _gdk_win32_colormap_color (dest->colormap, bgix);
1463 fg = _gdk_win32_colormap_color (dest->colormap, fgix);
1464 newtable[0].rgbBlue = GetBValue (bg);
1465 newtable[0].rgbGreen = GetGValue (bg);
1466 newtable[0].rgbRed = GetRValue (bg);
1467 newtable[0].rgbReserved = 0;
1468 newtable[1].rgbBlue = GetBValue (fg);
1469 newtable[1].rgbGreen = GetGValue (fg);
1470 newtable[1].rgbRed = GetRValue (fg);
1471 newtable[1].rgbReserved = 0;
1472 }
1473 if (ok)
1474 GDK_NOTE (DRAW, g_print ("bg: %02x %02x %02x "
1475 "fg: %02x %02x %02x\n",
1476 newtable[0].rgbRed,
1477 newtable[0].rgbGreen,
1478 newtable[0].rgbBlue,
1479 newtable[1].rgbRed,
1480 newtable[1].rgbGreen,
1481 newtable[1].rgbBlue));
1482 newtable_size = 2;
1483 }
1484 else if (GDK_IS_PIXMAP_IMPL_WIN32 (dest))
1485 {
1486 /* Destination is pixmap, get its color table */
1487
1488 if ((newtable_size = GetDIBColorTable (hdc, 0, 256, newtable)) == 0)
1489 WIN32_GDI_FAILED ("GetDIBColorTable"), ok = FALSE;
1490 }
1491
1492 /* If blitting between pixmaps, set source's color table */
1493 if (ok && newtable_size > 0)
1494 {
1495 GDK_NOTE (MISC_OR_COLORMAP,
1496 g_print ("blit_from_pixmap: set color table"
1497 " hdc=%p count=%d\n",
1498 srcdc, newtable_size));
1499 if (!GDI_CALL (SetDIBColorTable, (srcdc, 0, newtable_size, newtable)))
1500 ok = FALSE;
1501 }
1502 }
1503
1504 if (ok)
1505 if (!BitBlt (hdc, xdest, ydest, width, height,
1506 srcdc, xsrc, ysrc, rop2_to_rop3 (gcwin32->rop2)) &&
1507 GetLastError () != ERROR_INVALID_HANDLE)
1508 WIN32_GDI_FAILED ("BitBlt");
1509
1510 /* Restore source's color table if necessary */
1511 if (ok && newtable_size > 0 && oldtable_size > 0)
1512 {
1513 GDK_NOTE (MISC_OR_COLORMAP,
1514 g_print ("blit_from_pixmap: reset color table"
1515 " hdc=%p count=%d\n",
1516 srcdc, oldtable_size));
1517 GDI_CALL (SetDIBColorTable, (srcdc, 0, oldtable_size, oldtable));
1518 }
1519
1520 GDI_CALL (SelectObject, (srcdc, holdbitmap));
1521 }
1522
1523 _gdk_win32_drawable_release_dc (GDK_DRAWABLE (src));
1524 }
1525
1526 static void
1527 blit_inside_drawable (HDC hdc,
1528 GdkGCWin32 *gcwin32,
1529 GdkDrawableImplWin32 *src,
1530 gint xsrc,
1531 gint ysrc,
1532 gint xdest,
1533 gint ydest,
1534 gint width,
1535 gint height)
1536
1537 {
1538 GDK_NOTE (DRAW, g_print ("blit_inside_drawable\n"));
1539
1540 if GDK_IS_WINDOW_IMPL_WIN32 (src)
1541 {
1542 /* Simply calling BitBlt() instead of these ScrollDC() gymnastics might
1543 * seem tempting, but we need to do this to prevent blitting garbage when
1544 * scrolling a window that is partially obscured by another window. For
1545 * example, GIMP's toolbox being over the editor window. */
1546
1547 RECT emptyRect, clipRect;
1548 HRGN updateRgn;
1549 GdkRegion *update_region;
1550
1551 clipRect.left = xdest;
1552 clipRect.top = ydest;
1553 clipRect.right = xdest + width;
1554 clipRect.bottom = ydest + height;
1555
1556 SetRectEmpty (&emptyRect);
1557 updateRgn = CreateRectRgnIndirect (&emptyRect);
1558
1559 if (!ScrollDC (hdc, xdest - xsrc, ydest - ysrc, NULL, &clipRect, updateRgn, NULL))
1560 WIN32_GDI_FAILED ("ScrollDC");
1561 else
1562 {
1563 GdkDrawable *wrapper = src->wrapper;
1564 update_region = _gdk_win32_hrgn_to_region (updateRgn);
1565 if (!gdk_region_empty (update_region))
1566 _gdk_window_invalidate_for_expose (GDK_WINDOW (wrapper), update_region);
1567 gdk_region_destroy (update_region);
1568 }
1569
1570 if (!DeleteObject (updateRgn))
1571 WIN32_GDI_FAILED ("DeleteObject");
1572 }
1573 else
1574 {
1575 GDI_CALL (BitBlt, (hdc, xdest, ydest, width, height,
1576 hdc, xsrc, ysrc, rop2_to_rop3 (gcwin32->rop2)));
1577 }
1578 }
1579
1580 static void
1581 blit_from_window (HDC hdc,
1582 GdkGCWin32 *gcwin32,
1583 GdkDrawableImplWin32 *src,
1584 gint xsrc,
1585 gint ysrc,
1586 gint xdest,
1587 gint ydest,
1588 gint width,
1589 gint height)
1590 {
1591 HDC srcdc;
1592 HPALETTE holdpal = NULL;
1593 GdkColormap *cmap = gdk_colormap_get_system ();
1594
1595 GDK_NOTE (DRAW, g_print ("blit_from_window\n"));
1596
1597 if ((srcdc = GetDC (src->handle)) == NULL)
1598 {
1599 WIN32_GDI_FAILED ("GetDC");
1600 return;
1601 }
1602
1603 if (cmap->visual->type == GDK_VISUAL_PSEUDO_COLOR ||
1604 cmap->visual->type == GDK_VISUAL_STATIC_COLOR)
1605 {
1606 gint k;
1607
1608 if (!(holdpal = SelectPalette (srcdc, GDK_WIN32_COLORMAP_DATA (cmap)->hpal, FALSE)))
1609 WIN32_GDI_FAILED ("SelectPalette");
1610 else if ((k = RealizePalette (srcdc)) == GDI_ERROR)
1611 WIN32_GDI_FAILED ("RealizePalette");
1612 else if (k > 0)
1613 GDK_NOTE (MISC_OR_COLORMAP,
1614 g_print ("blit_from_window: realized %d\n", k));
1615 }
1616
1617 GDI_CALL (BitBlt, (hdc, xdest, ydest, width, height,
1618 srcdc, xsrc, ysrc, rop2_to_rop3 (gcwin32->rop2)));
1619
1620 if (holdpal != NULL)
1621 GDI_CALL (SelectPalette, (srcdc, holdpal, FALSE));
1622
1623 GDI_CALL (ReleaseDC, (src->handle, srcdc));
1624 }
1625
1626 void
1627 _gdk_win32_blit (gboolean use_fg_bg,
1628 GdkDrawableImplWin32 *draw_impl,
1629 GdkGC *gc,
1630 GdkDrawable *src,
1631 gint xsrc,
1632 gint ysrc,
1633 gint xdest,
1634 gint ydest,
1635 gint width,
1636 gint height)
1637 {
1638 HDC hdc;
1639 HRGN src_rgn, draw_rgn, outside_rgn;
1640 RECT r;
1641 GdkDrawableImplWin32 *src_impl = NULL;
1642 gint src_width, src_height;
1643
1644 GDK_NOTE (DRAW, g_print ("_gdk_win32_blit: src:%s %dx%d@%+d%+d\n"
1645 " dst:%s @%+d%+d use_fg_bg=%d\n",
1646 _gdk_win32_drawable_description (src),
1647 width, height, xsrc, ysrc,
1648 _gdk_win32_drawable_description (&draw_impl->parent_instance),
1649 xdest, ydest,
1650 use_fg_bg));
1651
1652 /* If blitting from the root window, take the multi-monitor offset
1653 * into account.
1654 */
1655 if (src == ((GdkWindowObject *)_gdk_root)->impl)
1656 {
1657 GDK_NOTE (DRAW, g_print ("... offsetting src coords\n"));
1658 xsrc -= _gdk_offset_x;
1659 ysrc -= _gdk_offset_y;
1660 }
1661
1662 if (GDK_IS_DRAWABLE_IMPL_WIN32 (src))
1663 src_impl = (GdkDrawableImplWin32 *) src;
1664 else if (GDK_IS_WINDOW (src))
1665 src_impl = (GdkDrawableImplWin32 *) GDK_WINDOW_OBJECT (src)->impl;
1666 else if (GDK_IS_PIXMAP (src))
1667 src_impl = (GdkDrawableImplWin32 *) GDK_PIXMAP_OBJECT (src)->impl;
1668 else
1669 g_assert_not_reached ();
1670
1671 if (GDK_IS_WINDOW_IMPL_WIN32 (draw_impl) &&
1672 GDK_IS_PIXMAP_IMPL_WIN32 (src_impl))
1673 {
1674 GdkPixmapImplWin32 *src_pixmap = GDK_PIXMAP_IMPL_WIN32 (src_impl);
1675
1676 if (xsrc < 0)
1677 {
1678 width += xsrc;
1679 xdest -= xsrc;
1680 xsrc = 0;
1681 }
1682
1683 if (ysrc < 0)
1684 {
1685 height += ysrc;
1686 ydest -= ysrc;
1687 ysrc = 0;
1688 }
1689
1690 if (xsrc + width > src_pixmap->width)
1691 width = src_pixmap->width - xsrc;
1692 if (ysrc + height > src_pixmap->height)
1693 height = src_pixmap->height - ysrc;
1694 }
1695
1696 hdc = gdk_win32_hdc_get (&draw_impl->parent_instance, gc, GDK_GC_FOREGROUND);
1697
1698 gdk_drawable_get_size (src_impl->wrapper, &src_width, &src_height);
1699
1700 if ((src_rgn = CreateRectRgn (0, 0, src_width + 1, src_height + 1)) == NULL)
1701 {
1702 WIN32_GDI_FAILED ("CreateRectRgn");
1703 }
1704 else if ((draw_rgn = CreateRectRgn (xsrc, ysrc,
1705 xsrc + width + 1,
1706 ysrc + height + 1)) == NULL)
1707 {
1708 WIN32_GDI_FAILED ("CreateRectRgn");
1709 }
1710 else
1711 {
1712 if (GDK_IS_WINDOW_IMPL_WIN32 (draw_impl))
1713 {
1714 int comb;
1715
1716 /* If we are drawing on a window, calculate the region that is
1717 * outside the source pixmap, and invalidate that, causing it to
1718 * be cleared. Not completely sure whether this is always needed. XXX
1719 */
1720 SetRectEmpty (&r);
1721 outside_rgn = CreateRectRgnIndirect (&r);
1722
1723 if ((comb = CombineRgn (outside_rgn,
1724 draw_rgn, src_rgn,
1725 RGN_DIFF)) == ERROR)
1726 WIN32_GDI_FAILED ("CombineRgn");
1727 else if (comb != NULLREGION)
1728 {
1729 OffsetRgn (outside_rgn, xdest, ydest);
1730 GDK_NOTE (DRAW, (GetRgnBox (outside_rgn, &r),
1731 g_print ("... InvalidateRgn "
1732 "bbox: %ldx%ld@%+ld%+ld\n",
1733 r.right - r.left - 1, r.bottom - r.top - 1,
1734 r.left, r.top)));
1735 InvalidateRgn (draw_impl->handle, outside_rgn, TRUE);
1736 }
1737 GDI_CALL (DeleteObject, (outside_rgn));
1738 }
1739
1740 #if 1 /* Don't know if this is necessary XXX */
1741 if (CombineRgn (draw_rgn, draw_rgn, src_rgn, RGN_AND) == COMPLEXREGION)
1742 g_warning ("gdk_win32_blit: CombineRgn returned a COMPLEXREGION");
1743
1744 GetRgnBox (draw_rgn, &r);
1745 if (r.left != xsrc || r.top != ysrc ||
1746 r.right != xsrc + width + 1 || r.bottom != ysrc + height + 1)
1747 {
1748 xdest += r.left - xsrc;
1749 xsrc = r.left;
1750 ydest += r.top - ysrc;
1751 ysrc = r.top;
1752 width = r.right - xsrc - 1;
1753 height = r.bottom - ysrc - 1;
1754
1755 GDK_NOTE (DRAW, g_print ("... restricted to src: %dx%d@%+d%+d, "
1756 "dest: @%+d%+d\n",
1757 width, height, xsrc, ysrc,
1758 xdest, ydest));
1759 }
1760 #endif
1761
1762 GDI_CALL (DeleteObject, (src_rgn));
1763 GDI_CALL (DeleteObject, (draw_rgn));
1764 }
1765
1766 if (draw_impl->handle == src_impl->handle)
1767 blit_inside_drawable (hdc, GDK_GC_WIN32 (gc), src_impl,
1768 xsrc, ysrc, xdest, ydest, width, height);
1769 else if (GDK_IS_PIXMAP_IMPL_WIN32 (src_impl))
1770 blit_from_pixmap (use_fg_bg, draw_impl, hdc,
1771 (GdkPixmapImplWin32 *) src_impl, gc,
1772 xsrc, ysrc, xdest, ydest, width, height);
1773 else
1774 blit_from_window (hdc, GDK_GC_WIN32 (gc), src_impl,
1775 xsrc, ysrc, xdest, ydest, width, height);
1776
1777 gdk_win32_hdc_release (&draw_impl->parent_instance, gc, GDK_GC_FOREGROUND);
1778 }
1779
1780 static void
1781 gdk_win32_draw_image (GdkDrawable *drawable,
1782 GdkGC *gc,
1783 GdkImage *image,
1784 gint xsrc,
1785 gint ysrc,
1786 gint xdest,
1787 gint ydest,
1788 gint width,
1789 gint height)
1790 {
1791 g_assert (GDK_IS_DRAWABLE_IMPL_WIN32 (drawable));
1792
1793 _gdk_win32_blit (TRUE, (GdkDrawableImplWin32 *) drawable,
1794 gc, (GdkPixmap *) image->windowing_data,
1795 xsrc, ysrc, xdest, ydest, width, height);
1796 }
1797
1798 static void
1799 gdk_win32_draw_pixbuf (GdkDrawable *drawable,
1800 GdkGC *gc,
1801 GdkPixbuf *pixbuf,
1802 gint src_x,
1803 gint src_y,
1804 gint dest_x,
1805 gint dest_y,
1806 gint width,
1807 gint height,
1808 GdkRgbDither dither,
1809 gint x_dither,
1810 gint y_dither)
1811 {
1812 GdkDrawable *wrapper = GDK_DRAWABLE_IMPL_WIN32 (drawable)->wrapper;
1813 GDK_DRAWABLE_CLASS (_gdk_drawable_impl_win32_parent_class)->draw_pixbuf (wrapper, gc, pixbuf,
1814 src_x, src_y, dest_x, dest_y,
1815 width, height,
1816 dither, x_dither, y_dither);
1817 }
1818
1819 /**
1820 * _gdk_win32_drawable_acquire_dc
1821 * @drawable: a Win32 #GdkDrawable implementation
1822 *
1823 * Gets a DC with the given drawable selected into
1824 * it.
1825 *
1826 * Return value: The DC, on success. Otherwise
1827 * %NULL. If this function succeeded
1828 * _gdk_win32_drawable_release_dc() must be called
1829 * release the DC when you are done using it.
1830 **/
1831 HDC
1832 _gdk_win32_drawable_acquire_dc (GdkDrawable *drawable)
1833 {
1834 GdkDrawableImplWin32 *impl = GDK_DRAWABLE_IMPL_WIN32 (drawable);
1835
1836 if (GDK_IS_WINDOW_IMPL_WIN32 (drawable) &&
1837 GDK_WINDOW_DESTROYED (impl->wrapper))
1838 return NULL;
1839
1840 if (!impl->hdc)
1841 {
1842 if (GDK_IS_PIXMAP_IMPL_WIN32 (impl))
1843 {
1844 impl->hdc = CreateCompatibleDC (NULL);
1845 if (!impl->hdc)
1846 WIN32_GDI_FAILED ("CreateCompatibleDC");
1847
1848 if (impl->hdc)
1849 {
1850 impl->saved_dc_bitmap = SelectObject (impl->hdc,
1851 impl->handle);
1852 if (!impl->saved_dc_bitmap)
1853 {
1854 WIN32_GDI_FAILED ("SelectObject");
1855 DeleteDC (impl->hdc);
1856 impl->hdc = NULL;
1857 }
1858 }
1859 }
1860 else
1861 {
1862 impl->hdc = GetDC (impl->handle);
1863 if (!impl->hdc)
1864 WIN32_GDI_FAILED ("GetDC");
1865 }
1866 }
1867
1868 if (impl->hdc)
1869 {
1870 impl->hdc_count++;
1871 return impl->hdc;
1872 }
1873 else
1874 {
1875 return NULL;
1876 }
1877 }
1878
1879 /**
1880 * _gdk_win32_drawable_release_dc
1881 * @drawable: a Win32 #GdkDrawable implementation
1882 *
1883 * Releases the reference count for the DC
1884 * from _gdk_win32_drawable_acquire_dc()
1885 **/
1886 void
1887 _gdk_win32_drawable_release_dc (GdkDrawable *drawable)
1888 {
1889 GdkDrawableImplWin32 *impl = GDK_DRAWABLE_IMPL_WIN32 (drawable);
1890
1891 g_return_if_fail (impl->hdc_count > 0);
1892
1893 impl->hdc_count--;
1894 if (impl->hdc_count == 0)
1895 {
1896 if (impl->saved_dc_bitmap)
1897 {
1898 GDI_CALL (SelectObject, (impl->hdc, impl->saved_dc_bitmap));
1899 impl->saved_dc_bitmap = NULL;
1900 }
1901
1902 if (impl->hdc)
1903 {
1904 if (GDK_IS_PIXMAP_IMPL_WIN32 (impl))
1905 GDI_CALL (DeleteDC, (impl->hdc));
1906 else
1907 GDI_CALL (ReleaseDC, (impl->handle, impl->hdc));
1908 impl->hdc = NULL;
1909 }
1910 }
1911 }
1912
1913 static void
1914 gdk_win32_cairo_surface_release_hdc (void *data)
1915 {
1916 _gdk_win32_drawable_release_dc (GDK_DRAWABLE (data));
1917 }
1918
1919 cairo_surface_t *
1920 _gdk_windowing_create_cairo_surface (GdkDrawable *drawable,
1921 gint width,
1922 gint height)
1923 {
1924 cairo_surface_t *surface;
1925 HDC hdc;
1926
1927 hdc = _gdk_win32_drawable_acquire_dc (drawable);
1928 if (!hdc)
1929 return NULL;
1930
1931 surface = cairo_win32_surface_create (hdc);
1932
1933 /* Whenever the cairo surface is destroyed, we need to release the
1934 * HDC that was acquired */
1935 cairo_surface_set_user_data (surface, &gdk_win32_cairo_hdc_key,
1936 drawable,
1937 gdk_win32_cairo_surface_release_hdc);
1938
1939 return surface;
1940 }
1941
1942 static void
1943 gdk_win32_cairo_surface_destroy (void *data)
1944 {
1945 GdkDrawableImplWin32 *impl = data;
1946
1947 impl->cairo_surface = NULL;
1948 }
1949
1950 static cairo_surface_t *
1951 gdk_win32_ref_cairo_surface (GdkDrawable *drawable)
1952 {
1953 GdkDrawableImplWin32 *impl = GDK_DRAWABLE_IMPL_WIN32 (drawable);
1954
1955 if (GDK_IS_WINDOW_IMPL_WIN32 (drawable) &&
1956 GDK_WINDOW_DESTROYED (impl->wrapper))
1957 return NULL;
1958
1959 if (!impl->cairo_surface)
1960 {
1961 /* width and height are determined from the DC */
1962 impl->cairo_surface = _gdk_windowing_create_cairo_surface (drawable, 0, 0);
1963
1964 /* Whenever the cairo surface is destroyed, we need to clear the
1965 * pointer that we had stored here */
1966 cairo_surface_set_user_data (impl->cairo_surface, &gdk_win32_cairo_key,
1967 drawable,
1968 gdk_win32_cairo_surface_destroy);
1969 }
1970 else
1971 cairo_surface_reference (impl->cairo_surface);
1972
1973 return impl->cairo_surface;
1974 }
1975
1976 void
1977 _gdk_windowing_set_cairo_surface_size (cairo_surface_t *surface,
1978 gint width,
1979 gint height)
1980 {
1981 // Do nothing. The surface size is determined by the DC
1982 }
1983
1984 static gint
1985 gdk_win32_get_depth (GdkDrawable *drawable)
1986 {
1987 /* This is a bit bogus but I'm not sure the other way is better */
1988
1989 return gdk_drawable_get_depth (GDK_DRAWABLE_IMPL_WIN32 (drawable)->wrapper);
1990 }
1991
1992 static GdkScreen*
1993 gdk_win32_get_screen (GdkDrawable *drawable)
1994 {
1995 return gdk_screen_get_default ();
1996 }
1997
1998 static GdkVisual*
1999 gdk_win32_get_visual (GdkDrawable *drawable)
2000 {
2001 return gdk_drawable_get_visual (GDK_DRAWABLE_IMPL_WIN32 (drawable)->wrapper);
2002 }
2003
2004 HGDIOBJ
2005 gdk_win32_drawable_get_handle (GdkDrawable *drawable)
2006 {
2007 if (GDK_IS_WINDOW (drawable))
2008 {
2009 GdkWindow *window = (GdkWindow *)drawable;
2010
2011 /* Try to ensure the window has a native window */
2012 if (!_gdk_window_has_impl (window))
2013 gdk_window_ensure_native (window);
2014
2015 if (!GDK_WINDOW_IS_WIN32 (window))
2016 {
2017 g_warning (G_STRLOC " drawable is not a native Win32 window");
2018 return NULL;
2019 }
2020 }
2021 else if (!GDK_IS_PIXMAP (drawable))
2022 {
2023 g_warning (G_STRLOC " drawable is not a pixmap or window");
2024 return NULL;
2025 }
2026
2027 return GDK_DRAWABLE_HANDLE (drawable);
2028 }
2029
2030 /**
2031 * _gdk_win32_drawable_finish
2032 * @drawable: a Win32 #GdkDrawable implementation
2033 *
2034 * Releases any resources allocated internally for the drawable.
2035 * This is called when the drawable becomes unusable
2036 * (gdk_window_destroy() for a window, or the refcount going to
2037 * zero for a pixmap.)
2038 **/
2039 void
2040 _gdk_win32_drawable_finish (GdkDrawable *drawable)
2041 {
2042 GdkDrawableImplWin32 *impl = GDK_DRAWABLE_IMPL_WIN32 (drawable);
2043
2044 if (impl->cairo_surface)
2045 {
2046 cairo_surface_finish (impl->cairo_surface);
2047 cairo_surface_set_user_data (impl->cairo_surface, &gdk_win32_cairo_hdc_key, NULL, NULL);
2048 cairo_surface_set_user_data (impl->cairo_surface, &gdk_win32_cairo_key, NULL, NULL);
2049 }
2050
2051 /* impl->hdc_count doesn't have to be 0 here; as there may still be surfaces
2052 * created with gdk_windowing_create_cairo_surface() out there, which are not
2053 * managed internally by the drawable */
2054 }