comparison mupdf-source/thirdparty/tesseract/java/com/google/scrollview/events/SVEventHandler.java @ 2:b50eed0cc0ef upstream

ADD: MuPDF v1.26.7: the MuPDF source as downloaded by a default build of PyMuPDF 1.26.4. The directory name has changed: no version number in the expanded directory now.
author Franz Glasner <fzglas.hg@dom66.de>
date Mon, 15 Sep 2025 11:43:07 +0200
parents
children
comparison
equal deleted inserted replaced
1:1d09e1dec1d9 2:b50eed0cc0ef
1 // Copyright 2007 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); You may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by
6 // applicable law or agreed to in writing, software distributed under the
7 // License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
8 // OF ANY KIND, either express or implied. See the License for the specific
9 // language governing permissions and limitations under the License.
10
11 package com.google.scrollview.events;
12
13 import com.google.scrollview.ScrollView;
14 import com.google.scrollview.events.SVEvent;
15 import com.google.scrollview.events.SVEventType;
16 import com.google.scrollview.ui.SVWindow;
17
18 import org.piccolo2d.PCamera;
19 import org.piccolo2d.PNode;
20 import org.piccolo2d.event.PBasicInputEventHandler;
21 import org.piccolo2d.event.PInputEvent;
22 import org.piccolo2d.nodes.PPath;
23
24 import java.awt.Color;
25 import java.awt.event.ActionEvent;
26 import java.awt.event.ActionListener;
27 import java.awt.event.KeyEvent;
28 import java.awt.event.KeyListener;
29 import java.awt.event.WindowEvent;
30 import java.awt.event.WindowListener;
31 import java.awt.Window;
32
33 import javax.swing.Timer;
34
35 /**
36 * The ScrollViewEventHandler takes care of any events which might happen on the
37 * canvas and converts them to an according SVEvent, which is (using the
38 * processEvent method) then added to a message queue. All events from the
39 * message queue get sent gradually.
40 *
41 * @author wanke@google.com
42 */
43 public class SVEventHandler extends PBasicInputEventHandler implements
44 ActionListener, KeyListener, WindowListener {
45
46 /** Necessary to wait for a defined period of time (for SVET_HOVER). */
47 public Timer timer;
48
49 /** The window which the event corresponds to. */
50 private SVWindow svWindow;
51
52 /** These are used to determine a selection size (for SVET_SELECTION). */
53 private int lastX = 0;
54 private int lastY = 0;
55
56 /**
57 * These are used in case we want to transmit our position, but do not get it
58 * because it was no MouseEvent, in particular SVET_HOVER and SVET_INPUT.
59 */
60 private int lastXMove = 0;
61 private int lastYMove = 0;
62
63 /** For Drawing a rubber-band rectangle for selection. */
64 private int startX = 0;
65 private int startY = 0;
66 private float rubberBandTransparency = 0.5f;
67 private PNode selection = null;
68
69 /** The string entered since the last enter. Since the client
70 * end eats all newlines, we can't use the newline
71 * character, so use ! for now, as it cannot be entered
72 * directly anyway and therefore can never show up for real. */
73 private String keyStr = "!";
74
75 /** Setup the timer. */
76 public SVEventHandler(SVWindow wdw) {
77 timer = new Timer(1000, this);
78 svWindow = wdw;
79 }
80
81 /**
82 * Store the newest x,y values, add the message to the queue and restart the
83 * timer.
84 */
85 private void processEvent(SVEvent e) {
86 lastXMove = e.x;
87 lastYMove = e.y;
88 ScrollView.addMessage(e);
89 timer.restart();
90 }
91
92 /** Show the associated popup menu at (x,y) (relative position of the window). */
93 private void showPopup(PInputEvent e) {
94 double x = e.getCanvasPosition().getX();
95 double y = e.getCanvasPosition().getY();
96
97 if (svWindow.svPuMenu != null) {
98 svWindow.svPuMenu.show(svWindow, (int) x, (int) y);
99 }
100 }
101
102
103 /** The mouse is clicked - create an SVET_CLICK event. */
104 @Override
105 public void mouseClicked(PInputEvent e) {
106 if (e.isPopupTrigger()) {
107 showPopup(e);
108 } else {
109 processEvent(new SVEvent(SVEventType.SVET_CLICK, svWindow, (int) e
110 .getPosition().getX(), (int) e.getPosition().getY(), 0, 0, null));
111 }
112 }
113
114 /**
115 * The mouse key is pressed (and keeps getting pressed).
116 * Depending on the OS, show a popup menu (if the button pressed is associated
117 * with popup menus, like the RMB under windows&linux) or otherwise save the
118 * position (in case it is a selection).
119 */
120 @Override
121 public void mousePressed(PInputEvent e) {
122 if (e.isPopupTrigger()) {
123 showPopup(e);
124 } else {
125 lastX = (int) e.getPosition().getX();
126 lastY = (int) e.getPosition().getY();
127 timer.restart();
128 }
129 }
130
131 /** The mouse is getting dragged - create an SVET_MOUSE event. */
132 @Override
133 public void mouseDragged(PInputEvent e) {
134 processEvent(new SVEvent(SVEventType.SVET_MOUSE, svWindow, (int) e
135 .getPosition().getX(), (int) e.getPosition().getY(), (int) e
136 .getPosition().getX()
137 - lastX, (int) e.getPosition().getY() - lastY, null));
138
139 // Paint a selection rectangle.
140 if (selection == null) {
141 startX = (int) e.getPosition().getX();
142 startY = (int) e.getPosition().getY();
143 selection = PPath.createRectangle(startX, startY, 1, 1);
144 selection.setTransparency(rubberBandTransparency);
145 svWindow.canvas.getLayer().addChild(selection);
146 } else {
147 int right = Math.max(startX, (int) e.getPosition().getX());
148 int left = Math.min(startX, (int) e.getPosition().getX());
149 int bottom = Math.max(startY, (int) e.getPosition().getY());
150 int top = Math.min(startY, (int) e.getPosition().getY());
151 svWindow.canvas.getLayer().removeChild(selection);
152 selection = PPath.createRectangle(left, top, right - left, bottom - top);
153 selection.setPaint(Color.YELLOW);
154 selection.setTransparency(rubberBandTransparency);
155 svWindow.canvas.getLayer().addChild(selection);
156 }
157 }
158
159 /**
160 * The mouse was released.
161 * Depending on the OS, show a popup menu (if the button pressed is associated
162 * with popup menus, like the RMB under windows&linux) or otherwise create an
163 * SVET_SELECTION event.
164 */
165 @Override
166 public void mouseReleased(PInputEvent e) {
167 if (e.isPopupTrigger()) {
168 showPopup(e);
169 } else {
170 processEvent(new SVEvent(SVEventType.SVET_SELECTION, svWindow, (int) e
171 .getPosition().getX(), (int) e.getPosition().getY(), (int) e
172 .getPosition().getX()
173 - lastX, (int) e.getPosition().getY() - lastY, null));
174 }
175 if (selection != null) {
176 svWindow.canvas.getLayer().removeChild(selection);
177 selection = null;
178 }
179 }
180
181 /**
182 * The mouse wheel is used to zoom in and out of the viewport and center on
183 * the (x,y) position the mouse is currently on.
184 */
185 @Override
186 public void mouseWheelRotated(PInputEvent e) {
187 PCamera lc = svWindow.canvas.getCamera();
188 double sf = SVWindow.SCALING_FACTOR;
189
190 if (e.getWheelRotation() < 0) {
191 sf = 1 / sf;
192 }
193 lc.scaleViewAboutPoint(lc.getScale() / sf, e.getPosition().getX(), e
194 .getPosition().getY());
195 }
196
197 /**
198 * The mouse was moved - create an SVET_MOTION event. NOTE: This obviously
199 * creates a lot of traffic and, depending on the type of application, could
200 * quite possibly be disabled.
201 */
202 @Override
203 public void mouseMoved(PInputEvent e) {
204 processEvent(new SVEvent(SVEventType.SVET_MOTION, svWindow, (int) e
205 .getPosition().getX(), (int) e.getPosition().getY(), 0, 0, null));
206 }
207
208 /**
209 * The mouse entered the window.
210 * Start the timer, which will then emit SVET_HOVER events every X ms. */
211 @Override
212 public void mouseEntered(PInputEvent e) {
213 timer.restart();
214 }
215
216 /**
217 * The mouse exited the window
218 * Stop the timer, so no more SVET_HOVER events will emit. */
219 @Override
220 public void mouseExited(PInputEvent e) {
221 timer.stop();
222 }
223
224 /**
225 * The only associated object with this is the timer, so we use it to send a
226 * SVET_HOVER event.
227 */
228 public void actionPerformed(ActionEvent e) {
229 processEvent(new SVEvent(SVEventType.SVET_HOVER, svWindow, lastXMove,
230 lastYMove, 0, 0, null));
231 }
232
233 /**
234 * A key was pressed - create an SVET_INPUT event.
235 *
236 * NOTE: Might be useful to specify hotkeys.
237 *
238 * Implementation note: The keyListener provided by Piccolo seems to be
239 * broken, so we use the AWT listener directly.
240 * There are never any keyTyped events received either so we are
241 * stuck with physical keys, which is very ugly.
242 */
243 public void keyPressed(KeyEvent e) {
244 char keyCh = e.getKeyChar();
245 if (keyCh == '\r' || keyCh == '\n' || keyCh == '\0' || keyCh == '?') {
246 processEvent(new SVEvent(SVEventType.SVET_INPUT, svWindow, lastXMove,
247 lastYMove, 0, 0, keyStr));
248 // Send newline characters as '!' as '!' can never be a keypressed
249 // and the client eats all newline characters.
250 keyStr = "!";
251 } else {
252 processEvent(new SVEvent(SVEventType.SVET_INPUT, svWindow, lastXMove,
253 lastYMove, 0, 0, String.valueOf(keyCh)));
254 keyStr += keyCh;
255 }
256 }
257
258 /**
259 * A window is closed (by the 'x') - create an SVET_DESTROY event. If it was
260 * the last open Window, also send an SVET_EXIT event (but do not exit unless
261 * the client says so).
262 */
263 public void windowClosing(WindowEvent e) {
264 processEvent(new SVEvent(SVEventType.SVET_DESTROY, svWindow, lastXMove,
265 lastYMove, 0, 0, null));
266 Window w = e.getWindow();
267 if (w != null) {
268 w.dispose();
269 }
270 SVWindow.nrWindows--;
271 if (SVWindow.nrWindows == 0) {
272 processEvent(new SVEvent(SVEventType.SVET_EXIT, svWindow, lastXMove,
273 lastYMove, 0, 0, null));
274 }
275 }
276
277 /** These are all events we do not care about and throw away. */
278 public void keyReleased(KeyEvent e) {
279 }
280
281 public void keyTyped(KeyEvent e) {
282 }
283
284 public void windowActivated(WindowEvent e) {
285 }
286
287 public void windowClosed(WindowEvent e) {
288 }
289
290 public void windowDeactivated(WindowEvent e) {
291 }
292
293 public void windowDeiconified(WindowEvent e) {
294 }
295
296 public void windowIconified(WindowEvent e) {
297 }
298
299 public void windowOpened(WindowEvent e) {
300 }
301 }