Back to index

lightning-sunbird  0.9+nobinonly
IEHtmlSelectionObject.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Alexandre Trémon <atremon@elansoftware.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "stdafx.h"
00040 #include "IEHtmlSelectionObject.h"
00041 #include "IEHtmlTxtRange.h"
00042 
00043 CIEHtmlSelectionObject::CIEHtmlSelectionObject()
00044 {
00045 }
00046 
00047 CIEHtmlSelectionObject::~CIEHtmlSelectionObject()
00048 {
00049 }
00050 
00051 void CIEHtmlSelectionObject::SetSelection(nsISelection *pSelection)
00052 {
00053     mSelection = pSelection;
00054 }
00055 
00056 void CIEHtmlSelectionObject::SetDOMDocumentRange(nsIDOMDocumentRange *pDOMDocumentRange) {
00057     mDOMDocumentRange = pDOMDocumentRange;
00058 }
00059 
00060 HRESULT STDMETHODCALLTYPE CIEHtmlSelectionObject::createRange(IDispatch __RPC_FAR *__RPC_FAR *range)
00061 {
00062     if (range == NULL)
00063         return E_INVALIDARG;
00064     *range = NULL;
00065     if (!mSelection)
00066         return E_FAIL;
00067     // get range corresponding to mSelection:
00068     nsCOMPtr<nsIDOMRange> domRange;
00069     int rc;
00070     mSelection->GetRangeCount(&rc);
00071     if (rc>0)
00072         mSelection->GetRangeAt(0, getter_AddRefs(domRange));
00073     else
00074     {
00075         // create empty range
00076         mDOMDocumentRange->CreateRange(getter_AddRefs(domRange));
00077     }
00078     if (!domRange)
00079         return E_FAIL;
00080     // create com object:
00081     CComBSTR strType;
00082     get_type(&strType);
00083     if (wcscmp(OLE2W(strType), L"Control") == 0)
00084     {
00085         // IHTMLControlRange:
00086         CIEHtmlControlRangeInstance *pControlRange = NULL;
00087         CIEHtmlControlRangeInstance::CreateInstance(&pControlRange);
00088         if (!pControlRange)
00089             return E_FAIL;
00090         // gives the range to the com object:
00091         pControlRange->SetRange(domRange);
00092         // return com object:
00093         pControlRange->QueryInterface(IID_IDispatch, (void **)range);
00094     } else
00095     {
00096         // IHTMLTxtRange:
00097         CIEHtmlTxtRangeInstance *pTxtRange = NULL;
00098         CIEHtmlTxtRangeInstance::CreateInstance(&pTxtRange);
00099         if (!pTxtRange)
00100             return E_FAIL;
00101         // gives the range to the com object:
00102         pTxtRange->SetRange(domRange);
00103         // return com object:
00104         pTxtRange->QueryInterface(IID_IDispatch, (void **)range);
00105     }
00106     return S_OK;
00107 }
00108 
00109 HRESULT STDMETHODCALLTYPE CIEHtmlSelectionObject::get_type(BSTR __RPC_FAR *p)
00110 {
00111     static NS_NAMED_LITERAL_STRING(strText,"Text");
00112     static NS_NAMED_LITERAL_STRING(strControl,"Control");
00113     static NS_NAMED_LITERAL_STRING(strNone,"None");
00114     nsCOMPtr<nsIDOMRange> domRange;
00115     nsCOMPtr<nsIDOMNode> domNode;
00116 
00117     if (p == NULL)
00118         return E_INVALIDARG;
00119     *p = NULL;
00120     
00121     // get range corresponding to mSelection:
00122     int rc;
00123     mSelection->GetRangeCount(&rc);
00124     if (rc<1) {
00125         *p = SysAllocString(strNone.get());
00126         return p ? S_OK : E_OUTOFMEMORY;
00127     }
00128     mSelection->GetRangeAt(0, getter_AddRefs(domRange));
00129     if (!domRange)
00130         return E_FAIL;
00131     domRange->GetStartContainer(getter_AddRefs(domNode));
00132     if (!domNode)
00133         return E_FAIL;
00134     unsigned short nodeType;
00135     domNode->GetNodeType(&nodeType);
00136     switch (nodeType)
00137     {
00138     case nsIDOMNode::ELEMENT_NODE:
00139         *p = SysAllocString(strControl.get());
00140         break;
00141     case nsIDOMNode::TEXT_NODE:
00142         *p = SysAllocString(strText.get());
00143         break;
00144     default:
00145         return E_UNEXPECTED;
00146     }
00147     return p ? S_OK : E_OUTOFMEMORY;
00148 }
00149 
00150 HRESULT STDMETHODCALLTYPE CIEHtmlSelectionObject::empty()
00151 {
00152     if (!mSelection)
00153         return E_FAIL;
00154     mSelection->RemoveAllRanges();
00155     return S_OK;
00156 }
00157 
00158 HRESULT STDMETHODCALLTYPE CIEHtmlSelectionObject::clear()
00159 {
00160     if (!mSelection)
00161         return E_FAIL;
00162     mSelection->DeleteFromDocument();
00163     return S_OK;
00164 }
00165