summaryrefslogtreecommitdiffstats
path: root/app/display/gimpcanvas-style.c
blob: 8d479618fdb9468bf05ff17deb586b1d0f8a2026 (plain)
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
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimpcanvas-style.c
 * Copyright (C) 2010  Michael Natterer <mitch@gimp.org>
 *
 * 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 3 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, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <gegl.h>
#include <gtk/gtk.h>

#include "libgimpcolor/gimpcolor.h"
#include "libgimpwidgets/gimpwidgets.h"

#include "display-types.h"

#include "core/gimp-cairo.h"
#include "core/gimpgrid.h"
#include "core/gimplayer.h"

#include "gimpcanvas-style.h"

/* Styles for common and custom guides. */
static const GimpRGB guide_normal_fg         = { 0.0, 0.0, 0.0, 1.0 };
static const GimpRGB guide_normal_bg         = { 0.0, 0.8, 1.0, 1.0 };
static const GimpRGB guide_active_fg         = { 0.0, 0.0, 0.0, 1.0 };
static const GimpRGB guide_active_bg         = { 1.0, 0.0, 0.0, 1.0 };

static const GimpRGB guide_mirror_normal_fg  = { 1.0, 1.0, 1.0, 1.0 };
static const GimpRGB guide_mirror_normal_bg  = { 0.0, 1.0, 0.0, 1.0 };
static const GimpRGB guide_mirror_active_fg  = { 0.0, 1.0, 0.0, 1.0 };
static const GimpRGB guide_mirror_active_bg  = { 1.0, 0.0, 0.0, 1.0 };

static const GimpRGB guide_mandala_normal_fg = { 1.0, 1.0, 1.0, 1.0 };
static const GimpRGB guide_mandala_normal_bg = { 0.0, 1.0, 1.0, 1.0 };
static const GimpRGB guide_mandala_active_fg = { 0.0, 1.0, 1.0, 1.0 };
static const GimpRGB guide_mandala_active_bg = { 1.0, 0.0, 0.0, 1.0 };

static const GimpRGB guide_split_normal_fg   = { 1.0, 1.0, 1.0, 1.0 };
static const GimpRGB guide_split_normal_bg   = { 1.0, 0.0, 1.0, 1.0 };
static const GimpRGB guide_split_active_fg   = { 1.0, 0.0, 1.0, 1.0 };
static const GimpRGB guide_split_active_bg   = { 1.0, 0.0, 0.0, 1.0 };

/* Styles for other canvas items. */
static const GimpRGB sample_point_normal = { 0.0, 0.8, 1.0, 1.0 };
static const GimpRGB sample_point_active = { 1.0, 0.0, 0.0, 1.0 };

static const GimpRGB layer_fg            = { 0.0, 0.0, 0.0, 1.0 };
static const GimpRGB layer_bg            = { 1.0, 1.0, 0.0, 1.0 };

static const GimpRGB layer_group_fg      = { 0.0, 0.0, 0.0, 1.0 };
static const GimpRGB layer_group_bg      = { 0.0, 1.0, 1.0, 1.0 };

static const GimpRGB layer_mask_fg       = { 0.0, 0.0, 0.0, 1.0 };
static const GimpRGB layer_mask_bg       = { 0.0, 1.0, 0.0, 1.0 };

static const GimpRGB canvas_fg           = { 0.0, 0.0, 0.0, 1.0 };
static const GimpRGB canvas_bg           = { 1.0, 0.5, 0.0, 1.0 };

static const GimpRGB selection_out_fg    = { 1.0, 1.0, 1.0, 1.0 };
static const GimpRGB selection_out_bg    = { 0.5, 0.5, 0.5, 1.0 };

static const GimpRGB selection_in_fg     = { 0.0, 0.0, 0.0, 1.0 };
static const GimpRGB selection_in_bg     = { 1.0, 1.0, 1.0, 1.0 };

static const GimpRGB vectors_normal_bg   = { 1.0, 1.0, 1.0, 0.6 };
static const GimpRGB vectors_normal_fg   = { 0.0, 0.0, 1.0, 0.8 };

