Пример #1
0
        /// <summary>
        /// Paste command QueryStatus handler
        /// </summary>
        private static void OnQueryStatusPaste(object target, CanExecuteRoutedEventArgs args)
        {
            TextEditor This = TextEditor._GetTextEditor(target);

            if (This == null || !This._IsEnabled || This.IsReadOnly)
            {
                return;
            }

            args.Handled = true;

            try
            {
                if (SecurityHelper.CallerHasAllClipboardPermission())
                {
                    // Define what format our paste mechanism recognizes on the clipbord appropriate for this selection
                    string formatToApply = GetPasteApplyFormat(This, Clipboard.GetDataObject());

                    args.CanExecute = formatToApply.Length > 0;
                }
                else
                {
                    // Simplified version of clipboard sniffing for partial trust
                    args.CanExecute = Clipboard.IsClipboardPopulated();
                }
            }
            catch (ExternalException)
            {
                // Clipboard is failed to get the data object.
                // One of reason should be the opening fail of Clipboard while other
                // process opens the clipboard or missing close of Clipboard.
                args.CanExecute = false;
            }
        }
        // Token: 0x0600385A RID: 14426 RVA: 0x000FC080 File Offset: 0x000FA280
        private static void OnQueryStatusPaste(object target, CanExecuteRoutedEventArgs args)
        {
            TextEditor textEditor = TextEditor._GetTextEditor(target);

            if (textEditor == null || !textEditor._IsEnabled || textEditor.IsReadOnly)
            {
                return;
            }
            args.Handled = true;
            try
            {
                if (SecurityHelper.CallerHasAllClipboardPermission())
                {
                    string pasteApplyFormat = TextEditorCopyPaste.GetPasteApplyFormat(textEditor, Clipboard.GetDataObject());
                    args.CanExecute = (pasteApplyFormat.Length > 0);
                }
                else
                {
                    args.CanExecute = Clipboard.IsClipboardPopulated();
                }
            }
            catch (ExternalException)
            {
                args.CanExecute = false;
            }
        }
        internal static bool _DoPaste(TextEditor This, IDataObject dataObject, bool isDragDrop)
        {
            if (!SecurityHelper.CallerHasAllClipboardPermission())
            {
                return(false);
            }
            Invariant.Assert(dataObject != null);
            bool   result        = false;
            string formatToApply = TextEditorCopyPaste.GetPasteApplyFormat(This, dataObject);
            DataObjectPastingEventArgs dataObjectPastingEventArgs;

            try
            {
                dataObjectPastingEventArgs = new DataObjectPastingEventArgs(dataObject, isDragDrop, formatToApply);
            }
            catch (ArgumentException)
            {
                return(result);
            }
            This.UiScope.RaiseEvent(dataObjectPastingEventArgs);
            if (!dataObjectPastingEventArgs.CommandCancelled)
            {
                IDataObject dataObject2 = dataObjectPastingEventArgs.DataObject;
                formatToApply = dataObjectPastingEventArgs.FormatToApply;
                result        = TextEditorCopyPaste.PasteContentData(This, dataObject, dataObject2, formatToApply);
            }
            return(result);
        }
