gdkgc-win32.c 32.8 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/* GDK - The GIMP Drawing Kit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * 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
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/*
 * Modified by the GTK+ Team and others 1997-1999.  See the AUTHORS
 * 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 "config.h"

#include <string.h>

#include "gdkgc.h"
#include "gdkfont.h"
#include "gdkpixmap.h"
#include "gdkprivate.h"
Tor Lillqvist's avatar
Tor Lillqvist committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
#include "gdkwin32.h"

static void gdk_win32_gc_destroy    (GdkGC           *gc);
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,
				     gchar            dash_list[],
				     gint             n);

static GdkGCClass gdk_win32_gc_class = {
  gdk_win32_gc_destroy,
  gdk_win32_gc_get_values,
  gdk_win32_gc_set_values,
  gdk_win32_gc_set_dashes
};
54

55 56 57 58 59 60
static void
gdk_win32_gc_values_to_win32values (GdkGCValues    *values,
				    GdkGCValuesMask mask,
				    GdkGCWin32Data *data)
{				    
  char *s = "";
61
  gint sw, sh;
62

63
  GDK_NOTE (MISC, g_print ("{"));
64

65
  if (mask & GDK_GC_FOREGROUND)
66
    {
67 68 69 70
      data->foreground = values->foreground.pixel;
      data->values_mask |= GDK_GC_FOREGROUND;
      GDK_NOTE (MISC, (g_print ("fg=%.06x", data->foreground),
		       s = ","));
71 72
    }
  
73
  if (mask & GDK_GC_BACKGROUND)
74
    {
75 76 77 78
      data->background = values->background.pixel;
      data->values_mask |= GDK_GC_BACKGROUND;
      GDK_NOTE (MISC, (g_print ("%sbg=%.06x", s, data->background),
		       s = ","));
79 80
    }

81 82
  if ((mask & GDK_GC_FONT) && (values->font->type == GDK_FONT_FONT
			       || values->font->type == GDK_FONT_FONTSET))
83
    {
84 85
      if (data->font != NULL)
	gdk_font_unref (data->font);
Tor Lillqvist's avatar
Tor Lillqvist committed
86
      data->font = values->font;
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
      if (data->font != NULL)
	{
	  gchar *xlfd;

	  gdk_font_ref (data->font);
	  data->values_mask |= GDK_GC_FONT;
	  GDK_NOTE (MISC, (xlfd = gdk_font_full_name_get (data->font),
			   g_print ("%sfont=%s", s, xlfd),
			   s = ",",
			   gdk_font_full_name_free (xlfd)));
	}
      else
	{
	  data->values_mask &= ~GDK_GC_FONT;
	  GDK_NOTE (MISC, (g_print ("%sfont=NULL"),
			   s = ","));
	}
104 105
    }

106
  if (mask & GDK_GC_FUNCTION)
107
    {
108 109
      GDK_NOTE (MISC, (g_print ("%srop2=", s),
		       s = ","));
110 111 112
      switch (values->function)
	{
	case GDK_COPY:
113 114 115
	  data->rop2 = R2_COPYPEN;
	  GDK_NOTE (MISC, g_print ("COPYPEN"));
	  break;
116
	case GDK_INVERT:
117 118 119
	  data->rop2 = R2_NOT;
	  GDK_NOTE (MISC, g_print ("NOT"));
	  break;
120
	case GDK_XOR:
121 122 123
	  data->rop2 = R2_XORPEN;
	  GDK_NOTE (MISC, g_print ("XORPEN"));
	  break;
124
	case GDK_CLEAR:
125 126 127
	  data->rop2 = R2_BLACK;
	  GDK_NOTE (MISC, g_print ("BLACK"));
	  break;
128
	case GDK_AND:
129 130 131
	  data->rop2 = R2_MASKPEN;
	  GDK_NOTE (MISC, g_print ("MASKPEN"));
	  break;
132
	case GDK_AND_REVERSE:
133 134 135
	  data->rop2 = R2_MASKPENNOT;
	  GDK_NOTE (MISC, g_print ("MASKPENNOT"));
	  break;
136
	case GDK_AND_INVERT:
137 138 139
	  data->rop2 = R2_MASKNOTPEN;
	  GDK_NOTE (MISC, g_print ("MASKNOTPEN"));
	  break;
140
	case GDK_NOOP:
141 142 143
	  data->rop2 = R2_NOP;
	  GDK_NOTE (MISC, g_print ("NOP"));
	  break;
144
	case GDK_OR:
145 146 147
	  data->rop2 = R2_MERGEPEN;
	  GDK_NOTE (MISC, g_print ("MERGEPEN"));
	  break;
148
	case GDK_EQUIV:
149 150 151
	  data->rop2 = R2_NOTXORPEN;
	  GDK_NOTE (MISC, g_print ("NOTXORPEN"));
	  break;
152
	case GDK_OR_REVERSE:
153 154 155
	  data->rop2 = R2_MERGEPENNOT;
	  GDK_NOTE (MISC, g_print ("MERGEPENNOT"));
	  break;
156
	case GDK_COPY_INVERT:
157 158 159
	  data->rop2 = R2_NOTCOPYPEN;
	  GDK_NOTE (MISC, g_print ("NOTCOPYPEN"));
	  break;
160
	case GDK_OR_INVERT:
161 162 163
	  data->rop2 = R2_MERGENOTPEN;
	  GDK_NOTE (MISC, g_print ("MERGENOTPEN"));
	  break;
164
	case GDK_NAND:
165 166 167
	  data->rop2 = R2_NOTMASKPEN;
	  GDK_NOTE (MISC, g_print ("NOTMASKPEN"));
	  break;
168
	case GDK_SET:
169 170 171
	  data->rop2 = R2_WHITE;
	  GDK_NOTE (MISC, g_print ("WHITE"));
	  break;
172
	}
173
      data->values_mask |= GDK_GC_FUNCTION;
174 175
    }

176
  if (mask & GDK_GC_FILL)
177
    {
Tor Lillqvist's avatar
Tor Lillqvist committed
178
      data->fill_style = values->fill;
179 180 181
      data->values_mask |= GDK_GC_FILL;
      GDK_NOTE (MISC, (g_print ("%sfill=%d", s, data->fill_style),
		       s = ","));
182 183
    }

184
  if (mask & GDK_GC_TILE)
185
    {
186 187
      if (data->tile != NULL)
	gdk_drawable_unref (data->tile);
Tor Lillqvist's avatar
Tor Lillqvist committed
188
      data->tile = values->tile;
189 190 191 192 193 194 195 196 197 198 199 200 201 202
      if (data->tile != NULL)
	{
	  gdk_drawable_ref (data->tile);
	  data->values_mask |= GDK_GC_TILE;
	  GDK_NOTE (MISC, (g_print ("%stile=%#x", s,
				    GDK_DRAWABLE_XID (data->tile)),
			   s = ","));
	}
      else
	{
	  data->values_mask &= ~GDK_GC_TILE;
	  GDK_NOTE (MISC, (g_print ("%stile=NULL", s),
			   s = ","));
	}
203 204
    }

205
  if (mask & GDK_GC_STIPPLE)
206
    {
207 208
      if (data->stipple != NULL)
	gdk_drawable_unref (data->stipple);
Tor Lillqvist's avatar
Tor Lillqvist committed
209
      data->stipple = values->stipple;
210 211
      if (data->stipple != NULL)
	{
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
	  gdk_drawable_get_size (data->stipple, &sw, &sh);

	  if (sw != 8 || sh != 8)
	    {
	      /* 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)
		    {
		      gdk_draw_drawable (bm, gc, data->stipple, 0, 0, i, j, sw, sh);
		      j += sh;
		    }
		  i += sw;
		}
	      data->stipple = bm;
	      gdk_gc_unref (gc);
	    }
	  else
	    gdk_drawable_ref (data->stipple);
242 243 244 245 246 247 248 249 250 251 252
	  data->values_mask |= GDK_GC_STIPPLE;
	  GDK_NOTE (MISC, (g_print ("%sstipple=%#x", s,
				    GDK_DRAWABLE_XID (data->stipple)),
			   s = ","));
	}
      else
	{
	  data->values_mask &= ~GDK_GC_STIPPLE;
	  GDK_NOTE (MISC, (g_print ("%sstipple=NULL", s),
			   s = ","));
	}
253 254
    }

255
  if (mask & GDK_GC_CLIP_MASK)
256
    {
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
      if (data->clip_region != NULL)
	if (!DeleteObject (data->clip_region))
	  WIN32_API_FAILED ("DeleteObject");
      if (values->clip_mask != NULL)
	{
	  data->clip_region =
	    BitmapToRegion ((HBITMAP) GDK_DRAWABLE_XID (values->clip_mask));
	  data->values_mask |= GDK_GC_CLIP_MASK;
	}
      else
	{
	  data->clip_region = NULL;
	  data->values_mask &= ~GDK_GC_CLIP_MASK;
	}
      GDK_NOTE (MISC, (g_print ("%sclip=%#x", s, data->clip_region),
		       s = ","));
273 274
    }

275
  if (mask & GDK_GC_SUBWINDOW)
276
    {
Tor Lillqvist's avatar
Tor Lillqvist committed
277
      data->subwindow_mode = values->subwindow_mode;
278 279 280
      data->values_mask |= GDK_GC_SUBWINDOW;
      GDK_NOTE (MISC, (g_print ("%ssubw=%d", s, data->subwindow_mode),
		       s = ","));
281 282
    }

283
  if (mask & GDK_GC_TS_X_ORIGIN)
284
    {
Tor Lillqvist's avatar
Tor Lillqvist committed
285
      data->ts_x_origin = values->ts_x_origin;
286 287 288
      data->values_mask |= GDK_GC_TS_X_ORIGIN;
      GDK_NOTE (MISC, (g_print ("%sts_x=%d", s, data->ts_x_origin),
		       s = ","));
289 290
    }

291
  if (mask & GDK_GC_TS_Y_ORIGIN)
292
    {
Tor Lillqvist's avatar
Tor Lillqvist committed
293
      data->ts_y_origin = values->ts_y_origin;
294 295 296
      data->values_mask |= GDK_GC_TS_Y_ORIGIN;
      GDK_NOTE (MISC, (g_print ("%sts_y=%d", s, data->ts_y_origin),
		       s = ","));
297 298
    }

299
  if (mask & GDK_GC_CLIP_X_ORIGIN)
300
    {
Tor Lillqvist's avatar
Tor Lillqvist committed
301
      data->clip_x_origin = values->clip_x_origin;
302 303 304
      data->values_mask |= GDK_GC_CLIP_X_ORIGIN;
      GDK_NOTE (MISC, (g_print ("%sclip_x=%d", s, data->clip_x_origin),
		       s = ","));
305 306
    }

307
  if (mask & GDK_GC_CLIP_Y_ORIGIN)
308
    {
Tor Lillqvist's avatar
Tor Lillqvist committed
309
      data->clip_y_origin = values->clip_y_origin; 
310 311 312
      data->values_mask |= GDK_GC_CLIP_Y_ORIGIN;
      GDK_NOTE (MISC, (g_print ("%sclip_y=%d", s, data->clip_y_origin),
		       s = ","));
313 314
   }
 
315
  if (mask & GDK_GC_EXPOSURES)
316
    {
Tor Lillqvist's avatar
Tor Lillqvist committed
317
      data->graphics_exposures = values->graphics_exposures;
318 319 320
      data->values_mask |= GDK_GC_EXPOSURES;
      GDK_NOTE (MISC, (g_print ("%sexp=%d", s, data->graphics_exposures),
		       s = ","));
321 322
    }

323 324 325 326 327 328 329
  if (mask & GDK_GC_LINE_WIDTH)
    {
      data->pen_width = values->line_width;
      data->values_mask |= GDK_GC_LINE_WIDTH;
      GDK_NOTE (MISC, (g_print ("%spw=%d", s, data->pen_width),
		       s = ","));
    }
330

331
  if (mask & GDK_GC_LINE_STYLE)
332
    {
333 334 335 336
      data->pen_style &= ~(PS_STYLE_MASK);
      GDK_NOTE (MISC, (g_print ("%sps|=", s),
		       s = ","));
      switch (values->line_style)
337
	{
338 339 340 341 342 343 344 345 346
	case GDK_LINE_SOLID:
	  GDK_NOTE (MISC, g_print ("LINE_SOLID"));
	  data->pen_style |= PS_SOLID;
	  break;
	case GDK_LINE_ON_OFF_DASH:
	case GDK_LINE_DOUBLE_DASH: /* ??? */
	  GDK_NOTE (MISC, g_print ("DASH"));
	  data->pen_style |= PS_DASH;
	  break;
