/// <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);
        }
Example #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;
        }
Example #3
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);
        }
Example #4
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()
        {
            //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;
        }