comctl32: Remove unneeded NONAMELESSSTRUCT directives.
[wine/multimedia.git] / dlls / mshtml / selection.c
blobf34154bfb2d865e4a2425d99a84b21d78b76e2cd
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;
38 LONG ref;
40 nsISelection *nsselection;
41 HTMLDocumentNode *doc;
43 struct list entry;
44 } HTMLSelectionObject;
46 static inline HTMLSelectionObject *impl_from_IHTMLSelectionObject(IHTMLSelectionObject *iface)
48 return CONTAINING_RECORD(iface, HTMLSelectionObject, IHTMLSelectionObject_iface);
51 static HRESULT WINAPI HTMLSelectionObject_QueryInterface(IHTMLSelectionObject *iface,
52 REFIID riid, void **ppv)
54 HTMLSelectionObject *This = impl_from_IHTMLSelectionObject(iface);
56 TRACE("(%p)->(%s %p)\n", This, debugstr_mshtml_guid(riid), ppv);
58 if(IsEqualGUID(&IID_IUnknown, riid)) {
59 *ppv = &This->IHTMLSelectionObject_iface;
60 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
61 *ppv = &This->IHTMLSelectionObject_iface;
62 }else if(IsEqualGUID(&IID_IHTMLSelectionObject, riid)) {
63 *ppv = &This->IHTMLSelectionObject_iface;
64 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
65 return *ppv ? S_OK : E_NOINTERFACE;
66 }else {
67 *ppv = NULL;
68 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
69 return E_NOINTERFACE;
72 IUnknown_AddRef((IUnknown*)*ppv);
73 return S_OK;
76 static ULONG WINAPI HTMLSelectionObject_AddRef(IHTMLSelectionObject *iface)
78 HTMLSelectionObject *This = impl_from_IHTMLSelectionObject(iface);
79 LONG ref = InterlockedIncrement(&This->ref);
81 TRACE("(%p) ref=%d\n", This, ref);
83 return ref;
86 static ULONG WINAPI HTMLSelectionObject_Release(IHTMLSelectionObject *iface)
88 HTMLSelectionObject *This = impl_from_IHTMLSelectionObject(iface);
89 LONG ref = InterlockedDecrement(&This->ref);
91 TRACE("(%p) ref=%d\n", This, ref);
93 if(!ref) {
94 if(This->nsselection)
95 nsISelection_Release(This->nsselection);
96 if(This->doc)
97 list_remove(&This->entry);
98 release_dispex(&This->dispex);
99 heap_free(This);
102 return ref;
105 static HRESULT WINAPI HTMLSelectionObject_GetTypeInfoCount(IHTMLSelectionObject *iface, UINT *pctinfo)
107 HTMLSelectionObject *This = impl_from_IHTMLSelectionObject(iface);
109 return IDispatchEx_GetTypeInfoCount(&This->dispex.IDispatchEx_iface, pctinfo);
112 static HRESULT WINAPI HTMLSelectionObject_GetTypeInfo(IHTMLSelectionObject *iface, UINT iTInfo,
113 LCID lcid, ITypeInfo **ppTInfo)
115 HTMLSelectionObject *This = impl_from_IHTMLSelectionObject(iface);
117 return IDispatchEx_GetTypeInfo(&This->dispex.IDispatchEx_iface, iTInfo, lcid, ppTInfo);
120 static HRESULT WINAPI HTMLSelectionObject_GetIDsOfNames(IHTMLSelectionObject *iface, REFIID riid,
121 LPOLESTR *rgszNames, UINT cNames,
122 LCID lcid, DISPID *rgDispId)
124 HTMLSelectionObject *This = impl_from_IHTMLSelectionObject(iface);
126 return IDispatchEx_GetIDsOfNames(&This->dispex.IDispatchEx_iface, riid, rgszNames,
127 cNames, lcid, rgDispId);
130 static HRESULT WINAPI HTMLSelectionObject_Invoke(IHTMLSelectionObject *iface, DISPID dispIdMember,
131 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
132 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
134 HTMLSelectionObject *This = impl_from_IHTMLSelectionObject(iface);
137 return IDispatchEx_Invoke(&This->dispex.IDispatchEx_iface, dispIdMember, riid,
138 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
141 static HRESULT WINAPI HTMLSelectionObject_createRange(IHTMLSelectionObject *iface, IDispatch **range)
143 HTMLSelectionObject *This = impl_from_IHTMLSelectionObject(iface);
144 IHTMLTxtRange *range_obj = NULL;
145 nsIDOMRange *nsrange = NULL;
146 HRESULT hres;
148 TRACE("(%p)->(%p)\n", This, range);
150 if(This->nsselection) {
151 LONG nsrange_cnt = 0;
152 nsresult nsres;
154 nsISelection_GetRangeCount(This->nsselection, &nsrange_cnt);
155 if(!nsrange_cnt) {
156 nsIDOMHTMLElement *nsbody = NULL;
158 TRACE("nsrange_cnt = 0\n");
160 if(!This->doc->nsdoc) {
161 WARN("nsdoc is NULL\n");
162 return E_UNEXPECTED;
165 nsres = nsIDOMHTMLDocument_GetBody(This->doc->nsdoc, &nsbody);
166 if(NS_FAILED(nsres) || !nsbody) {
167 ERR("Could not get body: %08x\n", nsres);
168 return E_FAIL;
171 nsres = nsISelection_Collapse(This->nsselection, (nsIDOMNode*)nsbody, 0);
172 nsIDOMHTMLElement_Release(nsbody);
173 if(NS_FAILED(nsres))
174 ERR("Collapse failed: %08x\n", nsres);
175 }else if(nsrange_cnt > 1) {
176 FIXME("range_cnt = %d\n", nsrange_cnt);
179 nsres = nsISelection_GetRangeAt(This->nsselection, 0, &nsrange);
180 if(NS_FAILED(nsres))
181 ERR("GetRangeAt failed: %08x\n", nsres);
184 hres = HTMLTxtRange_Create(This->doc, nsrange, &range_obj);
186 if (nsrange) nsIDOMRange_Release(nsrange);
187 *range = (IDispatch*)range_obj;
188 return hres;
191 static HRESULT WINAPI HTMLSelectionObject_empty(IHTMLSelectionObject *iface)
193 HTMLSelectionObject *This = impl_from_IHTMLSelectionObject(iface);
194 FIXME("(%p)\n", This);
195 return E_NOTIMPL;
198 static HRESULT WINAPI HTMLSelectionObject_clear(IHTMLSelectionObject *iface)
200 HTMLSelectionObject *This = impl_from_IHTMLSelectionObject(iface);
201 FIXME("(%p)\n", This);
202 return E_NOTIMPL;
205 static HRESULT WINAPI HTMLSelectionObject_get_type(IHTMLSelectionObject *iface, BSTR *p)
207 HTMLSelectionObject *This = impl_from_IHTMLSelectionObject(iface);
208 cpp_bool collapsed = TRUE;
210 static const WCHAR wszNone[] = {'N','o','n','e',0};
211 static const WCHAR wszText[] = {'T','e','x','t',0};
213 TRACE("(%p)->(%p)\n", This, p);
215 if(This->nsselection)
216 nsISelection_GetIsCollapsed(This->nsselection, &collapsed);
218 *p = SysAllocString(collapsed ? wszNone : wszText); /* FIXME: control */
219 TRACE("ret %s\n", debugstr_w(*p));
220 return S_OK;
223 static const IHTMLSelectionObjectVtbl HTMLSelectionObjectVtbl = {
224 HTMLSelectionObject_QueryInterface,
225 HTMLSelectionObject_AddRef,
226 HTMLSelectionObject_Release,
227 HTMLSelectionObject_GetTypeInfoCount,
228 HTMLSelectionObject_GetTypeInfo,
229 HTMLSelectionObject_GetIDsOfNames,
230 HTMLSelectionObject_Invoke,
231 HTMLSelectionObject_createRange,
232 HTMLSelectionObject_empty,
233 HTMLSelectionObject_clear,
234 HTMLSelectionObject_get_type
237 static const tid_t HTMLSelectionObject_iface_tids[] = {
238 IHTMLSelectionObject_tid,
241 static dispex_static_data_t HTMLSelectionObject_dispex = {
242 NULL,
243 IHTMLSelectionObject_tid,
244 NULL,
245 HTMLSelectionObject_iface_tids
248 HRESULT HTMLSelectionObject_Create(HTMLDocumentNode *doc, nsISelection *nsselection, IHTMLSelectionObject **ret)
250 HTMLSelectionObject *selection;
252 selection = heap_alloc(sizeof(HTMLSelectionObject));
253 if(!selection)
254 return E_OUTOFMEMORY;
256 init_dispex(&selection->dispex, (IUnknown*)&selection->IHTMLSelectionObject_iface, &HTMLSelectionObject_dispex);
258 selection->IHTMLSelectionObject_iface.lpVtbl = &HTMLSelectionObjectVtbl;
259 selection->ref = 1;
260 selection->nsselection = nsselection; /* We shouldn't call AddRef here */
262 selection->doc = doc;
263 list_add_head(&doc->selection_list, &selection->entry);
265 *ret = &selection->IHTMLSelectionObject_iface;
266 return S_OK;
269 void detach_selection(HTMLDocumentNode *This)
271 HTMLSelectionObject *iter;
273 LIST_FOR_EACH_ENTRY(iter, &This->selection_list, HTMLSelectionObject, entry) {
274 iter->doc = NULL;