imap_preview.c 14.1 KB
Newer Older
Sven Neumann's avatar
Sven Neumann committed
1 2 3 4 5
/*
 * This is a plug-in for the GIMP.
 *
 * Generates clickable image maps.
 *
Maurits Rijk's avatar
Maurits Rijk committed
6
 * Copyright (C) 1998-2005 Maurits Rijk  m.rijk@chello.nl
Sven Neumann's avatar
Sven Neumann committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * 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.
 *
 */

24 25 26 27
#include "config.h"

#include <gtk/gtk.h>

Sven Neumann's avatar
Sven Neumann committed
28 29
#include "libgimp/gimp.h"

30 31
#include <libgimp/gimpui.h>

32
#include "imap_commands.h"
Sven Neumann's avatar
Sven Neumann committed
33 34
#include "imap_grid.h"
#include "imap_main.h"
Maurits Rijk's avatar
Maurits Rijk committed
35
#include "imap_menu.h"
Sven Neumann's avatar
Sven Neumann committed
36 37
#include "imap_preview.h"

38
#define PREVIEW_MASK  (GDK_EXPOSURE_MASK       | \
Sven Neumann's avatar
Sven Neumann committed
39
		       GDK_POINTER_MOTION_MASK | \
40
                       GDK_BUTTON_PRESS_MASK   | \
Sven Neumann's avatar
Sven Neumann committed
41
		       GDK_BUTTON_RELEASE_MASK | \
42 43 44 45 46
		       GDK_BUTTON_MOTION_MASK  | \
		       GDK_KEY_PRESS_MASK      | \
		       GDK_KEY_RELEASE_MASK    | \
		       GDK_ENTER_NOTIFY_MASK   | \
		       GDK_LEAVE_NOTIFY_MASK)
Sven Neumann's avatar
Sven Neumann committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63

#define PREVIEW_SIZE 400

/*======================================================================
		Preview Rendering Util routine
=======================================================================*/

#define CHECKWIDTH 4
#define LIGHTCHECK 192
#define DARKCHECK  128
#ifndef OPAQUE
#define OPAQUE	   255
#endif

static Preview_t*
preview_user_data(GtkWidget *preview)
{
64
  return (Preview_t*) g_object_get_data (G_OBJECT (preview), "preview");
Sven Neumann's avatar
Sven Neumann committed
65 66
}

67
gint
Sven Neumann's avatar
Sven Neumann committed
68 69 70 71 72
preview_get_width(GtkWidget *preview)
{
   return preview_user_data(preview)->width;
}

73
gint
Sven Neumann's avatar
Sven Neumann committed
74 75 76 77 78 79
preview_get_height(GtkWidget *preview)
{
   return preview_user_data(preview)->height;
}

static void
80
render_gray_image(Preview_t *preview_base, GimpPixelRgn *srcrgn)
Sven Neumann's avatar
Sven Neumann committed
81
{
82
   guchar	 *src_row, *dest_buffer, *src, *dest;
Sven Neumann's avatar
Sven Neumann committed
83 84 85
   gint		 row, col;
   gint		 bpp, dwidth, dheight, pwidth, pheight;
   gint		 *src_col;
86
   GtkWidget     *preview = preview_base->preview;
Sven Neumann's avatar
Sven Neumann committed
87 88 89

   dwidth  = srcrgn->w;
   dheight = srcrgn->h;
90 91
   pwidth = preview_base->widget_width;
   pheight = preview_base->widget_height;
Sven Neumann's avatar
Sven Neumann committed
92 93 94
   bpp = srcrgn->bpp;

   src_row = g_new(guchar, dwidth * bpp);
95
   dest_buffer = g_new(guchar, pwidth * pheight);
Sven Neumann's avatar
Sven Neumann committed
96
   src_col = g_new(gint, pwidth);
97

Sven Neumann's avatar
Sven Neumann committed
98 99
   for (col = 0; col < pwidth; col++)
      src_col[col] = (col * dwidth / pwidth) * bpp;
100

101 102
   dest = dest_buffer;

Sven Neumann's avatar
Sven Neumann committed
103
   for (row = 0; row < pheight; row++) {
104
      gimp_pixel_rgn_get_row(srcrgn, src_row, 0, row * dheight / pheight,
Sven Neumann's avatar
Sven Neumann committed
105 106 107 108 109 110 111 112 113
			     dwidth);

      src = src_row;

      for (col = 0; col < pwidth; col++) {
	 src = &src_row[src_col[col]];
	 *dest++ = *src;
      }
   }
114 115 116 117 118
   gimp_preview_area_draw (GIMP_PREVIEW_AREA (preview),
                           0, 0, pwidth, pheight,
                           GIMP_GRAY_IMAGE,
                           dest_buffer,
                           pwidth);
Sven Neumann's avatar
Sven Neumann committed
119 120
   g_free(src_col);
   g_free(src_row);
121
   g_free(dest_buffer);
Sven Neumann's avatar
Sven Neumann committed
122 123 124 125

}

