conpoint.c 9.16 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright 2006 Jacek Caban for CodeWeavers
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
16
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 */

#include <stdarg.h>

#define COBJMACROS

#include "windef.h"
#include "winbase.h"
#include "winuser.h"
#include "ole2.h"

#include "wine/debug.h"

#include "mshtml_private.h"

WINE_DEFAULT_DEBUG_CHANNEL(mshtml);

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
static const char *debugstr_cp_guid(REFIID riid)
{
#define X(x) \
    if(IsEqualGUID(riid, &x)) \
        return #x

    X(IID_IPropertyNotifySink);
    X(DIID_HTMLDocumentEvents);
    X(DIID_HTMLDocumentEvents2);
    X(DIID_HTMLTableEvents);
    X(DIID_HTMLTextContainerEvents);

#undef X

    return debugstr_guid(riid);
}

51 52 53 54 55 56 57 58 59 60
void call_property_onchanged(ConnectionPoint *This, DISPID dispid)
{
    DWORD i;

    for(i=0; i<This->sinks_size; i++) {
        if(This->sinks[i].propnotif)
            IPropertyNotifySink_OnChanged(This->sinks[i].propnotif, dispid);
    }
}

61 62 63 64
static inline ConnectionPoint *impl_from_IConnectionPoint(IConnectionPoint *iface)
{
    return CONTAINING_RECORD(iface, ConnectionPoint, IConnectionPoint_iface);
}
65 66 67 68

static HRESULT WINAPI ConnectionPoint_QueryInterface(IConnectionPoint *iface,
                                                     REFIID riid, LPVOID *ppv)
{
69
    ConnectionPoint *This = impl_from_IConnectionPoint(iface);
70 71 72 73 74

    *ppv = NULL;

    if(IsEqualGUID(&IID_IUnknown, riid)) {
        TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
75
        *ppv = &This->IConnectionPoint_iface;
76 77
    }else if(IsEqualGUID(&IID_IConnectionPoint, riid)) {
        TRACE("(%p)->(IID_IConnectionPoint %p)\n", This, ppv);
78
        *ppv = &This->IConnectionPoint_iface;
79 80 81 82 83 84 85 86 87 88 89 90 91
    }

    if(*ppv) {
        IUnknown_AddRef((IUnknown*)*ppv);
        return S_OK;
    }

    WARN("Unsupported interface %s\n", debugstr_guid(riid));
    return E_NOINTERFACE;
}

static ULONG WINAPI ConnectionPoint_AddRef(IConnectionPoint *iface)
{
92 93
    ConnectionPoint *This = impl_from_IConnectionPoint(iface);
    return IConnectionPointContainer_AddRef(&This->container->IConnectionPointContainer_iface);
94 95 96 97
}

static ULONG WINAPI ConnectionPoint_Release(IConnectionPoint *iface)
{
98 99
    ConnectionPoint *This = impl_from_IConnectionPoint(iface);
    return IConnectionPointContainer_Release(&This->container->IConnectionPointContainer_iface);
100 101 102 103
}

static HRESULT WINAPI ConnectionPoint_GetConnectionInterface(IConnectionPoint *iface, IID *pIID)
{
104
    ConnectionPoint *This = impl_from_IConnectionPoint(iface);
105 106 107 108 109 110

    TRACE("(%p)->(%p)\n", This, pIID);

    if(!pIID)
        return E_POINTER;

111
    *pIID = *This->iid;
112
    return S_OK;
113 114 115 116 117
}

static HRESULT WINAPI ConnectionPoint_GetConnectionPointContainer(IConnectionPoint *iface,
        IConnectionPointContainer **ppCPC)
{
118
    ConnectionPoint *This = impl_from_IConnectionPoint(iface);
119 120 121 122 123 124

    TRACE("(%p)->(%p)\n", This, ppCPC);

    if(!ppCPC)
        return E_POINTER;

125
    *ppCPC = &This->container->IConnectionPointContainer_iface;
126 127
    IConnectionPointContainer_AddRef(*ppCPC);
    return S_OK;
128 129 130 131 132
}

