gimpgrid.c 10.1 KB
Newer Older
1
/* GIMP - The GNU Image Manipulation Program
2 3
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
4 5 6
 * GimpGrid
 * Copyright (C) 2003  Henrik Brix Andersen <brix@gimp.org>
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 3 of the License, or
10 11 12 13 14 15 16 17
 * (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
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19 20 21 22
 */

#include "config.h"

23
#include <string.h>
24

25
#include <cairo.h>
26
#include <gegl.h>
27
#include <gdk-pixbuf/gdk-pixbuf.h>
28

29
#include "libgimpmath/gimpmath.h"
30
#include "libgimpbase/gimpbase.h"
31
#include "libgimpconfig/gimpconfig.h"
32

33 34 35 36 37 38 39 40 41 42 43 44
#include "libgimpcolor/gimpcolor.h"

#include "core-types.h"

#include "gimpgrid.h"

#include "gimp-intl.h"


enum
{
  PROP_0,
45 46 47
  PROP_STYLE,
  PROP_FGCOLOR,
  PROP_BGCOLOR,
48 49 50 51 52
  PROP_XSPACING,
  PROP_YSPACING,
  PROP_SPACING_UNIT,
  PROP_XOFFSET,
  PROP_YOFFSET,
53
  PROP_OFFSET_UNIT
54 55 56
};


57 58 59 60 61 62 63 64
static void   gimp_grid_get_property (GObject      *object,
                                      guint         property_id,
                                      GValue       *value,
                                      GParamSpec   *pspec);
static void   gimp_grid_set_property (GObject      *object,
                                      guint         property_id,
                                      const GValue *value,
                                      GParamSpec   *pspec);
65 66


67
G_DEFINE_TYPE_WITH_CODE (GimpGrid, gimp_grid, GIMP_TYPE_OBJECT,
68
                         G_IMPLEMENT_INTERFACE (GIMP_TYPE_CONFIG, NULL))
69 70 71 72 73


static void
gimp_grid_class_init (GimpGridClass *klass)
{
74
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
75 76 77 78 79 80 81 82 83
  GimpRGB       black;
  GimpRGB       white;

  object_class->get_property = gimp_grid_get_property;
  object_class->set_property = gimp_grid_set_property;

  gimp_rgba_set (&black, 0.0, 0.0, 0.0, GIMP_OPACITY_OPAQUE);
  gimp_rgba_set (&white, 1.0, 1.0, 1.0, GIMP_OPACITY_OPAQUE);

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
  GIMP_CONFIG_PROP_ENUM (object_class, PROP_STYLE,
                         "style",
                         _("Line style"),
                         _("Line style used for the grid."),
                         GIMP_TYPE_GRID_STYLE,
                         GIMP_GRID_SOLID,
                         GIMP_PARAM_STATIC_STRINGS);

  GIMP_CONFIG_PROP_RGB (object_class, PROP_FGCOLOR,
                        "fgcolor",
                        _("Foreground color"),
                        _("The foreground color of the grid."),
                        TRUE, &black,
                        GIMP_PARAM_STATIC_STRINGS);

  GIMP_CONFIG_PROP_RGB (object_class, PROP_BGCOLOR,
                        "bgcolor",
                        _("Background color"),
                        _("The background color of the grid; "
                          "only used in double dashed line style."),
                        TRUE, &white,
                        GIMP_PARAM_STATIC_STRINGS);

  GIMP_CONFIG_PROP_DOUBLE (object_class, PROP_XSPACING,
                           "xspacing",
                           _("Spacing X"),
                           _("Horizontal spacing of grid lines."),
111
                           0.0, GIMP_MAX_IMAGE_SIZE, 10.0,
112 113 114 115 116 117
                           GIMP_PARAM_STATIC_STRINGS);

  GIMP_CONFIG_PROP_DOUBLE (object_class, PROP_YSPACING,
                           "yspacing",
                           _("Spacing Y"),
                           _("Vertical spacing of grid lines."),
118
                           0.0, GIMP_MAX_IMAGE_SIZE, 10.0,
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
                           GIMP_PARAM_STATIC_STRINGS);

  GIMP_CONFIG_PROP_UNIT (object_class, PROP_SPACING_UNIT,
                         "spacing-unit",
                         _("Spacing unit"),
                         NULL,
                         FALSE, FALSE, GIMP_UNIT_INCH,
                         GIMP_PARAM_STATIC_STRINGS);

  GIMP_CONFIG_PROP_DOUBLE (object_class, PROP_XOFFSET,
                           "xoffset",
                           _("Offset X"),
                           _("Horizontal offset of the first grid "
                             "line; this may be a negative number."),
                           - GIMP_MAX_IMAGE_SIZE,
                           GIMP_MAX_IMAGE_SIZE, 0.0,
                           GIMP_PARAM_STATIC_STRINGS);

  GIMP_CONFIG_PROP_DOUBLE (object_class, PROP_YOFFSET,
                           "yoffset",
                           _("Offset Y"),
                           _("Vertical offset of the first grid "
                             "line; this may be a negative number."),
                           - GIMP_MAX_IMAGE_SIZE,
                           GIMP_MAX_IMAGE_SIZE, 0.0,
                           GIMP_PARAM_STATIC_STRINGS);

  GIMP_CONFIG_PROP_UNIT (object_class, PROP_OFFSET_UNIT,
                         "offset-unit",
                         _("Offset unit"),
                         NULL,
                         FALSE, FALSE, GIMP_UNIT_INCH,
                         GIMP_PARAM_STATIC_STRINGS);
152 153 154
}

static void
155
gimp_grid_init (GimpGrid *grid)
156 157 158 159 160 161 162 163 164 165 166 167 168
{
}

