summaryrefslogtreecommitdiffstats
path: root/plug-ins/imagemap/imap_grid.c
blob: 48c6a54e4e4f7da4fdecebe257d4ecef5fe61bb0 (plain)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/*
 * This is a plug-in for GIMP.
 *
 * Generates clickable image maps.
 *
 * Copyright (C) 1998-2005 Maurits Rijk  m.rijk@chello.nl
 *
 * 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 3 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, see <https://www.gnu.org/licenses/>.
 *
 */

#include "config.h"

#include <stdlib.h>

#include <gtk/gtk.h>

#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>

#include "imap_grid.h"
#include "imap_main.h"
#include "imap_menu.h"
#include "imap_preview.h"
#include "imap_table.h"

#include "libgimp/stdplugins-intl.h"

typedef enum {GRID_HIDDEN, GRID_LINES, GRID_CROSSES} GridType_t;

typedef struct {
   DefaultDialog_t *dialog;
   GtkWidget *type_frame;
   GtkWidget *granularity_frame;
   GtkWidget *offset_frame;
   GtkWidget *snap;
   GtkWidget *width;
   GtkWidget *height;
   GtkWidget *chain_width_height;
   GtkWidget *left;
   GtkWidget *top;
   GtkWidget *chain_left_top;
   GtkWidget *hidden;
   GtkWidget *lines;
   GtkWidget *crosses;
   GtkWidget *preview;

   gboolean   enable_preview;
} GridDialog_t;


static gboolean grid_snap = FALSE;
static gint grid_width = 15;
static gint grid_height = 15;
static gint grid_left = 0;
static gint grid_top = 0;
static GridType_t grid_type = GRID_LINES;

static void
grid_settings_ok_cb(gpointer data)
{
   GridDialog_t *param = (GridDialog_t*) data;
   gboolean new_snap;

   new_snap = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(param->snap));
   grid_width = gtk_spin_button_get_value_as_int(
      GTK_SPIN_BUTTON(param->width));
   grid_height = gtk_spin_button_get_value_as_int(
      GTK_SPIN_BUTTON(param->height));
   grid_left = gtk_spin_button_get_value_as_int(
      GTK_SPIN_BUTTON(param->left));
   grid_top = gtk_spin_button_get_value_as_int(
      GTK_SPIN_BUTTON(param->top));

   if (grid_snap != new_snap) {
      grid_snap = new_snap;
      menu_check_grid(grid_snap);
   }
   preview_redraw();
}

static void
snap_toggled_cb(GtkWidget *widget, gpointer data)
{
   GridDialog_t *param = (GridDialog_t*) data;
   gint sensitive = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));

   gtk_widget_set_sensitive(param->type_frame, sensitive);
   gtk_widget_set_sensitive(param->granularity_frame, sensitive);
   gtk_widget_set_sensitive(param->offset_frame, sensitive);
   gtk_widget_set_sensitive(param->preview, sensitive);
}

static void
type_toggled_cb(GtkWidget *widget, gpointer data)
{
   if (gtk_widget_get_state (widget) & GTK_STATE_SELECTED)
     {
       grid_type = GPOINTER_TO_INT (data);
       preview_redraw();
     }
}

static void
toggle_preview_cb(GtkWidget *widget, GridDialog_t *param)
{
   param->enable_preview =
      gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widget));
   preview_redraw();
}

static void
grid_assign_value(GtkWidget *widget, gpointer data, gint *value)
{
   GridDialog_t *dialog = (GridDialog_t*) data;
   if (dialog->enable_preview) {
      *value = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget));
      preview_redraw();         /* Fix me! */
   }
}

static void
width_changed_cb(GtkWidget *widget, gpointer data)
{
   GridDialog_t *dialog = (GridDialog_t*) data;

   grid_assign_value(widget, data, &grid_width);
   if (gimp_chain_button_get_active(
          GIMP_CHAIN_BUTTON(dialog->chain_width_height))) {
      gint value = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget));
      gtk_spin_button_set_value(GTK_SPIN_BUTTON(dialog->height), value);
   }
}

static void
height_changed_cb(GtkWidget *widget, gpointer data)
{
   GridDialog_t *dialog = (GridDialog_t*) data;

   grid_assign_value(widget, data, &grid_height);
   if (gimp_chain_button_get_active(
          GIMP_CHAIN_BUTTON(dialog->chain_width_height))) {
      gint value = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget));
      gtk_spin_button_set_value(GTK_SPIN_BUTTON(dialog->width), value);
   }
}

static void
left_changed_cb(GtkWidget *widget, gpointer data)
{
   GridDialog_t *dialog = (GridDialog_t*) data;

   grid_assign_value(widget, data, &grid_left);
   if (gimp_chain_button_get_active(
          GIMP_CHAIN_BUTTON(dialog->chain_left_top))) {
      gint value = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget));
      gtk_spin_button_set_value(GTK_SPIN_BUTTON(dialog->top), value);
   }
}

