Beispiel #1
0
 public void InsertHtml(string html)
 {
     this.Selection.SynchronizeSelection();
     if (this.Selection.Type == HtmlSelectionType.ElementSelection)
     {
         //If it's a control range, we can only insert if we are in a div or td
         NativeMethods.IHtmlControlRange controlRange = (NativeMethods.IHtmlControlRange)Selection.Selection;
         int selectedItemCount = controlRange.GetLength();
         if (selectedItemCount == 1)
         {
             NativeMethods.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
     {
         NativeMethods.IHTMLTxtRange textRange = (NativeMethods.IHTMLTxtRange)Selection.Selection;
         textRange.PasteHTML(html);
     }
 }
Beispiel #2
0
        /// <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()
        {
            if (this.document == null)
            {
                this.document = this.control.HtmlDocument;
            }

            NativeMethods.IHTMLSelectionObject selection = this.document.GetSelection();
            object currentSelection = null;

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

            ArrayList         oldItems = this.items;
            HtmlSelectionType oldType  = this.type;
            int oldLength = this.selectionLength;

            // Default to an empty selection
            this.type            = HtmlSelectionType.Empty;
            this.selectionLength = 0;
            if (currentSelection != null)
            {
                this.selection = currentSelection;
                this.items     = new ArrayList();
                //If it's a text selection
                if (currentSelection is NativeMethods.IHTMLTxtRange)
                {
                    NativeMethods.IHTMLTxtRange textRange     = (NativeMethods.IHTMLTxtRange)currentSelection;
                    NativeMethods.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 (this.IsSelectableElement(new HtmlElement(parentElement, this.control)))
                    {
                        // Add the parent of the text selection
                        if (parentElement != null)
                        {
                            this.text = textRange.GetText();
                            if (this.text != null)
                            {
                                this.selectionLength = this.text.Length;
                            }
                            else
                            {
                                this.selectionLength = 0;
                            }
                            this.type = HtmlSelectionType.TextSelection;
                            this.items.Add(parentElement);
                        }
                    }
                }
                // If it is a control selection
                else if (currentSelection is NativeMethods.IHtmlControlRange)
                {
                    NativeMethods.IHtmlControlRange controlRange = (NativeMethods.IHtmlControlRange)currentSelection;
                    int selectedCount = controlRange.GetLength();

                    // Add all elements selected
                    if (selectedCount > 0)
                    {
                        this.type = HtmlSelectionType.ElementSelection;
                        for (int i = 0; i < selectedCount; i++)
                        {
                            NativeMethods.IHTMLElement currentElement = controlRange.Item(i);
                            this.items.Add(currentElement);
                        }
                        this.selectionLength = selectedCount;
                    }
                }
            }

            this.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 (this.type != oldType)
            {
                selectionChanged = true;
            }
            else if (this.selectionLength != oldLength)
            {
                selectionChanged = true;
            }
            else
            {
                if (this.items != null)
                {
                    //If the two selections have a different element, then the selection has changed
                    for (int i = 0; i < this.items.Count; i++)
                    {
                        if (this.items[i] != oldItems[i])
                        {
                            selectionChanged = true;
                            break;
                        }
                    }
                }
            }

            if (selectionChanged)
            {
                //Set this.elements to null so no one can retrieve a dirty copy of the selection element wrappers
                this.elements = null;

                OnSelectionChanged(EventArgs.Empty);
                return(true);
            }

            return(false);
        }
Beispiel #3
0
        public bool SelectElements(ICollection elements)
        {
            NativeMethods.IHTMLElement       body      = this.control.HtmlDocument.GetBody();
            NativeMethods.IHTMLTextContainer container = body as NativeMethods.IHTMLTextContainer;
            Debug.Assert(container != null);
            object controlRange = container.createControlRange();

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

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

            int hr = 0;

            foreach (object o in elements)
            {
                NativeMethods.IHTMLElement element = this.GetHtmlElement(o);
                if (element == null)
                {
                    return(false);
                }
                hr = htmlControlRange2.addElement(element);
                if (hr != NativeMethods.S_OK)
                {
                    break;
                }
            }
            if (hr == NativeMethods.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.
                NativeMethods.IHtmlBodyElement bodyElement = (NativeMethods.IHtmlBodyElement)body;
                NativeMethods.IHTMLTxtRange    textRange   = bodyElement.createTextRange();
                if (textRange != null)
                {
                    foreach (object o in elements)
                    {
                        try
                        {
                            NativeMethods.IHTMLElement element = this.GetHtmlElement(o);
                            if (element == null)
                            {
                                return(false);
                            }
                            textRange.MoveToElementText(element);
                        }
                        catch
                        {
                        }
                    }
                    textRange.Select();
                }
            }
            return(true);
        }