Exemplo n.º 1
0
        //-------------------------------------------------------------------------------
        //
        // Private Methods
        //
        //-------------------------------------------------------------------------------

        #region Private Methods

        /// <summary>
        /// Copy the current Selection in XAML format.
        /// Called by :
        ///             CopySelectedData
        /// </summary>
        /// <param name="dataObject"></param>
        /// <param name="strokes"></param>
        /// <param name="elements"></param>
        /// <param name="transform"></param>
        /// <param name="size"></param>
        /// <returns>True if the copy is succeeded</returns>
        private bool CopySelectionInXAML(IDataObject dataObject, StrokeCollection strokes, List <UIElement> elements, Matrix transform, Size size)
        {
            InkCanvas inkCanvas = new InkCanvas();

            // We already transform the Strokes in CopySelectedData.
            if (strokes.Count != 0)
            {
                inkCanvas.Strokes = strokes;
            }

            int elementCount = elements.Count;

            if (elementCount != 0)
            {
                InkCanvasSelection inkCanvasSelection = InkCanvas.InkCanvasSelection;

                for (int i = 0; i < elementCount; i++)
                {
                    // NOTICE-2005/05/05-WAYNEZEN,
                    // An element can't be added to two visual trees.
                    // So, we cannot add the elements to the new container since they have been added to the current InkCanvas.
                    // Here we have to do is according to the suggestion from Avalon team -
                    //      1. Presist the elements to Xaml
                    //      2. Load the xaml to create the new instances of the elements.
                    //      3. Add the new instances to the new container.
                    string xml = XamlWriter.Save(elements[i]);

                    UIElement newElement = XamlReader.Load(new XmlTextReader(new StringReader(xml))) as UIElement;
                    ((IAddChild)inkCanvas).AddChild(newElement);

                    // Now we tranform the element.
                    inkCanvasSelection.UpdateElementBounds(elements[i], newElement, transform);
                }
            }

            if (inkCanvas != null)
            {
                inkCanvas.Width  = size.Width;
                inkCanvas.Height = size.Height;

                ClipboardData data = new XamlClipboardData(new UIElement[] { inkCanvas });

                try
                {
                    data.CopyToDataObject(dataObject);
                }
                catch (SecurityException)
                {
                    // If we hit a SecurityException under the PartialTrust, we should just fail the copy
                    // operation.
                    inkCanvas = null;
                }
            }

            return(inkCanvas != null);
        }
        private bool CopySelectionInXAML(IDataObject dataObject, StrokeCollection strokes, List <UIElement> elements, Matrix transform, Size size)
        {
            if (!SecurityHelper.CheckUnmanagedCodePermission())
            {
                return(false);
            }
            InkCanvas inkCanvas = new InkCanvas();

            if (strokes.Count != 0)
            {
                inkCanvas.Strokes = strokes;
            }
            int count = elements.Count;

            if (count != 0)
            {
                InkCanvasSelection inkCanvasSelection = this.InkCanvas.InkCanvasSelection;
                for (int i = 0; i < count; i++)
                {
                    try
                    {
                        string    s         = XamlWriter.Save(elements[i]);
                        UIElement uielement = XamlReader.Load(new XmlTextReader(new StringReader(s))) as UIElement;
                        ((IAddChild)inkCanvas).AddChild(uielement);
                        inkCanvasSelection.UpdateElementBounds(elements[i], uielement, transform);
                    }
                    catch (SecurityException)
                    {
                        inkCanvas = null;
                        break;
                    }
                }
            }
            if (inkCanvas != null)
            {
                inkCanvas.Width  = size.Width;
                inkCanvas.Height = size.Height;
                ClipboardData clipboardData = new XamlClipboardData(new UIElement[]
                {
                    inkCanvas
                });
                try
                {
                    clipboardData.CopyToDataObject(dataObject);
                }
                catch (SecurityException)
                {
                    inkCanvas = null;
                }
            }
            return(inkCanvas != null);
        }
        // Token: 0x06006D0E RID: 27918 RVA: 0x001F5420 File Offset: 0x001F3620
        internal bool PasteData(IDataObject dataObject, ref StrokeCollection newStrokes, ref List <UIElement> newElements)
        {
            foreach (KeyValuePair <InkCanvasClipboardFormat, ClipboardData> keyValuePair in this._preferredClipboardData)
            {
                InkCanvasClipboardFormat key   = keyValuePair.Key;
                ClipboardData            value = keyValuePair.Value;
                if (value.CanPaste(dataObject))
                {
                    switch (key)
                    {
                    case InkCanvasClipboardFormat.InkSerializedFormat:
                    {
                        ISFClipboardData isfclipboardData = (ISFClipboardData)value;
                        isfclipboardData.PasteFromDataObject(dataObject);
                        newStrokes = isfclipboardData.Strokes;
                        break;
                    }

                    case InkCanvasClipboardFormat.Text:
                    {
                        TextClipboardData textClipboardData = (TextClipboardData)value;
                        textClipboardData.PasteFromDataObject(dataObject);
                        newElements = textClipboardData.Elements;
                        break;
                    }

                    case InkCanvasClipboardFormat.Xaml:
                    {
                        XamlClipboardData xamlClipboardData = (XamlClipboardData)value;
                        xamlClipboardData.PasteFromDataObject(dataObject);
                        List <UIElement> elements = xamlClipboardData.Elements;
                        if (elements != null && elements.Count != 0)
                        {
                            if (elements.Count == 1 && ClipboardProcessor.InkCanvasDType.IsInstanceOfType(elements[0]))
                            {
                                this.TearDownInkCanvasContainer((InkCanvas)elements[0], ref newStrokes, ref newElements);
                            }
                            else
                            {
                                newElements = elements;
                            }
                        }
                        break;
                    }
                    }
                    return(true);
                }
            }
            return(false);
        }