Пример #4
0
        /// <summary>
        /// Paste contents of data object into text selection
        /// </summary>
        /// <param name="This"></param>
        /// <param name="dataObject">
        /// data object containing data to paste
        /// </param>
        /// <param name="isDragDrop">
        /// </param>
        /// <returns>
        /// true if successful, false otherwise
        /// </returns>
        internal static bool _DoPaste(TextEditor This, IDataObject dataObject, bool isDragDrop)
        {
            // Don't try anything if the caller doesn't have the rights to read from the clipboard...
            //
            if (!SecurityHelper.CallerHasAllClipboardPermission())
            {
                return(false);
            }

            Invariant.Assert(dataObject != null);

            // Choose what format we are going to paste
            string formatToApply;
            bool   pasted;

            pasted = false;

            // Get the default paste content applying format
            formatToApply = GetPasteApplyFormat(This, dataObject);

            DataObjectPastingEventArgs dataObjectPastingEventArgs;

            try
            {
                // Let the application to participate in Paste process
                dataObjectPastingEventArgs = new DataObjectPastingEventArgs(dataObject, isDragDrop, formatToApply);
            }
            catch (ArgumentException)
            {
                // Clipboard can be changed by set new or empty data during creating
                // DataObjectPastingEvent that check the representing of the
                // formatToApply. Do nothing if we encounter AgrumentException.
                return(pasted);
            }

            // Public event call - could raise recoverable exception.
            This.UiScope.RaiseEvent(dataObjectPastingEventArgs);

            if (!dataObjectPastingEventArgs.CommandCancelled)
            {
                // When custom handler decides to suggest its own data,
                // it must create a new instance of DataObject and put it
                // into DataObjectPastingEventArgs.DataObject property.
                // Exisiting DataObject is on global Clipboard and can not be changed.
                // Here we need to get this potentially changed instance
                // of DataObject
                IDataObject dataObjectToApply = dataObjectPastingEventArgs.DataObject;

                formatToApply = dataObjectPastingEventArgs.FormatToApply;

                // Paste the content data(Text, Unicode, Xaml and Rtf) to the current text selection
                pasted = PasteContentData(This, dataObject, dataObjectToApply, formatToApply);
            }

            return(pasted);
        }
Пример #5
0
        /// <summary>
        /// Copy worker.
        /// </summary>
        internal static void Copy(TextEditor This, bool userInitiated)
        {
            if (userInitiated)
            {
                // Fail silently if the app explicitly denies clipboard access.
                try
                {
                    new UIPermission(UIPermissionClipboard.OwnClipboard).Demand();
                }
                catch (SecurityException)
                {
                    return;
                }
            }
            else if (!SecurityHelper.CallerHasAllClipboardPermission())
            {
                // Fail silently if we don't have clipboard permission.
                return;
            }

            TextEditorTyping._FlushPendingInputItems(This);

            TextEditorTyping._BreakTypingSequence(This);

            if (This.Selection != null && !This.Selection.IsEmpty)
            {
                // Note: _CreateDataObject raises a public event which might throw a recoverable exception.
                DataObject dataObject = TextEditorCopyPaste._CreateDataObject(This, /*isDragDrop:*/ false);

                if (dataObject != null)
                {
                    try
                    {
                        // The copy command was not terminated by application
                        // One of reason should be the opening fail of Clipboard by the destroyed hwnd.
                        Clipboard.CriticalSetDataObject(dataObject, true);
                    }
                    catch (ExternalException)
                        when(!FrameworkCompatibilityPreferences.ShouldThrowOnCopyOrCutFailure)
                        {
                            // Clipboard is failed to set the data object.
                            return;
                        }
                }
            }

            // Do not clear springload formatting
        }
        internal static void Cut(TextEditor This, bool userInitiated)
        {
            if (userInitiated)
            {
                try
                {
                    new UIPermission(UIPermissionClipboard.OwnClipboard).Demand();
                    goto IL_1E;
                }
                catch (SecurityException)
                {
                    return;
                }
            }
            if (!SecurityHelper.CallerHasAllClipboardPermission())
            {
                return;
            }
IL_1E:
            TextEditorTyping._FlushPendingInputItems(This);
            TextEditorTyping._BreakTypingSequence(This);
            if (This.Selection != null && !This.Selection.IsEmpty)
            {
                DataObject dataObject = TextEditorCopyPaste._CreateDataObject(This, false);
                if (dataObject != null)
                {
                    try
                    {
                        Clipboard.CriticalSetDataObject(dataObject, true);
                    }
                    catch (ExternalException obj) when(!FrameworkCompatibilityPreferences.ShouldThrowOnCopyOrCutFailure)
                    {
                        return;
                    }
                    using (This.Selection.DeclareChangeBlock())
                    {
                        TextEditorSelection._ClearSuggestedX(This);
                        This.Selection.Text = string.Empty;
                        if (This.Selection is TextSelection)
                        {
                            ((TextSelection)This.Selection).ClearSpringloadFormatting();
                        }
                    }
                }
            }
        }
        internal static void Paste(TextEditor This)
        {
            if (!SecurityHelper.CallerHasAllClipboardPermission())
            {
                return;
            }
            if (This.Selection.IsTableCellRange)
            {
                return;
            }
            TextEditorTyping._FlushPendingInputItems(This);
            TextEditorTyping._BreakTypingSequence(This);
            IDataObject dataObject;

            try
            {
                dataObject = Clipboard.GetDataObject();
            }
            catch (ExternalException)
            {
                dataObject = null;
            }
            bool coversEntireContent = This.Selection.CoversEntireContent;

            if (dataObject != null)
            {
                using (This.Selection.DeclareChangeBlock())
                {
                    TextEditorSelection._ClearSuggestedX(This);
                    if (TextEditorCopyPaste._DoPaste(This, dataObject, false))
                    {
                        This.Selection.SetCaretToPosition(This.Selection.End, LogicalDirection.Backward, false, true);
                        if (This.Selection is TextSelection)
                        {
                            ((TextSelection)This.Selection).ClearSpringloadFormatting();
                        }
                    }
                }
            }
            if (coversEntireContent)
            {
                This.Selection.ValidateLayout();
            }
        }
        internal static void Copy(TextEditor This, bool userInitiated)
        {
            if (userInitiated)
            {
                try
                {
                    new UIPermission(UIPermissionClipboard.OwnClipboard).Demand();
                    goto IL_1B;
                }
                catch (SecurityException)
                {
                    return;
                }
            }
            if (!SecurityHelper.CallerHasAllClipboardPermission())
            {
                return;
            }
IL_1B:
            TextEditorTyping._FlushPendingInputItems(This);
            TextEditorTyping._BreakTypingSequence(This);
            if (This.Selection != null && !This.Selection.IsEmpty)
            {
                DataObject dataObject = TextEditorCopyPaste._CreateDataObject(This, false);
                if (dataObject != null)
                {
                    try
                    {
                        Clipboard.CriticalSetDataObject(dataObject, true);
                    }
                    catch (ExternalException obj) when(!FrameworkCompatibilityPreferences.ShouldThrowOnCopyOrCutFailure)
                    {
                    }
                }
            }
        }