static void
top_changed_cb(GtkWidget *widget, gpointer data)
{
   GridDialog_t *dialog = (GridDialog_t*) data;

   grid_assign_value(widget, data, &grid_top);
   if (gimp_chain_button_get_active(
          GIMP_CHAIN_BUTTON(dialog->chain_left_top))) {
      gint value = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(widget));
      gtk_spin_button_set_value(GTK_SPIN_BUTTON(dialog->left), value);
   }
}

static GridDialog_t*
create_grid_settings_dialog(void)
{
   GridDialog_t *data = g_new(GridDialog_t, 1);
   DefaultDialog_t *dialog;
   GtkWidget *main_table, *table, *label;
   GtkWidget *frame;
   GtkWidget *hbox;
   GtkWidget *button;
   GtkWidget *chain_button;

   data->dialog = dialog = make_default_dialog(_("Grid Settings"));
   default_dialog_set_ok_cb(dialog, grid_settings_ok_cb, (gpointer) data);
   main_table = default_dialog_add_table(dialog, 4, 2);

   data->snap = gtk_check_button_new_with_mnemonic(_("_Snap-to grid enabled"));
   g_signal_connect(data->snap, "toggled",
                    G_CALLBACK (snap_toggled_cb), data);
   gtk_table_attach_defaults(GTK_TABLE(main_table), data->snap, 0, 1, 0, 1);
   gtk_widget_show(data->snap);

   data->type_frame = frame = gimp_frame_new(_("Grid Visibility and Type"));
   gtk_widget_show(frame);
   gtk_table_attach_defaults(GTK_TABLE(main_table), frame, 0, 2, 1, 2);
   hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
   gtk_container_add(GTK_CONTAINER(frame), hbox);
   gtk_widget_show(hbox);

   button = gtk_radio_button_new_with_mnemonic_from_widget(NULL, _("_Hidden"));
   data->hidden = button;
   g_signal_connect(button, "toggled",
                    G_CALLBACK (type_toggled_cb), (gpointer) GRID_HIDDEN);
   gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
   gtk_widget_show(button);

   button = gtk_radio_button_new_with_mnemonic_from_widget(
      GTK_RADIO_BUTTON(button), _("_Lines"));
   data->lines = button;
   g_signal_connect(button, "toggled",
                    G_CALLBACK (type_toggled_cb), (gpointer) GRID_LINES);
   gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), TRUE);
   gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
   gtk_widget_show(button);

   button = gtk_radio_button_new_with_mnemonic_from_widget(
      GTK_RADIO_BUTTON(button), _("C_rosses"));
   data->crosses = button;
   g_signal_connect(button, "toggled",
                    G_CALLBACK (type_toggled_cb),
                    (gpointer) GRID_CROSSES);
   gtk_box_pack_start(GTK_BOX(hbox), button, FALSE, FALSE, 0);
   gtk_widget_show(button);

   data->granularity_frame = frame = gimp_frame_new(_("Grid Granularity"));
   gtk_table_attach_defaults(GTK_TABLE(main_table), frame, 0, 1, 2, 3);
   table = gtk_table_new(2, 4, FALSE);
   gtk_table_set_row_spacings(GTK_TABLE(table), 6);
   gtk_table_set_col_spacings(GTK_TABLE(table), 6);
   gtk_container_add(GTK_CONTAINER(frame), table);

   label = create_label_in_table(table, 0, 0, _("_Width"));
   data->width = create_spin_button_in_table(table, label, 0, 1, 15, 1, 100);
   g_signal_connect(data->width, "value-changed",
                    G_CALLBACK (width_changed_cb), (gpointer) data);
   create_label_in_table(table, 0, 3, _("pixels"));

   label = create_label_in_table(table, 1, 0, _("_Height"));
   data->height = create_spin_button_in_table(table, label, 1, 1, 15, 1, 100);
   g_signal_connect(data->height, "value-changed",
                    G_CALLBACK (height_changed_cb), (gpointer) data);
   create_label_in_table(table, 1, 3, _("pixels"));

   chain_button = gimp_chain_button_new(GIMP_CHAIN_RIGHT);
   data->chain_width_height = chain_button;
   gtk_table_attach_defaults(GTK_TABLE(table), chain_button, 2, 3, 0, 2);
   gtk_widget_show(chain_button);

   gtk_widget_show(table);
   gtk_widget_show(frame);

   data->offset_frame = frame = gimp_frame_new(_("Grid Offset"));
   gtk_table_attach_defaults(GTK_TABLE(main_table), frame, 1, 2, 2, 3);
   table = gtk_table_new(2, 3, FALSE);
   gtk_table_set_row_spacings(GTK_TABLE(table), 6);
   gtk_table_set_col_spacings(GTK_TABLE(table), 6);
   gtk_container_add(GTK_CONTAINER(frame), table);

   label = create_label_in_table(table, 0, 2, _("pixels from l_eft"));
   data->left = create_spin_button_in_table(table, label, 0, 0, 0, 0, 100);
   g_signal_connect(data->left, "value-changed",
                    G_CALLBACK (left_changed_cb), (gpointer) data);

   label = create_label_in_table(table, 1, 2, _("pixels from _top"));
   data->top = create_spin_button_in_table(table, label, 1, 0, 0, 0, 100);
   g_signal_connect(data->top, "value-changed",
                    G_CALLBACK (top_changed_cb), (gpointer) data);

   chain_button = gimp_chain_button_new(GIMP_CHAIN_RIGHT);
   data->chain_left_top = chain_button;
   gtk_table_attach_defaults(GTK_TABLE(table), chain_button, 1, 2, 0, 2);
   gtk_widget_show(chain_button);

   data->preview = create_check_button_in_table(main_table, 3, 0,
                                                _("_Preview"));
   g_signal_connect(data->preview, "toggled",
                    G_CALLBACK (toggle_preview_cb), (gpointer) data);
   gtk_widget_show(data->preview);

   snap_toggled_cb(data->snap, data);

   gtk_widget_show(table);
   gtk_widget_show(frame);

   return data;
}

