diff dwtx/jface/text/source/ImageUtilities.d @ 129:eb30df5ca28b

Added JFace Text sources
author Frank Benoit <benoit@tionex.de>
date Sat, 23 Aug 2008 19:10:48 +0200
parents
children c4fb132a086c
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dwtx/jface/text/source/ImageUtilities.d	Sat Aug 23 19:10:48 2008 +0200
@@ -0,0 +1,96 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2008 IBM Corporation 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 and implementation
+ * Port to the D programming language:
+ *     Frank Benoit <benoit@tionex.de>
+ *******************************************************************************/
+module dwtx.jface.text.source.ImageUtilities;
+
+import dwt.dwthelper.utils;
+
+import dwt.DWT;
+import dwt.graphics.FontMetrics;
+import dwt.graphics.GC;
+import dwt.graphics.Image;
+import dwt.graphics.Rectangle;
+import dwt.widgets.Canvas;
+
+/**
+ * Provides methods for drawing images onto a canvas.
+ * <p>
+ * This class is neither intended to be instantiated nor subclassed.
+ * </p>
+ *
+ * @since 3.0
+ * @noinstantiate This class is not intended to be instantiated by clients.
+ * @noextend This class is not intended to be subclassed by clients.
+ */
+public class ImageUtilities {
+
+    /**
+     * Draws an image aligned inside the given rectangle on the given canvas.
+     *
+     * @param image the image to be drawn
+     * @param gc the drawing GC
+     * @param canvas the canvas on which to draw
+     * @param r the clipping rectangle
+     * @param halign the horizontal alignment of the image to be drawn
+     * @param valign the vertical alignment of the image to be drawn
+     */
+    public static void drawImage(Image image, GC gc, Canvas canvas, Rectangle r, int halign, int valign) {
+        if (image !is null) {
+
+            Rectangle bounds= image.getBounds();
+
+            int x= 0;
+            switch(halign) {
+            case DWT.LEFT:
+                break;
+            case DWT.CENTER:
+                x= (r.width - bounds.width) / 2;
+                break;
+            case DWT.RIGHT:
+                x= r.width - bounds.width;
+                break;
+            }
+
+            int y= 0;
+            switch (valign) {
+            case DWT.TOP: {
+                FontMetrics fontMetrics= gc.getFontMetrics();
+                y= (fontMetrics.getHeight() - bounds.height)/2;
+                break;
+            }
+            case DWT.CENTER:
+                y= (r.height - bounds.height) / 2;
+                break;
+            case DWT.BOTTOM: {
+                FontMetrics fontMetrics= gc.getFontMetrics();
+                y= r.height - (fontMetrics.getHeight() + bounds.height)/2;
+                break;
+            }
+            }
+
+            gc.drawImage(image, r.x+x, r.y+y);
+        }
+    }
+
+    /**
+     * Draws an image aligned inside the given rectangle on the given canvas.
+     *
+     * @param image the image to be drawn
+     * @param gc the drawing GC
+     * @param canvas the canvas on which to draw
+     * @param r the clipping rectangle
+     * @param align the alignment of the image to be drawn
+     */
+    public static void drawImage(Image image, GC gc, Canvas canvas, Rectangle r, int align) {
+        drawImage(image, gc, canvas, r, align, DWT.CENTER);
+    }
+}