gdkgc-win32.c 39.6 KB
Newer Older
1
2
/* GDK - The GIMP Drawing Kit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3
 * Copyright (C) 1998-2002 Tor Lillqvist
4
5
 *
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
8
9
10
11
12
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
16
17
18
19
20
21
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/*
22
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
23
24
25
26
27
28
29
30
31
32
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

#include <string.h>

#include "gdkgc.h"
#include "gdkfont.h"
#include "gdkpixmap.h"
33
34
#include "gdkregion-generic.h"
#include "gdkprivate-win32.h"
Tor Lillqvist's avatar
Tor Lillqvist committed
35
36
37
38
39
40
41
42

static void gdk_win32_gc_get_values (GdkGC           *gc,
				     GdkGCValues     *values);
static void gdk_win32_gc_set_values (GdkGC           *gc,
				     GdkGCValues     *values,
				     GdkGCValuesMask  values_mask);
static void gdk_win32_gc_set_dashes (GdkGC           *gc,
				     gint             dash_offset,
43
				     gint8            dash_list[],
Tor Lillqvist's avatar
Tor Lillqvist committed
44
45
				     gint             n);

46
47
48
49
50
51
static void gdk_gc_win32_class_init (GdkGCWin32Class *klass);
static void gdk_gc_win32_finalize   (GObject         *object);

static gpointer parent_class = NULL;

GType
52
_gdk_gc_win32_get_type (void)
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
{
  static GType object_type = 0;

  if (!object_type)
    {
      static const GTypeInfo object_info =
      {
        sizeof (GdkGCWin32Class),
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) gdk_gc_win32_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data */
        sizeof (GdkGCWin32),
        0,              /* n_preallocs */
        (GInstanceInitFunc) NULL,
      };
      
      object_type = g_type_register_static (GDK_TYPE_GC,
                                            "GdkGCWin32",
Tor Lillqvist's avatar
Tor Lillqvist committed
73
                                            &object_info, 0);
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
    }
  
  return object_type;
}

static void
gdk_gc_win32_class_init (GdkGCWin32Class *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GdkGCClass *gc_class = GDK_GC_CLASS (klass);
  
  parent_class = g_type_class_peek_parent (klass);

  object_class->finalize = gdk_gc_win32_finalize;

  gc_class->get_values = gdk_win32_gc_get_values;
  gc_class->set_values = gdk_win32_gc_set_values;
  gc_class->set_dashes = gdk_win32_gc_set_dashes;
}

static void
gdk_gc_win32_finalize (GObject *object)
{
  GdkGCWin32 *win32_gc = GDK_GC_WIN32 (object);
  
  if (win32_gc->clip_region)
    gdk_region_destroy (win32_gc->clip_region);
  
  if (win32_gc->values_mask & GDK_GC_FONT)
    gdk_font_unref (win32_gc->font);
  
  if (win32_gc->values_mask & GDK_GC_TILE)
    gdk_drawable_unref (win32_gc->tile);
  
  if (win32_gc->values_mask & GDK_GC_STIPPLE)
    gdk_drawable_unref (win32_gc->stipple);
  
  G_OBJECT_CLASS (parent_class)->finalize (object);
}
113

114
115
116
static void
gdk_win32_gc_values_to_win32values (GdkGCValues    *values,
				    GdkGCValuesMask mask,
117
				    GdkGCWin32     *win32_gc)
