KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > ole > win32 > OleControlSite


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.swt.ole.win32;
12
13 import org.eclipse.swt.*;
14 import org.eclipse.swt.internal.ole.win32.*;
15 import org.eclipse.swt.graphics.*;
16 import org.eclipse.swt.widgets.*;
17 import org.eclipse.swt.internal.win32.*;
18
19 /**
20  * OleControlSite provides a site to manage an embedded ActiveX Control within a container.
21  *
22  * <p>In addition to the behaviour provided by OleClientSite, this object provides the following:
23  * <ul>
24  * <li>events from the ActiveX control
25  * <li>notification of property changes from the ActiveX control
26  * <li>simplified access to well known properties of the ActiveX Control (e.g. font, background color)
27  * <li>expose ambient properties of the container to the ActiveX Control
28  * </ul>
29  *
30  * <p>This object implements the OLE Interfaces IOleControlSite, IDispatch, and IPropertyNotifySink.
31  *
32  * <p>Note that although this class is a subclass of <code>Composite</code>,
33  * it does not make sense to add <code>Control</code> children to it,
34  * or set a layout on it.
35  * </p><p>
36  * <dl>
37  * <dt><b>Styles</b> <dd>BORDER
38  * <dt><b>Events</b> <dd>Dispose, Move, Resize
39  * </dl>
40  *
41  */

