gimpcellrendererviewable.c 12.8 KB
Newer Older
1 2 3
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
4
 * gimpcellrendererviewable.c
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 * Copyright (C) 2003 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 2 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, 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 "widgets-types.h"

28
#include "core/gimpmarshal.h"
29 30 31
#include "core/gimpviewable.h"

#include "gimpcellrendererviewable.h"
32
#include "gimppreview-popup.h"
33
#include "gimppreviewrenderer.h"
34 35


36 37 38 39 40 41
enum
{
  CLICKED,
  LAST_SIGNAL
};

42 43 44
enum
{
  PROP_0,
45
  PROP_RENDERER
46 47 48 49 50 51
};


static void gimp_cell_renderer_viewable_class_init (GimpCellRendererViewableClass *klass);
static void gimp_cell_renderer_viewable_init       (GimpCellRendererViewable      *cell);

52
static void gimp_cell_renderer_viewable_finalize     (GObject         *object);
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
static void gimp_cell_renderer_viewable_get_property (GObject         *object,
                                                      guint            param_id,
                                                      GValue          *value,
                                                      GParamSpec      *pspec);
static void gimp_cell_renderer_viewable_set_property (GObject         *object,
                                                      guint            param_id,
                                                      const GValue    *value,
                                                      GParamSpec      *pspec);
static void gimp_cell_renderer_viewable_get_size     (GtkCellRenderer *cell,
                                                      GtkWidget       *widget,
                                                      GdkRectangle    *rectangle,
                                                      gint            *x_offset,
                                                      gint            *y_offset,
                                                      gint            *width,
                                                      gint            *height);
static void gimp_cell_renderer_viewable_render       (GtkCellRenderer *cell,
                                                      GdkWindow       *window,
                                                      GtkWidget       *widget,
                                                      GdkRectangle    *background_area,
                                                      GdkRectangle    *cell_area,
                                                      GdkRectangle    *expose_area,
74 75 76 77 78 79 80 81
                                                      GtkCellRendererState flags);
static gboolean gimp_cell_renderer_viewable_activate (GtkCellRenderer *cell,
                                                      GdkEvent        *event,
                                                      GtkWidget       *widget,
                                                      const gchar     *path,
                                                      GdkRectangle    *background_area,
                                                      GdkRectangle    *cell_area,
                                                      GtkCellRendererState flags);
82 83


84 85
static guint viewable_cell_signals[LAST_SIGNAL] = { 0 };

86
static GtkCellRendererClass *parent_class = NULL;
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108


GType
gimp_cell_renderer_viewable_get_type (void)
{
  static GType cell_type = 0;

  if (! cell_type)
    {
      static const GTypeInfo cell_info =
      {
        sizeof (GimpCellRendererViewableClass),
        NULL,		/* base_init */
        NULL,		/* base_finalize */
        (GClassInitFunc) gimp_cell_renderer_viewable_class_init,
        NULL,		/* class_finalize */
        NULL,		/* class_data */
        sizeof (GimpCellRendererViewable),
        0,              /* n_preallocs */
        (GInstanceInitFunc) gimp_cell_renderer_viewable_init,
      };

109
      cell_type = g_type_register_static (GTK_TYPE_CELL_RENDERER,
110
                                          "GimpCellRendererViewable",
111 112 113 114 115 116 117 118 119
                                          &cell_info, 0);
    }

  return cell_type;
}