118
119
{				    
  char *s = "";
120
  gint sw, sh;
121

122
  GDK_NOTE (GC, g_print ("{"));
123

124
  if (mask & GDK_GC_FOREGROUND)
125
    {
126
127
      win32_gc->foreground = values->foreground.pixel;
      win32_gc->values_mask |= GDK_GC_FOREGROUND;
128
129
      GDK_NOTE (GC, (g_print ("fg=%.06lx", win32_gc->foreground),
		     s = ","));
130
131
    }
  
132
  if (mask & GDK_GC_BACKGROUND)
133
    {
134
135
      win32_gc->background = values->background.pixel;
      win32_gc->values_mask |= GDK_GC_BACKGROUND;
136
137
      GDK_NOTE (GC, (g_print ("%sbg=%.06lx", s, win32_gc->background),
		     s = ","));
138
139
    }

140
141
  if ((mask & GDK_GC_FONT) && (values->font->type == GDK_FONT_FONT
			       || values->font->type == GDK_FONT_FONTSET))
142
    {
143
144
145
146
      if (win32_gc->font != NULL)
	gdk_font_unref (win32_gc->font);
      win32_gc->font = values->font;
      if (win32_gc->font != NULL)
147
148
149
	{
	  gchar *xlfd;

150
151
	  gdk_font_ref (win32_gc->font);
	  win32_gc->values_mask |= GDK_GC_FONT;
152
153
154
155
	  GDK_NOTE (GC, (xlfd = gdk_font_full_name_get (win32_gc->font),
			 g_print ("%sfont=%s", s, xlfd),
			 s = ",",
			 gdk_font_full_name_free (xlfd)));
156
157
158
	}
      else
	{
159
	  win32_gc->values_mask &= ~GDK_GC_FONT;
160
161
	  GDK_NOTE (GC, (g_print ("%sfont=NULL", s),
			 s = ","));
162
	}
163
164
    }

165
  if (mask & GDK_GC_FUNCTION)
166
    {
167
168
      GDK_NOTE (GC, (g_print ("%srop2=", s),
		     s = ","));
169
170
      switch (values->function)
	{
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#define CASE(x,y) case GDK_##x: win32_gc->rop2 = R2_##y; GDK_NOTE (GC, g_print (#y)); break
	CASE (COPY, COPYPEN);
	CASE (INVERT, NOT);
	CASE (XOR, XORPEN);
	CASE (CLEAR, BLACK);
	CASE (AND, MASKPEN);
	CASE (AND_REVERSE, MASKPENNOT);
	CASE (AND_INVERT, MASKNOTPEN);
	CASE (NOOP, NOP);
	CASE (OR, MERGEPEN);
	CASE (EQUIV, NOTXORPEN);
	CASE (OR_REVERSE, MERGEPENNOT);
	CASE (COPY_INVERT, NOTCOPYPEN);
	CASE (OR_INVERT, MERGENOTPEN);
	CASE (NAND, NOTMASKPEN);
	CASE (NOR, NOTMERGEPEN);
	CASE (SET, WHITE);
#undef CASE
189
	}
190
      win32_gc->values_mask |= GDK_GC_FUNCTION;
191
192
    }

193
  if (mask & GDK_GC_FILL)
194
    {
195
196
      win32_gc->fill_style = values->fill;
      win32_gc->values_mask |= GDK_GC_FILL;
197
198
      GDK_NOTE (GC, (g_print ("%sfill=%d", s, win32_gc->fill_style),
		     s = ","));
199
200
    }

201
  if (mask & GDK_GC_TILE)
202
    {
203
204
205
206
      if (win32_gc->tile != NULL)
	gdk_drawable_unref (win32_gc->tile);
      win32_gc->tile = values->tile;
      if (win32_gc->tile != NULL)
207
	{
208
209
	  gdk_drawable_ref (win32_gc->tile);
	  win32_gc->values_mask |= GDK_GC_TILE;
210
211
212
	  GDK_NOTE (GC,
		    (g_print ("%stile=%p", s,
			      GDK_PIXMAP_HBITMAP (win32_gc->tile)),
213
		     s = ","));
214
215
216
	}
      else
	{
217
	  win32_gc->values_mask &= ~GDK_GC_TILE;
218
219
	  GDK_NOTE (GC, (g_print ("%stile=NULL", s),
			 s = ","));
220
	}
221
222
    }

223
  if (mask & GDK_GC_STIPPLE)
224
    {
225
226
227
228
      if (win32_gc->stipple != NULL)
	gdk_drawable_unref (win32_gc->stipple);
      win32_gc->stipple = values->stipple;
      if (win32_gc->stipple != NULL)
229
	{
230
	  gdk_drawable_get_size (win32_gc->stipple, &sw, &sh);
231

232
233
234
235
236
237
#if 0 /* HB: this size limitation is disabled to make radio and check
       * buttons work. I got the impression from the API docs, that
       * it shouldn't be necessary at all, but win9x would do the clipping
       */
	  if (   (sw != 8 || sh != 8)
	      && !IS_WIN_NT ()) /* HB: the MSDN says it's a Win95 limitation */
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
	    {
	      /* It seems that it *must* be 8x8, at least on my machine. 
	       * Thus, tile an 8x8 bitmap with the stipple in case it is
	       * smaller, or simply use just the top left 8x8 in case it is
	       * larger.
	       */
	      gchar dummy[8];
	      GdkPixmap *bm = gdk_bitmap_create_from_data (NULL, dummy, 8, 8);
	      GdkGC *gc = gdk_gc_new (bm);
	      gint i, j;

	      i = 0;
	      while (i < 8)
		{
		  j = 0;
		  while (j < 8)
		    {
255
		      gdk_draw_drawable (bm, gc, win32_gc->stipple, 0, 0, i, j, sw, sh);
256
257
258
259
		      j += sh;
		    }
		  i += sw;
		}
260
	      win32_gc->stipple = bm;
261
262
263
	      gdk_gc_unref (gc);
	    }
	  else
264
#endif
265
266
	    gdk_drawable_ref (win32_gc->stipple);
	  win32_gc->values_mask |= GDK_GC_STIPPLE;
267
268
269
	  GDK_NOTE (GC,
		    (g_print ("%sstipple=%p", s,
			      GDK_PIXMAP_HBITMAP (win32_gc->stipple)),
270
		     s = ","));
271
272
273
	}
      else
	{
274
	  win32_gc->values_mask &= ~GDK_GC_STIPPLE;
275
276
	  GDK_NOTE (GC, (g_print ("%sstipple=NULL", s),
			 s = ","));
277
	}
278
279
    }

280
  if (mask & GDK_GC_CLIP_MASK)
281
    {
282
      if (win32_gc->clip_region != NULL)
283
	{
284
285
	  gdk_region_destroy (win32_gc->clip_region);
	  win32_gc->clip_region = NULL;
286
	}
287

288
      if (win32_gc->hcliprgn != NULL)
289
290
291
292
	{
	  DeleteObject (win32_gc->hcliprgn);
	  win32_gc->hcliprgn = NULL;
	}
293

294
295
      if (values->clip_mask != NULL)
	{
296
	  win32_gc->hcliprgn =
297
	    _gdk_win32_bitmap_to_region (values->clip_mask);
298
	  win32_gc->values_mask |= GDK_GC_CLIP_MASK;
299
300
301
	}
      else
	{
302
303
	  win32_gc->hcliprgn = NULL;
	  win32_gc->values_mask &= ~GDK_GC_CLIP_MASK;
304
	}
305
306
      GDK_NOTE (GC, (g_print ("%sclip=%p", s, win32_gc->hcliprgn),
		     s = ","));
307
308
    }

309
  if (mask & GDK_GC_SUBWINDOW)
310
    {
311
312
      win32_gc->subwindow_mode = values->subwindow_mode;
      win32_gc->values_mask |= GDK_GC_SUBWINDOW;
313
314
      GDK_NOTE (GC, (g_print ("%ssubw=%d", s, win32_gc->subwindow_mode),
		     s = ","));
315
316
    }

317
  if (mask & GDK_GC_TS_X_ORIGIN)
318
    {
319
      win32_gc->values_mask |= GDK_GC_TS_X_ORIGIN;
320
321
      GDK_NOTE (GC, (g_print ("%sts_x=%d", s, values->ts_x_origin),
		     s = ","));
322
323
    }

324
  if (mask & GDK_GC_TS_Y_ORIGIN)
325
    {
326
      win32_gc->values_mask |= GDK_GC_TS_Y_ORIGIN;
327
328
      GDK_NOTE (GC, (g_print ("%sts_y=%d", s, values->ts_y_origin),
		     s = ","));
329
330
    }

331
  if (mask & GDK_GC_CLIP_X_ORIGIN)
332
    {
333
      win32_gc->values_mask |= GDK_GC_CLIP_X_ORIGIN;
334
335
      GDK_NOTE (GC, (g_print ("%sclip_x=%d", s, values->clip_x_origin),
		     s = ","));
336
337
    }

338
  if (mask & GDK_GC_CLIP_Y_ORIGIN)
339
    {
340
      win32_gc->values_mask |= GDK_GC_CLIP_Y_ORIGIN;
341
342
      GDK_NOTE (GC, (g_print ("%sclip_y=%d", s, values->clip_y_origin),
		     s = ","));
Hans Breuer's avatar
updated    
Hans Breuer committed
343
344
    }

345
  if (mask & GDK_GC_EXPOSURES)
346
    {
347
348
      win32_gc->graphics_exposures = values->graphics_exposures;
      win32_gc->values_mask |= GDK_GC_EXPOSURES;
349
350
      GDK_NOTE (GC, (g_print ("%sexp=%d", s, win32_gc->graphics_exposures),
		     s = ","));
351
352
    }

353
354
  if (mask & GDK_GC_LINE_WIDTH)
    {
355
356
      win32_gc->pen_width = values->line_width;
      win32_gc->values_mask |= GDK_GC_LINE_WIDTH;
357
358
      GDK_NOTE (GC, (g_print ("%spw=%d", s, win32_gc->pen_width),
		     s = ","));
359
    }
360

361
  if (mask & GDK_GC_LINE_STYLE)
362
    {
363
      switch (values->line_style)
364
	{
365
	case GDK_LINE_SOLID:
366
        win32_gc->pen_style &= ~(PS_STYLE_MASK);
367
368
	  GDK_NOTE (GC, (g_print ("%sps|=LINE_SOLID", s),
			 s = ","));
369
	  win32_gc->pen_style |= PS_SOLID;
370
371
372
	  break;
	case GDK_LINE_ON_OFF_DASH:
	case GDK_LINE_DOUBLE_DASH: /* ??? */
373
	  /* Only set the linestyle here, if it isn't already set
374
375
376
377
378
	   * gdk_win32_gc_set_dashes () knows better
	   */
	  if (0 == (win32_gc->values_mask & GDK_GC_LINE_STYLE))
	    {
            win32_gc->pen_style &= ~(PS_STYLE_MASK);
379
380
	      GDK_NOTE (GC, (g_print ("%sps|=DASH", s),
			     s = ","));
381
382
	      win32_gc->pen_style |= PS_DASH;
	    }
383
	  break;
384
	}
385
      win32_gc->values_mask |= GDK_GC_LINE_STYLE;
386
387
    }

388
  if (mask & GDK_GC_CAP_STYLE)
389
    {
390
      win32_gc->pen_style &= ~(PS_ENDCAP_MASK);
391
392
393
394
      switch (values->cap_style)
	{
	case GDK_CAP_NOT_LAST:	/* ??? */
	case GDK_CAP_BUTT:
395
396
	  GDK_NOTE (GC, (g_print ("%sps|=ENDCAP_FLAT", s),
			 s = ","));
397
	  win32_gc->pen_style |= PS_ENDCAP_FLAT;
398
	  break;
399
	case GDK_CAP_ROUND:
400
401
	  GDK_NOTE (GC, (g_print ("%sps|=ENDCAP_ROUND", s),
			 s = ","));
402
	  win32_gc->pen_style |= PS_ENDCAP_ROUND;
403
	  break;
404
	case GDK_CAP_PROJECTING:
405
406
	  GDK_NOTE (GC, (g_print ("%sps|=ENDCAP_SQUARE", s),
			 s = ","));
407
	  win32_gc->pen_style |= PS_ENDCAP_SQUARE;
408
	  break;
409
	}
410
      win32_gc->values_mask |= GDK_GC_CAP_STYLE;
411
412
    }

413
  if (mask & GDK_GC_JOIN_STYLE)
414
    {
415
      win32_gc->pen_style &= ~(PS_JOIN_MASK);
416
417
418
      switch (values->join_style)
	{
	case GDK_JOIN_MITER:
419
420
	  GDK_NOTE (GC, (g_print ("%sps|=JOIN_MITER", s),
			 s = ","));
421
	  win32_gc->pen_style |= PS_JOIN_MITER;
422
423
	  break;
	case GDK_JOIN_ROUND:
424
425
	  GDK_NOTE (GC, (g_print ("%sps|=JOIN_ROUND", s),
			 s = ","));
426
	  win32_gc->pen_style |= PS_JOIN_ROUND;
427
428
	  break;
	case GDK_JOIN_BEVEL:
429
430
	  GDK_NOTE (GC, (g_print ("%sps|=JOIN_BEVEL", s),
			 s = ","));
431
	  win32_gc->pen_style |= PS_JOIN_BEVEL;
432
433
	  break;
	}
434
      win32_gc->values_mask |= GDK_GC_JOIN_STYLE;
435
    }
436
  GDK_NOTE (GC, g_print ("}\n"));
437
438
439
440
441
442
443
444
}