static const GimpRGB vectors_active_bg   = { 1.0, 1.0, 1.0, 0.6 };
static const GimpRGB vectors_active_fg   = { 1.0, 0.0, 0.0, 0.8 };

static const GimpRGB outline_bg          = { 1.0, 1.0, 1.0, 0.6 };
static const GimpRGB outline_fg          = { 0.0, 0.0, 0.0, 0.8 };

static const GimpRGB passe_partout       = { 0.0, 0.0, 0.0, 1.0 };

static const GimpRGB tool_bg             = { 0.0, 0.0, 0.0, 0.4 };
static const GimpRGB tool_fg             = { 1.0, 1.0, 1.0, 0.8 };
static const GimpRGB tool_fg_highlight   = { 1.0, 0.8, 0.2, 0.8 };


/*  public functions  */

void
gimp_canvas_set_guide_style (GtkWidget      *canvas,
                             cairo_t        *cr,
                             GimpGuideStyle  style,
                             gboolean        active,
                             gdouble         offset_x,
                             gdouble         offset_y)
{
  cairo_pattern_t *pattern;
  GimpRGB          normal_fg;
  GimpRGB          normal_bg;
  GimpRGB          active_fg;
  GimpRGB          active_bg;
  gdouble          line_width;

  g_return_if_fail (GTK_IS_WIDGET (canvas));
  g_return_if_fail (cr != NULL);

  switch (style)
    {
    case GIMP_GUIDE_STYLE_NORMAL:
      normal_fg  = guide_normal_fg;
      normal_bg  = guide_normal_bg;
      active_fg  = guide_active_fg;
      active_bg  = guide_active_bg;
      line_width = 1.0;
      break;

    case GIMP_GUIDE_STYLE_MIRROR:
      normal_fg  = guide_mirror_normal_fg;
      normal_bg  = guide_mirror_normal_bg;
      active_fg  = guide_mirror_active_fg;
      active_bg  = guide_mirror_active_bg;
      line_width = 1.0;
      break;

    case GIMP_GUIDE_STYLE_MANDALA:
      normal_fg  = guide_mandala_normal_fg;
      normal_bg  = guide_mandala_normal_bg;
      active_fg  = guide_mandala_active_fg;
      active_bg  = guide_mandala_active_bg;
      line_width = 1.0;
      break;

    case GIMP_GUIDE_STYLE_SPLIT_VIEW:
      normal_fg  = guide_split_normal_fg;
      normal_bg  = guide_split_normal_bg;
      active_fg  = guide_split_active_fg;
      active_bg  = guide_split_active_bg;
      line_width = 1.0;
      break;

    default: /* GIMP_GUIDE_STYLE_NONE */
      /* This should not happen. */
      g_return_if_reached ();
    }

  cairo_set_line_width (cr, line_width);

  if (active)
    pattern = gimp_cairo_pattern_create_stipple (&active_fg, &active_bg, 0,
                                                 offset_x, offset_y);
  else
    pattern = gimp_cairo_pattern_create_stipple (&normal_fg, &normal_bg, 0,
                                                 offset_x, offset_y);

  cairo_set_source (cr, pattern);
  cairo_pattern_destroy (pattern);
}

void
gimp_canvas_set_sample_point_style (GtkWidget *canvas,
                                    cairo_t   *cr,
                                    gboolean   active)
{
  g_return_if_fail (GTK_IS_WIDGET (canvas));
  g_return_if_fail (cr != NULL);

  cairo_set_line_width (cr, 1.0);

  if (active)
    gimp_cairo_set_source_rgb (cr, &sample_point_active);
  else
    gimp_cairo_set_source_rgb (cr, &sample_point_normal);
}

