File: gimpcanvas.c

package info (click to toggle)
gimp 2.2.13-1etch4
  • links: PTS
  • area: main
  • in suites: etch
  • size: 94,832 kB
  • ctags: 47,113
  • sloc: ansic: 524,858; xml: 36,798; lisp: 9,870; sh: 9,409; makefile: 7,923; python: 2,674; perl: 2,589; yacc: 520; lex: 334
file content (793 lines) | stat: -rw-r--r-- 22,696 bytes parent folder | download | duplicates (3)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
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
741
742
743
744
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
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <gtk/gtk.h>

#include "display-types.h"

#include "widgets/gimpwidgets-utils.h"

#include "gimpcanvas.h"


/*  local function prototypes  */

static void     gimp_canvas_class_init (GimpCanvasClass  *klass);
static void     gimp_canvas_init       (GimpCanvas       *gdisp);
static void     gimp_canvas_realize    (GtkWidget        *widget);
static void     gimp_canvas_unrealize  (GtkWidget        *widget);
static GdkGC  * gimp_canvas_gc_new     (GimpCanvas       *canvas,
                                        GimpCanvasStyle   style);


static GtkDrawingAreaClass *parent_class = NULL;

static const guchar stipples[GIMP_CANVAS_NUM_STIPPLES][8] =
{
  {
    0xF0,    /*  ####----  */
    0xE1,    /*  ###----#  */
    0xC3,    /*  ##----##  */
    0x87,    /*  #----###  */
    0x0F,    /*  ----####  */
    0x1E,    /*  ---####-  */
    0x3C,    /*  --####--  */
    0x78,    /*  -####---  */
  },
  {
    0xE1,    /*  ###----#  */
    0xC3,    /*  ##----##  */
    0x87,    /*  #----###  */
    0x0F,    /*  ----####  */
    0x1E,    /*  ---####-  */
    0x3C,    /*  --####--  */
    0x78,    /*  -####---  */
    0xF0,    /*  ####----  */
  },
  {
    0xC3,    /*  ##----##  */
    0x87,    /*  #----###  */
    0x0F,    /*  ----####  */
    0x1E,    /*  ---####-  */
    0x3C,    /*  --####--  */
    0x78,    /*  -####---  */
    0xF0,    /*  ####----  */
    0xE1,    /*  ###----#  */
  },
  {
    0x87,    /*  #----###  */
    0x0F,    /*  ----####  */
    0x1E,    /*  ---####-  */
    0x3C,    /*  --####--  */
    0x78,    /*  -####---  */
    0xF0,    /*  ####----  */
    0xE1,    /*  ###----#  */
    0xC3,    /*  ##----##  */
  },
  {
    0x0F,    /*  ----####  */
    0x1E,    /*  ---####-  */
    0x3C,    /*  --####--  */
    0x78,    /*  -####---  */
    0xF0,    /*  ####----  */
    0xE1,    /*  ###----#  */
    0xC3,    /*  ##----##  */
    0x87,    /*  #----###  */
  },
  {
    0x1E,    /*  ---####-  */
    0x3C,    /*  --####--  */
    0x78,    /*  -####---  */
    0xF0,    /*  ####----  */
    0xE1,    /*  ###----#  */
    0xC3,    /*  ##----##  */
    0x87,    /*  #----###  */
    0x0F,    /*  ----####  */
  },
  {
    0x3C,    /*  --####--  */
    0x78,    /*  -####---  */
    0xF0,    /*  ####----  */
    0xE1,    /*  ###----#  */
    0xC3,    /*  ##----##  */
    0x87,    /*  #----###  */
    0x0F,    /*  ----####  */
    0x1E,    /*  ---####-  */
  },
  {
    0x78,    /*  -####---  */
    0xF0,    /*  ####----  */
    0xE1,    /*  ###----#  */
    0xC3,    /*  ##----##  */
    0x87,    /*  #----###  */
    0x0F,    /*  ----####  */
    0x1E,    /*  ---####-  */
    0x3C,    /*  --####--  */
  },
};


