129
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2008 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 * IBM Corporation - initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module dwtx.jface.text.source.ImageUtilities;
|
|
14
|
|
15 import dwt.dwthelper.utils;
|
|
16
|
|
17 import dwt.DWT;
|
|
18 import dwt.graphics.FontMetrics;
|
|
19 import dwt.graphics.GC;
|
|
20 import dwt.graphics.Image;
|
|
21 import dwt.graphics.Rectangle;
|
|
22 import dwt.widgets.Canvas;
|
|
23
|
|
24 /**
|
|
25 * Provides methods for drawing images onto a canvas.
|
|
26 * <p>
|
|
27 * This class is neither intended to be instantiated nor subclassed.
|
|
28 * </p>
|
|
29 *
|
|
30 * @since 3.0
|
|
31 * @noinstantiate This class is not intended to be instantiated by clients.
|
|
32 * @noextend This class is not intended to be subclassed by clients.
|
|
33 */
|
|
34 public class ImageUtilities {
|
|
35
|
|
36 /**
|
|
37 * Draws an image aligned inside the given rectangle on the given canvas.
|
|
38 *
|
|
39 * @param image the image to be drawn
|
|
40 * @param gc the drawing GC
|
|
41 * @param canvas the canvas on which to draw
|
|
42 * @param r the clipping rectangle
|
|
43 * @param halign the horizontal alignment of the image to be drawn
|
|
44 * @param valign the vertical alignment of the image to be drawn
|
|
45 */
|
|
46 public static void drawImage(Image image, GC gc, Canvas canvas, Rectangle r, int halign, int valign) {
|
|
47 if (image !is null) {
|
|
48
|
|
49 Rectangle bounds= image.getBounds();
|
|
50
|
|
51 int x= 0;
|
|
52 switch(halign) {
|
|
53 case DWT.LEFT:
|
|
54 break;
|
|
55 case DWT.CENTER:
|
|
56 x= (r.width - bounds.width) / 2;
|
|
57 break;
|
|
58 case DWT.RIGHT:
|
|
59 x= r.width - bounds.width;
|
|
60 break;
|
|
61 }
|
|
62
|
|
63 int y= 0;
|
|
64 switch (valign) {
|
|
65 case DWT.TOP: {
|
|
66 FontMetrics fontMetrics= gc.getFontMetrics();
|
|
67 y= (fontMetrics.getHeight() - bounds.height)/2;
|
|
68 break;
|
|
69 }
|
|
70 case DWT.CENTER:
|
|
71 y= (r.height - bounds.height) / 2;
|
|
72 break;
|
|
73 case DWT.BOTTOM: {
|
|
74 FontMetrics fontMetrics= gc.getFontMetrics();
|
|
75 y= r.height - (fontMetrics.getHeight() + bounds.height)/2;
|
|
76 break;
|
|
77 }
|
|
78 }
|
|
79
|
|
80 gc.drawImage(image, r.x+x, r.y+y);
|
|
81 }
|
|
82 }
|
|
83
|
|
84 /**
|
|
85 * Draws an image aligned inside the given rectangle on the given canvas.
|
|
86 *
|
|
87 * @param image the image to be drawn
|
|
88 * @param gc the drawing GC
|
|
89 * @param canvas the canvas on which to draw
|
|
90 * @param r the clipping rectangle
|
|
91 * @param align the alignment of the image to be drawn
|
|
92 */
|
|
93 public static void drawImage(Image image, GC gc, Canvas canvas, Rectangle r, int align) {
|
|
94 drawImage(image, gc, canvas, r, align, DWT.CENTER);
|
|
95 }
|
|
96 }
|