347
	}
348
      data->values_mask |= GDK_GC_LINE_STYLE;
349 350
    }

351
  if (mask & GDK_GC_CAP_STYLE)
352
    {
353 354 355
      data->pen_style &= ~(PS_ENDCAP_MASK);
      GDK_NOTE (MISC, (g_print ("%sps|=", s),
		       s = ","));
356 357 358 359
      switch (values->cap_style)
	{
	case GDK_CAP_NOT_LAST:	/* ??? */
	case GDK_CAP_BUTT:
360 361 362
	  GDK_NOTE (MISC, g_print ("ENDCAP_FLAT"));
	  data->pen_style |= PS_ENDCAP_FLAT;
	  break;
363
	case GDK_CAP_ROUND:
364 365 366
	  GDK_NOTE (MISC, g_print ("ENDCAP_ROUND"));
	  data->pen_style |= PS_ENDCAP_ROUND;
	  break;
367
	case GDK_CAP_PROJECTING:
368 369 370
	  GDK_NOTE (MISC, g_print ("ENDCAP_SQUARE"));
	  data->pen_style |= PS_ENDCAP_SQUARE;
	  break;
371
	}
372
      data->values_mask |= GDK_GC_CAP_STYLE;
373 374
    }

375
  if (mask & GDK_GC_JOIN_STYLE)
