user32: Improve the HiliteMenuItem tests so they reflect actual behaviour on Windows.
[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 "config.h"
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #define COBJMACROS
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "winnls.h"
30 #include "ole2.h"
31
32 #include "wine/debug.h"
33 #include "wine/unicode.h"
34
35 #include "mshtml_private.h"
36
37 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
38
39 typedef struct {
40     const IHTMLSelectionObjectVtbl *lpHTMLSelectionObjectVtbl;
41
42     LONG ref;
43
44     nsISelection *nsselection;
45     HTMLDocument *doc;
46
47     struct list entry;
48 } HTMLSelectionObject;
49
50 #define HTMLSELOBJ(x)  ((IHTMLSelectionObject*) &(x)->lpHTMLSelectionObjectVtbl)
51
52 #define HTMLSELOBJ_THIS(iface) DEFINE_THIS(HTMLSelectionObject, HTMLSelectionObject, iface)
53
54 static HRESULT WINAPI HTMLSelectionObject_QueryInterface(IHTMLSelectionObject *iface,
55                                                          REFIID riid, void **ppv)
56 {
57     HTMLSelectionObject *This = HTMLSELOBJ_THIS(iface);
58
59     *ppv = NULL;
60
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);
70     }
71
72     if(*ppv) {
73         IUnknown_AddRef((IUnknown*)*ppv);
74         return S_OK;
75     }
76
77     WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
78     return E_NOINTERFACE;
79 }
80
81 static ULONG WINAPI HTMLSelectionObject_AddRef(IHTMLSelectionObject *iface)
82 {
83     HTMLSelectionObject *This = HTMLSELOBJ_THIS(iface);
84     LONG ref = InterlockedIncrement(&This->ref);
85
86     TRACE("(%p) ref=%d\n", This, ref);
87
88     return ref;
89 }
90
91 static ULONG WINAPI HTMLSelectionObject_Release(IHTMLSelectionObject *iface)
92 {
93     HTMLSelectionObject *This = HTMLSELOBJ_THIS(iface);
94     LONG ref = InterlockedDecrement(&This->ref);
95
96     TRACE("(%p) ref=%d\n", This, ref);
97
98     if(!ref) {
99         if(This->nsselection)
100             nsISelection_Release(This->nsselection);
101         if(This->doc)
102             list_remove(&This->entry);
103         heap_free(This);
104     }
105
106     return ref;
107 }
108
109 static HRESULT WINAPI HTMLSelectionObject_GetTypeInfoCount(IHTMLSelectionObject *iface, UINT *pctinfo)
110 {
111     HTMLSelectionObject *This = HTMLSELOBJ_THIS(iface);
112     FIXME("(%p)->(%p)\n", This, pctinfo);
113     return E_NOTIMPL;
114 }
115
116 static HRESULT WINAPI HTMLSelectionObject_GetTypeInfo(IHTMLSelectionObject *iface, UINT iTInfo,
117                                               LCID lcid, ITypeInfo **ppTInfo)
118 {
119     HTMLSelectionObject *This = HTMLSELOBJ_THIS(iface);
120     FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
121     return E_NOTIMPL;
122 }
123
124 static HRESULT WINAPI HTMLSelectionObject_GetIDsOfNames(IHTMLSelectionObject *iface, REFIID riid,
125                                                 LPOLESTR *rgszNames, UINT cNames,
126                                                 LCID lcid, DISPID *rgDispId)
127 {
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;
132 }
133
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)
137 {
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;
142 }
143
144 static HRESULT WINAPI HTMLSelectionObject_createRange(IHTMLSelectionObject *iface, IDispatch **range)
145 {
146     HTMLSelectionObject *This = HTMLSELOBJ_THIS(iface);
147     nsIDOMRange *nsrange = NULL;
148
149     TRACE("(%p)->(%p)\n", This, range);
150
151     if(This->nsselection) {
152         PRInt32 nsrange_cnt = 0;
153         nsresult nsres;
154
155         nsISelection_GetRangeCount(This->nsselection, &nsrange_cnt);
156         if(!nsrange_cnt) {
157             nsIDOMDocument *nsdoc;
158             nsIDOMHTMLDocument *nshtmldoc;
159             nsIDOMHTMLElement *nsbody = NULL;
160
161             TRACE("nsrange_cnt = 0\n");
162
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;
167             }
168
169             nsIDOMDocument_QueryInterface(nsdoc, &IID_nsIDOMHTMLDocument, (void**)&nshtmldoc);
170             nsIDOMDocument_Release(nsdoc);
171
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;
177             }
178
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);
185         }
186
187         nsres = nsISelection_GetRangeAt(This->nsselection, 0, &nsrange);
188         if(NS_FAILED(nsres))
189             ERR("GetRangeAt failed: %08x\n", nsres);
190     }
191
192     *range = (IDispatch*)HTMLTxtRange_Create(This->doc, nsrange);
193     return S_OK;
194 }
195
196 static HRESULT WINAPI HTMLSelectionObject_empty(IHTMLSelectionObject *iface)
197 {
198     HTMLSelectionObject *This = HTMLSELOBJ_THIS(iface);
199     FIXME("(%p)\n", This);
200     return E_NOTIMPL;
201 }
202
203 static HRESULT WINAPI HTMLSelectionObject_clear(IHTMLSelectionObject *iface)
204 {
205     HTMLSelectionObject *This = HTMLSELOBJ_THIS(iface);
206     FIXME("(%p)\n", This);
207     return E_NOTIMPL;
208 }
209
210 static HRESULT WINAPI HTMLSelectionObject_get_type(IHTMLSelectionObject *iface, BSTR *p)
211 {
212     HTMLSelectionObject *This = HTMLSELOBJ_THIS(iface);
213     PRBool collapsed = TRUE;
214
215     static const WCHAR wszNone[] = {'N','o','n','e',0};
216     static const WCHAR wszText[] = {'T','e','x','t',0};
217
218     TRACE("(%p)->(%p)\n", This, p);
219
220     if(This->nsselection)
221         nsISelection_GetIsCollapsed(This->nsselection, &collapsed);
222
223     *p = SysAllocString(collapsed ? wszNone : wszText); /* FIXME: control */
224     TRACE("ret %s\n", debugstr_w(*p));
225     return S_OK;
226 }
227
228 #undef HTMLSELOBJ_THIS
229
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
242 };
243
244 IHTMLSelectionObject *HTMLSelectionObject_Create(HTMLDocument *doc, nsISelection *nsselection)
245 {
246     HTMLSelectionObject *ret = heap_alloc(sizeof(HTMLSelectionObject));
247
248     ret->lpHTMLSelectionObjectVtbl = &HTMLSelectionObjectVtbl;
249     ret->ref = 1;
250     ret->nsselection = nsselection; /* We shouldn't call AddRef here */
251
252     ret->doc = doc;
253     list_add_head(&doc->selection_list, &ret->entry);
254
255     return HTMLSELOBJ(ret);
256 }
257
258 void detach_selection(HTMLDocument *This)
259 {
260     HTMLSelectionObject *iter;
261
262     LIST_FOR_EACH_ENTRY(iter, &This->selection_list, HTMLSelectionObject, entry) {
263         iter->doc = NULL;
264     }
265 }