void
gimp_canvas_set_grid_style (GtkWidget *canvas,
                            cairo_t   *cr,
                            GimpGrid  *grid,
                            gdouble    offset_x,
                            gdouble    offset_y)
{
  GimpRGB fg;
  GimpRGB bg;

  g_return_if_fail (GTK_IS_WIDGET (canvas));
  g_return_if_fail (cr != NULL);
  g_return_if_fail (GIMP_IS_GRID (grid));

  cairo_set_line_width (cr, 1.0);

  gimp_grid_get_fgcolor (grid, &fg);

  switch (gimp_grid_get_style (grid))
    {
      cairo_pattern_t *pattern;

    case GIMP_GRID_ON_OFF_DASH:
    case GIMP_GRID_DOUBLE_DASH:
      if (grid->style == GIMP_GRID_DOUBLE_DASH)
        {
          gimp_grid_get_bgcolor (grid, &bg);

          pattern = gimp_cairo_pattern_create_stipple (&fg, &bg, 0,
                                                       offset_x, offset_y);
        }
      else
        {
          gimp_rgba_set (&bg, 0.0, 0.0, 0.0, 0.0);

          pattern = gimp_cairo_pattern_create_stipple (&fg, &bg, 0,
                                                       offset_x, offset_y);
        }

      cairo_set_source (cr, pattern);
      cairo_pattern_destroy (pattern);
      break;

    case GIMP_GRID_DOTS:
    case GIMP_GRID_INTERSECTIONS:
    case GIMP_GRID_SOLID:
      gimp_cairo_set_source_rgb (cr, &fg);
      break;
    }
}

void
gimp_canvas_set_pen_style (GtkWidget     *canvas,
                           cairo_t       *cr,
                           const GimpRGB *color,
                           gint           width)
{
  g_return_if_fail (GTK_IS_WIDGET (canvas));
  g_return_if_fail (cr != NULL);
  g_return_if_fail (color != NULL);

  cairo_set_antialias (cr, CAIRO_ANTIALIAS_NONE);
  cairo_set_line_width (cr, width);
  cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND);
  cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);

  gimp_cairo_set_source_rgb (cr, color);
}

void
gimp_canvas_set_layer_style (GtkWidget *canvas,
                             cairo_t   *cr,
                             GimpLayer *layer,
                             gdouble    offset_x,
                             gdouble    offset_y)
{
  cairo_pattern_t *pattern;

  g_return_if_fail (GTK_IS_WIDGET (canvas));
  g_return_if_fail (cr != NULL);
  g_return_if_fail (GIMP_IS_LAYER (layer));

  cairo_set_line_width (cr, 1.0);
  cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE);

  if (gimp_layer_get_mask (layer) &&
      gimp_layer_get_edit_mask (layer))
    {
      pattern = gimp_cairo_pattern_create_stipple (&layer_mask_fg,
                                                   &layer_mask_bg,
                                                   0,
                                                   offset_x, offset_y);
    }
  else if (gimp_viewable_get_children (GIMP_VIEWABLE (layer)))
    {
      pattern = gimp_cairo_pattern_create_stipple (&layer_group_fg,
                                                   &layer_group_bg,
                                                   0,
                                                   offset_x, offset_y);
    }
  else
    {
      pattern = gimp_cairo_pattern_create_stipple (&layer_fg,
                                                   &layer_bg,
                                                   0,
                                                   offset_x, offset_y);
    }

  cairo_set_source (cr, pattern);
  cairo_pattern_destroy (pattern);
}

void
gimp_canvas_set_canvas_style (GtkWidget *canvas,
                              cairo_t   *cr,
                              gdouble    offset_x,
                              gdouble    offset_y)
{
  cairo_pattern_t *pattern;

  g_return_if_fail (GTK_IS_WIDGET (canvas));
  g_return_if_fail (cr != NULL);

  cairo_set_line_width (cr, 1.0);
  cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE);

  pattern = gimp_cairo_pattern_create_stipple (&canvas_fg,
                                               &canvas_bg,
                                               0,
                                               offset_x, offset_y);

  cairo_set_source (cr, pattern);
  cairo_pattern_destroy (pattern);
}

void
gimp_canvas_set_selection_out_style (GtkWidget *canvas,
                                     cairo_t   *cr,
                                     gdouble    offset_x,
                                     gdouble    offset_y)
{
  cairo_pattern_t *pattern;

  g_return_if_fail (GTK_IS_WIDGET (canvas));
  g_return_if_fail (cr != NULL);

  cairo_set_line_width (cr, 1.0);
  cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE);

  pattern = gimp_cairo_pattern_create_stipple (&selection_out_fg,
                                               &selection_out_bg,
                                               0,
                                               offset_x, offset_y);
  cairo_set_source (cr, pattern);
  cairo_pattern_destroy (pattern);
}