static void
126
render_indexed_image(Preview_t *preview_base, GimpPixelRgn *srcrgn)
Sven Neumann's avatar
Sven Neumann committed
127
{
128
   guchar	 *src_row, *dest_buffer, *src, *dest;
Sven Neumann's avatar
Sven Neumann committed
129 130 131 132 133 134 135
   gint		 row, col;
   gint		 dwidth, dheight, pwidth, pheight;
   gint		 *src_col;
   gint		 bpp, alpha, has_alpha;
   guchar 	 *cmap, *colour;
   gint 	 ncols;
   gboolean	 gray = get_map_info()->show_gray;
136
   GtkWidget    *preview = preview_base->preview;
Sven Neumann's avatar
Sven Neumann committed
137 138 139

   dwidth  = srcrgn->w;
   dheight = srcrgn->h;
140 141
   pwidth = preview_base->widget_width;
   pheight = preview_base->widget_height;
Sven Neumann's avatar
Sven Neumann committed
142 143
   bpp = srcrgn->bpp;
   alpha = bpp;
144
   has_alpha = gimp_drawable_has_alpha(srcrgn->drawable->drawable_id);
145
   if (has_alpha)
Sven Neumann's avatar
Sven Neumann committed
146 147
      alpha--;

148 149
   cmap = gimp_image_get_colormap (gimp_drawable_get_image (srcrgn->drawable->drawable_id),
                                   &ncols);
150

Sven Neumann's avatar
Sven Neumann committed
151
   src_row = g_new(guchar, dwidth * bpp);
152
   dest_buffer = g_new(guchar, pwidth * pheight * 3);
Sven Neumann's avatar
Sven Neumann committed
153
   src_col = g_new(gint, pwidth);
154

Sven Neumann's avatar
Sven Neumann committed
155 156
   for (col = 0; col < pwidth; col++)
      src_col[col] = (col * dwidth / pwidth) * bpp;
157

158
   dest = dest_buffer;
Sven Neumann's avatar
Sven Neumann committed
159
   for (row = 0; row < pheight; row++) {
160
      gimp_pixel_rgn_get_row(srcrgn, src_row, 0, row * dheight / pheight,
Sven Neumann's avatar
Sven Neumann committed
161
			     dwidth);
162

Sven Neumann's avatar
Sven Neumann committed
163 164 165 166 167
      for (col = 0; col < pwidth; col++) {
	 src = &src_row[src_col[col]];
	 colour = cmap + 3 * (int)(*src);

	 if (gray) {
168 169
	    guchar avg = (299 * colour[0] + 587 * colour[1] +
			  114 * colour[2]) / 1000;
Sven Neumann's avatar
Sven Neumann committed
170 171 172 173
	    *dest++ = avg;
	    *dest++ = avg;
	    *dest++ = avg;
	 } else {
174 175 176
	    *dest++ = colour[0];
	    *dest++ = colour[1];
	    *dest++ = colour[2];
Sven Neumann's avatar
Sven Neumann committed
177 178 179
	 }
      }
   }
180 181 182 183 184
   gimp_preview_area_draw(GIMP_PREVIEW_AREA(preview),
                          0, 0, pwidth, pheight,
                          GIMP_RGB_IMAGE,
                          dest_buffer,
                          pwidth * 3);
Sven Neumann's avatar
Sven Neumann committed
185 186
   g_free(src_col);
   g_free(src_row);
187
   g_free(dest_buffer);
Sven Neumann's avatar
Sven Neumann committed
188 189 190
}

