magnify.c 4.44 KB
Newer Older
1
/* Dia -- an diagram creation/manipulation program
Alexander Larsson's avatar
Alexander Larsson committed
2 3 4 5 6 7 8 9 10 11 12 13 14
 * Copyright (C) 1998 Alexander Larsson
 *
 * 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
15 16
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
Alexander Larsson's avatar
Alexander Larsson committed
17
 */
Lars Clausen's avatar
Lars Clausen committed
18
#include <config.h>
19
#include <stdlib.h>
20 21
#include <math.h>

Alexander Larsson's avatar
Alexander Larsson committed
22
#include "magnify.h"
23
#include "cursor.h"
Alexander Larsson's avatar
Alexander Larsson committed
24

25 26 27 28 29 30 31 32 33 34
struct _MagnifyTool {
  Tool tool;
  int box_active;
  int moved;
  int x, y;
  int oldx, oldy;

  gboolean zoom_out;
};

Alexander Larsson's avatar
Alexander Larsson committed
35 36 37 38 39 40 41 42
static void
magnify_button_press(MagnifyTool *tool, GdkEventButton *event,
		     DDisplay *ddisp)
{
  tool->x = tool->oldx = event->x;
  tool->y = tool->oldy = event->y;
  tool->box_active = TRUE;
  tool->moved = FALSE;
43
  gdk_pointer_grab (gtk_widget_get_window(ddisp->canvas), FALSE,
Alexander Larsson's avatar
Alexander Larsson committed
44 45 46 47 48 49 50 51 52 53
		    GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON1_MOTION_MASK | GDK_BUTTON_RELEASE_MASK,
		    NULL, NULL, event->time);
}

static void
magnify_button_release(MagnifyTool *tool, GdkEventButton *event,
		       DDisplay *ddisp)
{
  Rectangle *visible;
  Point p1, p2, tl;
54 55
  real diff;
  int idiff;
Alexander Larsson's avatar
Alexander Larsson committed
56 57 58 59
  real factor;

  tool->box_active = FALSE;

Zander's avatar
Zander committed
60 61 62 63 64
  dia_interactive_renderer_set_selection (ddisp->renderer,
                                          FALSE, 0, 0, 0, 0);
  ddisplay_flush (ddisp);


Alexander Larsson's avatar
Alexander Larsson committed
65
  visible = &ddisp->visible;
66

Alexander Larsson's avatar
Alexander Larsson committed
67 68 69 70 71 72
  ddisplay_untransform_coords(ddisp, tool->x, tool->y, &p1.x, &p1.y);
  ddisplay_untransform_coords(ddisp, event->x, event->y, &p2.x, &p2.y);

  tl.x = MIN(p1.x, p2.x);
  tl.y = MIN(p1.y, p2.y);

73 74
  diff = MAX(fabs(p2.x - p1.x), fabs(p2.y - p1.y));
  idiff = MAX(abs(tool->x - event->x), abs(tool->y - event->y));
Alexander Larsson's avatar
Alexander Larsson committed
75 76

  if (tool->moved) {
77 78 79 80
    if (idiff <= 4) {
      ddisplay_add_update_all(ddisp);
      ddisplay_flush(ddisp);
    } else if (!(event->state & GDK_CONTROL_MASK)) {
81 82 83 84
      /* the whole zoom rect should be visible, not just it's square equivalent */
      real fh = fabs(p2.y - p1.y) / (visible->bottom - visible->top);
      real fw = fabs(p2.x - p1.x) / (visible->right - visible->left);
      factor = 1.0 / MAX(fh, fw);
85 86 87
      tl.x += (visible->right - visible->left)/(2.0*factor);
      tl.y += (visible->bottom - visible->top)/(2.0*factor);
      ddisplay_zoom(ddisp, &tl, factor);
Alexander Larsson's avatar
Alexander Larsson committed
88 89 90 91 92 93 94
    } else {
      factor = diff / (visible->right - visible->left);
      tl.x = tl.x * factor + tl.x;
      tl.y = tl.y * factor + tl.y;
      ddisplay_zoom(ddisp, &tl, factor);
    }
  } else {
95 96 97 98
    if (event->state & GDK_SHIFT_MASK)
      ddisplay_zoom(ddisp, &tl, 0.5);
    else
      ddisplay_zoom(ddisp, &tl, 2.0);
Alexander Larsson's avatar
Alexander Larsson committed
99 100 101 102 103 104 105 106 107 108 109 110 111
  }

  gdk_pointer_ungrab (event->time);
}

typedef struct intPoint { int x,y; } intPoint;

static void
magnify_motion(MagnifyTool *tool, GdkEventMotion *event,
	       DDisplay *ddisp)
{
  intPoint tl, br;

112
  if (tool->box_active) {
Alexander Larsson's avatar
Alexander Larsson committed
113 114
    tool->moved = TRUE;

Zander's avatar
Zander committed
115 116
    tl.x = MIN (tool->x, event->x); tl.y = MIN (tool->y, event->y);
    br.x = MAX (tool->x, event->x); br.y = MAX (tool->y, event->y);
Alexander Larsson's avatar
Alexander Larsson committed
117

Zander's avatar
Zander committed
118 119 120 121
    dia_interactive_renderer_set_selection (ddisp->renderer,
                                            TRUE,
                                            tl.x, tl.y, br.x - tl.x, br.y - tl.y);
    ddisplay_flush (ddisp);
Alexander Larsson's avatar
Alexander Larsson committed
122 123 124
  }
}

125 126 127 128
void
set_zoom_out(Tool *tool)
{
  ((MagnifyTool *)tool)->zoom_out = TRUE;
129
  ddisplay_set_all_cursor_name (NULL, "zoom-out");
130 131 132 133 134 135
}

void
set_zoom_in(Tool *tool)
{
  ((MagnifyTool *)tool)->zoom_out = FALSE;
136
  ddisplay_set_all_cursor_name (NULL, "zoom-in");
137 138
}

Alexander Larsson's avatar
Alexander Larsson committed
139 140 141 142 143
Tool *
create_magnify_tool(void)
{
  MagnifyTool *tool;

Hubert Figuiere's avatar
Hubert Figuiere committed
144
  tool = g_new0(MagnifyTool, 1);
Alexander Larsson's avatar
Alexander Larsson committed
145 146 147 148
  tool->tool.type = MAGNIFY_TOOL;
  tool->tool.button_press_func = (ButtonPressFunc) &magnify_button_press;
  tool->tool.button_release_func = (ButtonPressFunc) &magnify_button_release;
  tool->tool.motion_func = (MotionFunc) &magnify_motion;
149
  tool->tool.double_click_func = NULL;
Alexander Larsson's avatar
Alexander Larsson committed
150 151

  tool->box_active = FALSE;
152
  tool->zoom_out = FALSE;
Alexander Larsson's avatar
Alexander Larsson committed
153

154 155
  ddisplay_set_all_cursor_name (NULL, "zoom-in");

Alexander Larsson's avatar
Alexander Larsson committed
156 157 158 159 160 161 162
  return (Tool *) tool;
}

void
free_magnify_tool(Tool *tool)
{
  g_free(tool);
163
  ddisplay_set_all_cursor(default_cursor);
Alexander Larsson's avatar
Alexander Larsson committed
164
}