示例#1
0
 public void MoveRangeToPointers(MarkupPointer pointerStart, MarkupPointer pointerFinish, HtmlTextRange range)
 {
     Interop.IMarkupPointer pPointerStart  = pointerStart.Native;
     Interop.IMarkupPointer pPointerFinish = pointerFinish.Native;
     Interop.IHTMLTxtRange  pIRange        = range.Native;
     ms.MoveRangeToPointers(pPointerStart, pPointerFinish, pIRange);
 }
        public void WrapSelection(string tag, IDictionary attributes)
        {
            //Create a string for all the attributes
            string attributeString = String.Empty;

            if (attributes != null)
            {
                foreach (string key in attributes.Keys)
                {
                    attributeString += key + "=\"" + attributes[key] + "\" ";
                }
            }
            SynchronizeSelection();
            if (_type == HtmlSelectionType.TextSelection)
            {
                Interop.IHTMLTxtRange textRange = (Interop.IHTMLTxtRange)MSHTMLSelection;
                string oldText = textRange.GetHtmlText();
                if (oldText == null)
                {
                    oldText = String.Empty;
                }
                string newText = "<" + tag + " " + attributeString + ">" + oldText + "</" + tag + ">";
                textRange.PasteHTML(newText);
            }
        }
示例#3
0
 public void MovePointersToRange(HtmlTextRange range, MarkupPointer pointerStart, MarkupPointer pointerFinish)
 {
     Interop.IHTMLTxtRange  pIRange        = range.Native;
     Interop.IMarkupPointer pPointerStart  = pointerStart;
     Interop.IMarkupPointer pPointerFinish = pointerFinish;
     ms.MovePointersToRange(pIRange, pPointerStart, pPointerFinish);
 }
示例#4
0
 internal HighLightSegment(Interop.IHighlightRenderingServices render, Interop.IHTMLRenderStyle renderStyle, Interop.IDisplayPointer dpStart, Interop.IDisplayPointer dpEnd, Interop.IHTMLTxtRange range)
 {
     Interop.IHighlightSegment ppI;
     this.render = render;
     this.range  = range;
     this.render.AddSegment(dpStart, dpEnd, renderStyle, out ppI);
     this.ppISegment = ppI;
 }
示例#5
0
        public void SelectAll()
        {
            Interop.IHTMLTxtRange textRange = null;
            // Select the entire body
            Interop.IHtmlBodyElement bodyElement = MSHTMLDocument.GetBody() as Interop.IHtmlBodyElement;
            Debug.Assert(bodyElement != null, "Couldn't get body element in HtmlControl.Find");

            textRange = bodyElement.createTextRange();
            while (textRange.MoveStart("character", -1) > 0)
            {
                ;
            }
            while (textRange.MoveEnd("character", 1) > 0)
            {
                ;
            }
            textRange.Select();
        }
        public bool Replace(string searchString, string replaceString, bool matchCase, bool wholeWord, bool searchUp)
        {
            // Synchronize first so the selection length is correct
            this.Selection.SynchronizeSelection();
            // Only perform a replace if something is already selected
            if ((this.Selection.Type == HtmlSelectionType.TextSelection) && (this.Selection.Length > 0))
            {
                Interop.IHTMLTxtRange selection = this.Selection.MSHTMLSelection as Interop.IHTMLTxtRange;

                // Set up the flags for matching case and whole word search
                int flags     = (matchCase ? 0x4 : 0) | (wholeWord ? 0x2 : 0);
                int direction = searchUp ? -10000000 : 10000000;
                // Ensure that the selected text conatins the search string
                if (selection.FindText(searchString, direction, flags))
                {
                    // If it does, replace the text
                    selection.SetText(replaceString);
                }
            }

            // Find the next instance of the string
            return(Find(searchString, matchCase, wholeWord, searchUp));
        }