GdkGC*
_gdk_win32_gc_new (GdkDrawable	  *drawable,
		   GdkGCValues	  *values,
		   GdkGCValuesMask mask)
{
  GdkGC *gc;
445
  GdkGCWin32 *win32_gc;
446

447
448
449
450
  /* NOTICE that the drawable here has to be the impl drawable,
   * not the publically-visible drawables.
   */
  g_return_val_if_fail (GDK_IS_DRAWABLE_IMPL_WIN32 (drawable), NULL);
451

452
  gc = g_object_new (_gdk_gc_win32_get_type (), NULL);
453
454
455
456
457
  win32_gc = GDK_GC_WIN32 (gc);

  win32_gc->hdc = NULL;
  win32_gc->clip_region = NULL;
  win32_gc->hcliprgn = NULL;
458

459
460
461
  /* Use the same default values as X11 does, even if they don't make
   * sense per se. But apps always set fg and bg anyway.
   */
462
463
464
465
466
467
468
469
470
471
472
  win32_gc->foreground = 0;
  win32_gc->background = 1;
  win32_gc->font = NULL;
  win32_gc->rop2 = R2_COPYPEN;
  win32_gc->fill_style = GDK_SOLID;
  win32_gc->tile = NULL;
  win32_gc->stipple = NULL;
  win32_gc->pen_style = PS_GEOMETRIC|PS_ENDCAP_FLAT|PS_JOIN_MITER;
  win32_gc->pen_width = 0;

  win32_gc->values_mask = GDK_GC_FUNCTION | GDK_GC_FILL;
473

474
  GDK_NOTE (GC, g_print ("_gdk_win32_gc_new: "));
475
  gdk_win32_gc_values_to_win32values (values, mask, win32_gc);
476

477
  win32_gc->hwnd = NULL;
478

479
  GDK_NOTE (GC, g_print (" = %p\n", gc));
480
481
482
483

  return gc;
}