static HRESULT WINAPI ConnectionPoint_Advise(IConnectionPoint *iface, IUnknown *pUnkSink,
                                             DWORD *pdwCookie)
{
133
    ConnectionPoint *This = impl_from_IConnectionPoint(iface);
134 135 136 137 138 139
    IUnknown *sink;
    DWORD i;
    HRESULT hres;

    TRACE("(%p)->(%p %p)\n", This, pUnkSink, pdwCookie);

140 141
    hres = IUnknown_QueryInterface(pUnkSink, This->iid, (void**)&sink);
    if(FAILED(hres) && !IsEqualGUID(&IID_IPropertyNotifySink, This->iid))
142
        hres = IUnknown_QueryInterface(pUnkSink, &IID_IDispatch, (void**)&sink);
143 144
    if(FAILED(hres))
        return CONNECT_E_CANNOTCONNECT;
145 146 147 148 149 150 151 152

    if(This->sinks) {
        for(i=0; i<This->sinks_size; i++) {
            if(!This->sinks[i].unk)
                break;
        }

        if(i == This->sinks_size)
153
            This->sinks = heap_realloc(This->sinks,(++This->sinks_size)*sizeof(*This->sinks));
154
    }else {
155
        This->sinks = heap_alloc(sizeof(*This->sinks));
156 157 158 159 160
        This->sinks_size = 1;
        i = 0;
    }

    This->sinks[i].unk = sink;
161 162
    if(pdwCookie)
        *pdwCookie = i+1;
163

164 165 166
    if(!i && This->data && This->data->on_advise)
        This->data->on_advise(This->container->outer, This->data);

167
    return S_OK;
168 169 170 171
}

static HRESULT WINAPI ConnectionPoint_Unadvise(IConnectionPoint *iface, DWORD dwCookie)
{
172
    ConnectionPoint *This = impl_from_IConnectionPoint(iface);
173
    TRACE("(%p)->(%d)\n", This, dwCookie);
174 175 176 177 178 179 180 181

    if(!dwCookie || dwCookie > This->sinks_size || !This->sinks[dwCookie-1].unk)
        return CONNECT_E_NOCONNECTION;

    IUnknown_Release(This->sinks[dwCookie-1].unk);
    This->sinks[dwCookie-1].unk = NULL;

    return S_OK;
182 183 184 185 186
}

static HRESULT WINAPI ConnectionPoint_EnumConnections(IConnectionPoint *iface,
                                                      IEnumConnections **ppEnum)
{
187
    ConnectionPoint *This = impl_from_IConnectionPoint(iface);
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
    FIXME("(%p)->(%p)\n", This, ppEnum);
    return E_NOTIMPL;
}

static const IConnectionPointVtbl ConnectionPointVtbl =
{
    ConnectionPoint_QueryInterface,
    ConnectionPoint_AddRef,
    ConnectionPoint_Release,
    ConnectionPoint_GetConnectionInterface,
    ConnectionPoint_GetConnectionPointContainer,
    ConnectionPoint_Advise,
    ConnectionPoint_Unadvise,
    ConnectionPoint_EnumConnections
};

204
void ConnectionPoint_Init(ConnectionPoint *cp, ConnectionPointContainer *container, REFIID riid, cp_static_data_t *data)
205
{
206
    cp->IConnectionPoint_iface.lpVtbl = &ConnectionPointVtbl;
207
    cp->container = container;
208 209
    cp->sinks = NULL;
    cp->sinks_size = 0;
210
    cp->iid = riid;
211
    cp->data = data;
212

213 214
    cp->next = container->cp_list;
    container->cp_list = cp;
215 216
}