376
    {
377 378 379
      data->pen_style &= ~(PS_JOIN_MASK);
      GDK_NOTE (MISC, (g_print ("%sps|=", s),
		       s = ","));
380 381 382
      switch (values->join_style)
	{
	case GDK_JOIN_MITER:
383
	  GDK_NOTE (MISC, g_print ("JOIN_MITER"));
Tor Lillqvist's avatar
Tor Lillqvist committed
384
	  data->pen_style |= PS_JOIN_MITER;
385 386
	  break;
	case GDK_JOIN_ROUND:
387
	  GDK_NOTE (MISC, g_print ("JOIN_ROUND"));
Tor Lillqvist's avatar
Tor Lillqvist committed
388
	  data->pen_style |= PS_JOIN_ROUND;
389 390
	  break;
	case GDK_JOIN_BEVEL:
391
	  GDK_NOTE (MISC, g_print ("JOIN_BEVEL"));
Tor Lillqvist's avatar
Tor Lillqvist committed
392
	  data->pen_style |= PS_JOIN_BEVEL;
393 394
	  break;
	}
395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
      data->values_mask |= GDK_GC_JOIN_STYLE;
    }
  GDK_NOTE (MISC, g_print ("}\n"));
}

GdkGC*
_gdk_win32_gc_new (GdkDrawable	  *drawable,
		   GdkGCValues	  *values,
		   GdkGCValuesMask mask)
{
  GdkGC *gc;
  GdkGCPrivate *private;
  GdkGCWin32Data *data;
  static GdkColor black;
  static GdkColor white;
  static gboolean beenhere = FALSE;

  if (!beenhere)
    {
      gdk_color_black (gdk_colormap_get_system (), &black);
      gdk_color_white (gdk_colormap_get_system (), &white);
      beenhere = TRUE;
417 418
    }

419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
  gc = gdk_gc_alloc ();
  private = (GdkGCPrivate *)gc;

  private->klass = &gdk_win32_gc_class;
  private->klass_data = data = g_new (GdkGCWin32Data, 1);
    
  data->foreground = black.pixel;
  data->background = white.pixel;
  data->font = NULL;
  data->rop2 = R2_COPYPEN;
  data->fill_style = GDK_SOLID;
  data->tile = NULL;
  data->stipple = NULL;
  data->clip_region = NULL;
  data->ts_x_origin = data->ts_y_origin =
    data->clip_x_origin = data->clip_y_origin = 0;
  data->pen_style = PS_GEOMETRIC;
  data->pen_width = 1;

  data->values_mask = GDK_GC_FUNCTION | GDK_GC_FILL;

  GDK_NOTE (MISC, g_print ("_gdk_win32_gc_new: "));
  gdk_win32_gc_values_to_win32values (values, mask, data);

Tor Lillqvist's avatar
Tor Lillqvist committed
443 444
  data->hwnd = NULL;
  data->xgc = NULL;
445

446
  GDK_NOTE (MISC, g_print (" = %p\n", gc));
447 448 449 450

  return gc;
}