static void
191
render_rgb_image(Preview_t *preview_base, GimpPixelRgn *srcrgn)
Sven Neumann's avatar
Sven Neumann committed
192
{
193
   guchar	 *src_row, *dest_buffer, *src, *dest;
Sven Neumann's avatar
Sven Neumann committed
194 195 196 197 198 199
   gint		 row, col;
   gint		 dwidth, dheight, pwidth, pheight;
   gint		 *src_col;
   gint		 bpp, alpha, has_alpha, b;
   guchar	 check;
   gboolean	 gray = get_map_info()->show_gray;
200
   GtkWidget    *preview = preview_base->preview;
Sven Neumann's avatar
Sven Neumann committed
201 202 203

   dwidth  = srcrgn->w;
   dheight = srcrgn->h;
204 205
   pwidth = preview_base->widget_width;
   pheight = preview_base->widget_height;
Sven Neumann's avatar
Sven Neumann committed
206 207
   bpp = srcrgn->bpp;
   alpha = bpp;
208
   has_alpha = gimp_drawable_has_alpha(srcrgn->drawable->drawable_id);
209
   if (has_alpha)
Sven Neumann's avatar
Sven Neumann committed
210
      alpha--;
211

Sven Neumann's avatar
Sven Neumann committed
212
   src_row = g_new(guchar, dwidth * bpp);
213
   dest_buffer = g_new(guchar, pwidth * pheight * bpp);
Sven Neumann's avatar
Sven Neumann committed
214
   src_col = g_new(gint, pwidth);
215

Sven Neumann's avatar
Sven Neumann committed
216 217
   for (col = 0; col < pwidth; col++)
      src_col[col] = (col * dwidth / pwidth) * bpp;
218

219
   dest = dest_buffer;
Sven Neumann's avatar
Sven Neumann committed
220
   for (row = 0; row < pheight; row++) {
221
      gimp_pixel_rgn_get_row(srcrgn, src_row, 0, row * dheight / pheight,
Sven Neumann's avatar
Sven Neumann committed
222 223 224 225 226 227 228 229 230 231 232 233 234 235
			     dwidth);
      for (col = 0; col < pwidth; col++) {
	 src = &src_row[src_col[col]];
	 if(!has_alpha || src[alpha] == OPAQUE) {
	    /* no alpha channel or opaque -- simple way */
	    for (b = 0; b < alpha; b++)
	       dest[b] = src[b];
	 } else {
	    /* more or less transparent */
	    if( ( col % (CHECKWIDTH*2) < CHECKWIDTH ) ^
		( row % (CHECKWIDTH*2) < CHECKWIDTH ) )
	       check = LIGHTCHECK;
	    else
	       check = DARKCHECK;
236

Sven Neumann's avatar
Sven Neumann committed
237 238 239 240 241 242 243
	    if (src[alpha] == 0) {
	       /* full transparent -- check */
	       for (b = 0; b < alpha; b++)
		  dest[b] = check;
	    } else {
	       /* middlemost transparent -- mix check and src */
	       for (b = 0; b < alpha; b++)
244
		  dest[b] = (src[b] * src[alpha] +
Sven Neumann's avatar
Sven Neumann committed
245 246 247 248 249 250 251 252 253 254 255 256
			     check * (OPAQUE - src[alpha])) / OPAQUE;
	    }
	 }
	 if (gray) {
	    guchar avg;
	    avg = (299 * dest[0] + 587 * dest[1] + 114 * dest[2]) / 1000;
	    for (b = 0; b < alpha; b++)
	       dest[b] = avg;
	 }
	 dest += alpha;
      }
   }
257 258
   gimp_preview_area_draw (GIMP_PREVIEW_AREA (preview),
                           0, 0, pwidth, pheight,
259
                           GIMP_RGB_IMAGE,
260
                           dest_buffer,
261
                           pwidth * 3);
Sven Neumann's avatar
Sven Neumann committed
262 263
   g_free(src_col);
   g_free(src_row);
264
   g_free(dest_buffer);
Sven Neumann's avatar
Sven Neumann committed
265 266 267
}

