riched20: Use ME_PointFromChar to calculate the caret position.
[wine] / dlls / ieframe / 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
27 #include "ieframe.h"
28
29 #include "wine/debug.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(ieframe);
32
33 struct ConnectionPoint {
34     IConnectionPoint IConnectionPoint_iface;
35
36     IConnectionPointContainer *container;
37
38     IDispatch **sinks;
39     DWORD sinks_size;
40
41     IID iid;
42 };
43
44 /**********************************************************************
45  * Implement the IConnectionPointContainer interface
46  */
47
48 static inline ConnectionPointContainer *impl_from_IConnectionPointContainer(IConnectionPointContainer *iface)
49 {
50     return CONTAINING_RECORD(iface, ConnectionPointContainer, IConnectionPointContainer_iface);
51 }
52
53 static HRESULT WINAPI ConnectionPointContainer_QueryInterface(IConnectionPointContainer *iface,
54         REFIID riid, LPVOID *ppv)
55 {
56     ConnectionPointContainer *This = impl_from_IConnectionPointContainer(iface);
57     return IUnknown_QueryInterface(This->impl, riid, ppv);
58 }
59
60 static ULONG WINAPI ConnectionPointContainer_AddRef(IConnectionPointContainer *iface)
61 {
62     ConnectionPointContainer *This = impl_from_IConnectionPointContainer(iface);
63     return IUnknown_AddRef(This->impl);
64 }
65
66 static ULONG WINAPI ConnectionPointContainer_Release(IConnectionPointContainer *iface)
67 {
68     ConnectionPointContainer *This = impl_from_IConnectionPointContainer(iface);
69     return IUnknown_Release(This->impl);
70 }
71
72 static HRESULT WINAPI ConnectionPointContainer_EnumConnectionPoints(IConnectionPointContainer *iface,
73         LPENUMCONNECTIONPOINTS *ppEnum)
74 {
75     ConnectionPointContainer *This = impl_from_IConnectionPointContainer(iface);
76     FIXME("(%p)->(%p)\n", This, ppEnum);
77     return E_NOTIMPL;
78 }
79
80 static HRESULT WINAPI ConnectionPointContainer_FindConnectionPoint(IConnectionPointContainer *iface,
81         REFIID riid, LPCONNECTIONPOINT *ppCP)
82 {
83     ConnectionPointContainer *This = impl_from_IConnectionPointContainer(iface);
84
85     if(!ppCP) {
86         WARN("ppCP == NULL\n");
87         return E_POINTER;
88     }
89
90     *ppCP = NULL;
91
92     if(IsEqualGUID(&DIID_DWebBrowserEvents2, riid)) {
93         TRACE("(%p)->(DIID_DWebBrowserEvents2 %p)\n", This, ppCP);
94         *ppCP = &This->wbe2->IConnectionPoint_iface;
95     }else if(IsEqualGUID(&DIID_DWebBrowserEvents, riid)) {
96         TRACE("(%p)->(DIID_DWebBrowserEvents %p)\n", This, ppCP);
97         *ppCP = &This->wbe->IConnectionPoint_iface;
98     }else if(IsEqualGUID(&IID_IPropertyNotifySink, riid)) {
99         TRACE("(%p)->(IID_IPropertyNotifySink %p)\n", This, ppCP);
100         *ppCP = &This->pns->IConnectionPoint_iface;
101     }
102
103     if(*ppCP) {
104         IConnectionPoint_AddRef(*ppCP);
105         return S_OK;
106     }
107
108     WARN("Unsupported IID %s\n", debugstr_guid(riid));
109     return CONNECT_E_NOCONNECTION;
110 }
111
112 #undef impl_from_IConnectionPointContainer
113
114 static const IConnectionPointContainerVtbl ConnectionPointContainerVtbl =
115 {
116     ConnectionPointContainer_QueryInterface,
117     ConnectionPointContainer_AddRef,
118     ConnectionPointContainer_Release,
119     ConnectionPointContainer_EnumConnectionPoints,
120     ConnectionPointContainer_FindConnectionPoint
121 };
122
123
124 /**********************************************************************
125  * Implement the IConnectionPoint interface
126  */
127
128 static inline ConnectionPoint *impl_from_IConnectionPoint(IConnectionPoint *iface)
129 {
130     return CONTAINING_RECORD(iface, ConnectionPoint, IConnectionPoint_iface);
131 }
132
133 static HRESULT WINAPI ConnectionPoint_QueryInterface(IConnectionPoint *iface,
134                                                      REFIID riid, LPVOID *ppv)
135 {
136     ConnectionPoint *This = impl_from_IConnectionPoint(iface);
137
138     *ppv = NULL;
139
140     if(IsEqualGUID(&IID_IUnknown, riid)) {
141         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
142         *ppv = &This->IConnectionPoint_iface;
143     }else if(IsEqualGUID(&IID_IConnectionPoint, riid)) {
144         TRACE("(%p)->(IID_IConnectionPoint %p)\n", This, ppv);
145         *ppv = &This->IConnectionPoint_iface;
146     }
147
148     if(*ppv) {
149         IConnectionPointContainer_AddRef(This->container);
150         return S_OK;
151     }
152
153     WARN("Unsupported interface %s\n", debugstr_guid(riid));
154     return E_NOINTERFACE;
155 }
156
157 static ULONG WINAPI ConnectionPoint_AddRef(IConnectionPoint *iface)
158 {
159     ConnectionPoint *This = impl_from_IConnectionPoint(iface);
160     return IConnectionPointContainer_AddRef(This->container);
161 }
162
163 static ULONG WINAPI ConnectionPoint_Release(IConnectionPoint *iface)
164 {
165     ConnectionPoint *This = impl_from_IConnectionPoint(iface);
166     return IConnectionPointContainer_Release(This->container);
167 }
168
169 static HRESULT WINAPI ConnectionPoint_GetConnectionInterface(IConnectionPoint *iface, IID *pIID)
170 {
171     ConnectionPoint *This = impl_from_IConnectionPoint(iface);
172
173     TRACE("(%p)->(%p)\n", This, pIID);
174
175     *pIID = This->iid;
176     return S_OK;
177 }
178
179 static HRESULT WINAPI ConnectionPoint_GetConnectionPointContainer(IConnectionPoint *iface,
180         IConnectionPointContainer **ppCPC)
181 {
182     ConnectionPoint *This = impl_from_IConnectionPoint(iface);
183
184     TRACE("(%p)->(%p)\n", This, ppCPC);
185
186     *ppCPC = This->container;
187     IConnectionPointContainer_AddRef(This->container);
188     return S_OK;
189 }
190
191 static HRESULT WINAPI ConnectionPoint_Advise(IConnectionPoint *iface, IUnknown *pUnkSink,
192                                              DWORD *pdwCookie)
193 {
194     ConnectionPoint *This = impl_from_IConnectionPoint(iface);
195     IDispatch *disp;
196     DWORD i;
197     HRESULT hres;
198
199     TRACE("(%p)->(%p %p)\n", This, pUnkSink, pdwCookie);
200
201     hres = IUnknown_QueryInterface(pUnkSink, &This->iid, (void**)&disp);
202     if(FAILED(hres)) {
203         hres = IUnknown_QueryInterface(pUnkSink, &IID_IDispatch, (void**)&disp);
204         if(FAILED(hres))
205             return CONNECT_E_CANNOTCONNECT;
206     }
207
208     if(This->sinks) {
209         for(i=0; i<This->sinks_size; i++) {
210             if(!This->sinks[i])
211                 break;
212         }
213
214         if(i == This->sinks_size)
215             This->sinks = heap_realloc(This->sinks,
216                                           (++This->sinks_size)*sizeof(*This->sinks));
217     }else {
218         This->sinks = heap_alloc(sizeof(*This->sinks));
219         This->sinks_size = 1;
220         i = 0;
221     }
222
223     This->sinks[i] = disp;
224     *pdwCookie = i+1;
225
226     return S_OK;
227 }
228
229 static HRESULT WINAPI ConnectionPoint_Unadvise(IConnectionPoint *iface, DWORD dwCookie)
230 {
231     ConnectionPoint *This = impl_from_IConnectionPoint(iface);
232
233     TRACE("(%p)->(%d)\n", This, dwCookie);
234
235     if(!dwCookie || dwCookie > This->sinks_size || !This->sinks[dwCookie-1])
236         return CONNECT_E_NOCONNECTION;
237
238     IDispatch_Release(This->sinks[dwCookie-1]);
239     This->sinks[dwCookie-1] = NULL;
240
241     return S_OK;
242 }
243
244 static HRESULT WINAPI ConnectionPoint_EnumConnections(IConnectionPoint *iface,
245                                                       IEnumConnections **ppEnum)
246 {
247     ConnectionPoint *This = impl_from_IConnectionPoint(iface);
248     FIXME("(%p)->(%p)\n", This, ppEnum);
249     return E_NOTIMPL;
250 }
251
252 static const IConnectionPointVtbl ConnectionPointVtbl =
253 {
254     ConnectionPoint_QueryInterface,
255     ConnectionPoint_AddRef,
256     ConnectionPoint_Release,
257     ConnectionPoint_GetConnectionInterface,
258     ConnectionPoint_GetConnectionPointContainer,
259     ConnectionPoint_Advise,
260     ConnectionPoint_Unadvise,
261     ConnectionPoint_EnumConnections
262 };
263
264 void call_sink(ConnectionPoint *This, DISPID dispid, DISPPARAMS *dispparams)
265 {
266     DWORD i;
267
268     for(i=0; i<This->sinks_size; i++) {
269         if(This->sinks[i])
270             IDispatch_Invoke(This->sinks[i], dispid, &IID_NULL, LOCALE_SYSTEM_DEFAULT,
271                              DISPATCH_METHOD, dispparams, NULL, NULL, NULL);
272     }
273 }
274
275 static void ConnectionPoint_Create(REFIID riid, ConnectionPoint **cp,
276                                    IConnectionPointContainer *container)
277 {
278     ConnectionPoint *ret = heap_alloc(sizeof(ConnectionPoint));
279
280     ret->IConnectionPoint_iface.lpVtbl = &ConnectionPointVtbl;
281
282     ret->sinks = NULL;
283     ret->sinks_size = 0;
284     ret->container = container;
285
286     ret->iid = *riid;
287
288     *cp = ret;
289 }
290
291 static void ConnectionPoint_Destroy(ConnectionPoint *This)
292 {
293     DWORD i;
294
295     for(i=0; i<This->sinks_size; i++) {
296         if(This->sinks[i])
297             IDispatch_Release(This->sinks[i]);
298     }
299
300     heap_free(This->sinks);
301     heap_free(This);
302 }
303
304 void ConnectionPointContainer_Init(ConnectionPointContainer *This, IUnknown *impl)
305 {
306     This->IConnectionPointContainer_iface.lpVtbl = &ConnectionPointContainerVtbl;
307
308     ConnectionPoint_Create(&DIID_DWebBrowserEvents2, &This->wbe2, &This->IConnectionPointContainer_iface);
309     ConnectionPoint_Create(&DIID_DWebBrowserEvents,  &This->wbe,  &This->IConnectionPointContainer_iface);
310     ConnectionPoint_Create(&IID_IPropertyNotifySink, &This->pns,  &This->IConnectionPointContainer_iface);
311
312     This->impl = impl;
313 }
314
315 void ConnectionPointContainer_Destroy(ConnectionPointContainer *This)
316 {
317     ConnectionPoint_Destroy(This->wbe2);
318     ConnectionPoint_Destroy(This->wbe);
319     ConnectionPoint_Destroy(This->pns);
320 }