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