gimpcellrendererviewable.c 12 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 120 121 122 123 124 125 126 127
                                          &cell_info, 0);
    }

  return cell_type;
}

static void
gimp_cell_renderer_viewable_class_init (GimpCellRendererViewableClass *klass)
{
  GObjectClass         *object_class;
  GtkCellRendererClass *cell_class;

  object_class = G_OBJECT_CLASS (klass);
  cell_class   = GTK_CELL_RENDERER_CLASS (klass);

  parent_class = g_type_class_peek_parent (klass);

128 129 130 131 132 133 134 135 136 137 138 139
  viewable_cell_signals[CLICKED] =
    g_signal_new ("clicked",
		  G_OBJECT_CLASS_TYPE (object_class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GimpCellRendererViewableClass, clicked),
		  NULL, NULL,
		  gimp_marshal_VOID__STRING_UINT,
		  G_TYPE_NONE, 2,
		  G_TYPE_STRING,
                  G_TYPE_UINT);

  object_class->finalize     = gimp_cell_renderer_viewable_finalize;
140 141 142 143 144
  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;
145
  cell_class->activate       = gimp_cell_renderer_viewable_activate;
146

147 148
  klass->clicked             = NULL;

149
  g_object_class_install_property (object_class,
150 151
                                   PROP_RENDERER,
                                   g_param_spec_object ("renderer",
152
                                                        NULL, NULL,
153
                                                        GIMP_TYPE_PREVIEW_RENDERER,
154 155 156 157 158 159
                                                        G_PARAM_READWRITE));
}

static void
gimp_cell_renderer_viewable_init (GimpCellRendererViewable *cellviewable)
{
160
  GTK_CELL_RENDERER (cellviewable)->mode = GTK_CELL_RENDERER_MODE_ACTIVATABLE;
161 162
}

163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
static void
gimp_cell_renderer_viewable_finalize (GObject *object)
{
  GimpCellRendererViewable *cell;

  cell = GIMP_CELL_RENDERER_VIEWABLE (object);

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

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

179 180 181 182 183 184 185 186 187 188 189 190
static void
gimp_cell_renderer_viewable_get_property (GObject    *object,
                                          guint       param_id,
                                          GValue     *value,
                                          GParamSpec *pspec)
{
  GimpCellRendererViewable *cell;

  cell = GIMP_CELL_RENDERER_VIEWABLE (object);

  switch (param_id)
    {
191 192
    case PROP_RENDERER:
      g_value_set_object (value, cell->renderer);
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
      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)
{
  GimpCellRendererViewable *cell;
  
  cell = GIMP_CELL_RENDERER_VIEWABLE (object);

  switch (param_id)
    {
213 214 215 216
    case PROP_RENDERER:
      if (cell->renderer)
	g_object_unref (cell->renderer);
      cell->renderer = (GimpPreviewRenderer *) g_value_dup_object (value);
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
      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);

242
  if (cellviewable->renderer)
243
    {
244 245 246 247
      preview_width  = (cellviewable->renderer->width  +
                        2 * cellviewable->renderer->border_width);
      preview_height = (cellviewable->renderer->height +
                        2 * cellviewable->renderer->border_width);
248
    }
249

250 251
  calc_width  = (gint) cell->xpad * 2 + preview_width;
  calc_height = (gint) cell->ypad * 2 + preview_height;
252

253 254 255 256 257 258 259
  if (x_offset) *x_offset = 0;
  if (y_offset) *y_offset = 0;

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

  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);

290
  if (cellviewable->renderer)
291 292 293 294 295 296 297 298 299 300 301 302 303
    {
      if (! flags & GTK_CELL_RENDERER_SELECTED)
        {
          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);
    }
304 305
}

306 307 308 309 310 311 312 313 314 315 316 317 318
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);

319
  if (cellviewable->renderer && event)
320
    {
321 322 323
      if (((GdkEventAny *) event)->type == GDK_BUTTON_PRESS &&
          ((GdkEventButton *) event)->button == 1)
        {
324 325 326
          g_signal_emit (cell, viewable_cell_signals[CLICKED], 0,
                         path, ((GdkEventButton *) event)->state);

327 328 329 330 331 332 333
          return gimp_preview_popup_show (widget,
                                          (GdkEventButton *) event,
                                          cellviewable->renderer->viewable,
                                          cellviewable->renderer->width,
                                          cellviewable->renderer->height,
                                          TRUE);
        }
334 335 336 337 338
    }

  return FALSE;
}

339 340 341 342 343
GtkCellRenderer *
gimp_cell_renderer_viewable_new (void)
{
  return g_object_new (GIMP_TYPE_CELL_RENDERER_VIEWABLE, NULL);
}