Tor Lillqvist's avatar
Tor Lillqvist committed
484
485
486
static void
gdk_win32_gc_get_values (GdkGC       *gc,
			 GdkGCValues *values)
487
{
488
  GdkGCWin32 *win32_gc = GDK_GC_WIN32 (gc);
489

490
491
492
  values->foreground.pixel = win32_gc->foreground;
  values->background.pixel = win32_gc->background;
  values->font = win32_gc->font;
493

494
  switch (win32_gc->rop2)
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
    {
    case R2_COPYPEN:
      values->function = GDK_COPY; break;
    case R2_NOT:
      values->function = GDK_INVERT; break;
    case R2_XORPEN:
      values->function = GDK_XOR; break;
    case R2_BLACK:
      values->function = GDK_CLEAR; break;
    case R2_MASKPEN:
      values->function = GDK_AND; break;
    case R2_MASKPENNOT:
      values->function = GDK_AND_REVERSE; break;
    case R2_MASKNOTPEN:
      values->function = GDK_AND_INVERT; break;
    case R2_NOP:
      values->function = GDK_NOOP; break;
    case R2_MERGEPEN:
      values->function = GDK_OR; break;
    case R2_NOTXORPEN:
      values->function = GDK_EQUIV; break;
    case R2_MERGEPENNOT:
      values->function = GDK_OR_REVERSE; break;
    case R2_NOTCOPYPEN:
      values->function = GDK_COPY_INVERT; break;
    case R2_MERGENOTPEN:
      values->function = GDK_OR_INVERT; break;
    case R2_NOTMASKPEN:
      values->function = GDK_NAND; break;
524
525
    case R2_NOTMERGEPEN:
      values->function = GDK_NOR; break;
526
527
528
529
    case R2_WHITE:
      values->function = GDK_SET; break;
    }

530
  values->fill = win32_gc->fill_style;
531

532
533
  values->tile = win32_gc->tile;
  values->stipple = win32_gc->stipple;
534

535
536
537
  /* Also the X11 backend always returns a NULL clip_mask */
  values->clip_mask = NULL;

538
539
540
541
542
543
544
  values->subwindow_mode = win32_gc->subwindow_mode;
  values->ts_x_origin = win32_gc->parent_instance.ts_x_origin;
  values->ts_y_origin = win32_gc->parent_instance.ts_y_origin;
  values->clip_x_origin = win32_gc->parent_instance.clip_x_origin;
  values->clip_y_origin = win32_gc->parent_instance.clip_y_origin;
  values->graphics_exposures = win32_gc->graphics_exposures;
  values->line_width = win32_gc->pen_width;
545
  
546
  if (win32_gc->pen_style & PS_SOLID)
547
    values->line_style = GDK_LINE_SOLID;
548
  else if (win32_gc->pen_style & PS_DASH)
549
550
551
552
553
    values->line_style = GDK_LINE_ON_OFF_DASH;
  else
    values->line_style = GDK_LINE_SOLID;

  /* PS_ENDCAP_ROUND is zero */
554
  if (win32_gc->pen_style & PS_ENDCAP_FLAT)
555
    values->cap_style = GDK_CAP_BUTT;
556
  else if (win32_gc->pen_style & PS_ENDCAP_SQUARE)
557
558
559
560
561
    values->cap_style = GDK_CAP_PROJECTING;
  else
    values->cap_style = GDK_CAP_ROUND;
    
  /* PS_JOIN_ROUND is zero */
562
  if (win32_gc->pen_style & PS_JOIN_MITER)
563
    values->join_style = GDK_JOIN_MITER;
564
  else if (win32_gc->pen_style & PS_JOIN_BEVEL)
565
566
567
568
569
    values->join_style = GDK_JOIN_BEVEL;
  else
    values->join_style = GDK_JOIN_ROUND;
}

