wintrust: Use path in WIN_TRUST_SUBJECT_FILE structure rather than assuming a path...
[wine] / dlls / mshtml / selection.c
1 /*
2  * Copyright 2006 Jacek Caban for CodeWeavers
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include <stdarg.h>
20
21 #define COBJMACROS
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "ole2.h"
27
28 #include "wine/debug.h"
29 #include "wine/unicode.h"
30
31 #include "mshtml_private.h"
32
33 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
34
35 typedef struct {
36     const IHTMLSelectionObjectVtbl *lpHTMLSelectionObjectVtbl;
37
38     LONG ref;
39
40     nsISelection *nsselection;
41     HTMLDocument *doc;
42
43     struct list entry;
44 } HTMLSelectionObject;
45
46 #define HTMLSELOBJ(x)  ((IHTMLSelectionObject*) &(x)->lpHTMLSelectionObjectVtbl)
47
48 #define HTMLSELOBJ_THIS(iface) DEFINE_THIS(HTMLSelectionObject, HTMLSelectionObject, iface)
49
50 static HRESULT WINAPI HTMLSelectionObject_QueryInterface(IHTMLSelectionObject *iface,
51                                                          REFIID riid, void **ppv)
52 {
53     HTMLSelectionObject *This = HTMLSELOBJ_THIS(iface);
54
55     *ppv = NULL;
56
57     if(IsEqualGUID(&IID_IUnknown, riid)) {
58         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
59         *ppv = HTMLSELOBJ(This);
60     }else if(IsEqualGUID(&IID_IDispatch, riid)) {
61         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
62         *ppv = HTMLSELOBJ(This);
63     }else if(IsEqualGUID(&IID_IHTMLSelectionObject, riid)) {
64         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
65         *ppv = HTMLSELOBJ(This);
66     }
67
68     if(*ppv) {
69         IUnknown_AddRef((IUnknown*)*ppv);
70         return S_OK;
71     }
72
73     WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
74     return E_NOINTERFACE;
75 }
76
77 static ULONG WINAPI HTMLSelectionObject_AddRef(IHTMLSelectionObject *iface)
78 {
79     HTMLSelectionObject *This = HTMLSELOBJ_THIS(iface);
80     LONG ref = InterlockedIncrement(&This->ref);
81
82     TRACE("(%p) ref=%d\n", This, ref);
83
84     return ref;
85 }
86
87 static ULONG WINAPI HTMLSelectionObject_Release(IHTMLSelectionObject *iface)
88 {
89     HTMLSelectionObject *This = HTMLSELOBJ_THIS(iface);
90     LONG ref = InterlockedDecrement(&This->ref);
91
92     TRACE("(%p) ref=%d\n", This, ref);
93
94     if(!ref) {
95         if(This->nsselection)
96             nsISelection_Release(This->nsselection);
97         if(This->doc)
98             list_remove(&This->entry);
99         heap_free(This);
100     }
101
102     return ref;
103 }
104
105 static HRESULT WINAPI HTMLSelectionObject_GetTypeInfoCount(IHTMLSelectionObject *iface, UINT *pctinfo)
106 {
107     HTMLSelectionObject *This = HTMLSELOBJ_THIS(iface);
108     FIXME("(%p)->(%p)\n", This, pctinfo);
109     return E_NOTIMPL;
110 }
111
112 static HRESULT WINAPI HTMLSelectionObject_GetTypeInfo(IHTMLSelectionObject *iface, UINT iTInfo,
113                                               LCID lcid, ITypeInfo **ppTInfo)
114 {
115     HTMLSelectionObject *This = HTMLSELOBJ_THIS(iface);
116     FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
117     return E_NOTIMPL;
118 }
119
120 static HRESULT WINAPI HTMLSelectionObject_GetIDsOfNames(IHTMLSelectionObject *iface, REFIID riid,
121                                                 LPOLESTR *rgszNames, UINT cNames,
122                                                 LCID lcid, DISPID *rgDispId)
123 {
124     HTMLSelectionObject *This = HTMLSELOBJ_THIS(iface);
125     FIXME("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
126           lcid, rgDispId);
127     return E_NOTIMPL;
128 }
129
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)
133 {
134     HTMLSelectionObject *This = HTMLSELOBJ_THIS(iface);
135     FIXME("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
136           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
137     return E_NOTIMPL;
138 }
139
140 static HRESULT WINAPI HTMLSelectionObject_createRange(IHTMLSelectionObject *iface, IDispatch **range)
141 {
142     HTMLSelectionObject *This = HTMLSELOBJ_THIS(iface);
143     nsIDOMRange *nsrange = NULL;
144
145     TRACE("(%p)->(%p)\n", This, range);
146
147     if(This->nsselection) {
148         PRInt32 nsrange_cnt = 0;
149         nsresult nsres;
150
151         nsISelection_GetRangeCount(This->nsselection, &nsrange_cnt);
152         if(!nsrange_cnt) {
153             nsIDOMDocument *nsdoc;
154             nsIDOMHTMLDocument *nshtmldoc;
155             nsIDOMHTMLElement *nsbody = NULL;
156
157             TRACE("nsrange_cnt = 0\n");
158
159             nsres = nsIWebNavigation_GetDocument(This->doc->nscontainer->navigation, &nsdoc);
160             if(NS_FAILED(nsres) || !nsdoc) {
161                 ERR("GetDocument failed: %08x\n", nsres);
162                 return E_FAIL;
163             }
164
165             nsIDOMDocument_QueryInterface(nsdoc, &IID_nsIDOMHTMLDocument, (void**)&nshtmldoc);
166             nsIDOMDocument_Release(nsdoc);
167
168             nsres = nsIDOMHTMLDocument_GetBody(nshtmldoc, &nsbody);
169             nsIDOMHTMLDocument_Release(nshtmldoc);
170             if(NS_FAILED(nsres) || !nsbody) {
171                 ERR("Could not get body: %08x\n", nsres);
172                 return E_FAIL;
173             }
174
175             nsres = nsISelection_Collapse(This->nsselection, (nsIDOMNode*)nsbody, 0);
176             nsIDOMHTMLElement_Release(nsbody);
177             if(NS_FAILED(nsres))
178                 ERR("Collapse failed: %08x\n", nsres);
179         }else if(nsrange_cnt > 1) {
180             FIXME("range_cnt = %d\n", nsrange_cnt);
181         }
182
183         nsres = nsISelection_GetRangeAt(This->nsselection, 0, &nsrange);
184         if(NS_FAILED(nsres))
185             ERR("GetRangeAt failed: %08x\n", nsres);
186     }
187
188     *range = (IDispatch*)HTMLTxtRange_Create(This->doc, nsrange);
189     return S_OK;
190 }
191
192 static HRESULT WINAPI HTMLSelectionObject_empty(IHTMLSelectionObject *iface)
193 {
194     HTMLSelectionObject *This = HTMLSELOBJ_THIS(iface);
195     FIXME("(%p)\n", This);
196     return E_NOTIMPL;
197 }
198
199 static HRESULT WINAPI HTMLSelectionObject_clear(IHTMLSelectionObject *iface)
200 {
201     HTMLSelectionObject *This = HTMLSELOBJ_THIS(iface);
202     FIXME("(%p)\n", This);
203     return E_NOTIMPL;
204 }
205
206 static HRESULT WINAPI HTMLSelectionObject_get_type(IHTMLSelectionObject *iface, BSTR *p)
207 {
208     HTMLSelectionObject *This = HTMLSELOBJ_THIS(iface);
209     PRBool collapsed = TRUE;
210
211     static const WCHAR wszNone[] = {'N','o','n','e',0};
212     static const WCHAR wszText[] = {'T','e','x','t',0};
213
214     TRACE("(%p)->(%p)\n", This, p);
215
216     if(This->nsselection)
217         nsISelection_GetIsCollapsed(This->nsselection, &collapsed);
218
219     *p = SysAllocString(collapsed ? wszNone : wszText); /* FIXME: control */
220     TRACE("ret %s\n", debugstr_w(*p));
221     return S_OK;
222 }
223
224 #undef HTMLSELOBJ_THIS
225
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
238 };
239
240 IHTMLSelectionObject *HTMLSelectionObject_Create(HTMLDocument *doc, nsISelection *nsselection)
241 {
242     HTMLSelectionObject *ret = heap_alloc(sizeof(HTMLSelectionObject));
243
244     ret->lpHTMLSelectionObjectVtbl = &HTMLSelectionObjectVtbl;
245     ret->ref = 1;
246     ret->nsselection = nsselection; /* We shouldn't call AddRef here */
247
248     ret->doc = doc;
249     list_add_head(&doc->selection_list, &ret->entry);
250
251     return HTMLSELOBJ(ret);
252 }
253
254 void detach_selection(HTMLDocument *This)
255 {
256     HTMLSelectionObject *iter;
257
258     LIST_FOR_EACH_ENTRY(iter, &This->selection_list, HTMLSelectionObject, entry) {
259         iter->doc = NULL;
260     }
261 }