Tor Lillqvist's avatar
Tor Lillqvist committed
451 452
static void
gdk_win32_gc_destroy (GdkGC *gc)
453
{
Tor Lillqvist's avatar
Tor Lillqvist committed
454
  GdkGCWin32Data *data = GDK_GC_WIN32DATA (gc);
455

Tor Lillqvist's avatar
Tor Lillqvist committed
456 457
  if (data->values_mask & GDK_GC_FONT)
    gdk_font_unref (data->font);
458
  
Tor Lillqvist's avatar
Tor Lillqvist committed
459
  if (data->values_mask & GDK_GC_TILE)
460
    gdk_drawable_unref (data->tile);
461
  
Tor Lillqvist's avatar
Tor Lillqvist committed
462
  if (data->values_mask & GDK_GC_STIPPLE)
463
    gdk_drawable_unref (data->stipple);
Tor Lillqvist's avatar
Tor Lillqvist committed
464 465 466
  
  if (data->values_mask & GDK_GC_CLIP_MASK)
    DeleteObject (data->clip_region);
467

Tor Lillqvist's avatar
Tor Lillqvist committed
468
  g_free (GDK_GC_WIN32DATA (gc));
469 470
}

Tor Lillqvist's avatar
Tor Lillqvist committed
471 472 473
static void
gdk_win32_gc_get_values (GdkGC       *gc,
			 GdkGCValues *values)
