gimpcanvasboundary.c 9.66 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
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimpcanvasboundary.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 <http://www.gnu.org/licenses/>.
 */

#include "config.h"

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

#include "libgimpbase/gimpbase.h"
#include "libgimpmath/gimpmath.h"

#include "display-types.h"

#include "base/boundary.h"

#include "core/gimpparamspecs.h"

35 36
#include "widgets/gimpcairo.h"

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
#include "gimpcanvasboundary.h"
#include "gimpdisplayshell.h"
#include "gimpdisplayshell-transform.h"


enum
{
  PROP_0,
  PROP_SEGS,
  PROP_OFFSET_X,
  PROP_OFFSET_Y
};


typedef struct _GimpCanvasBoundaryPrivate GimpCanvasBoundaryPrivate;

struct _GimpCanvasBoundaryPrivate
{
  BoundSeg *segs;
  gint      n_segs;
  gdouble   offset_x;
  gdouble   offset_y;
};

#define GET_PRIVATE(boundary) \
        G_TYPE_INSTANCE_GET_PRIVATE (boundary, \
                                     GIMP_TYPE_CANVAS_BOUNDARY, \
                                     GimpCanvasBoundaryPrivate)


/*  local function prototypes  */

static void        gimp_canvas_boundary_finalize     (GObject          *object);
static void        gimp_canvas_boundary_set_property (GObject          *object,
                                                      guint             property_id,
                                                      const GValue     *value,
                                                      GParamSpec       *pspec);
static void        gimp_canvas_boundary_get_property (GObject          *object,
                                                      guint             property_id,
                                                      GValue           *value,
                                                      GParamSpec       *pspec);
static void        gimp_canvas_boundary_draw         (GimpCanvasItem   *item,
                                                      GimpDisplayShell *shell,
                                                      cairo_t          *cr);
static GdkRegion * gimp_canvas_boundary_get_extents  (GimpCanvasItem   *item,
                                                      GimpDisplayShell *shell);


G_DEFINE_TYPE (GimpCanvasBoundary, gimp_canvas_boundary,
               GIMP_TYPE_CANVAS_ITEM)

#define parent_class gimp_canvas_boundary_parent_class


static void
gimp_canvas_boundary_class_init (GimpCanvasBoundaryClass *klass)
{
  GObjectClass        *object_class = G_OBJECT_CLASS (klass);
  GimpCanvasItemClass *item_class   = GIMP_CANVAS_ITEM_CLASS (klass);

  object_class->finalize     = gimp_canvas_boundary_finalize;
  object_class->set_property = gimp_canvas_boundary_set_property;
  object_class->get_property = gimp_canvas_boundary_get_property;

  item_class->draw           = gimp_canvas_boundary_draw;
  item_class->get_extents    = gimp_canvas_boundary_get_extents;

  g_object_class_install_property (object_class, PROP_SEGS,
                                   gimp_param_spec_array ("segs", NULL, NULL,
                                                          GIMP_PARAM_READWRITE));

  g_object_class_install_property (object_class, PROP_OFFSET_X,
                                   g_param_spec_double ("offset-x", NULL, NULL,
                                                        -GIMP_MAX_IMAGE_SIZE,
                                                        GIMP_MAX_IMAGE_SIZE, 0,
                                                        GIMP_PARAM_READWRITE));

  g_object_class_install_property (object_class, PROP_OFFSET_Y,
                                   g_param_spec_double ("offset-y", NULL, NULL,
                                                        -GIMP_MAX_IMAGE_SIZE,
                                                        GIMP_MAX_IMAGE_SIZE, 0,
                                                        GIMP_PARAM_READWRITE));

  g_type_class_add_private (klass, sizeof (GimpCanvasBoundaryPrivate));
}

static void
gimp_canvas_boundary_init (GimpCanvasBoundary *boundary)
{
126 127
  gimp_canvas_item_set_line_cap (GIMP_CANVAS_ITEM (boundary),
                                 CAIRO_LINE_CAP_SQUARE);
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
}

static void
gimp_canvas_boundary_finalize (GObject *object)
{
  GimpCanvasBoundaryPrivate *private = GET_PRIVATE (object);

  if (private->segs)
    {
      g_free (private->segs);
      private->segs = NULL;
      private->n_segs = 0;
    }

  G_OBJECT_CLASS (parent_class)->finalize (object);
}

