Ejemplo n.º 1
0
 public static SafeDataObject FromClipboard()
 {
     try
     {
         return(new SafeDataObject(ClipboardService.GetDataObject()));
     }
     catch (ExternalException ex)
     {
     }
     return((SafeDataObject)null);
 }
Ejemplo n.º 2
0
        private void PasteResource()
        {
            int index = -1;
            ResourceContainer container = (ResourceContainer)null;

            if (this.resourceManager.SelectedResourceContainers.Count == 1)
            {
                container = this.resourceManager.SelectedResourceContainers[0];
                index     = container.ResourceItems.Count;
            }
            else if (this.resourceManager.SelectedResourceItems.Count == 1)
            {
                ResourceItem resourceItem = this.resourceManager.SelectedResourceItems[0];
                container = resourceItem.Container;
                index     = container.ResourceItems.IndexOf(resourceItem) + 1;
            }
            SafeDataObject dataObject = SafeDataObject.FromClipboard();

            if (index < 0)
            {
                return;
            }
            SceneViewModel viewModel    = container.ViewModel;
            PastePackage   pastePackage = PastePackage.FromData(viewModel, dataObject);

            if (pastePackage != null)
            {
                if (pastePackage.Elements.Count > 0)
                {
                    viewModel.DesignerContext.MessageDisplayService.ShowError(StringTable.PasteResourcesFailedElementsFoundDialogMessage);
                }
                else
                {
                    IDictionary <DocumentNode, string> imageMap = (IDictionary <DocumentNode, string>) new Dictionary <DocumentNode, string>();
                    foreach (SceneNode sceneNode in pastePackage.Resources)
                    {
                        foreach (KeyValuePair <DocumentNode, string> keyValuePair in (IEnumerable <KeyValuePair <DocumentNode, string> >)Microsoft.Expression.DesignSurface.Utility.ResourceHelper.CreateImageReferenceMap(sceneNode.DocumentNode, pastePackage, viewModel))
                        {
                            imageMap.Add(keyValuePair);
                        }
                    }
                    using (SceneEditTransaction editTransaction = viewModel.CreateEditTransaction(StringTable.UndoUnitPaste))
                    {
                        Microsoft.Expression.DesignSurface.Utility.ResourceHelper.PasteResources(pastePackage, imageMap, ResourceConflictResolution.RenameNew | ResourceConflictResolution.OverwriteOld, container.Node, index, true);
                        editTransaction.Commit();
                    }
                }
            }
            else if (dataObject.GetDataPresent(DataFormats.FileDrop))
            {
                DesignerContext designerContext        = viewModel.DesignerContext;
                IDocumentType[] supportedDocumentTypes = new IDocumentType[1]
                {
                    designerContext.DocumentTypeManager.DocumentTypes[DocumentTypeNamesHelper.Image]
                };
                string[] supportedFiles = new FileDropUtility(designerContext.ProjectManager, (FrameworkElement)null, supportedDocumentTypes).GetSupportedFiles(ClipboardService.GetDataObject());
                if (supportedFiles.Length > 0)
                {
                    foreach (IProjectItem projectItem in designerContext.ActiveProject.AddItems(Enumerable.Select <string, DocumentCreationInfo>((IEnumerable <string>)supportedFiles, (Func <string, DocumentCreationInfo>)(file => new DocumentCreationInfo()
                    {
                        SourcePath = file
                    }))))
                    {
                        this.CreateImageBrushResource(container, projectItem);
                    }
                }
                else
                {
                    designerContext.MessageDisplayService.ShowError(StringTable.PasteElementsFailedDialogMessage);
                }
            }
            else
            {
                if (!dataObject.GetDataPresent(DataFormats.Bitmap))
                {
                    return;
                }
                IProject project = EnumerableExtensions.SingleOrNull <IProject>(this.projectManager.ItemSelectionSet.SelectedProjects);
                if (project == null)
                {
                    return;
                }
                IProjectItem projectItem = CutBuffer.AddImageDataFromClipboard(this.projectManager, project);
                this.CreateImageBrushResource(container, projectItem);
            }
        }
