Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added drag/drop #455

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/AvaloniaEdit.Demo/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public MainWindow()
_textEditor.TextArea.TextEntered += textEditor_TextArea_TextEntered;
_textEditor.TextArea.TextEntering += textEditor_TextArea_TextEntering;
_textEditor.Options.AllowToggleOverstrikeMode = true;
_textEditor.Options.EnableTextDragDrop = true;
_textEditor.Options.ShowBoxForControlCharacters = true;
_textEditor.Options.ColumnRulerPositions = new List<int>() { 80, 100 };
_textEditor.TextArea.IndentationStrategy = new Indentation.CSharp.CSharpIndentationStrategy(_textEditor.Options);
Expand Down
4 changes: 3 additions & 1 deletion src/AvaloniaEdit.Demo/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Avalonia;
using System;
using Avalonia;

namespace AvaloniaEdit.Demo
{
Expand All @@ -9,6 +10,7 @@ public static AppBuilder BuildAvaloniaApp()
=> AppBuilder.Configure<App>().UsePlatformDetect();

// The entry point. Things aren't ready yet
[STAThread]
public static int Main(string[] args)
=> BuildAvaloniaApp().StartWithClassicDesktopLifetime(args);
}
Expand Down
14 changes: 14 additions & 0 deletions src/AvaloniaEdit/Document/DataObjectCopyingEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Avalonia.Input;
using Avalonia.Interactivity;
using AvaloniaEdit.Utils;

namespace AvaloniaEdit.Document;

public class DataObjectCopyingEventArgs(IDataObject dataObject, bool isDragDrop) :
RoutedEventArgs(DataObjectEx.DataObjectCopyingEvent)
{
public bool CommandCancelled { get; private set; }
public IDataObject DataObject { get; } = dataObject;
public bool IsDragDrop { get; } = isDragDrop;
public void CancelCommand() => CommandCancelled = true;
}
19 changes: 19 additions & 0 deletions src/AvaloniaEdit/Editing/EditingCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
using Avalonia.Input;
using AvaloniaEdit.Utils;
using Avalonia.Controls;
using static System.Net.Mime.MediaTypeNames;

namespace AvaloniaEdit.Editing
{
Expand Down Expand Up @@ -422,6 +423,14 @@ private static bool CopySelectedText(TextArea textArea)
return true;
}

public static bool ConfirmDataFormat(TextArea textArea, DataObject dataObject, string format)
{
return true;
////var e = new DataObjectSettingDataEventArgs(dataObject, format);
////textArea.RaiseEvent(e);
////return !e.CommandCancelled;
}

private static void SetClipboardText(string text, Visual visual)
{
try
Expand Down Expand Up @@ -526,6 +535,16 @@ private static async void OnPaste(object target, ExecutedRoutedEventArgs args)
}
}

internal static string GetTextToPaste(IDataObject dataObject, TextArea textArea)
{
if (dataObject.Contains(DataFormats.Text))
{
return GetTextToPaste((string)dataObject.Get(DataFormats.Text), textArea);
}

return null;
}