Tor Lillqvist's avatar
Tor Lillqvist committed
570
571
572
static void
gdk_win32_gc_set_values (GdkGC           *gc,
			 GdkGCValues     *values,
573
			 GdkGCValuesMask  mask)
574
{
575
576
  g_return_if_fail (GDK_IS_GC (gc));

577
  GDK_NOTE (GC, g_print ("gdk_win32_gc_set_values: "));
578
579

  gdk_win32_gc_values_to_win32values (values, mask, GDK_GC_WIN32 (gc));
Tor Lillqvist's avatar
Tor Lillqvist committed
580
581
582
583
}

static void
gdk_win32_gc_set_dashes (GdkGC *gc,
584
585
			 gint	dash_offset,
			 gint8  dash_list[],
Tor Lillqvist's avatar
Tor Lillqvist committed
586
587
			 gint   n)
{
588
589
590
591
  GdkGCWin32 *win32_gc;

  g_return_if_fail (GDK_IS_GC (gc));
  g_return_if_fail (dash_list != NULL);
Tor Lillqvist's avatar
Tor Lillqvist committed
592

593
594
  win32_gc = GDK_GC_WIN32 (gc);

595
596
597
  /* mark as set, see gdk_win32_gc_values_to_win32values () for the reason */
  win32_gc->values_mask |= GDK_GC_LINE_STYLE;

598
  win32_gc->pen_style &= ~(PS_STYLE_MASK);
599
600
601
602
603
604
605
606
607
608
609

  /* 
   * Set the extended line style. This could be done by 
   * PS_USERSTYLE and ExtCreatePen; but ONLY on WinNT, 
   * so let's make a guess (based on the implementation 
   * in DIA). On Win9x this does only work for lines
   * with width one ...
   *
   * More workarounds for Win9x descibed at:
   * http://www.codeguru.com/gdi/dashed.shtml
   */
Tor Lillqvist's avatar
Tor Lillqvist committed
610
  if (!IS_WIN_NT () && win32_gc->pen_width > 1)
611
    {
612
      GDK_NOTE (GC, g_print ("gdk_win32_gc_set_dashes: not fully supported\n"));
613
      win32_gc->pen_style |= PS_SOLID;
614
615
616
      return;
    }
  
Hans Breuer's avatar
Hans Breuer committed
617
  win32_gc->pen_style = PS_COSMETIC; /* ??? */
618
  switch (n)
619
    {
620
    case 2:
621
622
      if ((dash_list[0] == dash_list[1]) && (dash_list[0] > 2))
        {
623
          win32_gc->pen_style |= PS_DASH;
624
625
          GDK_NOTE (GC, g_print ("gdk_win32_gc_set_dashes: PS_DASH (%d,%d)\n", 
				 dash_list[0], dash_list[1]));
626
627
628
        }
      else
        {
629
          win32_gc->pen_style |= PS_DOT;
630
631
          GDK_NOTE (GC, g_print ("gdk_win32_gc_set_dashes: PS_DOT (%d,%d)\n", 
				 dash_list[0], dash_list[1]));
632
        }
633
634
635
      break;

    case 4:
636
      win32_gc->pen_style |= PS_DASHDOT; 
637
638
639
640
641
642
      GDK_NOTE (GC, g_print ("gdk_win32_gc_set_dashes: PS_DASHDOT (%d,%d,%d,%d)\n", 
			     dash_list[0], dash_list[1],
			     dash_list[2], dash_list[3]));
      break;

    case 6:
643
      win32_gc->pen_style |= PS_DASHDOTDOT; 
644
645
646
647
648
649
650
      GDK_NOTE (GC, g_print ("gdk_win32_gc_set_dashes: PS_DASHDOTDOT (%d,%d,%d,%d,%d,%d)\n", 
			     dash_list[0], dash_list[1],
			     dash_list[2], dash_list[3],
			     dash_list[4], dash_list[5]));
      break;

    default:
651
      win32_gc->pen_style |= PS_DASH;
652
653
      GDK_NOTE (GC, g_print ("gdk_win32_gc_set_dashes: no guess for %d dashes\n", n));
      break;
654
    }
655
656
657
658
659
660
}