static void
268
render_preview(Preview_t *preview_base, GimpPixelRgn *srcrgn)
Sven Neumann's avatar
Sven Neumann committed
269
{
270
   switch (gimp_drawable_type(srcrgn->drawable->drawable_id)) {
271 272
   case GIMP_RGB_IMAGE:
   case GIMP_RGBA_IMAGE:
273
      render_rgb_image(preview_base, srcrgn);
Sven Neumann's avatar
Sven Neumann committed
274
      break;
275 276
   case GIMP_GRAY_IMAGE:
   case GIMP_GRAYA_IMAGE:
277
      render_gray_image(preview_base, srcrgn);
Sven Neumann's avatar
Sven Neumann committed
278
      break;
279 280
   case GIMP_INDEXED_IMAGE:
   case GIMP_INDEXEDA_IMAGE:
281
      render_indexed_image(preview_base, srcrgn);
Sven Neumann's avatar
Sven Neumann committed
282 283 284 285
      break;
   }
}

286
static gboolean
287 288
arrow_cb(GtkWidget *widget, GdkEventButton *event, gpointer data)
{
289 290
   if (event->button == 1)
      do_main_popup_menu(event);
291
   return TRUE;
292 293
}

294
static gboolean
Sven Neumann's avatar
Sven Neumann committed
295 296 297 298 299 300 301 302
preview_expose(GtkWidget *widget, GdkEventExpose *event)
{
   draw_grid(widget);
   draw_shapes(widget);
   return FALSE;
}

void
303
add_preview_motion_event(Preview_t *preview, GCallback func)
Sven Neumann's avatar
Sven Neumann committed
304
{
305
   g_signal_connect(preview->preview, "motion-notify-event",
306
		    func, NULL);
Sven Neumann's avatar
Sven Neumann committed
307 308 309
}

void
310
add_enter_notify_event(Preview_t *preview, GCallback func)
Sven Neumann's avatar
Sven Neumann committed
311
{
312
   g_signal_connect(preview->preview, "enter-notify-event",
313
		    func, NULL);
Sven Neumann's avatar
Sven Neumann committed
314 315 316
}

void
317
add_leave_notify_event(Preview_t *preview, GCallback func)
Sven Neumann's avatar
Sven Neumann committed
318
{
319
   g_signal_connect(preview->preview, "leave-notify-event",
320
		    func, NULL);
Sven Neumann's avatar
Sven Neumann committed
321 322 323
}

void
324
add_preview_button_press_event(Preview_t *preview, GCallback func)
Sven Neumann's avatar
Sven Neumann committed
325
{
326
   g_signal_connect(preview->preview, "button-press-event",
327
		    func, NULL);
Sven Neumann's avatar
Sven Neumann committed
328 329 330 331 332
}

void
preview_redraw(Preview_t *preview)
{
333
  gtk_widget_queue_draw(preview->preview);
Sven Neumann's avatar
Sven Neumann committed
334 335 336 337 338
}

void
preview_zoom(Preview_t *preview, gint zoom_factor)
{
339 340
   preview->widget_width = preview->width * zoom_factor;
   preview->widget_height = preview->height * zoom_factor;
341
   gtk_widget_set_size_request (preview->preview, preview->widget_width,
342
                                preview->widget_height);
Sven Neumann's avatar
Sven Neumann committed
343
   gtk_widget_queue_resize(preview->window);
344
   render_preview(preview, &preview->src_rgn);
Sven Neumann's avatar
Sven Neumann committed
345 346 347
   preview_redraw(preview);
}

