/// <summary> /// Handle the Cut operation to the clipboard /// </summary> protected internal override int CutToClipboard() { int returnValue = (int)OleConstants.OLECMDERR_E_NOTSUPPORTED; try { this.RegisterClipboardNotifications(true); // Create our data object and change the selection to show item(s) being cut IOleDataObject dataObject = this.PackageSelectionDataObject(true); if (dataObject != null) { this.SourceDraggedOrCutOrCopied = true; // Add our cut item(s) to the clipboard ErrorHandler.ThrowOnFailure(UnsafeNativeMethods.OleSetClipboard(dataObject)); // Inform VS (UiHierarchyWindow) of the cut IVsUIHierWinClipboardHelper clipboardHelper = (IVsUIHierWinClipboardHelper)GetService(typeof(SVsUIHierWinClipboardHelper)); if (clipboardHelper == null) { return(VSConstants.E_FAIL); } returnValue = ErrorHandler.ThrowOnFailure(clipboardHelper.Cut(dataObject)); } } catch (COMException e) { Trace.WriteLine("Exception : " + e.Message); returnValue = e.ErrorCode; } return(returnValue); }
/// <summary> /// Handle the Paste operation to a targetNode /// </summary> protected internal override int PasteFromClipboard(HierarchyNode targetNode) { int returnValue = (int)OleConstants.OLECMDERR_E_NOTSUPPORTED; //Get the clipboardhelper service and use it after processing dataobject IVsUIHierWinClipboardHelper clipboardHelper = (IVsUIHierWinClipboardHelper)GetService(typeof(SVsUIHierWinClipboardHelper)); if (clipboardHelper == null) { return(VSConstants.E_FAIL); } try { //Get dataobject from clipboard IOleDataObject dataObject = null; ErrorHandler.ThrowOnFailure(UnsafeNativeMethods.OleGetClipboard(out dataObject)); if (dataObject == null) { return(VSConstants.E_UNEXPECTED); } DropEffect dropEffect = DropEffect.None; DropDataType dropDataType = DropDataType.None; try { dropDataType = this.ProcessSelectionDataObject(dataObject, targetNode.GetDragTargetHandlerNode()); dropEffect = this.QueryDropEffect(dropDataType, 0); } catch (ExternalException e) { Trace.WriteLine("Exception : " + e.Message); // If it is a drop from windows and we get any kind of error ignore it. This // prevents bogus messages from the shell from being displayed if (dropDataType != DropDataType.Shell) { throw e; } } finally { // Inform VS (UiHierarchyWindow) of the paste returnValue = clipboardHelper.Paste(dataObject, (uint)dropEffect); } } catch (COMException e) { Trace.WriteLine("Exception : " + e.Message); returnValue = e.ErrorCode; } return(returnValue); }
/// <summary> /// Register/Unregister for Clipboard events for the UiHierarchyWindow (solution explorer) /// </summary> /// <param name="register">true for register, false for unregister</param> protected internal override void RegisterClipboardNotifications(bool register) { // Get the UiHierarchy window clipboard helper service IVsUIHierWinClipboardHelper clipboardHelper = (IVsUIHierWinClipboardHelper)GetService(typeof(SVsUIHierWinClipboardHelper)); if (clipboardHelper == null) { return; } if (register && this.copyPasteCookie == 0) { // Register ErrorHandler.ThrowOnFailure(clipboardHelper.AdviseClipboardHelperEvents(this, out this.copyPasteCookie)); Debug.Assert(this.copyPasteCookie != 0, "AdviseClipboardHelperEvents returned an invalid cookie"); } else if (!register && this.copyPasteCookie != 0) { // Unregister ErrorHandler.ThrowOnFailure(clipboardHelper.UnadviseClipboardHelperEvents(this.copyPasteCookie)); this.copyPasteCookie = 0; } }