changeset 56:f77382746524

Ports of control snippets 25,62 showing basic key/mouse event handling.
author Bill Baxter <bill@billbaxter.com>
date Wed, 09 Apr 2008 06:32:06 +0900
parents b0d5061ec21c
children d7e6c648cb57
files dsss.conf dwtsnippets/control/Snippet25.d dwtsnippets/control/Snippet62.d
diffstat 3 files changed, 230 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/dsss.conf	Mon Apr 07 11:02:46 2008 +0900
+++ b/dsss.conf	Wed Apr 09 06:32:06 2008 +0900
@@ -30,6 +30,8 @@
 [dwtexamples/helloworld/HelloWorld5.d]
 
 [dwtsnippets/button/Snippet293.d]
+[dwtsnippets/control/Snippet25.d]
+[dwtsnippets/control/Snippet62.d]
 [dwtsnippets/combo/Snippet26.d]
 [dwtsnippets/composite/Snippet9.d]
 [dwtsnippets/coolbar/Snippet20.d]
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dwtsnippets/control/Snippet25.d	Wed Apr 09 06:32:06 2008 +0900
@@ -0,0 +1,154 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2004 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:
+ *     Bill Baxter <bill@billbaxter.com>
+ *******************************************************************************/
+module dwtsnippets.control.Snippet25;
+ 
+/*
+ * Control example snippet: print key state, code and character
+ *
+ * For a list of all DWT example snippets see
+ * http://www.eclipse.org/swt/snippets/
+ * 
+ * @since 3.0
+ */
+import dwt.DWT;
+import dwt.widgets.Display;
+import dwt.widgets.Shell;
+import dwt.widgets.Listener;
+import dwt.widgets.Event;
+
+import tango.io.Stdout;
+import tango.text.convert.Format;
+
+static char[] stateMask (int stateMask) {
+	char[] string = "";
+	if ((stateMask & DWT.CTRL) != 0) string ~= " CTRL";
+	if ((stateMask & DWT.ALT) != 0) string ~= " ALT";
+	if ((stateMask & DWT.SHIFT) != 0) string ~= " SHIFT";
+	if ((stateMask & DWT.COMMAND) != 0) string ~= " COMMAND";
+	return string;
+}
+
+static char[] character (char character) {
+	switch (character) {
+    case 0: 		return "'\\0'";
+    case DWT.BS:	return "'\\b'";
+    case DWT.CR:	return "'\\r'";
+    case DWT.DEL:	return "DEL";
+    case DWT.ESC:	return "ESC";
+    case DWT.LF:	return "'\\n'";
+    case DWT.TAB:	return "'\\t'";
+    default:
+        return "'" ~ character ~"'";
+	}
+}
+
+static char[] keyCode (int keyCode) {
+	switch (keyCode) {
+		
+		/* Keyboard and Mouse Masks */
+    case DWT.ALT: 		return "ALT";
+    case DWT.SHIFT: 	return "SHIFT";
+    case DWT.CONTROL:	return "CONTROL";
+    case DWT.COMMAND:	return "COMMAND";
+			
+		/* Non-Numeric Keypad Keys */
+    case DWT.ARROW_UP:		return "ARROW_UP";
+    case DWT.ARROW_DOWN:	return "ARROW_DOWN";
+    case DWT.ARROW_LEFT:	return "ARROW_LEFT";
+    case DWT.ARROW_RIGHT:	return "ARROW_RIGHT";
+    case DWT.PAGE_UP:		return "PAGE_UP";
+    case DWT.PAGE_DOWN:		return "PAGE_DOWN";
+    case DWT.HOME:			return "HOME";
+    case DWT.END:			return "END";
+    case DWT.INSERT:		return "INSERT";
+
+		/* Virtual and Ascii Keys */
+    case DWT.BS:	return "BS";
+    case DWT.CR:	return "CR";		
+    case DWT.DEL:	return "DEL";
+    case DWT.ESC:	return "ESC";
+    case DWT.LF:	return "LF";
+    case DWT.TAB:	return "TAB";
+	
+		/* Functions Keys */
+    case DWT.F1:	return "F1";
+    case DWT.F2:	return "F2";
+    case DWT.F3:	return "F3";
+    case DWT.F4:	return "F4";
+    case DWT.F5:	return "F5";
+    case DWT.F6:	return "F6";
+    case DWT.F7:	return "F7";
+    case DWT.F8:	return "F8";
+    case DWT.F9:	return "F9";
+    case DWT.F10:	return "F10";
+    case DWT.F11:	return "F11";
+    case DWT.F12:	return "F12";
+    case DWT.F13:	return "F13";
+    case DWT.F14:	return "F14";
+    case DWT.F15:	return "F15";
+		
+		/* Numeric Keypad Keys */
+    case DWT.KEYPAD_ADD:		return "KEYPAD_ADD";
+    case DWT.KEYPAD_SUBTRACT:	return "KEYPAD_SUBTRACT";
+    case DWT.KEYPAD_MULTIPLY:	return "KEYPAD_MULTIPLY";
+    case DWT.KEYPAD_DIVIDE:		return "KEYPAD_DIVIDE";
+    case DWT.KEYPAD_DECIMAL:	return "KEYPAD_DECIMAL";
+    case DWT.KEYPAD_CR:			return "KEYPAD_CR";
+    case DWT.KEYPAD_0:			return "KEYPAD_0";
+    case DWT.KEYPAD_1:			return "KEYPAD_1";
+    case DWT.KEYPAD_2:			return "KEYPAD_2";
+    case DWT.KEYPAD_3:			return "KEYPAD_3";
+    case DWT.KEYPAD_4:			return "KEYPAD_4";
+    case DWT.KEYPAD_5:			return "KEYPAD_5";
+    case DWT.KEYPAD_6:			return "KEYPAD_6";
+    case DWT.KEYPAD_7:			return "KEYPAD_7";
+    case DWT.KEYPAD_8:			return "KEYPAD_8";
+    case DWT.KEYPAD_9:			return "KEYPAD_9";
+    case DWT.KEYPAD_EQUAL:		return "KEYPAD_EQUAL";
+
+		/* Other keys */
+    case DWT.CAPS_LOCK:		return "CAPS_LOCK";
+    case DWT.NUM_LOCK:		return "NUM_LOCK";
+    case DWT.SCROLL_LOCK:	return "SCROLL_LOCK";
+    case DWT.PAUSE:			return "PAUSE";
+    case DWT.BREAK:			return "BREAK";
+    case DWT.PRINT_SCREEN:	return "PRINT_SCREEN";
+    case DWT.HELP:			return "HELP";
+
+    default:
+        return character (cast(char) keyCode);
+	}
+
+}
+
+void main () {
+	Display display = new Display ();
+	Shell shell = new Shell (display);
+	Listener listener = new class Listener {
+		public void handleEvent (Event e) {
+			char[] string = e.type == DWT.KeyDown ? "DOWN:" : "UP  :";
+			string ~= Format("stateMask=0x{:x}{},", e.stateMask, stateMask (e.stateMask));
+			string ~= Format(" keyCode=0x{:x} {},", e.keyCode, keyCode (e.keyCode));
+			string ~= Format(" character=0x{:x} {}", e.character, character (e.character));
+			Stdout.formatln (string);
+		}
+	};
+	shell.addListener (DWT.KeyDown, listener);
+	shell.addListener (DWT.KeyUp, listener);
+	shell.setSize (200, 200);
+	shell.open ();
+	while (!shell.isDisposed ()) {
+		if (!display.readAndDispatch ()) display.sleep ();
+	}
+	display.dispose ();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dwtsnippets/control/Snippet62.d	Wed Apr 09 06:32:06 2008 +0900
@@ -0,0 +1,74 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2004 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:
+ *     Bill Baxter <bill@billbaxter.com>
+ *******************************************************************************/
+ module dwtsnippets.control.Snippet62;
+
+ /*
+  * Control example snippet: print mouse state and button (down, move, up)
+  *
+  * For a list of all SWT example snippets see
+  * http://www.eclipse.org/swt/snippets/
+  * 
+  * @since 3.1
+  */
+import dwt.DWT;
+import dwt.widgets.Display;
+import dwt.widgets.Shell;
+import dwt.widgets.Listener;
+import dwt.widgets.Event;
+
+import tango.io.Stdout;
+import tango.util.Convert;
+
+static char[] stateMask (int stateMask) {
+    char[] str = "";
+    if ((stateMask & DWT.CTRL) != 0) str ~= " CTRL";
+    if ((stateMask & DWT.ALT) != 0) str ~= " ALT";
+    if ((stateMask & DWT.SHIFT) != 0) str ~= " SHIFT";
+    if ((stateMask & DWT.COMMAND) != 0) str ~= " COMMAND";
+    return str;
+}
+
+void main () {
+    Display display = new Display ();
+    final Shell shell = new Shell (display);
+    Listener listener = new class Listener {
+        public void handleEvent (Event e) {
+            char[] str = "Unknown";
+            switch (e.type) {
+            case DWT.MouseDown: str = "DOWN"; break;
+            case DWT.MouseMove: str = "MOVE"; break;
+            case DWT.MouseUp: str = "UP"; break;
+            }
+            str ~=": button: " ~ to!(char[])(e.button) ~ ", ";
+            str ~= "stateMask=0x" ~ to!(char[])(e.stateMask) 
+            ~ stateMask (e.stateMask) 
+            ~ ", x=" ~ to!(char[])(e.x) ~ ", y=" ~ to!(char[])(e.y);
+            if ((e.stateMask & DWT.BUTTON1) != 0) str ~= " BUTTON1";
+            if ((e.stateMask & DWT.BUTTON2) != 0) str ~= " BUTTON2";
+            if ((e.stateMask & DWT.BUTTON3) != 0) str ~= " BUTTON3";
+            if ((e.stateMask & DWT.BUTTON4) != 0) str ~= " BUTTON4";
+            if ((e.stateMask & DWT.BUTTON5) != 0) str ~= " BUTTON5";
+            Stdout.formatln (str);
+        }
+    };
+    shell.addListener (DWT.MouseDown, listener);
+    shell.addListener (DWT.MouseMove, listener);
+    shell.addListener (DWT.MouseUp, listener);
+    shell.pack ();
+    shell.open ();
+    while (!shell.isDisposed ()) {
+        if (!display.readAndDispatch ()) display.sleep ();
+    }
+    display.dispose ();
+}
+