Пример #9
0
        /// <summary>
        /// Paste worker.
        /// </summary>
        internal static void Paste(TextEditor This)
        {
            // Don't try anything if the caller doesn't have the rights to read from the clipboard...
            if (!SecurityHelper.CallerHasAllClipboardPermission())
            {
                return;
            }

            if (This.Selection.IsTableCellRange)
            {
                //  We do not support clipboard for table selection so far
                // Word behavior: When source range is text segment then this segment is pasted
                // into each table cell overriding its current content.
                // If source range is table range then it is repeated on target -
                // cell by cell in circular manner.
                return;
            }

            TextEditorTyping._FlushPendingInputItems(This);

            TextEditorTyping._BreakTypingSequence(This);

            // Get DataObject from the Clipboard
            IDataObject dataObject;

            try
            {
                dataObject = Clipboard.GetDataObject();
            }
            catch (ExternalException)
            {
                // Clipboard is failed to get the data object.
                // One of reason should be the opening fail of Clipboard by the destroyed hwnd.
                dataObject = null;
                //  must re-throw ???
            }

            bool forceLayoutUpdate = This.Selection.CoversEntireContent;

            if (dataObject != null)
            {
                using (This.Selection.DeclareChangeBlock())
                {
                    // Forget previously suggested horizontal position
                    TextEditorSelection._ClearSuggestedX(This);

                    // _DoPaste raises a public event -- could raise recoverable exception.
                    if (TextEditorCopyPaste._DoPaste(This, dataObject, /*isDragDrop:*/ false))
                    {
                        // Collapse selection to the end
                        // Use backward direction to stay oriented towards pasted content
                        This.Selection.SetCaretToPosition(This.Selection.End, LogicalDirection.Backward, /*allowStopAtLineEnd:*/ false, /*allowStopNearSpace:*/ true);

                        // Clear springload formatting
                        if (This.Selection is TextSelection)
                        {
                            ((TextSelection)This.Selection).ClearSpringloadFormatting();
                        }
                    }
                } // PUBLIC EVENT RAISED HERE AS CHANGEBLOCK CLOSES!
            }

            // If we replaced the entire document content, background layout will
            // kick in.  Force it to complete now.
            if (forceLayoutUpdate)
            {
                This.Selection.ValidateLayout();
            }
        }
