1 /*
2  * This is a plug-in for GIMP.
3  *
4  * Generates clickable image maps.
5  *
6  * Copyright (C) 1998-2003 Maurits Rijk  lpeek.mrijk@consunet.nl
7  *
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 3 of the License, or
11  * (at your option) any later version.
12  *
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.
17  *
18  * 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/>.
20  *
21  */
22 
23 #include "config.h"
24 
25 #include <gtk/gtk.h>
26 
27 #include "imap_commands.h"
28 #include "imap_main.h"
29 
30 #include "libgimp/stdplugins-intl.h"
31 
32 static void move_sash_command_destruct(Command_t *command);
33 static CmdExecuteValue_t move_sash_command_execute(Command_t *command);
34 static void move_sash_command_redo(Command_t *command);
35 
36 static CommandClass_t move_sash_command_class = {
37    move_sash_command_destruct,
38    move_sash_command_execute,
39    NULL /*undo*/,
40    move_sash_command_redo
41 };
42 
43 typedef struct {
44    Command_t    parent;
45    GtkWidget   *widget;
46    Object_t    *obj;
47    gint         x;
48    gint         y;
49    gint         image_width;
50    gint         image_height;
51    MoveSashFunc_t sash_func;
52 } MoveSashCommand_t;
53 
54 Command_t*
move_sash_command_new(GtkWidget * widget,Object_t * obj,gint x,gint y,MoveSashFunc_t sash_func)55 move_sash_command_new(GtkWidget *widget, Object_t *obj,
56                       gint x, gint y, MoveSashFunc_t sash_func)
57 {
58    MoveSashCommand_t *command = g_new(MoveSashCommand_t, 1);
59    Command_t *parent;
60 
61    command->widget = widget;
62    command->obj = object_ref(obj);
63    command->x = x;
64    command->y = y;
65    command->image_width = get_image_width();
66    command->image_height = get_image_height();
67    command->sash_func = sash_func;
68 
69    parent = command_init(&command->parent, _("Move Sash"),
70                          &move_sash_command_class);
71    command_add_subcommand(parent, edit_object_command_new(obj));
72 
73    return parent;
74 }
75 
76 static void
move_sash_command_destruct(Command_t * parent)77 move_sash_command_destruct(Command_t *parent)
78 {
79    MoveSashCommand_t *command = (MoveSashCommand_t*) parent;
80    object_unref(command->obj);
81 }
82 
83 static void
sash_move(GtkWidget * widget,GdkEventMotion * event,gpointer data)84 sash_move(GtkWidget *widget, GdkEventMotion *event, gpointer data)
85 {
86    MoveSashCommand_t *command = (MoveSashCommand_t*) data;
87    Object_t *obj = command->obj;
88    gint x, y, dx, dy;
89 
90    x = get_real_coord((gint) event->x);
91    y = get_real_coord((gint) event->y);
92 
93    if (x < 0)
94       x = 0;
95    if (x > command->image_width)
96       x = command->image_width;
97 
98    if (y < 0)
99       y = 0;
100    if (y > command->image_height)
101       y = command->image_height;
102 
103    dx = x - command->x;
104    dy = y - command->y;
105 
106    command->x = x;
107    command->y = y;
108 
109    command->sash_func(obj, dx, dy);
110    object_emit_geometry_signal(obj);
111 
112    preview_redraw ();
113 }
114 
115 static void
sash_end(GtkWidget * widget,GdkEventButton * event,gpointer data)116 sash_end(GtkWidget *widget, GdkEventButton *event, gpointer data)
117 {
118    MoveSashCommand_t *command = (MoveSashCommand_t*) data;
119    Object_t *obj = command->obj;
120 
121    g_signal_handlers_disconnect_by_func(widget,
122                                         sash_move, data);
123    g_signal_handlers_disconnect_by_func(widget,
124                                         sash_end, data);
125    if (obj->class->normalize)
126       object_normalize(obj);
127    preview_unset_tmp_obj(command->obj);
128    preview_redraw();
129    show_url();
130 }
131 
132 static CmdExecuteValue_t
move_sash_command_execute(Command_t * parent)133 move_sash_command_execute(Command_t *parent)
134 {
135    MoveSashCommand_t *command = (MoveSashCommand_t*) parent;
136 
137    hide_url();
138    g_signal_connect(command->widget, "button-release-event",
139                     G_CALLBACK (sash_end), command);
140    g_signal_connect(command->widget, "motion-notify-event",
141                     G_CALLBACK (sash_move), command);
142    preview_set_tmp_obj(command->obj);
143 
144    return CMD_APPEND;
145 }
146 
move_sash_command_redo(Command_t * command)147 static void move_sash_command_redo(Command_t *command)
148 {
149    /* do nothing, but avoid running execute again which will break event handling */
150 }
151