windowscodecs: Avoid an iface -> impl conversion from a non-method.
[wine] / dlls / shdocvw / events.c
1 /*
2  * Implementation of event-related interfaces for WebBrowser control:
3  *
4  *  - IConnectionPointContainer
5  *  - IConnectionPoint
6  *
7  * Copyright 2001 John R. Sheets (for CodeWeavers)
8  * Copyright 2006 Jacek Caban for CodeWeavers
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 2.1 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24
25 #include <string.h>
26 #include "wine/debug.h"
27 #include "shdocvw.h"
28
29 WINE_DEFAULT_DEBUG_CHANNEL(shdocvw);
30
31 struct ConnectionPoint {
32     IConnectionPoint IConnectionPoint_iface;
33
34     IConnectionPointContainer *container;
35
36     IDispatch **sinks;
37     DWORD sinks_size;
38
39     IID iid;
40 };
41
42 /**********************************************************************
43  * Implement the IConnectionPointContainer interface
44  */
45
46 static inline ConnectionPointContainer *impl_from_IConnectionPointContainer(IConnectionPointContainer *iface)
47 {
48     return CONTAINING_RECORD(iface, ConnectionPointContainer, IConnectionPointContainer_iface);
49 }
50
51 static HRESULT WINAPI ConnectionPointContainer_QueryInterface(IConnectionPointContainer *iface,
52         REFIID riid, LPVOID *ppv)
53 {
54     ConnectionPointContainer *This = impl_from_IConnectionPointContainer(iface);
55     return IUnknown_QueryInterface(This->impl, riid, ppv);
56 }
57
58 static ULONG WINAPI ConnectionPointContainer_AddRef(IConnectionPointContainer *iface)
59 {
60     ConnectionPointContainer *This = impl_from_IConnectionPointContainer(iface);
61     return IUnknown_AddRef(This->impl);
62 }
63
64 static ULONG WINAPI ConnectionPointContainer_Release(IConnectionPointContainer *iface)
65 {
66     ConnectionPointContainer *This = impl_from_IConnectionPointContainer(iface);
67     return IUnknown_Release(This->impl);
68 }
69
70 static HRESULT WINAPI ConnectionPointContainer_EnumConnectionPoints(IConnectionPointContainer *iface,
71         LPENUMCONNECTIONPOINTS *ppEnum)
72 {
73     ConnectionPointContainer *This = impl_from_IConnectionPointContainer(iface);
74     FIXME("(%p)->(%p)\n", This, ppEnum);
75     return E_NOTIMPL;
76 }
77
78 static HRESULT WINAPI ConnectionPointContainer_FindConnectionPoint(IConnectionPointContainer *iface,
79         REFIID riid, LPCONNECTIONPOINT *ppCP)
80 {
81     ConnectionPointContainer *This = impl_from_IConnectionPointContainer(iface);
82
83     if(!ppCP) {
84         WARN("ppCP == NULL\n");
85         return E_POINTER;
86     }
87
88     *ppCP = NULL;
89
90     if(IsEqualGUID(&DIID_DWebBrowserEvents2, riid)) {
91         TRACE("(%p)->(DIID_DWebBrowserEvents2 %p)\n", This, ppCP);
92         *ppCP = &This->wbe2->IConnectionPoint_iface;
93     }else if(IsEqualGUID(&DIID_DWebBrowserEvents, riid)) {
94         TRACE("(%p)->(DIID_DWebBrowserEvents %p)\n", This, ppCP);
95         *ppCP = &This->wbe->IConnectionPoint_iface;
96     }else if(IsEqualGUID(&IID_IPropertyNotifySink, riid)) {
97         TRACE("(%p)->(IID_IPropertyNotifySink %p)\n", This, ppCP);
98         *ppCP = &This->pns->IConnectionPoint_iface;
99     }
100
101     if(*ppCP) {
102         IConnectionPoint_AddRef(*ppCP);
103         return S_OK;
104     }
105
106     WARN("Unsupported IID %s\n", debugstr_guid(riid));
107     return CONNECT_E_NOCONNECTION;
108 }
109
110 #undef impl_from_IConnectionPointContainer
111
112 static const IConnectionPointContainerVtbl ConnectionPointContainerVtbl =
113 {
114     ConnectionPointContainer_QueryInterface,
115     ConnectionPointContainer_AddRef,
116     ConnectionPointContainer_Release,
117     ConnectionPointContainer_EnumConnectionPoints,
118     ConnectionPointContainer_FindConnectionPoint
119 };
120
121
122 /**********************************************************************
123  * Implement the IConnectionPoint interface
124  */
125
126 static inline ConnectionPoint *impl_from_IConnectionPoint(IConnectionPoint *iface)
127 {
128     return CONTAINING_RECORD(iface, ConnectionPoint, IConnectionPoint_iface);
129 }
130
131 static HRESULT WINAPI ConnectionPoint_QueryInterface(IConnectionPoint *iface,
132                                                      REFIID riid, LPVOID *ppv)
133 {
134     ConnectionPoint *This = impl_from_IConnectionPoint(iface);
135
136     *ppv = NULL;
137
138     if(IsEqualGUID(&IID_IUnknown, riid)) {
139         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
140         *ppv = &This->IConnectionPoint_iface;
141     }else if(IsEqualGUID(&IID_IConnectionPoint, riid)) {
142         TRACE("(%p)->(IID_IConnectionPoint %p)\n", This, ppv);
143         *ppv = &This->IConnectionPoint_iface;
144     }
145
146     if(*ppv) {
147         IConnectionPointContainer_AddRef(This->container);
148         return S_OK;
149     }
150
151     WARN("Unsupported interface %s\n", debugstr_guid(riid));
152     return E_NOINTERFACE;
153 }
154
155 static ULONG WINAPI ConnectionPoint_AddRef(IConnectionPoint *iface)
156 {
157     ConnectionPoint *This = impl_from_IConnectionPoint(iface);
158     return IConnectionPointContainer_AddRef(This->container);
159 }
160
161 static ULONG WINAPI ConnectionPoint_Release(IConnectionPoint *iface)
162 {
163     ConnectionPoint *This = impl_from_IConnectionPoint(iface);
164     return IConnectionPointContainer_Release(This->container);
165 }
166
167 static HRESULT WINAPI ConnectionPoint_GetConnectionInterface(IConnectionPoint *iface, IID *pIID)
168 {
169     ConnectionPoint *This = impl_from_IConnectionPoint(iface);
170
171     TRACE("(%p)->(%p)\n", This, pIID);
172
173     *pIID = This->iid;
174     return S_OK;
175 }
176
177 static HRESULT WINAPI ConnectionPoint_GetConnectionPointContainer(IConnectionPoint *iface,
178         IConnectionPointContainer **ppCPC)
179 {
180     ConnectionPoint *This = impl_from_IConnectionPoint(iface);
181
182     TRACE("(%p)->(%p)\n", This, ppCPC);
183
184     *ppCPC = This->container;
185     IConnectionPointContainer_AddRef(This->container);
186     return S_OK;
187 }
188
189 static HRESULT WINAPI ConnectionPoint_Advise(IConnectionPoint *iface, IUnknown *pUnkSink,
190                                              DWORD *pdwCookie)
191 {
192     ConnectionPoint *This = impl_from_IConnectionPoint(iface);
193     IDispatch *disp;
194     DWORD i;
195     HRESULT hres;
196
197     TRACE("(%p)->(%p %p)\n", This, pUnkSink, pdwCookie);
198
199     hres = IUnknown_QueryInterface(pUnkSink, &This->iid, (void**)&disp);
200     if(FAILED(hres)) {
201         hres = IUnknown_QueryInterface(pUnkSink, &IID_IDispatch, (void**)&disp);
202         if(FAILED(hres))
203             return CONNECT_E_CANNOTCONNECT;
204     }
205
206     if(This->sinks) {
207         for(i=0; i<This->sinks_size; i++) {
208             if(!This->sinks[i])
209                 break;
210         }
211
212         if(i == This->sinks_size)
213             This->sinks = heap_realloc(This->sinks,
214                                           (++This->sinks_size)*sizeof(*This->sinks));
215     }else {
216         This->sinks = heap_alloc(sizeof(*This->sinks));
217         This->sinks_size = 1;
218         i = 0;
219     }
220
221     This->sinks[i] = disp;
222     *pdwCookie = i+1;
223
224     return S_OK;
225 }
226
227 static HRESULT WINAPI ConnectionPoint_Unadvise(IConnectionPoint *iface, DWORD dwCookie)
228 {
229     ConnectionPoint *This = impl_from_IConnectionPoint(iface);
230
231     TRACE("(%p)->(%d)\n", This, dwCookie);
232
233     if(!dwCookie || dwCookie > This->sinks_size || !This->sinks[dwCookie-1])
234         return CONNECT_E_NOCONNECTION;
235
236     IDispatch_Release(This->sinks[dwCookie-1]);
237     This->sinks[dwCookie-1] = NULL;
238
239     return S_OK;
240 }
241
242 static HRESULT WINAPI ConnectionPoint_EnumConnections(IConnectionPoint *iface,
243                                                       IEnumConnections **ppEnum)
244 {
245     ConnectionPoint *This = impl_from_IConnectionPoint(iface);
246     FIXME("(%p)->(%p)\n", This, ppEnum);
247     return E_NOTIMPL;
248 }
249
250 static const IConnectionPointVtbl ConnectionPointVtbl =
251 {
252     ConnectionPoint_QueryInterface,
253     ConnectionPoint_AddRef,
254     ConnectionPoint_Release,
255     ConnectionPoint_GetConnectionInterface,
256     ConnectionPoint_GetConnectionPointContainer,
257     ConnectionPoint_Advise,
258     ConnectionPoint_Unadvise,
259     ConnectionPoint_EnumConnections
260 };
261
262 void call_sink(ConnectionPoint *This, DISPID dispid, DISPPARAMS *dispparams)
263 {
264     DWORD i;
265
266     for(i=0; i<This->sinks_size; i++) {
267         if(This->sinks[i])
268             IDispatch_Invoke(This->sinks[i], dispid, &IID_NULL, LOCALE_SYSTEM_DEFAULT,
269                              DISPATCH_METHOD, dispparams, NULL, NULL, NULL);
270     }
271 }
272
273 static void ConnectionPoint_Create(REFIID riid, ConnectionPoint **cp,
274                                    IConnectionPointContainer *container)
275 {
276     ConnectionPoint *ret = heap_alloc(sizeof(ConnectionPoint));
277
278     ret->IConnectionPoint_iface.lpVtbl = &ConnectionPointVtbl;
279
280     ret->sinks = NULL;
281     ret->sinks_size = 0;
282     ret->container = container;
283
284     ret->iid = *riid;
285
286     *cp = ret;
287 }
288
289 static void ConnectionPoint_Destroy(ConnectionPoint *This)
290 {
291     DWORD i;
292
293     for(i=0; i<This->sinks_size; i++) {
294         if(This->sinks[i])
295             IDispatch_Release(This->sinks[i]);
296     }
297
298     heap_free(This->sinks);
299     heap_free(This);
300 }
301
302 void ConnectionPointContainer_Init(ConnectionPointContainer *This, IUnknown *impl)
303 {
304     This->IConnectionPointContainer_iface.lpVtbl = &ConnectionPointContainerVtbl;
305
306     ConnectionPoint_Create(&DIID_DWebBrowserEvents2, &This->wbe2, &This->IConnectionPointContainer_iface);
307     ConnectionPoint_Create(&DIID_DWebBrowserEvents,  &This->wbe,  &This->IConnectionPointContainer_iface);
308     ConnectionPoint_Create(&IID_IPropertyNotifySink, &This->pns,  &This->IConnectionPointContainer_iface);
309
310     This->impl = impl;
311 }
312
313 void ConnectionPointContainer_Destroy(ConnectionPointContainer *This)
314 {
315     ConnectionPoint_Destroy(This->wbe2);
316     ConnectionPoint_Destroy(This->wbe);
317     ConnectionPoint_Destroy(This->pns);
318 }