Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 15affc0b706e66e14e9e9ddf017209f402640f82 (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
/*******************************************************************************
 * Copyright (c) 2000, 2017 IBM Corporation and others.
 *
 * 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.compare.internal;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.ScrollBar;

/**
 * A <code>Canvas</code> showing a single centered SWT <code>Image</code>.
 * If the <code>Image</code> is larger than the <code>Canvas</code>,
 * <code>Scrollbars</code> will appear.
 */
class ImageCanvas extends Canvas {

	private Image fImage;

	/*
	 * Create a new ImageCanvas with the given SWT stylebits.
	 * (SWT.H_SCROLL and SWT.V_SCROLL are automtically added).
	 */
	public ImageCanvas(Composite parent, int style) {
		super(parent, style | SWT.H_SCROLL | SWT.V_SCROLL);

		ScrollBar sb= getHorizontalBar();
		sb.setIncrement(20);
		sb.addListener(SWT.Selection, e -> repaint());

		sb= getVerticalBar();
		sb.setIncrement(20);
		sb.addListener(SWT.Selection, e -> repaint());

		addListener(SWT.Resize, e -> updateScrollbars());

		addListener(SWT.Paint, event -> paint(event.gc));
	}

	/*
	 * Set the SWT Image to use as the ImageCanvas contents.
	 */
	public void setImage(Image img) {
		fImage= img;

		if (!isDisposed()) {
			getHorizontalBar().setSelection(0);
			getVerticalBar().setSelection(0);
			updateScrollbars();
			getParent().layout();
			redraw();
		}
	}

	public void repaint() {
		if (!isDisposed()) {
			GC gc= new GC(this);
			paint(gc);
			gc.dispose();
		}
	}

	void paint(GC gc) {
		if (fImage != null) {
			Rectangle bounds= fImage.getBounds();
			Rectangle clientArea= getClientArea();

			int x;
			if (bounds.width < clientArea.width)
				x= (clientArea.width - bounds.width) / 2;
			else
				x= -getHorizontalBar().getSelection();

			int y;
			if (bounds.height < clientArea.height)
				y= (clientArea.height - bounds.height) / 2;
			else
				y= -getVerticalBar().getSelection();

			gc.drawImage(fImage, x, y);
		}
	}

	/**
	 * @private
	 */
	void updateScrollbars() {
		Rectangle bounds= fImage != null ? fImage.getBounds() : new Rectangle(0, 0, 0, 0);
		Point size= getSize();
		Rectangle clientArea= getClientArea();

		ScrollBar horizontal= getHorizontalBar();
		if (bounds.width <= clientArea.width) {
			horizontal.setVisible(false);
			horizontal.setSelection(0);
		} else {
			horizontal.setPageIncrement(clientArea.width - horizontal.getIncrement());
			int max= bounds.width + (size.x - clientArea.width);
			horizontal.setMaximum(max);
			horizontal.setThumb(size.x > max ? max : size.x);
			horizontal.setVisible(true);
		}

		ScrollBar vertical= getVerticalBar();
		if (bounds.height <= clientArea.height) {
			vertical.setVisible(false);
			vertical.setSelection(0);
		} else {
			vertical.setPageIncrement(clientArea.height - vertical.getIncrement());
			int max= bounds.height + (size.y - clientArea.height);
			vertical.setMaximum(max);
			vertical.setThumb(size.y > max ? max : size.y);
			vertical.setVisible(true);
		}
	}

}

Back to the top