internal InkCanvasClipboardDataFormats CopySelectedData(IDataObject dataObject)
        {
            InkCanvasClipboardDataFormats inkCanvasClipboardDataFormats = InkCanvasClipboardDataFormats.None;
            InkCanvasSelection            inkCanvasSelection            = this.InkCanvas.InkCanvasSelection;
            StrokeCollection strokeCollection = inkCanvasSelection.SelectedStrokes;

            if (strokeCollection.Count > 1)
            {
                StrokeCollection strokeCollection2 = new StrokeCollection();
                StrokeCollection strokes           = this.InkCanvas.Strokes;
                int num = 0;
                while (num < strokes.Count && strokeCollection.Count != strokeCollection2.Count)
                {
                    for (int i = 0; i < strokeCollection.Count; i++)
                    {
                        if (strokes[num] == strokeCollection[i])
                        {
                            strokeCollection2.Add(strokeCollection[i]);
                            break;
                        }
                    }
                    num++;
                }
                strokeCollection = strokeCollection2.Clone();
            }
            else
            {
                strokeCollection = strokeCollection.Clone();
            }
            List <UIElement> list            = new List <UIElement>(inkCanvasSelection.SelectedElements);
            Rect             selectionBounds = inkCanvasSelection.SelectionBounds;

            if (strokeCollection.Count != 0 || list.Count != 0)
            {
                Matrix identity = Matrix.Identity;
                identity.OffsetX = -selectionBounds.Left;
                identity.OffsetY = -selectionBounds.Top;
                if (strokeCollection.Count != 0)
                {
                    inkCanvasSelection.TransformStrokes(strokeCollection, identity);
                    ClipboardData clipboardData = new ISFClipboardData(strokeCollection);
                    clipboardData.CopyToDataObject(dataObject);
                    inkCanvasClipboardDataFormats |= InkCanvasClipboardDataFormats.ISF;
                }
                if (this.CopySelectionInXAML(dataObject, strokeCollection, list, identity, selectionBounds.Size))
                {
                    inkCanvasClipboardDataFormats |= InkCanvasClipboardDataFormats.XAML;
                }
            }
            return(inkCanvasClipboardDataFormats);
        }
Exemple #2
0
        /// <summary>
        /// The method copies the current selection to the IDataObject if there is any
        /// Called by :
        ///             InkCanvas.CopyToDataObject
        /// </summary>
        /// <param name="dataObject">The IDataObject instance</param>
        /// <returns>true if there is data being copied. Otherwise return false</returns>
        internal InkCanvasClipboardDataFormats CopySelectedData(IDataObject dataObject)
        {
            InkCanvasClipboardDataFormats copiedDataFormat   = InkCanvasClipboardDataFormats.None;
            InkCanvasSelection            inkCanvasSelection = InkCanvas.InkCanvasSelection;

            StrokeCollection strokes = inkCanvasSelection.SelectedStrokes;

            if (strokes.Count > 1)
            {
                //
                // order the strokes so they are in the correct z-order
                // they appear in on the InkCanvas, or else they will be inconsistent
                // if copied / pasted
                StrokeCollection orderedStrokes   = new StrokeCollection();
                StrokeCollection inkCanvasStrokes = InkCanvas.Strokes; //cache to avoid multiple property gets
                for (int i = 0; i < inkCanvasStrokes.Count && strokes.Count != orderedStrokes.Count; i++)
                {
                    for (int j = 0; j < strokes.Count; j++)
                    {
                        if (inkCanvasStrokes[i] == strokes[j])
                        {
                            orderedStrokes.Add(strokes[j]);
                            break;
                        }
                    }
                }

                Debug.Assert(inkCanvasSelection.SelectedStrokes.Count == orderedStrokes.Count);
                //Make a copy collection since we will alter the transform before copying the data.
                strokes = orderedStrokes.Clone();
            }
            else
            {
                //we only have zero or one stroke so we don't need to order, but we
                //do need to clone.
                strokes = strokes.Clone();
            }

            List <UIElement> elements = new List <UIElement>(inkCanvasSelection.SelectedElements);
            Rect             bounds   = inkCanvasSelection.SelectionBounds;

            // Now copy the selection in the below orders.
            if (strokes.Count != 0 || elements.Count != 0)
            {
                //
                // The selection should be translated to the origin (0, 0) related to its bounds.
                // Get the translate transform as a relative bounds.
                Matrix transform = Matrix.Identity;
                transform.OffsetX = -bounds.Left;
                transform.OffsetY = -bounds.Top;

                // Add ISF data first.
                if (strokes.Count != 0)
                {
                    // Transform the strokes first.
                    inkCanvasSelection.TransformStrokes(strokes, transform);

                    ClipboardData data = new ISFClipboardData(strokes);
                    data.CopyToDataObject(dataObject);
                    copiedDataFormat |= InkCanvasClipboardDataFormats.ISF;
                }

                // Then add XAML data.
                if (CopySelectionInXAML(dataObject, strokes, elements, transform, bounds.Size))
                {
                    // We have to create an InkCanvas as a container and add all the selection to it.
                    copiedDataFormat |= InkCanvasClipboardDataFormats.XAML;
                }
            }
            else
            {
                Debug.Assert(false, "CopySelectData: InkCanvas should have a selection!");
            }

            return(copiedDataFormat);
        }