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
32 #include "wine/debug.h"
33 #include "wine/unicode.h"
35 #include "mshtml_private.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
40 const IHTMLTxtRangeVtbl *lpHTMLTxtRangeVtbl;
50 #define HTMLTXTRANGE(x) ((IHTMLTxtRange*) &(x)->lpHTMLTxtRangeVtbl)
52 static HTMLTxtRange *get_range_object(HTMLDocument *doc, IHTMLTxtRange *iface)
56 LIST_FOR_EACH_ENTRY(iter, &doc->range_list, HTMLTxtRange, entry) {
57 if(HTMLTXTRANGE(iter) == iface)
61 ERR("Could not find range in document\n");
65 static int string_to_nscmptype(LPCWSTR str)
67 static const WCHAR seW[] = {'S','t','a','r','t','T','o','E','n','d',0};
68 static const WCHAR ssW[] = {'S','t','a','r','t','T','o','S','t','a','r','t',0};
69 static const WCHAR esW[] = {'E','n','d','T','o','S','t','a','r','t',0};
70 static const WCHAR eeW[] = {'E','n','d','T','o','E','n','d',0};
72 if(!strcmpiW(str, seW)) return NS_START_TO_END;
73 if(!strcmpiW(str, ssW)) return NS_START_TO_START;
74 if(!strcmpiW(str, esW)) return NS_END_TO_START;
75 if(!strcmpiW(str, eeW)) return NS_END_TO_END;
80 static PRUint16 get_node_type(nsIDOMNode *node)
82 PRUint16 type = 0xfff;
85 nsIDOMNode_GetNodeType(node, &type);
90 #define HTMLTXTRANGE_THIS(iface) DEFINE_THIS(HTMLTxtRange, HTMLTxtRange, iface)
92 static HRESULT WINAPI HTMLTxtRange_QueryInterface(IHTMLTxtRange *iface, REFIID riid, void **ppv)
94 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
98 if(IsEqualGUID(&IID_IUnknown, riid)) {
99 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
100 *ppv = HTMLTXTRANGE(This);
101 }else if(IsEqualGUID(&IID_IDispatch, riid)) {
102 TRACE("(%p)->(IID_IDispatch %p)\n", This, ppv);
103 *ppv = HTMLTXTRANGE(This);
104 }else if(IsEqualGUID(&IID_IHTMLTxtRange, riid)) {
105 TRACE("(%p)->(IID_IHTMLTxtRange %p)\n", This, ppv);
106 *ppv = HTMLTXTRANGE(This);
110 IUnknown_AddRef((IUnknown*)*ppv);
114 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
115 return E_NOINTERFACE;
118 static ULONG WINAPI HTMLTxtRange_AddRef(IHTMLTxtRange *iface)
120 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
121 LONG ref = InterlockedIncrement(&This->ref);
123 TRACE("(%p) ref=%d\n", This, ref);
128 static ULONG WINAPI HTMLTxtRange_Release(IHTMLTxtRange *iface)
130 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
131 LONG ref = InterlockedDecrement(&This->ref);
133 TRACE("(%p) ref=%d\n", This, ref);
137 nsISelection_Release(This->nsrange);
139 list_remove(&This->entry);
146 static HRESULT WINAPI HTMLTxtRange_GetTypeInfoCount(IHTMLTxtRange *iface, UINT *pctinfo)
148 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
149 FIXME("(%p)->(%p)\n", This, pctinfo);
153 static HRESULT WINAPI HTMLTxtRange_GetTypeInfo(IHTMLTxtRange *iface, UINT iTInfo,
154 LCID lcid, ITypeInfo **ppTInfo)
156 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
157 FIXME("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
161 static HRESULT WINAPI HTMLTxtRange_GetIDsOfNames(IHTMLTxtRange *iface, REFIID riid,
162 LPOLESTR *rgszNames, UINT cNames,
163 LCID lcid, DISPID *rgDispId)
165 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
166 FIXME("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames,
171 static HRESULT WINAPI HTMLTxtRange_Invoke(IHTMLTxtRange *iface, DISPID dispIdMember,
172 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
173 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
175 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
176 FIXME("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
177 lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
181 static HRESULT WINAPI HTMLTxtRange_get_htmlText(IHTMLTxtRange *iface, BSTR *p)
183 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
185 TRACE("(%p)->(%p)\n", This, p);
190 nsIDOMDocumentFragment *fragment;
193 nsres = nsIDOMRange_CloneContents(This->nsrange, &fragment);
194 if(NS_SUCCEEDED(nsres)) {
195 const PRUnichar *nstext;
198 nsAString_Init(&nsstr, NULL);
199 nsnode_to_nsstring((nsIDOMNode*)fragment, &nsstr);
200 nsIDOMDocumentFragment_Release(fragment);
202 nsAString_GetData(&nsstr, &nstext, NULL);
203 *p = SysAllocString(nstext);
205 nsAString_Finish(&nsstr);
210 const WCHAR emptyW[] = {0};
211 *p = SysAllocString(emptyW);
214 TRACE("return %s\n", debugstr_w(*p));
218 static HRESULT WINAPI HTMLTxtRange_put_text(IHTMLTxtRange *iface, BSTR v)
220 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
221 nsIDOMDocument *nsdoc;
222 nsIDOMText *text_node;
226 TRACE("(%p)->(%s)\n", This, debugstr_w(v));
229 return MSHTML_E_NODOC;
231 nsres = nsIWebNavigation_GetDocument(This->doc->nscontainer->navigation, &nsdoc);
232 if(NS_FAILED(nsres)) {
233 ERR("GetDocument failed: %08x\n", nsres);
237 nsAString_Init(&text_str, v);
238 nsres = nsIDOMDocument_CreateTextNode(nsdoc, &text_str, &text_node);
239 nsAString_Finish(&text_str);
240 if(NS_FAILED(nsres)) {
241 ERR("CreateTextNode failed: %08x\n", nsres);
244 nsres = nsIDOMRange_DeleteContents(This->nsrange);
246 ERR("DeleteContents failed: %08x\n", nsres);
248 nsres = nsIDOMRange_InsertNode(This->nsrange, (nsIDOMNode*)text_node);
250 ERR("InsertNode failed: %08x\n", nsres);
255 static HRESULT WINAPI HTMLTxtRange_get_text(IHTMLTxtRange *iface, BSTR *p)
257 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
259 TRACE("(%p)->(%p)\n", This, p);
267 nsAString_Init(&text_str, NULL);
269 nsres = nsIDOMRange_ToString(This->nsrange, &text_str);
270 if(NS_SUCCEEDED(nsres)) {
271 const PRUnichar *nstext;
273 nsAString_GetData(&text_str, &nstext, NULL);
274 *p = SysAllocString(nstext);
276 ERR("ToString failed: %08x\n", nsres);
279 nsAString_Finish(&text_str);
283 static const WCHAR empty[] = {0};
284 *p = SysAllocString(empty);
290 static HRESULT WINAPI HTMLTxtRange_parentElement(IHTMLTxtRange *iface, IHTMLElement **parent)
292 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
293 nsIDOMNode *nsnode, *tmp;
297 TRACE("(%p)->(%p)\n", This, parent);
299 nsIDOMRange_GetCommonAncestorContainer(This->nsrange, &nsnode);
300 while(nsnode && get_node_type(nsnode) != ELEMENT_NODE) {
301 nsIDOMNode_GetParentNode(nsnode, &tmp);
302 nsIDOMNode_Release(nsnode);
311 node = get_node(This->doc, nsnode);
312 nsIDOMNode_Release(nsnode);
314 hres = IHTMLDOMNode_QueryInterface(HTMLDOMNODE(node), &IID_IHTMLElement, (void**)parent);
316 IHTMLDOMNode_Release(HTMLDOMNODE(node));
320 static HRESULT WINAPI HTMLTxtRange_duplicate(IHTMLTxtRange *iface, IHTMLTxtRange **Duplicate)
322 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
323 nsIDOMRange *nsrange = NULL;
325 TRACE("(%p)->(%p)\n", This, Duplicate);
327 nsIDOMRange_CloneRange(This->nsrange, &nsrange);
328 *Duplicate = HTMLTxtRange_Create(This->doc, nsrange);
329 nsIDOMRange_Release(nsrange);
334 static HRESULT WINAPI HTMLTxtRange_inRange(IHTMLTxtRange *iface, IHTMLTxtRange *Range,
335 VARIANT_BOOL *InRange)
337 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
338 HTMLTxtRange *src_range;
342 TRACE("(%p)->(%p %p)\n", This, Range, InRange);
344 *InRange = VARIANT_FALSE;
346 src_range = get_range_object(This->doc, Range);
350 nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, NS_START_TO_START,
351 src_range->nsrange, &nsret);
352 if(NS_SUCCEEDED(nsres) && nsret <= 0) {
353 nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, NS_END_TO_END,
354 src_range->nsrange, &nsret);
355 if(NS_SUCCEEDED(nsres) && nsret >= 0)
356 *InRange = VARIANT_TRUE;
360 ERR("CompareBoundaryPoints failed: %08x\n", nsres);
365 static HRESULT WINAPI HTMLTxtRange_isEqual(IHTMLTxtRange *iface, IHTMLTxtRange *Range,
366 VARIANT_BOOL *IsEqual)
368 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
369 HTMLTxtRange *src_range;
373 TRACE("(%p)->(%p %p)\n", This, Range, IsEqual);
375 *IsEqual = VARIANT_FALSE;
377 src_range = get_range_object(This->doc, Range);
381 nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, NS_START_TO_START,
382 src_range->nsrange, &nsret);
383 if(NS_SUCCEEDED(nsres) && !nsret) {
384 nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, NS_END_TO_END,
385 src_range->nsrange, &nsret);
386 if(NS_SUCCEEDED(nsres) && !nsret)
387 *IsEqual = VARIANT_TRUE;
391 ERR("CompareBoundaryPoints failed: %08x\n", nsres);
396 static HRESULT WINAPI HTMLTxtRange_scrollIntoView(IHTMLTxtRange *iface, VARIANT_BOOL fStart)
398 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
399 FIXME("(%p)->(%x)\n", This, fStart);
403 static HRESULT WINAPI HTMLTxtRange_collapse(IHTMLTxtRange *iface, VARIANT_BOOL Start)
405 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
407 TRACE("(%p)->(%x)\n", This, Start);
409 nsIDOMRange_Collapse(This->nsrange, Start != VARIANT_FALSE);
413 static HRESULT WINAPI HTMLTxtRange_expand(IHTMLTxtRange *iface, BSTR Unit, VARIANT_BOOL *Success)
415 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
416 FIXME("(%p)->(%s %p)\n", This, debugstr_w(Unit), Success);
420 static HRESULT WINAPI HTMLTxtRange_move(IHTMLTxtRange *iface, BSTR Unit,
421 long Count, long *ActualCount)
423 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
424 FIXME("(%p)->(%s %ld %p)\n", This, debugstr_w(Unit), Count, ActualCount);
428 static HRESULT WINAPI HTMLTxtRange_moveStart(IHTMLTxtRange *iface, BSTR Unit,
429 long Count, long *ActualCount)
431 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
432 FIXME("(%p)->(%s %ld %p)\n", This, debugstr_w(Unit), Count, ActualCount);
436 static HRESULT WINAPI HTMLTxtRange_moveEnd(IHTMLTxtRange *iface, BSTR Unit,
437 long Count, long *ActualCount)
439 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
440 FIXME("(%p)->(%s %ld %p)\n", This, debugstr_w(Unit), Count, ActualCount);
444 static HRESULT WINAPI HTMLTxtRange_select(IHTMLTxtRange *iface)
446 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
448 TRACE("(%p)\n", This);
450 if(This->doc->nscontainer) {
451 nsIDOMWindow *dom_window = NULL;
452 nsISelection *nsselection;
454 nsIWebBrowser_GetContentDOMWindow(This->doc->nscontainer->webbrowser, &dom_window);
455 nsIDOMWindow_GetSelection(dom_window, &nsselection);
456 nsIDOMWindow_Release(dom_window);
458 nsISelection_RemoveAllRanges(nsselection);
459 nsISelection_AddRange(nsselection, This->nsrange);
461 nsISelection_Release(nsselection);
467 static HRESULT WINAPI HTMLTxtRange_pasteHTML(IHTMLTxtRange *iface, BSTR html)
469 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
470 FIXME("(%p)->(%s)\n", This, debugstr_w(html));
474 static HRESULT WINAPI HTMLTxtRange_moveToElementText(IHTMLTxtRange *iface, IHTMLElement *element)
476 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
477 FIXME("(%p)->(%p)\n", This, element);
481 static HRESULT WINAPI HTMLTxtRange_setEndPoint(IHTMLTxtRange *iface, BSTR how,
482 IHTMLTxtRange *SourceRange)
484 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
485 FIXME("(%p)->(%s %p)\n", This, debugstr_w(how), SourceRange);
489 static HRESULT WINAPI HTMLTxtRange_compareEndPoints(IHTMLTxtRange *iface, BSTR how,
490 IHTMLTxtRange *SourceRange, long *ret)
492 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
493 HTMLTxtRange *src_range;
498 TRACE("(%p)->(%s %p %p)\n", This, debugstr_w(how), SourceRange, ret);
500 nscmpt = string_to_nscmptype(how);
504 src_range = get_range_object(This->doc, SourceRange);
508 nsres = nsIDOMRange_CompareBoundaryPoints(This->nsrange, nscmpt, src_range->nsrange, &nsret);
510 ERR("CompareBoundaryPoints failed: %08x\n", nsres);
516 static HRESULT WINAPI HTMLTxtRange_findText(IHTMLTxtRange *iface, BSTR String,
517 long count, long Flags, VARIANT_BOOL *Success)
519 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
520 FIXME("(%p)->(%s %ld %08lx %p)\n", This, debugstr_w(String), count, Flags, Success);
524 static HRESULT WINAPI HTMLTxtRange_moveToPoint(IHTMLTxtRange *iface, long x, long y)
526 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
527 FIXME("(%p)->(%ld %ld)\n", This, x, y);
531 static HRESULT WINAPI HTMLTxtRange_getBookmark(IHTMLTxtRange *iface, BSTR *Bookmark)
533 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
534 FIXME("(%p)->(%p)\n", This, Bookmark);
538 static HRESULT WINAPI HTMLTxtRange_moveToBookmark(IHTMLTxtRange *iface, BSTR Bookmark,
539 VARIANT_BOOL *Success)
541 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
542 FIXME("(%p)->(%s %p)\n", This, debugstr_w(Bookmark), Success);
546 static HRESULT WINAPI HTMLTxtRange_queryCommandSupported(IHTMLTxtRange *iface, BSTR cmdID,
549 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
550 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
554 static HRESULT WINAPI HTMLTxtRange_queryCommandEnabled(IHTMLTxtRange *iface, BSTR cmdID,
557 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
558 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
562 static HRESULT WINAPI HTMLTxtRange_queryCommandState(IHTMLTxtRange *iface, BSTR cmdID,
565 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
566 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
570 static HRESULT WINAPI HTMLTxtRange_queryCommandIndeterm(IHTMLTxtRange *iface, BSTR cmdID,
573 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
574 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
578 static HRESULT WINAPI HTMLTxtRange_queryCommandText(IHTMLTxtRange *iface, BSTR cmdID,
581 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
582 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pcmdText);
586 static HRESULT WINAPI HTMLTxtRange_queryCommandValue(IHTMLTxtRange *iface, BSTR cmdID,
589 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
590 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pcmdValue);
594 static HRESULT WINAPI HTMLTxtRange_execCommand(IHTMLTxtRange *iface, BSTR cmdID,
595 VARIANT_BOOL showUI, VARIANT value, VARIANT_BOOL *pfRet)
597 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
598 FIXME("(%p)->(%s %x v %p)\n", This, debugstr_w(cmdID), showUI, pfRet);
602 static HRESULT WINAPI HTMLTxtRange_execCommandShowHelp(IHTMLTxtRange *iface, BSTR cmdID,
605 HTMLTxtRange *This = HTMLTXTRANGE_THIS(iface);
606 FIXME("(%p)->(%s %p)\n", This, debugstr_w(cmdID), pfRet);
610 #undef HTMLTXTRANGE_THIS
612 static const IHTMLTxtRangeVtbl HTMLTxtRangeVtbl = {
613 HTMLTxtRange_QueryInterface,
615 HTMLTxtRange_Release,
616 HTMLTxtRange_GetTypeInfoCount,
617 HTMLTxtRange_GetTypeInfo,
618 HTMLTxtRange_GetIDsOfNames,
620 HTMLTxtRange_get_htmlText,
621 HTMLTxtRange_put_text,
622 HTMLTxtRange_get_text,
623 HTMLTxtRange_parentElement,
624 HTMLTxtRange_duplicate,
625 HTMLTxtRange_inRange,
626 HTMLTxtRange_isEqual,
627 HTMLTxtRange_scrollIntoView,
628 HTMLTxtRange_collapse,
631 HTMLTxtRange_moveStart,
632 HTMLTxtRange_moveEnd,
634 HTMLTxtRange_pasteHTML,
635 HTMLTxtRange_moveToElementText,
636 HTMLTxtRange_setEndPoint,
637 HTMLTxtRange_compareEndPoints,
638 HTMLTxtRange_findText,
639 HTMLTxtRange_moveToPoint,
640 HTMLTxtRange_getBookmark,
641 HTMLTxtRange_moveToBookmark,
642 HTMLTxtRange_queryCommandSupported,
643 HTMLTxtRange_queryCommandEnabled,
644 HTMLTxtRange_queryCommandState,
645 HTMLTxtRange_queryCommandIndeterm,
646 HTMLTxtRange_queryCommandText,
647 HTMLTxtRange_queryCommandValue,
648 HTMLTxtRange_execCommand,
649 HTMLTxtRange_execCommandShowHelp
652 IHTMLTxtRange *HTMLTxtRange_Create(HTMLDocument *doc, nsIDOMRange *nsrange)
654 HTMLTxtRange *ret = mshtml_alloc(sizeof(HTMLTxtRange));
656 ret->lpHTMLTxtRangeVtbl = &HTMLTxtRangeVtbl;
660 nsIDOMRange_AddRef(nsrange);
661 ret->nsrange = nsrange;
664 list_add_head(&doc->range_list, &ret->entry);
666 return HTMLTXTRANGE(ret);
669 void detach_ranges(HTMLDocument *This)
673 LIST_FOR_EACH_ENTRY(iter, &This->range_list, HTMLTxtRange, entry) {