static void
gimp_canvas_boundary_set_property (GObject      *object,
                                   guint         property_id,
                                   const GValue *value,
                                   GParamSpec   *pspec)
{
  GimpCanvasBoundaryPrivate *private = GET_PRIVATE (object);

  switch (property_id)
    {
    case PROP_SEGS:
      break;
    case PROP_OFFSET_X:
      private->offset_x = g_value_get_double (value);
      break;
    case PROP_OFFSET_Y:
      private->offset_y = g_value_get_double (value);
      break;

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

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

  switch (property_id)
    {
    case PROP_SEGS:
      break;
    case PROP_OFFSET_X:
      g_value_set_double (value, private->offset_x);
      break;
    case PROP_OFFSET_Y:
      g_value_set_double (value, private->offset_y);
      break;

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

static void
gimp_canvas_boundary_transform (GimpCanvasItem   *item,
                                GimpDisplayShell *shell,
198
                                GdkSegment       *segs)
199 200 201 202
{
  GimpCanvasBoundaryPrivate *private = GET_PRIVATE (item);
  gint                       i;

203 204 205 206
  gimp_display_shell_transform_segments (shell,
                                         private->segs, segs, private->n_segs,
                                         private->offset_x, private->offset_y);

207 208
  for (i = 0; i < private->n_segs; i++)
    {
209 210 211 212 213 214
      /*  If this segment is a closing segment && the segments lie inside
       *  the region, OR if this is an opening segment and the segments
       *  lie outside the region...
       *  we need to transform it by one display pixel
       */
      if (! private->segs[i].open)
215
        {
216 217 218 219 220 221 222 223 224 225 226
          /*  If it is vertical  */
          if (segs[i].x1 == segs[i].x2)
            {
              segs[i].x1 -= 1;
              segs[i].x2 -= 1;
            }
          else
            {
              segs[i].y1 -= 1;
              segs[i].y2 -= 1;
            }
227 228 229 230 231 232 233 234 235 236
        }
    }
}

static void
gimp_canvas_boundary_draw (GimpCanvasItem   *item,
                           GimpDisplayShell *shell,
                           cairo_t          *cr)
{
  GimpCanvasBoundaryPrivate *private = GET_PRIVATE (item);
237
  GdkSegment                *segs;
238

239
  segs = g_new0 (GdkSegment, private->n_segs);
240 241 242

  gimp_canvas_boundary_transform (item, shell, segs);

243
  gimp_cairo_add_segments (cr, segs, private->n_segs);
244

245
  _gimp_canvas_item_stroke (item, cr);
246 247 248 249 250 251 252 253 254 255

  g_free (segs);
}

static GdkRegion *
gimp_canvas_boundary_get_extents (GimpCanvasItem   *item,
                                  GimpDisplayShell *shell)
{
  GimpCanvasBoundaryPrivate *private = GET_PRIVATE (item);
  GdkRectangle               rectangle;
256
  GdkSegment                *segs;
257 258 259
  gint                       x1, y1, x2, y2;
  gint                       i;

260
  segs = g_new0 (GdkSegment, private->n_segs);
261 262 263

  gimp_canvas_boundary_transform (item, shell, segs);

264 265 266 267
  x1 = MIN (segs[0].x1, segs[0].x2) - 1;
  y1 = MIN (segs[0].y1, segs[0].y2) - 1;
  x2 = MAX (segs[0].x1, segs[0].x2) + 2;
  y2 = MAX (segs[0].y1, segs[0].y2) + 2;
268 269 270

  for (i = 1; i < private->n_segs; i++)
    {
271 272 273 274
      gint x3 = MIN (segs[i].x1, segs[i].x2) - 1;
      gint y3 = MIN (segs[i].y1, segs[i].y2) - 1;
      gint x4 = MAX (segs[i].x1, segs[i].x2) + 2;
      gint y4 = MAX (segs[i].y1, segs[i].y2) + 2;
275 276 277 278 279

      x1 = MIN (x1, x3);
      y1 = MIN (y1, y3);
      x2 = MAX (x2, x4);
      y2 = MAX (y2, y4);
280 281 282 283 284 285 286 287 288 289 290 291 292
    }

  g_free (segs);

  rectangle.x      = x1;
  rectangle.y      = y1;
  rectangle.width  = x2 - x1;
  rectangle.height = y2 - y1;

  return gdk_region_rectangle (&rectangle);
}

GimpCanvasItem *
293 294 295 296 297
gimp_canvas_boundary_new (GimpDisplayShell *shell,
                          const BoundSeg   *segs,
                          gint              n_segs,
                          gdouble           offset_x,
                          gdouble           offset_y)
298 299 300 301
{
  GimpCanvasItem            *item;
  GimpCanvasBoundaryPrivate *private;

302 303
  g_return_val_if_fail (GIMP_IS_DISPLAY_SHELL (shell), NULL);

304
  item = g_object_new (GIMP_TYPE_CANVAS_BOUNDARY,
305
                       "shell",    shell,
306 307 308 309 310 311 312 313 314 315 316
                       "offset-x", offset_x,
                       "offset-y", offset_y,
                       NULL);
  private = GET_PRIVATE (item);

  /* puke */
  private->segs   = g_memdup (segs, n_segs * sizeof (BoundSeg));
  private->n_segs = n_segs;

  return item;
}