Пример #10
0
        internal static void Cut(TextEditor This, bool userInitiated)
        {
            if (userInitiated)
            {
                // Fail silently if the app explicitly denies clipboard access.
                try
                {
                    new UIPermission(UIPermissionClipboard.OwnClipboard).Demand();
                }
                catch (SecurityException)
                {
                    return;
                }
            }
            else if (!SecurityHelper.CallerHasAllClipboardPermission())
            {
                // Fail silently if we don't have clipboard permission.
                return;
            }

            TextEditorTyping._FlushPendingInputItems(This);

            TextEditorTyping._BreakTypingSequence(This);

            if (This.Selection != null && !This.Selection.IsEmpty)
            {
                // Copy content onto the clipboard

                // Note: _CreateDataObject raises a public event which might throw a recoverable exception.
                DataObject dataObject = TextEditorCopyPaste._CreateDataObject(This, /*isDragDrop:*/ false);

                if (dataObject != null)
                {
                    try
                    {
                        // The copy command was not terminated by application
                        // One of reason should be the opening fail of Clipboard by the destroyed hwnd.
                        Clipboard.CriticalSetDataObject(dataObject, true);
                    }
                    catch (ExternalException)
                    {
                        // Clipboard is failed to set the data object.
                        return;
                    }

                    // Delete selected content
                    using (This.Selection.DeclareChangeBlock())
                    {
                        // Forget previously suggested horizontal position
                        TextEditorSelection._ClearSuggestedX(This);
                        This.Selection.Text = String.Empty;

                        // Clear springload formatting
                        if (This.Selection is TextSelection)
                        {
                            ((TextSelection)This.Selection).ClearSpringloadFormatting();
                        }
                    }
                }
            }
        }
Пример #11
0
        /// <summary>
        /// Kopiuje pojedynczą komórkę do schowka.
        /// </summary>
        public void CopyCellContentToClipboard()
        {
            DataGridCellInfo cellInfo;
            DataGridColumn   currentColumn = null;
            object           currentItem   = null;
            string           format        = DataFormats.UnicodeText;

            if (this.CurrentCell != null)
            {
                cellInfo = this.CurrentCell;
            }
            else if (this.SelectedCells.Count > 0)
            {
                cellInfo = this.SelectedCells[0];
            }

            currentColumn = cellInfo.Column;

            if (currentColumn == null)
            {
                if (this.CurrentCell != null && this.CurrentCell.Column != null)
                {
                    currentColumn = this.CurrentCell.Column;
                }
                else if (this.SelectedCells.Count > 0)
                {
                    currentColumn = this.SelectedCells[0].Column;
                }
            }

            if (currentColumn == null)
            {
                return;
            }

            if (cellInfo.Item != null)
            {
                currentItem = cellInfo.Item;
            }
            else if (this.CurrentItem != null)
            {
                currentItem = this.CurrentItem;
            }
            else if (this.SelectedCells.Count > 0 && this.SelectedCells[0].Item != null)
            {
                currentItem = this.SelectedCells[0].Item;
            }

            if (currentItem == null)
            {
                return;
            }

            var value = currentColumn.OnCopyingCellClipboardContent(currentItem);

            if (value == null || value == DBNull.Value)
            {
                value = "";
            }

            StringBuilder sb = new StringBuilder();

            DataGridBoldClipboardHelper.FormatPlainText(value, sb, format);

            DataObject dataObject;
            bool       hasPerms = SecurityHelper.CallerHasAllClipboardPermission() && SecurityHelper.CallerHasSerializationPermission();

            // Copy unconditionally in full trust.
            // Only copy in partial trust if user initiated.
            if (hasPerms)
            {
                (new UIPermission(UIPermissionClipboard.AllClipboard)).Assert();
                try
                {
                    dataObject = new DataObject();
                }
                finally
                {
                    UIPermission.RevertAssert();
                }

                dataObject.SetData(format, sb.ToString(), false /*autoConvert*/);

                // This assert is there for an OLE Callback to register CSV type for the clipboard
                (new SecurityPermission(SecurityPermissionFlag.SerializationFormatter | SecurityPermissionFlag.UnmanagedCode)).Assert();
                try
                {
                    if (dataObject != null)
                    {
                        Clipboard.SetDataObject(dataObject, true /* Copy */);
                    }
                }
                finally
                {
                    SecurityPermission.RevertAll();
                }
            }
        }