示例#7
0
 /// <summary>
 /// Inserts the specified string into the html over the current selection
 /// </summary>
 /// <param name="html"></param>
 public void InsertHtml(string html)
 {
     Selection.SynchronizeSelection();
     if (Selection.Type == HtmlSelectionType.ElementSelection)
     {
         //If it's a control range, we can only insert if we are in a div or td
         Interop.IHtmlControlRange controlRange = (Interop.IHtmlControlRange)Selection.MSHTMLSelection;
         int selectedItemCount = controlRange.GetLength();
         if (selectedItemCount == 1)
         {
             Interop.IHTMLElement element = controlRange.Item(0);
             if ((String.Compare(element.GetTagName(), "div", true) == 0) ||
                 (String.Compare(element.GetTagName(), "td", true) == 0))
             {
                 element.InsertAdjacentHTML("beforeEnd", html);
             }
         }
     }
     else
     {
         Interop.IHTMLTxtRange textRange = (Interop.IHTMLTxtRange)Selection.MSHTMLSelection;
         textRange.PasteHTML(html);
     }
 }
        /// <summary>
        /// Synchronizes the selection state held in this object with the selection state in MSHTML
        /// </summary>
        /// <returns>true if the selection has changed</returns>
        public bool SynchronizeSelection()
        {
            //Get the selection object from the MSHTML document
            if (_document == null)
            {
                _document = _editor.MSHTMLDocument;
            }
            Interop.IHTMLSelectionObject selectionObj = _document.GetSelection();

            //Get the current selection from that selection object
            object currentSelection = null;

            try
            {
                currentSelection = selectionObj.CreateRange();
            }
            catch
            {
            }

            ArrayList         oldItems = _items;
            HtmlSelectionType oldType  = _type;
            int oldLength = _selectionLength;

            //Default to an empty selection
            _type            = HtmlSelectionType.Empty;
            _selectionLength = 0;
            if (currentSelection != null)
            {
                _mshtmlSelection = currentSelection;
                _items           = new ArrayList();
                //If it's a text selection
                if (currentSelection is Interop.IHTMLTxtRange)
                {
                    Interop.IHTMLTxtRange textRange = (Interop.IHTMLTxtRange)currentSelection;
                    //IntPtr ptr = Marshal.GetIUnknownForObject(textRange);
                    Interop.IHTMLElement parentElement = textRange.ParentElement();
                    // If the document is in full document mode or we're selecting a non-body tag, allow it to select
                    // otherwise, leave the selection as empty (since we don't want the body tag to be selectable on an ASP.NET
                    // User Control
                    if (IsSelectableElement(Element.GetWrapperFor(parentElement, _editor)))
                    {
                        //Add the parent of the text selection
                        if (parentElement != null)
                        {
                            _text = textRange.GetText();
                            if (_text != null)
                            {
                                _selectionLength = _text.Length;
                            }
                            else
                            {
                                _selectionLength = 0;
                            }
                            _type = HtmlSelectionType.TextSelection;
                            _items.Add(parentElement);
                        }
                    }
                }
                //If it's a control selection
                else if (currentSelection is Interop.IHtmlControlRange)
                {
                    Interop.IHtmlControlRange controlRange = (Interop.IHtmlControlRange)currentSelection;
                    int selectedCount = controlRange.GetLength();
                    //Add all elements selected
                    if (selectedCount > 0)
                    {
                        _type = HtmlSelectionType.ElementSelection;
                        for (int i = 0; i < selectedCount; i++)
                        {
                            Interop.IHTMLElement currentElement = controlRange.Item(i);
                            _items.Add(currentElement);
                        }
                        _selectionLength = selectedCount;
                    }
                }
            }
            _sameParentValid = false;

            bool selectionChanged = false;

            //Now check if there was a change of selection
            //If the two selections have different lengths, then the selection has changed
            if (_type != oldType)
            {
                selectionChanged = true;
            }
            else if (_selectionLength != oldLength)
            {
                selectionChanged = true;
            }
            else
            {
                if (_items != null)
                {
                    //If the two selections have a different element, then the selection has changed
                    for (int i = 0; i < _items.Count; i++)
                    {
                        if (_items[i] != oldItems[i])
                        {
                            selectionChanged = true;
                            break;
                        }
                    }
                }
            }
            if (selectionChanged)
            {
                //Set _elements to null so no one can retrieve a dirty copy of the selection element wrappers
                _elements = null;

                OnSelectionChanged();
                return(true);
            }

            return(false);
        }
        public bool SelectElements(ICollection elements)
        {
            Interop.IHTMLElement       body      = _editor.MSHTMLDocument.GetBody();
            Interop.IHTMLTextContainer container = body as Interop.IHTMLTextContainer;
            Debug.Assert(container != null);
            object controlRange = container.createControlRange();

            Interop.IHtmlControlRange htmlControlRange = controlRange as Interop.IHtmlControlRange;
            Debug.Assert(htmlControlRange != null);
            if (htmlControlRange == null)
            {
                return(false);
            }

            Interop.IHtmlControlRange2 htmlControlRange2 = controlRange as Interop.IHtmlControlRange2;
            Debug.Assert(htmlControlRange2 != null);
            if (htmlControlRange2 == null)
            {
                return(false);
            }


            int hr = 0;

            foreach (object o in elements)
            {
                Interop.IHTMLElement element = GetIHtmlElement(o);
                if (element == null)
                {
                    return(false);
                }
                hr = htmlControlRange2.addElement(element);
                if (hr != Interop.S_OK)
                {
                    break;
                }
            }
            if (hr == Interop.S_OK)
            {
                //If it succeeded, simply select the control range
                htmlControlRange.Select();
            }
            else
            {
                // elements like DIV and SPAN, w/o layout, cannot be added to a control selelction.
                Interop.IHtmlBodyElement bodyElement = (Interop.IHtmlBodyElement)body;
                Interop.IHTMLTxtRange    textRange   = bodyElement.createTextRange();
                if (textRange != null)
                {
                    foreach (object o in elements)
                    {
                        try
                        {
                            Interop.IHTMLElement element = GetIHtmlElement(o);
                            if (element == null)
                            {
                                return(false);
                            }
                            textRange.MoveToElementText(element);
                        }
                        catch
                        {
                        }
                    }
                    textRange.Select();
                }
            }
            return(true);
        }
