Added
Link Here
|
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2007 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
6 |
* http://www.eclipse.org/legal/epl-v10.html |
7 |
* |
8 |
* Contributors: |
9 |
* Eugene Kuleshov - initial API and implementation (bug 155323) |
10 |
*******************************************************************************/ |
11 |
|
12 |
package org.eclipse.ui.internal.imageviewer; |
13 |
|
14 |
import org.eclipse.swt.SWT; |
15 |
import org.eclipse.swt.events.ControlAdapter; |
16 |
import org.eclipse.swt.events.ControlEvent; |
17 |
import org.eclipse.swt.events.MouseAdapter; |
18 |
import org.eclipse.swt.events.MouseEvent; |
19 |
import org.eclipse.swt.events.MouseMoveListener; |
20 |
import org.eclipse.swt.events.PaintEvent; |
21 |
import org.eclipse.swt.events.PaintListener; |
22 |
import org.eclipse.swt.events.SelectionAdapter; |
23 |
import org.eclipse.swt.events.SelectionEvent; |
24 |
import org.eclipse.swt.graphics.Color; |
25 |
import org.eclipse.swt.graphics.Cursor; |
26 |
import org.eclipse.swt.graphics.GC; |
27 |
import org.eclipse.swt.graphics.Image; |
28 |
import org.eclipse.swt.graphics.ImageData; |
29 |
import org.eclipse.swt.graphics.PaletteData; |
30 |
import org.eclipse.swt.widgets.Canvas; |
31 |
import org.eclipse.swt.widgets.Composite; |
32 |
import org.eclipse.swt.widgets.ScrollBar; |
33 |
|
34 |
/** |
35 |
* TODO |
36 |
* |
37 |
* @since 3.3 |
38 |
*/ |
39 |
public class ImageCanvas extends Canvas { |
40 |
|
41 |
private Image image; |
42 |
|
43 |
private int clientw; |
44 |
private int clienth; |
45 |
private int imagex; |
46 |
private int imagey; |
47 |
private int imagew; |
48 |
private int imageh; |
49 |
private int scrolly; |
50 |
private int scrollx; |
51 |
private int mousex; |
52 |
private int mousey; |
53 |
|
54 |
private Color backgroundColor; |
55 |
|
56 |
private boolean listenForMouseMovement = false; |
57 |
private Cursor cursorHand; |
58 |
private Cursor cursorArrow; |
59 |
|
60 |
private double zoomScale = 1; |
61 |
|
62 |
/** |
63 |
* TODO |
64 |
* |
65 |
* @param parent |
66 |
* @param style |
67 |
*/ |
68 |
public ImageCanvas(Composite parent, int style) { |
69 |
super(parent, style | SWT.NO_BACKGROUND | SWT.V_SCROLL | SWT.H_SCROLL); |
70 |
|
71 |
backgroundColor = getDisplay().getSystemColor(SWT.COLOR_WIDGET_BACKGROUND); |
72 |
cursorHand = new Cursor(getDisplay(), SWT.CURSOR_HAND); |
73 |
cursorArrow = new Cursor(getDisplay(), SWT.CURSOR_ARROW); |
74 |
|
75 |
this.addControlListener(new ControlAdapter() { |
76 |
public void controlResized(ControlEvent e) { |
77 |
updateCanvas(); |
78 |
} |
79 |
}); |
80 |
|
81 |
this.addMouseListener(new MouseAdapter() { |
82 |
public void mouseDown(MouseEvent e) { |
83 |
listenForMouseMovement = true; |
84 |
mousex = e.x; |
85 |
mousey = e.y; |
86 |
setCursor(cursorHand); |
87 |
} |
88 |
|
89 |
public void mouseUp(MouseEvent e) { |
90 |
listenForMouseMovement = false; |
91 |
setCursor(cursorArrow); |
92 |
} |
93 |
}); |
94 |
|
95 |
this.addMouseMoveListener(new MouseMoveListener() { |
96 |
public void mouseMove(MouseEvent e) { |
97 |
if (listenForMouseMovement) { |
98 |
followMouse(e); |
99 |
} |
100 |
} |
101 |
}); |
102 |
|
103 |
this.addPaintListener(new PaintListener() { |
104 |
public void paintControl(PaintEvent event) { |
105 |
paint(event.gc); |
106 |
} |
107 |
}); |
108 |
|
109 |
getHorizontalBar().setEnabled(true); |
110 |
getHorizontalBar().addSelectionListener(new SelectionAdapter() { |
111 |
public void widgetSelected(SelectionEvent event) { |
112 |
updateHorizontalScroll((ScrollBar) event.widget); |
113 |
} |
114 |
}); |
115 |
|
116 |
getVerticalBar().setEnabled(true); |
117 |
getVerticalBar().addSelectionListener(new SelectionAdapter() { |
118 |
public void widgetSelected(SelectionEvent event) { |
119 |
updateVerticalScroll((ScrollBar) event.widget); |
120 |
} |
121 |
}); |
122 |
} |
123 |
|
124 |
/** |
125 |
* TODO |
126 |
* |
127 |
* @param image |
128 |
*/ |
129 |
public void setImage(Image image) { |
130 |
if (this.image != null) { |
131 |
image.dispose(); |
132 |
} |
133 |
|
134 |
this.image = image; |
135 |
updateCanvas(); |
136 |
} |
137 |
|
138 |
/** |
139 |
* TODO |
140 |
*/ |
141 |
public void zoomFit() { |
142 |
zoomScale = 0; |
143 |
} |
144 |
|
145 |
/** |
146 |
* TODO |
147 |
*/ |
148 |
public void zoomIn() { |
149 |
zoomScale *= 2; |
150 |
} |
151 |
|
152 |
/** |
153 |
* TODO |
154 |
*/ |
155 |
public void zoomOut() { |
156 |
zoomScale /= 2; |
157 |
} |
158 |
|
159 |
/** |
160 |
* TODO |
161 |
*/ |
162 |
public void zoomOriginal() { |
163 |
zoomScale = 1; |
164 |
} |
165 |
|
166 |
public void dispose() { |
167 |
super.dispose(); |
168 |
if (image != null && !image.isDisposed()) { |
169 |
image.dispose(); |
170 |
} |
171 |
} |
172 |
|
173 |
/** |
174 |
* TODO |
175 |
*/ |
176 |
public void rotate() { |
177 |
ImageData originalData = image.getImageData(); |
178 |
PaletteData originalPalette = originalData.palette; |
179 |
ImageData tmpData; |
180 |
PaletteData tmpPalette; |
181 |
|
182 |
if (originalPalette.isDirect) |
183 |
tmpPalette = new PaletteData(originalPalette.redMask, |
184 |
originalPalette.greenMask, originalPalette.blueMask); |
185 |
else |
186 |
tmpPalette = new PaletteData(originalPalette.getRGBs()); |
187 |
|
188 |
tmpData = new ImageData(originalData.height, originalData.width, |
189 |
originalData.depth, tmpPalette); |
190 |
|
191 |
tmpData.transparentPixel = originalData.transparentPixel; |
192 |
|
193 |
for (int i = 0; i < originalData.width; i++) { |
194 |
for (int k = 0; k < originalData.height; k++) { |
195 |
tmpData.setPixel(k, originalData.width - 1 - i, originalData.getPixel( |
196 |
i, k)); |
197 |
} |
198 |
} |
199 |
|
200 |
if (image != null) { |
201 |
image.dispose(); |
202 |
} |
203 |
image = new Image(getDisplay(), tmpData); |
204 |
updateCanvas(); |
205 |
} |
206 |
|
207 |
private void updateCanvas() { |
208 |
clientw = getClientArea().width; |
209 |
clienth = getClientArea().height; |
210 |
if (clientw < 1) { |
211 |
clientw = 1; |
212 |
} |
213 |
if (clienth < 1) { |
214 |
clienth = 1; |
215 |
} |
216 |
|
217 |
if (image == null) { |
218 |
return; |
219 |
} |
220 |
|
221 |
imageh = image.getBounds().height; |
222 |
imagew = image.getBounds().width; |
223 |
|
224 |
updateScrollVisibility(); |
225 |
getVerticalBar().setSelection(0); |
226 |
getHorizontalBar().setSelection(0); |
227 |
imagex = clientw / 2 - imagew / 2; |
228 |
imagey = clienth / 2 - imageh / 2; |
229 |
|
230 |
if (imagex < 0) { |
231 |
imagex = 0; |
232 |
} |
233 |
if (imagey < 0) { |
234 |
imagey = 0; |
235 |
} |
236 |
|
237 |
scrollx = getHorizontalBar().getSelection(); |
238 |
scrolly = getVerticalBar().getSelection(); |
239 |
|
240 |
ScrollBar vertical = getVerticalBar(); |
241 |
vertical.setMaximum(imageh); |
242 |
vertical.setThumb(Math.min(clienth, imageh)); |
243 |
vertical.setIncrement(40); |
244 |
vertical.setPageIncrement(clienth); |
245 |
|
246 |
ScrollBar horizontal = getHorizontalBar(); |
247 |
horizontal.setMaximum(imagew); |
248 |
horizontal.setThumb(Math.min(clientw, imagew)); |
249 |
horizontal.setIncrement(40); |
250 |
horizontal.setPageIncrement(clientw); |
251 |
|
252 |
redraw(); |
253 |
} |
254 |
|
255 |
private void followMouse(MouseEvent e) { |
256 |
if (clientw < imagew) { |
257 |
int mouseDiffX = (mousex - e.x); |
258 |
mousex = e.x; |
259 |
imagex -= mouseDiffX; |
260 |
getHorizontalBar().setSelection( |
261 |
getHorizontalBar().getSelection() + mouseDiffX); |
262 |
int minx = clientw - imagew; |
263 |
int maxx = 0; |
264 |
if (imagex < minx) { |
265 |
imagex = minx; |
266 |
} |
267 |
if (imagex > maxx) { |
268 |
imagex = maxx; |
269 |
} |
270 |
scrollx = getHorizontalBar().getSelection(); |
271 |
} |
272 |
if (clienth < imageh) { |
273 |
int mouseDiffY = (mousey - e.y); |
274 |
mousey = e.y; |
275 |
imagey -= mouseDiffY; |
276 |
getVerticalBar().setSelection( |
277 |
getVerticalBar().getSelection() + mouseDiffY); |
278 |
int miny = clienth - imageh; |
279 |
int maxy = 0; |
280 |
if (imagey < miny) { |
281 |
imagey = miny; |
282 |
} |
283 |
if (imagey > maxy) { |
284 |
imagey = maxy; |
285 |
} |
286 |
|
287 |
scrolly = getVerticalBar().getSelection(); |
288 |
} |
289 |
|
290 |
redraw(); |
291 |
} |
292 |
|
293 |
void paint(GC gc) { |
294 |
Image backImage = new Image(getDisplay(), clientw, clienth); |
295 |
|
296 |
GC backGC = new GC(backImage); |
297 |
backGC.setBackground(backgroundColor); |
298 |
backGC.setClipping(getClientArea()); |
299 |
backGC.fillRectangle(getClientArea()); |
300 |
|
301 |
if (image != null) { |
302 |
backGC.drawImage(image, imagex, imagey); |
303 |
} |
304 |
|
305 |
gc.drawImage(backImage, 0, 0); |
306 |
backGC.dispose(); |
307 |
backImage.dispose(); |
308 |
} |
309 |
|
310 |
private void updateScrollVisibility() { |
311 |
getHorizontalBar().setVisible(clientw < imagew); |
312 |
getVerticalBar().setVisible(clienth < imageh); |
313 |
} |
314 |
|
315 |
private void updateVerticalScroll(ScrollBar bar) { |
316 |
imagey -= bar.getSelection() - scrolly; |
317 |
scrolly = bar.getSelection(); |
318 |
redraw(); |
319 |
} |
320 |
|
321 |
private void updateHorizontalScroll(ScrollBar bar) { |
322 |
imagex -= bar.getSelection() - scrollx; |
323 |
scrollx = bar.getSelection(); |
324 |
redraw(); |
325 |
} |
326 |
|
327 |
} |