imap_cmd_guides.c 9.92 KB
Newer Older
Sven Neumann's avatar
Sven Neumann committed
1
/*
2
 * This is a plug-in for GIMP.
Sven Neumann's avatar
Sven Neumann committed
3 4 5
 *
 * Generates clickable image maps.
 *
6
 * Copyright (C) 1998-2004 Maurits Rijk  m.rijk@chello.nl
Sven Neumann's avatar
Sven Neumann committed
7
 *
8
 * This program is free software: you can redistribute it and/or modify
Sven Neumann's avatar
Sven Neumann committed
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation; either version 3 of the License, or
Sven Neumann's avatar
Sven Neumann committed
11 12 13 14 15 16 17 18
 * (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
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
Sven Neumann's avatar
Sven Neumann committed
20 21 22
 *
 */

23 24
#include "config.h"

25 26
#include "libgimp/gimp.h"
#include "libgimp/gimpui.h"
27

28
#include "imap_commands.h"
Sven Neumann's avatar
Sven Neumann committed
29 30 31
#include "imap_default_dialog.h"
#include "imap_main.h"
#include "imap_rectangle.h"
32
#include "imap_ui_grid.h"
Sven Neumann's avatar
Sven Neumann committed
33

34 35
#include "libgimp/stdplugins-intl.h"

Sven Neumann's avatar
Sven Neumann committed
36
typedef struct {
37 38 39 40 41 42 43 44 45 46 47 48 49 50
   DefaultDialog_t      *dialog;
   GtkWidget            *image_dimensions;
   GtkWidget            *guide_bounds;
   GtkWidget            *width;
   GtkWidget            *height;
   GtkWidget            *left;
   GtkWidget            *top;
   GtkWidget            *horz_spacing;
   GtkWidget            *vert_spacing;
   GtkWidget            *no_across;
   GtkWidget            *no_down;
   GtkWidget            *base_url;

   ObjectList_t         *list;
Sven Neumann's avatar
Sven Neumann committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
} GuidesDialog_t;

static void
guides_ok_cb(gpointer data)
{
   GuidesDialog_t *param = (GuidesDialog_t*) data;
   gint y;
   int i, j;
   gint width, height, left, top, hspace, vspace, rows, cols;

   width = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(param->width));
   height = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(param->height));
   left = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(param->left));
   top = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(param->top));
   hspace = gtk_spin_button_get_value_as_int(
      GTK_SPIN_BUTTON(param->horz_spacing));
   vspace = gtk_spin_button_get_value_as_int(
      GTK_SPIN_BUTTON(param->vert_spacing));
   rows = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(param->no_down));
   cols = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(param->no_across));

72
   subcommand_start(_("Create Guides"));
Sven Neumann's avatar
Sven Neumann committed
73 74 75 76
   y = top;
   for (i = 0; i < rows; i++) {
      gint x = left;
      for (j = 0; j < cols; j++) {
77 78
         Object_t *obj = create_rectangle(x, y, width, height);
         Command_t *command = create_command_new(param->list, obj);
Sven Neumann's avatar
Sven Neumann committed
79

80 81 82
         object_set_url(obj, gtk_entry_get_text(GTK_ENTRY(param->base_url)));
         command_execute(command);
         x += width + hspace;
Sven Neumann's avatar
Sven Neumann committed
83 84 85 86 87 88 89 90 91 92 93 94
      }
      y += height + vspace;
   }
   subcommand_end();
}

static void
recalc_bounds(GtkWidget *widget, gpointer data)
{
   GuidesDialog_t *param = (GuidesDialog_t*) data;
   gint width, height, left, top, hspace, vspace, rows, cols;
   gint bound_w, bound_h;
95
   gchar *bounds;
Sven Neumann's avatar
Sven Neumann committed
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110

   width = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(param->width));
   height = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(param->height));
   left = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(param->left));
   top = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(param->top));
   hspace = gtk_spin_button_get_value_as_int(
      GTK_SPIN_BUTTON(param->horz_spacing));
   vspace = gtk_spin_button_get_value_as_int(
      GTK_SPIN_BUTTON(param->vert_spacing));
   rows = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(param->no_down));
   cols = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(param->no_across));

   bound_w = (width + hspace) * cols - hspace;
   bound_h = (height + vspace) * rows - vspace;

111
   bounds = g_strdup_printf (_("Resulting Guide Bounds: %d,%d to %d,%d (%d areas)"),
112
                             left, top, left + bound_w, top + bound_h, rows * cols);
