svg text alignment
[dia.git] / app / scroll_tool.c
blob03017a5afa433107d48886586a6b80cd143d17dc
1 /* Dia -- an diagram creation/manipulation program
2 * Copyright (C) 1998 Alexander Larsson
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 #include <config.h>
20 #include "scroll_tool.h"
21 #include "handle_ops.h"
22 #include "object_ops.h"
23 #include "connectionpoint_ops.h"
24 #include "message.h"
25 #include "cursor.h"
27 static void scroll_button_press(ScrollTool *tool, GdkEventButton *event,
28 DDisplay *ddisp);
29 static void scroll_button_release(ScrollTool *tool, GdkEventButton *event,
30 DDisplay *ddisp);
31 static void scroll_motion(ScrollTool *tool, GdkEventMotion *event,
32 DDisplay *ddisp);
33 static void scroll_double_click(ScrollTool *tool, GdkEventButton *event,
34 DDisplay *ddisp);
36 Tool *
37 create_scroll_tool(void)
39 ScrollTool *tool;
41 tool = g_new(ScrollTool, 1);
42 tool->tool.type = SCROLL_TOOL;
43 tool->tool.button_press_func = (ButtonPressFunc) &scroll_button_press;
44 tool->tool.button_release_func = (ButtonReleaseFunc) &scroll_button_release;
45 tool->tool.motion_func = (MotionFunc) &scroll_motion;
46 tool->tool.double_click_func = (DoubleClickFunc) &scroll_double_click;
48 tool->scrolling = FALSE;
49 tool->use_hand = TRUE;
51 ddisplay_set_all_cursor(get_cursor(CURSOR_GRAB));
53 return (Tool *)tool;
57 void
58 free_scroll_tool(Tool *tool)
60 g_free(tool);
61 ddisplay_set_all_cursor(default_cursor);
65 static void
66 scroll_double_click(ScrollTool *tool, GdkEventButton *event,
67 DDisplay *ddisp)
69 /* Do nothing */
72 static void
73 scroll_button_press(ScrollTool *tool, GdkEventButton *event,
74 DDisplay *ddisp)
76 Point clickedpoint;
78 tool->use_hand = (event->state & GDK_SHIFT_MASK) == 0;
79 if (tool->use_hand)
80 ddisplay_set_all_cursor(get_cursor(CURSOR_GRABBING));
81 else
82 ddisplay_set_all_cursor(get_cursor(CURSOR_SCROLL));
84 ddisplay_untransform_coords(ddisp,
85 (int)event->x, (int)event->y,
86 &clickedpoint.x, &clickedpoint.y);
88 tool->scrolling = TRUE;
89 tool->last_pos = clickedpoint;
95 static void
96 scroll_motion(ScrollTool *tool, GdkEventMotion *event,
97 DDisplay *ddisp)
99 Point to;
100 Point delta;
102 /* set the cursor appropriately, and change use_hand if needed */
103 if (!tool->scrolling) {
104 /* try to minimise the number of cursor type changes */
105 if ((event->state & GDK_SHIFT_MASK) == 0) {
106 if (!tool->use_hand) {
107 tool->use_hand = TRUE;
108 ddisplay_set_all_cursor(get_cursor(CURSOR_GRAB));
110 } else
111 if (tool->use_hand) {
112 tool->use_hand = FALSE;
113 ddisplay_set_all_cursor(get_cursor(CURSOR_SCROLL));
115 return;
118 ddisplay_untransform_coords(ddisp, event->x, event->y, &to.x, &to.y);
120 if (tool->use_hand) {
121 delta = tool->last_pos;
122 point_sub(&delta, &to);
124 tool->last_pos = to;
125 point_add(&tool->last_pos, &delta);
127 /* we use this so you can scroll past the edge of the image */
128 point_add(&delta, &ddisp->origo);
129 ddisplay_set_origo(ddisp, delta.x, delta.y);
130 ddisplay_update_scrollbars(ddisp);
131 ddisplay_add_update_all(ddisp);
132 } else {
133 delta = to;
134 point_sub(&delta, &tool->last_pos);
135 point_scale(&delta, 0.5);
137 ddisplay_scroll(ddisp, &delta);
138 tool->last_pos = to;
140 ddisplay_flush(ddisp);
144 static void
145 scroll_button_release(ScrollTool *tool, GdkEventButton *event,
146 DDisplay *ddisp)
148 tool->use_hand = (event->state & GDK_SHIFT_MASK) == 0;
149 if (tool->use_hand) {
150 ddisplay_set_all_cursor(get_cursor(CURSOR_GRAB));
151 } else
152 ddisplay_set_all_cursor(get_cursor(CURSOR_SCROLL));
154 tool->scrolling = FALSE;