42 public class OleControlSite extends OleClientSite
43 {
44     // interfaces for this container
45
private COMObject iOleControlSite;
46     private COMObject iDispatch;
47     
48     // supporting Property Change attributes
49
private OlePropertyChangeSink olePropertyChangeSink;
50     
51     // supporting Event Sink attributes
52
private OleEventSink[] oleEventSink = new OleEventSink[0];
53     private GUID[] oleEventSinkGUID = new GUID[0];
54     private int[] oleEventSinkIUnknown = new int[0];
55         
56     // supporting information for the Control COM object
57
private CONTROLINFO currentControlInfo;
58     private int[] sitePropertyIds = new int[0];
59     private Variant[] sitePropertyValues = new Variant[0];
60     
61     // work around for IE destroying the caret
62
static int SWT_RESTORECARET;
63     
64 /**
65  * Create an OleControlSite child widget using style bits
66  * to select a particular look or set of properties.
67  *
68  * @param parent a composite widget; must be an OleFrame
69  * @param style the bitwise OR'ing of widget styles
70  * @param progId the unique program identifier which has been registered for this ActiveX Control;
71  * the value of the ProgID key or the value of the VersionIndependentProgID key specified
72  * in the registry for this Control (for example, the VersionIndependentProgID for
73  * Internet Explorer is Shell.Explorer)
74  *
75  *@exception IllegalArgumentException <ul>
76  * <li>ERROR_NULL_ARGUMENT when the parent is null
77  *</ul>
78  * @exception SWTException <ul>
79  * <li>ERROR_THREAD_INVALID_ACCESS when called from the wrong thread
80  * <li>ERROR_INVALID_CLASSID when the progId does not map to a registered CLSID
81  * <li>ERROR_CANNOT_CREATE_OBJECT when failed to create OLE Object
82  * <li>ERROR_CANNOT_ACCESS_CLASSFACTORY when Class Factory could not be found
83  * <li>ERROR_CANNOT_CREATE_LICENSED_OBJECT when failed to create a licensed OLE Object
84  * </ul>
85  */

86 public OleControlSite(Composite parent, int style, String JavaDoc progId) {
87     super(parent, style);
88     try {
89     
90         // check for licensing
91
appClsid = getClassID(progId);
92         if (appClsid == null) OLE.error(OLE.ERROR_INVALID_CLASSID);
93     
94         int licinfo = getLicenseInfo(appClsid);
95         if (licinfo == 0) {
96             
97             // Open a storage object
98
tempStorage = createTempStorage();
99     
100             // Create ole object with storage object
101
int[] address = new int[1];
102             int result = COM.OleCreate(appClsid, COM.IIDIUnknown, COM.OLERENDER_DRAW, null, 0, tempStorage.getAddress(), address);
103             if (result != COM.S_OK)
104                 OLE.error(OLE.ERROR_CANNOT_CREATE_OBJECT, result);
105     
106             objIUnknown = new IUnknown(address[0]);
107             
108         } else {
109             // Prepare the ClassFactory
110
int[] ppvObject = new int[1];
111             try {
112                 int result = COM.CoGetClassObject(appClsid, COM.CLSCTX_INPROC_HANDLER | COM.CLSCTX_INPROC_SERVER, 0, COM.IIDIClassFactory2, ppvObject);
113                 if (result != COM.S_OK) {
114                     OLE.error(OLE.ERROR_CANNOT_ACCESS_CLASSFACTORY, result);
115                 }
116                 IClassFactory2 classFactory = new IClassFactory2(ppvObject[0]);
117                 // Create Com Object
118
ppvObject = new int[1];
119                 result = classFactory.CreateInstanceLic(0, 0, COM.IIDIUnknown, licinfo, ppvObject);
120                 classFactory.Release();
121                 if (result != COM.S_OK)
122                     OLE.error(OLE.ERROR_CANNOT_CREATE_LICENSED_OBJECT, result);
123             } finally {
124                 COM.SysFreeString(licinfo);
125             }
126             
127             objIUnknown = new IUnknown(ppvObject[0]);
128     
129             // Prepare a storage medium
130
ppvObject = new int[1];
131             if (objIUnknown.QueryInterface(COM.IIDIPersistStorage, ppvObject) == COM.S_OK) {
132                 IPersistStorage persist = new IPersistStorage(ppvObject[0]);
133                 tempStorage = createTempStorage();
134                 persist.InitNew(tempStorage.getAddress());
135                 persist.Release();
136             }
137         }
138     
139         // Init sinks
140
addObjectReferences();
141         
142         // Init site properties
143
setSiteProperty(COM.DISPID_AMBIENT_USERMODE, new Variant(true));
144         setSiteProperty(COM.DISPID_AMBIENT_UIDEAD, new Variant(false));
145             
146         if (COM.OleRun(objIUnknown.getAddress()) == OLE.S_OK) state= STATE_RUNNING;
147
148     } catch (SWTError e) {
149         dispose();
150         disposeCOMInterfaces();
151         throw e;
152     }
153 }
154 /**
155  * Adds the listener to receive events.
156  *
157  * @param eventID the id of the event
158  *
159  * @param listener the listener
160  *
161  * @exception IllegalArgumentException <ul>
162  * <li>ERROR_NULL_ARGUMENT when listener is null</li>
163  * </ul>
164  */

165 public void addEventListener(int eventID, OleListener listener) {
166     if (listener == null) OLE.error (SWT.ERROR_NULL_ARGUMENT);
167     GUID riid = getDefaultEventSinkGUID(objIUnknown);
168     if (riid != null) {
169         addEventListener(objIUnknown.getAddress(), riid, eventID, listener);
170     }
171     
172 }
173 static GUID getDefaultEventSinkGUID(IUnknown unknown) {
174     // get Event Sink I/F from IProvideClassInfo2
175
int[] ppvObject = new int[1];
176     if (unknown.QueryInterface(COM.IIDIProvideClassInfo2, ppvObject) == COM.S_OK) {
177         IProvideClassInfo2 pci2 = new IProvideClassInfo2(ppvObject[0]);
178         GUID riid = new GUID();
179         int result = pci2.GetGUID(COM.GUIDKIND_DEFAULT_SOURCE_DISP_IID, riid);
180         pci2.Release();
181         if (result == COM.S_OK) return riid;
182     }
183
184     // get Event Sink I/F from IProvideClassInfo
185
if (unknown.QueryInterface(COM.IIDIProvideClassInfo, ppvObject) == COM.S_OK) {
186         IProvideClassInfo pci = new IProvideClassInfo(ppvObject[0]);
187         int[] ppTI = new int[1];
188         int[] ppEI = new int[1];
189         int result = pci.GetClassInfo(ppTI);
190         pci.Release();
191         
192         if (result == COM.S_OK && ppTI[0] != 0) {
193             ITypeInfo classInfo = new ITypeInfo(ppTI[0]);
194             int[] ppTypeAttr = new int[1];
195             result = classInfo.GetTypeAttr(ppTypeAttr);
196             if (result == COM.S_OK && ppTypeAttr[0] != 0) {
197                 TYPEATTR typeAttribute = new TYPEATTR();
198                 COM.MoveMemory(typeAttribute, ppTypeAttr[0], TYPEATTR.sizeof);
199                 classInfo.ReleaseTypeAttr(ppTypeAttr[0]);
200                 int implMask = COM.IMPLTYPEFLAG_FDEFAULT | COM.IMPLTYPEFLAG_FSOURCE | COM.IMPLTYPEFLAG_FRESTRICTED;
201                 int implBits = COM.IMPLTYPEFLAG_FDEFAULT | COM.IMPLTYPEFLAG_FSOURCE;
202                 
203                 for (int i = 0; i < typeAttribute.cImplTypes; i++) {
204                     int[] pImplTypeFlags = new int[1];
205                     if (classInfo.GetImplTypeFlags(i, pImplTypeFlags) == COM.S_OK) {
206                         if ((pImplTypeFlags[0] & implMask) == implBits) {
207                             int[] pRefType = new int[1];
208                             if (classInfo.GetRefTypeOfImplType(i, pRefType) == COM.S_OK) {
209                                 classInfo.GetRefTypeInfo(pRefType[0], ppEI);
210                             }
211                         }
212                     }
213                 }
214             }
215             classInfo.Release();
216     
217             if (ppEI[0] != 0) {
218                 ITypeInfo eventInfo = new ITypeInfo(ppEI[0]);
219                 ppTypeAttr = new int[1];
220                 result = eventInfo.GetTypeAttr(ppTypeAttr);
221                 GUID riid = null;
222                 if (result == COM.S_OK && ppTypeAttr[0] != 0) {
223                     riid = new GUID();
224                     COM.MoveMemory(riid, ppTypeAttr[0], GUID.sizeof);
225                     eventInfo.ReleaseTypeAttr(ppTypeAttr[0]);
226                 }
227                 eventInfo.Release();
228                 return riid;
229             }
230         }
231     }
232     return null;
233 }
234
235 /**
236  * Adds the listener to receive events.
237  *
238  * @since 2.0
239  *
240  * @param automation the automation object that provides the event notification
241  * @param eventID the id of the event
242  * @param listener the listener
243  *
244  * @exception IllegalArgumentException <ul>
245  * <li>ERROR_NULL_ARGUMENT when listener is null</li>
246  * </ul>
247  */

248 public void addEventListener(OleAutomation automation, int eventID, OleListener listener) {
249     if (listener == null || automation == null) OLE.error (SWT.ERROR_NULL_ARGUMENT);
250     int address = automation.getAddress();
251     IUnknown unknown = new IUnknown(address);
252     GUID riid = getDefaultEventSinkGUID(unknown);
253     if (riid != null) {
254         addEventListener(address, riid, eventID, listener);
255     }
256     
257 }
258 /**
259  * Adds the listener to receive events.
260  *
261  * @since 3.2
262  *
263  * @param automation the automation object that provides the event notification
264  * @param eventSinkId the GUID of the event sink
265  * @param eventID the id of the event
266  * @param listener the listener
267  *
268  * @exception IllegalArgumentException <ul>
269  * <li>ERROR_NULL_ARGUMENT when listener is null</li>
270  * </ul>
271  */

272 public void addEventListener(OleAutomation automation, String JavaDoc eventSinkId, int eventID, OleListener listener) {
273     if (listener == null || automation == null || eventSinkId == null) OLE.error (SWT.ERROR_NULL_ARGUMENT);
274     int address = automation.getAddress();
275     if (address == 0) return;
276     char[] buffer = (eventSinkId +"\0").toCharArray();
277     GUID guid = new GUID();
278     if (COM.IIDFromString(buffer, guid) != COM.S_OK) return;
279     addEventListener(address, guid, eventID, listener);
280 }
281
282 void addEventListener(int iunknown, GUID guid, int eventID, OleListener listener) {
283     if (listener == null || iunknown == 0 || guid == null) OLE.error (SWT.ERROR_NULL_ARGUMENT);
284     // have we connected to this kind of event sink before?
285
int index = -1;
286     for (int i = 0; i < oleEventSinkGUID.length; i++) {
287         if (COM.IsEqualGUID(oleEventSinkGUID[i], guid)) {
288             if (iunknown == oleEventSinkIUnknown[i]) {
289                 index = i;
290                 break;
291             }
292         }
293     }
294     if (index != -1) {
295         oleEventSink[index].addListener(eventID, listener);
296     } else {
297         int oldLength = oleEventSink.length;
298         OleEventSink[] newOleEventSink = new OleEventSink[oldLength + 1];
299         GUID[] newOleEventSinkGUID = new GUID[oldLength + 1];
300         int[] newOleEventSinkIUnknown = new int[oldLength + 1];
301         System.arraycopy(oleEventSink, 0, newOleEventSink, 0, oldLength);
302         System.arraycopy(oleEventSinkGUID, 0, newOleEventSinkGUID, 0, oldLength);
303         System.arraycopy(oleEventSinkIUnknown, 0, newOleEventSinkIUnknown, 0, oldLength);
304         oleEventSink = newOleEventSink;
305         oleEventSinkGUID = newOleEventSinkGUID;
306         oleEventSinkIUnknown = newOleEventSinkIUnknown;
307         
308         oleEventSink[oldLength] = new OleEventSink(this, iunknown, guid);
309         oleEventSinkGUID[oldLength] = guid;
310         oleEventSinkIUnknown[oldLength] = iunknown;
311         oleEventSink[oldLength].AddRef();
312         oleEventSink[oldLength].connect();
313         oleEventSink[oldLength].addListener(eventID, listener);
314         
315     }
316 }
317 protected void addObjectReferences() {
318
319     super.addObjectReferences();
320     
321     // Get property change notification from control
322
connectPropertyChangeSink();
323
324     // Get access to the Control object
325
int[] ppvObject = new int[1];
326     if (objIUnknown.QueryInterface(COM.IIDIOleControl, ppvObject) == COM.S_OK) {
327         IOleControl objIOleControl = new IOleControl(ppvObject[0]);
328         // ask the control for its info in case users
329
// need to act on it
330
currentControlInfo = new CONTROLINFO();
331         objIOleControl.GetControlInfo(currentControlInfo);
332         objIOleControl.Release();
333     }
334 }
335 /**
336  * Adds the listener to receive events.
337  *
338  * @param propertyID the identifier of the property
339  * @param listener the listener
340  *
341  * @exception IllegalArgumentException <ul>
342  * <li>ERROR_NULL_ARGUMENT when listener is null</li>
343  * </ul>
344  */

345 public void addPropertyListener(int propertyID, OleListener listener) {
346     if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
347     olePropertyChangeSink.addListener(propertyID, listener);
348 }
349
350 private void connectPropertyChangeSink() {
351     olePropertyChangeSink = new OlePropertyChangeSink(this);
352     olePropertyChangeSink.AddRef();
353     olePropertyChangeSink.connect(objIUnknown);
354 }
355 protected void createCOMInterfaces () {
356     super.createCOMInterfaces();
357     
358     // register each of the interfaces that this object implements
359
iOleControlSite = new COMObject(new int[]{2, 0, 0, 0, 1, 1, 3, 2, 1, 0}){
360         public int method0(int[] args) {return QueryInterface(args[0], args[1]);}
361         public int method1(int[] args) {return AddRef();}
362         public int method2(int[] args) {return Release();}
363         public int method3(int[] args) {return OnControlInfoChanged();}
364         // method4 LockInPlaceActive - not implemented
365
// method5 GetExtendedControl - not implemented
366
// method6 TransformCoords - not implemented
367
// method7 Translate Accelerator - not implemented
368
public int method8(int[] args) {return OnFocus(args[0]);}
369         // method9 ShowPropertyFrame - not implemented
370
};
371     
372     iDispatch = new COMObject(new int[]{2, 0, 0, 1, 3, 5, 8}){
373         public int method0(int[] args) {return QueryInterface(args[0], args[1]);}
374         public int method1(int[] args) {return AddRef();}
375         public int method2(int[] args) {return Release();}
376         // method3 GetTypeInfoCount - not implemented
377
// method4 GetTypeInfo - not implemented
378
// method5 GetIDsOfNames - not implemented
379
public int method6(int[] args) {return Invoke(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]);}
380     };
381 }
382 private void disconnectEventSinks() {
383
384     for (int i = 0; i < oleEventSink.length; i++) {
385         OleEventSink sink = oleEventSink[i];
386         sink.disconnect();
387         sink.Release();
388     }
389     oleEventSink = new OleEventSink[0];
390     oleEventSinkGUID = new GUID[0];
391     oleEventSinkIUnknown = new int[0];
392 }
393 private void disconnectPropertyChangeSink() {
394
395     if (olePropertyChangeSink != null) {
396         olePropertyChangeSink.disconnect(objIUnknown);
397         olePropertyChangeSink.Release();
398     }
399     olePropertyChangeSink = null;
400 }
401 protected void disposeCOMInterfaces() {
402     super.disposeCOMInterfaces();
403
404     if (iOleControlSite != null)
405         iOleControlSite.dispose();
406     iOleControlSite = null;
407
408     if (iDispatch != null)
409         iDispatch.dispose();
410     iDispatch = null;
411 }
412 public Color getBackground () {
413
414     if (objIUnknown != null) {
415         // !! We are getting the OLE_COLOR - should we change this to the COLORREF value?
416
OleAutomation oleObject= new OleAutomation(this);
417         Variant varBackColor = oleObject.getProperty(COM.DISPID_BACKCOLOR);
418         oleObject.dispose();
419     
420         if (varBackColor != null){
421             int[] colorRef = new int[1];
422             if (COM.OleTranslateColor(varBackColor.getInt(), getDisplay().hPalette, colorRef) == COM.S_OK)
423                 return Color.win32_new(getDisplay(), colorRef[0]);
424         }
425     }
426         
427     return super.getBackground();
428 }
429 public Font getFont () {
430
431     if (objIUnknown != null) {
432         OleAutomation oleObject= new OleAutomation(this);
433         Variant varDispFont = oleObject.getProperty(COM.DISPID_FONT);
434         oleObject.dispose();
435     
436         if (varDispFont != null){
437             OleAutomation iDispFont = varDispFont.getAutomation();
438             Variant lfFaceName = iDispFont.getProperty(COM.DISPID_FONT_NAME);
439             Variant lfHeight = iDispFont.getProperty(COM.DISPID_FONT_SIZE);
440             Variant lfItalic = iDispFont.getProperty(COM.DISPID_FONT_ITALIC);
441             //Variant lfCharSet = iDispFont.getProperty(COM.DISPID_FONT_CHARSET);
442
Variant lfBold = iDispFont.getProperty(COM.DISPID_FONT_BOLD);
443             iDispFont.dispose();
444
445             if (lfFaceName != null &&
446                 lfHeight != null &&
447                 lfItalic != null &&
448                 lfBold != null){
449                 int style = 3 * lfBold.getInt() + 2 * lfItalic.getInt();
450                 Font font = new Font(getShell().getDisplay(), lfFaceName.getString(), lfHeight.getInt(), style);
451                 return font;
452             }
453         }
454     }
455         
456     return super.getFont();
457 }
458 public Color getForeground () {
459
460     if (objIUnknown != null) {
461         // !! We are getting the OLE_COLOR - should we change this to the COLORREF value?
462
OleAutomation oleObject= new OleAutomation(this);
463         Variant varForeColor = oleObject.getProperty(COM.DISPID_FORECOLOR);
464         oleObject.dispose();
465     
466         if (varForeColor != null){
467             int[] colorRef = new int[1];
468             if (COM.OleTranslateColor(varForeColor.getInt(), getDisplay().hPalette, colorRef) == COM.S_OK)
469                 return Color.win32_new(getDisplay(), colorRef[0]);
470         }
471     }
472         
473     return super.getForeground();
474 }
475 protected int getLicenseInfo(GUID clsid) {
476     int[] ppvObject = new int[1];
477
478     if (COM.CoGetClassObject(clsid, COM.CLSCTX_INPROC_HANDLER | COM.CLSCTX_INPROC_SERVER, 0, COM.IIDIClassFactory2, ppvObject) != COM.S_OK) {
479         return 0;
480     }
481     IClassFactory2 classFactory = new IClassFactory2(ppvObject[0]);
482     LICINFO licinfo = new LICINFO();
483     if (classFactory.GetLicInfo(licinfo) != COM.S_OK) {
484         classFactory.Release();
485         return 0;
486     }
487     int[] pBstrKey = new int[1];
488     if (licinfo != null && licinfo.fRuntimeKeyAvail) {
489         if (classFactory.RequestLicKey(0, pBstrKey) == COM.S_OK) {
490             classFactory.Release();
491             return pBstrKey[0];
492         }
493     }
494     classFactory.Release();
495     return 0;
496 }
497 /**
498  *
499  * Get the control site property specified by the dispIdMember.
500  *
501  * @since 2.1
502  *
503  */

