gimpcanvasitem.c 11.5 KB
Newer Older
1 2 3
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
4 5 6
 * gimpcanvasitem.c
 * Copyright (C) 2010 Michael Natterer <mitch@gimp.org>
 *
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 * 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 <http://www.gnu.org/licenses/>.
 */

#include "config.h"

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

#include "display-types.h"

28 29
#include "libgimpmath/gimpmath.h"

30 31 32 33 34
#include "gimpcanvasitem.h"
#include "gimpdisplayshell.h"
#include "gimpdisplayshell-style.h"


35 36 37
enum
{
  PROP_0,
38
  PROP_SHELL,
39 40 41 42 43
  PROP_LINE_CAP,
  PROP_HIGHLIGHT
};


44 45 46 47
typedef struct _GimpCanvasItemPrivate GimpCanvasItemPrivate;

struct _GimpCanvasItemPrivate
{
48 49 50 51 52
  GimpDisplayShell *shell;
  cairo_line_cap_t  line_cap;
  gboolean          highlight;
  gint              suspend_stroking;
  gint              suspend_filling;
53 54 55 56 57 58 59 60
};

#define GET_PRIVATE(item) \
        G_TYPE_INSTANCE_GET_PRIVATE (item, \
                                     GIMP_TYPE_CANVAS_ITEM, \
                                     GimpCanvasItemPrivate)


61 62
/*  local function prototypes  */

63
static void        gimp_canvas_item_constructed      (GObject          *object);
64 65 66 67 68 69 70 71 72
static void        gimp_canvas_item_set_property     (GObject          *object,
                                                      guint             property_id,
                                                      const GValue     *value,
                                                      GParamSpec       *pspec);
static void        gimp_canvas_item_get_property     (GObject          *object,
                                                      guint             property_id,
                                                      GValue           *value,
                                                      GParamSpec       *pspec);

73 74 75 76 77
static void        gimp_canvas_item_real_draw        (GimpCanvasItem   *item,
                                                      GimpDisplayShell *shell,
                                                      cairo_t          *cr);
static GdkRegion * gimp_canvas_item_real_get_extents (GimpCanvasItem   *item,
                                                      GimpDisplayShell *shell);
78 79 80 81 82 83
static void        gimp_canvas_item_real_stroke      (GimpCanvasItem   *item,
                                                      GimpDisplayShell *shell,
                                                      cairo_t          *cr);
static void        gimp_canvas_item_real_fill        (GimpCanvasItem   *item,
                                                      GimpDisplayShell *shell,
                                                      cairo_t          *cr);
84 85 86 87 88 89 90 91 92 93 94


G_DEFINE_TYPE (GimpCanvasItem, gimp_canvas_item,
               GIMP_TYPE_OBJECT)

#define parent_class gimp_canvas_item_parent_class


static void
gimp_canvas_item_class_init (GimpCanvasItemClass *klass)
{
95 96
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

97
  object_class->constructed  = gimp_canvas_item_constructed;
98 99 100 101 102
  object_class->set_property = gimp_canvas_item_set_property;
  object_class->get_property = gimp_canvas_item_get_property;

  klass->draw                = gimp_canvas_item_real_draw;
  klass->get_extents         = gimp_canvas_item_real_get_extents;
103 104
  klass->stroke              = gimp_canvas_item_real_stroke;
  klass->fill                = gimp_canvas_item_real_fill;
105

106 107 108 109 110 111 112
  g_object_class_install_property (object_class, PROP_SHELL,
                                   g_param_spec_object ("shell",
                                                        NULL, NULL,
                                                        GIMP_TYPE_DISPLAY_SHELL,
                                                        GIMP_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT_ONLY));

113 114 115 116 117 118 119 120 121 122 123 124 125
  g_object_class_install_property (object_class, PROP_LINE_CAP,
                                   g_param_spec_int ("line-cap",
                                                     NULL, NULL,
                                                     CAIRO_LINE_CAP_BUTT,
                                                     CAIRO_LINE_CAP_SQUARE,
                                                     CAIRO_LINE_CAP_ROUND,
                                                     GIMP_PARAM_READWRITE));

  g_object_class_install_property (object_class, PROP_HIGHLIGHT,
                                   g_param_spec_boolean ("highlight",
                                                         NULL, NULL,
                                                         FALSE,
                                                         GIMP_PARAM_READWRITE));
126 127

  g_type_class_add_private (klass, sizeof (GimpCanvasItemPrivate));
128 129 130 131 132
}

