Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 160572 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/mylyn/internal/tasks/ui/wizards/ScreenShotAttachmentPage.java (-51 / +254 lines)
Lines 12-29 Link Here
12
import org.eclipse.jface.wizard.IWizardPage;
12
import org.eclipse.jface.wizard.IWizardPage;
13
import org.eclipse.jface.wizard.WizardPage;
13
import org.eclipse.jface.wizard.WizardPage;
14
import org.eclipse.mylyn.internal.tasks.core.LocalAttachment;
14
import org.eclipse.mylyn.internal.tasks.core.LocalAttachment;
15
import org.eclipse.mylyn.tasks.ui.TasksUiPlugin;
15
import org.eclipse.swt.SWT;
16
import org.eclipse.swt.SWT;
16
import org.eclipse.swt.custom.ScrolledComposite;
17
import org.eclipse.swt.custom.ScrolledComposite;
18
import org.eclipse.swt.events.ControlAdapter;
19
import org.eclipse.swt.events.ControlEvent;
20
import org.eclipse.swt.events.MouseEvent;
21
import org.eclipse.swt.events.MouseListener;
22
import org.eclipse.swt.events.MouseMoveListener;
17
import org.eclipse.swt.events.PaintEvent;
23
import org.eclipse.swt.events.PaintEvent;
18
import org.eclipse.swt.events.PaintListener;
24
import org.eclipse.swt.events.PaintListener;
19
import org.eclipse.swt.events.SelectionEvent;
25
import org.eclipse.swt.events.SelectionEvent;
20
import org.eclipse.swt.events.SelectionListener;
26
import org.eclipse.swt.events.SelectionListener;
27
import org.eclipse.swt.graphics.Cursor;
21
import org.eclipse.swt.graphics.GC;
28
import org.eclipse.swt.graphics.GC;
22
import org.eclipse.swt.graphics.Image;
29
import org.eclipse.swt.graphics.Image;
23
import org.eclipse.swt.graphics.Point;
30
import org.eclipse.swt.graphics.Point;
24
import org.eclipse.swt.graphics.Rectangle;
31
import org.eclipse.swt.graphics.Rectangle;
25
import org.eclipse.swt.layout.FillLayout;
26
import org.eclipse.swt.layout.GridData;
27
import org.eclipse.swt.layout.GridLayout;
32
import org.eclipse.swt.layout.GridLayout;
28
import org.eclipse.swt.widgets.Button;
33
import org.eclipse.swt.widgets.Button;
29
import org.eclipse.swt.widgets.Canvas;
34
import org.eclipse.swt.widgets.Canvas;
Lines 44-55 Link Here
44
49
45
	private Button makeShotButton;
50
	private Button makeShotButton;
46
51
47
	private Button showShotButton;
52
	private Button cropShotButton;
53
54
	private Button fitShotButton;
48
55
49
	private Image screenshotImage;
56
	private Image screenshotImage;
50
57
51
	private Canvas canvas;
58
	private Canvas canvas;
52
59
60
	private ScrolledComposite scrolledComposite;
61
62
	private Rectangle cropRegionBounds;
63
64
	private boolean isCropFit;
65
53
	protected ScreenShotAttachmentPage(LocalAttachment attachment) {
66
	protected ScreenShotAttachmentPage(LocalAttachment attachment) {
54
		super("ScreenShotAttachment");
67
		super("ScreenShotAttachment");
55
		setTitle("Create a screenshot");
68
		setTitle("Create a screenshot");
Lines 58-121 Link Here
58
	}
71
	}
