Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a501f9205b561a2f65c1e1e8e9f7b5f9f89507ae (plain) (blame)
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
/**
 * Copyright (c) 2018 Laurent CARON.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 * Laurent CARON (laurent.caron at gmail dot com) - initial API and implementation (bug 542777)
 */
package org.eclipse.swt.custom;

import org.eclipse.swt.*;
import org.eclipse.swt.graphics.*;
import org.eclipse.swt.widgets.*;

/**
 * This class add the following behaviour to <code>StyledText</code> widgets:<br/>
 * When the user clicks on the wheel, a circle with arrows appears. When the user moves the mouse,
 * the StyledText content is scrolled (on the right or on the left for horizontal movements, up or down for vertical movements).<br/>
 *
 * @since 3.110
 */
class MouseNavigator {
	private final StyledText parent;
	boolean navigationActivated = false;
	private GC gc;
	private static final int CIRCLE_RADIUS = 15;
	private static final int CENTRAL_POINT_RADIUS = 2;
	private Point originalMouseLocation;
	private final Listener mouseDownListener, mouseUpListener, paintListener, mouseMoveListener, focusOutListener;
	private boolean hasHBar, hasVBar;

	MouseNavigator(final StyledText styledText) {
		if (styledText == null) {
			SWT.error(SWT.ERROR_NULL_ARGUMENT);
		}
		if (styledText.isDisposed()) {
			SWT.error(SWT.ERROR_WIDGET_DISPOSED);
		}
		parent = styledText;

		mouseDownListener = (event) -> {
			onMouseDown(event);
		};
		parent.addListener(SWT.MouseDown, mouseDownListener);

		mouseUpListener = (event) -> {
			onMouseUp(event);
		};
		parent.addListener(SWT.MouseUp, mouseUpListener);

		paintListener = (event) -> {
			onPaint(event);
		};
		parent.addListener(SWT.Paint, paintListener);

		mouseMoveListener = (event) -> {
			onMouseMove(event);
		};
		parent.addListener(SWT.MouseMove, mouseMoveListener);

		focusOutListener = (event) -> {
			onFocusOut(event);
		};
		parent.addListener(SWT.FocusOut, focusOutListener);
	}

	private void onMouseDown(Event e) {
		if ((e.button != 2) || navigationActivated) {
			return;
		}

		if (!parent.isVisible() || !parent.getEnabled()) {
			return;
		}

		// Widget has no bar or bars are not enabled
		initBarState();

		if (!hasHBar && !hasVBar) {
			return;
		}

		navigationActivated = true;
		parent.setCursor(parent.getDisplay().getSystemCursor(SWT.CURSOR_ARROW));
		originalMouseLocation = getMouseLocation();
		parent.redraw();
	}

	private void initBarState() {
		hasHBar = computeHasHorizontalBar();
		hasVBar = computeHasVerticalBar();
	}

	private boolean computeHasHorizontalBar() {
		final ScrollBar horizontalBar = parent.getHorizontalBar();
		final boolean hasHorizontalBar = horizontalBar != null && horizontalBar.isVisible();
		final boolean exceedHorizontalSpace = parent.computeSize(SWT.DEFAULT, SWT.DEFAULT).x > parent.getSize().x;
		return hasHorizontalBar && exceedHorizontalSpace;
	}

	private boolean computeHasVerticalBar() {
		final ScrollBar verticalBar = parent.getVerticalBar();
		final boolean hasVerticalBar = verticalBar != null && verticalBar.isEnabled();
		final boolean exceedVerticalSpace = parent.computeSize(SWT.DEFAULT, SWT.DEFAULT).y > parent.getSize().y;
		return hasVerticalBar && exceedVerticalSpace;
	}

	private void onMouseUp(Event e) {
		if ((computeDist() < CIRCLE_RADIUS) && (computeDist() >= 0)) {
			return;
		}
		deactivate();
	}

	public int computeDist() {
		if (originalMouseLocation == null) {
			return -1;
		}
		final Point mouseLocation = getMouseLocation();
		final int deltaX = originalMouseLocation.x - mouseLocation.x;
		final int deltaY = originalMouseLocation.y - mouseLocation.y;
		final int dist = (int) Math.sqrt(deltaX * deltaX + deltaY * deltaY);
		return dist;
	}

	private void deactivate() {
		parent.setCursor(parent.getDisplay().getSystemCursor(SWT.CURSOR_ARROW));
		navigationActivated = false;
		originalMouseLocation = null;
		parent.redraw();
	}

	private void onFocusOut(Event e) {
		deactivate();
	}

