gimpcanvasgroup.c 8.63 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 35 36 37
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * gimpcanvasgroup.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 "gimpcanvasgroup.h"
#include "gimpdisplayshell.h"
#include "gimpdisplayshell-transform.h"


enum
{
38
  PROP_0,
39 40
  PROP_GROUP_STROKING,
  PROP_GROUP_FILLING
41 42 43 44 45 46 47
};


typedef struct _GimpCanvasGroupPrivate GimpCanvasGroupPrivate;

struct _GimpCanvasGroupPrivate
{
48 49
  GList    *items;
  gboolean  group_stroking;
50
  gboolean  group_filling;
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
};

#define GET_PRIVATE(group) \
        G_TYPE_INSTANCE_GET_PRIVATE (group, \
                                     GIMP_TYPE_CANVAS_GROUP, \
                                     GimpCanvasGroupPrivate)


/*  local function prototypes  */

static void        gimp_canvas_group_dispose      (GObject          *object);
static void        gimp_canvas_group_set_property (GObject          *object,
                                                   guint             property_id,
                                                   const GValue     *value,
                                                   GParamSpec       *pspec);
static void        gimp_canvas_group_get_property (GObject          *object,
                                                   guint             property_id,
                                                   GValue           *value,
                                                   GParamSpec       *pspec);
static void        gimp_canvas_group_draw         (GimpCanvasItem   *item,
                                                   GimpDisplayShell *shell,
                                                   cairo_t          *cr);
static GdkRegion * gimp_canvas_group_get_extents  (GimpCanvasItem   *item,
                                                   GimpDisplayShell *shell);


G_DEFINE_TYPE (GimpCanvasGroup, gimp_canvas_group, GIMP_TYPE_CANVAS_ITEM)

#define parent_class gimp_canvas_group_parent_class


static void
gimp_canvas_group_class_init (GimpCanvasGroupClass *klass)
{
  GObjectClass        *object_class = G_OBJECT_CLASS (klass);
  GimpCanvasItemClass *item_class   = GIMP_CANVAS_ITEM_CLASS (klass);

  object_class->dispose      = gimp_canvas_group_dispose;
  object_class->set_property = gimp_canvas_group_set_property;
  object_class->get_property = gimp_canvas_group_get_property;

  item_class->draw           = gimp_canvas_group_draw;
  item_class->get_extents    = gimp_canvas_group_get_extents;

95 96 97 98 99 100
  g_object_class_install_property (object_class, PROP_GROUP_STROKING,
                                   g_param_spec_boolean ("group-stroking",
                                                         NULL, NULL,
                                                         FALSE,
                                                         GIMP_PARAM_READWRITE));

101 102 103 104 105 106
  g_object_class_install_property (object_class, PROP_GROUP_FILLING,
                                   g_param_spec_boolean ("group-filling",
                                                         NULL, NULL,
                                                         FALSE,
                                                         GIMP_PARAM_READWRITE));

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
  g_type_class_add_private (klass, sizeof (GimpCanvasGroupPrivate));
}

static void
gimp_canvas_group_init (GimpCanvasGroup *group)
{
}