void
do_grid_settings_dialog(void)
{
   static GridDialog_t* dialog;
   GtkWidget *type;

   if (!dialog)
      dialog = create_grid_settings_dialog();

   gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(dialog->snap), grid_snap);
   gtk_spin_button_set_value(GTK_SPIN_BUTTON(dialog->width), grid_width);
   gtk_spin_button_set_value(GTK_SPIN_BUTTON(dialog->height), grid_height);
   gtk_spin_button_set_value(GTK_SPIN_BUTTON(dialog->left), grid_left);
   gtk_spin_button_set_value(GTK_SPIN_BUTTON(dialog->top), grid_top);

   if (grid_type == GRID_HIDDEN)
      type = dialog->hidden;
   else if (grid_type == GRID_LINES)
      type = dialog->lines;
   else
      type = dialog->crosses;
   gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(type), TRUE);

   default_dialog_show(dialog->dialog);
}

static void
draw_lines(cairo_t *cr, gint width, gint height)
{
   gint x, y;
   gdouble dash = 4.;

   cairo_set_dash (cr, &dash, 1, 0.);
   for (x = grid_left % grid_width; x < width; x += grid_width)
      draw_line(cr, x, 1, x, height);
   for (y = grid_top % grid_height; y < height; y += grid_height)
      draw_line(cr, 1, y, width, y);
}

static void
draw_crosses(cairo_t *cr, gint width, gint height)
{
   gint x, y;
   gdouble dash[4] = { 7., grid_height - 7., 7., grid_width - 7. };

   cairo_set_dash (cr, dash, 2, 4.5 - grid_top);
   for (x = grid_left % grid_width; x < width; x += grid_width)
      draw_line(cr, x, 1, x, height);
   cairo_set_dash (cr, dash+2, 2, 4.5 - grid_left);
   for (y = grid_top % grid_height; y < height; y += grid_height)
      draw_line(cr, 1, y, width, y);
}

void
draw_grid(cairo_t *cr, gint width, gint height)
{
  if (grid_snap && grid_type != GRID_HIDDEN)
    {
      cairo_save (cr);
      if (grid_type == GRID_LINES)
        {
          draw_lines(cr, width, height);
        }
      else
        {
          draw_crosses(cr, width, height);
        }
      cairo_restore (cr);
    }
}

void
toggle_grid(void)
{
   grid_snap = !grid_snap;
   preview_redraw();
}

static gint
grid_nearest_x(gint x)
{
   return grid_left + (x - grid_left + grid_width / 2) / grid_width
      * grid_width;
}

static gint
grid_nearest_y(gint y)
{
   return grid_top + (y - grid_top + grid_height / 2) / grid_height
      * grid_height;
}

void
round_to_grid(gint *x, gint *y)
{
   if (grid_snap) {
      *x = grid_nearest_x(*x);
      *y = grid_nearest_y(*y);
   }
}

gboolean
grid_near_x(gint x)
{
   return grid_snap && grid_type != GRID_HIDDEN
      && abs(grid_nearest_x(x) - x) <= 1;
}

gboolean
grid_near_y(gint y)
{
   return grid_snap && grid_type != GRID_HIDDEN
      && abs(grid_nearest_x(y) - y) <= 1;
}