113
   if (left + bound_w > get_image_width() ||
114 115 116 117 118 119 120 121 122 123 124
       top + bound_h > get_image_height())
     {
       gtk_dialog_set_response_sensitive (GTK_DIALOG (param->dialog->dialog),
                                          GTK_RESPONSE_OK, FALSE);
     }
   else
     {
       gtk_dialog_set_response_sensitive (GTK_DIALOG (param->dialog->dialog),
                                          GTK_RESPONSE_OK, TRUE);
     }

Sven Neumann's avatar
Sven Neumann committed
125
   gtk_label_set_text(GTK_LABEL(param->guide_bounds), bounds);
126
   g_free (bounds);
Sven Neumann's avatar
Sven Neumann committed
127 128 129
}

static GuidesDialog_t*
130
make_guides_dialog (void)
Sven Neumann's avatar
Sven Neumann committed
131 132 133
{
   GuidesDialog_t *data = g_new(GuidesDialog_t, 1);
   DefaultDialog_t *dialog;
134
   GtkWidget *grid;
Sven Neumann's avatar
Sven Neumann committed
135 136
   GtkWidget *label;
   GtkWidget *hbox;
137

138
   dialog = data->dialog = make_default_dialog(_("Create Guides"));
139 140
   default_dialog_set_ok_cb (dialog, guides_ok_cb, data);

141
   hbox = gimp_hint_box_new (
142
      _("Guides are pre-defined rectangles covering the image. You define "
143 144 145
        "them by their width, height, and spacing from each other. This "
        "allows you to rapidly create the most common image map type - "
        "image collection of \"thumbnails\", suitable for navigation bars."));
146 147
   gtk_box_pack_start (GTK_BOX (dialog->vbox), hbox, FALSE, FALSE, 0);
   gtk_widget_show (hbox);
Sven Neumann's avatar
Sven Neumann committed
148

149
   data->image_dimensions = gtk_label_new ("");
150
   gtk_label_set_xalign (GTK_LABEL (data->image_dimensions), 0.0);
151 152 153
   gtk_box_pack_start (GTK_BOX (dialog->vbox),
                       data->image_dimensions, FALSE, FALSE, 0);
   gtk_widget_show (data->image_dimensions);
Sven Neumann's avatar
Sven Neumann committed
154

155
   data->guide_bounds = gtk_label_new ("");
156
   gtk_label_set_xalign (GTK_LABEL (data->guide_bounds), 0.0);
157 158 159 160
   gtk_box_pack_start (GTK_BOX (dialog->vbox),
                       data->guide_bounds, FALSE, FALSE, 0);
   gtk_widget_show (data->guide_bounds);

161
   grid = default_dialog_add_grid (dialog);
162

163 164
   label = create_label_in_grid (grid, 0, 0, _("_Width:"));
   data->width = create_spin_button_in_grid (grid, label, 0, 1, 32, 1, 100);
165 166
   g_signal_connect (data->width, "changed",
                     G_CALLBACK(recalc_bounds), (gpointer) data);
Sven Neumann's avatar
Sven Neumann committed
167

168 169
   label = create_label_in_grid (grid, 0, 2, _("_Left start at:"));
   data->left = create_spin_button_in_grid  (grid, label, 0, 3, 0, 0, 100);
170 171
   g_signal_connect (data->left, "changed",
                     G_CALLBACK(recalc_bounds), (gpointer) data);
Sven Neumann's avatar
Sven Neumann committed
172

173 174
   label = create_label_in_grid (grid, 1, 0, _("_Height:"));
   data->height = create_spin_button_in_grid (grid, label, 1, 1, 32, 1, 100);
175 176
   g_signal_connect (data->height, "changed",
                     G_CALLBACK(recalc_bounds), (gpointer) data);
Sven Neumann's avatar
Sven Neumann committed
177

178 179
   label = create_label_in_grid (grid, 1, 2, _("_Top start at:"));
   data->top = create_spin_button_in_grid (grid, label, 1, 3, 0, 0, 100);
180 181
   g_signal_connect (data->top, "changed",
                     G_CALLBACK(recalc_bounds), (gpointer) data);
Sven Neumann's avatar
Sven Neumann committed
182

183 184
   label = create_label_in_grid (grid, 2, 0, _("_Horz. spacing:"));
   data->horz_spacing = create_spin_button_in_grid (grid, label, 2, 1, 0, 0,
185 186 187
                                                     100);
   g_signal_connect (data->horz_spacing, "changed",
                     G_CALLBACK(recalc_bounds), (gpointer) data);
Sven Neumann's avatar
Sven Neumann committed
188

189 190
   label = create_label_in_grid (grid, 2, 2, _("_No. across:"));
   data->no_across = create_spin_button_in_grid (grid, label, 2, 3, 0, 0,
191
                                                 100);
192 193
   g_signal_connect (data->no_across, "changed",
                     G_CALLBACK(recalc_bounds), (gpointer) data);
Sven Neumann's avatar
Sven Neumann committed
194

195 196
   label = create_label_in_grid (grid, 3, 0, _("_Vert. spacing:"));
   data->vert_spacing = create_spin_button_in_grid (grid, label, 3, 1, 0, 0,
197
                                                    100);
198 199
   g_signal_connect (data->vert_spacing, "changed",
                     G_CALLBACK(recalc_bounds), (gpointer) data);
Sven Neumann's avatar
Sven Neumann committed
200

201 202
   label = create_label_in_grid (grid, 3, 2, _("No. _down:"));
   data->no_down = create_spin_button_in_grid (grid, label, 3, 3, 0, 0, 100);
203 204 205
   g_signal_connect (data->no_down, "changed",
                     G_CALLBACK(recalc_bounds), (gpointer) data);

206
   hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
207
   gtk_box_pack_start (GTK_BOX (dialog->vbox), hbox, TRUE, TRUE, 0);
208
   gtk_widget_show(hbox);
Sven Neumann's avatar
Sven Neumann committed
209

210
   label = gtk_label_new_with_mnemonic(_("Base _URL:"));
211
   gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
Sven Neumann's avatar
Sven Neumann committed
212
   gtk_widget_show(label);
213 214

   data->base_url = gtk_entry_new ();
215
   gtk_box_pack_start (GTK_BOX (hbox), data->base_url, TRUE, TRUE, 0);
Sven Neumann's avatar
Sven Neumann committed
216
   gtk_widget_show(data->base_url);
217 218

   gtk_label_set_mnemonic_widget (GTK_LABEL (label), data->base_url);
Sven Neumann's avatar
Sven Neumann committed
219 220 221 222 223 224 225

   return data;
}