474
{
Tor Lillqvist's avatar
Tor Lillqvist committed
475
  GdkGCWin32Data *data = GDK_GC_WIN32DATA (gc);
476

477 478
  values->foreground.pixel = data->foreground;
  values->background.pixel = data->background;
Tor Lillqvist's avatar
Tor Lillqvist committed
479
  values->font = data->font;
480

Tor Lillqvist's avatar
Tor Lillqvist committed
481
  switch (data->rop2)
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
    {
    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;
    case R2_WHITE:
      values->function = GDK_SET; break;
    }

Tor Lillqvist's avatar
Tor Lillqvist committed
515
  values->fill = data->fill_style;
516

Tor Lillqvist's avatar
Tor Lillqvist committed
517 518
  values->tile = data->tile;
  values->stipple = data->stipple;
519

Tor Lillqvist's avatar
Tor Lillqvist committed
520
  if (data->clip_region != NULL)
521 522 523 524 525 526 527
    {
      RECT rect;
      HBRUSH hbr;
      HDC hdc;
      HGDIOBJ oldbitmap;
      GdkPixmap *pixmap;

Tor Lillqvist's avatar
Tor Lillqvist committed
528
      GetRgnBox (data->clip_region, &rect);
529 530 531 532 533
      pixmap =
	gdk_pixmap_new (NULL, rect.right - rect.left, rect.bottom - rect.top,
			1);
      hbr = GetStockObject (WHITE_BRUSH);
      if ((hdc = CreateCompatibleDC (NULL)) == NULL)
534
	WIN32_API_FAILED ("CreateCompatibleDC");
535 536
      if ((oldbitmap =
	   SelectObject (hdc, GDK_DRAWABLE_XID (pixmap))) == NULL)
537
	WIN32_API_FAILED ("SelectObject #1");
538 539
      hbr = GetStockObject (BLACK_BRUSH);
      if (!FillRect (hdc, &rect, hbr))
540
	WIN32_API_FAILED ("FillRect");
541
      hbr = GetStockObject (WHITE_BRUSH);
Tor Lillqvist's avatar
Tor Lillqvist committed
542
      if (!FillRgn (hdc, data->clip_region, hbr))
543
	WIN32_API_FAILED ("FillRgn");
544
      if (SelectObject (hdc, oldbitmap) == NULL)
545
	WIN32_API_FAILED ("SelectObject #2");
546 547 548 549 550
      DeleteDC (hdc);
      values->clip_mask = pixmap;
    }
  else
    values->clip_mask = NULL;
Tor Lillqvist's avatar
Tor Lillqvist committed
551 552 553 554 555 556 557
  values->subwindow_mode = data->subwindow_mode;
  values->ts_x_origin = data->ts_x_origin;
  values->ts_y_origin = data->ts_y_origin;
  values->clip_x_origin = data->clip_x_origin;
  values->clip_y_origin = data->clip_y_origin;
  values->graphics_exposures = data->graphics_exposures;
  values->line_width = data->pen_width;
558
  
Tor Lillqvist's avatar
Tor Lillqvist committed
559
  if (data->pen_style & PS_SOLID)
560
    values->line_style = GDK_LINE_SOLID;
Tor Lillqvist's avatar
Tor Lillqvist committed
561
  else if (data->pen_style & PS_DASH)
562 563 564 565 566
    values->line_style = GDK_LINE_ON_OFF_DASH;
  else
    values->line_style = GDK_LINE_SOLID;

  /* PS_ENDCAP_ROUND is zero */
Tor Lillqvist's avatar
Tor Lillqvist committed
567
  if (data->pen_style & PS_ENDCAP_FLAT)
568
    values->cap_style = GDK_CAP_BUTT;
Tor Lillqvist's avatar
Tor Lillqvist committed
569
  else if (data->pen_style & PS_ENDCAP_SQUARE)
570 571 572 573 574
    values->cap_style = GDK_CAP_PROJECTING;
  else
    values->cap_style = GDK_CAP_ROUND;
    
  /* PS_JOIN_ROUND is zero */
Tor Lillqvist's avatar
Tor Lillqvist committed
575
  if (data->pen_style & PS_JOIN_MITER)
576
    values->join_style = GDK_JOIN_MITER;
Tor Lillqvist's avatar
Tor Lillqvist committed
577
  else if (data->pen_style & PS_JOIN_BEVEL)
578 579 580 581 582
    values->join_style = GDK_JOIN_BEVEL;
  else
    values->join_style = GDK_JOIN_ROUND;
}

Tor Lillqvist's avatar
Tor Lillqvist committed
583 584 585
static void
gdk_win32_gc_set_values (GdkGC           *gc,
			 GdkGCValues     *values,
586
			 GdkGCValuesMask  mask)
587
{
Tor Lillqvist's avatar
Tor Lillqvist committed
588
  GdkGCWin32Data *data = GDK_GC_WIN32DATA (gc);
589

590
  GDK_NOTE (MISC, g_print ("gdk_win32_gc_set_values: "));
591

592
  gdk_win32_gc_values_to_win32values (values, mask, data);
Tor Lillqvist's avatar
Tor Lillqvist committed
593 594 595 596 597 598 599 600 601 602 603 604 605 606
}

static void
gdk_win32_gc_set_dashes (GdkGC *gc,
			 gint	  dash_offset,
			 gchar  dash_list[],
			 gint   n)
{
  GdkGCWin32Data *data = GDK_GC_WIN32DATA (gc);

  /* XXX ??? */

  data->pen_style &= ~(PS_STYLE_MASK);
  data->pen_style |= PS_DASH;
607 608 609 610 611 612
}

