changeset 150:297120c376f7

Add ole/Snippet81 and styledtext/Snippet212, thanks to yidabu and Enzo Petrelli
author Frank Benoit <benoit@tionex.de>
date Wed, 20 Aug 2008 19:54:39 +0200
parents 80f47186dc48
children 47a861d0d0f2
files snippets/dsss.conf snippets/ole/Snippet81.d snippets/styledtext/Snippet212.d
diffstat 3 files changed, 445 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/snippets/dsss.conf	Sat Aug 16 22:24:25 2008 +0200
+++ b/snippets/dsss.conf	Wed Aug 20 19:54:39 2008 +0200
@@ -95,6 +95,7 @@
 [spinner/Snippet190.d]
 [styledtext/Snippet163.d]
 [styledtext/Snippet189.d]
+[styledtext/Snippet212.d]
 [tabfolder/Snippet76.d]
 [table/Snippet144.d]
 [table/Snippet38.d]
@@ -116,6 +117,10 @@
 [tree/Snippet8.d]
 [treeeditor/Snippet111.d]
 
+version(Windows){
+    [ole/Snippet81.d]
+}
+
 version(Derelict){
     [opengl]
     type=subdir
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/snippets/ole/Snippet81.d	Wed Aug 20 19:54:39 2008 +0200
@@ -0,0 +1,237 @@
+/*******************************************************************************
+ * 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:
+ *     Enzo Petrelli
+ *******************************************************************************/
+module ole.Snippet81;
+
+/*
+ * OLE and ActiveX example snippet: browse the typelibinfo for a program id (win32 only)
+ * NOTE: This snippet uses internal SWT packages that are
+ * subject to change without notice.
+ *
+ * For a list of all SWT example snippets see
+ * http://www.eclipse.org/swt/snippets/
+ */
+
+bug
+{
+    pragma(lib, "DD-dwt-dbg.lib");
+}
+else
+{
+    pragma(lib, "DD-dwt.lib");
+}
+pragma(lib, "advapi32.lib");
+pragma(lib, "comctl32.lib");
+pragma(lib, "comdlg32.lib");
+pragma(lib, "gdi32.lib");
+pragma(lib, "gdiplus.lib");
+pragma(lib, "glu32.lib");
+pragma(lib, "imm32.lib");
+pragma(lib, "kernel32.lib");
+pragma(lib, "msimg32.lib");
+pragma(lib, "ole32.lib");
+pragma(lib, "oleacc.lib");
+pragma(lib, "oleaut32.lib");
+pragma(lib, "olepro32.lib");
+pragma(lib, "opengl32.lib");
+pragma(lib, "shlwapi.lib");
+pragma(lib, "user32.lib");
+pragma(lib, "usp10.lib");
+pragma(lib, "uxtheme.lib");
+
+import dwt.DWT;
+import dwt.widgets.Display;
+import dwt.widgets.Shell;
+
+import dwt.dwthelper.utils;             // String
+import dwt.internal.ole.win32.OAIDL;
+import dwt.ole.win32.OLE;
+import dwt.ole.win32.OleAutomation;
+import dwt.ole.win32.OleControlSite;
+import dwt.ole.win32.OleFrame;
+import dwt.ole.win32.OleFunctionDescription;
+import dwt.ole.win32.OlePropertyDescription;
+
+import std.cstream;
+import std.path;
+import std.stream;
+
+alias char[] string;
+
+int main(string[] asArg)
+{
+    int iRes = 0;
+
+    try
+    {
+    //  String progID = "Shell.Explorer";
+        String progID = "Excel.Application";
+        
+        Display oDisplay = new Display();
+        Shell oShell = new Shell(oDisplay);
+    
+        OleFrame frame = new OleFrame(oShell, DWT.NONE);
+        OleControlSite oOleSite = null;
+        OleAutomation oOleAutoObj = null;
+        try
+        {
+            oOleSite = new OleControlSite(frame, DWT.NONE, progID); 
+        } catch (Exception oExc)
+        {
+            dout.writefln("Exception %s creating OleControlSite on type library for %s", oExc.toString(), progID);
+            return 1;
+        }
+        try
+        {
+            oOleAutoObj = new OleAutomation(oOleSite);
+        } catch (Exception oExc)
+        {
+            dout.writefln("Exception %s  OleAutomation on type library for %s", oExc.toString(), progID);
+            return 1;
+        }
+        
+        dout.writefln("TypeLibrary for: '%s'", progID);
+        printTypeInfo(oOleAutoObj, dout);
+        
+        oShell.dispose();
+        oDisplay.dispose();
+    } catch (Exception oExc)
+    {
+        dout.writefln("Exception %s in main()", oExc.toString());
+        iRes = 1;
+    }
+    
+    return iRes;
+}
+
+private static void printTypeInfo(OleAutomation oOleAutoObj, OutputStream oOut)
+{
+    dwt.internal.ole.win32.OAIDL.TYPEATTR * pTypeAttr = oOleAutoObj.getTypeInfoAttributes();
+    if (pTypeAttr !is null)
+    {       
+        if (pTypeAttr.cFuncs > 0)
+        {
+            oOut.writefln("Functions :");
+            for (int iIdx = 0; iIdx < pTypeAttr.cFuncs; ++iIdx)
+            {           
+                OleFunctionDescription oData = oOleAutoObj.getFunctionDescription(iIdx);
+                string sArgList = "";
+                int iFirstOptionalArgIndex = oData.args.length - oData.optionalArgCount;
+                for (int iArg = 0; iArg < oData.args.length; ++iArg)
+                {
+                    sArgList ~= "[";
+                    if (iArg >= iFirstOptionalArgIndex)
+                        sArgList ~= "optional, ";
+                    sArgList ~= getDirection(oData.args[iArg].flags) ~ "] " ~
+                        getTypeName(oData.args[iArg].type) ~ " " ~
+                        oData.args[iArg].name
+                        ;
+                    if (iArg < oData.args.length - 1)
+                        sArgList ~= ", ";
+                }
+                oOut.writefln("%s (id = %d (0x%08X))", getInvokeKind(oData.invokeKind), oData.id, oData.id);
+                oOut.writefln("\tSignature  : %s %s(%s)", getTypeName(oData.returnType), oData.name, sArgList);
+                oOut.writefln("\tDescription: %s", oData.documentation);
+                oOut.writefln("\tHelp File  : %s", oData.helpFile);
+                oOut.writefln();
+            }
+        }
+        
+        if (pTypeAttr.cVars > 0)
+        {
+            oOut.writefln("\n\nVariables :");
+            for (int iIdx = 0; iIdx < pTypeAttr.cVars; ++iIdx)
+            {
+                OlePropertyDescription oData = oOleAutoObj.getPropertyDescription(iIdx);
+                oOut.writefln("PROPERTY (id = %d (0x%08X)", oData.id, oData.id);
+                oOut.writefln("\tName : %s", oData.name);
+                oOut.writefln("\tType : %s", getTypeName(oData.type));
+                oOut.writefln();
+            }
+        }
+    }
+}
+private static string getTypeName(int iType)
+{
+    int iBase = iType & ~OLE.VT_BYREF;
+    String sDsc = null;
+    switch (iBase)
+    {
+        case OLE.VT_BOOL :          sDsc = "boolean"; break;
+        case OLE.VT_R4 :            sDsc = "float"; break;
+        case OLE.VT_R8 :            sDsc = "double"; break;
+        case OLE.VT_I4 :            sDsc = "int"; break;
+        case OLE.VT_DISPATCH :      sDsc = "IDispatch"; break;
+        case OLE.VT_UNKNOWN :       sDsc = "IUnknown"; break;
+        case OLE.VT_I2 :            sDsc = "short"; break;
+        case OLE.VT_BSTR :          sDsc = "String"; break;
+        case OLE.VT_VARIANT :       sDsc = "Variant"; break;
+        case OLE.VT_CY :            sDsc = "Currency"; break;
+        case OLE.VT_DATE :          sDsc = "Date"; break;
+        case OLE.VT_UI1 :           sDsc = "unsigned char"; break;
+        case OLE.VT_UI4 :           sDsc = "unsigned int"; break;
+        case OLE.VT_USERDEFINED :   sDsc = "UserDefined"; break;
+        case OLE.VT_HRESULT :       sDsc = "HRESULT"; break;
+        case OLE.VT_VOID :          sDsc = "void"; break;
+        
+        case OLE.VT_UI2 :           sDsc = "unsigned short"; break;
+        case OLE.VT_UINT :          sDsc = "unsigned int"; break;
+        case OLE.VT_PTR :           sDsc = "void *"; break;
+        default: break;
+    }
+    if (sDsc !is null)
+    {
+        if ((iType & OLE.VT_BYREF) == OLE.VT_BYREF)
+            return sDsc ~ " *";
+        return sDsc;
+    }
+    return std.string.format("unknown %d (0x%04X)", iType, iType);
+}
+private static string getDirection(int bDirection)
+{
+    string sDirString = "";
+    bool bComma = false;
+    if ((bDirection & OLE.IDLFLAG_FIN) != 0)
+    {
+        sDirString ~= "in";
+        bComma = true;
+    }
+    if ((bDirection & OLE.IDLFLAG_FOUT) != 0)
+    {
+        if (bComma) sDirString ~= ", ";
+        sDirString ~= "out";
+        bComma = true;
+    }
+    if ((bDirection & OLE.IDLFLAG_FLCID) != 0)
+    {
+        if (bComma) sDirString ~= ", ";
+        sDirString ~= "lcid";
+        bComma = true;
+    }
+    if ((bDirection & OLE.IDLFLAG_FRETVAL) != 0)
+    {
+        if (bComma) sDirString ~= ", "; 
+        sDirString ~= "retval";
+    }
+    return sDirString;
+}
+private static string getInvokeKind(int iInvKind) {
+    switch (iInvKind)
+    {
+        case OLE.INVOKE_FUNC : return "METHOD";
+        case OLE.INVOKE_PROPERTYGET : return "PROPERTY GET";
+        case OLE.INVOKE_PROPERTYPUT : return "PROPERTY PUT";
+        case OLE.INVOKE_PROPERTYPUTREF : return "PROPERTY PUT BY REF";
+        default: break;
+    }
+    return std.string.format("unknown %d", iInvKind);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/snippets/styledtext/Snippet212.d	Wed Aug 20 19:54:39 2008 +0200
@@ -0,0 +1,203 @@
+/*******************************************************************************
+ * 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:
+ *     yidabu at gmail dot com  ( D China http://www.d-programming-language-china.org/ )
+ *******************************************************************************/
+/*
+Unhandled Exception: EXCEPTION_ACCESS_VIOLATION(0xc0000005) at ntdll.dll (0x7c90316c) thread(1480)
+->us
+#0 ?? () at dwt\graphics\TextLayout.d:2915 from ntdll.dll
+#1 0x77f205bf in ?? () at dwt\graphics\TextLayout.d:2915 from GDI32.dll
+#2 0x00483c72 in  dwt.graphics.TextLayout.TextLayout.shape () at dwt\graphics\TextLayout.d:2915
+#3 0x0047bfff in  dwt.graphics.TextLayout.TextLayout.computeRuns () at dwt\graphics\TextLayout.d:267
+#4 0x00480474 in  dwt.graphics.Rectangle.Rectangle dwt.graphics.TextLayout.TextLayout.getBounds () at dwt\graphics\TextLayout.d:1387
+#5 0x00411ea7 in  dwt.graphics.Point.Point dwt.custom.StyledText.StyledText.computeSize () at dwt\custom\StyledText.d:1784
+#6 0x0041f24b in  dwt.layout.GridData.GridData.computeSize () at dwt\layout\GridData.d:484
+#7 0x0043258f in  dwt.graphics.Point.Point dwt.layout.GridLayout.GridLayout.layout () at dwt\layout\GridLayout.d:232
+#8 0x00432325 in  dwt.layout.GridLayout.GridLayout.layout () at dwt\layout\GridLayout.d:208
+#9 0x004999af in  dwt.widgets.Composite.Composite.updateLayout () at dwt\widgets\Composite.d:1170
+#10 0x00498eba in  dwt.widgets.Composite.Composite.sendResize () at dwt\widgets\Composite.d:879
+#11 0x00499053 in  dwt.widgets.Composite.Composite.setBounds () at dwt\widgets\Composite.d:924
+#12 0x004d959d in  dwt.widgets.Decorations.Decorations.setBounds () at dwt\widgets\Decorations.d:888
+#13 0x0042f9a2 in  dwt.widgets.Shell.Shell.setBounds () at dwt\widgets\Shell.d:1460
+#14 0x004733f3 in  dwt.widgets.Control.Control.setBounds () at dwt\widgets\Control.d:2639
+#15 0x00473d27 in  dwt.widgets.Control.Control.setSize () at dwt\widgets\Control.d:3153
+#16 0x004042c8 in _Dmain () at Snippet212.d:171
+#17 0x00645778 in extern (C) int dmain2.main(int, char**) . void runMain(void*) () from dmain2
+#18 0x006457af in extern (C) int dmain2.main(int, char**) . void runAll(void*) () from dmain2
+#19 0x006454f0 in _main () from dmain2
+#20 0x0065f801 in _mainCRTStartup () from constart
+#21 0x7c816fd7 in ?? () from KERNEL32.dll
+*/
+module styledtext.Snippet212;
+/**
+ * StyledText snippet: embed images
+ *
+ * For a list of all SWT example snippets see
+ * http://www.eclipse.org/swt/snippets/
+ * 
+ * @since 3.2
+ */
+
+import dwt.DWT;
+import dwt.custom.StyledText;
+import dwt.custom.StyleRange;
+import dwt.layout.GridLayout;
+import dwt.layout.GridData;
+import dwt.widgets.Display;
+import dwt.widgets.Shell;
+import dwt.widgets.Button;
+import dwt.widgets.FileDialog;
+import dwt.widgets.Event;
+import dwt.widgets.Listener;
+import dwt.custom.PaintObjectEvent;
+import dwt.custom.PaintObjectListener;
+import dwt.events.VerifyEvent;
+import dwt.events.VerifyListener;
+import dwt.graphics.FontData;
+import dwt.graphics.Font;
+import dwt.graphics.Rectangle;
+import dwt.graphics.GC;
+import dwt.graphics.Image;
+import dwt.graphics.GlyphMetrics;
+import dwt.dwthelper.utils;
+import dwt.dwthelper.System;
+
+void main() {
+    static StyledText styledText;
+    static String text = 
+        "This snippet shows how to embed images in a StyledText.\n"
+        "Here is one: \uFFFC, and here is another: \uFFFC."
+        "Use the add button to add an image from your filesystem to the StyledText at the current caret offset.";
+    static Image[] images;
+    static int[] offsets;
+
+    static void addImage(Image image, int offset) {
+        StyleRange style = new StyleRange ();
+        style.start = offset;
+        style.length = 1;
+        Rectangle rect = image.getBounds();
+        style.metrics = new GlyphMetrics(rect.height, 0, rect.width);
+        styledText.setStyleRange(style);        
+    }
+
+    Display display = new Display();
+    Shell shell = new Shell(display);
+    shell.setLayout(new GridLayout());
+    styledText = new StyledText(shell, DWT.WRAP | DWT.BORDER);
+    styledText.setLayoutData(new GridData(DWT.FILL, DWT.FILL, true, true));
+    styledText.setText(text);
+    images = [
+        display.getSystemImage(DWT.ICON_QUESTION),
+        display.getSystemImage(DWT.ICON_INFORMATION)
+    ];
+    offsets = new int[images.length];
+    int lastOffset = 0;
+    for (int i = 0; i < images.length; i++) {
+        int offset = text.indexOf("\uFFFC", lastOffset);
+        offsets[i] = offset;
+        addImage(images[i], offset);
+        lastOffset = offset + 1;
+    }
+    
+    void onVerify(Event e) {
+        int start = e.start;
+        int replaceCharCount = e.end - e.start;
+        int newCharCount = e.text.length;
+        for (int i = 0; i < offsets.length; i++) {
+            int offset = offsets[i];
+            if (start <= offset && offset < start + replaceCharCount) {
+                // this image is being deleted from the text
+                if (images[i] !is null && !images[i].isDisposed()) {
+                    images[i].dispose();
+                    images[i] = null;
+                }
+                offset = -1;
+            }
+            if (offset != -1 && offset >= start) offset += newCharCount - replaceCharCount;
+            offsets[i] = offset;
+        }
+    }    
+    // use a verify listener to keep the offsets up to date
+    styledText.addListener(DWT.Verify, dgListener(&onVerify));
+
+    styledText.addPaintObjectListener(new class(images, offsets) PaintObjectListener {
+        Image[] images;
+        int[] offsets;
+        this( Image[] images_, int[] offsets_) {
+            this.images = images_;
+            this.offsets = offsets_;
+        }
+        public void paintObject(PaintObjectEvent event) {
+            GC gc = event.gc;
+            StyleRange style = event.style;
+            int start = style.start;
+            for (int i = 0; i < offsets.length; i++) {
+                int offset = offsets[i];
+                if (start == offset) {
+                    Image image = images[i];
+                    int x = event.x;
+                    int y = event.y + event.ascent - style.metrics.ascent;						
+                    gc.drawImage(image, x, y);
+                }
+            }
+        }
+    });    
+    
+    Button button = new Button (shell, DWT.PUSH);
+    button.setText("Add Image");
+    button.setLayoutData(new GridData(DWT.CENTER, DWT.CENTER, false, false));
+    
+    void onSelection(Event e) {
+       FileDialog dialog = new FileDialog(shell);
+        String filename = dialog.open();
+        if (filename !is null) {
+            try {
+                Image image = new Image(display, filename);
+                int offset = styledText.getCaretOffset();
+                styledText.replaceTextRange(offset, 0, "\uFFFC");
+                int index = 0;
+                while (index < offsets.length) {
+                    if (offsets[index] == -1 && images[index] is null) break;
+                    index++;
+                }
+                if (index == offsets.length) {
+                    int[] tmpOffsets = new int[index + 1];
+                    System.arraycopy(offsets, 0, tmpOffsets, 0, offsets.length);
+                    offsets = tmpOffsets;
+                    Image[] tmpImages = new Image[index + 1];
+                    System.arraycopy(images, 0, tmpImages, 0, images.length);
+                    images = tmpImages;
+                }
+                offsets[index] = offset;
+                images[index] = image;
+                addImage(image, offset);
+            } catch (Exception e) {
+                ExceptionPrintStackTrace(e);
+            }
+        }	        
+    }
+    button.addListener(DWT.Selection, dgListener(&onSelection));
+    shell.setSize(400, 400);
+    shell.open();
+    while (!shell.isDisposed()) {
+        if (!display.readAndDispatch())
+            display.sleep();
+    }
+    for (int i = 0; i < images.length; i++) {
+        Image image = images[i];
+        if (image !is null && !image.isDisposed()) {
+            image.dispose();
+        }
+    }
+    display.dispose();
+
+}
+