59
72
60
	public void createControl(Composite parent) {
73
	public void createControl(Composite parent) {
74
61
		Composite composite = new Composite(parent, SWT.NONE);
75
		Composite composite = new Composite(parent, SWT.NONE);
62
		GridLayout gridLayout = new GridLayout();
76
		composite.setLayout(new GridLayout());
63
		gridLayout.numColumns = 2;
64
		composite.setLayout(gridLayout);
65
		setControl(composite);
77
		setControl(composite);
66
78
67
		composite.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
79
		Composite buttonsComposite = new Composite(composite, SWT.NONE);
68
		composite.setLayout(new GridLayout(3, false));
80
		buttonsComposite.setLayout(new GridLayout(4, false));
69
81
		makeShotButton = new Button(buttonsComposite, SWT.PUSH);
70
		makeShotButton = new Button(composite, SWT.PUSH);
82
		makeShotButton.setText("Capture full screen");
71
		makeShotButton.setText("Take a screenshot");
83
		makeShotButton.setImage(TasksUiPlugin.imageDescriptorFromPlugin(TasksUiPlugin.ID_PLUGIN,
84
				"icons/etool16/scr-fullscreen.gif").createImage());
72
		makeShotButton.addSelectionListener(new SelectionListener() {
85
		makeShotButton.addSelectionListener(new SelectionListener() {
73
86
74
			public void widgetSelected(SelectionEvent e) {
87
			public void widgetSelected(SelectionEvent e) {
75
				storeScreenshotContent();
88
				storeScreenshotContent();
76
				page.setErrorMessage(null);
89
				page.setErrorMessage(null);
77
				showShotButton.setEnabled(true);
90
				cropShotButton.setEnabled(true);
91
				fitShotButton.setEnabled(true);
78
			}
92
			}
79
93
80
			public void widgetDefaultSelected(SelectionEvent e) {
94
			public void widgetDefaultSelected(SelectionEvent e) {
95
				//ignore
81
			}
96
			}
82
97
83
		});
98
		});
84
99
85
		showShotButton = new Button(composite, SWT.PUSH);
100
		cropShotButton = new Button(buttonsComposite, SWT.TOGGLE);
86
		showShotButton.setText("Show the screenshot");
101
		cropShotButton.setText("Crop a region");
87
		showShotButton.addSelectionListener(new SelectionListener() {
102
		cropShotButton.setImage(TasksUiPlugin.imageDescriptorFromPlugin(TasksUiPlugin.ID_PLUGIN,
103
				"icons/etool16/scr-crop.gif").createImage());
104
		cropShotButton.addSelectionListener(new SelectionListener() {
88
105
89
			public void widgetSelected(SelectionEvent e) {
106
			public void widgetSelected(SelectionEvent e) {
90
				showScreenshotContent();
107
				drawCanvas();
108
				cropScreenshotContent();
91
			}
109
			}
92
110
93
			public void widgetDefaultSelected(SelectionEvent e) {
111
			public void widgetDefaultSelected(SelectionEvent e) {
112
				//ignore
94
			}
113
			}
95
114
96
		});
115
		});
116
		cropShotButton.setEnabled(false);
117
118
		fitShotButton = new Button(buttonsComposite, SWT.TOGGLE);
119
		fitShotButton.setText("Fit image");
120
		fitShotButton.setImage(TasksUiPlugin.imageDescriptorFromPlugin(TasksUiPlugin.ID_PLUGIN,
121
				"icons/etool16/scr-fit.gif").createImage());
122
		fitShotButton.addSelectionListener(new SelectionListener() {
97
123
98
		canvas = new Canvas(composite, SWT.BORDER);
124
			public void widgetSelected(SelectionEvent e) {
99
		canvas.setLayoutData(GridDataFactory.fillDefaults()
125
				drawCanvas();
126
			}
127
128
			public void widgetDefaultSelected(SelectionEvent e) {
129
				// ignore
130
			}
131
132
		});
133
		fitShotButton.setSelection(true);
134
		fitShotButton.setEnabled(false);
135
		isCropFit = true;
136
137
		scrolledComposite = new ScrolledComposite(composite, SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER);
138
		scrolledComposite.setLayoutData(GridDataFactory.fillDefaults()
100
				.align(SWT.FILL, SWT.FILL)
139
				.align(SWT.FILL, SWT.FILL)
101
				.grab(true, true)
140
				.grab(true, true)
102
				.span(2, 1)
103
				.create());
141
				.create());
104
142
143
		canvas = new Canvas(scrolledComposite, SWT.DOUBLE_BUFFERED | SWT.NONE);
144
		scrolledComposite.setContent(canvas);
105
		canvas.addPaintListener(new PaintListener() {
145
		canvas.addPaintListener(new PaintListener() {
106
			public void paintControl(PaintEvent e) {
146
			public void paintControl(PaintEvent e) {
107
				if (screenshotImage != null) {
147
				if (screenshotImage != null) {
108
					Rectangle screenBounds = screenshotImage.getBounds();
148
					Rectangle imageBounds = screenshotImage.getBounds();
109
					Rectangle canvasBounds = canvas.getBounds();
149
					Rectangle canvasBounds = canvas.getBounds();
110
					e.gc.drawImage(screenshotImage, 0, 0, screenBounds.width, screenBounds.height, 0, 0,
150
111
							canvasBounds.width, canvasBounds.height);
151
					if (fitShotButton.getSelection()) {
152
						e.gc.drawImage(screenshotImage, 0, 0, imageBounds.width, imageBounds.height, 0, 0,
153
								canvasBounds.width, canvasBounds.height);
154
					} else
155
						e.gc.drawImage(screenshotImage, 0, 0);
156
157
					if (cropRegionBounds != null && !cropShotButton.getSelection()) {
158
						drawRegion(e.gc);
159
					}
160
112
				} else {
161
				} else {
113
					page.setErrorMessage("Screenshot required");
162
					page.setErrorMessage("Screenshot required");
114
					showShotButton.setEnabled(false);
163
					cropShotButton.setEnabled(false);
164
					fitShotButton.setEnabled(false);
115
				}
165
				}
116
			}
166
			}
117
		});
167
		});
118
168
169
		scrolledComposite.addControlListener(new ControlAdapter() {
170
			@Override
171
			public void controlResized(ControlEvent e) {
172
				drawCanvas();
173
			}
174
		});
175
119
	}
176
	}
