home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / plug-ins / imagemap / imap_cmd_move.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-22  |  5.2 KB  |  165 lines

  1. /*
  2.  * This is a plug-in for the GIMP.
  3.  *
  4.  * Generates clickable image maps.
  5.  *
  6.  * Copyright (C) 1998-1999 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 2 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, write to the Free Software
  20.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  21.  *
  22.  */
  23.  
  24. #include "imap_cmd_move.h"
  25. #include "imap_cmd_object_move.h"
  26. #include "libgimp/stdplugins-intl.h"
  27. #include "imap_main.h"
  28.  
  29. static void move_command_destruct(Command_t *parent);
  30. static CmdExecuteValue_t move_command_execute(Command_t *parent);
  31.  
  32. CommandClass_t move_command_class = {
  33.    move_command_destruct,
  34.    move_command_execute,
  35.    NULL,            /* move_command_undo */
  36.    NULL                /* move_command_redo */
  37. };
  38.  
  39. typedef struct {
  40.    Command_t parent;
  41.    PreferencesData_t *preferences;
  42.    Preview_t *preview;
  43.    Object_t *obj;
  44.    gint start_x;
  45.    gint start_y;
  46.    gint obj_start_x;
  47.    gint obj_start_y;
  48.    gint obj_x;
  49.    gint obj_y;
  50.    gint obj_width;
  51.    gint obj_height;
  52.  
  53.    gint image_width;
  54.    gint image_height;
  55.  
  56.    GdkCursorType cursor;    /* Remember previous cursor */
  57.    gboolean moved_first_time;
  58. } MoveCommand_t;
  59.  
  60. Command_t* 
  61. move_command_new(Preview_t *preview, Object_t *obj, gint x, gint y)
  62. {
  63.    MoveCommand_t *command = g_new(MoveCommand_t, 1);
  64.  
  65.    command->preferences = get_preferences();
  66.    command->preview = preview;
  67.    command->obj = object_ref(obj);
  68.    command->start_x = x;
  69.    command->start_y = y;
  70.    object_get_dimensions(obj, &command->obj_x, &command->obj_y, 
  71.              &command->obj_width, &command->obj_height);
  72.    command->obj_start_x = command->obj_x;
  73.    command->obj_start_y = command->obj_y;
  74.  
  75.    command->image_width = get_image_width();
  76.    command->image_height = get_image_height();
  77.  
  78.    command->moved_first_time = TRUE;
  79.  
  80.    return command_init(&command->parent, _("Move"), &move_command_class);
  81. }
  82.  
  83. static void
  84. move_command_destruct(Command_t *parent)
  85. {
  86.    MoveCommand_t *command = (MoveCommand_t*) parent;
  87.    object_unref(command->obj);
  88. }
  89.  
  90. static void
  91. button_motion(GtkWidget *widget, GdkEventMotion *event, gpointer data)
  92. {
  93.    MoveCommand_t *command = (MoveCommand_t*) data;
  94.    Object_t *obj = command->obj;
  95.    gint dx = get_real_coord((gint) event->x) - command->start_x;
  96.    gint dy = get_real_coord((gint) event->y) - command->start_y;
  97.  
  98.    if (command->moved_first_time) {
  99.       command->moved_first_time = FALSE;
  100.       command->cursor = preview_set_cursor(command->preview, GDK_FLEUR);
  101.       gdk_gc_set_function(command->preferences->normal_gc, GDK_EQUIV);
  102.       gdk_gc_set_function(command->preferences->selected_gc, GDK_EQUIV);
  103.       hide_url();
  104.    }
  105.  
  106.    if (command->obj_x + dx < 0)
  107.       dx = -command->obj_x;
  108.    if (command->obj_x + command->obj_width + dx > command->image_width)
  109.       dx = command->image_width - command->obj_width - command->obj_x;
  110.    if (command->obj_y + dy < 0)
  111.       dy = -command->obj_y;
  112.    if (command->obj_y + command->obj_height + dy > command->image_height)
  113.       dy = command->image_height - command->obj_height - command->obj_y;
  114.  
  115.    if (dx || dy) {
  116.       command->start_x = get_real_coord((gint) event->x);
  117.       command->start_y = get_real_coord((gint) event->y);
  118.       command->obj_x += dx;
  119.       command->obj_y += dy;
  120.  
  121.       object_draw(obj, widget->window);
  122.       object_move(obj, dx, dy);
  123.       object_draw(obj, widget->window);
  124.    }
  125. }
  126.  
  127. static void
  128. button_release(GtkWidget *widget, GdkEventButton *event, gpointer data)
  129. {
  130.    MoveCommand_t *command = (MoveCommand_t*) data;
  131.  
  132.    gtk_signal_disconnect_by_func(GTK_OBJECT(widget), 
  133.                  (GtkSignalFunc) button_motion, data);
  134.    gtk_signal_disconnect_by_func(GTK_OBJECT(widget), 
  135.                  (GtkSignalFunc) button_release, data);
  136.  
  137.    if (!command->moved_first_time) {
  138.       preview_set_cursor(command->preview, command->cursor);
  139.       gdk_gc_set_function(command->preferences->normal_gc, GDK_COPY);
  140.       gdk_gc_set_function(command->preferences->selected_gc, GDK_COPY);
  141.       show_url();
  142.    }
  143.    command->obj_x -= command->obj_start_x;
  144.    command->obj_y -= command->obj_start_y;
  145.    if (command->obj_x || command->obj_y)
  146.       command_list_add(object_move_command_new(command->obj, command->obj_x, 
  147.                            command->obj_y));
  148.  
  149.    preview_thaw();
  150. }
  151.  
  152. static CmdExecuteValue_t
  153. move_command_execute(Command_t *parent)
  154. {
  155.    MoveCommand_t *command = (MoveCommand_t*) parent;
  156.    GtkWidget *widget = command->preview->preview;
  157.  
  158.    preview_freeze();
  159.    gtk_signal_connect(GTK_OBJECT(widget), "button_release_event", 
  160.               (GtkSignalFunc) button_release, command);   
  161.    gtk_signal_connect(GTK_OBJECT(widget), "motion_notify_event", 
  162.               (GtkSignalFunc) button_motion, command);   
  163.    return CMD_DESTRUCT;
  164. }
  165.