GType
gimp_canvas_get_type (void)
{
  static GType canvas_type = 0;

  if (! canvas_type)
    {
      static const GTypeInfo canvas_info =
      {
        sizeof (GimpCanvasClass),
        (GBaseInitFunc) NULL,
        (GBaseFinalizeFunc) NULL,
        (GClassInitFunc) gimp_canvas_class_init,
        NULL,           /* class_finalize */
        NULL,           /* class_data     */
        sizeof (GimpCanvas),
        0,              /* n_preallocs    */
        (GInstanceInitFunc) gimp_canvas_init,
      };

      canvas_type = g_type_register_static (GTK_TYPE_DRAWING_AREA,
                                            "GimpCanvas",
                                            &canvas_info, 0);
    }

  return canvas_type;
}

static void
gimp_canvas_class_init (GimpCanvasClass *klass)
{
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  parent_class = g_type_class_peek_parent (klass);

  widget_class->realize   = gimp_canvas_realize;
  widget_class->unrealize = gimp_canvas_unrealize;
}

static void
gimp_canvas_init (GimpCanvas *canvas)
{
  gint i;

  for (i = 0; i < GIMP_CANVAS_NUM_STYLES; i++)
    canvas->gc[i] = NULL;

  for (i = 0; i < GIMP_CANVAS_NUM_STIPPLES; i++)
    canvas->stipple[i] = NULL;
}

static void
gimp_canvas_realize (GtkWidget *widget)
{
  GimpCanvas *canvas = GIMP_CANVAS (widget);

  GTK_WIDGET_CLASS (parent_class)->realize (widget);

  canvas->stipple[0] =
    gdk_bitmap_create_from_data (widget->window,
                                 (const gchar *) stipples[0], 8, 8);
}

static void
gimp_canvas_unrealize (GtkWidget *widget)
{
  GimpCanvas *canvas = GIMP_CANVAS (widget);
  gint        i;

  for (i = 0; i < GIMP_CANVAS_NUM_STYLES; i++)
    {
      if (canvas->gc[i])
        {
          g_object_unref (canvas->gc[i]);
          canvas->gc[i] = NULL;
        }
    }

  for (i = 0; i < GIMP_CANVAS_NUM_STIPPLES; i++)
    {
      if (canvas->stipple[i])
        {
          g_object_unref (canvas->stipple[i]);
          canvas->stipple[i] = NULL;
        }
    }

  GTK_WIDGET_CLASS (parent_class)->unrealize (widget);
}

static GdkGC *
gimp_canvas_gc_new (GimpCanvas      *canvas,
                    GimpCanvasStyle  style)
{
  GdkGC           *gc;
  GdkGCValues      values;
  GdkGCValuesMask  mask = 0;
  GdkColor         fg;
  GdkColor         bg;

  if (! GTK_WIDGET_REALIZED (canvas))
    return NULL;

  switch (style)
    {
    case GIMP_CANVAS_STYLE_BLACK:
    case GIMP_CANVAS_STYLE_WHITE:
      break;

    case GIMP_CANVAS_STYLE_RENDER:
      mask |= GDK_GC_EXPOSURES;
      values.graphics_exposures = TRUE;
      break;

    case GIMP_CANVAS_STYLE_XOR_DASHED:
      mask |= GDK_GC_LINE_STYLE;
      values.line_style = GDK_LINE_ON_OFF_DASH;
      /*  fallthrough  */

    case GIMP_CANVAS_STYLE_XOR:
      mask |= GDK_GC_FUNCTION | GDK_GC_CAP_STYLE | GDK_GC_JOIN_STYLE;
      values.function   = GDK_INVERT;
      values.cap_style  = GDK_CAP_NOT_LAST;
      values.join_style = GDK_JOIN_MITER;
      break;

    case GIMP_CANVAS_STYLE_SELECTION_IN:
    case GIMP_CANVAS_STYLE_SELECTION_OUT:
    case GIMP_CANVAS_STYLE_LAYER_BOUNDARY:
    case GIMP_CANVAS_STYLE_GUIDE_NORMAL:
    case GIMP_CANVAS_STYLE_GUIDE_ACTIVE:
      mask |= GDK_GC_CAP_STYLE | GDK_GC_FILL | GDK_GC_STIPPLE;
      values.cap_style = GDK_CAP_NOT_LAST;
      values.fill      = GDK_OPAQUE_STIPPLED;
      values.stipple   = canvas->stipple[0];
      break;

    case GIMP_CANVAS_STYLE_CUSTOM:
    default:
      return NULL;
    }

  gc = gdk_gc_new_with_values (GTK_WIDGET (canvas)->window, &values, mask);

  switch (style)
    {
    default:
      return gc;

    case GIMP_CANVAS_STYLE_WHITE:
      fg.red   = 0xffff;
      fg.green = 0xffff;
      fg.blue  = 0xffff;

      bg.red   = 0x0;
      bg.green = 0x0;
      bg.blue  = 0x0;
      break;

    case GIMP_CANVAS_STYLE_BLACK:
    case GIMP_CANVAS_STYLE_SELECTION_IN:
      fg.red   = 0x0;
      fg.green = 0x0;
      fg.blue  = 0x0;

      bg.red   = 0xffff;
      bg.green = 0xffff;
      bg.blue  = 0xffff;
      break;

    case GIMP_CANVAS_STYLE_SELECTION_OUT:
      fg.red   = 0xffff;
      fg.green = 0xffff;
      fg.blue  = 0xffff;

      bg.red   = 0x7f7f;
      bg.green = 0x7f7f;
      bg.blue  = 0x7f7f;
      break;

    case GIMP_CANVAS_STYLE_LAYER_BOUNDARY:
      fg.red   = 0x0;
      fg.green = 0x0;
      fg.blue  = 0x0;

      bg.red   = 0xffff;
      bg.green = 0xffff;
      bg.blue  = 0x0;
      break;

    case GIMP_CANVAS_STYLE_GUIDE_NORMAL:
      fg.red   = 0x0;
      fg.green = 0x0;
      fg.blue  = 0x0;

      bg.red   = 0x0;
      bg.green = 0x7f7f;
      bg.blue  = 0xffff;
      break;

    case GIMP_CANVAS_STYLE_GUIDE_ACTIVE:
      fg.red   = 0x0;
      fg.green = 0x0;
      fg.blue  = 0x0;

      bg.red   = 0xffff;
      bg.green = 0x0;
      bg.blue  = 0x0;
      break;
    }

  gdk_gc_set_rgb_fg_color (gc, &fg);
  gdk_gc_set_rgb_bg_color (gc, &bg);

  return gc;
}

