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
|
/* The GIMP -- an image manipulation program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdlib.h>
#include "appenv.h"
#include "draw_core.h"
#include "gdisplay.h"
#include "gimprc.h"
#include "info_window.h"
#include "magnify.h"
#include "scale.h"
/* types of magnify operations */
#define ZOOMIN 0
#define ZOOMOUT 1
/* local structures */
typedef struct _MagnifyOptions MagnifyOptions;
struct _MagnifyOptions
{
int dummy;
};
typedef struct _magnify Magnify;
struct _magnify
{
DrawCore * core; /* Core select object */
int x, y; /* upper left hand coordinate */
int w, h; /* width and height */
int op; /* magnify operation */
};
/* magnify utility functions */
static void zoom_in (int *, int *, int);
static void zoom_out (int *, int *, int);
/* magnify action functions */
static void magnify_button_press (Tool *, GdkEventButton *, gpointer);
static void magnify_button_release (Tool *, GdkEventButton *, gpointer);
static void magnify_motion (Tool *, GdkEventMotion *, gpointer);
static void magnify_cursor_update (Tool *, GdkEventMotion *, gpointer);
static void magnify_control (Tool *, int, gpointer);
static void *magnify_options = NULL;
/* magnify utility functions */
static void
zoom_in (int *src,
int *dest,
int scale)
{
while (scale--)
{
if (*src > 1)
(*src)--;
else
if (*dest < 0x10)
(*dest)++;
}
}
static void
zoom_out (int *src,
int *dest,
int scale)
{
while (scale--)
{
if (*dest > 1)
(*dest)--;
else
if (*src < 0x10)
(*src)++;
}
}
/* magnify action functions */
static void
magnify_toggle_update (GtkWidget *w,
gpointer data)
{
int *toggle_val;
toggle_val = (int *) data;
if (GTK_TOGGLE_BUTTON (w)->active)
*toggle_val = TRUE;
else
*toggle_val = FALSE;
}
static MagnifyOptions *
create_magnify_options (void)
{
MagnifyOptions *options;
GtkWidget *vbox;
GtkWidget *label;
GtkWidget *allow_resize_toggle;
/* the new options structure */
options = (MagnifyOptions *) g_malloc (sizeof (MagnifyOptions));
/* the main vbox */
vbox = gtk_vbox_new (FALSE, 1);
/* the main label */
label = gtk_label_new ("Magnify Options");
gtk_box_pack_start (GTK_BOX (vbox), label, FALSE, FALSE, 0);
gtk_widget_show (label);
/* the allow_resize toggle button */
allow_resize_toggle = gtk_check_button_new ();
gtk_box_pack_start (GTK_BOX (vbox), allow_resize_toggle, FALSE, FALSE, 0);
label = gtk_label_new ("Allow Window Resizing");
gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
gtk_container_add (GTK_CONTAINER (allow_resize_toggle), label);
gtk_widget_show (label);
gtk_signal_connect (GTK_OBJECT (allow_resize_toggle), "toggled",
(GtkSignalFunc) magnify_toggle_update,
&allow_resize_windows);
gtk_widget_show (allow_resize_toggle);
gtk_toggle_button_set_state (GTK_TOGGLE_BUTTON (allow_resize_toggle), allow_resize_windows);
/* Register this selection options widget with the main tools options dialog */
tools_register_options (MAGNIFY, vbox);
return options;
}
static void
magnify_button_press (Tool *tool,
GdkEventButton *bevent,
gpointer gdisp_ptr)
{
GDisplay * gdisp;
Magnify * magnify;
int x, y;
gdisp = (GDisplay *) gdisp_ptr;
magnify = (Magnify *) tool->private;
gdisplay_untransform_coords (gdisp, bevent->x, bevent->y, &x, &y, TRUE, 0);
magnify->x = x;
magnify->y = y;
magnify->w = 0;
magnify->h = 0;
gdk_pointer_grab (gdisp->canvas->window, FALSE,
GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON1_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
NULL, NULL, bevent->time);
tool->state = ACTIVE;
tool->gdisp_ptr = gdisp_ptr;
if (bevent->state & GDK_SHIFT_MASK)
magnify->op = ZOOMOUT;
else
magnify->op = ZOOMIN; /* default */
draw_core_start (magnify->core,
gdisp->canvas->window,
tool);
}
static void
magnify_button_release (Tool *tool,
GdkEventButton *bevent,
gpointer gdisp_ptr)
{
Magnify * magnify;
GDisplay * gdisp;
int win_width, win_height;
int width, height;
int scalesrc, scaledest;
int scale;
int x1, y1, x2, y2, w, h;
gdisp = (GDisplay *) gdisp_ptr;
magnify = (Magnify *) tool->private;
gdk_pointer_ungrab (bevent->time);
gdk_flush ();
draw_core_stop (magnify->core, tool);
tool->state = INACTIVE;
/* First take care of the case where the user "cancels" the action */
if (! (bevent->state & GDK_BUTTON3_MASK))
{
x1 = (magnify->w < 0) ? magnify->x + magnify->w : magnify->x;
y1 = (magnify->h < 0) ? magnify->y + magnify->h : magnify->y;
w = (magnify->w < 0) ? -magnify->w : magnify->w;
h = (magnify->h < 0) ? -magnify->h : magnify->h;
x2 = x1 + w;
y2 = y1 + h;
scalesrc = SCALESRC(gdisp);
scaledest = SCALEDEST(gdisp);
win_width = gdisp->disp_width;
win_height = gdisp->disp_height;
width = (win_width * scalesrc) / scaledest;
height = (win_height * scalesrc) / scaledest;
if (!w || !h)
scale = 1;
else
scale = MINIMUM ((width / w), (height / h));
switch (magnify->op)
{
case ZOOMIN:
zoom_in (&scalesrc, &scaledest, scale);
break;
case ZOOMOUT:
zoom_out (&scalesrc, &scaledest, scale);
break;
}
gdisp->scale = (scaledest << 8) + scalesrc;
gdisp->offset_x = (scaledest * ((x1 + x2) / 2)) / scalesrc -
(win_width / 2);
gdisp->offset_y = (scaledest * ((y1 + y2) / 2)) / scalesrc -
(win_height / 2);
/* resize the image */
resize_display (gdisp, allow_resize_windows, TRUE);
}
}
static void
magnify_motion (Tool *tool,
GdkEventMotion *mevent,
gpointer gdisp_ptr)
{
Magnify * magnify;
GDisplay * gdisp;
int x, y;
if (tool->state != ACTIVE)
return;
gdisp = (GDisplay *) gdisp_ptr;
magnify = (Magnify *) tool->private;
draw_core_pause (magnify->core, tool);
gdisplay_untransform_coords (gdisp, mevent->x, mevent->y, &x, &y, TRUE, 0);
magnify->w = (x - magnify->x);
magnify->h = (y - magnify->y);
draw_core_resume (magnify->core, tool);
}
static void
magnify_cursor_update (Tool *tool,
GdkEventMotion *mevent,
gpointer gdisp_ptr)
{
GDisplay * gdisp;
gdisp = (GDisplay *) gdisp_ptr;
gdisplay_install_tool_cursor (gdisp, GDK_TCROSS);
}
void
magnify_draw (Tool *tool)
{
GDisplay * gdisp;
Magnify * magnify;
int x1, y1, x2, y2;
gdisp = (GDisplay *) tool->gdisp_ptr;
magnify = (Magnify *) tool->private;
x1 = MINIMUM (magnify->x, magnify->x + magnify->w);
y1 = MINIMUM (magnify->y, magnify->y + magnify->h);
x2 = MAXIMUM (magnify->x, magnify->x + magnify->w);
y2 = MAXIMUM (magnify->y, magnify->y + magnify->h);
gdisplay_transform_coords (gdisp, x1, y1, &x1, &y1, 0);
gdisplay_transform_coords (gdisp, x2, y2, &x2, &y2, 0);
gdk_draw_rectangle (magnify->core->win, magnify->core->gc, 0,
x1, y1, (x2 - x1), (y2 - y1));
}
static void
magnify_control (Tool *tool,
int action,
gpointer gdisp_ptr)
{
Magnify * magnify;
magnify = (Magnify *) tool->private;
switch (action)
{
case PAUSE :
draw_core_pause (magnify->core, tool);
break;
case RESUME :
draw_core_resume (magnify->core, tool);
break;
case HALT :
draw_core_stop (magnify->core, tool);
break;
}
}
Tool *
tools_new_magnify (void)
{
Tool * tool;
Magnify * private;
if (! magnify_options)
magnify_options = create_magnify_options ();
tool = (Tool *) g_malloc (sizeof (Tool));
private = (Magnify *) g_malloc (sizeof (Magnify));
private->core = draw_core_new (magnify_draw);
private->x = private->y = 0;
private->w = private->h = 0;
tool->type = MAGNIFY;
tool->state = INACTIVE;
tool->scroll_lock = 1; /* disallow scrolling */
tool->auto_snap_to = FALSE;
tool->private = (void *) private;
tool->button_press_func = magnify_button_press;
tool->button_release_func = magnify_button_release;
tool->motion_func = magnify_motion;
tool->arrow_keys_func = standard_arrow_keys_func;
tool->cursor_update_func = magnify_cursor_update;
tool->control_func = magnify_control;
return tool;
}
void
tools_free_magnify (Tool *tool)
{
Magnify * magnify;
magnify = (Magnify *) tool->private;
draw_core_free (magnify->core);
g_free (magnify);
}
|