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