static inline gboolean
gimp_canvas_ensure_style (GimpCanvas      *canvas,
                          GimpCanvasStyle  style)
{
  return (canvas->gc[style] != NULL ||
          (canvas->gc[style] = gimp_canvas_gc_new (canvas, style)) != NULL);
}


/*  public functions  */

/**
 * gimp_canvas_new:
 *
 * Creates a new #GimpCanvas widget.
 *
 * The #GimpCanvas widget is a #GtkDrawingArea abstraction. It manages
 * a set of graphic contexts for drawing on a GIMP display. If you
 * draw using a #GimpCanvasStyle, #GimpCanvas makes sure that the
 * associated #GdkGC is created. All drawing on the canvas needs to
 * happen by means of the #GimpCanvas drawing functions. Besides from
 * not needing a #GdkGC pointer, the #GimpCanvas drawing functions
 * look and work like their #GdkDrawable counterparts. #GimpCanvas
 * gracefully handles attempts to draw on the unrealized widget.
 *
 * Return value: a new #GimpCanvas widget
 **/
GtkWidget *
gimp_canvas_new (void)
{
  return g_object_new (GIMP_TYPE_CANVAS,
                       "name", "gimp-canvas",
                       NULL);
}

/**
 * gimp_canvas_draw_cursor:
 * @canvas: the #GimpCanvas widget to draw on.
 * @x: x coordinate
 * @y: y coordinate
 *
 * Draws a plus-shaped black and white cursor, centered at the point
 * @x, @y.
 **/
void
gimp_canvas_draw_cursor (GimpCanvas *canvas,
                         gint        x,
                         gint        y)
{
  GtkWidget *widget = GTK_WIDGET (canvas);

  if (! (gimp_canvas_ensure_style (canvas, GIMP_CANVAS_STYLE_BLACK) &&
         gimp_canvas_ensure_style (canvas, GIMP_CANVAS_STYLE_WHITE)) )
    return;

  gdk_draw_line (widget->window, canvas->gc[GIMP_CANVAS_STYLE_WHITE],
                 x - 7, y - 1, x + 7, y - 1);
  gdk_draw_line (widget->window, canvas->gc[GIMP_CANVAS_STYLE_BLACK],
                 x - 7, y,     x + 7, y    );
  gdk_draw_line (widget->window, canvas->gc[GIMP_CANVAS_STYLE_WHITE],
                 x - 7, y + 1, x + 7, y + 1);
  gdk_draw_line (widget->window, canvas->gc[GIMP_CANVAS_STYLE_WHITE],
                 x - 1, y - 7, x - 1, y + 7);
  gdk_draw_line (widget->window, canvas->gc[GIMP_CANVAS_STYLE_BLACK],
                 x,     y - 7, x,     y + 7);
  gdk_draw_line (widget->window, canvas->gc[GIMP_CANVAS_STYLE_WHITE],
                 x + 1, y - 7, x + 1, y + 7);
}