Пример #12
0
        private void OnExecutedCopyInternal(ExecutedRoutedEventArgs args)
        {
            if (ClipboardCopyMode == DataGridClipboardCopyMode.None)
            {
                throw new NotSupportedException("Cannot perform copy if ClipboardCopyMode is None.");
            }

            args.Handled = true;

            // Supported default formats: Html, Text, UnicodeText and CSV
            Collection <string> formats = new Collection <string>(new string[] { DataFormats.Html, DataFormats.Text, DataFormats.UnicodeText, DataFormats.CommaSeparatedValue });
            Dictionary <string, StringBuilder> dataGridStringBuilders = new Dictionary <string, StringBuilder>(formats.Count);

            foreach (string format in formats)
            {
                dataGridStringBuilders[format] = new StringBuilder();
            }

            int minRowIndex;
            int maxRowIndex;
            int minColumnDisplayIndex;
            int maxColumnDisplayIndex;

            // Get the bounding box of the selected cells
            if (this.GetSelectionRange(out minColumnDisplayIndex, out maxColumnDisplayIndex, out minRowIndex, out maxRowIndex))
            {
                // Add column headers if enabled
                if (ClipboardCopyMode == DataGridClipboardCopyMode.IncludeHeader)
                {
                    DataGridRowClipboardEventArgs preparingRowClipboardContentEventArgs = new DataGridRowClipboardEventArgs(null, minColumnDisplayIndex, maxColumnDisplayIndex, true /*IsColumnHeadersRow*/);
                    OnCopyingRowClipboardContent(preparingRowClipboardContentEventArgs);

                    foreach (string format in formats)
                    {
                        dataGridStringBuilders[format].Append(preparingRowClipboardContentEventArgs.FormatClipboardCellValues(format));
                    }
                }

                // Add each selected row
                for (int i = minRowIndex; i <= maxRowIndex; i++)
                {
                    object row = Items[i];

                    // Row has a selecion
                    if (this.SelectedCellsIntersects(i))
                    {
                        DataGridRowClipboardEventArgs preparingRowClipboardContentEventArgs = (DataGridRowClipboardEventArgs)CONSTRUCTORINFO_DATAGRIDROWCLIPBOARDEVENTARGS.Invoke(
                            new object[] { row, minColumnDisplayIndex, maxColumnDisplayIndex, false /*IsColumnHeadersRow*/, i });
                        OnCopyingRowClipboardContent(preparingRowClipboardContentEventArgs);

                        foreach (string format in formats)
                        {
                            dataGridStringBuilders[format].Append(preparingRowClipboardContentEventArgs.FormatClipboardCellValues(format));
                        }
                    }
                }
            }

            DataGridBoldClipboardHelper.GetClipboardContentForHtml(dataGridStringBuilders[DataFormats.Html]);

            DataObject dataObject;
            bool       hasPerms = SecurityHelper.CallerHasAllClipboardPermission() && SecurityHelper.CallerHasSerializationPermission();

            // Copy unconditionally in full trust.
            // Only copy in partial trust if user initiated.
            if (hasPerms || args.GetUserInitiated())
            {
                (new UIPermission(UIPermissionClipboard.AllClipboard)).Assert();
                try
                {
                    dataObject = new DataObject();
                }
                finally
                {
                    UIPermission.RevertAssert();
                }

                foreach (string format in formats)
                {
                    if (dataGridStringBuilders[format] != null)
                    {
                        dataObject.SetData(format, dataGridStringBuilders[format].ToString(), false /*autoConvert*/);
                    }
                }

                // This assert is there for an OLE Callback to register CSV type for the clipboard
                (new SecurityPermission(SecurityPermissionFlag.SerializationFormatter | SecurityPermissionFlag.UnmanagedCode)).Assert();
                try
                {
                    if (dataObject != null)
                    {
                        Clipboard.SetDataObject(dataObject, true /* Copy */);
                    }
                }
                finally
                {
                    SecurityPermission.RevertAll();
                }
            }
        }