217 218
static void ConnectionPoint_Destroy(ConnectionPoint *This)
{
219
    DWORD i;
220 221 222 223 224 225

    for(i=0; i<This->sinks_size; i++) {
        if(This->sinks[i].unk)
            IUnknown_Release(This->sinks[i].unk);
    }

226
    heap_free(This->sinks);
227 228
}

229 230 231 232
static inline ConnectionPointContainer *impl_from_IConnectionPointContainer(IConnectionPointContainer *iface)
{
    return CONTAINING_RECORD(iface, ConnectionPointContainer, IConnectionPointContainer_iface);
}
233 234 235 236

static HRESULT WINAPI ConnectionPointContainer_QueryInterface(IConnectionPointContainer *iface,
                                                              REFIID riid, void **ppv)
{
237
    ConnectionPointContainer *This = impl_from_IConnectionPointContainer(iface);
238
    return IUnknown_QueryInterface(This->outer, riid, ppv);
239 240 241 242
}

static ULONG WINAPI ConnectionPointContainer_AddRef(IConnectionPointContainer *iface)
{
243
    ConnectionPointContainer *This = impl_from_IConnectionPointContainer(iface);
244
    return IUnknown_AddRef(This->outer);
245 246 247 248
}

static ULONG WINAPI ConnectionPointContainer_Release(IConnectionPointContainer *iface)
{
249
    ConnectionPointContainer *This = impl_from_IConnectionPointContainer(iface);
250
    return IUnknown_Release(This->outer);
251 252 253 254 255
}

static HRESULT WINAPI ConnectionPointContainer_EnumConnectionPoints(IConnectionPointContainer *iface,
        IEnumConnectionPoints **ppEnum)
{
256
    ConnectionPointContainer *This = impl_from_IConnectionPointContainer(iface);
257 258 259 260 261 262 263
    FIXME("(%p)->(%p)\n", This, ppEnum);
    return E_NOTIMPL;
}

static HRESULT WINAPI ConnectionPointContainer_FindConnectionPoint(IConnectionPointContainer *iface,
        REFIID riid, IConnectionPoint **ppCP)
{
264
    ConnectionPointContainer *This = impl_from_IConnectionPointContainer(iface);
265 266
    ConnectionPoint *iter;

267
    TRACE("(%p)->(%s %p)\n", This, debugstr_cp_guid(riid), ppCP);
268

269
    if(This->forward_container)
270
        return IConnectionPointContainer_FindConnectionPoint(&This->forward_container->IConnectionPointContainer_iface,
271
                riid, ppCP);
272

273 274
    *ppCP = NULL;

275
    for(iter = This->cp_list; iter; iter = iter->next) {
276
        if(IsEqualGUID(iter->iid, riid))
277
            *ppCP = &iter->IConnectionPoint_iface;
278 279 280 281 282 283 284
    }

    if(*ppCP) {
        IConnectionPoint_AddRef(*ppCP);
        return S_OK;
    }

285
    FIXME("unsupported riid %s\n", debugstr_cp_guid(riid));
286
    return CONNECT_E_NOCONNECTION;
287 288 289 290 291 292 293 294 295 296
}

static const IConnectionPointContainerVtbl ConnectionPointContainerVtbl = {
    ConnectionPointContainer_QueryInterface,
    ConnectionPointContainer_AddRef,
    ConnectionPointContainer_Release,
    ConnectionPointContainer_EnumConnectionPoints,
    ConnectionPointContainer_FindConnectionPoint
};

297
void ConnectionPointContainer_Init(ConnectionPointContainer *This, IUnknown *outer)
298
{
299
    This->IConnectionPointContainer_iface.lpVtbl = &ConnectionPointContainerVtbl;
300
    This->cp_list = NULL;
301
    This->outer = outer;
302
}
303

304
void ConnectionPointContainer_Destroy(ConnectionPointContainer *This)
305
{
306
    ConnectionPoint *iter = This->cp_list;
307 308 309 310 311

    while(iter) {
        ConnectionPoint_Destroy(iter);
        iter = iter->next;
    }
312
}