void
gdk_gc_set_clip_rectangle (GdkGC	*gc,
			   GdkRectangle *rectangle)
{
Tor Lillqvist's avatar
Tor Lillqvist committed
613
  GdkGCWin32Data *data;
614 615 616
   
  g_return_if_fail (gc != NULL);

Tor Lillqvist's avatar
Tor Lillqvist committed
617
  data = GDK_GC_WIN32DATA (gc);
618

Tor Lillqvist's avatar
Tor Lillqvist committed
619 620
  if (data->clip_region != NULL)
    if (!DeleteObject (data->clip_region))
621
      WIN32_API_FAILED ("DeleteObject");
622 623 624 625
  if (rectangle)
    {
      GDK_NOTE (MISC,
		g_print ("gdk_gc_set_clip_rectangle: (%d) %dx%d@+%d+%d\n",
Tor Lillqvist's avatar
Tor Lillqvist committed
626
			 data,
627 628
			 rectangle->width, rectangle->height,
			 rectangle->x, rectangle->y));
Tor Lillqvist's avatar
Tor Lillqvist committed
629
      if ((data->clip_region =
630 631 632
	   CreateRectRgn (rectangle->x, rectangle->y,
			  rectangle->x + rectangle->width,
			  rectangle->y + rectangle->height)) == NULL)
633
	WIN32_API_FAILED ("CreateRectRgn");
634

Tor Lillqvist's avatar
Tor Lillqvist committed
635
      data->values_mask |= GDK_GC_CLIP_MASK;
636 637 638
    }
  else
    {
Tor Lillqvist's avatar
Tor Lillqvist committed
639 640 641 642
      GDK_NOTE (MISC, g_print ("gdk_gc_set_clip_rectangle: (%d) NULL\n",
			       data));
      data->clip_region = NULL;
      data->values_mask &= ~GDK_GC_CLIP_MASK;
643
    }
Tor Lillqvist's avatar
Tor Lillqvist committed
644
    data->values_mask &= ~(GDK_GC_CLIP_X_ORIGIN | GDK_GC_CLIP_Y_ORIGIN);
645 646 647 648 649 650
} 

void
gdk_gc_set_clip_region (GdkGC		 *gc,
			GdkRegion	 *region)
{
Tor Lillqvist's avatar
Tor Lillqvist committed
651
  GdkGCWin32Data *data;
652 653 654

  g_return_if_fail (gc != NULL);

Tor Lillqvist's avatar
Tor Lillqvist committed
655
  data = GDK_GC_WIN32DATA (gc);
656 657

  GDK_NOTE (MISC, g_print ("gdk_gc_set_clip_region: (%d) %s\n",
Tor Lillqvist's avatar
Tor Lillqvist committed
658
			   data, (region != NULL ? "xxx" : "None")));
659

Tor Lillqvist's avatar
Tor Lillqvist committed
660 661
  if (data->clip_region != NULL)
    if (!DeleteObject (data->clip_region))
662
      WIN32_API_FAILED ("DeleteObject");
663 664 665 666 667
  if (region)
    {
      GdkRegionPrivate *region_private;

      region_private = (GdkRegionPrivate*) region;
Tor Lillqvist's avatar
Tor Lillqvist committed
668 669 670
      data->clip_region = CreateRectRgn (1, 1, 0, 0);
      CombineRgn (data->clip_region, region_private->xregion, NULL, RGN_COPY);
      data->values_mask |= GDK_GC_CLIP_MASK;
671 672 673
    }
  else
    {
Tor Lillqvist's avatar
Tor Lillqvist committed
674 675
      data->clip_region = NULL;
      data->values_mask &= ~GDK_GC_CLIP_MASK;
676 677 678 679 680 681
    }
}

void
gdk_gc_copy (GdkGC *dst_gc, GdkGC *src_gc)
{
Tor Lillqvist's avatar
Tor Lillqvist committed
682 683
  GdkGCWin32Data *dst_data = GDK_GC_WIN32DATA (dst_gc);
  GdkGCWin32Data *src_data = GDK_GC_WIN32DATA (src_gc);
684 685
  DWORD nbytes;
  LPRGNDATA rgn;
Tor Lillqvist's avatar
Tor Lillqvist committed
686

687
  if (dst_data->font != NULL)
Tor Lillqvist's avatar
Tor Lillqvist committed
688
    gdk_font_unref (dst_data->font);
689
  if (dst_data->tile != NULL)
690
    gdk_drawable_unref (dst_data->tile);
691
  if (dst_data->stipple != NULL)
692
    gdk_drawable_unref (dst_data->stipple);
693 694 695
  if (dst_data->clip_region != NULL)
    if (!DeleteObject (dst_data->clip_region))
      WIN32_API_FAILED ("DeleteObject");
Tor Lillqvist's avatar
Tor Lillqvist committed
696 697
  
  *dst_data = *src_data;
698 699 700 701 702 703 704 705 706 707

  if (dst_data->clip_region != NULL)
    {
      nbytes = GetRegionData (dst_data->clip_region, 0, NULL);
      rgn = g_malloc (nbytes);
      GetRegionData (dst_data->clip_region, nbytes, rgn);
      if ((dst_data->clip_region = ExtCreateRegion (NULL, nbytes, rgn)) == NULL)
	WIN32_API_FAILED ("ExtCreateRegion");
      g_free (rgn);
    }
Tor Lillqvist's avatar
Tor Lillqvist committed
708
  
709
  if (dst_data->font != NULL)
Tor Lillqvist's avatar
Tor Lillqvist committed
710
    gdk_font_ref (dst_data->font);
711
  if (dst_data->tile != NULL)
712
    gdk_drawable_ref (dst_data->tile);
713
  if (dst_data->stipple != NULL)
714
    gdk_drawable_ref (dst_data->stipple);
715 716
}

717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740
static guint bitmask[9] = { 0, 1, 3, 7, 15, 31, 63, 127, 255 };