static void
gimp_cell_renderer_viewable_class_init (GimpCellRendererViewableClass *klass)
{
Michael Natterer's avatar
Michael Natterer committed
120 121
  GObjectClass         *object_class = G_OBJECT_CLASS (klass);
  GtkCellRendererClass *cell_class   = GTK_CELL_RENDERER_CLASS (klass);
122 123 124

  parent_class = g_type_class_peek_parent (klass);

125 126
  viewable_cell_signals[CLICKED] =
    g_signal_new ("clicked",
Michael Natterer's avatar
Michael Natterer committed
127 128 129 130 131 132 133
                  G_OBJECT_CLASS_TYPE (object_class),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (GimpCellRendererViewableClass, clicked),
                  NULL, NULL,
                  gimp_marshal_VOID__STRING_FLAGS,
                  G_TYPE_NONE, 2,
                  G_TYPE_STRING,
134
                  GDK_TYPE_MODIFIER_TYPE);
135 136

  object_class->finalize     = gimp_cell_renderer_viewable_finalize;
137 138 139 140 141
  object_class->get_property = gimp_cell_renderer_viewable_get_property;
  object_class->set_property = gimp_cell_renderer_viewable_set_property;

  cell_class->get_size       = gimp_cell_renderer_viewable_get_size;
  cell_class->render         = gimp_cell_renderer_viewable_render;
142
  cell_class->activate       = gimp_cell_renderer_viewable_activate;
143

144 145
  klass->clicked             = NULL;

146
  g_object_class_install_property (object_class,
147 148
                                   PROP_RENDERER,
                                   g_param_spec_object ("renderer",
149
                                                        NULL, NULL,
150
                                                        GIMP_TYPE_PREVIEW_RENDERER,
151 152 153 154 155 156
                                                        G_PARAM_READWRITE));
}

static void
gimp_cell_renderer_viewable_init (GimpCellRendererViewable *cellviewable)
{
157
  GTK_CELL_RENDERER (cellviewable)->mode = GTK_CELL_RENDERER_MODE_ACTIVATABLE;
158 159
}