void
gimp_canvas_set_selection_in_style (GtkWidget *canvas,
                                    cairo_t   *cr,
                                    gint       index,
                                    gdouble    offset_x,
                                    gdouble    offset_y)
{
  cairo_pattern_t *pattern;

  g_return_if_fail (GTK_IS_WIDGET (canvas));
  g_return_if_fail (cr != NULL);

  cairo_set_line_width (cr, 1.0);
  cairo_set_line_cap (cr, CAIRO_LINE_CAP_SQUARE);

  pattern = gimp_cairo_pattern_create_stipple (&selection_in_fg,
                                               &selection_in_bg,
                                               index,
                                               offset_x, offset_y);
  cairo_set_source (cr, pattern);
  cairo_pattern_destroy (pattern);
}

void
gimp_canvas_set_vectors_bg_style (GtkWidget *canvas,
                                  cairo_t   *cr,
                                  gboolean   active)
{
  g_return_if_fail (GTK_IS_WIDGET (canvas));
  g_return_if_fail (cr != NULL);

  cairo_set_line_width (cr, 3.0);

  if (active)
    gimp_cairo_set_source_rgba (cr, &vectors_active_bg);
  else
    gimp_cairo_set_source_rgba (cr, &vectors_normal_bg);
}

void
gimp_canvas_set_vectors_fg_style (GtkWidget *canvas,
                                  cairo_t   *cr,
                                  gboolean   active)
{
  g_return_if_fail (GTK_IS_WIDGET (canvas));
  g_return_if_fail (cr != NULL);

  cairo_set_line_width (cr, 1.0);

  if (active)
    gimp_cairo_set_source_rgba (cr, &vectors_active_fg);
  else
    gimp_cairo_set_source_rgba (cr, &vectors_normal_fg);
}

void
gimp_canvas_set_outline_bg_style (GtkWidget *canvas,
                                  cairo_t   *cr)
{
  g_return_if_fail (GTK_IS_WIDGET (canvas));
  g_return_if_fail (cr != NULL);

  cairo_set_line_width (cr, 1.0);
  gimp_cairo_set_source_rgba (cr, &outline_bg);
}

void
gimp_canvas_set_outline_fg_style (GtkWidget *canvas,
                                  cairo_t   *cr)
{
  static const double dashes[] = { 4.0, 4.0 };

  g_return_if_fail (GTK_IS_WIDGET (canvas));
  g_return_if_fail (cr != NULL);

  cairo_set_line_width (cr, 1.0);
  gimp_cairo_set_source_rgba (cr, &outline_fg);
  cairo_set_dash (cr, dashes, G_N_ELEMENTS (dashes), 0);
}

void
gimp_canvas_set_passe_partout_style (GtkWidget *canvas,
                                     cairo_t   *cr)
{
  g_return_if_fail (GTK_IS_WIDGET (canvas));
  g_return_if_fail (cr != NULL);

  gimp_cairo_set_source_rgba (cr, &passe_partout);
}

void
gimp_canvas_set_tool_bg_style (GtkWidget *canvas,
                               cairo_t   *cr)
{
  g_return_if_fail (GTK_IS_WIDGET (canvas));
  g_return_if_fail (cr != NULL);

  cairo_set_line_width (cr, 3.0);
  cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);

  gimp_cairo_set_source_rgba (cr, &tool_bg);
}

void
gimp_canvas_set_tool_fg_style (GtkWidget *canvas,
                               cairo_t   *cr,
                               gboolean   highlight)
{
  g_return_if_fail (cr != NULL);

  cairo_set_line_width (cr, 1.0);
  cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND);

  if (highlight)
    gimp_cairo_set_source_rgba (cr, &tool_fg_highlight);
  else
    gimp_cairo_set_source_rgba (cr, &tool_fg);
}