	private void onMouseMove(Event e) {
		if (!navigationActivated) {
			return;
		}

		final Point mouseLocation = getMouseLocation();
		final int deltaX = originalMouseLocation.x - mouseLocation.x;
		final int deltaY = originalMouseLocation.y - mouseLocation.y;
		final int dist = (int) Math.sqrt(deltaX * deltaX + deltaY * deltaY);
		if (dist < CIRCLE_RADIUS) {
			return;
		}

		parent.setRedraw(false);
		if (hasHBar) {
			final ScrollBar bar = parent.getHorizontalBar();
			bar.setSelection((int) (bar.getSelection() - deltaX * .1));
			fireSelectionEvent(e, bar);
		}

		if (hasVBar) {
			final ScrollBar bar = parent.getVerticalBar();
			bar.setSelection((int) (bar.getSelection() - deltaY * .1));
			fireSelectionEvent(e, bar);
		}
		parent.setRedraw(true);
		parent.redraw();
	}

	private void fireSelectionEvent(final Event e, final ScrollBar bar) {
		final Event event = new Event();
		event.widget = bar;
		event.display = parent.getDisplay();
		event.type = SWT.Selection;
		event.time = e.time;

		for (final Listener selectionListener : bar.getListeners(SWT.Selection)) {
			selectionListener.handleEvent(event);
		}
	}

	private Point getMouseLocation() {
		final Point cursorLocation = Display.getCurrent().getCursorLocation();
		final Point relativeCursorLocation = parent.toControl(cursorLocation);
		return relativeCursorLocation;
	}

	private void onPaint(final Event e) {
		if (!navigationActivated) {
			return;
		}

		final Rectangle rect = parent.getClientArea();
		if (rect.width == 0 || rect.height == 0) {
			return;
		}
		gc = e.gc;
		gc.setAntialias(SWT.ON);
		gc.setAdvanced(true);

		final Color oldForegroundColor = gc.getForeground();
		final Color oldBackgroundColor = gc.getBackground();
		gc.setBackground(parent.getForeground());

		drawCircle();
		drawCentralPoint();

		drawArrows();

		gc.setForeground(oldForegroundColor);
		gc.setBackground(oldBackgroundColor);
	}

	private void drawCircle() {
		gc.setBackground(parent.getBackground());
		gc.setForeground(parent.getForeground());
		gc.setAlpha(200);
		gc.fillOval(originalMouseLocation.x - CIRCLE_RADIUS, originalMouseLocation.y - CIRCLE_RADIUS, CIRCLE_RADIUS * 2, CIRCLE_RADIUS * 2);
		gc.setBackground(parent.getForeground());
		gc.setAlpha(255);
		gc.drawOval(originalMouseLocation.x - CIRCLE_RADIUS, originalMouseLocation.y - CIRCLE_RADIUS, CIRCLE_RADIUS * 2, CIRCLE_RADIUS * 2);
	}

	private void drawCentralPoint() {
		gc.fillOval(originalMouseLocation.x - CENTRAL_POINT_RADIUS, originalMouseLocation.y - CENTRAL_POINT_RADIUS, CENTRAL_POINT_RADIUS * 2, CENTRAL_POINT_RADIUS * 2);
	}

	private void drawArrows() {
		gc.setLineWidth(2);
		if (hasHBar) {
			drawHorizontalArrows();
		}
		if (hasVBar) {
			drawVerticalArrows();
		}
	}

	private void drawHorizontalArrows() {
		final int[] points = new int[6];
		// Left
		points[0] = originalMouseLocation.x - 6;
		points[1] = originalMouseLocation.y + 3;
		points[2] = originalMouseLocation.x - 9;
		points[3] = originalMouseLocation.y;
		points[4] = originalMouseLocation.x - 6;
		points[5] = originalMouseLocation.y - 3;
		gc.drawPolyline(points);

		// Right
		points[0] = originalMouseLocation.x + 7;
		points[1] = originalMouseLocation.y + 3;
		points[2] = originalMouseLocation.x + 10;
		points[3] = originalMouseLocation.y;
		points[4] = originalMouseLocation.x + 7;
		points[5] = originalMouseLocation.y - 3;
		gc.drawPolyline(points);
	}

	private void drawVerticalArrows() {
		final int[] points = new int[6];
		// Upper
		points[0] = originalMouseLocation.x - 3;
		points[1] = originalMouseLocation.y - 6;
		points[2] = originalMouseLocation.x;
		points[3] = originalMouseLocation.y - 10;
		points[4] = originalMouseLocation.x + 3;
		points[5] = originalMouseLocation.y - 6;
		gc.drawPolyline(points);

		// Lower
		points[0] = originalMouseLocation.x - 3;
		points[1] = originalMouseLocation.y + 7;
		points[2] = originalMouseLocation.x;
		points[3] = originalMouseLocation.y + 11;
		points[4] = originalMouseLocation.x + 3;
		points[5] = originalMouseLocation.y + 7;
		gc.drawPolyline(points);

	}

	void dispose() {
		if (parent.isDisposed()) {
			return;
		}
		parent.removeListener(SWT.MouseDown, mouseDownListener);
		parent.removeListener(SWT.MouseUp, mouseUpListener);
		parent.removeListener(SWT.Paint, paintListener);
		parent.removeListener(SWT.MouseMove, mouseMoveListener);
		parent.removeListener(SWT.MouseExit, focusOutListener);
	}
}

Back to the top