504 public Variant getSiteProperty(int dispId){
505     for (int i = 0; i < sitePropertyIds.length; i++) {
506         if (sitePropertyIds[i] == dispId) {
507             return sitePropertyValues[i];
508         }
509     }
510     return null;
511 }
512 protected int GetWindow(int phwnd) {
513
514     if (phwnd == 0)
515         return COM.E_INVALIDARG;
516     if (frame == null) {
517         COM.MoveMemory(phwnd, new int[] {0}, 4);
518         return COM.E_NOTIMPL;
519     }
520     
521     // Copy the Window's handle into the memory passed in
522
COM.MoveMemory(phwnd, new int[] {handle}, 4);
523     return COM.S_OK;
524 }
525 private int Invoke(int dispIdMember, int riid, int lcid, int dwFlags, int pDispParams, int pVarResult, int pExcepInfo, int pArgErr) {
526     if (pVarResult == 0 || dwFlags != COM.DISPATCH_PROPERTYGET) {
527         if (pExcepInfo != 0) COM.MoveMemory(pExcepInfo, new int[] {0}, 4);
528         if (pArgErr != 0) COM.MoveMemory(pArgErr, new int[] {0}, 4);
529         return COM.DISP_E_MEMBERNOTFOUND;
530     }
531     Variant result = getSiteProperty(dispIdMember);
532     if (result != null) {
533         if (pVarResult != 0) result.getData(pVarResult);
534         return COM.S_OK;
535     }
536     switch (dispIdMember) {
537             // indicate a false result
538
case COM.DISPID_AMBIENT_SUPPORTSMNEMONICS :
539         case COM.DISPID_AMBIENT_SHOWGRABHANDLES :
540         case COM.DISPID_AMBIENT_SHOWHATCHING :
541             if (pVarResult != 0) COM.MoveMemory(pVarResult, new int[] {0}, 4);
542             if (pExcepInfo != 0) COM.MoveMemory(pExcepInfo, new int[] {0}, 4);
543             if (pArgErr != 0) COM.MoveMemory(pArgErr, new int[] {0}, 4);
544             return COM.S_FALSE;
545
546             // not implemented
547
case COM.DISPID_AMBIENT_OFFLINEIFNOTCONNECTED :
548         case COM.DISPID_AMBIENT_BACKCOLOR :
549         case COM.DISPID_AMBIENT_FORECOLOR :
550         case COM.DISPID_AMBIENT_FONT :
551         case COM.DISPID_AMBIENT_LOCALEID :
552         case COM.DISPID_AMBIENT_SILENT :
553         case COM.DISPID_AMBIENT_MESSAGEREFLECT :
554             if (pVarResult != 0) COM.MoveMemory(pVarResult, new int[] {0}, 4);
555             if (pExcepInfo != 0) COM.MoveMemory(pExcepInfo, new int[] {0}, 4);
556             if (pArgErr != 0) COM.MoveMemory(pArgErr, new int[] {0}, 4);
557             return COM.E_NOTIMPL;
558             
559         default :
560             if (pVarResult != 0) COM.MoveMemory(pVarResult, new int[] {0}, 4);
561             if (pExcepInfo != 0) COM.MoveMemory(pExcepInfo, new int[] {0}, 4);
562             if (pArgErr != 0) COM.MoveMemory(pArgErr, new int[] {0}, 4);
563             return COM.DISP_E_MEMBERNOTFOUND;
564     }
565 }
566 private int OnControlInfoChanged() {
567     int[] ppvObject = new int[1];
568     if (objIUnknown.QueryInterface(COM.IIDIOleControl, ppvObject) == COM.S_OK) {
569         IOleControl objIOleControl = new IOleControl(ppvObject[0]);
570         // ask the control for its info in case users
571
// need to act on it
572
currentControlInfo = new CONTROLINFO();
573         objIOleControl.GetControlInfo(currentControlInfo);
574         objIOleControl.Release();
575     }
576     return COM.S_OK;
577 }
578 void onFocusIn(Event e) {
579     if (objIOleInPlaceObject == null) return;
580     doVerb(OLE.OLEIVERB_UIACTIVATE);
581     if (isFocusControl()) return;
582     int[] phwnd = new int[1];
583     objIOleInPlaceObject.GetWindow(phwnd);
584     if (phwnd[0] == 0) return;
585     OS.SetFocus(phwnd[0]);
586 }
587 void onFocusOut(Event e) {
588     if (objIOleInPlaceObject != null) {
589         /*
590         * Bug in Windows. When IE7 loses focus and UIDeactivate()
591         * is called, IE destroys the caret even though it is
592         * no longer owned by IE. If focus has moved to a control
593         * that shows a caret then the caret disappears. The fix
594         * is to detect this case and restore the caret.
595         */

596         int threadId = OS.GetCurrentThreadId();
597         GUITHREADINFO lpgui1 = new GUITHREADINFO();
598         lpgui1.cbSize = GUITHREADINFO.sizeof;
599         OS.GetGUIThreadInfo(threadId, lpgui1);
600         objIOleInPlaceObject.UIDeactivate();
601         if (lpgui1.hwndCaret != 0) {
602             GUITHREADINFO lpgui2 = new GUITHREADINFO();
603             lpgui2.cbSize = GUITHREADINFO.sizeof;
604             OS.GetGUIThreadInfo(threadId, lpgui2);
605             if (lpgui2.hwndCaret == 0 && lpgui1.hwndCaret == OS.GetFocus()) {
606                 if (SWT_RESTORECARET == 0) {
607                     SWT_RESTORECARET = OS.RegisterWindowMessage (new TCHAR (0, "SWT_RESTORECARET", true));
608                 }
609                 /*
610                 * If the caret was not restored by SWT, put it back using
611                 * the information from GUITHREADINFO. Note that this will
612                 * not be correct when the caret has a bitmap. There is no
613                 * API to query the bitmap that the caret is using.
614                 */

615                 if (OS.SendMessage (lpgui1.hwndCaret, SWT_RESTORECARET, 0, 0) == 0) {
616                     int width = lpgui1.right - lpgui1.left;
617                     int height = lpgui1.bottom - lpgui1.top;
618                     OS.CreateCaret (lpgui1.hwndCaret, 0, width, height);
619                     OS.SetCaretPos (lpgui1.left, lpgui1.top);
620                     OS.ShowCaret (lpgui1.hwndCaret);
621                 }
622             }
623         }
624     }
625 }
626 private int OnFocus(int fGotFocus) {
627     return COM.S_OK;
628 }
629 protected int OnUIDeactivate(int fUndoable) {
630     // controls don't need to do anything for
631
// border space or menubars
632
state = STATE_INPLACEACTIVE;
633     return COM.S_OK;
634 }
635 protected int QueryInterface(int riid, int ppvObject) {
636     int result = super.QueryInterface(riid, ppvObject);
637     if (result == COM.S_OK)
638         return result;
639     if (riid == 0 || ppvObject == 0)
640         return COM.E_INVALIDARG;
641     GUID guid = new GUID();
642     COM.MoveMemory(guid, riid, GUID.sizeof);
643     if (COM.IsEqualGUID(guid, COM.IIDIOleControlSite)) {
644         COM.MoveMemory(ppvObject, new int[] {iOleControlSite.getAddress()}, 4);
645         AddRef();
646         return COM.S_OK;
647     }
648     if (COM.IsEqualGUID(guid, COM.IIDIDispatch)) {
649         COM.MoveMemory(ppvObject, new int[] {iDispatch.getAddress()}, 4);
650         AddRef();
651         return COM.S_OK;
652     }
653     COM.MoveMemory(ppvObject, new int[] {0}, 4);
654     return COM.E_NOINTERFACE;
655 }
656 protected int Release() {
657     int result = super.Release();
658     if (result == 0) {
659         for (int i = 0; i < sitePropertyIds.length; i++) {
660             sitePropertyValues[i].dispose();
661         }
662         sitePropertyIds = new int[0];
663         sitePropertyValues = new Variant[0];
664     }
665     return result;
666 }
667 protected void releaseObjectInterfaces() {
668     
669     disconnectEventSinks();
670     
671     disconnectPropertyChangeSink();
672
673     super.releaseObjectInterfaces();
674 }
675 /**
676  * Removes the listener.
677  *
678  * @param eventID the event identifier
679  *
680  * @param listener the listener
681  *
682  * @exception IllegalArgumentException <ul>
683  * <li>ERROR_NULL_ARGUMENT when listener is null</li>
684  * </ul>
685  */