Exemplo n.º 4
0
        /// <summary>
        /// The method returns the Strokes or UIElement array if there is the supported data in the IDataObject
        /// </summary>
        /// <param name="dataObject">The IDataObject instance</param>
        /// <param name="newStrokes">The strokes which are converted from the data in the IDataObject</param>
        /// <param name="newElements">The elements array which are converted from the data in the IDataObject</param>
        internal bool PasteData(IDataObject dataObject, ref StrokeCollection newStrokes, ref List <UIElement> newElements)
        {
            Debug.Assert(dataObject != null && _preferredClipboardData != null);

            // We honor the order in our preferred list.
            foreach (KeyValuePair <InkCanvasClipboardFormat, ClipboardData> pair in _preferredClipboardData)
            {
                InkCanvasClipboardFormat format = pair.Key;
                ClipboardData            data   = pair.Value;

                if (data.CanPaste(dataObject))
                {
                    switch (format)
                    {
                    case InkCanvasClipboardFormat.Xaml:
                    {
                        XamlClipboardData xamlData = (XamlClipboardData)data;
                        xamlData.PasteFromDataObject(dataObject);

                        List <UIElement> elements = xamlData.Elements;

                        if (elements != null && elements.Count != 0)
                        {
                            // If the Xaml data has been set in an InkCanvas, the top element will be a container InkCanvas.
                            // In this case, the new elements will be the children of the container.
                            // Otherwise, the new elements will be whatever data from the data object.
                            if (elements.Count == 1 && ClipboardProcessor.InkCanvasDType.IsInstanceOfType(elements[0]))
                            {
                                TearDownInkCanvasContainer((InkCanvas)(elements[0]), ref newStrokes, ref newElements);
                            }
                            else
                            {
                                // The new elements are the data in the data object.
                                newElements = elements;
                            }
                        }
                        break;
                    }

                    case InkCanvasClipboardFormat.InkSerializedFormat:
                    {
                        // Retrieve the stroke data.
                        ISFClipboardData isfData = (ISFClipboardData)data;
                        isfData.PasteFromDataObject(dataObject);

                        newStrokes = isfData.Strokes;
                        break;
                    }

                    case InkCanvasClipboardFormat.Text:
                    {
                        // Convert the text data in the data object to a text box element.
                        TextClipboardData textData = (TextClipboardData)data;
                        textData.PasteFromDataObject(dataObject);
                        newElements = textData.Elements;
                        break;
                    }
                    }

                    // Once we've done pasting, just return now.
                    return(true);
                }
            }

            // Nothing gets pasted.
            return(false);
        }
Exemplo n.º 5
0
        private bool CopySelectionInXAML(IDataObject dataObject, StrokeCollection strokes, List<UIElement> elements, Matrix transform, Size size)
        {
            //NOTE: after meeting with the partial trust team, we have 
            //collectively decided to only allow copy / cut of XAML if the caller
            //has unmanagedcode permission, else we silently ignore the XAML
            if (!SecurityHelper.CheckUnmanagedCodePermission())
            {
                return false;
            }
            else
            {

                InkCanvas inkCanvas = new InkCanvas();

                // NOTICE-2005/12/06-WAYNEZEN,
                // We already transform the Strokes in CopySelectedData.
                if (strokes.Count != 0)
                {
                    inkCanvas.Strokes = strokes;
                }

                int elementCount = elements.Count;
                if (elementCount != 0)
                {
                    InkCanvasSelection inkCanvasSelection = InkCanvas.InkCanvasSelection;

                    for (int i = 0; i < elementCount; i++)
                    {
                        // NOTICE-2005/05/05-WAYNEZEN,
                        // An element can't be added to two visual trees.
                        // So, we cannot add the elements to the new container since they have been added to the current InkCanvas.
                        // Here we have to do is according to the suggestion from Avalon team -
                        //      1. Presist the elements to Xaml 
                        //      2. Load the xaml to create the new instances of the elements.
                        //      3. Add the new instances to the new container.
                        string xml;

                        try
                        {
                            xml = XamlWriter.Save(elements[i]);

                            UIElement newElement = XamlReader.Load(new XmlTextReader(new StringReader(xml))) as UIElement;
                            ((IAddChild)inkCanvas).AddChild(newElement);

                            // Now we tranform the element.
                            inkCanvasSelection.UpdateElementBounds(elements[i], newElement, transform);
                        }
                        catch (SecurityException)
                        {
                            // If we hit a SecurityException under the PartialTrust, we should just stop generating
                            // the containing InkCanvas.
                            inkCanvas = null;
                            break;
                        }
                    }
                }

                if (inkCanvas != null)
                {
                    inkCanvas.Width = size.Width;
                    inkCanvas.Height = size.Height;

                    ClipboardData data = new XamlClipboardData(new UIElement[] { inkCanvas });

                    try
                    {
                        data.CopyToDataObject(dataObject);
                    }
                    catch (SecurityException)
                    {
                        // If we hit a SecurityException under the PartialTrust, we should just fail the copy
                        // operation.
                        inkCanvas = null;
                    }
                }

                return inkCanvas != null;
            }
        }