COLORREF
gdk_colormap_color (GdkColormapPrivateWin32 *colormap_private,
		    gulong                   pixel)
{
  GdkVisual *visual;
  guchar r, g, b;

  if (colormap_private == NULL || colormap_private->xcolormap->rc_palette)
    return PALETTEINDEX (pixel);
  else
    {
      visual = colormap_private->base.visual;
      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);
    }
}
741 742 743 744

static void
predraw_set_foreground (GdkGCWin32Data          *data,
			GdkColormapPrivateWin32 *colormap_private)
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775
{
  COLORREF fg;
  LOGBRUSH logbrush;
  HPEN hpen;
  HBRUSH hbr;

  if (colormap_private == NULL)
    {
      /* A 1 bit deep bitmap */
      struct
      {
	WORD palVersion;
	WORD palNumEntries;
	PALETTEENTRY palPalEntry[2];
      } logpal;
      static HPALETTE hpal = NULL;

      if (hpal == NULL)
	{
	  /* Create a b&w palette */
	  logpal.palVersion = 0x300;
	  logpal.palNumEntries = 2;
	  logpal.palPalEntry[0].peRed = 
	    logpal.palPalEntry[0].peGreen = 
	    logpal.palPalEntry[0].peBlue = 0x00;
	  logpal.palPalEntry[0].peFlags = 0x00;
	  logpal.palPalEntry[1].peRed = 
	    logpal.palPalEntry[1].peGreen = 
	    logpal.palPalEntry[1].peBlue = 0xFF;
	  logpal.palPalEntry[1].peFlags = 0x00;
	  if ((hpal = CreatePalette ((LOGPALETTE *) &logpal)) == NULL)
776
	    WIN32_API_FAILED ("CreatePalette");
777
	}
Tor Lillqvist's avatar
Tor Lillqvist committed
778 779
      SelectPalette (data->xgc, hpal, FALSE);
      RealizePalette (data->xgc);
780
      fg = PALETTEINDEX (data->foreground);
781
    }
782
  else if (colormap_private->xcolormap->rc_palette)
783 784
    {
      int k;
785 786 787
      if (SelectPalette (data->xgc, colormap_private->xcolormap->palette,
			 FALSE) == NULL)
	WIN32_API_FAILED ("SelectPalette");
788 789
      if (TRUE || colormap_private->xcolormap->stale)
	{
Tor Lillqvist's avatar
Tor Lillqvist committed
790
	  if ((k = RealizePalette (data->xgc)) == GDI_ERROR)
791
	    WIN32_API_FAILED ("RealizePalette");
792 793 794 795
	  colormap_private->xcolormap->stale = FALSE;
	}
#if 0
      g_print ("Selected palette %#x for gc %#x, realized %d colors\n",
Tor Lillqvist's avatar
Tor Lillqvist committed
796
	       colormap_private->xcolormap->palette, data->xgc, k);
797 798
#endif
    }
799

800
  fg = gdk_colormap_color (colormap_private, data->foreground);
801 802 803 804 805 806

  if (SetTextColor (data->xgc, fg) == CLR_INVALID)
    WIN32_API_FAILED ("SetTextColor");

  /* Create and select pen and brush. */

807 808
  logbrush.lbStyle = BS_SOLID;
  logbrush.lbColor = fg;
809

Tor Lillqvist's avatar
Tor Lillqvist committed
810
  if ((hpen = ExtCreatePen (data->pen_style, data->pen_width,
811
			    &logbrush, 0, NULL)) == NULL)
812
    WIN32_API_FAILED ("ExtCreatePen");
813
  
Tor Lillqvist's avatar
Tor Lillqvist committed
814
  if (SelectObject (data->xgc, hpen) == NULL)
815
    WIN32_API_FAILED ("SelectObject #2");
816

Tor Lillqvist's avatar
Tor Lillqvist committed
817
  switch (data->fill_style)
818
    {
819 820 821
    case GDK_OPAQUE_STIPPLED:
      if ((hbr = CreatePatternBrush (GDK_DRAWABLE_XID (data->stipple))) == NULL)
	WIN32_API_FAILED ("CreatePatternBrush");
822
	
823 824
      SetBrushOrgEx(data->xgc, data->ts_x_origin,
		    data->ts_y_origin, NULL);
825

826
      break;
827

828 829 830
    case GDK_SOLID:
    default:
      if ((hbr = CreateSolidBrush (fg)) == NULL)
831
	WIN32_API_FAILED ("CreateSolidBrush");
832 833
      break;
  }
Tor Lillqvist's avatar
Tor Lillqvist committed
834
  if (SelectObject (data->xgc, hbr) == NULL)
835 836
    WIN32_API_FAILED ("SelectObject #3");
}  
837

838 839 840 841
void
predraw_set_background (GdkGCWin32Data          *data,
			GdkColormapPrivateWin32 *colormap_private)
{
842
  COLORREF bg = gdk_colormap_color (colormap_private, data->background);
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879

  if (SetBkColor (data->xgc, bg) == CLR_INVALID)
    WIN32_API_FAILED ("SetBkColor");
}