static void
init_guides_dialog(GuidesDialog_t *dialog, ObjectList_t *list)
{
226
   gchar *dimension;
Sven Neumann's avatar
Sven Neumann committed
227 228

   dialog->list = list;
229 230 231 232
   dimension = g_strdup_printf (_("Image dimensions: %d × %d"),
                                get_image_width(),
                                get_image_height());
   gtk_label_set_text (GTK_LABEL(dialog->image_dimensions), dimension);
233
   g_free (dimension);
234 235 236
   gtk_label_set_text (GTK_LABEL(dialog->guide_bounds),
                       _("Resulting Guide Bounds: 0,0 to 0,0 (0 areas)"));
   gtk_widget_grab_focus (dialog->width);
Sven Neumann's avatar
Sven Neumann committed
237 238 239
}

static void
240
do_create_guides_dialog_local (ObjectList_t *list)
Sven Neumann's avatar
Sven Neumann committed
241 242 243 244 245 246 247 248 249 250
{
   static GuidesDialog_t *dialog;

   if (!dialog)
      dialog = make_guides_dialog();

   init_guides_dialog(dialog, list);
   default_dialog_show(dialog->dialog);
}

251
static CmdExecuteValue_t guides_command_execute(Command_t *parent);
Sven Neumann's avatar
Sven Neumann committed
252 253

static CommandClass_t guides_command_class = {
254
   NULL,                        /* guides_command_destruct */
Sven Neumann's avatar
Sven Neumann committed
255
   guides_command_execute,
256 257
   NULL,                        /* guides_command_undo */
   NULL                         /* guides_command_redo */
Sven Neumann's avatar
Sven Neumann committed
258 259 260 261 262 263 264 265 266 267 268 269
};

typedef struct {
   Command_t parent;
   ObjectList_t *list;
} GuidesCommand_t;

Command_t*
guides_command_new(ObjectList_t *list)
{
   GuidesCommand_t *command = g_new(GuidesCommand_t, 1);
   command->list = list;
270
   return command_init(&command->parent, _("Guides"), &guides_command_class);
Sven Neumann's avatar
Sven Neumann committed
271 272
}

273
static CmdExecuteValue_t
Sven Neumann's avatar
Sven Neumann committed
274 275 276
guides_command_execute(Command_t *parent)
{
   GuidesCommand_t *command = (GuidesCommand_t*) parent;
277
   do_create_guides_dialog_local (command->list);
278
   return CMD_DESTRUCT;
Sven Neumann's avatar
Sven Neumann committed
279
}