160 161 162
static void
gimp_cell_renderer_viewable_finalize (GObject *object)
{
Michael Natterer's avatar
Michael Natterer committed
163
  GimpCellRendererViewable *cell = GIMP_CELL_RENDERER_VIEWABLE (object);
164 165 166 167 168 169 170 171 172 173

  if (cell->renderer)
    {
      g_object_unref (cell->renderer);
      cell->renderer = NULL;
    }

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

174 175 176 177 178 179
static void
gimp_cell_renderer_viewable_get_property (GObject    *object,
                                          guint       param_id,
                                          GValue     *value,
                                          GParamSpec *pspec)
{
Michael Natterer's avatar
Michael Natterer committed
180
  GimpCellRendererViewable *cell = GIMP_CELL_RENDERER_VIEWABLE (object);
181 182 183

  switch (param_id)
    {
184 185
    case PROP_RENDERER:
      g_value_set_object (value, cell->renderer);
186 187 188 189 190 191 192 193 194 195 196 197 198 199
      break;

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

static void
gimp_cell_renderer_viewable_set_property (GObject      *object,
                                          guint         param_id,
                                          const GValue *value,
                                          GParamSpec   *pspec)
{
Michael Natterer's avatar
Michael Natterer committed
200
  GimpCellRendererViewable *cell = GIMP_CELL_RENDERER_VIEWABLE (object);
201 202 203

  switch (param_id)
    {
204
    case PROP_RENDERER:
Michael Natterer's avatar
Michael Natterer committed
205 206 207 208 209 210 211 212
      {
        GimpPreviewRenderer *renderer;

        renderer = (GimpPreviewRenderer *) g_value_dup_object (value);
        if (cell->renderer)
          g_object_unref (cell->renderer);
        cell->renderer = renderer;
      }
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
      break;

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

static void
gimp_cell_renderer_viewable_get_size (GtkCellRenderer *cell,
                                      GtkWidget       *widget,
                                      GdkRectangle    *cell_area,
                                      gint            *x_offset,
                                      gint            *y_offset,
                                      gint            *width,
                                      gint            *height)
{
  GimpCellRendererViewable *cellviewable;
  gint                      preview_width  = 0;
  gint                      preview_height = 0;
  gint                      calc_width;
  gint                      calc_height;

  cellviewable = GIMP_CELL_RENDERER_VIEWABLE (cell);

238
  if (cellviewable->renderer)
239
    {
240 241 242 243
      preview_width  = (cellviewable->renderer->width  +
                        2 * cellviewable->renderer->border_width);
      preview_height = (cellviewable->renderer->height +
                        2 * cellviewable->renderer->border_width);
244
    }
245

246 247
  calc_width  = (gint) cell->xpad * 2 + preview_width;
  calc_height = (gint) cell->ypad * 2 + preview_height;
248

249 250 251 252 253 254 255
  if (x_offset) *x_offset = 0;
  if (y_offset) *y_offset = 0;

  if (cell_area && preview_width > 0 && preview_height > 0)
    {
      if (x_offset)
	{
256 257
	  *x_offset = (((gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL) ?
                        1.0 - cell->xalign : cell->xalign) *
258 259
                       (cell_area->width - calc_width - 2 * cell->xpad));
	  *x_offset = (MAX (*x_offset, 0) + cell->xpad);
260 261 262
	}
      if (y_offset)
	{
263 264 265
	  *y_offset = (cell->yalign *
                       (cell_area->height - calc_height - 2 * cell->ypad));
	  *y_offset = (MAX (*y_offset, 0) + cell->ypad);
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
	}
    }

  if (width)  *width  = calc_width;
  if (height) *height = calc_height;
}

static void
gimp_cell_renderer_viewable_render (GtkCellRenderer      *cell,
                                    GdkWindow            *window,
                                    GtkWidget            *widget,
                                    GdkRectangle         *background_area,
                                    GdkRectangle         *cell_area,
                                    GdkRectangle         *expose_area,
                                    GtkCellRendererState  flags)
{
  GimpCellRendererViewable *cellviewable;

  cellviewable = GIMP_CELL_RENDERER_VIEWABLE (cell);

286
  if (cellviewable->renderer)
287
    {
288
      if (! (flags & GTK_CELL_RENDERER_SELECTED))
289 290 291 292 293 294 295 296 297 298 299
        {
          GimpRGB black = { 0.0, 0.0, 0.0, 1.0 };

          gimp_preview_renderer_set_border_color (cellviewable->renderer,
                                                  &black);
          gimp_preview_renderer_remove_idle (cellviewable->renderer);
        }

      gimp_preview_renderer_draw (cellviewable->renderer, window, widget,
                                  cell_area, expose_area);
    }
300 301
}

302 303 304 305 306 307 308 309 310 311 312 313 314
static gboolean
gimp_cell_renderer_viewable_activate (GtkCellRenderer      *cell,
                                      GdkEvent             *event,
                                      GtkWidget            *widget,
                                      const gchar          *path,
                                      GdkRectangle         *background_area,
                                      GdkRectangle         *cell_area,
                                      GtkCellRendererState  flags)
{
  GimpCellRendererViewable *cellviewable;

  cellviewable = GIMP_CELL_RENDERER_VIEWABLE (cell);

315
  if (cellviewable->renderer)
316
    {
317 318 319 320 321 322 323 324
      GdkModifierType state = 0;

      if (event && ((GdkEventAny *) event)->type == GDK_BUTTON_PRESS)
        state = ((GdkEventButton *) event)->state;

      if (! event ||
          (((GdkEventAny *) event)->type == GDK_BUTTON_PRESS &&
           ((GdkEventButton *) event)->button == 1))
325
        {
326 327 328
          gimp_cell_renderer_viewable_clicked (cellviewable, path, state);

          return TRUE;
329
        }
330 331 332 333 334
    }

  return FALSE;
}

335 336 337 338 339
GtkCellRenderer *
gimp_cell_renderer_viewable_new (void)
{
  return g_object_new (GIMP_TYPE_CELL_RENDERER_VIEWABLE, NULL);
}
340 341 342 343 344 345

void
gimp_cell_renderer_viewable_clicked (GimpCellRendererViewable *cell,
                                     const gchar              *path,
                                     GdkModifierType           state)
{
346 347
  GdkEvent *event;

348 349 350 351
  g_return_if_fail (GIMP_IS_CELL_RENDERER_VIEWABLE (cell));
  g_return_if_fail (path != NULL);

  g_signal_emit (cell, viewable_cell_signals[CLICKED], 0, path, state);
352 353 354

  event = gtk_get_current_event ();

355 356 357 358 359 360 361 362 363 364 365 366 367
  if (event)
    {
      if (((GdkEventAny *) event)->type == GDK_BUTTON_PRESS &&
          ((GdkEventButton *) event)->button == 1)
        gimp_preview_popup_show (gtk_get_event_widget (event),
                                 (GdkEventButton *) event,
                                 cell->renderer->viewable,
                                 cell->renderer->width,
                                 cell->renderer->height,
                                 TRUE);

      gdk_event_free (event);
    }
368
}