HDC
gdk_gc_predraw (GdkDrawable    *drawable,
		GdkGCPrivate   *gc_private,
		GdkGCValuesMask usage)
{
  GdkDrawablePrivate *drawable_private = (GdkDrawablePrivate *) drawable;
  GdkColormapPrivateWin32 *colormap_private =
    (GdkColormapPrivateWin32 *) drawable_private->colormap;
  GdkGCWin32Data *data = GDK_GC_WIN32DATA (gc_private);

  g_assert (data->xgc == NULL);

  data->hwnd = GDK_DRAWABLE_XID (drawable);
  
  if (GDK_DRAWABLE_TYPE (drawable) == GDK_DRAWABLE_PIXMAP)
    {
      if ((data->xgc = CreateCompatibleDC (NULL)) == NULL)
	WIN32_API_FAILED ("CreateCompatibleDC");

      if ((data->saved_dc = SaveDC (data->xgc)) == 0)
	WIN32_API_FAILED ("SaveDC #1");
      
      if (SelectObject (data->xgc, data->hwnd) == NULL)
	WIN32_API_FAILED ("SelectObject #1");
    }
  else
    {
      if ((data->xgc = GetDC (data->hwnd)) == NULL)
	WIN32_API_FAILED ("GetDC");
      
      if ((data->saved_dc = SaveDC (data->xgc)) == 0)
	WIN32_API_FAILED ("SaveDC #2");
880 881
    }
  
882 883 884 885 886 887 888 889 890 891
  if (usage & GDK_GC_FOREGROUND)
    predraw_set_foreground (data, colormap_private);

  if ((usage & GDK_GC_BACKGROUND) && (data->values_mask & GDK_GC_BACKGROUND))
    predraw_set_background (data, colormap_private);
  
  if (usage & GDK_GC_FONT)
    {
      if (SetBkMode (data->xgc, TRANSPARENT) == 0)
	WIN32_API_FAILED ("SetBkMode");
892
  
893 894 895
      if (SetTextAlign (data->xgc, TA_BASELINE) == GDI_ERROR)
	WIN32_API_FAILED ("SetTextAlign");
    }
896
  
Tor Lillqvist's avatar
Tor Lillqvist committed
897 898
  if (data->values_mask & GDK_GC_FUNCTION)
    if (SetROP2 (data->xgc, data->rop2) == 0)
899
      WIN32_API_FAILED ("SetROP2");
900

901
  if ((data->values_mask & GDK_GC_CLIP_MASK)
Tor Lillqvist's avatar
Tor Lillqvist committed
902
      && data->clip_region != NULL)
903
    {
Tor Lillqvist's avatar
Tor Lillqvist committed
904 905 906 907
      if (data->values_mask & (GDK_GC_CLIP_X_ORIGIN | GDK_GC_CLIP_Y_ORIGIN))
	OffsetRgn (data->clip_region,
		   data->clip_x_origin, data->clip_y_origin);
      SelectClipRgn (data->xgc, data->clip_region);
908 909
    }

Tor Lillqvist's avatar
Tor Lillqvist committed
910
  return data->xgc;
911 912 913
}

void
914 915 916
gdk_gc_postdraw (GdkDrawable    *drawable,
		 GdkGCPrivate   *gc_private,
		 GdkGCValuesMask usage)
917
{
918
  GdkDrawablePrivate *drawable_private = (GdkDrawablePrivate *) drawable;
Tor Lillqvist's avatar
Tor Lillqvist committed
919 920 921
  GdkColormapPrivateWin32 *colormap_private =
    (GdkColormapPrivateWin32 *) drawable_private->colormap;
  GdkGCWin32Data *data = GDK_GC_WIN32DATA (gc_private);
922 923
  HGDIOBJ hpen = NULL;
  HGDIOBJ hbr = NULL;
924

925 926 927 928
  if (usage & GDK_GC_FOREGROUND)
    {
      if ((hpen = GetCurrentObject (data->xgc, OBJ_PEN)) == NULL)
	WIN32_API_FAILED ("GetCurrentObject #1");
929

930 931 932
      if ((hbr = GetCurrentObject (data->xgc, OBJ_BRUSH)) == NULL)
	WIN32_API_FAILED ("GetCurrentObject #2");
    }
933

Tor Lillqvist's avatar
Tor Lillqvist committed
934
  if (!RestoreDC (data->xgc, data->saved_dc))
935
    WIN32_API_FAILED ("RestoreDC");
936 937 938 939 940
#if 0
  if (colormap_private != NULL
      && colormap_private->xcolormap->rc_palette
      && colormap_private->xcolormap->stale)
    {
Tor Lillqvist's avatar
Tor Lillqvist committed
941
      SelectPalette (data->xgc, GetStockObject (DEFAULT_PALETTE), FALSE);
942
      if (!UnrealizeObject (colormap_private->xcolormap->palette))
943
	WIN32_API_FAILED ("UnrealizeObject");
944 945
    }
#endif
946
  if (GDK_DRAWABLE_TYPE (drawable) == GDK_DRAWABLE_PIXMAP)
947
    {
Tor Lillqvist's avatar
Tor Lillqvist committed
948
      if (!DeleteDC (