gimpaspectpreview.c 12.6 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
/* LIBGIMP - The GIMP Library
 * Copyright (C) 1995-1997 Peter Mattis and Spencer Kimball
 *
 * gimpaspectpreview.c
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include <gtk/gtk.h>

#include "libgimpwidgets/gimpwidgets.h"

#include "gimpuitypes.h"

#include "gimp.h"

#include "libgimp-intl.h"

#include "gimpaspectpreview.h"


37 38 39 40 41 42
enum
{
  PROP_0,
  PROP_DRAWABLE
};

Sven Neumann's avatar
Sven Neumann committed
43 44 45 46 47 48 49 50 51 52
typedef struct
{
  gboolean  update;
} PreviewSettings;


static GObject * gimp_aspect_preview_constructor (GType                  type,
                                                  guint                  n_params,
                                                  GObjectConstructParam *params);

53 54

static void  gimp_aspect_preview_get_property (GObject         *object,
Sven Neumann's avatar
Sven Neumann committed
55 56 57
                                               guint            property_id,
                                               GValue          *value,
                                               GParamSpec      *pspec);
58
static void  gimp_aspect_preview_set_property (GObject         *object,
Sven Neumann's avatar
Sven Neumann committed
59 60 61
                                               guint            property_id,
                                               const GValue    *value,
                                               GParamSpec      *pspec);
Sven Neumann's avatar
Sven Neumann committed
62
static void  gimp_aspect_preview_destroy      (GtkObject       *object);
63 64 65 66 67 68
static void  gimp_aspect_preview_style_set    (GtkWidget       *widget,
                                               GtkStyle        *prev_style);
static void  gimp_aspect_preview_draw         (GimpPreview     *preview);
static void  gimp_aspect_preview_draw_buffer  (GimpPreview     *preview,
                                               const guchar    *buffer,
                                               gint             rowstride);
69 70 71 72 73 74 75 76 77 78
static void  gimp_aspect_preview_transform    (GimpPreview     *preview,
                                               gint             src_x,
                                               gint             src_y,
                                               gint            *dest_x,
                                               gint            *dest_y);
static void  gimp_aspect_preview_untransform  (GimpPreview     *preview,
                                               gint             src_x,
                                               gint             src_y,
                                               gint            *dest_x,
                                               gint            *dest_y);
79 80

static void  gimp_aspect_preview_set_drawable (GimpAspectPreview *preview,
Sven Neumann's avatar
Sven Neumann committed
81
                                               GimpDrawable      *drawable);
82

83

84
G_DEFINE_TYPE (GimpAspectPreview, gimp_aspect_preview, GIMP_TYPE_PREVIEW)
85

86
#define parent_class gimp_aspect_preview_parent_class
87

Sven Neumann's avatar
Sven Neumann committed
88 89
static gint gimp_aspect_preview_counter = 0;

90 91 92 93

static void
gimp_aspect_preview_class_init (GimpAspectPreviewClass *klass)
{
Sven Neumann's avatar
Sven Neumann committed
94 95 96 97
  GObjectClass     *object_class     = G_OBJECT_CLASS (klass);
  GtkObjectClass   *gtk_object_class = GTK_OBJECT_CLASS (klass);
  GtkWidgetClass   *widget_class     = GTK_WIDGET_CLASS (klass);
  GimpPreviewClass *preview_class    = GIMP_PREVIEW_CLASS (klass);
98

Sven Neumann's avatar
Sven Neumann committed
99
  object_class->constructor  = gimp_aspect_preview_constructor;
100 101 102
  object_class->get_property = gimp_aspect_preview_get_property;
  object_class->set_property = gimp_aspect_preview_set_property;

Sven Neumann's avatar
Sven Neumann committed
103 104
  gtk_object_class->destroy  = gimp_aspect_preview_destroy;

105 106 107 108
  widget_class->style_set    = gimp_aspect_preview_style_set;

  preview_class->draw        = gimp_aspect_preview_draw;
  preview_class->draw_buffer = gimp_aspect_preview_draw_buffer;
109 110
  preview_class->transform   = gimp_aspect_preview_transform;
  preview_class->untransform = gimp_aspect_preview_untransform;
111 112 113 114 115 116 117 118

  /**
   * GimpAspectPreview:drawable:
   *
   * Since: GIMP 2.4
   */
  g_object_class_install_property (object_class, PROP_DRAWABLE,
                                   g_param_spec_pointer ("drawable", NULL, NULL,
Sven Neumann's avatar
Sven Neumann committed
119 120
                                                         GIMP_PARAM_READWRITE |
                                                         G_PARAM_CONSTRUCT_ONLY));
