2
* This is a plug-in for the GIMP.
4
* Generates clickable image maps.
6
* Copyright (C) 1998-2004 Maurits Rijk m.rijk@chello.nl
8
* This program is free software; you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License as published by
10
* the Free Software Foundation; either version 2 of the License, or
11
* (at your option) any later version.
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU General Public License for more details.
18
* You should have received a copy of the GNU General Public License
19
* along with this program; if not, write to the Free Software
20
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
26
#include <stdlib.h> /* abs */
30
#include <libgimp/gimp.h>
31
#include <libgimp/gimpui.h>
33
#include "imap_main.h"
34
#include "imap_misc.h"
35
#include "imap_object_popup.h"
36
#include "imap_rectangle.h"
37
#include "imap_stock.h"
38
#include "imap_table.h"
40
#include "libgimp/stdplugins-intl.h"
43
static gboolean rectangle_is_valid(Object_t *obj);
44
static Object_t *rectangle_clone(Object_t *obj);
45
static void rectangle_assign(Object_t *obj, Object_t *des);
46
static void rectangle_normalize(Object_t *obj);
47
static void rectangle_draw(Object_t *obj, GdkWindow *window, GdkGC* gc);
48
static void rectangle_draw_sashes(Object_t *obj, GdkWindow *window, GdkGC* gc);
49
static MoveSashFunc_t rectangle_near_sash(Object_t *obj, gint x, gint y);
50
static gboolean rectangle_point_is_on(Object_t *obj, gint x, gint y);
51
static void rectangle_get_dimensions(Object_t *obj, gint *x, gint *y,
52
gint *width, gint *height);
53
static void rectangle_resize(Object_t *obj, gint percentage_x,
55
static void rectangle_move(Object_t *obj, gint dx, gint dy);
56
static gpointer rectangle_create_info_widget(GtkWidget *frame);
57
static void rectangle_fill_info_tab(Object_t *obj, gpointer data);
58
static void rectangle_set_initial_focus(Object_t *obj, gpointer data);
59
static void rectangle_update(Object_t *obj, gpointer data);
60
static void rectangle_write_csim(Object_t *obj, gpointer param,
62
static void rectangle_write_cern(Object_t *obj, gpointer param,
64
static void rectangle_write_ncsa(Object_t *obj, gpointer param,
66
static const gchar* rectangle_get_stock_icon_name(void);
68
static ObjectClass_t rectangle_class = {
70
NULL, /* info_dialog */
75
NULL, /* rectangle_destruct */
80
rectangle_draw_sashes,
82
rectangle_point_is_on,
83
rectangle_get_dimensions,
86
rectangle_create_info_widget,
87
rectangle_fill_info_tab, /* rectangle_update_info_widget */
88
rectangle_fill_info_tab,
89
rectangle_set_initial_focus,
95
rectangle_get_stock_icon_name
99
create_rectangle(gint x, gint y, gint width, gint height)
101
Rectangle_t *rectangle = g_new(Rectangle_t, 1);
104
rectangle->width = width;
105
rectangle->height = height;
106
return object_init(&rectangle->obj, &rectangle_class);
110
draw_any_rectangle(GdkWindow *window, GdkGC *gc, gint x, gint y, gint w,
121
draw_rectangle(window, gc, FALSE, x, y, w, h);
125
rectangle_is_valid(Object_t *obj)
127
Rectangle_t *rectangle = ObjectToRectangle(obj);
128
return rectangle->width && rectangle->height;
132
rectangle_clone(Object_t *obj)
134
Rectangle_t *rectangle = ObjectToRectangle(obj);
135
Rectangle_t *clone = g_new(Rectangle_t, 1);
137
clone->x = rectangle->x;
138
clone->y = rectangle->y;
139
clone->width = rectangle->width;
140
clone->height = rectangle->height;
145
rectangle_assign(Object_t *obj, Object_t *des)
147
Rectangle_t *src_rectangle = ObjectToRectangle(obj);
148
Rectangle_t *des_rectangle = ObjectToRectangle(des);
149
des_rectangle->x = src_rectangle->x;
150
des_rectangle->y = src_rectangle->y;
151
des_rectangle->width = src_rectangle->width;
152
des_rectangle->height = src_rectangle->height;
156
rectangle_normalize(Object_t *obj)
158
Rectangle_t *rectangle = ObjectToRectangle(obj);
159
if (rectangle->width < 0) {
160
rectangle->x += rectangle->width;
161
rectangle->width = -rectangle->width;
163
if (rectangle->height < 0) {
164
rectangle->y += rectangle->height;
165
rectangle->height = -rectangle->height;
170
rectangle_draw(Object_t *obj, GdkWindow *window, GdkGC *gc)
172
Rectangle_t *rectangle = ObjectToRectangle(obj);
173
draw_any_rectangle(window, gc, rectangle->x, rectangle->y,
174
rectangle->width, rectangle->height);
178
rectangle_draw_sashes(Object_t *obj, GdkWindow *window, GdkGC *gc)
180
Rectangle_t *rectangle = ObjectToRectangle(obj);
181
draw_sash(window, gc, rectangle->x, rectangle->y);
182
draw_sash(window, gc, rectangle->x + rectangle->width / 2, rectangle->y);
183
draw_sash(window, gc, rectangle->x + rectangle->width, rectangle->y);
184
draw_sash(window, gc, rectangle->x, rectangle->y + rectangle->height / 2);
185
draw_sash(window, gc, rectangle->x + rectangle->width,
186
rectangle->y + rectangle->height / 2);
187
draw_sash(window, gc, rectangle->x, rectangle->y + rectangle->height);
188
draw_sash(window, gc, rectangle->x + rectangle->width / 2,
189
rectangle->y + rectangle->height);
190
draw_sash(window, gc, rectangle->x + rectangle->width,
191
rectangle->y + rectangle->height);
195
MoveUpperSash(Object_t *obj, gint dx, gint dy)
197
Rectangle_t *rectangle = ObjectToRectangle(obj);
199
rectangle->height -= dy;
203
MoveLeftSash(Object_t *obj, gint dx, gint dy)
205
Rectangle_t *rectangle = ObjectToRectangle(obj);
207
rectangle->width -= dx;
211
MoveRightSash(Object_t *obj, gint dx, gint dy)
213
Rectangle_t *rectangle = ObjectToRectangle(obj);
214
rectangle->width += dx;
218
MoveLowerSash(Object_t *obj, gint dx, gint dy)
220
Rectangle_t *rectangle = ObjectToRectangle(obj);
221
rectangle->height += dy;
225
MoveUpperLeftSash(Object_t *obj, gint dx, gint dy)
227
Rectangle_t *rectangle = ObjectToRectangle(obj);
230
rectangle->width -= dx;
231
rectangle->height -= dy;
235
MoveUpperRightSash(Object_t *obj, gint dx, gint dy)
237
Rectangle_t *rectangle = ObjectToRectangle(obj);
239
rectangle->width += dx;
240
rectangle->height -= dy;
244
MoveLowerLeftSash(Object_t *obj, gint dx, gint dy)
246
Rectangle_t *rectangle = ObjectToRectangle(obj);
248
rectangle->width -= dx;
249
rectangle->height += dy;
253
MoveLowerRightSash(Object_t *obj, gint dx, gint dy)
255
Rectangle_t *rectangle = ObjectToRectangle(obj);
256
rectangle->width += dx;
257
rectangle->height += dy;
260
static MoveSashFunc_t
261
rectangle_near_sash(Object_t *obj, gint x, gint y)
263
Rectangle_t *rectangle = ObjectToRectangle(obj);
264
if (near_sash(rectangle->x, rectangle->y, x, y))
265
return MoveUpperLeftSash;
266
else if (near_sash(rectangle->x + rectangle->width / 2, rectangle->y, x, y))
267
return MoveUpperSash;
268
else if (near_sash(rectangle->x + rectangle->width, rectangle->y, x, y))
269
return MoveUpperRightSash;
270
else if (near_sash(rectangle->x, rectangle->y + rectangle->height / 2,
273
else if (near_sash(rectangle->x + rectangle->width,
274
rectangle->y + rectangle->height / 2, x, y))
275
return MoveRightSash;
276
else if (near_sash(rectangle->x, rectangle->y + rectangle->height, x, y))
277
return MoveLowerLeftSash;
278
else if (near_sash(rectangle->x + rectangle->width / 2,
279
rectangle->y + rectangle->height, x, y))
280
return MoveLowerSash;
281
else if (near_sash(rectangle->x + rectangle->width,
282
rectangle->y + rectangle->height, x, y))
283
return MoveLowerRightSash;
288
rectangle_point_is_on(Object_t *obj, gint x, gint y)
290
Rectangle_t *rectangle = ObjectToRectangle(obj);
291
return x >= rectangle->x && x <= rectangle->x + rectangle->width &&
292
y >= rectangle->y && y <= rectangle->y + rectangle->height;
296
rectangle_get_dimensions(Object_t *obj, gint *x, gint *y,
297
gint *width, gint *height)
299
Rectangle_t *rectangle = ObjectToRectangle(obj);
302
*width = rectangle->width;
303
*height = rectangle->height;
307
rectangle_resize(Object_t *obj, gint percentage_x, gint percentage_y)
309
Rectangle_t *rectangle = ObjectToRectangle(obj);
310
rectangle->x = rectangle->x * percentage_x / 100;
311
rectangle->y = rectangle->y * percentage_y / 100;
312
rectangle->width = rectangle->width * percentage_x / 100;
313
rectangle->height = rectangle->height * percentage_y / 100;
317
rectangle_move(Object_t *obj, gint dx, gint dy)
319
Rectangle_t *rectangle = ObjectToRectangle(obj);
330
GtkWidget *chain_button;
331
} RectangleProperties_t;
334
x_changed_cb(GtkWidget *widget, gpointer data)
336
RectangleProperties_t *props = (RectangleProperties_t*) data;
337
Object_t *obj = props->obj;
338
gint x = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget));
340
if (gimp_chain_button_get_active(GIMP_CHAIN_BUTTON(props->chain_button)))
341
gtk_spin_button_set_value(GTK_SPIN_BUTTON(props->y), x);
343
ObjectToRectangle(obj)->x = x;
344
edit_area_info_dialog_emit_geometry_signal(obj->class->info_dialog);
348
y_changed_cb(GtkWidget *widget, gpointer data)
350
Object_t *obj = ((RectangleProperties_t*) data)->obj;
351
gint y = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget));
352
ObjectToRectangle(obj)->y = y;
353
edit_area_info_dialog_emit_geometry_signal(obj->class->info_dialog);
357
width_changed_cb(GtkWidget *widget, gpointer data)
359
Object_t *obj = ((RectangleProperties_t*) data)->obj;
360
gint width = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget));
361
ObjectToRectangle(obj)->width = width;
362
edit_area_info_dialog_emit_geometry_signal(obj->class->info_dialog);
366
height_changed_cb(GtkWidget *widget, gpointer data)
368
Object_t *obj = ((RectangleProperties_t*) data)->obj;
369
gint height = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget));
370
ObjectToRectangle(obj)->height = height;
371
edit_area_info_dialog_emit_geometry_signal(obj->class->info_dialog);
375
rectangle_create_info_widget(GtkWidget *frame)
377
RectangleProperties_t *props = g_new(RectangleProperties_t, 1);
378
GtkWidget *table, *label, *chain_button;
379
gint max_width = get_image_width();
380
gint max_height = get_image_height();
382
table = gtk_table_new(4, 4, FALSE);
383
gtk_container_add(GTK_CONTAINER(frame), table);
385
gtk_table_set_row_spacings(GTK_TABLE(table), 6);
386
gtk_table_set_col_spacings(GTK_TABLE(table), 6);
387
gtk_widget_show(table);
389
label = create_label_in_table(table, 0, 0, _("Upper left _x:"));
390
props->x = create_spin_button_in_table(table, label, 0, 1, 1, 0,
392
g_signal_connect(props->x, "value_changed",
393
G_CALLBACK(x_changed_cb), (gpointer) props);
394
create_label_in_table(table, 0, 3, _("pixels"));
396
label = create_label_in_table(table, 1, 0, _("Upper left _y:"));
397
props->y = create_spin_button_in_table(table, label, 1, 1, 1, 0,
399
g_signal_connect(props->y, "value_changed",
400
G_CALLBACK(y_changed_cb), (gpointer) props);
401
create_label_in_table(table, 1, 3, _("pixels"));
403
label = create_label_in_table(table, 2, 0, _("_Width:"));
404
props->width = create_spin_button_in_table(table, label, 2, 1, 1, 1,
406
g_signal_connect(props->width, "value_changed",
407
G_CALLBACK(width_changed_cb), (gpointer) props);
408
create_label_in_table(table, 2, 3, _("pixels"));
410
label = create_label_in_table(table, 3, 0, _("_Height:"));
411
props->height = create_spin_button_in_table(table, label, 3, 1, 1, 1,
413
g_signal_connect(props->height, "value_changed",
414
G_CALLBACK(height_changed_cb), (gpointer) props);
415
create_label_in_table(table, 3, 3, _("pixels"));
417
chain_button = gimp_chain_button_new(GIMP_CHAIN_RIGHT);
418
props->chain_button = chain_button;
419
gtk_table_attach_defaults(GTK_TABLE(table), chain_button, 2, 3, 2, 4);
420
gtk_widget_show(chain_button);
426
rectangle_fill_info_tab(Object_t *obj, gpointer data)
428
Rectangle_t *rectangle = ObjectToRectangle(obj);
429
RectangleProperties_t *props = (RectangleProperties_t*) data;
432
gtk_spin_button_set_value(GTK_SPIN_BUTTON(props->x), rectangle->x);
433
gtk_spin_button_set_value(GTK_SPIN_BUTTON(props->y), rectangle->y);
434
gtk_spin_button_set_value(GTK_SPIN_BUTTON(props->width), rectangle->width);
435
gtk_spin_button_set_value(GTK_SPIN_BUTTON(props->height),
440
rectangle_set_initial_focus(Object_t *obj, gpointer data)
442
RectangleProperties_t *props = (RectangleProperties_t*) data;
443
gtk_widget_grab_focus(props->x);
447
rectangle_update(Object_t* obj, gpointer data)
449
Rectangle_t *rectangle = ObjectToRectangle(obj);
450
RectangleProperties_t *props = (RectangleProperties_t*) data;
452
rectangle->x = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(props->x));
453
rectangle->y = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(props->y));
454
rectangle->width = gtk_spin_button_get_value_as_int(
455
GTK_SPIN_BUTTON(props->width));
456
rectangle->height = gtk_spin_button_get_value_as_int(
457
GTK_SPIN_BUTTON(props->height));
461
rectangle_write_csim(Object_t *obj, gpointer param, OutputFunc_t output)
463
Rectangle_t *rectangle = ObjectToRectangle(obj);
464
output(param, "\"rect\" coords=\"%d,%d,%d,%d\"", rectangle->x, rectangle->y,
465
rectangle->x + rectangle->width, rectangle->y + rectangle->height);
469
rectangle_write_cern(Object_t *obj, gpointer param, OutputFunc_t output)
471
Rectangle_t *rectangle = ObjectToRectangle(obj);
472
output(param, "rect (%d,%d) (%d,%d)", rectangle->x, rectangle->y,
473
rectangle->x + rectangle->width, rectangle->y + rectangle->height);
477
rectangle_write_ncsa(Object_t *obj, gpointer param, OutputFunc_t output)
479
Rectangle_t *rectangle = ObjectToRectangle(obj);
480
output(param, "rect %s %d,%d %d,%d", obj->url,
481
rectangle->x, rectangle->y,
482
rectangle->x + rectangle->width, rectangle->y + rectangle->height);
486
rectangle_get_stock_icon_name(void)
488
return IMAP_STOCK_RECTANGLE;
492
rectangle_factory_finish(Object_t *obj, gint x, gint y)
494
Rectangle_t *rectangle = ObjectToRectangle(obj);
496
rectangle->width = x - rectangle->x;
497
rectangle->height = y - rectangle->y;
499
rectangle_normalize(obj);
505
rectangle_factory_create_object(gint x, gint y)
507
return create_rectangle(x, y, 0, 0);
511
rectangle_factory_set_xy(Object_t *obj, guint state, gint x, gint y)
513
Rectangle_t *rectangle = ObjectToRectangle(obj);
515
rectangle->width = x - rectangle->x;
516
rectangle->height = y - rectangle->y;
518
if (state & GDK_SHIFT_MASK){
519
gint width = abs(rectangle->width);
520
gint height = abs(rectangle->height);
522
rectangle->height = (rectangle->height < 0) ? -width : width;
524
rectangle->width = (rectangle->width < 0) ? -height : height;
527
main_set_dimension(rectangle->width, rectangle->height);
530
static ObjectFactory_t rectangle_factory = {
531
NULL, /* Object pointer */
532
rectangle_factory_finish,
533
NULL, /* Cancel func */
534
rectangle_factory_create_object,
535
rectangle_factory_set_xy
539
get_rectangle_factory(guint state)
541
return &rectangle_factory;