Skip to main content
summaryrefslogtreecommitdiffstats
blob: 05d96ec8469b4fcf8edad40f38ec6e52bb7f4cfb (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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*******************************************************************************
 * Copyright (c) 2014 IBM Corporation, BestSolution.at and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * 	   IBM Corporation - initial API
 *     Tom Schindl<tom.schindl@bestsolution.at> - initial API and implementation
 *******************************************************************************/
package org.eclipse.swt.graphics;

import java.util.List;

import javafx.geometry.Rectangle2D;
import javafx.scene.SnapshotParameters;
import javafx.scene.image.WritableImage;

import org.eclipse.swt.SWT;
import org.eclipse.swt.internal.Util;
import org.eclipse.swt.widgets.Control;

import com.sun.javafx.geom.BaseBounds;
import com.sun.javafx.geom.Shape;
import com.sun.javafx.scene.text.TextLayout;
import com.sun.javafx.scene.text.TextLayoutFactory;
import com.sun.javafx.tk.Toolkit;

public abstract class Device implements Drawable {
	public static boolean DEBUG;
	
	DeviceColor COLOR_BLACK, COLOR_DARK_RED, COLOR_DARK_GREEN, COLOR_DARK_YELLOW, COLOR_DARK_BLUE;
	DeviceColor COLOR_DARK_MAGENTA, COLOR_DARK_CYAN, COLOR_GRAY, COLOR_DARK_GRAY, COLOR_RED;
	DeviceColor COLOR_GREEN, COLOR_YELLOW, COLOR_BLUE, COLOR_MAGENTA, COLOR_CYAN, COLOR_WHITE;

	private Font SYSTEM_FONT;
	private volatile boolean disposed;
	
	public Device() {
		this(null);
	}
	
	public Device(DeviceData data) {
		init();
	}
	
	protected void init() {
		COLOR_BLACK = new DeviceColor (this, 0,0,0);
		COLOR_DARK_RED = new DeviceColor (this, 0x80,0,0);
		COLOR_DARK_GREEN = new DeviceColor (this, 0,0x80,0);
		COLOR_DARK_YELLOW = new DeviceColor (this, 0x80,0x80,0);
		COLOR_DARK_BLUE = new DeviceColor (this, 0,0,0x80);
		COLOR_DARK_MAGENTA = new DeviceColor (this, 0x80,0,0x80);
		COLOR_DARK_CYAN = new DeviceColor (this, 0,0x80,0x80);
		COLOR_GRAY = new DeviceColor (this, 0xC0,0xC0,0xC0);
		COLOR_DARK_GRAY = new DeviceColor (this, 0x80,0x80,0x80);
		COLOR_RED = new DeviceColor (this, 0xFF,0,0);
		COLOR_GREEN = new DeviceColor (this, 0,0xFF,0);
		COLOR_YELLOW = new DeviceColor (this, 0xFF,0xFF,0);
		COLOR_BLUE = new DeviceColor (this, 0,0,0xFF);
		COLOR_MAGENTA = new DeviceColor (this, 0xFF,0,0xFF);
		COLOR_CYAN = new DeviceColor (this, 0,0xFF,0xFF);
		COLOR_WHITE = new DeviceColor (this, 0xFF,0xFF,0xFF);
		
		SYSTEM_FONT = new DeviceFont(this,javafx.scene.text.Font.getDefault());
	}
	
	@Override
	public DrawableGC internal_new_GC() {
		return new NoOpDrawableGC(this, getSystemFont());
	}

	@Override
	public void internal_dispose_GC(DrawableGC gc) {
		// TODO Auto-generated method stub
		
	}
	
	public Font getSystemFont () {
		return SYSTEM_FONT;
	}
	
	public FontData[] getFontList(String name, boolean scalable) {
		if (!scalable) return new FontData[0];
		List<String> fontNames = javafx.scene.text.Font.getFontNames(name);
		FontData[] rv = new FontData[fontNames.size()];
		for( int i = 0; i < rv.length; i++ ) {
			int style = SWT.NORMAL;
			if( fontNames.get(i).toLowerCase().contains("bold") ) {
				style |= SWT.BOLD;
			}
			if( fontNames.get(i).toLowerCase().contains("italic") ) {
				style |= SWT.ITALIC;
			}
			rv[i] = new FontData(fontNames.get(i),0,style);
		}
		return rv;
	}
	
	public void dispose() {
		disposed = true;
	}
	
	public boolean isDisposed() {
		return disposed;
	}

	public static class NoOpDrawableGC implements DrawableGC {
		private Font font;
		private Drawable d;
		
		public NoOpDrawableGC(Drawable d, Font font) {
			this.d = d;
			this.font = font;
		}
		
		@Override
		public void setBackground(Color color) {
			Util.logNotImplemented();
		}
		
		@Override
		public void fillRectangle(int x, int y, int width, int height) {
			Util.logNotImplemented();
		}
		
		@Override
		public void drawRectangle(int x, int y, int width, int height) {
			Util.logNotImplemented();
		}

		@Override
		public void fillOval(int x, int y, int width, int height) {
			Util.logNotImplemented();
		}
		
		@Override
		public void setAlpha(int alpha) {
			Util.logNotImplemented();
		}
		
		@Override
		public void setTransform(Transform transform) {
			Util.logNotImplemented();
		}
		
		@Override
		public void setForeground(Color color) {
			Util.logNotImplemented();
		}
		
		@Override
		public void fillPath(Path path) {
			Util.logNotImplemented();
		}
		
		@Override
		public void drawPath(Path path) {
			Util.logNotImplemented();
		}
		
		@Override
		public void drawImage(Image image, int srcX, int srcY, int srcWidth,
				int srcHeight, int destX, int destY, int destWidth,
				int destHeight) {
			Util.logNotImplemented();
		}
		
		@Override
		public void setLineWidth(int lineWidth) {
			Util.logNotImplemented();
		}
		
		@Override
		public void drawLine(int x1, int y1, int x2, int y2) {
			Util.logNotImplemented();
		}
		
		@Override
		public void drawImage(Image image, int x, int y) {
			Util.logNotImplemented();
		}
		
		@Override
		public void drawText (String string, int x, int y, int flags) {
			Util.logNotImplemented();
		}
		
		@Override
		public void drawPolyline(int[] pointArray) {
			Util.logNotImplemented();
		}
		
		@Override
		public Point textExtent(String string, int flags) {
			TextLayoutFactory factory = Toolkit.getToolkit().getTextLayoutFactory();
			TextLayout layout = factory.createLayout();
			layout.setContent(string, getFont().internal_getNativeObject().impl_getNativeFont());
			BaseBounds b = layout.getBounds();
					
			return new Point((int)b.getWidth(), (int)b.getHeight());
		}
		
		@Override
		public void setFont(org.eclipse.swt.graphics.Font font) {
			this.font = font;
		}
		
		@Override
		public Font getFont() {
			return font;
		}
		
		@Override
		public void fillGradientRectangle(int x, int y, int width, int height,
				boolean vertical) {
			Util.logNotImplemented();
		}
		
		@Override
		public void setClipping(Region region) {
			Util.logNotImplemented();
		}
		
		@Override
		public void fillPolygon(int[] pointArray) {
			Util.logNotImplemented();
		}
		
		@Override
		public void drawPolygon(int[] pointArray) {
			Util.logNotImplemented();
		}
		
		@Override
		public void drawRoundRectangle(int x, int y, int width, int height,
				int arcWidth, int arcHeight) {
			Util.logNotImplemented();
		}
		
		@Override
		public void fillRoundRectangle(int x, int y, int width, int height,
				int arcWidth, int arcHeight) {
			Util.logNotImplemented();
		}
		
		@Override
		public void drawPoint(int x, int y) {
			Util.logNotImplemented();
		}
		
		@Override
		public void drawFocus(int x, int y, int width, int height) {
			Util.logNotImplemented();
		}
		
		@Override
		public Color getBackground() {
			Util.logNotImplemented();
			return null;
		}
		
		@Override
		public Color getForeground() {
			Util.logNotImplemented();
			return null;
		}
		
		@Override
		public void fillArc(int x, int y, int width, int height,
				int startAngle, int arcAngle) {
			Util.logNotImplemented();
		}
		
		@Override
		public void copyArea(Image image, int x, int y) {
			if( d instanceof Control ) {
				javafx.scene.layout.Region nativeObject = ((Control) d).internal_getNativeControl();
				SnapshotParameters p = new SnapshotParameters();
				p.setViewport(new Rectangle2D(x, y, image.getBounds().width, image.getBounds().height));
				nativeObject.snapshot(p, (WritableImage) image.internal_getImage());
			}
		}
		
		@Override
		public void drawArc(int x, int y, int width, int height,
				int startAngle, int arcAngle) {
			Util.logNotImplemented();
		}
		
		@Override
		public Point stringExtent(String string) {
			TextLayoutFactory factory = Toolkit.getToolkit().getTextLayoutFactory();
			TextLayout layout = factory.createLayout();
			layout.setContent(string, getFont().internal_getNativeObject().impl_getNativeFont());
			BaseBounds b = layout.getBounds();
					
			return new Point((int)b.getWidth(), (int)b.getHeight());
		}
				
		@Override
		public void setLineCap(int cap) {
			Util.logNotImplemented();
		}
		
		@Override
		public void setLineJoin(int join) {
			Util.logNotImplemented();
		}

		@Override
		public void drawShape(int x, int y, Shape shape) {
			Util.logNotImplemented();
		}
		
		@Override
		public void setLineStyle(int lineStyle) {
			Util.logNotImplemented();
		}
		
		@Override
		public void setBackgroundPattern(Pattern pattern) {
			Util.logNotImplemented();
		}
		
		@Override
		public void setForegroundPattern(Pattern pattern) {
			Util.logNotImplemented();
		}
		
		@Override
		public void dispose() {
			Util.logNotImplemented();
		}
		
		@Override
		public void drawOval(int x, int y, int width, int height) {
			Util.logNotImplemented();
		}
	}
	
	public Color getSystemColor (int id) {
		switch (id) {
			case SWT.COLOR_BLACK: 				return COLOR_BLACK;
			case SWT.COLOR_DARK_RED: 			return COLOR_DARK_RED;
			case SWT.COLOR_DARK_GREEN:	 		return COLOR_DARK_GREEN;
			case SWT.COLOR_DARK_YELLOW: 		return COLOR_DARK_YELLOW;
			case SWT.COLOR_DARK_BLUE: 			return COLOR_DARK_BLUE;
			case SWT.COLOR_DARK_MAGENTA: 		return COLOR_DARK_MAGENTA;
			case SWT.COLOR_DARK_CYAN: 			return COLOR_DARK_CYAN;
			case SWT.COLOR_GRAY: 				return COLOR_GRAY;
			case SWT.COLOR_DARK_GRAY: 			return COLOR_DARK_GRAY;
			case SWT.COLOR_RED: 				return COLOR_RED;
			case SWT.COLOR_GREEN: 				return COLOR_GREEN;
			case SWT.COLOR_YELLOW: 				return COLOR_YELLOW;
			case SWT.COLOR_BLUE: 				return COLOR_BLUE;
			case SWT.COLOR_MAGENTA: 			return COLOR_MAGENTA;
			case SWT.COLOR_CYAN: 				return COLOR_CYAN;
			case SWT.COLOR_WHITE: 				return COLOR_WHITE;
		}
		return COLOR_BLACK;
	}
	
	static class DeviceColor extends Color {

		public DeviceColor(Device device, int red, int green, int blue) {
			super(device, red, green, blue);
		}
		
		@Override
		public void dispose() {
			// not disposable
		}
	}
	
	static class DeviceFont extends Font {
		public DeviceFont(Device device, javafx.scene.text.Font font) {
			super(device,font);
		}
		
		@Override
		public void dispose() {
			// not disposable
		}
	}
}

Back to the top