/**
 * gimp_canvas_draw_point:
 * @canvas: a #GimpCanvas widget
 * @style:  one of the enumerated #GimpCanvasStyle's.
 * @x:      x coordinate
 * @y:      y coordinate
 *
 * Draw a single pixel at the specified location in the specified
 * style.
 **/
void
gimp_canvas_draw_point (GimpCanvas      *canvas,
                        GimpCanvasStyle  style,
                        gint             x,
                        gint             y)
{
  if (! gimp_canvas_ensure_style (canvas, style))
    return;

  gdk_draw_point (GTK_WIDGET (canvas)->window, canvas->gc[style],
                  x, y);
}

/**
 * gimp_canvas_draw_points:
 * @canvas:     a #GimpCanvas widget
 * @style:      one of the enumerated #GimpCanvasStyle's.
 * @points:     an array of GdkPoint x-y pairs.
 * @num_points: the number of points in the array
 *
 * Draws a set of one-pixel points at the locations given in the
 * @points argument, in the specified style.
 **/
void
gimp_canvas_draw_points (GimpCanvas      *canvas,
                         GimpCanvasStyle  style,
                         GdkPoint        *points,
                         gint             num_points)
{
  if (! gimp_canvas_ensure_style (canvas, style))
    return;

  gdk_draw_points (GTK_WIDGET (canvas)->window, canvas->gc[style],
                   points, num_points);
}

/**
 * gimp_canvas_draw_line:
 * @canvas: a #GimpCanvas widget
 * @style:  one of the enumerated #GimpCanvasStyle's.
 * @x1:     X coordinate of the first point
 * @y1:     Y coordinate of the first point
 * @x2:     X coordinate of the second point
 * @y2:     Y coordinate of the second point
 *
 * Draw a line connecting the specified points, using the specified
 * style.
 **/
void
gimp_canvas_draw_line (GimpCanvas      *canvas,
                       GimpCanvasStyle  style,
                       gint             x1,
                       gint             y1,
                       gint             x2,
                       gint             y2)
{
  if (! gimp_canvas_ensure_style (canvas, style))
    return;

  gdk_draw_line (GTK_WIDGET (canvas)->window, canvas->gc[style],
                 x1, y1, x2, y2);
}

/**
 * gimp_canvas_draw_lines:
 * @canvas:     a #GimpCanvas widget
 * @style:      one of the enumerated #GimpCanvasStyle's.
 * @points:     a #GdkPoint array.
 * @num_points: the number of points in the array.
 *
 * Draws a set of lines connecting the specified points, in the
 * specified style.
 **/
void
gimp_canvas_draw_lines (GimpCanvas      *canvas,
                        GimpCanvasStyle  style,
                        GdkPoint        *points,
                        gint             num_points)
{
  if (! gimp_canvas_ensure_style (canvas, style))
    return;

  gdk_draw_lines (GTK_WIDGET (canvas)->window, canvas->gc[style],
                  points, num_points);
}

/**
 * gimp_canvas_draw_rectangle:
 * @canvas: a #GimpCanvas widget
 * @style:  one of the enumerated #GimpCanvasStyle's.
 * @filled: %TRUE if the rectangle is to be filled.
 * @x:      X coordinate of the upper left corner.
 * @y:      Y coordinate of the upper left corner.
 * @width:  width of the rectangle.
 * @height: height of the rectangle.
 *
 * Draws a rectangle in the specified style.
 **/
void
gimp_canvas_draw_rectangle (GimpCanvas      *canvas,
                            GimpCanvasStyle  style,
                            gboolean         filled,
                            gint             x,
                            gint             y,
                            gint             width,
                            gint             height)
{
  if (! gimp_canvas_ensure_style (canvas, style))
    return;

  gdk_draw_rectangle (GTK_WIDGET (canvas)->window, canvas->gc[style],
                      filled, x, y, width, height);
}