示例#10
0
        /// <summary>
        /// Finds the specified string in the content of the control and selects it if it exists
        /// </summary>
        /// <param name="searchString">The string to find</param>
        /// <param name="matchCase">Set to true to match casing of the string</param>
        /// <param name="wholeWord">Set to true to only find whole words</param>
        /// <returns></returns>
        public bool Find(string searchString, bool matchCase, bool wholeWord, bool searchUp)
        {
            Interop.IHTMLSelectionObject selectionObj = MSHTMLDocument.GetSelection();

            // Check if a selection actually exists
            bool selectionExists = false;

            if (selectionObj != null)
            {
                selectionExists = selectionObj.GetSelectionType().Equals("Text");
            }
            Interop.IHTMLTxtRange textRange = null;
            if (selectionExists)
            {
                object o = selectionObj.CreateRange();
                textRange = o as Interop.IHTMLTxtRange;
            }
            if (textRange == null)
            {
                // If no selection exists, select the entire body
                Interop.IHtmlBodyElement bodyElement = MSHTMLDocument.GetBody() as Interop.IHtmlBodyElement;
                Debug.Assert(bodyElement != null, "Couldn't get body element in HtmlControl.Find");
                selectionExists = false;
                textRange       = bodyElement.createTextRange();
            }

            // Set up the bounds of the search
            if (searchUp)
            {
                // If we're search up in the document
                if (selectionExists)
                {
                    // If a selection exists, move the range's end to one character before the selection
                    textRange.MoveEnd("character", -1);
                }
                // Move the range's beginning to the start of the document
                int temp = 1;
                while (temp == 1)
                {
                    temp = textRange.MoveStart("textedit", -1);
                }
            }
            else
            {
                // If we're searching down in the document
                if (selectionExists)
                {
                    // If a selection exists, start one char after the selection
                    textRange.MoveStart("character", 1);
                }
                // Move the range's end to the end of the document
                int temp = 1;
                while (temp == 1)
                {
                    temp = textRange.MoveEnd("textedit", 1);
                }
            }

            // Set up the flags for matching case and whole word search
            int flags     = (matchCase ? 0x4 : 0) | (wholeWord ? 0x2 : 0);
            int direction = searchUp ? -10000000 : 10000000;

            //Do the search
            bool success = textRange.FindText(searchString, direction, flags);

            if (success)
            {
                // If we succeeded, select the text, scroll it into view, and we're done!
                textRange.Select();
                textRange.ScrollIntoView(true);
                return(true);
            }
            else if (selectionExists)
            {
                // If we only searched a portion of the document
                // we need to wrap around the document...
                textRange = selectionObj.CreateRange() as Interop.IHTMLTxtRange;
                // Set up the bounds of the search
                if (searchUp)
                {
                    // If we're searching up in the document
                    // Start one char after the selection
                    textRange.MoveStart("character", 1);
                    // Move the range's end to the end of the document
                    int temp = 1;
                    while (temp == 1)
                    {
                        temp = textRange.MoveEnd("textedit", 1);
                    }
                }
                else
                {
                    // If we're searching down in the document
                    // Move the range's end to one character before the selection
                    textRange.MoveEnd("character", -1);
                    // Move the range's beginning to the start of the document
                    int temp = 1;
                    while (temp == 1)
                    {
                        temp = textRange.MoveStart("textedit", -1);
                    }
                }

                success = textRange.FindText(searchString, direction, flags);
                if (success)
                {
                    // If we succeeded, select the text, scroll it into view, and we're done!
                    textRange.Select();
                    textRange.ScrollIntoView(true);
                    return(true);
                }
            }
            return(false);
        }
示例#11
0
 internal HtmlTextRange(Interop.IHTMLTxtRange tr, IHtmlEditor editor)
 {
     this.editor = editor;
     this.tr     = tr;
 }