void
gdk_gc_set_clip_rectangle (GdkGC	*gc,
			   GdkRectangle *rectangle)
{
661
662
663
  GdkGCWin32 *win32_gc;

  g_return_if_fail (GDK_IS_GC (gc));
664

665
  win32_gc = GDK_GC_WIN32 (gc);
666

667
668
669
670
671
672
  if (win32_gc->hcliprgn)
    {
      DeleteObject (win32_gc->hcliprgn);
      win32_gc->hcliprgn = NULL;
    }

673
674
  if (win32_gc->clip_region)
    gdk_region_destroy (win32_gc->clip_region);
675

676
677
  if (rectangle)
    {
678
      GDK_NOTE (GC,
679
		g_print ("gdk_gc_set_clip_rectangle: %dx%d@+%d+%d\n",
680
681
			 rectangle->width, rectangle->height,
			 rectangle->x, rectangle->y));
682
683
      win32_gc->clip_region = gdk_region_rectangle (rectangle);
      win32_gc->values_mask |= GDK_GC_CLIP_MASK;
684
685
686
    }
  else
    {
687
      GDK_NOTE (GC, g_print ("gdk_gc_set_clip_rectangle: NULL\n"));
688

689
690
      win32_gc->clip_region = NULL;
      win32_gc->values_mask &= ~GDK_GC_CLIP_MASK;
691
    }
692
    win32_gc->values_mask &= ~(GDK_GC_CLIP_X_ORIGIN | GDK_GC_CLIP_Y_ORIGIN);
693
694
695
696
697
698
} 

void
gdk_gc_set_clip_region (GdkGC		 *gc,
			GdkRegion	 *region)
{
699
  GdkGCWin32 *win32_gc;
700

701
  g_return_if_fail (GDK_IS_GC (gc));
702

703
  win32_gc = GDK_GC_WIN32 (gc);
704

705
706
  if (win32_gc->clip_region)
    gdk_region_destroy (win32_gc->clip_region);
707
708
709

  if (region)
    {
710
      GDK_NOTE (GC, g_print ("gdk_gc_set_clip_region: %dx%d+%d+%d\n",
711
712
713
			       region->extents.x2 - region->extents.x1,
			       region->extents.y2 - region->extents.y1,
			       region->extents.x1, region->extents.y1));
714

715
716
      win32_gc->clip_region = gdk_region_copy (region);
      win32_gc->values_mask |= GDK_GC_CLIP_MASK;
717
718
719
    }
  else
    {
720
      GDK_NOTE (GC, g_print ("gdk_gc_set_clip_region: NULL\n"));
721

722
723
      win32_gc->clip_region = NULL;
      win32_gc->values_mask &= ~GDK_GC_CLIP_MASK;
724
    }
725

726
727
  gc->clip_x_origin = 0;
  gc->clip_y_origin = 0;
728

729
  win32_gc->values_mask &= ~(GDK_GC_CLIP_X_ORIGIN | GDK_GC_CLIP_Y_ORIGIN);
730
731
732
}

void
733
734
gdk_gc_copy (GdkGC *dst_gc,
	     GdkGC *src_gc)
735
{
Tor Lillqvist's avatar
Tor Lillqvist committed
736
737
  GdkGCWin32 *dst_win32_gc;
  GdkGCWin32 *src_win32_gc;
Tor Lillqvist's avatar
Tor Lillqvist committed
738

739
740
741
742
743
744
745
746
747
748
749
750
751
752
  g_return_if_fail (GDK_IS_GC_WIN32 (dst_gc));
  g_return_if_fail (GDK_IS_GC_WIN32 (src_gc));
  
  dst_win32_gc = GDK_GC_WIN32 (dst_gc);
  src_win32_gc = GDK_GC_WIN32 (src_gc);

  if (dst_win32_gc->font != NULL)
    gdk_font_unref (dst_win32_gc->font);
  if (dst_win32_gc->tile != NULL)
    gdk_drawable_unref (dst_win32_gc->tile);
  if (dst_win32_gc->stipple != NULL)
    gdk_drawable_unref (dst_win32_gc->stipple);
  if (dst_win32_gc->clip_region != NULL)
    gdk_region_destroy (dst_win32_gc->clip_region);
753
754
  if (dst_win32_gc->hcliprgn != NULL)
    DeleteObject (dst_win32_gc->hcliprgn);
Tor Lillqvist's avatar
Tor Lillqvist committed
755
  
756
757
  *dst_win32_gc = *src_win32_gc;

758
759
760
  if (dst_win32_gc->hcliprgn)
    {
      /* create a new region, to copy to */
761
      dst_win32_gc->hcliprgn = CreateRectRgn (0,0,1,1);
762
763
764
765
      /* overwrite from source */
      CombineRgn (dst_win32_gc->hcliprgn, src_win32_gc->hcliprgn,
		  NULL, RGN_COPY);
    }
766
767
768
769
770
771
772
773
  if (dst_win32_gc->clip_region != NULL)
    dst_win32_gc->clip_region = gdk_region_copy (dst_win32_gc->clip_region);
  if (dst_win32_gc->font != NULL)
    gdk_font_ref (dst_win32_gc->font);
  if (dst_win32_gc->tile != NULL)
    gdk_drawable_ref (dst_win32_gc->tile);
  if (dst_win32_gc->stipple != NULL)
    gdk_drawable_ref (dst_win32_gc->stipple);
774
775
}