/**
 * gimp_canvas_draw_arc:
 * @canvas: a #GimpCanvas widget
 * @style:  one of the enumerated #GimpCanvasStyle's.
 * @filled: %TRUE if the arc is to be filled, producing a 'pie slice'.
 * @x:      X coordinate of the left edge of the bounding rectangle.
 * @y:      Y coordinate of the top edge of the bounding rectangle.
 * @width:  width of the bounding rectangle.
 * @height: height of the bounding rectangle.
 * @angle1: the start angle of the arc.
 * @angle2: the end angle of the arc.
 *
 * Draws an arc or pie slice, in the specified style.
 **/
void
gimp_canvas_draw_arc (GimpCanvas      *canvas,
                      GimpCanvasStyle  style,
                      gboolean         filled,
                      gint             x,
                      gint             y,
                      gint             width,
                      gint             height,
                      gint             angle1,
                      gint             angle2)
{
  if (! gimp_canvas_ensure_style (canvas, style))
    return;

  gdk_draw_arc (GTK_WIDGET (canvas)->window, canvas->gc[style],
                filled, x, y, width, height, angle1, angle2);
}

/**
 * gimp_canvas_draw_polygon:
 * @canvas:     a #GimpCanvas widget
 * @style:      one of the enumerated #GimpCanvasStyle's.
 * @filled:     if %TRUE, fill the polygon.
 * @points:     a #GdkPoint array.
 * @num_points: the number of points in the array.
 *
 * Draws a polygon connecting the specified points, in the specified
 * style.
 **/
void
gimp_canvas_draw_polygon (GimpCanvas      *canvas,
                          GimpCanvasStyle  style,
                          gboolean         filled,
                          GdkPoint        *points,
                          gint             num_points)
{
  if (! gimp_canvas_ensure_style (canvas, style))
    return;

  gdk_draw_polygon (GTK_WIDGET (canvas)->window, canvas->gc[style],
                    filled, points, num_points);
}

/**
 * gimp_canvas_draw_segments:
 * @canvas:       a #GimpCanvas widget
 * @style:        one of the enumerated #GimpCanvasStyle's.
 * @segments:     a #GdkSegment array.
 * @num_segments: the number of segments in the array.
 *
 * Draws a set of line segments in the specified style.
 **/
void
gimp_canvas_draw_segments (GimpCanvas      *canvas,
                           GimpCanvasStyle  style,
                           GdkSegment      *segments,
                           gint             num_segments)
{
  if (! gimp_canvas_ensure_style (canvas, style))
    return;

  while (num_segments >= 32000)
    {
      gdk_draw_segments (GTK_WIDGET (canvas)->window, canvas->gc[style],
                         segments, 32000);
      num_segments -= 32000;
      segments     += 32000;
    }
  gdk_draw_segments (GTK_WIDGET (canvas)->window, canvas->gc[style],
                     segments, num_segments);
}

/**
 * gimp_canvas_draw_rgb:
 * @canvas:    a #GimpCanvas widget
 * @style:     one of the enumerated #GimpCanvasStyle's.
 * @x:         X coordinate of the upper left corner.
 * @y:         Y coordinate of the upper left corner.
 * @width:     width of the rectangle to be drawn.
 * @height:    height of the rectangle to be drawn.
 * @rgb_buf:   pixel data for the image to be drawn.
 * @rowstride: the rowstride in @rgb_buf.
 * @xdith:     x offset for dither alignment.
 * @ydith:     y offset for dither alignment.
 *
 * Draws an RGB image on the canvas in the specified style.
 **/
void
gimp_canvas_draw_rgb (GimpCanvas      *canvas,
                      GimpCanvasStyle  style,
                      gint             x,
                      gint             y,
                      gint             width,
                      gint             height,
                      guchar          *rgb_buf,
                      gint             rowstride,
                      gint             xdith,
                      gint             ydith)
{
  if (! gimp_canvas_ensure_style (canvas, style))
    return;

  gdk_draw_rgb_image_dithalign (GTK_WIDGET (canvas)->window, canvas->gc[style],
                                x, y, width, height,
                                GDK_RGB_DITHER_MAX,
                                rgb_buf, rowstride, xdith, ydith);
}

/**
 * gimp_canvas_set_clip_rect:
 * @canvas: a #GimpCanvas widget
 * @style:  one of the enumerated #GimpCanvasStyle's.
 * @rect:   a #GdkRectangle to set the bounds of the clipping area.
 *
 * Sets a rectangular clipping area for the specified style.
 **/
