wmvcore: Print the debug string and not the pointer to it.
[wine.git] / dlls / mshtml / selection.c
blobdfbb386b93821c3170cbfee45711cb2944eda7be
1 /*
2 * Copyright 2006 Jacek Caban for CodeWeavers
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 <stdarg.h>
21 #define COBJMACROS
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "ole2.h"
28 #include "wine/debug.h"
30 #include "mshtml_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
34 typedef struct {
35 DispatchEx dispex;
36 IHTMLSelectionObject IHTMLSelectionObject_iface;
37 IHTMLSelectionObject2 IHTMLSelectionObject2_iface;
39 LONG ref;
41 nsISelection *nsselection;
42 HTMLDocumentNode *doc;
44 struct list entry;
45 } HTMLSelectionObject;
47 static inline HTMLSelectionObject *impl_from_IHTMLSelectionObject(IHTMLSelectionObject *iface)
49 return CONTAINING_RECORD(iface, HTMLSelectionObject, IHTMLSelectionObject_iface);
52 static HRESULT WINAPI HTMLSelectionObject_QueryInterface(IHTMLSelectionObject *iface,
53 REFIID riid, void **ppv)
55 HTMLSelectionObject *This = impl_from_IHTMLSelectionObject(iface);
57 TRACE("(%p)->(%s %p)\n", This, debugstr_mshtml_guid(riid), ppv);
59 if(IsEqualGUID(&IID_IUnknown, riid)) {
60 *ppv = &This->IHTMLSelectionObject_iface;
61 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
62 *ppv = &This->IHTMLSelectionObject_iface;
63 }else if(IsEqualGUID(&IID_IHTMLSelectionObject, riid)) {
64 *ppv = &This->IHTMLSelectionObject_iface;
65 }else if(IsEqualGUID(&IID_IHTMLSelectionObject2, riid)) {
66 *ppv = &This->IHTMLSelectionObject2_iface;
67 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
68 return *ppv ? S_OK : E_NOINTERFACE;
69 }else {
70 *ppv = NULL;
71 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
72 return E_NOINTERFACE;
75 IUnknown_AddRef((IUnknown*)*ppv);
76 return S_OK;
79 static ULONG WINAPI HTMLSelectionObject_AddRef(IHTMLSelectionObject *iface)
81 HTMLSelectionObject *This = impl_from_IHTMLSelectionObject(iface);
82 LONG ref = InterlockedIncrement(&This->ref);
84 TRACE("(%p) ref=%d\n", This, ref);
86 return ref;
89 static ULONG WINAPI HTMLSelectionObject_Release(IHTMLSelectionObject *iface)
91 HTMLSelectionObject *This = impl_from_IHTMLSelectionObject(iface);
92 LONG ref = InterlockedDecrement(&This->ref);
94 TRACE("(%p) ref=%d\n", This, ref);
96 if(!ref) {
97 if(This->nsselection)
98 nsISelection_Release(This->nsselection);
99 if(This->doc)
100 list_remove(&This->entry);
101 release_dispex(&This->dispex);
102 heap_free(This);
105 return ref;
108 static HRESULT WINAPI HTMLSelectionObject_GetTypeInfoCount(IHTMLSelectionObject *iface, UINT *pctinfo)
110 HTMLSelectionObject *This = impl_from_IHTMLSelectionObject(iface);
112 return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
115 static HRESULT WINAPI HTMLSelectionObject_GetTypeInfo(IHTMLSelectionObject *iface, UINT iTInfo,
116 LCID lcid, ITypeInfo **ppTInfo)
118 HTMLSelectionObject *This = impl_from_IHTMLSelectionObject(iface);
120 return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
123 static HRESULT WINAPI HTMLSelectionObject_GetIDsOfNames(IHTMLSelectionObject *iface, REFIID riid,
124 LPOLESTR *rgszNames, UINT cNames,
125 LCID lcid, DISPID *rgDispId)
127 HTMLSelectionObject *This = impl_from_IHTMLSelectionObject(iface);
129 return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames,
130 cNames, lcid, rgDispId);
133 static HRESULT WINAPI HTMLSelectionObject_Invoke(IHTMLSelectionObject *iface, DISPID dispIdMember,
134 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
135 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
137 HTMLSelectionObject *This = impl_from_IHTMLSelectionObject(iface);
140 return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid,
141 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
144 static HRESULT WINAPI HTMLSelectionObject_createRange(IHTMLSelectionObject *iface, IDispatch **range)
146 HTMLSelectionObject *This = impl_from_IHTMLSelectionObject(iface);
147 IHTMLTxtRange *range_obj = NULL;
148 nsIDOMRange *nsrange = NULL;
149 HRESULT hres;
151 TRACE("(%p)->(%p)\n", This, range);
153 if(This->nsselection) {
154 LONG nsrange_cnt = 0;
155 nsresult nsres;
157 nsISelection_GetRangeCount(This->nsselection, &nsrange_cnt);
158 if(!nsrange_cnt) {
159 nsIDOMHTMLElement *nsbody = NULL;
161 TRACE("nsrange_cnt = 0\n");
163 if(!This->doc->nsdoc) {
164 WARN("nsdoc is NULL\n");
165 return E_UNEXPECTED;
168 nsres = nsIDOMHTMLDocument_GetBody(This->doc->nsdoc, &nsbody);
169 if(NS_FAILED(nsres) || !nsbody) {
170 ERR("Could not get body: %08x\n", nsres);
171 return E_FAIL;
174 nsres = nsISelection_Collapse(This->nsselection, (nsIDOMNode*)nsbody, 0);
175 nsIDOMHTMLElement_Release(nsbody);
176 if(NS_FAILED(nsres))
177 ERR("Collapse failed: %08x\n", nsres);
178 }else if(nsrange_cnt > 1) {
179 FIXME("range_cnt = %d\n", nsrange_cnt);
182 nsres = nsISelection_GetRangeAt(This->nsselection, 0, &nsrange);
183 if(NS_FAILED(nsres))
184 ERR("GetRangeAt failed: %08x\n", nsres);
187 hres = HTMLTxtRange_Create(This->doc, nsrange, &range_obj);
189 if (nsrange) nsIDOMRange_Release(nsrange);
190 *range = (IDispatch*)range_obj;
191 return hres;
194 static HRESULT WINAPI HTMLSelectionObject_empty(IHTMLSelectionObject *iface)
196 HTMLSelectionObject *This = impl_from_IHTMLSelectionObject(iface);
197 FIXME("(%p)\n", This);
198 return E_NOTIMPL;
201 static HRESULT WINAPI HTMLSelectionObject_clear(IHTMLSelectionObject *iface)
203 HTMLSelectionObject *This = impl_from_IHTMLSelectionObject(iface);
204 FIXME("(%p)\n", This);
205 return E_NOTIMPL;
208 static HRESULT WINAPI HTMLSelectionObject_get_type(IHTMLSelectionObject *iface, BSTR *p)
210 HTMLSelectionObject *This = impl_from_IHTMLSelectionObject(iface);
211 cpp_bool collapsed = TRUE;
213 static const WCHAR wszNone[] = {'N','o','n','e',0};
214 static const WCHAR wszText[] = {'T','e','x','t',0};
216 TRACE("(%p)->(%p)\n", This, p);
218 if(This->nsselection)
219 nsISelection_GetIsCollapsed(This->nsselection, &collapsed);
221 *p = SysAllocString(collapsed ? wszNone : wszText); /* FIXME: control */
222 TRACE("ret %s\n", debugstr_w(*p));
223 return S_OK;
226 static const IHTMLSelectionObjectVtbl HTMLSelectionObjectVtbl = {
227 HTMLSelectionObject_QueryInterface,
228 HTMLSelectionObject_AddRef,
229 HTMLSelectionObject_Release,
230 HTMLSelectionObject_GetTypeInfoCount,
231 HTMLSelectionObject_GetTypeInfo,
232 HTMLSelectionObject_GetIDsOfNames,
233 HTMLSelectionObject_Invoke,
234 HTMLSelectionObject_createRange,
235 HTMLSelectionObject_empty,
236 HTMLSelectionObject_clear,
237 HTMLSelectionObject_get_type
240 static inline HTMLSelectionObject *impl_from_IHTMLSelectionObject2(IHTMLSelectionObject2 *iface)
242 return CONTAINING_RECORD(iface, HTMLSelectionObject, IHTMLSelectionObject2_iface);
245 static HRESULT WINAPI HTMLSelectionObject2_QueryInterface(IHTMLSelectionObject2 *iface, REFIID riid, void **ppv)
247 HTMLSelectionObject *This = impl_from_IHTMLSelectionObject2(iface);
249 return IHTMLSelectionObject_QueryInterface(&This->IHTMLSelectionObject_iface, riid, ppv);
252 static ULONG WINAPI HTMLSelectionObject2_AddRef(IHTMLSelectionObject2 *iface)
254 HTMLSelectionObject *This = impl_from_IHTMLSelectionObject2(iface);
256 return IHTMLSelectionObject_AddRef(&This->IHTMLSelectionObject_iface);
259 static ULONG WINAPI HTMLSelectionObject2_Release(IHTMLSelectionObject2 *iface)
261 HTMLSelectionObject *This = impl_from_IHTMLSelectionObject2(iface);
263 return IHTMLSelectionObject_Release(&This->IHTMLSelectionObject_iface);
266 static HRESULT WINAPI HTMLSelectionObject2_GetTypeInfoCount(IHTMLSelectionObject2 *iface, UINT *pctinfo)
268 HTMLSelectionObject *This = impl_from_IHTMLSelectionObject2(iface);
270 return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
273 static HRESULT WINAPI HTMLSelectionObject2_GetTypeInfo(IHTMLSelectionObject2 *iface, UINT iTInfo,
274 LCID lcid, ITypeInfo **ppTInfo)
276 HTMLSelectionObject *This = impl_from_IHTMLSelectionObject2(iface);
278 return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
281 static HRESULT WINAPI HTMLSelectionObject2_GetIDsOfNames(IHTMLSelectionObject2 *iface, REFIID riid,
282 LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId)
284 HTMLSelectionObject *This = impl_from_IHTMLSelectionObject2(iface);
286 return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames,
287 cNames, lcid, rgDispId);
290 static HRESULT WINAPI HTMLSelectionObject2_Invoke(IHTMLSelectionObject2 *iface, DISPID dispIdMember,
291 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult,
292 EXCEPINFO *pExcepInfo, UINT *puArgErr)
294 HTMLSelectionObject *This = impl_from_IHTMLSelectionObject2(iface);
296 return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid,
297 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
300 static HRESULT WINAPI HTMLSelectionObject2_createRangeCollection(IHTMLSelectionObject2 *iface, IDispatch **rangeCollection)
302 HTMLSelectionObject *This = impl_from_IHTMLSelectionObject2(iface);
303 FIXME("(%p)->(%p)\n", This, rangeCollection);
304 return E_NOTIMPL;
307 static HRESULT WINAPI HTMLSelectionObject2_get_typeDetail(IHTMLSelectionObject2 *iface, BSTR *p)
309 HTMLSelectionObject *This = impl_from_IHTMLSelectionObject2(iface);
311 static const WCHAR undefinedW[] = {'u','n','d','e','f','i','n','e','d',0};
313 FIXME("(%p)->(%p) semi-stub\n", This, p);
315 /* FIXME: We should try to use ISelectionServicesListener::GetTypeDetail here. */
316 *p = SysAllocString(undefinedW);
317 return *p ? S_OK : E_OUTOFMEMORY;
320 static const IHTMLSelectionObject2Vtbl HTMLSelectionObject2Vtbl = {
321 HTMLSelectionObject2_QueryInterface,
322 HTMLSelectionObject2_AddRef,
323 HTMLSelectionObject2_Release,
324 HTMLSelectionObject2_GetTypeInfoCount,
325 HTMLSelectionObject2_GetTypeInfo,
326 HTMLSelectionObject2_GetIDsOfNames,
327 HTMLSelectionObject2_Invoke,
328 HTMLSelectionObject2_createRangeCollection,
329 HTMLSelectionObject2_get_typeDetail
332 static const tid_t HTMLSelectionObject_iface_tids[] = {
333 IHTMLSelectionObject_tid,
334 IHTMLSelectionObject2_tid,
337 static dispex_static_data_t HTMLSelectionObject_dispex = {
338 NULL,
339 IHTMLSelectionObject_tid, /* FIXME: We have a test for that, but it doesn't expose IHTMLSelectionObject2 iface. */
340 HTMLSelectionObject_iface_tids
343 HRESULT HTMLSelectionObject_Create(HTMLDocumentNode *doc, nsISelection *nsselection, IHTMLSelectionObject **ret)
345 HTMLSelectionObject *selection;
347 selection = heap_alloc(sizeof(HTMLSelectionObject));
348 if(!selection)
349 return E_OUTOFMEMORY;
351 init_dispex(&selection->dispex, (IUnknown*)&selection->IHTMLSelectionObject_iface, &HTMLSelectionObject_dispex);
353 selection->IHTMLSelectionObject_iface.lpVtbl = &HTMLSelectionObjectVtbl;
354 selection->IHTMLSelectionObject2_iface.lpVtbl = &HTMLSelectionObject2Vtbl;
355 selection->ref = 1;
356 selection->nsselection = nsselection; /* We shouldn't call AddRef here */
358 selection->doc = doc;
359 list_add_head(&doc->selection_list, &selection->entry);
361 *ret = &selection->IHTMLSelectionObject_iface;
362 return S_OK;
365 void detach_selection(HTMLDocumentNode *This)
367 HTMLSelectionObject *iter;
369 LIST_FOR_EACH_ENTRY(iter, &This->selection_list, HTMLSelectionObject, entry) {
370 iter->doc = NULL;