static void
gimp_canvas_item_init (GimpCanvasItem *item)
{
133 134
  GimpCanvasItemPrivate *private = GET_PRIVATE (item);

135
  private->shell            = NULL;
136 137 138
  private->line_cap         = CAIRO_LINE_CAP_ROUND;
  private->highlight        = FALSE;
  private->suspend_stroking = 0;
139
  private->suspend_filling  = 0;
140 141
}

142 143 144 145 146 147 148 149 150 151 152
static void
gimp_canvas_item_constructed (GObject *object)
{
  GimpCanvasItemPrivate *private = GET_PRIVATE (object);

  g_assert (GIMP_IS_DISPLAY_SHELL (private->shell));

  if (G_OBJECT_CLASS (parent_class)->constructed)
    G_OBJECT_CLASS (parent_class)->constructed (object);
}

153 154 155 156 157 158 159 160 161 162
static void
gimp_canvas_item_set_property (GObject      *object,
                               guint         property_id,
                               const GValue *value,
                               GParamSpec   *pspec)
{
  GimpCanvasItemPrivate *private = GET_PRIVATE (object);

  switch (property_id)
    {
163 164 165
    case PROP_SHELL:
      private->shell = g_value_get_object (value); /* don't ref */
      break;
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
    case PROP_LINE_CAP:
      private->line_cap = g_value_get_int (value);
      break;
    case PROP_HIGHLIGHT:
      private->highlight = g_value_get_boolean (value);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gimp_canvas_item_get_property (GObject    *object,
                               guint       property_id,
                               GValue     *value,
                               GParamSpec *pspec)
{
  GimpCanvasItemPrivate *private = GET_PRIVATE (object);

  switch (property_id)
    {
189 190 191
    case PROP_SHELL:
      g_value_set_object (value, private->shell);
      break;
192 193 194 195 196 197 198 199 200 201 202
    case PROP_LINE_CAP:
      g_value_set_int (value, private->line_cap);
      break;
    case PROP_HIGHLIGHT:
      g_value_set_boolean (value, private->highlight);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
203 204 205 206 207 208 209 210 211 212
}

static void
gimp_canvas_item_real_draw (GimpCanvasItem   *item,
                            GimpDisplayShell *shell,
                            cairo_t          *cr)
{
  g_warn_if_reached ();
}

213 214 215
static GdkRegion *
gimp_canvas_item_real_get_extents (GimpCanvasItem   *item,
                                   GimpDisplayShell *shell)
216
{
217
  return NULL;
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
static void
gimp_canvas_item_real_stroke (GimpCanvasItem   *item,
                              GimpDisplayShell *shell,
                              cairo_t          *cr)
{
  GimpCanvasItemPrivate *private = GET_PRIVATE (item);

  cairo_set_line_cap (cr, private->line_cap);

  gimp_display_shell_set_tool_bg_style (shell, cr);
  cairo_stroke_preserve (cr);

  gimp_display_shell_set_tool_fg_style (shell, cr, private->highlight);
  cairo_stroke (cr);
}

static void
gimp_canvas_item_real_fill (GimpCanvasItem   *item,
                            GimpDisplayShell *shell,
                            cairo_t          *cr)
{
  GimpCanvasItemPrivate *private = GET_PRIVATE (item);

  gimp_display_shell_set_tool_bg_style (shell, cr);
  cairo_set_line_width (cr, 2.0);
  cairo_stroke_preserve (cr);

  gimp_display_shell_set_tool_fg_style (shell, cr, private->highlight);
  cairo_fill (cr);
}

251 252 253 254

/*  public functions  */

void
255 256
gimp_canvas_item_draw (GimpCanvasItem *item,
                       cairo_t        *cr)
257
{
258 259
  GimpCanvasItemPrivate *private;

260 261 262
  g_return_if_fail (GIMP_IS_CANVAS_ITEM (item));
  g_return_if_fail (cr != NULL);

263 264
  private = GET_PRIVATE (item);

265 266
  cairo_save (cr);

267
  GIMP_CANVAS_ITEM_GET_CLASS (item)->draw (item, private->shell, cr);
268 269 270 271

  cairo_restore (cr);
}

272
GdkRegion *
273
gimp_canvas_item_get_extents (GimpCanvasItem *item)
274
{
275 276
  GimpCanvasItemPrivate *private;

277 278
  g_return_val_if_fail (GIMP_IS_CANVAS_ITEM (item), NULL);

279 280 281
  private = GET_PRIVATE (item);

  return GIMP_CANVAS_ITEM_GET_CLASS (item)->get_extents (item, private->shell);
282 283
}

284 285 286 287 288 289 290 291 292 293 294
void
gimp_canvas_item_set_line_cap (GimpCanvasItem   *item,
                               cairo_line_cap_t  line_cap)
{
  g_return_if_fail (GIMP_IS_CANVAS_ITEM (item));

  g_object_set (item,
                "line-cap", line_cap,
                NULL);
}

295 296 297 298 299 300
void
gimp_canvas_item_set_highlight (GimpCanvasItem *item,
                                gboolean        highlight)
{
  g_return_if_fail (GIMP_IS_CANVAS_ITEM (item));

301 302 303
  g_object_set (item,
                "highlight", highlight,
                NULL);
304 305
}

306 307 308 309 310 311 312 313 314 315 316 317
gboolean
gimp_canvas_item_get_highlight (GimpCanvasItem *item)
{
  GimpCanvasItemPrivate *private;

  g_return_val_if_fail (GIMP_IS_CANVAS_ITEM (item), FALSE);

  private = GET_PRIVATE (item);

  return private->highlight;
}

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
void
gimp_canvas_item_suspend_stroking (GimpCanvasItem *item)
{
  GimpCanvasItemPrivate *private;

  g_return_if_fail (GIMP_IS_CANVAS_ITEM (item));

  private = GET_PRIVATE (item);

  private->suspend_stroking++;
}

void
gimp_canvas_item_resume_stroking (GimpCanvasItem *item)
{
  GimpCanvasItemPrivate *private;

  g_return_if_fail (GIMP_IS_CANVAS_ITEM (item));

  private = GET_PRIVATE (item);

  g_return_if_fail (private->suspend_stroking > 0);

  private->suspend_stroking--;
}

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
void
gimp_canvas_item_suspend_filling (GimpCanvasItem *item)
{
  GimpCanvasItemPrivate *private;

  g_return_if_fail (GIMP_IS_CANVAS_ITEM (item));

  private = GET_PRIVATE (item);

  private->suspend_filling++;
}

void
gimp_canvas_item_resume_filling (GimpCanvasItem *item)
{
  GimpCanvasItemPrivate *private;

  g_return_if_fail (GIMP_IS_CANVAS_ITEM (item));

  private = GET_PRIVATE (item);

  g_return_if_fail (private->suspend_filling > 0);

  private->suspend_filling--;
}

370

371
/*  protected functions  */
372

373
void
374 375
_gimp_canvas_item_stroke (GimpCanvasItem *item,
                          cairo_t        *cr)
376
{
377 378
  GimpCanvasItemPrivate *private = GET_PRIVATE (item);

379 380 381
  if (private->suspend_filling > 0)
    g_warning ("_gimp_canvas_item_stroke() on an item that is in a filling group");

382 383
  if (private->suspend_stroking == 0)
    {
384
      GIMP_CANVAS_ITEM_GET_CLASS (item)->stroke (item, private->shell, cr);
385 386 387 388 389
    }
  else
    {
      cairo_new_sub_path (cr);
    }
390 391 392 393 394 395
}

void
_gimp_canvas_item_fill (GimpCanvasItem   *item,
                        cairo_t          *cr)
{
396 397
  GimpCanvasItemPrivate *private = GET_PRIVATE (item);

398 399 400
  if (private->suspend_stroking > 0)
    g_warning ("_gimp_canvas_item_fill() on an item that is in a stroking group");

401 402
  if (private->suspend_filling == 0)
    {
403
      GIMP_CANVAS_ITEM_GET_CLASS (item)->fill (item, private->shell, cr);
404 405 406 407 408
    }
  else
    {
      cairo_new_sub_path (cr);
    }
409
}