void
gimp_canvas_set_clip_rect (GimpCanvas      *canvas,
                           GimpCanvasStyle  style,
                           GdkRectangle    *rect)
{
  if (! canvas->gc[style])
    {
      if (! rect)
        return;

      canvas->gc[style] = gimp_canvas_gc_new (canvas, style);
    }

  gdk_gc_set_clip_rectangle (canvas->gc[style], rect);
}

/**
 * gimp_canvas_set_clip_region:
 * @canvas: a #GimpCanvas widget
 * @style:  one of the enumerated #GimpCanvasStyle's.
 * @region: a #GdkRegion to set the bounds of the clipping area.
 *
 * Sets a clipping region for the specified style.
 **/
void
gimp_canvas_set_clip_region (GimpCanvas      *canvas,
                             GimpCanvasStyle  style,
                             GdkRegion       *region)
{
  if (! canvas->gc[style])
    {
      if (! region)
        return;

      canvas->gc[style] = gimp_canvas_gc_new (canvas, style);
    }

  gdk_gc_set_clip_region (canvas->gc[style], region);
}

/**
 * gimp_canvas_set_stipple_index:
 * @canvas: a #GimpCanvas widget
 * @style:  the #GimpCanvasStyle to alter
 * @index:  the new stipple index
 *
 * Some styles of the #GimpCanvas do a stipple fill. #GimpCanvas has a
 * set of %GIMP_CANVAS_NUM_STIPPLES stipple bitmaps. This function
 * allows you to change the bitmap being used. This can be used to
 * implement a marching ants effect. An older implementation used to
 * use this feature and so it is included since it might be useful in
 * the future. All stipple bitmaps but the default one are created on
 * the fly.
 */
void
gimp_canvas_set_stipple_index (GimpCanvas      *canvas,
                               GimpCanvasStyle  style,
                               guint            index)
{
  if (! gimp_canvas_ensure_style (canvas, style))
    return;

  index = index % GIMP_CANVAS_NUM_STIPPLES;

  if (! canvas->stipple[index])
    {
      canvas->stipple[index] =
        gdk_bitmap_create_from_data (GTK_WIDGET (canvas)->window,
                                     (const gchar *) stipples[index], 8, 8);
    }

  gdk_gc_set_stipple (canvas->gc[style], canvas->stipple[index]);
}

/**
 * gimp_canvas_set_custom_gc:
 * @canvas: a #GimpCanvas widget
 * @gc:     a #GdkGC;
 *
 * The #GimpCanvas widget has an extra style for a custom #GdkGC. This
 * function allows you to set the @gc for the %GIMP_CANVAS_STYLE_CUSTOM.
 * Drawing with the custom style only works if you set a #GdkGC
 * earlier.  Since the custom #GdkGC can under certain circumstances
 * be destroyed by #GimpCanvas, you should always set the custom gc
 * before calling a #GimpCanvas drawing function with
 * %GIMP_CANVAS_STYLE_CUSTOM.
 **/
void
gimp_canvas_set_custom_gc (GimpCanvas *canvas,
                           GdkGC      *gc)
{
  if (canvas->gc[GIMP_CANVAS_STYLE_CUSTOM])
    g_object_unref (canvas->gc[GIMP_CANVAS_STYLE_CUSTOM]);

  canvas->gc[GIMP_CANVAS_STYLE_CUSTOM] = gc;

  if (gc)
    g_object_ref (gc);
}

/**
 * gimp_canvas_set_bg_color:
 * @canvas:   a #GimpCanvas widget
 * @color:    a color in #GimpRGB format
 *
 * Sets the background color of the canvas's window.  This
 * is the color the canvas is set to if it is cleared.
 **/
void
gimp_canvas_set_bg_color (GimpCanvas *canvas,
                          GimpRGB    *color)
{
  GtkWidget   *widget = GTK_WIDGET (canvas);
  GdkColormap *colormap;
  GdkColor     gdk_color;

  if (! GTK_WIDGET_REALIZED (widget))
    return;

  gimp_rgb_get_gdk_color (color, &gdk_color);

  colormap = gdk_drawable_get_colormap (widget->window);
  g_return_if_fail (colormap != NULL);
  gdk_colormap_alloc_color (colormap, &gdk_color, FALSE, TRUE);

  gdk_window_set_background (widget->window, &gdk_color);
}