1 11 package org.eclipse.swt.ole.win32; 12 13 import org.eclipse.swt.*; 14 import org.eclipse.swt.internal.ole.win32.*; 15 16 final class OleEventSink 17 { 18 private OleControlSite widget; 19 20 private COMObject iDispatch; 21 private int refCount; 22 23 private IUnknown objIUnknown; 24 private int eventCookie; 25 private GUID eventGuid; 26 27 private OleEventTable eventTable; 28 29 OleEventSink(OleControlSite widget, int iUnknown, GUID riid) { 30 31 this.widget = widget; 32 this.eventGuid = riid; 33 this.objIUnknown = new IUnknown(iUnknown); 34 35 createCOMInterfaces(); 36 } 37 38 void connect () { 39 int[] ppvObject = new int[1]; 40 if (objIUnknown.QueryInterface(COM.IIDIConnectionPointContainer, ppvObject) == COM.S_OK) { 41 IConnectionPointContainer cpc = new IConnectionPointContainer(ppvObject[0]); 42 int[] ppCP = new int[1]; 43 if (cpc.FindConnectionPoint(eventGuid, ppCP) == COM.S_OK) { 44 IConnectionPoint cp = new IConnectionPoint(ppCP[0]); 45 int[] pCookie = new int[1]; 46 if (cp.Advise(iDispatch.getAddress(), pCookie) == COM.S_OK) 47 eventCookie = pCookie[0]; 48 cp.Release(); 49 } 50 cpc.Release(); 51 } 52 } 53 void addListener(int eventID, OleListener listener) { 54 if (listener == null) OLE.error (SWT.ERROR_NULL_ARGUMENT); 55 if (eventTable == null) eventTable = new OleEventTable (); 56 eventTable.hook(eventID, listener); 57 } 58 int AddRef() { 59 refCount++; 60 return refCount; 61 } 62 private void createCOMInterfaces() { 63 iDispatch = new COMObject(new int[]{2, 0, 0, 1, 3, 4, 8}){ 64 public int method0(int[] args) {return QueryInterface(args[0], args[1]);} 65 public int method1(int[] args) {return AddRef();} 66 public int method2(int[] args) {return Release();} 67 public int method6(int[] args) {return Invoke(args[0], args[1], args[2], args[3], args[4], args[5], args[6], args[7]);} 71 }; 72 } 73 void disconnect() { 74 if (eventCookie != 0 && objIUnknown != null) { 76 int[] ppvObject = new int[1]; 77 if (objIUnknown.QueryInterface(COM.IIDIConnectionPointContainer, ppvObject) == COM.S_OK) { 78 IConnectionPointContainer cpc = new IConnectionPointContainer(ppvObject[0]); 79 if (cpc.FindConnectionPoint(eventGuid, ppvObject) == COM.S_OK) { 80 IConnectionPoint cp = new IConnectionPoint(ppvObject[0]); 81 if (cp.Unadvise(eventCookie) == COM.S_OK) { 82 eventCookie = 0; 83 } 84 cp.Release(); 85 } 86 cpc.Release(); 87 } 88 } 89 } 90 private void disposeCOMInterfaces() { 91 if (iDispatch != null) 92 iDispatch.dispose(); 93 iDispatch = null; 94 95 } 96 private int Invoke(int dispIdMember, int riid, int lcid, int dwFlags, int pDispParams, int pVarResult, int pExcepInfo, int pArgErr) 97 { 98 if (eventTable == null || !eventTable.hooks(dispIdMember)) return COM.S_OK; 99 100 Variant[] eventInfo = null; 103 if (pDispParams != 0) { 104 DISPPARAMS dispParams = new DISPPARAMS(); 105 COM.MoveMemory(dispParams, pDispParams, DISPPARAMS.sizeof); 106 eventInfo = new Variant[dispParams.cArgs]; 107 int size = Variant.sizeof; 108 int offset = (dispParams.cArgs - 1) * size; 109 110 for (int j = 0; j < dispParams.cArgs; j++){ 111 eventInfo[j] = new Variant(); 112 eventInfo[j].setData(dispParams.rgvarg + offset); 113 offset = offset - size; 114 } 115 } 116 117 OleEvent event = new OleEvent(); 118 event.arguments = eventInfo; 119 notifyListener(dispIdMember,event); 120 return COM.S_OK; 121 } 122 139 private void notifyListener (int eventType, OleEvent event) { 140 if (event == null) OLE.error (SWT.ERROR_NULL_ARGUMENT); 141 if (eventTable == null) return; 142 event.type = eventType; 143 event.widget = widget; 144 eventTable.sendEvent (event); 145 } 146 private int QueryInterface(int riid, int ppvObject) { 147 148 if (riid == 0 || ppvObject == 0) 149 return COM.E_INVALIDARG; 150 GUID guid = new GUID(); 151 COM.MoveMemory(guid, riid, GUID.sizeof); 152 153 if ( COM.IsEqualGUID(guid, COM.IIDIUnknown) || COM.IsEqualGUID(guid, COM.IIDIDispatch) || 154 COM.IsEqualGUID(guid, eventGuid)) { 155 COM.MoveMemory(ppvObject, new int[] {iDispatch.getAddress()}, 4); 156 AddRef(); 157 return OLE.S_OK; 158 } 159 160 COM.MoveMemory(ppvObject, new int[] {0}, 4); 161 return COM.E_NOINTERFACE; 162 } 163 int Release() { 164 refCount--; 165 if (refCount == 0) { 166 disposeCOMInterfaces(); 167 } 168 169 return refCount; 170 } 171 void removeListener(int eventID, OleListener listener) { 172 if (listener == null) OLE.error (SWT.ERROR_NULL_ARGUMENT); 173 if (eventTable == null) return; 174 eventTable.unhook (eventID, listener); 175 } 176 boolean hasListeners() { 177 return eventTable.hasEntries(); 178 } 179 } 180 | Popular Tags |