private SidebarControl GetSidebarForCurrentSelection(object selection)
        {
            IHtmlEditorSelection htmlSelection = selection as IHtmlEditorSelection;

            if (selection == null || (!htmlSelection.IsValid && !InlineEditField.IsEditField(selection)))
            {
                return(_defaultSidebarControl);
            }

            foreach (SidebarEntry sidebarEntry in _sidebars)
            {
                ISidebar sidebar = sidebarEntry.Sidebar;
                if (sidebar.AppliesToSelection(selection))
                {
                    SidebarControl sidebarControl = sidebarEntry.SidebarControl;
                    if (sidebarControl == null)
                    {
                        // demand-create sidebar
                        sidebarEntry.SidebarControl         = CreateAndInitializeSidebar(sidebar);
                        sidebarEntry.SidebarControl.Visible = sidebarEntry.SidebarControl.Controls.Count > 0;
                    }
                    return(sidebarEntry.SidebarControl);
                }
            }

            // got this far so no active sidebar for current selection
            return(_defaultSidebarControl);
        }
Example #2
0
        public static bool IsEditField(object selection)
        {
            IHtmlEditorSelection htmlSelection = selection as IHtmlEditorSelection;

            if (htmlSelection != null)
            {
                return(IsEditField(htmlSelection.SelectedMarkupRange.ParentElement()));
            }
            else if (selection is IHTMLElement)
            {
                return(IsEditField((IHTMLElement)selection));
            }
            return(false);
        }
        private void ClearSelection(IHtmlEditorSelection selection)
        {
            using (_damageServices.CreateDeleteDamageTracker(selection.SelectedMarkupRange))
            {
                // allow override by components
                HtmlEditorSelectionOperationEventArgs ea = new HtmlEditorSelectionOperationEventArgs(selection);
                OnClear(ea);

                // if no override then execute
                if (!ea.Handled)
                {
                    GetMshtmlCommand(IDM.DELETE).Execute();
                    // Clear out any state maintained by MSHTML regarding the backcolor.
                    if (selection.SelectedMarkupRange.IsTagId(_ELEMENT_TAG_ID.TAGID_FONT, false))
                        GetMshtmlCommand(IDM.BACKCOLOR).Execute(null);
                }

                FireSelectionChanged();
            }
        }
        private void PasteOverSelection(IHtmlEditorSelection selection)
        {
            if (!IsValidContentInsertionPoint(selection.SelectedMarkupRange))
                return;

            try
            {
                // get the current contents of the clipboard
                DataObjectMeister clipboardMeister = new DataObjectMeister(Clipboard.GetDataObject());

                // see if one of our data handlers wants to handle the paste
                using (DataFormatHandler dataFormatHandler = DataFormatHandlerFactory.CreateFrom(clipboardMeister, DataFormatHandlerContext.ClipboardPaste))
                {
                    if (dataFormatHandler != null)
                    {
                        using (new WaitCursor())
                        {
                            // get selection
                            MarkupRange selectionRange = selection.SelectedMarkupRange.Clone();
                            selectionRange.Start.Cling = false;
                            selectionRange.Start.Gravity = _POINTER_GRAVITY.POINTER_GRAVITY_Left;
                            selectionRange.End.Cling = false;
                            selectionRange.End.Gravity = _POINTER_GRAVITY.POINTER_GRAVITY_Right;

                            // do the paste
                            dataFormatHandler.InsertData(DataAction.Copy, selectionRange.Start, selectionRange.End);

                            MarkupRange newMarkUpRange = MarkupServices.CreateMarkupRange(selectionRange.End.Clone(), selectionRange.End.Clone());
                            MarkupPointerMoveHelper.DriveSelectionToLogicalPosition(newMarkUpRange, PostBodyElement, false);

                            // update selection
                            selectionRange.Start.MoveToPointer(newMarkUpRange.Start);
                            selectionRange.End.MoveToPointer(newMarkUpRange.Start);
                            selectionRange.ToTextRange().select();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //avoid throwing up error dialogs if paste fails.
                Trace.Fail("failed to paste content: " + ex.Message, ex.StackTrace);
            }
        }
        private void CopySelection(IHtmlEditorSelection selection)
        {
            // allow override by components
            HtmlEditorSelectionOperationEventArgs ea = new HtmlEditorSelectionOperationEventArgs(selection);
            OnCopy(ea);

            // if no override then execute
            if (!ea.Handled)
                GetMshtmlCommand(IDM.COPY).Execute();
        }
        private void CutSelection(IHtmlEditorSelection selection)
        {
            using (_damageServices.CreateDeleteDamageTracker(selection.SelectedMarkupRange))
            {
                // allow override by components
                HtmlEditorSelectionOperationEventArgs ea = new HtmlEditorSelectionOperationEventArgs(selection);
                OnCut(ea);

                // if no override then execute
                if (!ea.Handled)
                    GetMshtmlCommand(IDM.CUT).Execute();
            }
        }
        protected void OnSelectionChanged(EventArgs e, IHtmlEditorSelection newSelection, bool moveSelectionToFont)
        {
            _selection = newSelection;

            // fire our own selection changed event
            if (SelectionChanged != null)
                SelectionChanged(this, EventArgs.Empty);

            // fire our command-state changed event
            OnCommandStateChanged();
        }
 public HtmlEditorSelectionOperationEventArgs(IHtmlEditorSelection selection)
 {
     _selection = selection;
 }
Example #9
0
        private void ResetSelection()
        {
            try
            {
                // Keep it within the postBody div!
                _mshtmlEditor.DocumentEvents.SelectionChanged -= new EventHandler(DocumentEvents_SelectionChanged);
                IHTMLElement postBody = PostBodyElement;
                if (postBody != null)
                {
                    MarkupRange range = MarkupServices.CreateMarkupRange(postBody, false);
                    range.Collapse(true);
                    range.ToTextRange().select();
                }
            }
            finally
            {
                _mshtmlEditor.DocumentEvents.SelectionChanged += new EventHandler(DocumentEvents_SelectionChanged);
            }

            _defaultSelection = new HtmlEditorSelection(_mshtmlEditor, HTMLDocument);
            ((IHtmlEditorComponentContext)this).Selection = _defaultSelection;
        }
Example #10
0
        private void DocumentEvents_SelectionChanged(object sender, EventArgs e)
        {
            using (ApplicationPerformance.LogEvent("SelectionChanged"))
            {
                BeginSelectionChangedCaching();

                try
                {

                    if (_mshtmlEditor.MshtmlControl.DocumentIsComplete)
                    {
            #if SELECTION_DEBUG
                        if (SelectionDebugDialog == null)
                        {
                            SelectionDebugDialog = new SelectionDebugDialog();
                            SelectionDebugDialog.Show();
                        }

                        SelectionDebugDialog.Add(SelectedMarkupRange);
            #endif
                        // update selection type
                        if (_defaultSelection == null)
                        {
                            _defaultSelection = new HtmlEditorSelection(MshtmlEditor, HTMLDocument);
                        }

                        ((IHtmlEditorComponentContext)this).Selection = _defaultSelection;

                        if (_suspendSelectionValidationDepth == 0)
                        {
                            //If the selection is contiguous, let the editor decide if its a valid selection
                            //and clear it if it is not.

                            //Resetting the selection may cause another selection change event, which can incorrectly return
                            //a non-empty selection, so to avoid recursive stack overflows, we suspend validation for subsequent
                            //selection changes. (bug 405391)
                            _suspendSelectionValidationDepth++;

                            try
                            {
                                if (!IsValidContiguousSelection())
                                {
                                    if (HasContiguousSelection)
                                    {
                                        if (ShouldEmptySelection())
                                        {
                                            EmptySelection();
                                        }
                                        else
                                        {
                                            // WinLive 196005: In some circumstances we do not want to empty an invalid selection.
                                            if (_selectionIsInvalid == false)
                                            {
                                                _selectionIsInvalid = true;
                                                OnCommandStateChanged();
                                            }
                                        }

                                        return;
                                    }
                                    else
                                    {
                                        if (TryMoveToValidSelection(((IHtmlEditorComponentContext)this).Selection.SelectedMarkupRange))
                                        {
                                            return;
                                        }
                                    }
                                }
                                else
                                {
                                    if (_selectionIsInvalid == true)
                                    {
                                        _selectionIsInvalid = false;
                                        OnCommandStateChanged();
                                    }
                                }
                            }
                            finally
                            {
                                //unsuspend selection validation now that any subsequent selection changes have passed.
                                _suspendSelectionValidationDepth--;
                            }
                        }
                        else
                        {
                            // Are we still seeing the problems that made this _suspendSelectionValidationDepth stuff necessary?
                            // Rather than masking product instability, we should figure out the root cause.
                            // I don't feel good about just removing this immediately, however.
                            // Who knows what code has now been written that depends on this code being in place.
                            // Let's try to determine if we're still hitting the stack overflow scenario.
                            // In time, if we have not seen this assert, then we may want to remove the _suspendSelectionValidationDepth code.
                            Debug.WriteLine("Avoided a recursive stack overflow.  Figure out why it was happening in the first place!");
                        }
                    }
                }
                finally
                {
                    EndSelectionChangedCaching();
                    if (ApplicationPerformance.ContainsEvent("Backspace"))
                        ApplicationPerformance.EndEvent("Backspace");
                }
            }
        }
 public HtmlEditorSelectionOperationEventArgs(IHtmlEditorSelection selection)
 {
     _selection = selection;
 }