686 public void removeEventListener(int eventID, OleListener listener) {
687     checkWidget();
688     if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
689     
690     GUID riid = getDefaultEventSinkGUID(objIUnknown);
691     if (riid != null) {
692         removeEventListener(objIUnknown.getAddress(), riid, eventID, listener);
693     }
694 }
695 /**
696  * Removes the listener.
697  *
698  * @since 2.0
699  * @deprecated - use OleControlSite.removeEventListener(OleAutomation, int, OleListener)
700  *
701  * @param automation the automation object that provides the event notification
702  *
703  * @param guid the identifier of the events COM interface
704  *
705  * @param eventID the event identifier
706  *
707  * @param listener the listener
708  *
709  * @exception IllegalArgumentException <ul>
710  * <li>ERROR_NULL_ARGUMENT when listener is null</li>
711  * </ul>
712  */

713 public void removeEventListener(OleAutomation automation, GUID guid, int eventID, OleListener listener) {
714     checkWidget();
715     if (automation == null || listener == null || guid == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
716     removeEventListener(automation.getAddress(), guid, eventID, listener);
717 }
718 /**
719  * Removes the listener.
720  *
721  * @since 2.0
722  *
723  * @param automation the automation object that provides the event notification
724  *
725  * @param eventID the event identifier
726  *
727  * @param listener the listener
728  *
729  * @exception IllegalArgumentException <ul>
730  * <li>ERROR_NULL_ARGUMENT when listener is null</li>
731  * </ul>
732  */

733 public void removeEventListener(OleAutomation automation, int eventID, OleListener listener) {
734     checkWidget();
735     if (automation == null || listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
736     int address = automation.getAddress();
737     IUnknown unknown = new IUnknown(address);
738     GUID riid = getDefaultEventSinkGUID(unknown);
739     if (riid != null) {
740         removeEventListener(address, riid, eventID, listener);
741     }
742 }
743 void removeEventListener(int iunknown, GUID guid, int eventID, OleListener listener) {
744     if (listener == null || guid == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
745     for (int i = 0; i < oleEventSink.length; i++) {
746         if (COM.IsEqualGUID(oleEventSinkGUID[i], guid)) {
747             if (iunknown == oleEventSinkIUnknown[i]) {
748                 oleEventSink[i].removeListener(eventID, listener);
749                 if (!oleEventSink[i].hasListeners()) {
750                     //free resources associated with event sink
751
oleEventSink[i].disconnect();
752                     oleEventSink[i].Release();
753                     int oldLength = oleEventSink.length;
754                     if (oldLength == 1) {
755                         oleEventSink = new OleEventSink[0];
756                         oleEventSinkGUID = new GUID[0];
757                         oleEventSinkIUnknown = new int[0];
758                     } else {
759                         OleEventSink[] newOleEventSink = new OleEventSink[oldLength - 1];
760                         System.arraycopy(oleEventSink, 0, newOleEventSink, 0, i);
761                         System.arraycopy(oleEventSink, i + 1, newOleEventSink, i, oldLength - i - 1);
762                         oleEventSink = newOleEventSink;
763                         
764                         GUID[] newOleEventSinkGUID = new GUID[oldLength - 1];
765                         System.arraycopy(oleEventSinkGUID, 0, newOleEventSinkGUID, 0, i);
766                         System.arraycopy(oleEventSinkGUID, i + 1, newOleEventSinkGUID, i, oldLength - i - 1);
767                         oleEventSinkGUID = newOleEventSinkGUID;
768                         
769                         int[] newOleEventSinkIUnknown = new int[oldLength - 1];
770                         System.arraycopy(oleEventSinkIUnknown, 0, newOleEventSinkIUnknown, 0, i);
771                         System.arraycopy(oleEventSinkIUnknown, i + 1, newOleEventSinkIUnknown, i, oldLength - i - 1);
772                         oleEventSinkIUnknown = newOleEventSinkIUnknown;
773                     }
774                 }
775                 return;
776             }
777         }
778     }
779 }
780 /**
781  * Removes the listener.
782  *
783  * @param listener the listener
784  *
785  * @exception IllegalArgumentException <ul>
786  * <li>ERROR_NULL_ARGUMENT when listener is null</li>
787  * </ul>
788  */

789 public void removePropertyListener(int propertyID, OleListener listener) {
790     if (listener == null) SWT.error (SWT.ERROR_NULL_ARGUMENT);
791     olePropertyChangeSink.removeListener(propertyID, listener);
792 }
793 public void setBackground (Color color) {
794
795     super.setBackground(color);
796
797     //set the background of the ActiveX Control
798
if (objIUnknown != null) {
799         OleAutomation oleObject= new OleAutomation(this);
800         oleObject.setProperty(COM.DISPID_BACKCOLOR, new Variant(color.handle));
801         oleObject.dispose();
802     }
803 }
804 public void setFont (Font font) {
805
806     super.setFont(font);
807     
808     //set the font of the ActiveX Control
809
if (objIUnknown != null) {
810         
811         OleAutomation oleObject= new OleAutomation(this);
812         Variant varDispFont = oleObject.getProperty(COM.DISPID_FONT);
813         oleObject.dispose();
814     
815         if (varDispFont != null){
816             OleAutomation iDispFont = varDispFont.getAutomation();
817             FontData[] fdata = font.getFontData();
818             iDispFont.setProperty(COM.DISPID_FONT_NAME, new Variant(fdata[0].getName()));
819             iDispFont.setProperty(COM.DISPID_FONT_SIZE, new Variant(fdata[0].getHeight()));
820             iDispFont.setProperty(COM.DISPID_FONT_ITALIC, new Variant(fdata[0].getStyle() & SWT.ITALIC));
821             //iDispFont.setProperty(COM.DISPID_FONT_CHARSET, new Variant(fdata[0].getCharset));
822
iDispFont.setProperty(COM.DISPID_FONT_BOLD, new Variant((fdata[0].getStyle() & SWT.BOLD)));
823             iDispFont.dispose();
824         }
825     }
826         
827     return;
828 }
829 public void setForeground (Color color) {
830
831     super.setForeground(color);
832
833     //set the foreground of the ActiveX Control
834
if (objIUnknown != null) {
835         OleAutomation oleObject= new OleAutomation(this);
836         oleObject.setProperty(COM.DISPID_FORECOLOR, new Variant(color.handle));
837         oleObject.dispose();
838     }
839 }
840 /**
841  * Sets the control site property specified by the dispIdMember to a new value.
842  * The value will be disposed by the control site when it is no longer required
843  * using Variant.dispose. Passing a value of null will clear the dispId value.
844  *
845  * @param dispId the ID of the property as specified by the IDL of the ActiveX Control
846  * @param value The new value for the property as expressed in a Variant.
847  *
848  * @since 2.1
849  */

850 public void setSiteProperty(int dispId, Variant value){
851     for (int i = 0; i < sitePropertyIds.length; i++) {
852         if (sitePropertyIds[i] == dispId) {
853             if (sitePropertyValues[i] != null) {
854                 sitePropertyValues[i].dispose();
855             }
856             if (value != null) {
857                 sitePropertyValues[i] = value;
858             } else {
859                 int oldLength = sitePropertyIds.length;
860                 int[] newSitePropertyIds = new int[oldLength - 1];
861                 Variant[] newSitePropertyValues = new Variant[oldLength - 1];
862                 System.arraycopy(sitePropertyIds, 0, newSitePropertyIds, 0, i);
863                 System.arraycopy(sitePropertyIds, i + 1, newSitePropertyIds, i, oldLength - i - 1);
864                 System.arraycopy(sitePropertyValues, 0, newSitePropertyValues, 0, i);
865                 System.arraycopy(sitePropertyValues, i + 1, newSitePropertyValues, i, oldLength - i - 1);
866                 sitePropertyIds = newSitePropertyIds;
867                 sitePropertyValues = newSitePropertyValues;
868             }
869             return;
870         }
871     }
872     int oldLength = sitePropertyIds.length;
873     int[] newSitePropertyIds = new int[oldLength + 1];
874     Variant[] newSitePropertyValues = new Variant[oldLength + 1];
875     System.arraycopy(sitePropertyIds, 0, newSitePropertyIds, 0, oldLength);
876     System.arraycopy(sitePropertyValues, 0, newSitePropertyValues, 0, oldLength);
877     newSitePropertyIds[oldLength] = dispId;
878     newSitePropertyValues[oldLength] = value;
879     sitePropertyIds = newSitePropertyIds;
880     sitePropertyValues = newSitePropertyValues;
881 }
882 }
883
Popular Tags