776
777
778
static guint bitmask[9] = { 0, 1, 3, 7, 15, 31, 63, 127, 255 };

COLORREF
779
780
_gdk_win32_colormap_color (GdkColormap *colormap,
                           gulong       pixel)
781
{
Tor Lillqvist's avatar
Tor Lillqvist committed
782
783
  const GdkVisual *visual;
  GdkColormapPrivateWin32 *colormap_private;
784
785
  guchar r, g, b;

Tor Lillqvist's avatar
Tor Lillqvist committed
786
  if (colormap == NULL)
787
    return DIBINDEX (pixel & 1);
Tor Lillqvist's avatar
Tor Lillqvist committed
788

789
  colormap_private = GDK_WIN32_COLORMAP_DATA (colormap);
Tor Lillqvist's avatar
Tor Lillqvist committed
790

791
  g_assert (colormap_private != NULL);
Tor Lillqvist's avatar
Tor Lillqvist committed
792
793

  visual = colormap->visual;
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
  switch (visual->type)
    {
    case GDK_VISUAL_GRAYSCALE:
    case GDK_VISUAL_PSEUDO_COLOR:
    case GDK_VISUAL_STATIC_COLOR:
      return PALETTEINDEX (pixel);

    case GDK_VISUAL_TRUE_COLOR:
      r = (pixel & visual->red_mask) >> visual->red_shift;
      r = (r * 255) / bitmask[visual->red_prec];
      g = (pixel & visual->green_mask) >> visual->green_shift;
      g = (g * 255) / bitmask[visual->green_prec];
      b = (pixel & visual->blue_mask) >> visual->blue_shift;
      b = (b * 255) / bitmask[visual->blue_prec];
      return RGB (r, g, b);

    default:
      g_assert_not_reached ();
      return 0;
    }
814
}
815
816

static void
Tor Lillqvist's avatar
Tor Lillqvist committed
817
818
819
predraw_set_foreground (GdkGC       *gc,
			GdkColormap *colormap,
			gboolean    *ok)
820
821
822
823
824
{
  COLORREF fg;
  LOGBRUSH logbrush;
  HPEN hpen;
  HBRUSH hbr;
825
826
827
  GdkGCWin32 *win32_gc = (GdkGCWin32 *) gc;
  GdkColormapPrivateWin32 *colormap_private;
  gint k;
828

829
830
831
  if (colormap &&
      (colormap->visual->type == GDK_VISUAL_PSEUDO_COLOR ||
       colormap->visual->type == GDK_VISUAL_STATIC_COLOR))
832
    {
833
      colormap_private = GDK_WIN32_COLORMAP_DATA (colormap);
834

835
836
837
838
839
840
841
842
843
      g_assert (colormap_private != NULL);

      if (!(win32_gc->holdpal = SelectPalette (win32_gc->hdc, colormap_private->hpal, FALSE)))
	WIN32_GDI_FAILED ("SelectPalette"), *ok = FALSE;
      else if ((k = RealizePalette (win32_gc->hdc)) == GDI_ERROR)
	WIN32_GDI_FAILED ("RealizePalette"), *ok = FALSE;
      else if (k > 0)
	GDK_NOTE (COLORMAP, g_print ("predraw_set_foreground: realized %p: %d colors\n",
				     colormap_private->hpal, k));
844
    }
845

846
  fg = _gdk_win32_colormap_color (colormap, win32_gc->foreground);
847

848
849
  GDK_NOTE (GC, g_print ("predraw_set_foreground: fg=%06lx\n", fg));

850
  if (SetTextColor (win32_gc->hdc, fg) == CLR_INVALID)
851
    WIN32_GDI_FAILED ("SetTextColor"), *ok = FALSE;
852
853
854

  /* Create and select pen and brush. */

855
856
  logbrush.lbStyle = BS_SOLID;
  logbrush.lbColor = fg;
857
  logbrush.lbHatch = 0;
858

859
860
  if (*ok && (hpen = ExtCreatePen (win32_gc->pen_style,
				   (win32_gc->pen_width > 0 ? win32_gc->pen_width : 1),
861
				   &logbrush, 0, NULL)) == NULL)
862
    WIN32_GDI_FAILED ("ExtCreatePen"), *ok = FALSE;
863
  
864
  if (*ok && SelectObject (win32_gc->hdc, hpen) == NULL)
865
    WIN32_GDI_FAILED ("SelectObject"), *ok = FALSE;
866

867
  switch (win32_gc->fill_style)
868
    {
869
    case GDK_OPAQUE_STIPPLED:
Tor Lillqvist's avatar
Tor Lillqvist committed
870
      if (*ok && (hbr = CreatePatternBrush (GDK_PIXMAP_HBITMAP (win32_gc->stipple))) == NULL)
871
	WIN32_GDI_FAILED ("CreatePatternBrush"), *ok = FALSE;
Tor Lillqvist's avatar
Tor Lillqvist committed
872
873
      if (*ok && !SetBrushOrgEx(win32_gc->hdc, gc->ts_x_origin,
				gc->ts_y_origin, NULL))
874
	WIN32_GDI_FAILED ("SetBrushOrgEx"), *ok = FALSE;
875
	
876
      break;
877

878
879
    case GDK_SOLID:
    default:
880
881
      if (*ok && (hbr = CreateSolidBrush (fg)) == NULL)
	WIN32_GDI_FAILED ("CreateSolidBrush"), *ok = FALSE;
882
883
      break;
  }
884
885
886
887
888
889
  if (*ok)
    {
      HBRUSH old_hbr = SelectObject (win32_gc->hdc, hbr);
      if (old_hbr == NULL)
	WIN32_GDI_FAILED ("SelectObject"), *ok = FALSE;
    }
890
}  
891