Sven Neumann's avatar
Sven Neumann committed
348
GdkCursorType
Sven Neumann's avatar
Sven Neumann committed
349 350
preview_set_cursor(Preview_t *preview, GdkCursorType cursor_type)
{
351 352 353 354 355
   GdkCursorType  prev_cursor = preview->cursor;
   GdkDisplay    *display     = gtk_widget_get_display (preview->window);
   GdkCursor     *cursor      = gdk_cursor_new_for_display (display,
                                                            cursor_type);

Sven Neumann's avatar
Sven Neumann committed
356
   gdk_window_set_cursor(preview->window->window, cursor);
357
   gdk_cursor_unref(cursor);
358

Sven Neumann's avatar
Sven Neumann committed
359
   preview->cursor = cursor_type;
360

Sven Neumann's avatar
Sven Neumann committed
361 362 363 364 365 366 367 368
   return prev_cursor;
}

static GtkTargetEntry target_table[] = {
   {"STRING", 0, 1 },
   {"text/plain", 0, 2 }
};

369 370
static void
handle_drop(GtkWidget *widget, GdkDragContext *context, gint x, gint y,
Sven Neumann's avatar
Sven Neumann committed
371 372 373 374 375 376 377 378 379 380 381 382
	    GtkSelectionData *data, guint info, guint time)
{
   gboolean success = FALSE;
   if (data->length >= 0 && data->format == 8) {
      ObjectList_t *list = get_shapes();
      Object_t *obj;

      x = get_real_coord(x);
      y = get_real_coord(y);
      obj = object_list_find(list, x, y);
      if (obj && !obj->locked) {
	 command_list_add(edit_object_command_new(obj));
Sven Neumann's avatar
Sven Neumann committed
383
	 object_set_url(obj, (const gchar *) data->data);
Sven Neumann's avatar
Sven Neumann committed
384 385 386 387 388
	 object_emit_update_signal(obj);
	 success = TRUE;
      }
   }
   gtk_drag_finish(context, success, FALSE, time);
Sven Neumann's avatar
Sven Neumann committed
389 390
}

391
static void
392 393
preview_size_allocate (GtkWidget *widget,
                       GtkAllocation *allocation,
394 395 396 397 398 399 400 401 402
                       gpointer preview_void)
{
  Preview_t * preview;

  preview = (Preview_t *)preview_void;

  render_preview(preview, &preview->src_rgn);
}

Sven Neumann's avatar
Sven Neumann committed
403
Preview_t*
404
make_preview(GimpDrawable *drawable)
Sven Neumann's avatar
Sven Neumann committed
405 406 407 408
{
   Preview_t *data = g_new(Preview_t, 1);
   GtkWidget *preview;
   GtkWidget *window;
409
   GtkWidget *button, *arrow;
Sven Neumann's avatar
Sven Neumann committed
410 411 412 413 414 415
   GtkWidget *ruler;
   GtkWidget *frame;
   GtkWidget *table;
   gint width, height;

   data->drawable = drawable;
416
   data->preview = preview = gimp_preview_area_new ();
Sven Neumann's avatar
Sven Neumann committed
417

418
   g_object_set_data (G_OBJECT (preview), "preview", data);
Sven Neumann's avatar
Sven Neumann committed
419
   gtk_widget_set_events(GTK_WIDGET(preview), PREVIEW_MASK);
420
   g_signal_connect_after(preview, "expose-event",
421
			  G_CALLBACK(preview_expose), data);
422 423
   g_signal_connect (preview, "size-allocate",
                     G_CALLBACK (preview_size_allocate), (gpointer)data);
Sven Neumann's avatar
Sven Neumann committed
424

Sven Neumann's avatar
Sven Neumann committed
425 426 427
   /* Handle drop of links in preview widget */
   gtk_drag_dest_set(preview, GTK_DEST_DEFAULT_ALL, target_table,
		     2, GDK_ACTION_COPY);
428
   g_signal_connect(preview, "drag_data_received",
429
		    G_CALLBACK(handle_drop), NULL);
Sven Neumann's avatar
Sven Neumann committed
430

431
   data->widget_width = data->width =
432
       gimp_drawable_width(drawable->drawable_id);
433
   data->widget_height = data->height =
434
       gimp_drawable_height(drawable->drawable_id);
435
   gtk_widget_set_size_request (preview, data->widget_width,
436
                                data->widget_height);
Sven Neumann's avatar
Sven Neumann committed
437 438 439 440

   data->window = window = gtk_scrolled_window_new(NULL, NULL);
   width = (data->width > 600) ? 600 : data->width;
   height = (data->height > 400) ? 400 : data->height;
441
   gtk_widget_set_size_request(window, width, height);
442
   gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(window),
Sven Neumann's avatar
Sven Neumann committed
443 444 445 446 447 448 449 450 451 452 453
				  GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
   gtk_widget_show(window);

   data->frame = frame = gtk_frame_new(NULL);
   gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(window), frame);
   gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);

   /* The main table */
   table = gtk_table_new(3, 3, FALSE);
   gtk_table_attach(GTK_TABLE(table), preview, 1, 2, 1, 2, GTK_FILL, GTK_FILL,
		    0, 0);