internal static string GetTextToPaste(string text, TextArea textArea)
{
try
Expand Down
32 changes: 16 additions & 16 deletions src/AvaloniaEdit/Editing/RectangleSelection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Avalonia;
Expand Down Expand Up @@ -383,24 +384,23 @@ public static bool PerformRectangularPaste(TextArea textArea, TextViewPosition s
return false;
}

// TODO: clipboard

///// <summary>
///// Gets the name of the entry in the DataObject that signals rectangle selections.
///// </summary>
//public const string RectangularSelectionDataType = "AvalonEditRectangularSelection";
/// <summary>
/// Gets the name of the entry in the DataObject that signals rectangle selections.
/// </summary>
public const string RectangularSelectionDataType = "AvalonEditRectangularSelection";

//public override System.Windows.DataObject CreateDataObject(TextArea textArea)
//{
// var data = base.CreateDataObject(textArea);
public override Avalonia.Input.DataObject CreateDataObject(TextArea textArea)
{
var data = base.CreateDataObject(textArea);

// if (EditingCommandHandler.ConfirmDataFormat(textArea, data, RectangularSelectionDataType)) {
// MemoryStream isRectangle = new MemoryStream(1);
// isRectangle.WriteByte(1);
// data.SetData(RectangularSelectionDataType, isRectangle, false);
// }
// return data;
//}
if (EditingCommandHandler.ConfirmDataFormat(textArea, data, RectangularSelectionDataType))
{
MemoryStream isRectangle = new MemoryStream(1);
isRectangle.WriteByte(1);
data.Set(RectangularSelectionDataType, isRectangle);
}
return data;
}

/// <inheritdoc/>
public override string ToString()
Expand Down
61 changes: 32 additions & 29 deletions src/AvaloniaEdit/Editing/Selection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Avalonia.Input;
using AvaloniaEdit.Document;
using AvaloniaEdit.Utils;

Expand Down Expand Up @@ -262,37 +263,39 @@ public virtual bool Contains(int offset)
Segments.Any(s => s.Contains(offset, 0));
}

// TODO: clipboard
///// <summary>
///// Creates a data object containing the selection's text.
///// </summary>
//public virtual DataObject CreateDataObject(TextArea textArea)
//{
// DataObject data = new DataObject();

// // Ensure we use the appropriate newline sequence for the OS
// string text = TextUtilities.NormalizeNewLines(GetText(), Environment.NewLine);
/// <summary>
/// Creates a data object containing the selection's text.
/// </summary>
public virtual DataObject CreateDataObject(TextArea textArea)
{
DataObject data = new DataObject();

// // Enable drag/drop to Word, Notepad++ and others
// if (EditingCommandHandler.ConfirmDataFormat(textArea, data, DataFormats.UnicodeText)) {
// data.SetText(text);
// }
// Ensure we use the appropriate newline sequence for the OS
string text = TextUtilities.NormalizeNewLines(GetText(), Environment.NewLine);

// // Enable drag/drop to SciTe:
// // We cannot use SetText, thus we need to use typeof(string).FullName as data format.
// // new DataObject(object) calls SetData(object), which in turn calls SetData(Type, data),
// // which then uses Type.FullName as format.
// // We immitate that behavior here as well:
// if (EditingCommandHandler.ConfirmDataFormat(textArea, data, typeof(string).FullName)) {
// data.SetData(typeof(string).FullName, text);
// }
// Enable drag/drop to Word, Notepad++ and others
if (EditingCommandHandler.ConfirmDataFormat(textArea, data, DataFormats.Text))
{
data.Set(DataFormats.Text, text);
}

// // Also copy text in HTML format to clipboard - good for pasting text into Word
// // or to the SharpDevelop forums.
// if (EditingCommandHandler.ConfirmDataFormat(textArea, data, DataFormats.Html)) {
// HtmlClipboard.SetHtml(data, CreateHtmlFragment(new HtmlOptions(textArea.Options)));
// }
// return data;
//}
// Enable drag/drop to SciTe:
// We cannot use SetText, thus we need to use typeof(string).FullName as data format.
// new DataObject(object) calls SetData(object), which in turn calls SetData(Type, data),
// which then uses Type.FullName as format.
// We immitate that behavior here as well:
////if (EditingCommandHandler.ConfirmDataFormat(textArea, data, typeof(string).FullName))
////{
//// data.SetData(typeof(string).FullName, text);
////}

// Also copy text in HTML format to clipboard - good for pasting text into Word
// or to the SharpDevelop forums.
////if (EditingCommandHandler.ConfirmDataFormat(textArea, data, DataFormats.Html))
////{
//// HtmlClipboard.SetHtml(data, CreateHtmlFragment(new HtmlOptions(textArea.Options)));
////}
return data;
}
}
}
Loading