120
177
121
	@Override
178
	@Override
Lines 131-136 Link Here
131
		attachment.setContentType("image/jpeg");
188
		attachment.setContentType("image/jpeg");
132
		page.setFilePath(InputAttachmentSourcePage.SCREENSHOT_LABEL);
189
		page.setFilePath(InputAttachmentSourcePage.SCREENSHOT_LABEL);
133
		page.setContentType();
190
		page.setContentType();
191
		getCropScreenshot();
134
		return page;
192
		return page;
135
	}
193
	}
136
194
Lines 144-157 Link Here
144
		final Display display = Display.getDefault();
202
		final Display display = Display.getDefault();
145
		final Shell wizardShell = getWizard().getContainer().getShell();
203
		final Shell wizardShell = getWizard().getContainer().getShell();
146
		wizardShell.setVisible(false);
204
		wizardShell.setVisible(false);
205
		isCropFit = fitShotButton.getSelection();
147
206
148
		display.asyncExec(new Runnable() {
207
		display.asyncExec(new Runnable() {
149
			public void run() {
208
			public void run() {
150
				GC gc = new GC(display);
209
				GC gc = new GC(display);
151
				screenshotImage = new Image(display, display.getBounds());
210
				Rectangle displayBounds = display.getBounds();
211
				screenshotImage = new Image(display, displayBounds);
152
				gc.copyArea(screenshotImage, 0, 0);
212
				gc.copyArea(screenshotImage, 0, 0);
213
				gc.drawRectangle(0, 0, displayBounds.width - 1, displayBounds.height - 1);
153
				gc.dispose();
214
				gc.dispose();
154
				canvas.redraw();
215
				drawCanvas();
155
				wizardShell.setVisible(true);
216
				wizardShell.setVisible(true);
156
				if (screenshotImage != null)
217
				if (screenshotImage != null)
157
					setPageComplete(true);
218
					setPageComplete(true);
Lines 159-196 Link Here
159
		});
220
		});
160
	}
221
	}