454 455 456 457 458 459 460 461
   gtk_container_add(GTK_CONTAINER(frame), table);

   /* Create button with arrow */
   button = gtk_button_new();
   GTK_WIDGET_UNSET_FLAGS(button, GTK_CAN_FOCUS);
   gtk_table_attach(GTK_TABLE(table), button, 0, 1, 0, 1, GTK_FILL, GTK_FILL,
		    0, 0);
   gtk_widget_set_events(button,
462
			 GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
463
   g_signal_connect(button, "button-press-event",
464
		    G_CALLBACK(arrow_cb), NULL);
465 466 467 468 469
   gtk_widget_show(button);

   arrow = gtk_arrow_new(GTK_ARROW_RIGHT, GTK_SHADOW_OUT);
   gtk_container_add(GTK_CONTAINER(button), arrow);
   gtk_widget_show(arrow);
Sven Neumann's avatar
Sven Neumann committed
470 471 472 473

   /* Create horizontal ruler */
   data->hruler = ruler = gtk_hruler_new();
   gtk_ruler_set_range(GTK_RULER(ruler), 0, data->width, 0, PREVIEW_SIZE);
474
   g_signal_connect_swapped(preview, "motion-notify-event",
475
			    G_CALLBACK(GTK_WIDGET_GET_CLASS(ruler)->motion_notify_event),
476
			    ruler);
477

478
   gtk_table_attach(GTK_TABLE(table), ruler, 1, 2, 0, 1, GTK_FILL, GTK_FILL,
Sven Neumann's avatar
Sven Neumann committed
479 480 481 482 483 484
		    0, 0);
   gtk_widget_show(ruler);

   /* Create vertical ruler */
   data->vruler = ruler = gtk_vruler_new();
   gtk_ruler_set_range(GTK_RULER(ruler), 0, data->height, 0, PREVIEW_SIZE);
485
   g_signal_connect_swapped(preview, "motion-notify-event",
486
			    G_CALLBACK(GTK_WIDGET_GET_CLASS(ruler)->motion_notify_event),
487
			    ruler);
488
   gtk_table_attach(GTK_TABLE(table), ruler, 0, 1, 1, 2, GTK_FILL, GTK_FILL,
Sven Neumann's avatar
Sven Neumann committed
489 490 491
		    0, 0);
   gtk_widget_show(ruler);

492
   gimp_pixel_rgn_init(&data->src_rgn, drawable, 0, 0, data->width,
Sven Neumann's avatar
Sven Neumann committed
493
		       data->height, FALSE, FALSE);
494
   render_preview(data, &data->src_rgn);
Sven Neumann's avatar
Sven Neumann committed
495 496 497 498 499 500 501 502

   gtk_widget_show(preview);

   gtk_widget_show(frame);
   gtk_widget_show(table);

   return data;
}