changeset 42:a57305009edb

region_shell
author Frank Benoit <benoit@tionex.de>
date Fri, 04 Apr 2008 08:06:10 +0200
parents a17899018eb6
children bc5301dd9679
files dsss.conf dwtsnippets/images/region_shell.gif user/region_shell.d
diffstat 3 files changed, 150 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/dsss.conf	Mon Mar 17 00:47:56 2008 +0100
+++ b/dsss.conf	Fri Apr 04 08:06:10 2008 +0200
@@ -53,6 +53,7 @@
 [user/dragdrop/texttolabel.d]
 [user/drawingboard/DrawingBoard.d]
 [user/torhu_synctest.d]
+[user/region_shell.d]
 
 version(DwtAddons){
     [dwtexamples/sleak/SleakExample.d]
Binary file dwtsnippets/images/region_shell.gif has changed
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/user/region_shell.d	Fri Apr 04 08:06:10 2008 +0200
@@ -0,0 +1,149 @@
+module user.region_shell;// image attached: region.gif
+import dwt.DWT;
+
+import dwt.widgets.Display;
+import dwt.widgets.Shell;
+import dwt.widgets.Button;
+import dwt.widgets.Listener;
+import dwt.widgets.Event;
+
+import dwt.layout.GridLayout;
+
+import dwt.dwthelper.ByteArrayInputStream;
+
+import dwt.graphics.Image;
+import dwt.graphics.ImageData;
+import dwt.graphics.Region;
+import dwt.graphics.Point;
+import dwt.graphics.Rectangle;
+
+import tango.io.Stdout;
+
+class RegionShell: Shell
+{
+    this(Display display, int style, Image image, bool moveable = true)
+    {
+        super(display, style);
+        
+        setShellRegion(image, moveable);
+    }
+    
+    this(Display display, Image image, bool moveable = true)
+    {
+        this(display, DWT.NO_TRIM, image, moveable);
+    }
+    
+    private void setShellRegion(Image image, bool moveable)
+    {
+        Region region = new Region();
+        ImageData imageData = image.getImageData();
+        
+        if (imageData.alphaData != null)
+        {
+            Rectangle pixel = new Rectangle(0, 0, 1, 1);
+            
+            for (int y = 0; y < imageData.height; y++)
+            {
+                for (int x = 0; x < imageData.width; x++)
+                {
+                    if (imageData.getAlpha(x, y) == 255)
+                    {
+                        pixel.x = imageData.x + x;
+                        pixel.y = imageData.y + y;
+                        
+                        region.add(pixel);
+                    }
+                }
+            }
+        }
+        else
+        {
+            ImageData mask = imageData.getTransparencyMask();
+            Rectangle pixel = new Rectangle(0, 0, 1, 1);
+            for (int y = 0; y < mask.height; y++)
+            {
+                for (int x = 0; x < mask.width; x++)
+                {
+                    if (mask.getPixel(x, y) != 0)
+                    {
+                        pixel.x = imageData.x + x;
+                        pixel.y = imageData.y + y;
+                        
+                        region.add(pixel);
+                    }
+                }
+            }
+        }
+        
+        this.setRegion(region);
+        this.setSize(imageData.x + imageData.width, imageData.y + imageData.height);
+        
+        if (moveable)
+        {
+            setMoveable();
+        }
+    }
+    
+    private Shell getCurrent()
+    {
+        return this;
+    }
+    
+    private void setMoveable()
+    {
+        Listener l = new class Listener {
+            Point origin;
+            
+            public void handleEvent(Event e) {
+                switch (e.type) {
+                    case DWT.MouseDown:
+                        origin = new Point(e.x, e.y);
+                        break;
+                    case DWT.MouseUp:
+                        origin = null;
+                        break;
+                    case DWT.MouseMove:
+                        if (origin !is null) {
+                            Point p = display.map(getCurrent(), null, e.x, e.y);
+                            getCurrent.setLocation(p.x - origin.x, p.y - origin.y);
+                        }
+                        break;
+                }
+            }
+        };
+        
+        this.addListener(DWT.MouseDown, l);
+        this.addListener(DWT.MouseUp, l);
+        this.addListener(DWT.MouseMove, l);
+    }
+}
+
+void main()
+{
+    Display display = new Display();
+    Image image = new Image(display, new ImageData(new ByteArrayInputStream(cast(byte[]) import("region_shell.gif"))));
+    RegionShell shell = new RegionShell(display, image);
+    
+    Button closeBtn = new Button(shell, DWT.PUSH);
+    closeBtn.setText("Close");
+    closeBtn.addListener(DWT.Selection, new class Listener {
+        public void handleEvent(Event e) {
+            shell.close();
+        }
+    });
+    
+    shell.setLayout(new GridLayout());
+    shell.setBackgroundImage(image);
+    shell.open();
+    
+    while (!shell.isDisposed())
+    {
+        if (!display.readAndDispatch())
+        {
+            display.sleep();
+        }
+    }
+    
+    image.dispose();
+    display.dispose();
+}