static void
gimp_canvas_group_dispose (GObject *object)
{
  GimpCanvasGroupPrivate *private = GET_PRIVATE (object);

  if (private->items)
    {
      g_list_foreach (private->items, (GFunc) g_object_unref, NULL);
      g_list_free (private->items);
      private->items = NULL;
    }

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

static void
gimp_canvas_group_set_property (GObject      *object,
                                guint         property_id,
                                const GValue *value,
                                GParamSpec   *pspec)
{
136
  GimpCanvasGroupPrivate *private = GET_PRIVATE (object);
137 138 139

  switch (property_id)
    {
140 141 142
    case PROP_GROUP_STROKING:
      private->group_stroking = g_value_get_boolean (value);
      break;
143 144 145
    case PROP_GROUP_FILLING:
      private->group_filling = g_value_get_boolean (value);
      break;
146

147 148 149 150 151 152 153 154 155 156 157 158
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gimp_canvas_group_get_property (GObject    *object,
                                guint       property_id,
                                GValue     *value,
                                GParamSpec *pspec)
{
159
  GimpCanvasGroupPrivate *private = GET_PRIVATE (object);
160 161 162

  switch (property_id)
    {
163 164 165
    case PROP_GROUP_STROKING:
      g_value_set_boolean (value, private->group_stroking);
      break;
166 167 168
    case PROP_GROUP_FILLING:
      g_value_set_boolean (value, private->group_filling);
      break;
169

170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gimp_canvas_group_draw (GimpCanvasItem   *item,
                        GimpDisplayShell *shell,
                        cairo_t          *cr)
{
  GimpCanvasGroupPrivate *private = GET_PRIVATE (item);
  GList                  *list;

  for (list = private->items; list; list = g_list_next (list))
    {
      GimpCanvasItem *sub_item = list->data;

188
      gimp_canvas_item_draw (sub_item, cr);
189
    }
190 191

  if (private->group_stroking)
192
    _gimp_canvas_item_stroke (item, cr);
193 194

  if (private->group_filling)
195
    _gimp_canvas_item_fill (item, cr);
196 197 198 199 200 201 202 203 204 205 206 207 208
}

static GdkRegion *
gimp_canvas_group_get_extents (GimpCanvasItem   *item,
                               GimpDisplayShell *shell)
{
  GimpCanvasGroupPrivate *private = GET_PRIVATE (item);
  GdkRegion              *region  = NULL;
  GList                  *list;

  for (list = private->items; list; list = g_list_next (list))
    {
      GimpCanvasItem *sub_item   = list->data;
209
      GdkRegion      *sub_region = gimp_canvas_item_get_extents (sub_item);
210

211
      if (! region)
212
        {
213
          region = sub_region;
214
        }
215
      else if (sub_region)
216
        {
217 218
          gdk_region_union (region, sub_region);
          gdk_region_destroy (sub_region);
219 220 221 222 223 224 225
        }
    }

  return region;
}

GimpCanvasItem *
226
gimp_canvas_group_new (GimpDisplayShell *shell)
227
{
228 229 230 231 232
  g_return_val_if_fail (GIMP_IS_DISPLAY_SHELL (shell), NULL);

  return g_object_new (GIMP_TYPE_CANVAS_GROUP,
                       "shell", shell,
                       NULL);
233 234 235 236 237 238 239 240 241 242 243 244 245 246
}

void
gimp_canvas_group_add_item (GimpCanvasGroup *group,
                            GimpCanvasItem  *item)
{
  GimpCanvasGroupPrivate *private;

  g_return_if_fail (GIMP_IS_CANVAS_GROUP (group));
  g_return_if_fail (GIMP_IS_CANVAS_ITEM (item));
  g_return_if_fail (GIMP_CANVAS_ITEM (group) != item);

  private = GET_PRIVATE (group);

247 248 249
  if (private->group_stroking)
    gimp_canvas_item_suspend_stroking (item);

250 251 252
  if (private->group_filling)
    gimp_canvas_item_suspend_filling (item);

253 254
  private->items = g_list_append (private->items, g_object_ref (item));
}
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

void
gimp_canvas_group_remove_item (GimpCanvasGroup *group,
                               GimpCanvasItem  *item)
{
  GimpCanvasGroupPrivate *private;

  g_return_if_fail (GIMP_IS_CANVAS_GROUP (group));
  g_return_if_fail (GIMP_IS_CANVAS_ITEM (item));

  private = GET_PRIVATE (group);

  g_return_if_fail (g_list_find (private->items, item));

  private->items = g_list_remove (private->items, item);
  g_object_unref (item);
}

void
gimp_canvas_group_set_group_stroking (GimpCanvasGroup *group,
                                      gboolean         group_stroking)
{
  g_return_if_fail (GIMP_IS_CANVAS_GROUP (group));

  g_object_set (group,
                "group-stroking", group_stroking ? TRUE : FALSE,
                NULL);
}
283 284 285 286 287 288 289 290 291 292 293

void
gimp_canvas_group_set_group_filling (GimpCanvasGroup *group,
                                     gboolean         group_filling)
{
  g_return_if_fail (GIMP_IS_CANVAS_GROUP (group));

  g_object_set (group,
                "group-filling", group_filling ? TRUE : FALSE,
                NULL);
}