diff dwtx/draw2d/CheckBox.d @ 98:95307ad235d9

Added Draw2d code, still work in progress
author Frank Benoit <benoit@tionex.de>
date Sun, 03 Aug 2008 00:52:14 +0200
parents
children 2d6540440fe6
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dwtx/draw2d/CheckBox.d	Sun Aug 03 00:52:14 2008 +0200
@@ -0,0 +1,97 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2005 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.draw2d.CheckBox;
+
+import dwt.dwthelper.utils;
+import dwt.dwthelper.ByteArrayInputStream;
+
+import dwt.graphics.Image;
+import dwt.graphics.ImageData;
+import dwtx.draw2d.Toggle;
+import dwtx.draw2d.Label;
+import dwtx.draw2d.ChangeListener;
+import dwtx.draw2d.ChangeEvent;
+import dwtx.draw2d.ButtonModel;
+
+/**
+ * A Checkbox is a toggle figure which toggles between the checked and unchecked figures
+ * to simulate a check box. A check box contains a text label to represent it.
+ */
+public final class CheckBox
+    : Toggle
+{
+
+private Label label = null;
+
+static const Image
+    UNCHECKED,
+    CHECKED;
+
+static this(){
+    UNCHECKED = createImage( getImportData!("dwtx.draw2d.checkboxenabledoff.gif")); //$NON-NLS-1$
+    CHECKED = createImage( getImportData!("dwtx.draw2d.checkboxenabledon.gif")); //$NON-NLS-1$
+}
+
+private static Image createImage( ImportData importdata ) {
+    Image image = new Image(null, new ImageData(new ByteArrayInputStream( cast(byte[]) importdata.data)));
+    return image;
+}
+
+/**
+ * Constructs a CheckBox with no text.
+ *
+ * @since 2.0
+ */
+public this() {
+    this(""); //$NON-NLS-1$
+}
+
+/**
+ * Constructs a CheckBox with the passed text in its label.
+ * @param text The label text
+ * @since 2.0
+ */
+public this(String text) {
+    setContents(label = new Label(text, UNCHECKED));
+}
+
+/**
+ * Adjusts CheckBox's icon depending on selection status.
+ *
+ * @since 2.0
+ */
+protected void handleSelectionChanged() {
+    if (isSelected())
+        label.setIcon(CHECKED);
+    else
+        label.setIcon(UNCHECKED);
+}
+
+/**
+ * Initializes this Clickable by setting a default model and adding a clickable event
+ * handler for that model. Also adds a ChangeListener to update icon when  selection
+ * status changes.
+ *
+ * @since 2.0
+ */
+protected void init() {
+    super.init();
+    addChangeListener(new class() ChangeListener {
+        public void handleStateChanged(ChangeEvent changeEvent) {
+            if (changeEvent.getPropertyName().equals(ButtonModel.SELECTED_PROPERTY))
+                handleSelectionChanged();
+        }
+    });
+}
+
+}