892
static void
Tor Lillqvist's avatar
Tor Lillqvist committed
893
894
895
predraw_set_background (GdkGC       *gc,
			GdkColormap *colormap,
			gboolean    *ok)
896
{
Tor Lillqvist's avatar
Tor Lillqvist committed
897
  GdkGCWin32 *win32_gc = (GdkGCWin32 *) gc;
898

Hans Breuer's avatar
Hans Breuer committed
899
900
  if (win32_gc->values_mask & GDK_GC_BACKGROUND)
    {
901
      COLORREF bg = _gdk_win32_colormap_color (colormap, win32_gc->background);
Hans Breuer's avatar
Hans Breuer committed
902
903
904
905
906
907

      if (SetBkColor (win32_gc->hdc, bg) == CLR_INVALID)
        WIN32_GDI_FAILED ("SetBkColor"), *ok = FALSE;
    }
  else
    {
908
      if (!SetBkMode (win32_gc->hdc, TRANSPARENT))
Hans Breuer's avatar
Hans Breuer committed
909
910
        WIN32_GDI_FAILED ("SetBkMode"), *ok = FALSE;
    }
911
912
913
}

HDC
Tor Lillqvist's avatar
Tor Lillqvist committed
914
915
916
gdk_win32_hdc_get (GdkDrawable    *drawable,
		   GdkGC          *gc,
		   GdkGCValuesMask usage)
917
{
Tor Lillqvist's avatar
Tor Lillqvist committed
918
  GdkGCWin32 *win32_gc = (GdkGCWin32 *) gc;
919
  GdkDrawableImplWin32 *impl = NULL;
920
921
  gboolean ok = TRUE;
  int flag;
922

923
  g_assert (win32_gc->hdc == NULL);
924

Tor Lillqvist's avatar
Tor Lillqvist committed
925
926
927
  if (GDK_IS_DRAWABLE_IMPL_WIN32 (drawable))
    impl = GDK_DRAWABLE_IMPL_WIN32(drawable);
  else if (GDK_IS_WINDOW (drawable))
Hans Breuer's avatar
Hans Breuer committed
928
    impl = GDK_DRAWABLE_IMPL_WIN32 ((GDK_WINDOW_OBJECT (drawable))->impl);
Tor Lillqvist's avatar
Tor Lillqvist committed
929
  else if (GDK_IS_PIXMAP (drawable))
Hans Breuer's avatar
Hans Breuer committed
930
    impl = GDK_DRAWABLE_IMPL_WIN32 ((GDK_PIXMAP_OBJECT (drawable))->impl);
931
932
  else
    g_assert_not_reached ();
Tor Lillqvist's avatar
Update.    
Tor Lillqvist committed
933

Tor Lillqvist's avatar
Tor Lillqvist committed
934
935
936
  win32_gc->hwnd = impl->handle;

  if (GDK_IS_PIXMAP_IMPL_WIN32 (impl))
937
    {
938
      if ((win32_gc->hdc = CreateCompatibleDC (NULL)) == NULL)
939
	WIN32_GDI_FAILED ("CreateCompatibleDC"), ok = FALSE;
940

941
      if (ok && (win32_gc->saved_dc = SaveDC (win32_gc->hdc)) == 0)
942
	WIN32_GDI_FAILED ("SaveDC"), ok = FALSE;
943
      
944
      if (ok && SelectObject (win32_gc->hdc, win32_gc->hwnd) == NULL)
945
	WIN32_GDI_FAILED ("SelectObject"), ok = FALSE;
946
947
948
    }
  else
    {
949
      if ((win32_gc->hdc = GetDC (win32_gc->hwnd)) == NULL)
950
	WIN32_GDI_FAILED ("GetDC");
951
      
952
      if (ok && (win32_gc->saved_dc = SaveDC (win32_gc->hdc)) == 0)
953
	WIN32_GDI_FAILED ("SaveDC");
954
955
    }
  
956
  if (ok && (usage & GDK_GC_FOREGROUND))
Tor Lillqvist's avatar
Tor Lillqvist committed
957
    predraw_set_foreground (gc, impl->colormap, &ok);
958

959
  if (ok && (usage & GDK_GC_BACKGROUND))
Tor Lillqvist's avatar
Tor Lillqvist committed
960
    predraw_set_background (gc, impl->colormap, &ok);
961
  
962
  if (ok && (usage & GDK_GC_FONT))
963
    {
964
      if (SetBkMode (win32_gc->hdc, TRANSPARENT) == 0)
965
	WIN32_GDI_FAILED ("SetBkMode"), ok = FALSE;
966
  
967
      if (ok && SetTextAlign (win32_gc->hdc, TA_BASELINE) == GDI_ERROR)
968
	WIN32_GDI_FAILED ("SetTextAlign"), ok = FALSE;
969
    }
970
  
971
972
  if (ok && (win32_gc->values_mask & GDK_GC_FUNCTION))
    if (SetROP2 (win32_gc->hdc, win32_gc->rop2) == 0)