1 #!/usr/bin/env dub
2 /+
3 dub.sdl:
4     name "snippet81"
5     dependency "dwt" path="../../../../../../"
6     libs \
7       "atk-1.0" \
8       "cairo" \
9       "dl" \
10       "fontconfig" \
11       "gdk-x11-2.0" \
12       "gdk_pixbuf-2.0" \
13       "glib-2.0" \
14       "gmodule-2.0" \
15       "gnomeui-2" \
16       "gnomevfs-2" \
17       "gobject-2.0" \
18       "gthread-2.0" \
19       "gtk-x11-2.0" \
20       "pango-1.0" \
21       "pangocairo-1.0" \
22       "X11" \
23       "Xcomposite" \
24       "Xcursor" \
25       "Xdamage" \
26       "Xext" \
27       "Xfixes" \
28       "Xi" \
29       "Xinerama" \
30       "Xrandr" \
31       "Xrender" \
32       "Xtst" \
33       platform="linux"
34 +/
35 
36 /*******************************************************************************
37  * Copyright (c) 2000, 2004 IBM Corporation and others.
38  * All rights reserved. This program and the accompanying materials
39  * are made available under the terms of the Eclipse Public License v1.0
40  * which accompanies this distribution, and is available at
41  * http://www.eclipse.org/legal/epl-v10.html
42  *
43  * Contributors:
44  *     IBM Corporation - initial API and implementation
45  * Port to the D programming language:
46  *     Enzo Petrelli
47  *******************************************************************************/
48 module org.eclipse.swt.snippets.Snippet81;
49 
50 /*
51  * OLE and ActiveX example snippet: browse the typelibinfo for a program id (win32 only)
52  * NOTE: This snippet uses internal SWT packages that are
53  * subject to change without notice.
54  *
55  * For a list of all SWT example snippets see
56  * http://www.eclipse.org/swt/snippets/
57  */
58 
59 import org.eclipse.swt.SWT;
60 import org.eclipse.swt.widgets.Display;
61 import org.eclipse.swt.widgets.Shell;
62 
63 import java.lang.all;             // String
64 import org.eclipse.swt.internal.ole.win32.OAIDL;
65 import org.eclipse.swt.ole.win32.OLE;
66 import org.eclipse.swt.ole.win32.OleAutomation;
67 import org.eclipse.swt.ole.win32.OleControlSite;
68 import org.eclipse.swt.ole.win32.OleFrame;
69 import org.eclipse.swt.ole.win32.OleFunctionDescription;
70 import org.eclipse.swt.ole.win32.OlePropertyDescription;
71 
72 version(Tango){
73     import tango.io.Stdout;
74     import tango.io.stream.Format;
75     import tango.text.convert.Format;
76 } else { // Phobos
77     import std.stdio;
78     import std..string;
79     class FormatOutput(T) {
80         alias writefln formatln;
81     }
82 }
83 
84 int main() {
85     int iRes = 0;
86 
87     String progID = "Shell.Explorer";
88     //String progID = "Excel.Application";
89 
90     Display oDisplay = new Display();
91     Shell oShell = new Shell(oDisplay);
92 
93     OleFrame frame = new OleFrame(oShell, SWT.NONE);
94     OleControlSite oOleSite = null;
95     OleAutomation oOleAutoObj = null;
96     try {
97         oOleSite = new OleControlSite(frame, SWT.NONE, progID);
98     }
99     catch (Exception oExc) {
100         version(Tango){
101             Stdout.formatln("Exception {} creating OleControlSite on type library for {}", oExc.toString(), progID);
102         } else { // Phobos
103             writefln("Exception %s creating OleControlSite on type library for %s", oExc.toString(), progID);
104         }
105         return 1;
106     }
107     try {
108         oOleAutoObj = new OleAutomation(oOleSite);
109     }
110     catch (Exception oExc) {
111         version(Tango){
112             Stdout.formatln("Exception {}  OleAutomation on type library for {}", oExc.toString(), progID);
113         } else { // Phobos
114             writefln("Exception %s  OleAutomation on type library for %s", oExc.toString(), progID);
115         }
116         return 1;
117     }
118 
119     version(Tango){
120         Stdout.formatln("TypeLibrary for: '{}'", progID);
121         printTypeInfo(oOleAutoObj, Stdout);
122     } else {
123         writefln("TypeLibrary for: '%s'", progID);
124         printTypeInfo(oOleAutoObj, new FormatOutput!(char));
125     }
126 
127     oShell.dispose();
128     oDisplay.dispose();
129     return iRes;
130 }
131 
132 void printTypeInfo(OleAutomation oOleAutoObj, FormatOutput!(char) oOut)
133 {
134     org.eclipse.swt.internal.ole.win32.OAIDL.TYPEATTR * pTypeAttr = oOleAutoObj.getTypeInfoAttributes();
135     if (pTypeAttr !is null) {
136         if (pTypeAttr.cFuncs > 0) {
137             oOut.formatln("Functions :");
138             for (int iIdx = 0; iIdx < pTypeAttr.cFuncs; ++iIdx) {
139                 OleFunctionDescription oData = oOleAutoObj.getFunctionDescription(iIdx);
140                 String sArgList = "";
141                 int iFirstOptionalArgIndex = cast(int) oData.args.length - oData.optionalArgCount;
142                 for (int iArg = 0; iArg < oData.args.length; ++iArg) {
143                     sArgList ~= "[";
144                     if (iArg >= iFirstOptionalArgIndex){
145                         sArgList ~= "optional, ";
146                     }
147                     sArgList ~= getDirection(oData.args[iArg].flags) ~ "] " ~
148                         getTypeName(oData.args[iArg].type) ~ " " ~
149                         oData.args[iArg].name
150                         ;
151                     if (iArg < oData.args.length - 1){
152                         sArgList ~= ", ";
153                     }
154                 }
155                 version(Tango){
156                     oOut.formatln("{} (id = {} (0x{:X8}))", getInvokeKind(oData.invokeKind), oData.id, oData.id);
157                     oOut.formatln("\tSignature  : {} {}({})", getTypeName(oData.returnType), oData.name, sArgList);
158                     oOut.formatln("\tDescription: {}", oData.documentation);
159                     oOut.formatln("\tHelp File  : {}", oData.helpFile);
160                     oOut.formatln("");
161                 } else { // Phobos
162                     oOut.formatln("%s (id = %s (0x%8X))", getInvokeKind(oData.invokeKind), oData.id, oData.id);
163                     oOut.formatln("\tSignature  : %s %s(%s)", getTypeName(oData.returnType), oData.name, sArgList);
164                     oOut.formatln("\tDescription: %s", oData.documentation);
165                     oOut.formatln("\tHelp File  : %s", oData.helpFile);
166                     oOut.formatln("");
167                 }
168             }
169         }
170 
171         if (pTypeAttr.cVars > 0) {
172             version(Tango){
173                 oOut.formatln("\n\nVariables :");
174                 for (int iIdx = 0; iIdx < pTypeAttr.cVars; ++iIdx) {
175                     OlePropertyDescription oData = oOleAutoObj.getPropertyDescription(iIdx);
176                     oOut.formatln("PROPERTY (id = {} (0x{:X8})", oData.id, oData.id);
177                     oOut.formatln("\tName : {}", oData.name);
178                     oOut.formatln("\tType : {}", getTypeName(oData.type));
179                     oOut.formatln("");
180                 }
181             } else { // Phobos
182                 oOut.formatln("\n\nVariables :");
183                 for (int iIdx = 0; iIdx < pTypeAttr.cVars; ++iIdx) {
184                     OlePropertyDescription oData = oOleAutoObj.getPropertyDescription(iIdx);
185                     oOut.formatln("PROPERTY (id = %s (0x%8X)", oData.id, oData.id);
186                     oOut.formatln("\tName : %s", oData.name);
187                     oOut.formatln("\tType : %s", getTypeName(oData.type));
188                     oOut.formatln("");
189                 }
190             }
191         }
192     }
193 }
194 
195 String getTypeName(int iType) {
196     int iBase = iType & ~OLE.VT_BYREF;
197     String sDsc = null;
198     switch (iBase) {
199         case OLE.VT_BOOL :          sDsc = "boolean"; break;
200         case OLE.VT_R4 :            sDsc = "float"; break;
201         case OLE.VT_R8 :            sDsc = "double"; break;
202         case OLE.VT_I4 :            sDsc = "int"; break;
203         case OLE.VT_DISPATCH :      sDsc = "IDispatch"; break;
204         case OLE.VT_UNKNOWN :       sDsc = "IUnknown"; break;
205         case OLE.VT_I2 :            sDsc = "short"; break;
206         case OLE.VT_BSTR :          sDsc = "String"; break;
207         case OLE.VT_VARIANT :       sDsc = "Variant"; break;
208         case OLE.VT_CY :            sDsc = "Currency"; break;
209         case OLE.VT_DATE :          sDsc = "Date"; break;
210         case OLE.VT_UI1 :           sDsc = "unsigned char"; break;
211         case OLE.VT_UI4 :           sDsc = "unsigned int"; break;
212         case OLE.VT_USERDEFINED :   sDsc = "UserDefined"; break;
213         case OLE.VT_HRESULT :       sDsc = "HRESULT"; break;
214         case OLE.VT_VOID :          sDsc = "void"; break;
215         case OLE.VT_UI2 :           sDsc = "unsigned short"; break;
216         case OLE.VT_UINT :          sDsc = "unsigned int"; break;
217         case OLE.VT_PTR :           sDsc = "void *"; break;
218         default: break;
219     }
220     if (sDsc !is null) {
221         if ((iType & OLE.VT_BYREF) == OLE.VT_BYREF){
222             return sDsc ~ " *";
223         }
224         return sDsc;
225     }
226     version(Tango){
227         return Format("unknown {} (0x{:X4})", iType, iType);
228     } else { // Phobos
229         return format("unknown %s (0x%4X)", iType, iType);
230     }
231 }
232 
233 String getDirection(int bDirection) {
234     String sDirString = "";
235     bool bComma = false;
236     if ((bDirection & OLE.IDLFLAG_FIN) != 0) {
237         sDirString ~= "in";
238         bComma = true;
239     }
240     if ((bDirection & OLE.IDLFLAG_FOUT) != 0) {
241         if (bComma) sDirString ~= ", ";
242         sDirString ~= "out";
243         bComma = true;
244     }
245     if ((bDirection & OLE.IDLFLAG_FLCID) != 0) {
246         if (bComma) sDirString ~= ", ";
247         sDirString ~= "lcid";
248         bComma = true;
249     }
250     if ((bDirection & OLE.IDLFLAG_FRETVAL) != 0) {
251         if (bComma) sDirString ~= ", ";
252         sDirString ~= "retval";
253     }
254     return sDirString;
255 }
256 
257 String getInvokeKind(int iInvKind) {
258     switch (iInvKind) {
259         case OLE.INVOKE_FUNC : return "METHOD";
260         case OLE.INVOKE_PROPERTYGET : return "PROPERTY GET";
261         case OLE.INVOKE_PROPERTYPUT : return "PROPERTY PUT";
262         case OLE.INVOKE_PROPERTYPUTREF : return "PROPERTY PUT BY REF";
263         default: break;
264     }
265     version(Tango){
266         return Format("unknown {}", iInvKind);
267     } else { // Phobos
268         return format("unknown %s", iInvKind);
269     }
270 }
271 
272