121 122 123 124 125 126 127 128 129 130 131
}

static void
gimp_aspect_preview_init (GimpAspectPreview *preview)
{
  g_object_set (GIMP_PREVIEW (preview)->area,
                "check-size", gimp_check_size (),
                "check-type", gimp_check_type (),
                NULL);
}

Sven Neumann's avatar
Sven Neumann committed
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
static GObject *
gimp_aspect_preview_constructor (GType                  type,
                                 guint                  n_params,
                                 GObjectConstructParam *params)
{
  GObject         *object;
  gchar           *data_name;
  PreviewSettings  settings;

  data_name = g_strdup_printf ("%s-aspect-preview-%d",
                               g_get_prgname (),
                               gimp_aspect_preview_counter++);

  object = G_OBJECT_CLASS (parent_class)->constructor (type, n_params, params);

  if (gimp_get_data (data_name, &settings))
    {
      gimp_preview_set_update (GIMP_PREVIEW (object), settings.update);
    }

  g_object_set_data_full (object, "gimp-aspect-preview-data-name",
                          data_name, (GDestroyNotify) g_free);

  return object;
}

158 159
static void
gimp_aspect_preview_get_property (GObject    *object,
Sven Neumann's avatar
Sven Neumann committed
160 161 162
                                  guint       property_id,
                                  GValue     *value,
                                  GParamSpec *pspec)
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
{
  GimpAspectPreview *preview = GIMP_ASPECT_PREVIEW (object);

  switch (property_id)
    {
    case PROP_DRAWABLE:
      g_value_set_pointer (value, preview->drawable);
      break;

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

static void
gimp_aspect_preview_set_property (GObject      *object,
Sven Neumann's avatar
Sven Neumann committed
180 181 182
                                    guint         property_id,
                                    const GValue *value,
                                    GParamSpec   *pspec)
183 184 185 186 187 188 189
{
  GimpAspectPreview *preview = GIMP_ASPECT_PREVIEW (object);

  switch (property_id)
    {
    case PROP_DRAWABLE:
      gimp_aspect_preview_set_drawable (preview,
Sven Neumann's avatar
Sven Neumann committed
190
                                        g_value_get_pointer (value));
191 192 193 194 195 196 197 198
      break;

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

Sven Neumann's avatar
Sven Neumann committed
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
static void
gimp_aspect_preview_destroy (GtkObject *object)
{
  const gchar *data_name = g_object_get_data (G_OBJECT (object),
                                              "gimp-aspect-preview-data-name");

  if (data_name)
    {
      GimpPreview     *preview = GIMP_PREVIEW (object);
      PreviewSettings  settings;

      settings.update = gimp_preview_get_update (preview);

      gimp_set_data (data_name, &settings, sizeof (PreviewSettings));
    }

  GTK_OBJECT_CLASS (parent_class)->destroy (object);
}

218 219 220 221
static void
gimp_aspect_preview_style_set (GtkWidget *widget,
                               GtkStyle  *prev_style)
{
222 223 224
  GimpPreview  *preview  = GIMP_PREVIEW (widget);
  GimpDrawable *drawable = GIMP_ASPECT_PREVIEW (preview)->drawable;
  gint          size;
225 226 227 228 229 230 231 232

  if (GTK_WIDGET_CLASS (parent_class)->style_set)
    GTK_WIDGET_CLASS (parent_class)->style_set (widget, prev_style);

  gtk_widget_style_get (widget,
                        "size", &size,
                        NULL);

233
  if (drawable->width > drawable->height)
234
    {
235 236
      preview->width  = MIN (drawable->width, size);
      preview->height = (drawable->height * preview->width) / drawable->width;
237 238 239
    }
  else
    {
240 241
      preview->height = MIN (drawable->height, size);
      preview->width  = (drawable->width * preview->height) / drawable->height;
242
    }
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265

  gtk_widget_set_size_request (preview->area,
                               preview->width, preview->height);
}


static void
gimp_aspect_preview_draw (GimpPreview *preview)
{
  g_return_if_fail (GIMP_IS_ASPECT_PREVIEW (preview));

  gimp_preview_area_fill (GIMP_PREVIEW_AREA (preview->area),
                          0, 0,
                          preview->width,
                          preview->height,
                          0, 0, 0);
}

static void
gimp_aspect_preview_draw_buffer (GimpPreview  *preview,
                                 const guchar *buffer,
                                 gint          rowstride)
{
266
  GimpDrawable *drawable = GIMP_ASPECT_PREVIEW (preview)->drawable;
267 268 269
  gint32        image_id;

  image_id = gimp_drawable_get_image (drawable->drawable_id);
270

271 272 273 274 275 276 277 278 279 280 281 282 283 284
  if (gimp_selection_is_empty (image_id))
    {
      gimp_preview_area_draw (GIMP_PREVIEW_AREA (preview->area),
                              0, 0,
                              preview->width, preview->height,
                              gimp_drawable_type (drawable->drawable_id),
                              buffer,
                              rowstride);
    }
  else
    {
      guchar *sel;
      guchar *src;
      gint    selection_id;
285 286
      gint    width, height;
      gint    bpp;
287 288 289 290 291

      selection_id = gimp_image_get_selection (image_id);

      width  = preview->width;
      height = preview->height;
292

293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
      src = gimp_drawable_get_thumbnail_data (drawable->drawable_id,
                                              &width, &height, &bpp);
      sel = gimp_drawable_get_thumbnail_data (selection_id,
                                              &width, &height, &bpp);

      gimp_preview_area_mask (GIMP_PREVIEW_AREA (preview->area),
                              0, 0, preview->width, preview->height,
                              gimp_drawable_type (drawable->drawable_id),
                              src, width * drawable->bpp,
                              buffer, rowstride,
                              sel, width);

      g_free (sel);
      g_free (src);
    }
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
static void
gimp_aspect_preview_transform (GimpPreview *preview,
                               gint         src_x,
                               gint         src_y,
                               gint        *dest_x,
                               gint        *dest_y)
{
  GimpDrawable *drawable = GIMP_ASPECT_PREVIEW (preview)->drawable;

  *dest_x = (gdouble) src_x * preview->width / drawable->width;
  *dest_y = (gdouble) src_y * preview->height / drawable->height;
}

static void
gimp_aspect_preview_untransform (GimpPreview *preview,
                                 gint         src_x,
                                 gint         src_y,
                                 gint        *dest_x,
                                 gint        *dest_y)
{
  GimpDrawable *drawable = GIMP_ASPECT_PREVIEW (preview)->drawable;

  *dest_x = (gdouble) src_x * drawable->width / preview->width;
  *dest_y = (gdouble) src_y * drawable->height / preview->height;
}

336 337
static void
gimp_aspect_preview_set_drawable (GimpAspectPreview *preview,
Sven Neumann's avatar
Sven Neumann committed
338
                                  GimpDrawable      *drawable)
339
{
340 341
  gint width;
  gint height;
342 343 344

  preview->drawable = drawable;

345
  if (drawable->width > drawable->height)
346
    {
347 348
      width  = MIN (drawable->width, 512);
      height = (drawable->height * width) / drawable->width;
349 350 351
    }
  else
    {
352 353
      height = MIN (drawable->height, 512);
      width  = (drawable->width * height) / drawable->height;
354
    }
355
  gimp_preview_set_bounds (GIMP_PREVIEW (preview), 0, 0, width, height);
356 357 358

  if (height > 0)
    g_object_set (GIMP_PREVIEW (preview)->frame,
359 360
                  "ratio",
                  (gdouble) drawable->width / (gdouble) drawable->height,
Sven Neumann's avatar
Sven Neumann committed
361
                  NULL);
362 363
}

364 365 366
/**
 * gimp_aspect_preview_new:
 * @drawable: a #GimpDrawable
Sven Neumann's avatar
Sven Neumann committed
367
 * @toggle:   unused
368 369 370 371
 *
 * Creates a new #GimpAspectPreview widget for @drawable. See also
 * gimp_drawable_preview_new().
 *
Sven Neumann's avatar
Sven Neumann committed
372 373 374 375 376
 * In GIMP 2.2 the @toggle parameter was provided to conviently access
 * the state of the "Preview" check-button. This is not any longer
 * necessary as the preview itself now stores this state, as well as
 * the scroll offset.
 *
377 378
 * Since: GIMP 2.2
 **/
379 380 381 382
GtkWidget *
gimp_aspect_preview_new (GimpDrawable *drawable,
                         gboolean     *toggle)
{
383
  g_return_val_if_fail (drawable != NULL, NULL);
384

Sven Neumann's avatar
Sven Neumann committed
385 386 387
  return g_object_new (GIMP_TYPE_ASPECT_PREVIEW,
                       "drawable", drawable,
                       NULL);
388
}