Commit | Line | Data |
---|---|---|
73005152 BH |
1 | /********************************************************************** |
2 | * Copyright (c) 2005, 2008, 2011 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 | * $Id: NGC.java,v 1.3 2008/01/24 02:29:01 apnan Exp $ | |
8 | * | |
9 | * Contributors: | |
10 | * IBM - Initial API and implementation | |
11 | * Bernd Hufmann - Updated for TMF | |
12 | **********************************************************************/ | |
13 | package org.eclipse.linuxtools.tmf.ui.views.uml2sd; | |
14 | ||
15 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.core.Frame; | |
16 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IColor; | |
17 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IFont; | |
18 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC; | |
19 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IImage; | |
20 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.impl.ColorImpl; | |
21 | import org.eclipse.linuxtools.tmf.ui.views.uml2sd.preferences.SDViewPref; | |
22 | import org.eclipse.swt.SWT; | |
23 | import org.eclipse.swt.graphics.Color; | |
24 | import org.eclipse.swt.graphics.Font; | |
25 | import org.eclipse.swt.graphics.FontData; | |
26 | import org.eclipse.swt.graphics.GC; | |
27 | import org.eclipse.swt.graphics.Image; | |
28 | import org.eclipse.swt.graphics.Point; | |
29 | import org.eclipse.swt.graphics.Rectangle; | |
30 | import org.eclipse.swt.widgets.Display; | |
31 | ||
32 | /** | |
33 | * @author sveyrier | |
34 | * | |
35 | */ | |
36 | public class NGC implements IGC { | |
37 | ||
38 | protected GC context; | |
39 | protected SDWidget view; | |
40 | protected Font tempFont = null; | |
41 | protected IColor gradientColor = null; | |
42 | protected IColor backGround = null; | |
43 | protected IColor foreGround = null; | |
44 | protected int y_; | |
45 | protected int x_; | |
46 | protected int yx; | |
47 | protected int xx; | |
48 | protected boolean drawWithFocus = false; | |
49 | ||
e6ace8bb BH |
50 | private static int vscreen_bounds = 0; |
51 | ||
73005152 BH |
52 | public NGC(SDWidget scrollView, GC gc) { |
53 | context = gc; | |
54 | view = scrollView; | |
55 | } | |
56 | ||
57 | @Override | |
58 | public void setLineStyle(int style) { | |
59 | context.setLineStyle(style); | |
60 | } | |
61 | ||
62 | @Override | |
63 | public int getLineStyle() { | |
64 | return context.getLineStyle(); | |
65 | } | |
66 | ||
67 | @Override | |
68 | public int getContentsX() { | |
69 | return Math.round(view.getContentsX() / view.zoomValue); | |
70 | } | |
71 | ||
72 | @Override | |
73 | public int getContentsY() { | |
74 | return Math.round(view.getContentsY() / view.zoomValue); | |
75 | } | |
76 | ||
77 | @Override | |
78 | public int getVisibleWidth() { | |
79 | return Math.round(view.getVisibleWidth() / view.zoomValue); | |
80 | } | |
81 | ||
82 | @Override | |
83 | public int getVisibleHeight() { | |
84 | return Math.round(view.getVisibleHeight() / view.zoomValue); | |
85 | } | |
86 | ||
87 | @Override | |
88 | public int contentsToViewX(int x) { | |
89 | return view.contentsToViewX(x); | |
90 | } | |
91 | ||
92 | @Override | |
93 | public int contentsToViewY(int y) { | |
94 | return view.contentsToViewY(y); | |
95 | } | |
96 | ||
97 | protected byte code(int x, int y) { | |
98 | byte c = 0; | |
99 | y_ = vscreen_bounds; | |
100 | x_ = vscreen_bounds; | |
101 | yx = view.getVisibleHeight() + vscreen_bounds; | |
102 | xx = view.getVisibleWidth() + vscreen_bounds; | |
103 | if (y > yx) | |
104 | c |= 0x01; // top | |
105 | else if (y < y_) | |
106 | c |= 0x02; // bottom | |
107 | if (x > xx) | |
108 | c |= 0x04; // right | |
109 | else if (x < x_) | |
110 | c |= 0x08; // left | |
111 | return c; | |
112 | } | |
113 | ||
114 | @Override | |
115 | public void drawLine(int x1, int y1, int x2, int y2) { | |
116 | x1 = Math.round(x1 * view.zoomValue); | |
117 | y1 = Math.round(y1 * view.zoomValue); | |
118 | x2 = Math.round(x2 * view.zoomValue); | |
119 | y2 = Math.round(y2 * view.zoomValue); | |
120 | x1 = view.contentsToViewX(x1); | |
121 | y1 = view.contentsToViewY(y1); | |
122 | x2 = view.contentsToViewX(x2); | |
123 | y2 = view.contentsToViewY(y2); | |
124 | ||
125 | byte code1 = code(x1, y1); | |
126 | byte code2 = code(x2, y2); | |
127 | byte codex; | |
128 | boolean draw = false; | |
129 | boolean end = false; | |
130 | int x = 0, y = 0; | |
131 | ||
132 | do { | |
133 | if (code1 == 0 && code2 == 0) { | |
134 | draw = true; | |
135 | end = true; | |
136 | } else if ((code1 & code2) != 0) { | |
137 | end = true; | |
138 | } else { | |
139 | codex = (code1 != 0) ? code1 : code2; | |
140 | if ((codex & 0x01) != 0) // top | |
141 | { | |
142 | x = x1 + ((x2 - x1) * (yx - y1)) / (y2 - y1); | |
143 | y = yx; | |
144 | } else if ((codex & 0x02) != 0) // bottom | |
145 | { | |
146 | x = x1 + ((x2 - x1) * (y_ - y1)) / (y2 - y1); | |
147 | y = y_; | |
148 | } else if ((codex & 0x04) != 0) // right | |
149 | { | |
150 | y = y1 + ((y2 - y1) * (xx - x1)) / (x2 - x1); | |
151 | x = xx; | |
152 | } else if ((codex & 0x08) != 0) // left | |
153 | { | |
154 | y = y1 + ((y2 - y1) * (x_ - x1)) / (x2 - x1); | |
155 | x = x_; | |
156 | } | |
157 | ||
158 | if (codex == code1) { | |
159 | x1 = x; | |
160 | y1 = y; | |
161 | code1 = code(x1, y1); | |
162 | } else { | |
163 | x2 = x; | |
164 | y2 = y; | |
165 | code2 = code(x2, y2); | |
166 | } | |
167 | } | |
168 | } while (!end); | |
169 | if (draw) { | |
170 | context.drawLine(x1, y1, x2, y2); | |
171 | } | |
172 | } | |
173 | ||
174 | @Override | |
175 | public void drawRectangle(int x, int y, int width, int height) { | |
176 | x = Math.round(x * view.zoomValue); | |
177 | // Workaround to avoid problems for some special cases (not very nice) | |
178 | if (y != getContentsY()) { | |
179 | y = Math.round(y * view.zoomValue); | |
180 | y = view.contentsToViewY(y); | |
181 | } else | |
182 | y = 0; | |
183 | width = Math.round(width * view.zoomValue); | |
184 | height = Math.round(height * view.zoomValue); | |
185 | x = view.contentsToViewX(x); | |
186 | ||
187 | if (x < -vscreen_bounds) { | |
188 | width = width + x + vscreen_bounds; | |
189 | x = -vscreen_bounds; | |
190 | } | |
191 | if (y < -vscreen_bounds) { | |
192 | height = height + y + vscreen_bounds; | |
193 | y = -vscreen_bounds; | |
194 | } | |
195 | if ((width < -vscreen_bounds) && (x + width < -vscreen_bounds)) | |
196 | width = -vscreen_bounds; | |
197 | else if (width + x > view.getVisibleWidth() + vscreen_bounds) | |
198 | width = view.getVisibleWidth() + vscreen_bounds - x; | |
199 | if ((height < -vscreen_bounds) && (y + height < -vscreen_bounds)) | |
200 | height = -vscreen_bounds; | |
201 | else if (height + y > view.getVisibleHeight() + vscreen_bounds) | |
202 | height = view.getVisibleHeight() + vscreen_bounds - y; | |
203 | context.drawRectangle(x, y, width, height); | |
204 | } | |
205 | ||
206 | @Override | |
207 | public void drawFocus(int x, int y, int width, int height) { | |
208 | IColor bC = getBackground(); | |
209 | IColor fC = getForeground(); | |
210 | ||
211 | if (width < 0) { | |
212 | x = x + width; | |
213 | width = -width; | |
214 | } | |
215 | ||
216 | if (height < 0) { | |
217 | y = y + height; | |
218 | height = -height; | |
219 | } | |
220 | ||
221 | x = Math.round(x * view.zoomValue); | |
222 | y = Math.round(y * view.zoomValue); | |
223 | width = Math.round(width * view.zoomValue); | |
224 | height = Math.round(height * view.zoomValue); | |
225 | ||
226 | setForeground(Frame.getUserPref().getForeGroundColorSelection()); | |
227 | setBackground(Frame.getUserPref().getBackGroundColorSelection()); | |
228 | ||
229 | context.drawFocus(view.contentsToViewX(x - 1), view.contentsToViewY(y - 1), width + 3, height + 3); | |
230 | ||
231 | setBackground(bC); | |
232 | setForeground(fC); | |
233 | } | |
234 | ||
235 | @Override | |
236 | public void fillPolygon(int[] points) { | |
237 | int len = (points.length / 2) * 2; | |
238 | int[] localPoint = new int[len]; | |
239 | for (int i = 0; i < len; i++) { | |
240 | localPoint[i] = view.contentsToViewX(Math.round(points[i] * view.zoomValue)); | |
241 | i++; | |
242 | localPoint[i] = view.contentsToViewY(Math.round(points[i] * view.zoomValue)); | |
243 | } | |
244 | ||
245 | if (validatePolygonHeight(localPoint) <= 0) | |
246 | return; | |
247 | ||
248 | context.fillPolygon(localPoint); | |
249 | } | |
250 | ||
251 | @Override | |
252 | public void drawPolygon(int[] points) { | |
253 | int len = (points.length / 2) * 2; | |
254 | int[] localPoint = new int[len]; | |
255 | for (int i = 0; i < len; i++) { | |
256 | localPoint[i] = view.contentsToViewX(Math.round(points[i] * view.zoomValue)); | |
257 | i++; | |
258 | localPoint[i] = view.contentsToViewY(Math.round(points[i] * view.zoomValue)); | |
259 | } | |
260 | ||
261 | if (validatePolygonHeight(localPoint) <= 0) | |
262 | return; | |
263 | ||
264 | context.drawPolygon(localPoint); | |
265 | } | |
266 | ||
267 | private int validatePolygonHeight(int[] localPoint) { | |
268 | int i = 1; | |
269 | int max = 0; | |
270 | int min = Integer.MAX_VALUE; | |
271 | while (i < localPoint.length) { | |
272 | max = Math.abs(localPoint[i]) > Math.abs(max) ? localPoint[i] : max; | |
273 | min = Math.abs(localPoint[i]) < Math.abs(min) ? localPoint[i] : min; | |
274 | i+=2; | |
275 | } | |
276 | int height = max - min; | |
277 | if (min < -vscreen_bounds) { | |
278 | height = height + min + vscreen_bounds; | |
279 | min = -vscreen_bounds; | |
280 | } | |
281 | if ((height < -vscreen_bounds) && (min + height < -vscreen_bounds)) | |
282 | height = -vscreen_bounds; | |
283 | ||
284 | else if (height + min > view.getVisibleHeight() + vscreen_bounds) | |
285 | height = view.getVisibleHeight() + vscreen_bounds - min; | |
286 | return height; | |
287 | } | |
288 | ||
289 | @Override | |
290 | public void fillRectangle(int x, int y, int width, int height) { | |
291 | x = Math.round(x * view.zoomValue); | |
292 | // Workaround to avoid problems for some special cases (not very nice) | |
293 | if (y != getContentsY()) { | |
294 | y = Math.round(y * view.zoomValue); | |
295 | y = view.contentsToViewY(y) + 1; | |
296 | } else | |
297 | y = 1; | |
298 | width = Math.round(width * view.zoomValue) - 1; | |
299 | height = Math.round(height * view.zoomValue) - 1; | |
300 | x = view.contentsToViewX(x) + 1; | |
301 | if (x < -vscreen_bounds) { | |
302 | width = width + x + vscreen_bounds; | |
303 | x = -vscreen_bounds; | |
304 | } | |
305 | if (y < -vscreen_bounds) { | |
306 | height = height + y + vscreen_bounds; | |
307 | y = -vscreen_bounds; | |
308 | } | |
309 | if ((width < -vscreen_bounds) && (x + width < -vscreen_bounds)) | |
310 | width = -vscreen_bounds; | |
311 | else if (width + x > view.getVisibleWidth() + vscreen_bounds) | |
312 | width = view.getVisibleWidth() + vscreen_bounds - x; | |
313 | if ((height < -vscreen_bounds) && (y + height < -vscreen_bounds)) | |
314 | height = -vscreen_bounds; | |
315 | else if (height + y > view.getVisibleHeight() + vscreen_bounds) | |
316 | height = view.getVisibleHeight() + vscreen_bounds - y; | |
317 | context.fillRectangle(x, y, width, height); | |
318 | ||
319 | } | |
320 | ||
321 | @Override | |
322 | public void fillGradientRectangle(int x, int y, int width, int height, boolean vertical) { | |
323 | x = Math.round(x * view.zoomValue); | |
324 | y = Math.round(y * view.zoomValue); | |
325 | width = Math.round(width * view.zoomValue); | |
326 | height = Math.round(height * view.zoomValue); | |
327 | IColor tempColor = foreGround; | |
328 | setForeground(gradientColor); | |
329 | x = view.contentsToViewX(x); | |
330 | y = view.contentsToViewY(y); | |
331 | ||
332 | if (x < -vscreen_bounds) { | |
333 | width = width + x + vscreen_bounds; | |
334 | x = -vscreen_bounds; | |
335 | } | |
336 | if (y < -vscreen_bounds) { | |
337 | height = height + y + vscreen_bounds; | |
338 | y = -vscreen_bounds; | |
339 | } | |
340 | if ((width < -vscreen_bounds) && (x + width < -vscreen_bounds)) | |
341 | width = -vscreen_bounds; | |
342 | else if (width + x > view.getVisibleWidth() + vscreen_bounds) | |
343 | width = view.getVisibleWidth() + vscreen_bounds - x; | |
344 | if ((height < -vscreen_bounds) && (y + height < -vscreen_bounds)) | |
345 | height = -vscreen_bounds; | |
346 | else if (height + y > view.getVisibleHeight() + vscreen_bounds) | |
347 | height = view.getVisibleHeight() + vscreen_bounds - y; | |
348 | if (vertical) { | |
349 | context.fillGradientRectangle(x, y, width, height, vertical); | |
350 | } | |
351 | else { | |
352 | context.fillGradientRectangle(x + width, y, -width, height + 1, vertical); | |
353 | } | |
354 | setForeground(tempColor); | |
355 | } | |
356 | ||
357 | ||
358 | @Override | |
359 | public int textExtent(String name) { | |
360 | return ((Point) (context.textExtent(name))).x; | |
361 | } | |
362 | ||
363 | @Override | |
364 | public void drawText(String string, int x, int y, boolean trans) { | |
365 | x = Math.round(x * view.zoomValue); | |
366 | y = Math.round(y * view.zoomValue); | |
367 | context.drawText(string, view.contentsToViewX(x), view.contentsToViewY(y), trans); | |
368 | if (drawWithFocus) { | |
369 | Point r = context.textExtent(string); | |
370 | context.drawFocus(x - 1, y - 1, r.x + 2, r.y + 2); | |
371 | } | |
372 | } | |
373 | ||
374 | @Override | |
375 | public void drawText(String string, int x, int y) { | |
376 | x = Math.round(x * view.zoomValue); | |
377 | y = Math.round(y * view.zoomValue); | |
378 | context.drawText(string, view.contentsToViewX(x), view.contentsToViewY(y), true); | |
379 | if (drawWithFocus) { | |
380 | Point r = context.textExtent(string); | |
381 | context.drawFocus(x - 1, y - 1, r.x + 2, r.y + 2); | |
382 | } | |
383 | } | |
384 | ||
385 | @Override | |
386 | public void fillOval(int x, int y, int width, int height) { | |
387 | x = Math.round(x * view.zoomValue); | |
388 | y = Math.round(y * view.zoomValue); | |
389 | width = Math.round(width * view.zoomValue); | |
390 | height = Math.round(height * view.zoomValue); | |
391 | context.fillOval(view.contentsToViewX(x), view.contentsToViewY(y), width, height); | |
392 | } | |
393 | ||
394 | @Override | |
395 | public IColor getBackground() { | |
396 | if (backGround != null) | |
397 | if (backGround.getColor() instanceof Color) | |
398 | if (!((Color) (backGround.getColor())).isDisposed()) | |
399 | return backGround; | |
400 | return ColorImpl.getSystemColor(SWT.COLOR_WHITE); | |
401 | } | |
402 | ||
403 | @Override | |
404 | public IColor getForeground() { | |
405 | if (foreGround != null) | |
406 | if (foreGround.getColor() instanceof Color) | |
407 | if (!((Color) (foreGround.getColor())).isDisposed()) | |
408 | return foreGround; | |
409 | return ColorImpl.getSystemColor(SWT.COLOR_WHITE); | |
410 | } | |
411 | ||
412 | @Override | |
413 | public void setBackground(IColor color) { | |
414 | if (color == null) | |
415 | return; | |
416 | if (color.getColor() instanceof Color) { | |
417 | context.setBackground((Color) color.getColor()); | |
418 | backGround = color; | |
419 | } | |
420 | } | |
421 | ||
422 | @Override | |
423 | public void setForeground(IColor color) { | |
424 | if (color == null) | |
425 | return; | |
426 | if (color.getColor() instanceof Color) { | |
427 | Color c = (Color) color.getColor(); | |
428 | if (!c.isDisposed()) { | |
429 | context.setForeground(c); | |
430 | foreGround = color; | |
431 | } | |
432 | } | |
433 | } | |
434 | ||
435 | @Override | |
436 | public void setGradientColor(IColor color) { | |
437 | if (color == null) | |
438 | return; | |
439 | if (color.getColor() instanceof Color) | |
440 | gradientColor = color; | |
441 | } | |
442 | ||
443 | @Override | |
444 | public void setLineWidth(int width) { | |
445 | if (view.isPrinting()) | |
446 | context.setLineWidth(width * 2); | |
447 | else | |
448 | context.setLineWidth(width); | |
449 | } | |
450 | ||
451 | @Override | |
452 | public int getLineWidth() { | |
453 | return context.getLineWidth(); | |
454 | } | |
455 | ||
456 | // Linux GTK Workaround | |
457 | protected void localDrawText(String string, int x, int y, boolean trans) { | |
458 | Point r = context.textExtent(string); | |
459 | if (!trans) | |
460 | context.fillRectangle(x, y, r.x, r.y); | |
461 | context.drawText(string, x, y, trans); | |
462 | if ((drawWithFocus) && (string.length() > 1)) | |
463 | context.drawFocus(x - 1, y - 1, r.x + 2, r.y + 2); | |
464 | } | |
465 | ||
466 | @Override | |
467 | public void drawTextTruncatedCentred(String name, int _x, int _y, int width, int height, boolean trans) { | |
468 | Point tx = context.textExtent(name); | |
469 | _x = Math.round(_x * view.zoomValue); | |
470 | int y = 0; | |
471 | // Workaround to avoid round problems for some special cases (not very nice) | |
472 | if (_y != getContentsY()) { | |
473 | _y = Math.round(_y * view.zoomValue); | |
474 | y = view.contentsToViewY(_y); | |
475 | } | |
476 | width = Math.round(width * view.zoomValue); | |
477 | height = Math.round(height * view.zoomValue); | |
478 | int x = view.contentsToViewX(_x); | |
479 | if (tx.y > height) | |
480 | return; | |
481 | ||
482 | // Adjust height and y | |
483 | if (y < -vscreen_bounds) { | |
484 | height = height + y + vscreen_bounds; | |
485 | y = -vscreen_bounds; | |
486 | } | |
487 | if ((height < -vscreen_bounds) && (y + height < -vscreen_bounds)) | |
488 | height = -vscreen_bounds; | |
489 | else if (height + y > view.getVisibleHeight() + vscreen_bounds) | |
490 | height = view.getVisibleHeight() + vscreen_bounds - y; | |
491 | ||
492 | if (tx.x <= width) { | |
493 | localDrawText(name, x + 1 + (width - tx.x) / 2, y + 1 + (height - tx.y) / 2, trans); | |
494 | } else { | |
495 | String nameToDisplay = name; | |
496 | for (int i = name.length() - 1; i >= 0 && context.textExtent(nameToDisplay).x >= width; i--) | |
497 | nameToDisplay = name.substring(0, i); | |
498 | int dotCount = 0; | |
499 | for (int i = 1; i <= 3 && nameToDisplay.length() - i > 0; i++) | |
500 | dotCount++; | |
501 | nameToDisplay = nameToDisplay.substring(0, nameToDisplay.length() - dotCount); | |
502 | StringBuffer buf = new StringBuffer(nameToDisplay); | |
503 | for (int i = 0; i < dotCount; i++) { | |
504 | buf.append("."); //$NON-NLS-1$ | |
505 | // nameToDisplay = nameToDisplay + "."; //$NON-NLS-1$ | |
506 | } | |
507 | nameToDisplay = buf.toString(); | |
508 | localDrawText(nameToDisplay, x + 1 + (width - context.textExtent(nameToDisplay).x) / 2, y + 1 + (height - context.textExtent(nameToDisplay).y) / 2, trans); | |
509 | } | |
510 | } | |
511 | ||
512 | @Override | |
513 | public void drawTextTruncated(String name, int _x, int _y, int width, int height, boolean trans) { | |
514 | _x = Math.round(_x * view.zoomValue); | |
515 | _y = Math.round(_y * view.zoomValue); | |
516 | width = Math.round(width * view.zoomValue); | |
517 | height = Math.round(height * view.zoomValue); | |
518 | int x = view.contentsToViewX(_x); | |
519 | int y = view.contentsToViewY(_y); | |
520 | if (context.textExtent(name).x <= width) { | |
521 | localDrawText(name, x + 1, y + 1 + height, trans); | |
522 | } else { | |
523 | String nameToDisplay = name; | |
524 | for (int i = name.length() - 1; i >= 0 && context.textExtent(nameToDisplay).x >= width; i--) | |
525 | nameToDisplay = name.substring(0, i); | |
526 | int dotCount = 0; | |
527 | for (int i = 1; i <= 3 && nameToDisplay.length() - i > 0; i++) | |
528 | dotCount++; | |
529 | nameToDisplay = nameToDisplay.substring(0, nameToDisplay.length() - dotCount); | |
530 | ||
531 | StringBuffer buf = new StringBuffer(nameToDisplay); | |
532 | ||
533 | for (int i = 0; i < dotCount; i++) { | |
534 | buf.append("."); //$NON-NLS-1$ | |
535 | // nameToDisplay = nameToDisplay + "."; //$NON-NLS-1$ | |
536 | } | |
537 | nameToDisplay = buf.toString(); | |
538 | localDrawText(nameToDisplay, x + 1, y + 1 + height, trans); | |
539 | } | |
540 | } | |
541 | ||
542 | @Override | |
543 | public void drawImage(IImage image, int _x, int _y, int maxWith, int maxHeight) { | |
544 | Image img = null; | |
545 | if (image != null && image.getImage() instanceof Image) | |
546 | img = (Image) image.getImage(); | |
547 | else { | |
548 | _x = Math.round(_x * view.zoomValue); | |
549 | _y = Math.round(_y * view.zoomValue); | |
550 | int x = view.contentsToViewX(_x); | |
551 | int y = view.contentsToViewY(_y); | |
552 | float tempZoom = view.zoomValue; | |
553 | int width = Math.round(maxWith * tempZoom); | |
554 | int height = Math.round(maxHeight * tempZoom); | |
555 | context.setBackground(view.getDisplay().getSystemColor(SWT.COLOR_RED)); | |
556 | context.fillRectangle(x, y, width, height); | |
557 | return; | |
558 | } | |
559 | _x = Math.round(_x * view.zoomValue); | |
560 | _y = Math.round(_y * view.zoomValue); | |
561 | int x = view.contentsToViewX(_x); | |
562 | int y = view.contentsToViewY(_y); | |
563 | Rectangle b = ((Image) image.getImage()).getBounds(); | |
564 | int width = b.width; | |
565 | int height = b.height; | |
566 | if (width > maxWith) | |
567 | width = maxWith; | |
568 | if (height > maxHeight) | |
569 | height = maxHeight; | |
570 | float tempZoom = view.zoomValue; | |
571 | width = Math.round(width * tempZoom); | |
572 | height = Math.round(height * tempZoom); | |
573 | ||
574 | if (view.printing && width > 0 && height > 0) { | |
575 | Image dbuffer = new Image(view.getDisplay(), width, height); | |
576 | GC tempgc = new GC(dbuffer); | |
577 | tempgc.drawImage(img, 0, 0, b.width, b.height, 0, 0, width, height); | |
578 | Image dbuffer2 = new Image(view.getDisplay(), dbuffer.getImageData()); | |
579 | context.drawImage(dbuffer2, x, y); | |
580 | tempgc.dispose(); | |
581 | dbuffer.dispose(); | |
582 | dbuffer2.dispose(); | |
583 | } else { | |
584 | context.drawImage(img, 0, 0, b.width, b.height, x, y, width, height); | |
585 | } | |
586 | } | |
587 | ||
588 | @Override | |
589 | public void drawArc(int x, int y, int width, int height, int startAngle, int endAngle) { | |
590 | x = Math.round(x * view.zoomValue); | |
591 | y = Math.round(y * view.zoomValue); | |
592 | width = Math.round(width * view.zoomValue); | |
593 | height = Math.round(height * view.zoomValue); | |
594 | if (width == 0 || height == 0 || endAngle == 0) | |
595 | return; | |
596 | context.drawArc(view.contentsToViewX(x), view.contentsToViewY(y), width, height, startAngle, endAngle); | |
597 | } | |
598 | ||
599 | @Override | |
600 | public void setFont(IFont font) { | |
601 | if (font.getFont() != null && ((Font) font.getFont()).getFontData().length > 0) { | |
602 | FontData fontData = ((Font) font.getFont()).getFontData()[0]; | |
603 | if (SDViewPref.getInstance().fontLinked() || view.printing) { | |
604 | int h = Math.round(fontData.getHeight() * view.zoomValue); | |
605 | if (h > 0) | |
606 | fontData.setHeight(h); | |
607 | } | |
608 | if (tempFont != null) | |
609 | tempFont.dispose(); | |
610 | tempFont = new Font(Display.getCurrent(), fontData); | |
611 | context.setFont(tempFont); | |
612 | } | |
613 | } | |
614 | ||
615 | @Override | |
616 | public int getFontHeight(IFont font) { | |
617 | if (font.getFont() != null && (font.getFont() instanceof Font) && ((Font) font.getFont()).getFontData().length > 0) { | |
618 | Font toRestore = context.getFont(); | |
619 | context.setFont((Font) font.getFont()); | |
620 | int height = context.textExtent("lp").y;//$NON-NLS-1$ | |
621 | context.setFont(toRestore); | |
622 | return height; | |
623 | } | |
624 | return 0; | |
625 | } | |
626 | ||
627 | protected int getCurrentFontHeight() { | |
628 | return context.textExtent("lp").y; //$NON-NLS-1$ | |
629 | } | |
630 | ||
631 | @Override | |
632 | public int getFontWidth(IFont font) { | |
633 | if ((font.getFont() != null) && (font.getFont() instanceof Font)) { | |
634 | Font toRestore = context.getFont(); | |
635 | context.setFont((Font) font.getFont()); | |
636 | int width = context.getFontMetrics().getAverageCharWidth(); | |
637 | context.setFont(toRestore); | |
638 | return width; | |
639 | } | |
640 | return 0; | |
641 | } | |
642 | ||
643 | public void dispose() { | |
644 | if (tempFont != null) | |
645 | tempFont.dispose(); | |
646 | tempFont = null; | |
647 | if (context != null) | |
648 | context.dispose(); | |
649 | context = null; | |
650 | } | |
651 | ||
652 | @Override | |
653 | public float getZoom() { | |
654 | if (view != null) | |
655 | return view.zoomValue; | |
656 | else | |
657 | return 1; | |
658 | } | |
659 | ||
660 | @Override | |
661 | public int getLineDotStyle() { | |
662 | return SWT.LINE_DOT; | |
663 | } | |
664 | ||
665 | @Override | |
666 | public int getLineDashStyle() { | |
667 | return SWT.LINE_DASH; | |
668 | } | |
669 | ||
670 | @Override | |
671 | public int getLineSolidStyle() { | |
672 | return SWT.LINE_SOLID; | |
673 | } | |
674 | ||
675 | @Override | |
676 | public IColor createColor(int r, int g, int b) { | |
677 | return new ColorImpl(Display.getDefault(), r, g, b); | |
678 | } | |
679 | ||
680 | /* | |
681 | * (non-Javadoc) | |
682 | * @see org.eclipse.linuxtools.tmf.ui.views.uml2sd.drawings.IGC#setDrawTextWithFocusStyle(boolean) | |
683 | */ | |
684 | @Override | |
685 | public void setDrawTextWithFocusStyle(boolean focus) { | |
686 | drawWithFocus = focus; | |
687 | } | |
e6ace8bb BH |
688 | |
689 | protected static int getVscreenBounds() { | |
690 | return vscreen_bounds; | |
691 | } | |
692 | ||
693 | protected static void setVscreenBounds(int vBounds) { | |
694 | vscreen_bounds = vBounds; | |
695 | } | |
696 | ||
73005152 | 697 | } |