push 9ed7f32abbb93ea3a813fd6b1650e5bcfc506606
[wine/hacks.git] / dlls / mshtml / selection.c
blobdf2d6353b7dd1dab4ef760a4519d6758f9f813b2
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 "config.h"
21 #include <stdarg.h>
22 #include <stdio.h>
24 #define COBJMACROS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "winnls.h"
30 #include "ole2.h"
32 #include "wine/debug.h"
33 #include "wine/unicode.h"
35 #include "mshtml_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
39 typedef struct {
40 const IHTMLSelectionObjectVtbl *lpHTMLSelectionObjectVtbl;
42 LONG ref;
44 nsISelection *nsselection;
45 HTMLDocument *doc;
47 struct list entry;
48 } HTMLSelectionObject;
50 #define HTMLSELOBJ(x) ((IHTMLSelectionObject*) &(x)->lpHTMLSelectionObjectVtbl)
52 #define HTMLSELOBJ_THIS(iface) DEFINE_THIS(HTMLSelectionObject, HTMLSelectionObject, iface)
54 static HRESULT WINAPI HTMLSelectionObject_QueryInterface(IHTMLSelectionObject *iface,
55 REFIID riid, void **ppv)
57 HTMLSelectionObject *This = HTMLSELOBJ_THIS(iface);
59 *ppv = NULL;
61 if(IsEqualGUID(&IID_IUnknown, riid)) {
62 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
63 *ppv = HTMLSELOBJ(This);
64 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
65 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
66 *ppv = HTMLSELOBJ(This);
67 }else if(IsEqualGUID(&IID_IHTMLSelectionObject, riid)) {
68 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
69 *ppv = HTMLSELOBJ(This);
72 if(*ppv) {
73 IUnknown_AddRef((IUnknown*)*ppv);
74 return S_OK;
77 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
78 return E_NOINTERFACE;
81 static ULONG WINAPI HTMLSelectionObject_AddRef(IHTMLSelectionObject *iface)
83 HTMLSelectionObject *This = HTMLSELOBJ_THIS(iface);
84 LONG ref = InterlockedIncrement(&This->ref);
86 TRACE("(%p) ref=%d\n", This, ref);
88 return ref;
91 static ULONG WINAPI HTMLSelectionObject_Release(IHTMLSelectionObject *iface)
93 HTMLSelectionObject *This = HTMLSELOBJ_THIS(iface);
94 LONG ref = InterlockedDecrement(&This->ref);
96 TRACE("(%p) ref=%d\n", This, ref);
98 if(!ref) {
99 if(This->nsselection)
100 nsISelection_Release(This->nsselection);
101 if(This->doc)
102 list_remove(&This->entry);
103 mshtml_free(This);
106 return ref;
109 static HRESULT WINAPI HTMLSelectionObject_GetTypeInfoCount(IHTMLSelectionObject *iface, UINT *pctinfo)
111 HTMLSelectionObject *This = HTMLSELOBJ_THIS(iface);
112 FIXME("(%p)->(%p)\n", This, pctinfo);
113 return E_NOTIMPL;
116 static HRESULT WINAPI HTMLSelectionObject_GetTypeInfo(IHTMLSelectionObject *iface, UINT iTInfo,
117 LCID lcid, ITypeInfo **ppTInfo)
119 HTMLSelectionObject *This = HTMLSELOBJ_THIS(iface);
120 FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
121 return E_NOTIMPL;
124 static HRESULT WINAPI HTMLSelectionObject_GetIDsOfNames(IHTMLSelectionObject *iface, REFIID riid,
125 LPOLESTR *rgszNames, UINT cNames,
126 LCID lcid, DISPID *rgDispId)
128 HTMLSelectionObject *This = HTMLSELOBJ_THIS(iface);
129 FIXME("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
130 lcid, rgDispId);
131 return E_NOTIMPL;
134 static HRESULT WINAPI HTMLSelectionObject_Invoke(IHTMLSelectionObject *iface, DISPID dispIdMember,
135 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
136 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
138 HTMLSelectionObject *This = HTMLSELOBJ_THIS(iface);
139 FIXME("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
140 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
141 return E_NOTIMPL;
144 static HRESULT WINAPI HTMLSelectionObject_createRange(IHTMLSelectionObject *iface, IDispatch **range)
146 HTMLSelectionObject *This = HTMLSELOBJ_THIS(iface);
147 nsIDOMRange *nsrange = NULL;
149 TRACE("(%p)->(%p)\n", This, range);
151 if(This->nsselection) {
152 PRInt32 nsrange_cnt = 0;
153 nsresult nsres;
155 nsISelection_GetRangeCount(This->nsselection, &nsrange_cnt);
156 if(!nsrange_cnt) {
157 nsIDOMDocument *nsdoc;
158 nsIDOMHTMLDocument *nshtmldoc;
159 nsIDOMHTMLElement *nsbody = NULL;
161 TRACE("nsrange_cnt = 0\n");
163 nsres = nsIWebNavigation_GetDocument(This->doc->nscontainer->navigation, &nsdoc);
164 if(NS_FAILED(nsres) || !nsdoc) {
165 ERR("GetDocument failed: %08x\n", nsres);
166 return E_FAIL;
169 nsIDOMDocument_QueryInterface(nsdoc, &IID_nsIDOMHTMLDocument, (void**)&nshtmldoc);
170 nsIDOMDocument_Release(nsdoc);
172 nsres = nsIDOMHTMLDocument_GetBody(nshtmldoc, &nsbody);
173 nsIDOMHTMLDocument_Release(nshtmldoc);
174 if(NS_FAILED(nsres) || !nsbody) {
175 ERR("Could not get body: %08x\n", nsres);
176 return E_FAIL;
179 nsres = nsISelection_Collapse(This->nsselection, (nsIDOMNode*)nsbody, 0);
180 nsIDOMHTMLElement_Release(nsbody);
181 if(NS_FAILED(nsres))
182 ERR("Collapse failed: %08x\n", nsres);
183 }else if(nsrange_cnt > 1) {
184 FIXME("range_cnt = %d\n", nsrange_cnt);
187 nsres = nsISelection_GetRangeAt(This->nsselection, 0, &nsrange);
188 if(NS_FAILED(nsres))
189 ERR("GetRangeAt failed: %08x\n", nsres);
192 *range = (IDispatch*)HTMLTxtRange_Create(This->doc, nsrange);
193 return S_OK;
196 static HRESULT WINAPI HTMLSelectionObject_empty(IHTMLSelectionObject *iface)
198 HTMLSelectionObject *This = HTMLSELOBJ_THIS(iface);
199 FIXME("(%p)\n", This);
200 return E_NOTIMPL;
203 static HRESULT WINAPI HTMLSelectionObject_clear(IHTMLSelectionObject *iface)
205 HTMLSelectionObject *This = HTMLSELOBJ_THIS(iface);
206 FIXME("(%p)\n", This);
207 return E_NOTIMPL;
210 static HRESULT WINAPI HTMLSelectionObject_get_type(IHTMLSelectionObject *iface, BSTR *p)
212 HTMLSelectionObject *This = HTMLSELOBJ_THIS(iface);
213 PRBool collapsed = TRUE;
215 static const WCHAR wszNone[] = {'N','o','n','e',0};
216 static const WCHAR wszText[] = {'T','e','x','t',0};
218 TRACE("(%p)->(%p)\n", This, p);
220 if(This->nsselection)
221 nsISelection_GetIsCollapsed(This->nsselection, &collapsed);
223 *p = SysAllocString(collapsed ? wszNone : wszText); /* FIXME: control */
224 TRACE("ret %s\n", debugstr_w(*p));
225 return S_OK;
228 #undef HTMLSELOBJ_THIS
230 static const IHTMLSelectionObjectVtbl HTMLSelectionObjectVtbl = {
231 HTMLSelectionObject_QueryInterface,
232 HTMLSelectionObject_AddRef,
233 HTMLSelectionObject_Release,
234 HTMLSelectionObject_GetTypeInfoCount,
235 HTMLSelectionObject_GetTypeInfo,
236 HTMLSelectionObject_GetIDsOfNames,
237 HTMLSelectionObject_Invoke,
238 HTMLSelectionObject_createRange,
239 HTMLSelectionObject_empty,
240 HTMLSelectionObject_clear,
241 HTMLSelectionObject_get_type
244 IHTMLSelectionObject *HTMLSelectionObject_Create(HTMLDocument *doc, nsISelection *nsselection)
246 HTMLSelectionObject *ret = mshtml_alloc(sizeof(HTMLSelectionObject));
248 ret->lpHTMLSelectionObjectVtbl = &HTMLSelectionObjectVtbl;
249 ret->ref = 1;
250 ret->nsselection = nsselection; /* We shouldn't call AddRef here */
252 ret->doc = doc;
253 list_add_head(&doc->selection_list, &ret->entry);
255 return HTMLSELOBJ(ret);
258 void detach_selection(HTMLDocument *This)
260 HTMLSelectionObject *iter;
262 LIST_FOR_EACH_ENTRY(iter, &This->selection_list, HTMLSelectionObject, entry) {
263 iter->doc = NULL;