Release 1.1.37.
[wine/gsoc-2012-control.git] / dlls / urlmon / format.c
blobbf9dda060343b162ddb7185fcfbb3ea4b521a885
1 /*
2 * Copyright 2005 Jacek Caban
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * 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
19 #include "urlmon_main.h"
20 #include "wine/debug.h"
22 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
24 static WCHAR wszEnumFORMATETC[] = {'_','E','n','u','m','F','O','R','M','A','T','E','T','C','_',0};
26 typedef struct {
27 const IEnumFORMATETCVtbl *lpEnumFORMATETCVtbl;
29 FORMATETC *fetc;
30 UINT fetc_cnt;
31 UINT it;
33 LONG ref;
34 } EnumFORMATETC;
36 static IEnumFORMATETC *EnumFORMATETC_Create(UINT cfmtetc, const FORMATETC *rgfmtetc, UINT it);
38 #define ENUMF_THIS(iface) DEFINE_THIS(EnumFORMATETC, EnumFORMATETC, iface)
40 static HRESULT WINAPI EnumFORMATETC_QueryInterface(IEnumFORMATETC *iface, REFIID riid, void **ppv)
42 EnumFORMATETC *This = ENUMF_THIS(iface);
44 TRACE("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
46 *ppv = NULL;
48 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IEnumFORMATETC, riid)) {
49 IEnumFORMATETC_AddRef(iface);
50 *ppv = iface;
51 return S_OK;
54 WARN("not supported interface %s\n", debugstr_guid(riid));
55 return E_NOINTERFACE;
58 static ULONG WINAPI EnumFORMATETC_AddRef(IEnumFORMATETC *iface)
60 EnumFORMATETC *This = ENUMF_THIS(iface);
61 LONG ref = InterlockedIncrement(&This->ref);
62 TRACE("(%p) ref=%d\n", This, ref);
63 return ref;
66 static ULONG WINAPI EnumFORMATETC_Release(IEnumFORMATETC *iface)
68 EnumFORMATETC *This = ENUMF_THIS(iface);
69 LONG ref = InterlockedDecrement(&This->ref);
71 TRACE("(%p) ref=%d\n", This, ref);
73 if(!ref) {
74 heap_free(This->fetc);
75 heap_free(This);
77 URLMON_UnlockModule();
80 return ref;
83 static HRESULT WINAPI EnumFORMATETC_Next(IEnumFORMATETC *iface, ULONG celt,
84 FORMATETC *rgelt, ULONG *pceltFetched)
86 EnumFORMATETC *This = ENUMF_THIS(iface);
87 ULONG cnt;
89 TRACE("(%p)->(%d %p %p)\n", This, celt, rgelt, pceltFetched);
91 if(!rgelt)
92 return E_INVALIDARG;
94 if(This->it >= This->fetc_cnt || !celt) {
95 if(pceltFetched)
96 *pceltFetched = 0;
97 return celt ? S_FALSE : S_OK;
100 cnt = This->fetc_cnt-This->it > celt ? celt : This->fetc_cnt-This->it;
102 memcpy(rgelt, This->fetc+This->it, cnt*sizeof(FORMATETC));
103 This->it += cnt;
105 if(pceltFetched)
106 *pceltFetched = cnt;
108 return cnt == celt ? S_OK : S_FALSE;
111 static HRESULT WINAPI EnumFORMATETC_Skip(IEnumFORMATETC *iface, ULONG celt)
113 EnumFORMATETC *This = ENUMF_THIS(iface);
115 TRACE("(%p)->(%d)\n", This, celt);
117 This->it += celt;
118 return This->it > This->fetc_cnt ? S_FALSE : S_OK;
121 static HRESULT WINAPI EnumFORMATETC_Reset(IEnumFORMATETC *iface)
123 EnumFORMATETC *This = ENUMF_THIS(iface);
125 TRACE("(%p)\n", This);
127 This->it = 0;
128 return S_OK;
131 static HRESULT WINAPI EnumFORMATETC_Clone(IEnumFORMATETC *iface, IEnumFORMATETC **ppenum)
133 EnumFORMATETC *This = ENUMF_THIS(iface);
135 TRACE("(%p)->(%p)\n", This, ppenum);
137 if(!ppenum)
138 return E_INVALIDARG;
140 *ppenum = EnumFORMATETC_Create(This->fetc_cnt, This->fetc, This->it);
141 return S_OK;
144 static const IEnumFORMATETCVtbl EnumFORMATETCVtbl = {
145 EnumFORMATETC_QueryInterface,
146 EnumFORMATETC_AddRef,
147 EnumFORMATETC_Release,
148 EnumFORMATETC_Next,
149 EnumFORMATETC_Skip,
150 EnumFORMATETC_Reset,
151 EnumFORMATETC_Clone
154 static IEnumFORMATETC *EnumFORMATETC_Create(UINT cfmtetc, const FORMATETC *rgfmtetc, UINT it)
156 EnumFORMATETC *ret = heap_alloc(sizeof(EnumFORMATETC));
158 URLMON_LockModule();
160 ret->lpEnumFORMATETCVtbl = &EnumFORMATETCVtbl;
161 ret->ref = 1;
162 ret->it = it;
163 ret->fetc_cnt = cfmtetc;
165 ret->fetc = heap_alloc(cfmtetc*sizeof(FORMATETC));
166 memcpy(ret->fetc, rgfmtetc, cfmtetc*sizeof(FORMATETC));
168 return (IEnumFORMATETC*)ret;
171 /**********************************************************
172 * CreateFormatEnumerator (urlmon.@)
174 HRESULT WINAPI CreateFormatEnumerator(UINT cfmtetc, FORMATETC *rgfmtetc,
175 IEnumFORMATETC** ppenumfmtetc)
177 TRACE("(%d %p %p)\n", cfmtetc, rgfmtetc, ppenumfmtetc);
179 if(!ppenumfmtetc)
180 return E_INVALIDARG;
181 if(!cfmtetc)
182 return E_FAIL;
184 *ppenumfmtetc = EnumFORMATETC_Create(cfmtetc, rgfmtetc, 0);
185 return S_OK;
188 /**********************************************************
189 * RegisterFormatEnumerator (urlmon.@)
191 HRESULT WINAPI RegisterFormatEnumerator(LPBC pBC, IEnumFORMATETC *pEFetc, DWORD reserved)
193 TRACE("(%p %p %d)\n", pBC, pEFetc, reserved);
195 if(reserved)
196 WARN("reserved != 0\n");
198 if(!pBC || !pEFetc)
199 return E_INVALIDARG;
201 return IBindCtx_RegisterObjectParam(pBC, wszEnumFORMATETC, (IUnknown*)pEFetc);
204 /**********************************************************
205 * RevokeFormatEnumerator (urlmon.@)
207 HRESULT WINAPI RevokeFormatEnumerator(LPBC pbc, IEnumFORMATETC *pEFetc)
209 TRACE("(%p %p)\n", pbc, pEFetc);
211 if(!pbc)
212 return E_INVALIDARG;
214 return IBindCtx_RevokeObjectParam(pbc, wszEnumFORMATETC);