Exemplo n.º 1
0
        /// <summary>
        /// To avoid changing the clipboard during a copy/cut and paste action.
        /// One solution for this is to save clipboard into a temp slide and revert clipboard afterwards.
        /// </summary>
        public static TResult RestoreClipboardAfterAction <TResult>(System.Func <TResult> action, PowerPointPresentation pres, PowerPointSlide origSlide)
        {
            TResult result;

            if (!IsClipboardEmpty())
            {
                // Save clipboard onto a temp slide
                PowerPointSlide tempClipboardSlide  = null;
                ShapeRange      tempClipboardShapes = null;
                SlideRange      tempPastedSlide     = null;

                Logger.Log("RestoreClipboardAfterAction: Trying to paste as slide.", ActionFramework.Common.Logger.LogType.Info);
                tempPastedSlide = TryPastingAsSlide(pres, origSlide);

                if (tempPastedSlide == null)
                {
                    tempClipboardSlide = pres.AddSlide();
                    Logger.Log("RestoreClipboardAfterAction: Trying to paste as text.", ActionFramework.Common.Logger.LogType.Info);
                    tempClipboardShapes = TryPastingAsText(tempClipboardSlide);
                }

                if (CheckIfPastingFailed(tempPastedSlide, tempClipboardShapes))
                {
                    Logger.Log("RestoreClipboardAfterAction: Trying to paste as shape.", ActionFramework.Common.Logger.LogType.Info);
                    tempClipboardShapes = TryPastingAsShape(tempClipboardSlide);
                }

                if (CheckIfPastingFailed(tempPastedSlide, tempClipboardShapes))
                {
                    Logger.Log("RestoreClipboardAfterAction: Trying to paste as PNG picture", ActionFramework.Common.Logger.LogType.Info);
                    tempClipboardShapes = TryPastingAsPNG(tempClipboardSlide);
                }

                if (CheckIfPastingFailed(tempPastedSlide, tempClipboardShapes))
                {
                    Logger.Log("RestoreClipboardAfterAction: Trying to paste as bitmap picture", ActionFramework.Common.Logger.LogType.Info);
                    tempClipboardShapes = TryPastingAsBitmap(tempClipboardSlide);
                }

                if (CheckIfPastingFailed(tempPastedSlide, tempClipboardShapes))
                {
                    Logger.Log("RestoreClipboardAfterAction: Trying to paste onto view", ActionFramework.Common.Logger.LogType.Info);
                    tempClipboardShapes = TryPastingOntoView(pres, tempClipboardSlide, origSlide);
                }

                result = action();

                RestoreClipboard(tempClipboardShapes, tempPastedSlide);
                if (tempClipboardSlide != null)
                {
                    tempClipboardSlide.Delete();
                }
            }
            else
            {
                // Clipboard is empty, we can just run the action function
                result = action();
            }
            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// To avoid changing the clipboard during a copy/cut and paste action.
        /// One solution for this is to save clipboard into a temp slide and revert clipboard afterwards.
        /// </summary>
        public static void RestoreClipboardAfterAction(System.Action action, PowerPointPresentation pres, PowerPointSlide origSlide)
        {
            if (!IsClipboardEmpty())
            {
                // Save clipboard onto a temp slide
                PowerPointSlide tempClipboardSlide  = null;
                ShapeRange      tempClipboardShapes = null;
                SlideRange      tempPastedSlide     = null;
                Shape           tempClipboardShape  = null;

                Logger.Log("RestoreClipboardAfterAction: Trying to paste as slide.", ActionFramework.Common.Logger.LogType.Info);
                tempPastedSlide = TryPastingAsSlide(pres, origSlide);

                if (tempPastedSlide == null)
                {
                    tempClipboardSlide = pres.AddSlide();
                    Logger.Log("RestoreClipboardAfterAction: Trying to paste as text.", ActionFramework.Common.Logger.LogType.Info);
                    tempClipboardShapes = TryPastingAsText(tempClipboardSlide);
                }

                if (tempPastedSlide == null && (tempClipboardShapes == null || tempClipboardShapes.Count < 1))
                {
                    Logger.Log("RestoreClipboardAfterAction: Trying to paste as shape.", ActionFramework.Common.Logger.LogType.Info);
                    tempClipboardShapes = TryPastingAsShape(tempClipboardSlide);
                }

                if (tempPastedSlide == null && (tempClipboardShapes == null || tempClipboardShapes.Count < 1))
                {
                    Logger.Log("RestoreClipboardAfterAction: Trying to paste onto current view of the document window.", ActionFramework.Common.Logger.LogType.Info);
                    tempClipboardShape = TryPastingOntoView(pres, tempClipboardSlide, origSlide);
                }

                action();

                RestoreClipboard(tempClipboardShape, tempClipboardShapes, tempPastedSlide);
                if (tempClipboardSlide != null)
                {
                    tempClipboardSlide.Delete();
                }
            }
            else
            {
                // Clipboard is empty, we can just run the action function
                action();
            }
        }
Exemplo n.º 3
0
        private static ClipboardUtilData SaveClipboardUnsafe(PowerPointPresentation pres, PowerPointSlide origSlide)
        {
            PowerPointSlide tempClipboardSlide  = null;
            ShapeRange      tempClipboardShapes = null;
            SlideRange      tempPastedSlide     = null;

            tempPastedSlide = TryPastingAsSlide(pres, origSlide);

            if (tempPastedSlide == null)
            {
                tempClipboardSlide = pres.AddSlide();
                Logger.Log("RestoreClipboardAfterAction: Trying to paste as text.", ActionFramework.Common.Logger.LogType.Info);
                tempClipboardShapes = TryPastingAsText(tempClipboardSlide);
            }

            if (CheckIfPastingFailed(tempPastedSlide, tempClipboardShapes))
            {
                Logger.Log("RestoreClipboardAfterAction: Trying to paste as shape.", ActionFramework.Common.Logger.LogType.Info);
                tempClipboardShapes = TryPastingAsShape(tempClipboardSlide);
            }

            if (CheckIfPastingFailed(tempPastedSlide, tempClipboardShapes))
            {
                Logger.Log("RestoreClipboardAfterAction: Trying to paste as PNG picture", ActionFramework.Common.Logger.LogType.Info);
                tempClipboardShapes = TryPastingAsPNG(tempClipboardSlide);
            }

            if (CheckIfPastingFailed(tempPastedSlide, tempClipboardShapes))
            {
                Logger.Log("RestoreClipboardAfterAction: Trying to paste as bitmap picture", ActionFramework.Common.Logger.LogType.Info);
                tempClipboardShapes = TryPastingAsBitmap(tempClipboardSlide);
            }

            if (CheckIfPastingFailed(tempPastedSlide, tempClipboardShapes))
            {
                Logger.Log("RestoreClipboardAfterAction: Trying to paste onto view", ActionFramework.Common.Logger.LogType.Info);
                tempClipboardShapes = TryPastingOntoView(pres, tempClipboardSlide, origSlide);
            }
            return(new ClipboardUtilData()
            {
                tempClipboardSlide = tempClipboardSlide,
                tempClipboardShapes = tempClipboardShapes,
                tempPastedSlide = tempPastedSlide
            });
        }
        protected override ShapeRange ExecutePasteAction(string ribbonId, PowerPointPresentation presentation, PowerPointSlide slide,
                                                         ShapeRange selectedShapes, ShapeRange selectedChildShapes)
        {
            PowerPointSlide tempSlide         = presentation.AddSlide(index: slide.Index);
            ShapeRange      tempPastingShapes = PasteShapesFromClipboard(tempSlide);

            if (tempPastingShapes == null)
            {
                tempSlide.Delete();
                return(PasteShapesFromClipboard(slide));
            }

            ShapeRange pastingShapes = slide.CopyShapesToSlide(tempPastingShapes);

            tempSlide.Delete();

            return(pastingShapes);
        }
        protected override ShapeRange ExecutePasteAction(string ribbonId, PowerPointPresentation presentation, PowerPointSlide slide,
                                                         ShapeRange selectedShapes, ShapeRange selectedChildShapes)
        {
            PowerPointSlide tempSlide         = presentation.AddSlide(index: slide.Index);
            ShapeRange      tempPastingShapes = ClipboardUtil.PasteShapesFromClipboard(presentation, tempSlide);

            if (tempPastingShapes == null)
            {
                tempSlide.Delete();
                ShapeRange shapes = ClipboardUtil.PasteShapesFromClipboard(presentation, slide);
                if (shapes == null)
                {
                    Logger.Log("PasteLab: Could not paste clipboard contents.");
                    MessageBox.Show(PasteLabText.ErrorPaste, PasteLabText.ErrorDialogTitle);
                }
                return(shapes);
            }

            ShapeRange pastingShapes = slide.CopyShapesToSlide(tempPastingShapes);

            tempSlide.Delete();

            return(pastingShapes);
        }
        // Sealed method: Subclasses should override ExecutePasteAction instead
        protected sealed override void ExecuteAction(string ribbonId)
        {
            this.StartNewUndoEntry();

            PowerPointPresentation presentation = this.GetCurrentPresentation();
            PowerPointSlide        slide        = this.GetCurrentSlide();
            Selection selection = this.GetCurrentSelection();

            if (ClipboardUtil.IsClipboardEmpty())
            {
                Logger.Log(ribbonId + " failed. Clipboard is empty.");
                return;
            }

            ShapeRange passedSelectedShapes      = null;
            ShapeRange passedSelectedChildShapes = null;

            if (ShapeUtil.IsSelectionShape(selection) && !IsSelectionIgnored(ribbonId))
            {
                // When pasting some objects, the selection may change to the pasted object (e.g. jpg from desktop).
                // Therefore we must capture the selection first.
                ShapeRange selectedShapes = selection.ShapeRange;

                // Save clipboard onto a temp slide, because CorruptionCorrrection uses Copy-Paste
                PowerPointSlide tempClipboardSlide  = presentation.AddSlide(index: slide.Index);
                ShapeRange      tempClipboardShapes = ClipboardUtil.PasteShapesFromClipboard(tempClipboardSlide);

                // Nothing is pasted, stop now
                if (tempClipboardShapes == null)
                {
                    tempClipboardSlide.Delete();
                    return;
                }

                // Preserve selection by tagging them
                for (int i = 1; i <= selectedShapes.Count; i++)
                {
                    selectedShapes[i].Tags.Add(SelectOrderTagName, i.ToString());
                }

                ShapeRange selectedChildShapes = null;
                if (selection.HasChildShapeRange)
                {
                    selectedChildShapes = selection.ChildShapeRange;
                    for (int i = 1; i <= selectedChildShapes.Count; i++)
                    {
                        selectedChildShapes[i].Tags.Add(SelectChildOrderTagName, i.ToString());
                    }
                }

                // Corruption correction
                ShapeRange correctedShapes = ShapeUtil.CorruptionCorrection(selectedShapes, slide);

                // Reselect the preserved selections
                List <Shape> correctedShapeList      = new List <Shape>();
                List <Shape> correctedChildShapeList = new List <Shape>();
                foreach (Shape shape in correctedShapes)
                {
                    correctedShapeList.Add(shape);
                    correctedChildShapeList.AddRange(ShapeUtil.GetChildrenWithNonEmptyTag(shape, SelectChildOrderTagName));
                }
                correctedShapeList.Sort((sh1, sh2) => int.Parse(sh1.Tags[SelectOrderTagName]) - int.Parse(sh2.Tags[SelectOrderTagName]));
                correctedChildShapeList.Sort((sh1, sh2) => int.Parse(sh1.Tags[SelectChildOrderTagName]) - int.Parse(sh2.Tags[SelectChildOrderTagName]));
                passedSelectedShapes      = slide.ToShapeRange(correctedShapeList);
                passedSelectedChildShapes = slide.ToShapeRange(correctedChildShapeList);

                // Remove shape tags after they have been used
                ShapeUtil.DeleteTagFromShapes(passedSelectedShapes, SelectOrderTagName);
                ShapeUtil.DeleteTagFromShapes(passedSelectedChildShapes, SelectChildOrderTagName);

                // Revert clipboard
                tempClipboardShapes.Copy();
                tempClipboardSlide.Delete();
            }

            ShapeRange result = ExecutePasteAction(ribbonId, presentation, slide, passedSelectedShapes, passedSelectedChildShapes);

            if (result != null)
            {
                result.Select();
            }
        }