161
222
162
	private void showScreenshotContent() {
223
	private void cropScreenshotContent() {
163
		Display display = Display.getDefault();
164
224
165
		Shell popup = new Shell(display.getActiveShell(), SWT.SHELL_TRIM);
225
		final Point downPoint = new Point(-1, -1);
166
		popup.setLayout(new FillLayout());
167
		popup.setText("Screenshot Image");
168
226
169
		Rectangle displayBounds = Display.getDefault().getBounds();
227
		final Display display = Display.getDefault();
170
		Point dialogSize = new Point(0, 0);
228
171
		dialogSize.x = displayBounds.width / 2;
229
		final Rectangle originalBounds = screenshotImage.getBounds();
172
		dialogSize.y = displayBounds.height / 2;
230
173
		Point dialoglocation = new Point(0, 0);
231
		final GC cropRegionGC = new GC(canvas);
174
		dialoglocation.x = displayBounds.x + displayBounds.width / 2 - dialogSize.x / 2;
232
175
		dialoglocation.y = displayBounds.y + displayBounds.height / 2 - dialogSize.y / 2;
233
		final MouseMoveListener mouseMoveListener = new MouseMoveListener() {
176
		popup.setSize(dialogSize);
234
			public void mouseMove(MouseEvent e) {
177
		popup.setLocation(dialoglocation);
235
				if (downPoint.x != -1 && downPoint.y != -1) {
178
236
					int width = e.x - downPoint.x;
179
		ScrolledComposite sc = new ScrolledComposite(popup, SWT.V_SCROLL | SWT.H_SCROLL);
237
					int height = e.y - downPoint.y;
180
		Canvas canvas = new Canvas(sc, SWT.NONE);
238
					canvas.redraw();
181
		sc.setContent(canvas);
239
					cropRegionGC.drawRectangle(downPoint.x, downPoint.y, width, height);
182
		canvas.setBounds(display.getBounds());
240
				}
183
		canvas.addPaintListener(new PaintListener() {
184
			public void paintControl(PaintEvent e) {
185
				if (screenshotImage != null)
186
					e.gc.drawImage(screenshotImage, 0, 0);
187
			}
241
			}
188
		});
242
		};
189
		popup.open();
243
244
		final MouseListener mouseListener = new MouseListener() {
245
			public void mouseUp(MouseEvent e) {
246
				if (downPoint.x != -1 && downPoint.y != -1) {
247
248
					int width;
249
					if (e.x > originalBounds.width) {
250
						width = originalBounds.width - downPoint.x;
251
					} else if (e.x < 0) {
252
						width = downPoint.x;
253
						downPoint.x = 0;
254
					} else {
255
						width = e.x - downPoint.x;
256
					}
257
258
					int height;
259
					if (e.y > originalBounds.height) {
260
						height = originalBounds.height - downPoint.y;
261
					} else if (e.y < 0) {
262
						height = downPoint.y;
263
						downPoint.y = 0;
264
					} else {
265
						height = e.y - downPoint.y;
266
					}
267
268
					display.getActiveShell().setCursor(new Cursor(null, SWT.CURSOR_ARROW));
269
270
					cropRegionBounds = new Rectangle(downPoint.x, downPoint.y, width, height);
271
					drawRegion(cropRegionGC);
272
273
					canvas.removeMouseMoveListener(mouseMoveListener);
274
					canvas.removeMouseListener(this);
275
					downPoint.x = -1;
276
					downPoint.y = -1;
277
278
					cropShotButton.setSelection(false);
279
				}
280
			}
281
282
			public void mouseDown(MouseEvent e) {
283
				if (downPoint.x == -1 && downPoint.y == -1) {
284
					display.getActiveShell().setCursor(new Cursor(null, SWT.CURSOR_CROSS));
285
					downPoint.x = e.x;
286
					downPoint.y = e.y;
287
					canvas.addMouseMoveListener(mouseMoveListener);
288
				}
289
			}
290
291
			public void mouseDoubleClick(MouseEvent e) {
292
				// ignore
293
			}
294
		};
295
296
		isCropFit = fitShotButton.getSelection();
297
298
		canvas.addMouseListener(mouseListener);
299
300
	}
301
302
	private void drawCanvas() {
303
		if (fitShotButton.getSelection()) {
304
			Rectangle bounds = scrolledComposite.getBounds();
305
			bounds.x = 0;
306
			bounds.y = 0;
307
			bounds.width -= 5;
308
			bounds.height -= 5;
309
			canvas.setBounds(bounds);
310
		} else {
311
			canvas.setBounds(screenshotImage.getBounds());
312
		}
313
		scrolledComposite.redraw();
314
		canvas.redraw();
315
	}
316
317
	private void drawRegion(GC gc) {
318
319
		Rectangle displayBounds = Display.getDefault().getBounds();
320
		Rectangle bounds = scrolledComposite.getBounds();
321
		bounds.width -= 5;
322
		bounds.height -= 5;
323
324
		double ratioX = (double) displayBounds.width / (double) bounds.width;
325
		double ratioY = (double) displayBounds.height / (double) bounds.height;
326
327
		Rectangle newCropRegion = new Rectangle(0, 0, 0, 0);
328
329
		if (isCropFit && !fitShotButton.getSelection()) {
330
			newCropRegion.x = (int) (cropRegionBounds.x * ratioX);
331
			newCropRegion.width = (int) (cropRegionBounds.width * ratioX);
332
			newCropRegion.y = (int) (cropRegionBounds.y * ratioY);
333
			newCropRegion.height = (int) (cropRegionBounds.height * ratioY);
334
		} else if (!isCropFit && fitShotButton.getSelection()) {
335
			newCropRegion.x = (int) (cropRegionBounds.x / ratioX);
336
			newCropRegion.width = (int) (cropRegionBounds.width / ratioX);
337
			newCropRegion.y = (int) (cropRegionBounds.y / ratioY);
338
			newCropRegion.height = (int) (cropRegionBounds.height / ratioY);
339
		} else {
340
			newCropRegion = cropRegionBounds;
341
		}
342
343
		Rectangle topRegionBounds = new Rectangle(0, 0, displayBounds.width, newCropRegion.y);
344
		int bottomRegionY = newCropRegion.y + newCropRegion.height;
345
		int bottomRegionHeight = displayBounds.height - bottomRegionY;
346
		Rectangle bottomRegionBounds = new Rectangle(0, bottomRegionY, displayBounds.width, bottomRegionHeight);
347
		Rectangle leftRegionBounds = new Rectangle(0, newCropRegion.y, newCropRegion.x, newCropRegion.height);
348
		int rightRegionX = newCropRegion.x + newCropRegion.width;
349
		int rightRegionWidth = displayBounds.width - rightRegionX;
350
		Rectangle rightRegionBounds = new Rectangle(rightRegionX, newCropRegion.y, rightRegionWidth,
351
				newCropRegion.height);
352
353
		gc.setLineStyle(SWT.LINE_DASH);
354
		gc.setAdvanced(true);
355
		gc.setAlpha(200);
356
357
		gc.fillRectangle(topRegionBounds);
358
		gc.fillRectangle(bottomRegionBounds);
359
		gc.fillRectangle(leftRegionBounds);
360
		gc.fillRectangle(rightRegionBounds);
361
362
		gc.drawRectangle(newCropRegion);
363
	}
364
365
	private Image getCropScreenshot() {
366
367
		if (cropRegionBounds == null)
368
			return screenshotImage;
369
370
		Rectangle bounds;
371
		if (!isCropFit) {
372
			bounds = cropRegionBounds;
373
		} else {
374
			bounds = new Rectangle(0, 0, 0, 0);
375
			Rectangle displayBounds = Display.getDefault().getBounds();
376
			Rectangle scrolledBounds = scrolledComposite.getBounds();
377
			double ratioX = (double) displayBounds.width / (double) scrolledBounds.width;
378
			double ratioY = (double) displayBounds.height / (double) scrolledBounds.height;
379
			bounds.x = (int) (cropRegionBounds.x * ratioX);
380
			bounds.width = (int) (cropRegionBounds.width * ratioX);
381
			bounds.y = (int) (cropRegionBounds.y * ratioY);
382
			bounds.height = (int) (cropRegionBounds.height * ratioY);
383
		}
384
385
		Image image = new Image(Display.getDefault(), bounds);
386
		GC gc = new GC(image);
387
		gc.drawImage(screenshotImage, bounds.x, bounds.y, bounds.width, bounds.height, 0, 0, bounds.width,
388
				bounds.height);
389
		gc.drawRectangle(0, 0, bounds.width - 1, bounds.height - 1);
390
		gc.dispose();
391
392
		return image;
190
	}
393
	}
191
394
192
	public Image getScreenshotImage() {
395
	public Image getScreenshotImage() {
193
		return screenshotImage;
396
		return getCropScreenshot();
194
	}
397
	}
195
398
196
}
399
}

Return to bug 160572