Ejemplo n.º 3
0
        private static ICollection <SceneNode> PasteData(SceneViewModel viewModel, SafeDataObject dataObject, ISceneInsertionPoint insertionPoint, bool allowInsertionPointChange, out bool canceledPasteOperation)
        {
            PerformanceUtility.MeasurePerformanceUntilRender(PerformanceEvent.PasteElements);
            canceledPasteOperation = false;
            List <SceneNode> list         = new List <SceneNode>();
            PastePackage     pastePackage = PastePackage.FromData(viewModel, dataObject);

            if (pastePackage != null)
            {
                if (allowInsertionPointChange)
                {
                    insertionPoint = PasteCommand.ComputeNewInsertionPoint(viewModel, insertionPoint, pastePackage);
                }
                if (!PasteCommand.CanAddMultipleElements(insertionPoint.SceneElement, pastePackage.Elements.Count))
                {
                    string name = insertionPoint.SceneElement.TargetType.Name;
                    viewModel.DesignerContext.MessageDisplayService.ShowError(string.Format((IFormatProvider)CultureInfo.CurrentCulture, StringTable.PasteMultipleInSingleContainerError, new object[1]
                    {
                        (object)name
                    }));
                }
                else if (pastePackage.Elements.Count == 0 && pastePackage.Storyboards.Count == 0 && pastePackage.Resources.Count > 0)
                {
                    viewModel.DesignerContext.MessageDisplayService.ShowError(StringTable.PasteElementsFailedNoElementsDialogMessage);
                }
                else
                {
                    IDictionary <DocumentNode, string> imageMap = (IDictionary <DocumentNode, string>) new Dictionary <DocumentNode, string>();
                    foreach (SceneNode sceneNode in pastePackage.Elements)
                    {
                        foreach (KeyValuePair <DocumentNode, string> keyValuePair in (IEnumerable <KeyValuePair <DocumentNode, string> >)Microsoft.Expression.DesignSurface.Utility.ResourceHelper.CreateImageReferenceMap(sceneNode.DocumentNode, pastePackage, viewModel))
                        {
                            imageMap.Add(keyValuePair);
                        }
                    }
                    foreach (SceneNode sceneNode in pastePackage.Resources)
                    {
                        foreach (KeyValuePair <DocumentNode, string> keyValuePair in (IEnumerable <KeyValuePair <DocumentNode, string> >)Microsoft.Expression.DesignSurface.Utility.ResourceHelper.CreateImageReferenceMap(sceneNode.DocumentNode, pastePackage, viewModel))
                        {
                            imageMap.Add(keyValuePair);
                        }
                    }
                    int index = 0;
                    ISupportsResources resourcesCollection = ResourceNodeHelper.GetResourcesCollection(viewModel.RootNode.DocumentNode);
                    if (resourcesCollection != null && resourcesCollection.Resources != null && resourcesCollection.Resources.SupportsChildren)
                    {
                        index = resourcesCollection.Resources.Children.Count;
                    }
                    if (Microsoft.Expression.DesignSurface.Utility.ResourceHelper.PasteResources(pastePackage, imageMap, ResourceConflictResolution.UseExisting | ResourceConflictResolution.RenameNew | ResourceConflictResolution.OverwriteOld, viewModel.RootNode, index, false))
                    {
                        ILayoutDesigner designerForParent = viewModel.GetLayoutDesignerForParent(insertionPoint.SceneElement, true);
                        List <PasteCommand.DelayedElementTranslationInfo> elementsToTranslateLater = new List <PasteCommand.DelayedElementTranslationInfo>(pastePackage.Elements.Count);
                        string copyElementToken = pastePackage.ClipboardCopyElementToken;
                        foreach (SceneElement element in pastePackage.Elements)
                        {
                            SceneElement sceneElement = PasteCommand.PasteElement(viewModel, element, elementsToTranslateLater, insertionPoint);
                            if (sceneElement != null)
                            {
                                Microsoft.Expression.DesignSurface.Utility.ResourceHelper.UpdateImageReferences(sceneElement.DocumentNode, imageMap, pastePackage, viewModel);
                                list.Add((SceneNode)sceneElement);
                                sceneElement.ClearValue(DesignTimeProperties.CopyTokenProperty);
                            }
                        }
                        if (copyElementToken != null)
                        {
                            pastePackage.SetGlobalCopyElementToken(copyElementToken);
                            PastePackage.PasteSelectionChangePending = true;
                        }
                        foreach (SceneNode childProperty in pastePackage.ChildPropertyNodes)
                        {
                            PasteCommand.PasteChildProperty(viewModel, childProperty, (IList <SceneNode>)list);
                        }
                        foreach (StoryboardTimelineSceneNode storyboard in pastePackage.Storyboards)
                        {
                            PasteCommand.PasteStoryboard(viewModel, storyboard, (IList <SceneNode>)list);
                        }
                        viewModel.Document.OnUpdatedEditTransaction();
                        viewModel.DefaultView.UpdateLayout();
                        using (viewModel.ForceBaseValue())
                        {
                            Rect empty = Rect.Empty;
                            foreach (PasteCommand.DelayedElementTranslationInfo elementTranslationInfo in elementsToTranslateLater)
                            {
                                empty.Union(elementTranslationInfo.Bounds);
                            }
                            foreach (PasteCommand.DelayedElementTranslationInfo elementTranslationInfo in elementsToTranslateLater)
                            {
                                elementTranslationInfo.UpdateTranslation(designerForParent, empty);
                            }
                        }
                    }
                    else
                    {
                        canceledPasteOperation = true;
                    }
                }
            }
            else if (dataObject.GetDataPresent(DataFormats.FileDrop))
            {
                DesignerContext designerContext = viewModel.DesignerContext;
                string[]        supportedFiles  = FileDropToolBehavior.CreateImageOrMediaDrop(designerContext).GetSupportedFiles(ClipboardService.GetDataObject());
                if (supportedFiles.Length > 0)
                {
                    IEnumerable <IProjectItem> importedItems = designerContext.ActiveProject.AddItems(Enumerable.Select <string, DocumentCreationInfo>((IEnumerable <string>)supportedFiles, (Func <string, DocumentCreationInfo>)(file => new DocumentCreationInfo()
                    {
                        SourcePath = file
                    })));
                    FileDropToolBehavior.AddItemsToDocument(viewModel.DefaultView, importedItems, new Point(0.0, 0.0), viewModel.ActiveSceneInsertionPoint);
                }
                else
                {
                    viewModel.DesignerContext.MessageDisplayService.ShowError(StringTable.PasteElementsFailedDialogMessage);
                }
            }
            else if (dataObject.GetDataPresent(DataFormats.Bitmap))
            {
                DesignerContext designerContext = viewModel.DesignerContext;
                IProjectItem    projectItem     = CutBuffer.AddImageDataFromClipboard(designerContext.ProjectManager, designerContext.ActiveProject);
                if (projectItem != null)
                {
                    FileDropToolBehavior.AddItemsToDocument(viewModel.DefaultView, (IEnumerable <IProjectItem>) new List <IProjectItem>()
                    {
                        projectItem
                    }, new Point(0.0, 0.0), viewModel.ActiveSceneInsertionPoint);
                }
                else
                {
                    viewModel.DesignerContext.MessageDisplayService.ShowError(StringTable.PasteElementsFailedDialogMessage);
                }
            }
            else
            {
                viewModel.DesignerContext.MessageDisplayService.ShowError(StringTable.PasteElementsFailedDialogMessage);
            }
            return((ICollection <SceneNode>)list);
        }