static void
gimp_grid_get_property (GObject      *object,
                        guint         property_id,
                        GValue       *value,
                        GParamSpec   *pspec)
{
  GimpGrid *grid = GIMP_GRID (object);

  switch (property_id)
    {
169 170 171 172 173 174 175 176 177
    case PROP_STYLE:
      g_value_set_enum (value, grid->style);
      break;
    case PROP_FGCOLOR:
      g_value_set_boxed (value, &grid->fgcolor);
      break;
    case PROP_BGCOLOR:
      g_value_set_boxed (value, &grid->bgcolor);
      break;
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
    case PROP_XSPACING:
      g_value_set_double (value, grid->xspacing);
      break;
    case PROP_YSPACING:
      g_value_set_double (value, grid->yspacing);
      break;
    case PROP_SPACING_UNIT:
      g_value_set_int (value, grid->spacing_unit);
      break;
    case PROP_XOFFSET:
      g_value_set_double (value, grid->xoffset);
      break;
    case PROP_YOFFSET:
      g_value_set_double (value, grid->yoffset);
      break;
    case PROP_OFFSET_UNIT:
      g_value_set_int (value, grid->offset_unit);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
gimp_grid_set_property (GObject      *object,
                        guint         property_id,
                        const GValue *value,
                        GParamSpec   *pspec)
{
  GimpGrid *grid = GIMP_GRID (object);
  GimpRGB  *color;

  switch (property_id)
    {
213 214 215 216 217 218 219 220 221 222 223
    case PROP_STYLE:
      grid->style = g_value_get_enum (value);
      break;
    case PROP_FGCOLOR:
      color = g_value_get_boxed (value);
      grid->fgcolor = *color;
      break;
    case PROP_BGCOLOR:
      color = g_value_get_boxed (value);
      grid->bgcolor = *color;
      break;
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
    case PROP_XSPACING:
      grid->xspacing = g_value_get_double (value);
      break;
    case PROP_YSPACING:
      grid->yspacing = g_value_get_double (value);
      break;
    case PROP_SPACING_UNIT:
      grid->spacing_unit = g_value_get_int (value);
      break;
    case PROP_XOFFSET:
      grid->xoffset = g_value_get_double (value);
      break;
    case PROP_YOFFSET:
      grid->yoffset = g_value_get_double (value);
      break;
    case PROP_OFFSET_UNIT:
      grid->offset_unit = g_value_get_int (value);
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}
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

/*  public functions  */

GimpGridStyle
gimp_grid_get_style (GimpGrid *grid)
{
  g_return_val_if_fail (GIMP_IS_GRID (grid), GIMP_GRID_SOLID);

  return grid->style;
}

void
gimp_grid_get_fgcolor (GimpGrid *grid,
                       GimpRGB  *fgcolor)
{
  g_return_if_fail (GIMP_IS_GRID (grid));
  g_return_if_fail (fgcolor != NULL);

  *fgcolor = grid->fgcolor;
}

void
gimp_grid_get_bgcolor (GimpGrid *grid,
                       GimpRGB  *bgcolor)
{
  g_return_if_fail (GIMP_IS_GRID (grid));
  g_return_if_fail (bgcolor != NULL);

  *bgcolor = grid->bgcolor;
}

279 280 281 282 283 284 285
void
gimp_grid_get_spacing (GimpGrid *grid,
                       gdouble  *xspacing,
                       gdouble  *yspacing)
{
  g_return_if_fail (GIMP_IS_GRID (grid));

286 287
  if (xspacing) *xspacing = grid->xspacing;
  if (yspacing) *yspacing = grid->yspacing;
288 289 290 291 292 293 294 295 296
}

void
gimp_grid_get_offset (GimpGrid *grid,
                      gdouble  *xoffset,
                      gdouble  *yoffset)
{
  g_return_if_fail (GIMP_IS_GRID (grid));

297 298
  if (xoffset) *xoffset = grid->xoffset;
  if (yoffset) *yoffset = grid->yoffset;
299 300
}

301 302 303 304 305 306 307
const gchar *
gimp_grid_parasite_name (void)
{
  return "gimp-image-grid";
}

GimpParasite *
308
gimp_grid_to_parasite (GimpGrid *grid)
309 310 311 312 313 314
{
  GimpParasite *parasite;
  gchar        *str;

  g_return_val_if_fail (GIMP_IS_GRID (grid), NULL);

315
  str = gimp_config_serialize_to_string (GIMP_CONFIG (grid), NULL);
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337
  g_return_val_if_fail (str != NULL, NULL);

  parasite = gimp_parasite_new (gimp_grid_parasite_name (),
                                GIMP_PARASITE_PERSISTENT,
                                strlen (str) + 1, str);
  g_free (str);

  return parasite;
}

GimpGrid *
gimp_grid_from_parasite (const GimpParasite *parasite)
{
  GimpGrid    *grid;
  const gchar *str;
  GError      *error = NULL;

  g_return_val_if_fail (parasite != NULL, NULL);
  g_return_val_if_fail (strcmp (gimp_parasite_name (parasite),
                                gimp_grid_parasite_name ()) == 0, NULL);

  str = gimp_parasite_data (parasite);
338 339 340 341 342 343 344

  if (! str)
    {
      g_warning ("Empty grid parasite");

      return NULL;
    }
345 346 347

  grid = g_object_new (GIMP_TYPE_GRID, NULL);

348
  if (! gimp_config_deserialize_string (GIMP_CONFIG (grid),
349 350 351 352 353 354 355 356 357 358 359
                                        str,
                                        gimp_parasite_data_size (parasite),
                                        NULL,
                                        &error))
    {
      g_warning ("Failed to deserialize grid parasite: %s", error->message);
      g_error_free (error);
    }

  return grid;
}