|
|
using Echodict;
|
|
|
using Microsoft.Win32;
|
|
|
using Newtonsoft.Json;
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
using OpenCvSharp;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
|
using System.IO;
|
|
|
using System.Linq;
|
|
|
using System.Printing;
|
|
|
using System.Runtime.ConstrainedExecution;
|
|
|
using System.Runtime.Serialization.Formatters.Binary;
|
|
|
using System.Security.Cryptography;
|
|
|
using System.Text;
|
|
|
using System.Windows;
|
|
|
using System.Windows.Controls;
|
|
|
using System.Windows.Input;
|
|
|
using System.Windows.Media;
|
|
|
using System.Windows.Media.Imaging;
|
|
|
using System.Windows.Media.Media3D;
|
|
|
using System.Windows.Media.TextFormatting;
|
|
|
using System.Windows.Shapes;
|
|
|
using static System.Net.Mime.MediaTypeNames;
|
|
|
using Path = System.IO.Path;
|
|
|
using Point = System.Windows.Point;
|
|
|
using Rect = System.Windows.Rect;
|
|
|
using Size = System.Windows.Size;
|
|
|
using Window = System.Windows.Window;
|
|
|
|
|
|
namespace WpfEditor
|
|
|
{
|
|
|
public class TextEditorControl : Control
|
|
|
{
|
|
|
private Window _imageWindow = null;
|
|
|
private bool _isCut = false;
|
|
|
public EditorMode _mode = EditorMode.ImageOCR;
|
|
|
|
|
|
public JObject _jsn_OpenImg;
|
|
|
public string _md5_OpenImg;
|
|
|
public Dictionary<string, string> _dic_deleted_char = new Dictionary<string, string>();
|
|
|
|
|
|
public string _pthimg;
|
|
|
|
|
|
|
|
|
public MainWindow _mainWindow;
|
|
|
|
|
|
public static BitmapSource MatToBitmapSource(Mat mat)
|
|
|
{
|
|
|
|
|
|
var pixelFormat = mat.Channels() == 1 ? PixelFormats.Gray8 : PixelFormats.Bgr24;
|
|
|
|
|
|
|
|
|
return BitmapSource.Create(
|
|
|
mat.Width, mat.Height,
|
|
|
96, 96,
|
|
|
pixelFormat,
|
|
|
null,
|
|
|
mat.Data,
|
|
|
(int)mat.Step() * mat.Height,
|
|
|
(int)mat.Step()
|
|
|
);
|
|
|
}
|
|
|
|
|
|
public void ShowDraggableImageControl(Mat image, Mat image_orig)
|
|
|
{
|
|
|
var mainWindow = Window.GetWindow(this) as MainWindow;
|
|
|
|
|
|
|
|
|
BitmapSource bitmapSource = MatToBitmapSource(image);
|
|
|
BitmapSource bitmapSource2 = MatToBitmapSource(image_orig);
|
|
|
|
|
|
|
|
|
if (_imageWindow != null)
|
|
|
{
|
|
|
var brdr = _imageWindow.Content as Border;
|
|
|
var stckpnl = brdr.Child as StackPanel;
|
|
|
var img_ctl = stckpnl.Children[1] as System.Windows.Controls.Image;
|
|
|
|
|
|
|
|
|
if (img_ctl != null)
|
|
|
{
|
|
|
img_ctl.Source = bitmapSource;
|
|
|
|
|
|
|
|
|
AdjustWindowSizeBasedOnImage(_imageWindow, bitmapSource.PixelWidth, bitmapSource.PixelHeight);
|
|
|
|
|
|
mainWindow?.ShowImageInPanel(bitmapSource2);
|
|
|
}
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
|
|
|
_imageWindow = new Window
|
|
|
{
|
|
|
WindowStyle = WindowStyle.None,
|
|
|
AllowsTransparency = true,
|
|
|
Background = Brushes.Transparent,
|
|
|
ShowInTaskbar = false,
|
|
|
Topmost = true,
|
|
|
Focusable = false,
|
|
|
ResizeMode = ResizeMode.NoResize,
|
|
|
SizeToContent = SizeToContent.WidthAndHeight,
|
|
|
Owner = Window.GetWindow(this)
|
|
|
};
|
|
|
|
|
|
|
|
|
Border border = new Border
|
|
|
{
|
|
|
Background = new SolidColorBrush(Color.FromArgb(200, 30, 30, 30)),
|
|
|
BorderBrush = Brushes.LightGray,
|
|
|
BorderThickness = new Thickness(1),
|
|
|
CornerRadius = new CornerRadius(3),
|
|
|
Padding = new Thickness(5)
|
|
|
};
|
|
|
|
|
|
|
|
|
StackPanel stackPanel = new StackPanel {
|
|
|
HorizontalAlignment = HorizontalAlignment.Left
|
|
|
};
|
|
|
|
|
|
|
|
|
Grid titleBar = new Grid { Height = 20, Margin = new Thickness(0, 0, 0, 5) };
|
|
|
titleBar.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) });
|
|
|
titleBar.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto });
|
|
|
|
|
|
TextBlock titleText = new TextBlock
|
|
|
{
|
|
|
Text = "图片预览",
|
|
|
Foreground = Brushes.White,
|
|
|
VerticalAlignment = VerticalAlignment.Center
|
|
|
};
|
|
|
|
|
|
Button closeButton = new Button
|
|
|
{
|
|
|
Content = "X",
|
|
|
Width = 20,
|
|
|
Height = 20,
|
|
|
Padding = new Thickness(0),
|
|
|
Background = Brushes.Transparent,
|
|
|
Foreground = Brushes.White,
|
|
|
BorderThickness = new Thickness(0)
|
|
|
};
|
|
|
closeButton.Click += (s, e) => _imageWindow.Close();
|
|
|
|
|
|
Grid.SetColumn(titleText, 0);
|
|
|
Grid.SetColumn(closeButton, 1);
|
|
|
titleBar.Children.Add(titleText);
|
|
|
titleBar.Children.Add(closeButton);
|
|
|
|
|
|
|
|
|
System.Windows.Controls.Image imageControl = new System.Windows.Controls.Image
|
|
|
{
|
|
|
Source = bitmapSource,
|
|
|
Stretch = Stretch.None,
|
|
|
HorizontalAlignment = HorizontalAlignment.Left
|
|
|
};
|
|
|
|
|
|
|
|
|
stackPanel.Children.Add(titleBar);
|
|
|
stackPanel.Children.Add(imageControl);
|
|
|
|
|
|
|
|
|
border.Child = stackPanel;
|
|
|
|
|
|
|
|
|
_imageWindow.Content = border;
|
|
|
|
|
|
|
|
|
border.MouseLeftButtonDown += (s, e) =>
|
|
|
{
|
|
|
if (e.ClickCount == 1)
|
|
|
{
|
|
|
_imageWindow.DragMove();
|
|
|
}
|
|
|
};
|
|
|
|
|
|
|
|
|
ContextMenu contextMenu = new ContextMenu();
|
|
|
MenuItem closeMenuItem = new MenuItem { Header = "关闭" };
|
|
|
closeMenuItem.Click += (s, e) => _imageWindow.Close();
|
|
|
contextMenu.Items.Add(closeMenuItem);
|
|
|
border.ContextMenu = contextMenu;
|
|
|
|
|
|
|
|
|
_imageWindow.Show();
|
|
|
|
|
|
|
|
|
AdjustWindowSizeBasedOnImage(_imageWindow, bitmapSource.PixelWidth, bitmapSource.PixelHeight);
|
|
|
|
|
|
|
|
|
if (mainWindow != null)
|
|
|
{
|
|
|
_imageWindow.Left = 0;
|
|
|
_imageWindow.Top = mainWindow.Top + (mainWindow.Height - _imageWindow.ActualHeight) / 2;
|
|
|
}
|
|
|
|
|
|
if (mainWindow != null)
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
private void AdjustWindowSizeBasedOnImage(Window window, int imageWidth, int imageHeight)
|
|
|
{
|
|
|
|
|
|
double screenWidth = SystemParameters.WorkArea.Width;
|
|
|
double screenHeight = SystemParameters.WorkArea.Height;
|
|
|
|
|
|
|
|
|
double maxWidth = screenWidth * 0.8;
|
|
|
double maxHeight = screenHeight * 0.8;
|
|
|
|
|
|
|
|
|
const double uiExtraWidth = 30;
|
|
|
const double uiExtraHeight = 50;
|
|
|
|
|
|
|
|
|
double requiredWidth = imageWidth + uiExtraWidth;
|
|
|
double requiredHeight = imageHeight + uiExtraHeight;
|
|
|
|
|
|
|
|
|
if (requiredWidth > maxWidth || requiredHeight > maxHeight)
|
|
|
{
|
|
|
|
|
|
double scaleX = maxWidth / requiredWidth;
|
|
|
double scaleY = maxHeight / requiredHeight;
|
|
|
double scale = Math.Min(scaleX, scaleY);
|
|
|
|
|
|
|
|
|
var image = ((window.Content as Border).Child as StackPanel).Children[1] as System.Windows.Controls.Image;
|
|
|
if (image != null)
|
|
|
{
|
|
|
|
|
|
image.Stretch = Stretch.Uniform;
|
|
|
image.MaxWidth = maxWidth - uiExtraWidth;
|
|
|
image.MaxHeight = maxHeight - uiExtraHeight;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
window.UpdateLayout();
|
|
|
}
|
|
|
|
|
|
#region 依赖属性
|
|
|
|
|
|
public static readonly DependencyProperty TextProperty =
|
|
|
DependencyProperty.Register("Text", typeof(string), typeof(TextEditorControl),
|
|
|
new FrameworkPropertyMetadata(string.Empty, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnTextChanged));
|
|
|
|
|
|
public static readonly DependencyProperty IsReadOnlyProperty =
|
|
|
DependencyProperty.Register("IsReadOnly", typeof(bool), typeof(TextEditorControl),
|
|
|
new PropertyMetadata(false));
|
|
|
|
|
|
public static readonly DependencyProperty TabSizeProperty =
|
|
|
DependencyProperty.Register("TabSize", typeof(int), typeof(TextEditorControl),
|
|
|
new PropertyMetadata(4));
|
|
|
|
|
|
public static readonly DependencyProperty LineNumbersVisibleProperty =
|
|
|
DependencyProperty.Register("LineNumbersVisible", typeof(bool), typeof(TextEditorControl),
|
|
|
new PropertyMetadata(true));
|
|
|
|
|
|
public static readonly DependencyProperty ShowWhitespacesProperty =
|
|
|
DependencyProperty.Register("ShowWhitespaces", typeof(bool), typeof(TextEditorControl),
|
|
|
new PropertyMetadata(true));
|
|
|
|
|
|
public static readonly DependencyProperty HighlightCurrentLineProperty =
|
|
|
DependencyProperty.Register("HighlightCurrentLine", typeof(bool), typeof(TextEditorControl),
|
|
|
new PropertyMetadata(true));
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
private void CreateContextMenu()
|
|
|
{
|
|
|
ContextMenu = new ContextMenu();
|
|
|
|
|
|
|
|
|
MenuItem copyMenuItem = new MenuItem
|
|
|
{
|
|
|
Header = "复制",
|
|
|
Command = ApplicationCommands.Copy
|
|
|
};
|
|
|
ContextMenu.Items.Add(copyMenuItem);
|
|
|
|
|
|
|
|
|
MenuItem cutMenuItem = new MenuItem
|
|
|
{
|
|
|
Header = "剪切",
|
|
|
Command = ApplicationCommands.Cut
|
|
|
};
|
|
|
ContextMenu.Items.Add(cutMenuItem);
|
|
|
|
|
|
|
|
|
MenuItem pasteMenuItem = new MenuItem
|
|
|
{
|
|
|
Header = "粘贴",
|
|
|
Command = ApplicationCommands.Paste
|
|
|
};
|
|
|
ContextMenu.Items.Add(pasteMenuItem);
|
|
|
|
|
|
|
|
|
ContextMenu.Items.Add(new Separator());
|
|
|
|
|
|
|
|
|
MenuItem selectAllMenuItem = new MenuItem
|
|
|
{
|
|
|
Header = "全选",
|
|
|
Command = ApplicationCommands.SelectAll
|
|
|
};
|
|
|
ContextMenu.Items.Add(selectAllMenuItem);
|
|
|
|
|
|
|
|
|
|
|
|
MenuItem jishoMenuItem = new MenuItem
|
|
|
{
|
|
|
Header = "jisho"
|
|
|
};
|
|
|
jishoMenuItem.Click += OnjishoMenuItemClick;
|
|
|
ContextMenu.Items.Add(jishoMenuItem);
|
|
|
|
|
|
|
|
|
MenuItem naverjaMenuItem = new MenuItem
|
|
|
{
|
|
|
Header = "naverja"
|
|
|
};
|
|
|
naverjaMenuItem.Click += OnnaverjaMenuItemMenuItemClick;
|
|
|
ContextMenu.Items.Add(naverjaMenuItem);
|
|
|
|
|
|
|
|
|
MenuItem QeryMenuItem = new MenuItem
|
|
|
{
|
|
|
Header = "查询"
|
|
|
};
|
|
|
QeryMenuItem.Click += OnQueryMenuItemClick;
|
|
|
ContextMenu.Items.Add(QeryMenuItem);
|
|
|
|
|
|
|
|
|
MenuItem translateMenuItem = new MenuItem
|
|
|
{
|
|
|
Header = "翻译"
|
|
|
};
|
|
|
translateMenuItem.Click += OnTranslateMenuItemClick;
|
|
|
ContextMenu.Items.Add(translateMenuItem);
|
|
|
|
|
|
|
|
|
ContextMenu.Opened += OnContextMenuOpened;
|
|
|
}
|
|
|
|
|
|
|
|
|
private void OnContextMenuOpened(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
bool hasSelection = HasSelection();
|
|
|
bool hasClipboardText = Clipboard.ContainsText() || Clipboard.ContainsData("TextEditorCustomFormat");
|
|
|
|
|
|
|
|
|
foreach (var item in ContextMenu.Items)
|
|
|
{
|
|
|
if (item is MenuItem menuItem)
|
|
|
{
|
|
|
if (menuItem.Command == ApplicationCommands.Copy ||
|
|
|
menuItem.Command == ApplicationCommands.Cut)
|
|
|
{
|
|
|
menuItem.IsEnabled = hasSelection;
|
|
|
}
|
|
|
else if (menuItem.Command == ApplicationCommands.Paste)
|
|
|
{
|
|
|
menuItem.IsEnabled = !IsReadOnly && hasClipboardText;
|
|
|
}
|
|
|
else if (menuItem.Command == ApplicationCommands.SelectAll)
|
|
|
{
|
|
|
menuItem.IsEnabled = _lines.Count > 0 &&
|
|
|
_lines.Any(line => line.Characters.Count > 0);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
private async void OnTranslateMenuItemClick(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
|
|
|
if (HasSelection()) {
|
|
|
string text = GetSelectedText();
|
|
|
string res = await DeepSeek.translate(text);
|
|
|
if (res != "")
|
|
|
{
|
|
|
JObject jsn = Newtonsoft.Json.JsonConvert.DeserializeObject<JObject>(res);
|
|
|
string zh = jsn["choices"][0]["message"]["content"].Value<string>();
|
|
|
var win = new TranslationResultWindow(zh);
|
|
|
win.Owner = Window.GetWindow(this);
|
|
|
win.WindowStartupLocation = WindowStartupLocation.CenterOwner;
|
|
|
win.ShowDialog();
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private async void OnjishoMenuItemClick(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
if (HasSelection())
|
|
|
{
|
|
|
string text = GetSelectedText();
|
|
|
var content = System.Web.HttpUtility.UrlEncode(text);
|
|
|
String url = $"https://jisho.org/search/{content}";
|
|
|
|
|
|
var win = Echodict.EchodictWindow.Instance;
|
|
|
win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
|
|
|
win.OpenUrl(url);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private async void OnnaverjaMenuItemMenuItemClick(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
if (HasSelection())
|
|
|
{
|
|
|
string text = GetSelectedText();
|
|
|
var content = System.Web.HttpUtility.UrlEncode(text);
|
|
|
String url = $"https://ja.dict.naver.com/#/search?query={content}&range=all";
|
|
|
|
|
|
var win = Echodict.EchodictWindow.Instance;
|
|
|
win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
|
|
|
win.OpenUrl(url);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
private async void OnQueryMenuItemClick(object sender, RoutedEventArgs e)
|
|
|
{
|
|
|
if (HasSelection())
|
|
|
{
|
|
|
string text = GetSelectedText();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var win = Echodict.EchodictWindow.Instance;
|
|
|
|
|
|
win.WindowStartupLocation = WindowStartupLocation.CenterScreen;
|
|
|
win.ShowWord(text);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
#region 属性
|
|
|
|
|
|
public string Text
|
|
|
{
|
|
|
get { return (string)GetValue(TextProperty); }
|
|
|
set { SetValue(TextProperty, value); }
|
|
|
}
|
|
|
|
|
|
public bool IsReadOnly
|
|
|
{
|
|
|
get { return (bool)GetValue(IsReadOnlyProperty); }
|
|
|
set { SetValue(IsReadOnlyProperty, value); }
|
|
|
}
|
|
|
|
|
|
public int TabSize
|
|
|
{
|
|
|
get { return (int)GetValue(TabSizeProperty); }
|
|
|
set { SetValue(TabSizeProperty, value); }
|
|
|
}
|
|
|
|
|
|
public bool LineNumbersVisible
|
|
|
{
|
|
|
get { return (bool)GetValue(LineNumbersVisibleProperty); }
|
|
|
set { SetValue(LineNumbersVisibleProperty, value); }
|
|
|
}
|
|
|
|
|
|
public bool ShowWhitespaces
|
|
|
{
|
|
|
get { return (bool)GetValue(ShowWhitespacesProperty); }
|
|
|
set { SetValue(ShowWhitespacesProperty, value); }
|
|
|
}
|
|
|
|
|
|
public bool HighlightCurrentLine
|
|
|
{
|
|
|
get { return (bool)GetValue(HighlightCurrentLineProperty); }
|
|
|
set { SetValue(HighlightCurrentLineProperty, value); }
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region 内部数据结构
|
|
|
|
|
|
public List<TextLine> _lines = new List<TextLine>();
|
|
|
public int _cursorLine = 0;
|
|
|
public int _cursorColumn = 0;
|
|
|
public int _selectionStartLine = -1;
|
|
|
public int _selectionStartColumn = -1;
|
|
|
public int _selectionEndLine = -1;
|
|
|
public int _selectionEndColumn = -1;
|
|
|
public List<UndoAction> _undoBuffer = new List<UndoAction>();
|
|
|
public int _undoIndex = -1;
|
|
|
public bool _textChanged = false;
|
|
|
public int _scrollOffset = 0;
|
|
|
|
|
|
|
|
|
private FindDialog _findDialog = null;
|
|
|
private string _lastSearchText = "";
|
|
|
private bool _lastMatchCase = false;
|
|
|
private bool _lastWholeWord = false;
|
|
|
private int _lastFoundLine = -1;
|
|
|
private int _lastFoundColumn = -1;
|
|
|
private List<(int line, int column, int length)> _findResults = new List<(int, int, int)>();
|
|
|
private int _currentFindIndex = -1;
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region 构造函数
|
|
|
|
|
|
static TextEditorControl()
|
|
|
{
|
|
|
DefaultStyleKeyProperty.OverrideMetadata(typeof(TextEditorControl),
|
|
|
new FrameworkPropertyMetadata(typeof(TextEditorControl)));
|
|
|
}
|
|
|
|
|
|
public TextEditorControl()
|
|
|
{
|
|
|
this.Focusable = true;
|
|
|
this.FocusVisualStyle = null;
|
|
|
this.ClipToBounds = true;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_lines.Add(new TextLine());
|
|
|
|
|
|
|
|
|
CommandBindings.Add(new CommandBinding(ApplicationCommands.Copy, OnCopy));
|
|
|
CommandBindings.Add(new CommandBinding(ApplicationCommands.Cut, OnCut));
|
|
|
CommandBindings.Add(new CommandBinding(ApplicationCommands.Paste, OnPaste));
|
|
|
CommandBindings.Add(new CommandBinding(ApplicationCommands.Undo, OnUndo));
|
|
|
CommandBindings.Add(new CommandBinding(ApplicationCommands.Redo, OnRedo));
|
|
|
CommandBindings.Add(new CommandBinding(ApplicationCommands.SelectAll, OnSelectAll));
|
|
|
|
|
|
|
|
|
CreateContextMenu();
|
|
|
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region 事件处理
|
|
|
|
|
|
public static void OnTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
|
|
|
{
|
|
|
var control = (TextEditorControl)d;
|
|
|
if (e.NewValue is string)
|
|
|
{
|
|
|
|
|
|
}
|
|
|
else if (e.NewValue is JObject)
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
protected override void OnRender(DrawingContext drawingContext)
|
|
|
{
|
|
|
base.OnRender(drawingContext);
|
|
|
RenderEditor(drawingContext);
|
|
|
}
|
|
|
|
|
|
protected override void OnMouseWheel(MouseWheelEventArgs e)
|
|
|
{
|
|
|
base.OnMouseWheel(e);
|
|
|
|
|
|
|
|
|
double lineHeight = FontSize * 1.2;
|
|
|
|
|
|
|
|
|
|
|
|
int scrollLines = e.Delta > 0 ? -3 : 3;
|
|
|
_scrollOffset += scrollLines;
|
|
|
|
|
|
|
|
|
int maxVisibleLines = (int)(ActualHeight / lineHeight);
|
|
|
int maxScrollOffset = Math.Max(0, _lines.Count - maxVisibleLines + 1);
|
|
|
_scrollOffset = Math.Max(0, Math.Min(_scrollOffset, maxScrollOffset));
|
|
|
|
|
|
|
|
|
|
|
|
e.Handled = true;
|
|
|
InvalidateVisual();
|
|
|
}
|
|
|
|
|
|
protected override void OnTextInput(TextCompositionEventArgs e)
|
|
|
{
|
|
|
base.OnTextInput(e);
|
|
|
if (!IsReadOnly)
|
|
|
{
|
|
|
|
|
|
InsertTextCharacters(e.Text);
|
|
|
|
|
|
|
|
|
_textChanged = true;
|
|
|
|
|
|
|
|
|
OnTextChanged();
|
|
|
|
|
|
|
|
|
OnCursorPositionChanged(_cursorLine, _cursorColumn);
|
|
|
|
|
|
|
|
|
InvalidateVisual();
|
|
|
|
|
|
e.Handled = true;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
protected override void OnKeyDown(KeyEventArgs e)
|
|
|
{
|
|
|
base.OnKeyDown(e);
|
|
|
HandleKeyDown(e);
|
|
|
}
|
|
|
|
|
|
public void showCurrChar()
|
|
|
{
|
|
|
TextCharacter chr = GetCharacterAfterCursor();
|
|
|
if (chr != null)
|
|
|
{
|
|
|
string m5 = chr.ImageMD5;
|
|
|
if (m5 != "")
|
|
|
{
|
|
|
|
|
|
var (pth_img, pth_json) = Global.get_img_json_pthv2(_pthimg);
|
|
|
if (Path.Exists(pth_json) && Path.Exists(pth_img))
|
|
|
{
|
|
|
string jsn_str = File.ReadAllText(pth_json, Encoding.UTF8);
|
|
|
JObject jsn = (JObject)JsonConvert.DeserializeObject(jsn_str);
|
|
|
|
|
|
|
|
|
string b64_str = File.ReadAllText(pth_img, Encoding.UTF8);
|
|
|
byte[] img_bytes = Base64.Base64Decode(b64_str);
|
|
|
|
|
|
Mat im_orig = Cv2.ImDecode(img_bytes, ImreadModes.Color);
|
|
|
|
|
|
int width = jsn.Value<int>("width");
|
|
|
int height = jsn.Value<int>("height");
|
|
|
|
|
|
var prism_wordsInfo = jsn["prism_wordsInfo"].Value<JArray>();
|
|
|
bool found = false;
|
|
|
foreach (var item in prism_wordsInfo)
|
|
|
{
|
|
|
JArray charInfo = item["charInfo"].Value<JArray>();
|
|
|
string line_word = item["word"].Value<string>();
|
|
|
int line_x = item["x"].Value<int>();
|
|
|
int line_y = item["y"].Value<int>();
|
|
|
int line_width = item["width"].Value<int>();
|
|
|
int line_height = item["height"].Value<int>();
|
|
|
|
|
|
foreach (var item2 in charInfo)
|
|
|
{
|
|
|
string oneChar = item2["word"].Value<string>();
|
|
|
int x = item2["x"].Value<int>();
|
|
|
int y = item2["y"].Value<int>();
|
|
|
int w = item2["w"].Value<int>();
|
|
|
int h = item2["h"].Value<int>();
|
|
|
|
|
|
|
|
|
if (true)
|
|
|
{
|
|
|
if (chr.x == x)
|
|
|
{
|
|
|
if (chr.y == y)
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
Cv2.Rectangle(im_orig, new OpenCvSharp.Rect(chr.x, chr.y, chr.w, chr.h), new OpenCvSharp.Scalar(0, 0, 255));
|
|
|
|
|
|
Mat cut = im_orig.Clone(new OpenCvSharp.Rect(0, line_y, im_orig.Width, line_height));
|
|
|
|
|
|
|
|
|
ShowDraggableImageControl(cut, im_orig);
|
|
|
|
|
|
|
|
|
var displayPos = ConvertOriginalToDisplayCoordinates(chr.x, chr.y);
|
|
|
var displaySize = ConvertOriginalToDisplaySize(chr.w, chr.h);
|
|
|
|
|
|
var x_y_ = ConvertDisplayToOriginalCoordinates(displayPos.X, displayPos.Y);
|
|
|
|
|
|
var w_h_ = ConvertDisplayToOriginalSize(displaySize.Width, displaySize.Height);
|
|
|
|
|
|
|
|
|
_mainWindow?.ShowCharacterSelection(displayPos.X, displayPos.Y, displaySize.Width, displaySize.Height);
|
|
|
|
|
|
found = true;
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (found)
|
|
|
{
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
System.Diagnostics.Debug.WriteLine($"光标后字符: {chr.Character}");
|
|
|
}
|
|
|
}
|
|
|
|
|
|
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
|
|
|
{
|
|
|
base.OnMouseLeftButtonDown(e);
|
|
|
Focus();
|
|
|
Point position = e.GetPosition(this);
|
|
|
SetCursorPositionFromPoint(position);
|
|
|
if (Keyboard.Modifiers != ModifierKeys.Shift)
|
|
|
{
|
|
|
ClearSelection();
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
UpdateSelection();
|
|
|
}
|
|
|
|
|
|
|
|
|
if (e.ChangedButton == MouseButton.Left && e.ButtonState == MouseButtonState.Pressed)
|
|
|
{
|
|
|
showCurrChar();
|
|
|
}
|
|
|
|
|
|
e.Handled = true;
|
|
|
InvalidateVisual();
|
|
|
}
|
|
|
|
|
|
|
|
|
protected override void OnMouseRightButtonDown(MouseButtonEventArgs e)
|
|
|
{
|
|
|
base.OnMouseRightButtonDown(e);
|
|
|
|
|
|
|
|
|
Focus();
|
|
|
|
|
|
|
|
|
Point position = e.GetPosition(this);
|
|
|
|
|
|
|
|
|
|
|
|
if (!IsPositionInSelection(position))
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
InvalidateVisual();
|
|
|
}
|
|
|
|
|
|
e.Handled = true;
|
|
|
}
|
|
|
|
|
|
protected override void OnMouseMove(MouseEventArgs e)
|
|
|
{
|
|
|
base.OnMouseMove(e);
|
|
|
if (e.LeftButton == MouseButtonState.Pressed)
|
|
|
{
|
|
|
Point position = e.GetPosition(this);
|
|
|
SetCursorPositionFromPoint(position);
|
|
|
UpdateSelection();
|
|
|
InvalidateVisual();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region 命令处理
|
|
|
|
|
|
|
|
|
public Point ConvertOriginalToDisplayCoordinates(int originalX, int originalY)
|
|
|
{
|
|
|
if (_mainWindow?.ViewModel?.Image == null)
|
|
|
return new Point(originalX, originalY);
|
|
|
|
|
|
|
|
|
var imageElement = _mainWindow.image;
|
|
|
if (imageElement == null)
|
|
|
return new Point(originalX, originalY);
|
|
|
|
|
|
|
|
|
double originalWidth = _mainWindow.ViewModel.Image.PixelWidth;
|
|
|
double originalHeight = _mainWindow.ViewModel.Image.PixelHeight;
|
|
|
|
|
|
|
|
|
double displayWidth = imageElement.ActualWidth;
|
|
|
double displayHeight = imageElement.ActualHeight;
|
|
|
|
|
|
|
|
|
double scaleX = displayWidth / originalWidth;
|
|
|
double scaleY = displayHeight / originalHeight;
|
|
|
|
|
|
|
|
|
double displayX = originalX * scaleX;
|
|
|
double displayY = originalY * scaleY;
|
|
|
|
|
|
return new Point(displayX, displayY);
|
|
|
}
|
|
|
|
|
|
|
|
|
public Point ConvertDisplayToOriginalCoordinates(double displayX, double displayY)
|
|
|
{
|
|
|
if (_mainWindow?.ViewModel?.Image == null)
|
|
|
return new Point(displayX, displayY);
|
|
|
|
|
|
|
|
|
var imageElement = _mainWindow.image;
|
|
|
if (imageElement == null)
|
|
|
return new Point(displayX, displayY);
|
|
|
|
|
|
|
|
|
double originalWidth = _mainWindow.ViewModel.Image.PixelWidth;
|
|
|
double originalHeight = _mainWindow.ViewModel.Image.PixelHeight;
|
|
|
|
|
|
|
|
|
double displayWidth = imageElement.ActualWidth;
|
|
|
double displayHeight = imageElement.ActualHeight;
|
|
|
|
|
|
|
|
|
double scaleX = displayWidth / originalWidth;
|
|
|
double scaleY = displayHeight / originalHeight;
|
|
|
|
|
|
|
|
|
double originalX = displayX / scaleX;
|
|
|
double originalY = displayY / scaleY;
|
|
|
|
|
|
return new Point(originalX, originalY);
|
|
|
}
|
|
|
|
|
|
|
|
|
public Size ConvertOriginalToDisplaySize(int originalWidth, int originalHeight)
|
|
|
{
|
|
|
if (_mainWindow?.ViewModel?.Image == null)
|
|
|
return new Size(originalWidth, originalHeight);
|
|
|
|
|
|
|
|
|
var imageElement = _mainWindow.image;
|
|
|
if (imageElement == null)
|
|
|
return new Size(originalWidth, originalHeight);
|
|
|
|
|
|
|
|
|
double imgOriginalWidth = _mainWindow.ViewModel.Image.PixelWidth;
|
|
|
double imgOriginalHeight = _mainWindow.ViewModel.Image.PixelHeight;
|
|
|
|
|
|
|
|
|
double displayWidth = imageElement.ActualWidth;
|
|
|
double displayHeight = imageElement.ActualHeight;
|
|
|
|
|
|
|
|
|
double scaleX = displayWidth / imgOriginalWidth;
|
|
|
double scaleY = displayHeight / imgOriginalHeight;
|
|
|
|
|
|
|
|
|
double scaledWidth = originalWidth * scaleX;
|
|
|
double scaledHeight = originalHeight * scaleY;
|
|
|
|
|
|
return new Size(scaledWidth, scaledHeight);
|
|
|
}
|
|
|
|
|
|
|
|
|
public Size ConvertDisplayToOriginalSize(double displayWidth, double displayHeight)
|
|
|
{
|
|
|
if (_mainWindow?.ViewModel?.Image == null)
|
|
|
return new Size(displayWidth, displayHeight);
|
|
|
|
|
|
|
|
|
var imageElement = _mainWindow.image;
|
|
|
if (imageElement == null)
|
|
|
return new Size(displayWidth, displayHeight);
|
|
|
|
|
|
|
|
|
double imgOriginalWidth = _mainWindow.ViewModel.Image.PixelWidth;
|
|
|
double imgOriginalHeight = _mainWindow.ViewModel.Image.PixelHeight;
|
|
|
|
|
|
|
|
|
double imgDisplayWidth = imageElement.ActualWidth;
|
|
|
double imgDisplayHeight = imageElement.ActualHeight;
|
|
|
|
|
|
|
|
|
double scaleX = imgDisplayWidth / imgOriginalWidth;
|
|
|
double scaleY = imgDisplayHeight / imgOriginalHeight;
|
|
|
|
|
|
|
|
|
double originalWidth = displayWidth / scaleX;
|
|
|
double originalHeight = displayHeight / scaleY;
|
|
|
|
|
|
return new Size(originalWidth, originalHeight);
|
|
|
}
|
|
|
|
|
|
private void OnCopy(object sender, ExecutedRoutedEventArgs e)
|
|
|
{
|
|
|
if (HasSelection())
|
|
|
{
|
|
|
_isCut = false;
|
|
|
|
|
|
|
|
|
DataObject dataObject = new DataObject();
|
|
|
|
|
|
|
|
|
dataObject.SetText(GetSelectedText());
|
|
|
|
|
|
|
|
|
try
|
|
|
{
|
|
|
var characters = GetSelectedTextCharacters();
|
|
|
if (characters.Count > 0)
|
|
|
{
|
|
|
string json = Newtonsoft.Json.JsonConvert.SerializeObject(characters);
|
|
|
dataObject.SetData("TextEditorCustomFormat", json);
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
System.Diagnostics.Debug.WriteLine($"复制到剪贴板时出错: {ex.Message}");
|
|
|
}
|
|
|
|
|
|
|
|
|
Clipboard.SetDataObject(dataObject);
|
|
|
}
|
|
|
e.Handled = true;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void OnCut(object sender, ExecutedRoutedEventArgs e)
|
|
|
{
|
|
|
if (!IsReadOnly && HasSelection())
|
|
|
{
|
|
|
_isCut = true;
|
|
|
|
|
|
|
|
|
DataObject dataObject = new DataObject();
|
|
|
|
|
|
|
|
|
dataObject.SetText(GetSelectedText());
|
|
|
|
|
|
|
|
|
try
|
|
|
{
|
|
|
var characters = GetSelectedTextCharacters();
|
|
|
if (characters.Count > 0)
|
|
|
{
|
|
|
|
|
|
string json = Newtonsoft.Json.JsonConvert.SerializeObject(characters);
|
|
|
dataObject.SetData("TextEditorCustomFormat", json);
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
System.Diagnostics.Debug.WriteLine($"剪切到剪贴板时出错: {ex.Message}");
|
|
|
}
|
|
|
|
|
|
|
|
|
Clipboard.SetDataObject(dataObject);
|
|
|
|
|
|
DeleteSelection();
|
|
|
InvalidateVisual();
|
|
|
}
|
|
|
e.Handled = true;
|
|
|
}
|
|
|
|
|
|
private void OnPaste(object sender, ExecutedRoutedEventArgs e)
|
|
|
{
|
|
|
if (!IsReadOnly)
|
|
|
{
|
|
|
List<TextCharacter> characters_selected = GetSelectedTextCharacters();
|
|
|
|
|
|
|
|
|
bool customDataPasted = false;
|
|
|
if (Clipboard.ContainsData("TextEditorCustomFormat"))
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
|
|
|
string json = Clipboard.GetData("TextEditorCustomFormat") as string;
|
|
|
|
|
|
|
|
|
List<TextCharacter> characters = new List<TextCharacter>();
|
|
|
|
|
|
List<JObject> ls = Newtonsoft.Json.JsonConvert.DeserializeObject<List<JObject>>(json);
|
|
|
|
|
|
|
|
|
foreach (var item in ls)
|
|
|
{
|
|
|
var Character = item["Character"].Value<JObject>();
|
|
|
var Value = Character["Value"].Value<int>();
|
|
|
System.Text.Rune rune = new System.Text.Rune(Value);
|
|
|
|
|
|
|
|
|
var chr = new TextCharacter(
|
|
|
rune,
|
|
|
item["guid"].Value<string>(),
|
|
|
item["x"].Value<int>(),
|
|
|
item["y"].Value<int>(),
|
|
|
item["w"].Value<int>(),
|
|
|
item["h"].Value<int>(),
|
|
|
item["ImageMD5"].Value<string>()
|
|
|
);
|
|
|
characters.Add(chr);
|
|
|
}
|
|
|
|
|
|
if (characters != null && characters.Count > 0)
|
|
|
{
|
|
|
InsertTextCharacters(characters);
|
|
|
customDataPasted = true;
|
|
|
}
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
System.Diagnostics.Debug.WriteLine($"从剪贴板粘贴时出错: {ex.Message}");
|
|
|
customDataPasted = false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
if (!customDataPasted && Clipboard.ContainsText())
|
|
|
{
|
|
|
string text = Clipboard.GetText();
|
|
|
if (text.Length > 0)
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
InsertTextCharacters(text);
|
|
|
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (_isCut)
|
|
|
{
|
|
|
Clipboard.Clear();
|
|
|
_isCut = false;
|
|
|
}
|
|
|
|
|
|
InvalidateVisual();
|
|
|
}
|
|
|
|
|
|
if (HasSelection())
|
|
|
{
|
|
|
DeleteSelection();
|
|
|
}
|
|
|
|
|
|
e.Handled = true;
|
|
|
}
|
|
|
|
|
|
|
|
|
public void InsertTextCharacters(string text)
|
|
|
{
|
|
|
if (text.Length > 0)
|
|
|
{
|
|
|
List<TextCharacter> characters = new List<TextCharacter>();
|
|
|
|
|
|
TextCharacter c = null;
|
|
|
if (HasSelection())
|
|
|
{
|
|
|
List<TextCharacter> characters_selected = GetSelectedTextCharacters();
|
|
|
if (characters_selected.Count > 0)
|
|
|
{
|
|
|
c = characters_selected[0];
|
|
|
}
|
|
|
} else
|
|
|
{
|
|
|
c = GetCharacterAfterCursor();
|
|
|
if (c == null)
|
|
|
{
|
|
|
c = GetCharacterBeforeCursor();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
for (int i = 0; i < text.Length;)
|
|
|
{
|
|
|
Rune rune = Rune.GetRuneAt(text, i);
|
|
|
|
|
|
if (c != null)
|
|
|
{
|
|
|
var chr = new TextCharacter(
|
|
|
rune,
|
|
|
c.guid,
|
|
|
c.x,
|
|
|
c.y,
|
|
|
c.w,
|
|
|
c.h,
|
|
|
c.ImageMD5
|
|
|
);
|
|
|
characters.Add(chr);
|
|
|
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
var chr = new TextCharacter(
|
|
|
rune,
|
|
|
Guid.NewGuid().ToString(),
|
|
|
- 1,
|
|
|
-1,
|
|
|
-1,
|
|
|
-1,
|
|
|
""
|
|
|
);
|
|
|
characters.Add(chr);
|
|
|
}
|
|
|
i += rune.Utf16SequenceLength;
|
|
|
}
|
|
|
|
|
|
if (characters != null && characters.Count > 0)
|
|
|
{
|
|
|
InsertTextCharacters(characters);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private void InsertTextCharacters(List<TextCharacter> characters)
|
|
|
{
|
|
|
if (characters == null || characters.Count == 0)
|
|
|
return;
|
|
|
|
|
|
|
|
|
if (HasSelection())
|
|
|
{
|
|
|
DeleteSelection();
|
|
|
}
|
|
|
|
|
|
|
|
|
var undoAction = new UndoAction
|
|
|
{
|
|
|
CursorBeforeLine = _cursorLine,
|
|
|
CursorBeforeColumn = _cursorColumn
|
|
|
};
|
|
|
|
|
|
|
|
|
var line = _lines[_cursorLine];
|
|
|
line.Characters.InsertRange(_cursorColumn, characters);
|
|
|
_cursorColumn += characters.Count;
|
|
|
|
|
|
|
|
|
undoAction.AddedText = string.Concat(characters.Select(c => c.Character.ToString()));
|
|
|
undoAction.AddedStartLine = _cursorLine;
|
|
|
undoAction.AddedStartColumn = _cursorColumn - characters.Count;
|
|
|
undoAction.AddedEndLine = _cursorLine;
|
|
|
undoAction.AddedEndColumn = _cursorColumn;
|
|
|
|
|
|
|
|
|
undoAction.CursorAfterLine = _cursorLine;
|
|
|
undoAction.CursorAfterColumn = _cursorColumn;
|
|
|
|
|
|
|
|
|
if (_undoIndex < _undoBuffer.Count - 1)
|
|
|
{
|
|
|
_undoBuffer.RemoveRange(_undoIndex + 1, _undoBuffer.Count - _undoIndex - 1);
|
|
|
}
|
|
|
_undoBuffer.Add(undoAction);
|
|
|
_undoIndex = _undoBuffer.Count - 1;
|
|
|
|
|
|
|
|
|
_textChanged = true;
|
|
|
|
|
|
|
|
|
OnTextChanged();
|
|
|
|
|
|
|
|
|
OnCursorPositionChanged(_cursorLine, _cursorColumn);
|
|
|
}
|
|
|
|
|
|
private void OnUndo(object sender, ExecutedRoutedEventArgs e)
|
|
|
{
|
|
|
if (!IsReadOnly && CanUndo())
|
|
|
{
|
|
|
Undo();
|
|
|
InvalidateVisual();
|
|
|
}
|
|
|
e.Handled = true;
|
|
|
}
|
|
|
|
|
|
private void OnRedo(object sender, ExecutedRoutedEventArgs e)
|
|
|
{
|
|
|
if (!IsReadOnly && CanRedo())
|
|
|
{
|
|
|
Redo();
|
|
|
InvalidateVisual();
|
|
|
}
|
|
|
e.Handled = true;
|
|
|
}
|
|
|
|
|
|
private void OnSelectAll(object sender, ExecutedRoutedEventArgs e)
|
|
|
{
|
|
|
SelectAll();
|
|
|
InvalidateVisual();
|
|
|
e.Handled = true;
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
#region 辅助方法
|
|
|
|
|
|
public void SetText(string text)
|
|
|
{
|
|
|
_lines.Clear();
|
|
|
_undoBuffer.Clear();
|
|
|
_undoIndex = -1;
|
|
|
|
|
|
if (string.IsNullOrEmpty(text))
|
|
|
{
|
|
|
_lines.Add(new TextLine());
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
var lines = text.Replace("\r\n", "\n").Replace("\r", "\n").Split('\n');
|
|
|
foreach (var line in lines)
|
|
|
{
|
|
|
var textLine = new TextLine();
|
|
|
foreach (var c in line)
|
|
|
{
|
|
|
textLine.Characters.Add(new TextCharacter(c));
|
|
|
}
|
|
|
_lines.Add(textLine);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
_cursorLine = 0;
|
|
|
_cursorColumn = 0;
|
|
|
ClearSelection();
|
|
|
_textChanged = true;
|
|
|
InvalidateVisual();
|
|
|
|
|
|
|
|
|
OnTextChanged();
|
|
|
}
|
|
|
|
|
|
public void SetText(JObject jsn, string md5)
|
|
|
{
|
|
|
_lines.Clear();
|
|
|
_undoBuffer.Clear();
|
|
|
_undoIndex = -1;
|
|
|
|
|
|
if (jsn == null)
|
|
|
{
|
|
|
SetText(string.Empty);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
int width = jsn.Value<int>("width");
|
|
|
int height = jsn.Value<int>("height");
|
|
|
|
|
|
var prism_wordsInfo = jsn["prism_wordsInfo"].Value<JArray>();
|
|
|
foreach (var item in prism_wordsInfo)
|
|
|
{
|
|
|
JArray charInfo = item["charInfo"].Value<JArray>();
|
|
|
string line_word = item["word"].Value<string>();
|
|
|
|
|
|
var textLine = new TextLine();
|
|
|
foreach (var item2 in charInfo)
|
|
|
{
|
|
|
string oneChar = item2["word"].Value<string>();
|
|
|
string guid = item2["guid"].Value<string>();
|
|
|
int isDeleted = item2["isDeleted"].Value<int>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (isDeleted == 1)
|
|
|
{
|
|
|
continue;
|
|
|
}
|
|
|
|
|
|
int x = item2["x"].Value<int>();
|
|
|
int y = item2["y"].Value<int>();
|
|
|
int w = item2["w"].Value<int>();
|
|
|
int h = item2["h"].Value<int>();
|
|
|
|
|
|
|
|
|
for (int i = 0; i < oneChar.Length;)
|
|
|
{
|
|
|
|
|
|
Rune rune = Rune.GetRuneAt(oneChar, i);
|
|
|
|
|
|
|
|
|
textLine.Characters.Add(new TextCharacter(rune, guid, x, y, w, h, md5));
|
|
|
|
|
|
|
|
|
i += rune.Utf16SequenceLength;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
_lines.Add(textLine);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
_cursorLine = 0;
|
|
|
_cursorColumn = 0;
|
|
|
ClearSelection();
|
|
|
_textChanged = true;
|
|
|
InvalidateVisual();
|
|
|
|
|
|
|
|
|
OnTextChanged();
|
|
|
}
|
|
|
|
|
|
|
|
|
public List<TextLine> get_text_lines(JObject jsn, string md5)
|
|
|
{
|
|
|
List<TextLine> lines = new List<TextLine>();
|
|
|
|
|
|
int width = jsn.Value<int>("width");
|
|
|
int height = jsn.Value<int>("height");
|
|
|
|
|
|
var prism_wordsInfo = jsn["prism_wordsInfo"].Value<JArray>();
|
|
|
foreach (var item in prism_wordsInfo)
|
|
|
{
|
|
|
JObject selected_rectange = new JObject();
|
|
|
if (_mode == EditorMode.ReviseEdit)
|
|
|
{
|
|
|
selected_rectange = item["selected_rectange"].Value<JObject>();
|
|
|
}
|
|
|
|
|
|
JArray pos = item["pos"].Value<JArray>();
|
|
|
JArray charInfo = item["charInfo"].Value<JArray>();
|
|
|
|
|
|
|
|
|
string line_word = item["word"].Value<string>();
|
|
|
|
|
|
var textLine = new TextLine(pos, selected_rectange);
|
|
|
foreach (var item2 in charInfo)
|
|
|
{
|
|
|
string oneChar = item2["word"].Value<string>();
|
|
|
string guid = item2["guid"].Value<string>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int x = item2["x"].Value<int>();
|
|
|
int y = item2["y"].Value<int>();
|
|
|
int w = item2["w"].Value<int>();
|
|
|
int h = item2["h"].Value<int>();
|
|
|
|
|
|
|
|
|
for (int i = 0; i < oneChar.Length;)
|
|
|
{
|
|
|
|
|
|
Rune rune = Rune.GetRuneAt(oneChar, i);
|
|
|
|
|
|
|
|
|
textLine.Characters.Add(new TextCharacter(rune, guid, x, y, w, h, md5));
|
|
|
|
|
|
|
|
|
i += rune.Utf16SequenceLength;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
lines.Add(textLine);
|
|
|
|
|
|
}
|
|
|
|
|
|
return lines;
|
|
|
}
|
|
|
|
|
|
public void InsertText(JObject jsn, string md5)
|
|
|
{
|
|
|
_undoBuffer.Clear();
|
|
|
_undoIndex = -1;
|
|
|
|
|
|
if (jsn == null)
|
|
|
{
|
|
|
SetText(string.Empty);
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var lines = get_text_lines(jsn, md5);
|
|
|
foreach(var line in lines)
|
|
|
{
|
|
|
_lines.Add(line);
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ClearSelection();
|
|
|
_textChanged = true;
|
|
|
InvalidateVisual();
|
|
|
|
|
|
|
|
|
OnTextChanged();
|
|
|
}
|
|
|
|
|
|
|
|
|
public bool saveJson(string pth_img_)
|
|
|
{
|
|
|
|
|
|
bool isModify = false;
|
|
|
|
|
|
string m5 = MD5.ComputeFileHash(pth_img_);
|
|
|
|
|
|
var (pth_img, pth_json) = Global.get_img_json_pthv2(pth_img_);
|
|
|
|
|
|
if (!Path.Exists(pth_json) || !Path.Exists(pth_img))
|
|
|
{
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
if (_mode == EditorMode.ReviseEdit)
|
|
|
{
|
|
|
Global.saveJsonReviseEdit(pth_json, _lines);
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
if (_mode == EditorMode.MenuSplit)
|
|
|
{
|
|
|
Global.saveJsonReviseEdit(pth_json, _lines);
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
|
|
|
var jsonstr = File.ReadAllText(pth_json, new System.Text.UTF8Encoding(false));
|
|
|
|
|
|
JObject jsn = null;
|
|
|
try
|
|
|
{
|
|
|
jsn = JObject.Parse(jsonstr);
|
|
|
}
|
|
|
catch (Exception ex)
|
|
|
{
|
|
|
MessageBox.Show("json 解析失败", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
int width = jsn.Value<int>("width");
|
|
|
int height = jsn.Value<int>("height");
|
|
|
|
|
|
var prism_wordsInfo = jsn["prism_wordsInfo"].Value<JArray>();
|
|
|
foreach (var item in prism_wordsInfo)
|
|
|
{
|
|
|
JArray charInfo = item["charInfo"].Value<JArray>();
|
|
|
string line_word = item["word"].Value<string>();
|
|
|
|
|
|
var textLine = new TextLine();
|
|
|
foreach (var item2 in charInfo)
|
|
|
{
|
|
|
string oneChar = item2["word"].Value<string>();
|
|
|
string guid = item2["guid"].Value<string>();
|
|
|
int x = item2["x"].Value<int>();
|
|
|
int y = item2["y"].Value<int>();
|
|
|
int w = item2["w"].Value<int>();
|
|
|
int h = item2["h"].Value<int>();
|
|
|
|
|
|
|
|
|
for (int i = 0; i < oneChar.Length;)
|
|
|
{
|
|
|
Rune rune = Rune.GetRuneAt(oneChar, i);
|
|
|
|
|
|
|
|
|
i += rune.Utf16SequenceLength;
|
|
|
}
|
|
|
|
|
|
if ( _dic_deleted_char.ContainsKey(guid) ) {
|
|
|
|
|
|
item2["isDeleted"] = 1;
|
|
|
isModify = true;
|
|
|
}
|
|
|
|
|
|
foreach (TextLine line in _lines)
|
|
|
{
|
|
|
bool line_modify = false;
|
|
|
foreach (TextCharacter c in line.Characters)
|
|
|
{
|
|
|
string ImageMD5 = c.ImageMD5;
|
|
|
if (ImageMD5 == m5)
|
|
|
{
|
|
|
if (guid == c.guid)
|
|
|
{
|
|
|
string c_str = c.Character.ToString();
|
|
|
if (c_str != oneChar)
|
|
|
{
|
|
|
item2["word"] = c_str;
|
|
|
line_modify = true;
|
|
|
isModify = true;
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
if (line_modify)
|
|
|
{
|
|
|
string new_line_word = string.Concat(line.Characters.Select(c => c.Character.ToString()));
|
|
|
item["word"] = new_line_word;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
if (isModify)
|
|
|
{
|
|
|
string jsn_str = jsn.ToString(Formatting.Indented).Replace("\r\n", "\n");
|
|
|
File.WriteAllText(pth_json, jsn_str);
|
|
|
}
|
|
|
|
|
|
return isModify;
|
|
|
}
|
|
|
|
|
|
private void RenderEditor(DrawingContext drawingContext)
|
|
|
{
|
|
|
|
|
|
drawingContext.DrawRectangle(Brushes.White, null, new Rect(0, 0, ActualWidth, ActualHeight));
|
|
|
|
|
|
|
|
|
var typeface = new Typeface(FontFamily, FontStyle, FontWeight, FontStretch);
|
|
|
double lineHeight = FontSize * 1.2;
|
|
|
double defaultCharWidth = FontSize * 0.6;
|
|
|
|
|
|
|
|
|
double lineNumberWidth = LineNumbersVisible ? 40 : 0;
|
|
|
|
|
|
|
|
|
if (LineNumbersVisible)
|
|
|
{
|
|
|
drawingContext.DrawRectangle(Brushes.LightGray, null, new Rect(0, 0, lineNumberWidth, ActualHeight));
|
|
|
}
|
|
|
|
|
|
double textStartX = lineNumberWidth + 5;
|
|
|
|
|
|
|
|
|
int visibleLineCount = (int)Math.Ceiling(ActualHeight / lineHeight);
|
|
|
int startLine = _scrollOffset;
|
|
|
int endLine = Math.Min(_scrollOffset + visibleLineCount, _lines.Count - 1);
|
|
|
|
|
|
|
|
|
if (HighlightCurrentLine && _cursorLine >= startLine && _cursorLine <= endLine)
|
|
|
{
|
|
|
drawingContext.DrawRectangle(Brushes.LightYellow, null,
|
|
|
new Rect(lineNumberWidth, (_cursorLine - _scrollOffset) * lineHeight, ActualWidth - lineNumberWidth, lineHeight));
|
|
|
}
|
|
|
|
|
|
|
|
|
for (int i = startLine; i <= endLine; i++)
|
|
|
{
|
|
|
double y = (i - _scrollOffset) * lineHeight;
|
|
|
|
|
|
|
|
|
if (LineNumbersVisible)
|
|
|
{
|
|
|
var lineNumberText = new FormattedText(
|
|
|
(i + 1).ToString(),
|
|
|
System.Globalization.CultureInfo.CurrentCulture,
|
|
|
FlowDirection.LeftToRight,
|
|
|
typeface,
|
|
|
FontSize,
|
|
|
Brushes.Gray,
|
|
|
VisualTreeHelper.GetDpi(this).PixelsPerDip);
|
|
|
|
|
|
drawingContext.DrawText(lineNumberText, new Point(5, y));
|
|
|
}
|
|
|
|
|
|
|
|
|
var line = _lines[i];
|
|
|
double x = textStartX;
|
|
|
|
|
|
for (int j = 0; j < line.Characters.Count; j++)
|
|
|
{
|
|
|
var character = line.Characters[j];
|
|
|
|
|
|
|
|
|
double charWidth = IsFullWidthCharacter(character.Character) ? defaultCharWidth * 2 : defaultCharWidth;
|
|
|
|
|
|
if (HasSelection() && IsPositionSelected(i, j))
|
|
|
{
|
|
|
drawingContext.DrawRectangle(Brushes.LightBlue, null, new Rect(x, y, charWidth, lineHeight));
|
|
|
}
|
|
|
|
|
|
|
|
|
var formattedText = new FormattedText(
|
|
|
character.Character.ToString(),
|
|
|
System.Globalization.CultureInfo.CurrentCulture,
|
|
|
FlowDirection.LeftToRight,
|
|
|
typeface,
|
|
|
FontSize,
|
|
|
character.Foreground,
|
|
|
VisualTreeHelper.GetDpi(this).PixelsPerDip);
|
|
|
|
|
|
drawingContext.DrawText(formattedText, new Point(x, y));
|
|
|
|
|
|
|
|
|
if (ShowWhitespaces && character.Character.Value == ' ')
|
|
|
{
|
|
|
drawingContext.DrawLine(
|
|
|
new Pen(Brushes.LightGray, 1),
|
|
|
new Point(x + charWidth / 2, y + lineHeight / 2),
|
|
|
new Point(x + charWidth / 2, y + lineHeight / 2));
|
|
|
}
|
|
|
|
|
|
x += charWidth;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
if (IsFocused && _cursorLine >= startLine && _cursorLine <= endLine)
|
|
|
{
|
|
|
|
|
|
double cursorX = textStartX;
|
|
|
var line = _lines[_cursorLine];
|
|
|
for (int j = 0; j < _cursorColumn && j < line.Characters.Count; j++)
|
|
|
{
|
|
|
cursorX += IsFullWidthCharacter(line.Characters[j].Character) ? defaultCharWidth * 2 : defaultCharWidth;
|
|
|
}
|
|
|
|
|
|
double cursorY = (_cursorLine - _scrollOffset) * lineHeight;
|
|
|
|
|
|
drawingContext.DrawLine(
|
|
|
new Pen(Brushes.Black, 1),
|
|
|
new Point(cursorX, cursorY),
|
|
|
new Point(cursorX, cursorY + lineHeight));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
private bool IsFullWidthCharacter(System.Text.Rune r)
|
|
|
{
|
|
|
int value = r.Value;
|
|
|
|
|
|
if (value >= 0x4E00 && value <= 0x9FFF) return true;
|
|
|
if (value >= 0x3400 && value <= 0x4DBF) return true;
|
|
|
if (value >= 0x20000 && value <= 0x2A6DF) return true;
|
|
|
if (value >= 0x2A700 && value <= 0x2B73F) return true;
|
|
|
if (value >= 0x2B740 && value <= 0x2B81F) return true;
|
|
|
if (value >= 0x2B820 && value <= 0x2CEAF) return true;
|
|
|
if (value >= 0xF900 && value <= 0xFAFF) return true;
|
|
|
|
|
|
|
|
|
if (value >= 0x3040 && value <= 0x309F) return true;
|
|
|
if (value >= 0x30A0 && value <= 0x30FF) return true;
|
|
|
|
|
|
|
|
|
if (value >= 0xFF00 && value <= 0xFFEF) return true;
|
|
|
|
|
|
|
|
|
if (value >= 0x3000 && value <= 0x303F) return true;
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
private bool IsPositionSelected(int line, int column)
|
|
|
{
|
|
|
if (!HasSelection())
|
|
|
return false;
|
|
|
|
|
|
int startLine = Math.Min(_selectionStartLine, _selectionEndLine);
|
|
|
int startColumn = startLine == _selectionStartLine ? _selectionStartColumn : _selectionEndColumn;
|
|
|
int endLine = Math.Max(_selectionStartLine, _selectionEndLine);
|
|
|
int endColumn = endLine == _selectionEndLine ? _selectionEndColumn : _selectionStartColumn;
|
|
|
|
|
|
if (line < startLine || line > endLine)
|
|
|
return false;
|
|
|
|
|
|
if (line == startLine && line == endLine)
|
|
|
return column >= startColumn && column < endColumn;
|
|
|
|
|
|
if (line == startLine)
|
|
|
return column >= startColumn;
|
|
|
|
|
|
if (line == endLine)
|
|
|
return column < endColumn;
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
|
|
|
private bool IsPositionInSelection(Point position)
|
|
|
{
|
|
|
if (!HasSelection())
|
|
|
return false;
|
|
|
|
|
|
|
|
|
var typeface = new Typeface(FontFamily, FontStyle, FontWeight, FontStretch);
|
|
|
var formattedText = new FormattedText("M", System.Globalization.CultureInfo.CurrentCulture,
|
|
|
FlowDirection.LeftToRight, typeface, FontSize, Brushes.Black, VisualTreeHelper.GetDpi(this).PixelsPerDip);
|
|
|
double charWidth = formattedText.Width;
|
|
|
double lineHeight = formattedText.Height;
|
|
|
|
|
|
|
|
|
int clickLine = Math.Max(0, Math.Min((int)(position.Y / lineHeight) + _scrollOffset, _lines.Count - 1));
|
|
|
int clickColumn = Math.Max(0, (int)((position.X - 50) / charWidth));
|
|
|
|
|
|
|
|
|
int startLine = Math.Min(_selectionStartLine, _selectionEndLine);
|
|
|
int startColumn = startLine == _selectionStartLine ? _selectionStartColumn : _selectionEndColumn;
|
|
|
int endLine = Math.Max(_selectionStartLine, _selectionEndLine);
|
|
|
int endColumn = endLine == _selectionEndLine ? _selectionEndColumn : _selectionStartColumn;
|
|
|
|
|
|
|
|
|
if (clickLine < startLine || clickLine > endLine)
|
|
|
return false;
|
|
|
|
|
|
if (clickLine == startLine && clickLine == endLine)
|
|
|
{
|
|
|
return clickColumn >= startColumn && clickColumn <= endColumn;
|
|
|
}
|
|
|
else if (clickLine == startLine)
|
|
|
{
|
|
|
return clickColumn >= startColumn;
|
|
|
}
|
|
|
else if (clickLine == endLine)
|
|
|
{
|
|
|
return clickColumn <= endColumn;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
private void SetCursorPositionFromPoint(Point point)
|
|
|
{
|
|
|
if (_lines.Count <= 0)
|
|
|
{
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
|
|
|
double lineHeight = FontSize * 1.2;
|
|
|
double defaultCharWidth = FontSize * 0.6;
|
|
|
|
|
|
|
|
|
double lineNumberWidth = LineNumbersVisible ? 40 : 0;
|
|
|
double textStartX = lineNumberWidth + 5;
|
|
|
|
|
|
|
|
|
int line = (int)(point.Y / lineHeight) + _scrollOffset;
|
|
|
line = Math.Max(0, Math.Min(line, _lines.Count - 1));
|
|
|
|
|
|
|
|
|
double x = textStartX;
|
|
|
int column = 0;
|
|
|
var textLine = _lines[line];
|
|
|
|
|
|
|
|
|
while (column < textLine.Characters.Count && x < point.X)
|
|
|
{
|
|
|
x += IsFullWidthCharacter(textLine.Characters[column].Character) ? defaultCharWidth * 2 : defaultCharWidth;
|
|
|
if (x > point.X)
|
|
|
{
|
|
|
|
|
|
|
|
|
double charWidth = IsFullWidthCharacter(textLine.Characters[column].Character) ? defaultCharWidth * 2 : defaultCharWidth;
|
|
|
if (point.X > x - charWidth / 2)
|
|
|
column++;
|
|
|
break;
|
|
|
}
|
|
|
column++;
|
|
|
}
|
|
|
|
|
|
|
|
|
_cursorLine = line;
|
|
|
_cursorColumn = column;
|
|
|
|
|
|
|
|
|
OnCursorPositionChanged(_cursorLine, _cursorColumn);
|
|
|
}
|
|
|
private void HandleKeyDown(KeyEventArgs e)
|
|
|
{
|
|
|
if (IsReadOnly)
|
|
|
{
|
|
|
e.Handled = true;
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
|
|
|
if (Keyboard.Modifiers == ModifierKeys.Control)
|
|
|
{
|
|
|
switch (e.Key)
|
|
|
{
|
|
|
case Key.F:
|
|
|
ShowFindDialog();
|
|
|
e.Handled = true;
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
switch (e.Key)
|
|
|
{
|
|
|
case Key.Left:
|
|
|
if (_cursorColumn > 0)
|
|
|
{
|
|
|
_cursorColumn--;
|
|
|
showCurrChar();
|
|
|
}
|
|
|
else if (_cursorLine > 0)
|
|
|
{
|
|
|
_cursorLine--;
|
|
|
_cursorColumn = _lines[_cursorLine].Characters.Count;
|
|
|
showCurrChar();
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
case Key.Right:
|
|
|
if (_cursorColumn < _lines[_cursorLine].Characters.Count)
|
|
|
{
|
|
|
_cursorColumn++;
|
|
|
showCurrChar();
|
|
|
}
|
|
|
else if (_cursorLine < _lines.Count - 1)
|
|
|
{
|
|
|
_cursorLine++;
|
|
|
_cursorColumn = 0;
|
|
|
showCurrChar();
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
case Key.Up:
|
|
|
if (_cursorLine > 0)
|
|
|
{
|
|
|
_cursorLine--;
|
|
|
_cursorColumn = Math.Min(_cursorColumn, _lines[_cursorLine].Characters.Count);
|
|
|
|
|
|
|
|
|
if (_cursorLine < _scrollOffset)
|
|
|
{
|
|
|
_scrollOffset--;
|
|
|
}
|
|
|
showCurrChar();
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
case Key.Down:
|
|
|
if (_cursorLine < _lines.Count - 1)
|
|
|
{
|
|
|
_cursorLine++;
|
|
|
_cursorColumn = Math.Min(_cursorColumn, _lines[_cursorLine].Characters.Count);
|
|
|
|
|
|
|
|
|
double lineHeight = FontSize * 1.2;
|
|
|
int visibleLineCount = (int)(ActualHeight / lineHeight);
|
|
|
|
|
|
|
|
|
if (_cursorLine >= _scrollOffset + visibleLineCount)
|
|
|
{
|
|
|
_scrollOffset++;
|
|
|
}
|
|
|
showCurrChar();
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
case Key.Home:
|
|
|
_cursorColumn = 0;
|
|
|
break;
|
|
|
|
|
|
case Key.End:
|
|
|
_cursorColumn = _lines[_cursorLine].Characters.Count;
|
|
|
break;
|
|
|
|
|
|
case Key.Delete:
|
|
|
if (HasSelection())
|
|
|
{
|
|
|
DeleteSelection();
|
|
|
}
|
|
|
else if (_cursorColumn < _lines[_cursorLine].Characters.Count)
|
|
|
{
|
|
|
|
|
|
TextCharacter c = _lines[_cursorLine].Characters[_cursorColumn];
|
|
|
if (c.guid != "")
|
|
|
{
|
|
|
_dic_deleted_char[c.guid] = c.Character.Value.ToString();
|
|
|
}
|
|
|
_lines[_cursorLine].Characters.RemoveAt(_cursorColumn);
|
|
|
_textChanged = true;
|
|
|
OnTextChanged();
|
|
|
}
|
|
|
else if (_cursorLine < _lines.Count - 1)
|
|
|
{
|
|
|
|
|
|
var nextLine = _lines[_cursorLine + 1];
|
|
|
_lines[_cursorLine].Characters.AddRange(nextLine.Characters);
|
|
|
_lines.RemoveAt(_cursorLine + 1);
|
|
|
_textChanged = true;
|
|
|
OnTextChanged();
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
case Key.Back:
|
|
|
if (HasSelection())
|
|
|
{
|
|
|
DeleteSelection();
|
|
|
}
|
|
|
else if (_cursorColumn > 0)
|
|
|
{
|
|
|
|
|
|
_cursorColumn--;
|
|
|
TextCharacter c = _lines[_cursorLine].Characters[_cursorColumn];
|
|
|
if (c.guid != "")
|
|
|
{
|
|
|
_dic_deleted_char[c.guid] = c.Character.Value.ToString();
|
|
|
}
|
|
|
_lines[_cursorLine].Characters.RemoveAt(_cursorColumn);
|
|
|
_textChanged = true;
|
|
|
OnTextChanged();
|
|
|
}
|
|
|
else if (_cursorLine > 0)
|
|
|
{
|
|
|
|
|
|
_cursorColumn = _lines[_cursorLine - 1].Characters.Count;
|
|
|
_lines[_cursorLine - 1].Characters.AddRange(_lines[_cursorLine].Characters);
|
|
|
_lines.RemoveAt(_cursorLine);
|
|
|
_cursorLine--;
|
|
|
_textChanged = true;
|
|
|
OnTextChanged();
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
case Key.Enter:
|
|
|
|
|
|
var currentLine = _lines[_cursorLine];
|
|
|
var newLine = new TextLine();
|
|
|
|
|
|
|
|
|
if (_cursorColumn < currentLine.Characters.Count)
|
|
|
{
|
|
|
var remainingChars = currentLine.Characters.GetRange(_cursorColumn, currentLine.Characters.Count - _cursorColumn);
|
|
|
newLine.Characters.AddRange(remainingChars);
|
|
|
currentLine.Characters.RemoveRange(_cursorColumn, remainingChars.Count);
|
|
|
}
|
|
|
|
|
|
_lines.Insert(_cursorLine + 1, newLine);
|
|
|
_cursorLine++;
|
|
|
_cursorColumn = 0;
|
|
|
_textChanged = true;
|
|
|
OnTextChanged();
|
|
|
break;
|
|
|
|
|
|
case Key.Tab:
|
|
|
|
|
|
for (int i = 0; i < TabSize; i++)
|
|
|
{
|
|
|
InsertText(" ");
|
|
|
}
|
|
|
break;
|
|
|
|
|
|
default:
|
|
|
e.Handled = false;
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
|
|
|
if (Keyboard.Modifiers == ModifierKeys.Shift)
|
|
|
{
|
|
|
UpdateSelection();
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
ClearSelection();
|
|
|
}
|
|
|
|
|
|
|
|
|
OnCursorPositionChanged(_cursorLine, _cursorColumn);
|
|
|
|
|
|
|
|
|
InvalidateVisual();
|
|
|
|
|
|
e.Handled = true;
|
|
|
}
|
|
|
|
|
|
private void DeleteSelection()
|
|
|
{
|
|
|
if (!HasSelection())
|
|
|
return;
|
|
|
|
|
|
|
|
|
int startLine = Math.Min(_selectionStartLine, _selectionEndLine);
|
|
|
int startColumn = startLine == _selectionStartLine ? _selectionStartColumn : _selectionEndColumn;
|
|
|
int endLine = Math.Max(_selectionStartLine, _selectionEndLine);
|
|
|
int endColumn = endLine == _selectionEndLine ? _selectionEndColumn : _selectionStartColumn;
|
|
|
|
|
|
if (startLine == endLine)
|
|
|
{
|
|
|
|
|
|
int start = Math.Min(startColumn, endColumn);
|
|
|
int end = Math.Max(startColumn, endColumn);
|
|
|
var line = _lines[startLine];
|
|
|
if (start < line.Characters.Count)
|
|
|
{
|
|
|
int length = Math.Min(end - start, line.Characters.Count - start);
|
|
|
line.Characters.RemoveRange(start, length);
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
|
|
|
|
|
|
var firstLine = _lines[startLine];
|
|
|
if (startColumn < firstLine.Characters.Count)
|
|
|
{
|
|
|
firstLine.Characters.RemoveRange(startColumn, firstLine.Characters.Count - startColumn);
|
|
|
}
|
|
|
|
|
|
|
|
|
var lastLine = _lines[endLine];
|
|
|
List<TextCharacter> lastLineRemaining = new List<TextCharacter>();
|
|
|
if (endColumn < lastLine.Characters.Count)
|
|
|
{
|
|
|
lastLineRemaining = lastLine.Characters.GetRange(endColumn, lastLine.Characters.Count - endColumn);
|
|
|
}
|
|
|
|
|
|
|
|
|
_lines.RemoveRange(startLine + 1, endLine - startLine);
|
|
|
|
|
|
|
|
|
firstLine.Characters.AddRange(lastLineRemaining);
|
|
|
}
|
|
|
|
|
|
|
|
|
_cursorLine = startLine;
|
|
|
_cursorColumn = startColumn;
|
|
|
|
|
|
|
|
|
ClearSelection();
|
|
|
|
|
|
|
|
|
_textChanged = true;
|
|
|
|
|
|
|
|
|
OnTextChanged();
|
|
|
|
|
|
|
|
|
OnCursorPositionChanged(_cursorLine, _cursorColumn);
|
|
|
}
|
|
|
|
|
|
public string GetSelectedText()
|
|
|
{
|
|
|
if (!HasSelection())
|
|
|
return string.Empty;
|
|
|
|
|
|
|
|
|
int startLine = Math.Min(_selectionStartLine, _selectionEndLine);
|
|
|
int startColumn = startLine == _selectionStartLine ? _selectionStartColumn : _selectionEndColumn;
|
|
|
int endLine = Math.Max(_selectionStartLine, _selectionEndLine);
|
|
|
int endColumn = endLine == _selectionEndLine ? _selectionEndColumn : _selectionStartColumn;
|
|
|
|
|
|
if (startLine == endLine)
|
|
|
{
|
|
|
|
|
|
int start = Math.Min(startColumn, endColumn);
|
|
|
int end = Math.Max(startColumn, endColumn);
|
|
|
var line = _lines[startLine];
|
|
|
if (start < line.Characters.Count)
|
|
|
{
|
|
|
int length = Math.Min(end - start, line.Characters.Count - start);
|
|
|
return string.Concat(line.Characters.GetRange(start, length).Select(c => c.Character.ToString()));
|
|
|
}
|
|
|
return string.Empty;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
|
|
|
|
var firstLine = _lines[startLine];
|
|
|
if (startColumn < firstLine.Characters.Count)
|
|
|
{
|
|
|
sb.Append(string.Concat(firstLine.Characters.GetRange(startColumn, firstLine.Characters.Count - startColumn).Select(c => c.Character.ToString())));
|
|
|
}
|
|
|
sb.Append("\n");
|
|
|
|
|
|
|
|
|
for (int i = startLine + 1; i < endLine; i++)
|
|
|
{
|
|
|
sb.Append(string.Concat(_lines[i].Characters.Select(c => c.Character.ToString())));
|
|
|
sb.Append("\n");
|
|
|
}
|
|
|
|
|
|
|
|
|
var lastLine = _lines[endLine];
|
|
|
if (endColumn > 0 && lastLine.Characters.Count > 0)
|
|
|
{
|
|
|
sb.Append(string.Concat(lastLine.Characters.GetRange(0, Math.Min(endColumn, lastLine.Characters.Count)).Select(c => c.Character.ToString())));
|
|
|
}
|
|
|
|
|
|
return sb.ToString();
|
|
|
}
|
|
|
}
|
|
|
|
|
|
private List<TextCharacter> GetSelectedTextCharacters()
|
|
|
{
|
|
|
if (!HasSelection())
|
|
|
return new List<TextCharacter>();
|
|
|
|
|
|
|
|
|
int startLine = Math.Min(_selectionStartLine, _selectionEndLine);
|
|
|
int startColumn = startLine == _selectionStartLine ? _selectionStartColumn : _selectionEndColumn;
|
|
|
int endLine = Math.Max(_selectionStartLine, _selectionEndLine);
|
|
|
int endColumn = endLine == _selectionEndLine ? _selectionEndColumn : _selectionStartColumn;
|
|
|
|
|
|
List<TextCharacter> result = new List<TextCharacter>();
|
|
|
|
|
|
if (startLine == endLine)
|
|
|
{
|
|
|
|
|
|
int start = Math.Min(startColumn, endColumn);
|
|
|
int end = Math.Max(startColumn, endColumn);
|
|
|
var line = _lines[startLine];
|
|
|
if (start < line.Characters.Count)
|
|
|
{
|
|
|
int length = Math.Min(end - start, line.Characters.Count - start);
|
|
|
result.AddRange(line.Characters.GetRange(start, length));
|
|
|
}
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
|
|
|
|
|
|
var firstLine = _lines[startLine];
|
|
|
if (startColumn < firstLine.Characters.Count)
|
|
|
{
|
|
|
result.AddRange(firstLine.Characters.GetRange(startColumn, firstLine.Characters.Count - startColumn));
|
|
|
}
|
|
|
|
|
|
|
|
|
for (int i = startLine + 1; i < endLine; i++)
|
|
|
{
|
|
|
result.AddRange(_lines[i].Characters);
|
|
|
}
|
|
|
|
|
|
|
|
|
var lastLine = _lines[endLine];
|
|
|
if (endColumn > 0 && lastLine.Characters.Count > 0)
|
|
|
{
|
|
|
result.AddRange(lastLine.Characters.GetRange(0, Math.Min(endColumn, lastLine.Characters.Count)));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
private bool HasSelection()
|
|
|
{
|
|
|
|
|
|
return _selectionStartLine >= 0 && _selectionEndLine >= 0;
|
|
|
}
|
|
|
|
|
|
private bool IsSingleSelection()
|
|
|
{
|
|
|
if (_selectionStartColumn >= 0 && _selectionEndColumn >= 0)
|
|
|
{
|
|
|
return _selectionStartColumn + 1 == _selectionEndColumn;
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
public void ClearSelection()
|
|
|
{
|
|
|
|
|
|
_selectionStartLine = -1;
|
|
|
_selectionStartColumn = -1;
|
|
|
_selectionEndLine = -1;
|
|
|
_selectionEndColumn = -1;
|
|
|
}
|
|
|
|
|
|
private void UpdateSelection()
|
|
|
{
|
|
|
|
|
|
if (_selectionStartLine < 0)
|
|
|
{
|
|
|
_selectionStartLine = _cursorLine;
|
|
|
_selectionStartColumn = _cursorColumn;
|
|
|
}
|
|
|
_selectionEndLine = _cursorLine;
|
|
|
_selectionEndColumn = _cursorColumn;
|
|
|
}
|
|
|
|
|
|
private void SelectAll()
|
|
|
{
|
|
|
|
|
|
_selectionStartLine = 0;
|
|
|
_selectionStartColumn = 0;
|
|
|
_selectionEndLine = _lines.Count - 1;
|
|
|
_selectionEndColumn = _lines[_selectionEndLine].Characters.Count;
|
|
|
}
|
|
|
|
|
|
private bool CanUndo()
|
|
|
{
|
|
|
|
|
|
return _undoIndex >= 0;
|
|
|
}
|
|
|
|
|
|
private bool CanRedo()
|
|
|
{
|
|
|
|
|
|
return _undoIndex < _undoBuffer.Count - 1;
|
|
|
}
|
|
|
|
|
|
private void Undo()
|
|
|
{
|
|
|
if (!CanUndo())
|
|
|
return;
|
|
|
|
|
|
var action = _undoBuffer[_undoIndex];
|
|
|
|
|
|
|
|
|
_cursorLine = action.CursorBeforeLine;
|
|
|
_cursorColumn = action.CursorBeforeColumn;
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(action.AddedText))
|
|
|
{
|
|
|
|
|
|
_selectionStartLine = action.AddedStartLine;
|
|
|
_selectionStartColumn = action.AddedStartColumn;
|
|
|
_selectionEndLine = action.AddedEndLine;
|
|
|
_selectionEndColumn = action.AddedEndColumn;
|
|
|
|
|
|
|
|
|
DeleteSelection();
|
|
|
}
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(action.RemovedText))
|
|
|
{
|
|
|
|
|
|
_cursorLine = action.RemovedStartLine;
|
|
|
_cursorColumn = action.RemovedStartColumn;
|
|
|
|
|
|
|
|
|
InsertText(action.RemovedText);
|
|
|
|
|
|
|
|
|
_cursorLine = action.CursorBeforeLine;
|
|
|
_cursorColumn = action.CursorBeforeColumn;
|
|
|
}
|
|
|
|
|
|
|
|
|
ClearSelection();
|
|
|
|
|
|
|
|
|
_undoIndex--;
|
|
|
|
|
|
|
|
|
OnCursorPositionChanged(_cursorLine, _cursorColumn);
|
|
|
}
|
|
|
|
|
|
private void Redo()
|
|
|
{
|
|
|
if (!CanRedo())
|
|
|
return;
|
|
|
|
|
|
|
|
|
_undoIndex++;
|
|
|
|
|
|
var action = _undoBuffer[_undoIndex];
|
|
|
|
|
|
|
|
|
_cursorLine = action.CursorBeforeLine;
|
|
|
_cursorColumn = action.CursorBeforeColumn;
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(action.RemovedText))
|
|
|
{
|
|
|
|
|
|
_selectionStartLine = action.RemovedStartLine;
|
|
|
_selectionStartColumn = action.RemovedStartColumn;
|
|
|
_selectionEndLine = action.RemovedEndLine;
|
|
|
_selectionEndColumn = action.RemovedEndColumn;
|
|
|
|
|
|
|
|
|
DeleteSelection();
|
|
|
}
|
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(action.AddedText))
|
|
|
{
|
|
|
|
|
|
_cursorLine = action.AddedStartLine;
|
|
|
_cursorColumn = action.AddedStartColumn;
|
|
|
|
|
|
|
|
|
InsertText(action.AddedText);
|
|
|
}
|
|
|
|
|
|
|
|
|
_cursorLine = action.CursorAfterLine;
|
|
|
_cursorColumn = action.CursorAfterColumn;
|
|
|
|
|
|
|
|
|
ClearSelection();
|
|
|
|
|
|
|
|
|
OnCursorPositionChanged(_cursorLine, _cursorColumn);
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
public event EventHandler TextChanged;
|
|
|
public event EventHandler<CursorPositionChangedEventArgs> CursorPositionChanged;
|
|
|
|
|
|
public virtual void OnTextChanged()
|
|
|
{
|
|
|
|
|
|
UpdateTextProperty();
|
|
|
|
|
|
TextChanged?.Invoke(this, EventArgs.Empty);
|
|
|
}
|
|
|
|
|
|
private void UpdateTextProperty()
|
|
|
{
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
|
for (int i = 0; i < _lines.Count; i++)
|
|
|
{
|
|
|
sb.Append(string.Concat(_lines[i].Characters.Select(c => c.Character.ToString())));
|
|
|
|
|
|
|
|
|
if (i < _lines.Count - 1)
|
|
|
sb.Append("\n");
|
|
|
}
|
|
|
|
|
|
|
|
|
int savedCursorLine = _cursorLine;
|
|
|
int savedCursorColumn = _cursorColumn;
|
|
|
bool textChangedFlag = _textChanged;
|
|
|
|
|
|
|
|
|
SetValue(TextProperty, sb.ToString());
|
|
|
|
|
|
|
|
|
_cursorLine = savedCursorLine;
|
|
|
_cursorColumn = savedCursorColumn;
|
|
|
_textChanged = textChangedFlag;
|
|
|
}
|
|
|
|
|
|
public virtual void OnCursorPositionChanged(int line, int column)
|
|
|
{
|
|
|
CursorPositionChanged?.Invoke(this, new CursorPositionChangedEventArgs(line, column));
|
|
|
}
|
|
|
|
|
|
|
|
|
private TextCharacter GetCharacterAfterCursor()
|
|
|
{
|
|
|
if (_cursorLine >= 0 && _cursorLine < _lines.Count)
|
|
|
{
|
|
|
var line = _lines[_cursorLine];
|
|
|
if (_cursorColumn >= 0 && _cursorColumn < line.Characters.Count)
|
|
|
{
|
|
|
|
|
|
return line.Characters[_cursorColumn];
|
|
|
}
|
|
|
else if (_cursorColumn == line.Characters.Count && _cursorLine + 1 < _lines.Count)
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
private TextCharacter GetCharacterBeforeCursor()
|
|
|
{
|
|
|
if (_cursorLine >= 0 && _cursorLine < _lines.Count)
|
|
|
{
|
|
|
var line = _lines[_cursorLine];
|
|
|
if (_cursorColumn > 0 && _cursorColumn <= line.Characters.Count)
|
|
|
{
|
|
|
|
|
|
return line.Characters[_cursorColumn - 1];
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
public void InsertText(string text)
|
|
|
{
|
|
|
if (string.IsNullOrEmpty(text))
|
|
|
return;
|
|
|
|
|
|
|
|
|
if (HasSelection())
|
|
|
{
|
|
|
if ( IsSingleSelection() )
|
|
|
{
|
|
|
if (text.Length == 1)
|
|
|
{
|
|
|
|
|
|
var line = _lines[_cursorLine];
|
|
|
TextCharacter c = line.Characters[_cursorColumn-1];
|
|
|
c.Character = System.Text.Rune.GetRuneAt(text, 0);
|
|
|
|
|
|
|
|
|
ClearSelection();
|
|
|
|
|
|
_cursorColumn = _cursorColumn - 1;
|
|
|
|
|
|
OnCursorPositionChanged(_cursorLine, _cursorColumn);
|
|
|
|
|
|
|
|
|
InvalidateVisual();
|
|
|
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
DeleteSelection();
|
|
|
}
|
|
|
|
|
|
|
|
|
var undoAction = new UndoAction
|
|
|
{
|
|
|
CursorBeforeLine = _cursorLine,
|
|
|
CursorBeforeColumn = _cursorColumn
|
|
|
};
|
|
|
|
|
|
|
|
|
var lines = text.Replace("\r\n", "\n").Replace("\r", "\n").Split('\n');
|
|
|
|
|
|
if (lines.Length == 1)
|
|
|
{
|
|
|
|
|
|
var line = _lines[_cursorLine];
|
|
|
var chars = lines[0].EnumerateRunes().Select(r => new TextCharacter(r)).ToList();
|
|
|
line.Characters.InsertRange(_cursorColumn, chars);
|
|
|
_cursorColumn += chars.Count;
|
|
|
|
|
|
|
|
|
undoAction.AddedText = text;
|
|
|
undoAction.AddedStartLine = _cursorLine;
|
|
|
undoAction.AddedStartColumn = _cursorColumn - chars.Count;
|
|
|
undoAction.AddedEndLine = _cursorLine;
|
|
|
undoAction.AddedEndColumn = _cursorColumn;
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
|
|
|
var currentLine = _lines[_cursorLine];
|
|
|
var remainingChars = currentLine.Characters.GetRange(_cursorColumn, currentLine.Characters.Count - _cursorColumn).ToList();
|
|
|
currentLine.Characters.RemoveRange(_cursorColumn, remainingChars.Count);
|
|
|
|
|
|
|
|
|
var firstLineChars = lines[0].EnumerateRunes().Select(r => new TextCharacter(r)).ToList();
|
|
|
currentLine.Characters.AddRange(firstLineChars);
|
|
|
|
|
|
|
|
|
for (int i = 1; i < lines.Length - 1; i++)
|
|
|
{
|
|
|
var newLine = new TextLine();
|
|
|
newLine.Characters.AddRange(lines[i].EnumerateRunes().Select(r => new TextCharacter(r)));
|
|
|
_lines.Insert(_cursorLine + i, newLine);
|
|
|
}
|
|
|
|
|
|
|
|
|
var lastLine = new TextLine();
|
|
|
lastLine.Characters.AddRange(lines[lines.Length - 1].EnumerateRunes().Select(r => new TextCharacter(r)));
|
|
|
lastLine.Characters.AddRange(remainingChars);
|
|
|
_lines.Insert(_cursorLine + lines.Length - 1, lastLine);
|
|
|
|
|
|
|
|
|
_cursorLine += lines.Length - 1;
|
|
|
_cursorColumn = lastLine.Characters.Count - remainingChars.Count;
|
|
|
|
|
|
|
|
|
undoAction.AddedText = text;
|
|
|
undoAction.AddedStartLine = _cursorLine - (lines.Length - 1);
|
|
|
undoAction.AddedStartColumn = _cursorColumn - firstLineChars.Count;
|
|
|
undoAction.AddedEndLine = _cursorLine;
|
|
|
undoAction.AddedEndColumn = _cursorColumn;
|
|
|
}
|
|
|
|
|
|
|
|
|
undoAction.CursorAfterLine = _cursorLine;
|
|
|
undoAction.CursorAfterColumn = _cursorColumn;
|
|
|
|
|
|
|
|
|
if (_undoIndex < _undoBuffer.Count - 1)
|
|
|
{
|
|
|
_undoBuffer.RemoveRange(_undoIndex + 1, _undoBuffer.Count - _undoIndex - 1);
|
|
|
}
|
|
|
_undoBuffer.Add(undoAction);
|
|
|
_undoIndex = _undoBuffer.Count - 1;
|
|
|
|
|
|
|
|
|
_textChanged = true;
|
|
|
|
|
|
|
|
|
OnTextChanged();
|
|
|
|
|
|
|
|
|
OnCursorPositionChanged(_cursorLine, _cursorColumn);
|
|
|
|
|
|
|
|
|
InvalidateVisual();
|
|
|
}
|
|
|
|
|
|
#region 查找功能
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void ShowFindDialog()
|
|
|
{
|
|
|
if (_findDialog == null || !_findDialog.IsLoaded)
|
|
|
{
|
|
|
_findDialog = new FindDialog(this);
|
|
|
_findDialog.Owner = Window.GetWindow(this);
|
|
|
}
|
|
|
|
|
|
_findDialog.Show();
|
|
|
_findDialog.Activate();
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public bool FindNext(string searchText, bool matchCase, bool wholeWord)
|
|
|
{
|
|
|
if (string.IsNullOrEmpty(searchText))
|
|
|
return false;
|
|
|
|
|
|
|
|
|
if (searchText != _lastSearchText || matchCase != _lastMatchCase || wholeWord != _lastWholeWord)
|
|
|
{
|
|
|
SearchAll(searchText, matchCase, wholeWord);
|
|
|
}
|
|
|
|
|
|
if (_findResults.Count == 0)
|
|
|
return false;
|
|
|
|
|
|
|
|
|
_currentFindIndex = (_currentFindIndex + 1) % _findResults.Count;
|
|
|
var result = _findResults[_currentFindIndex];
|
|
|
|
|
|
|
|
|
_cursorLine = result.line;
|
|
|
_cursorColumn = result.column;
|
|
|
|
|
|
|
|
|
_selectionStartLine = result.line;
|
|
|
_selectionStartColumn = result.column;
|
|
|
_selectionEndLine = result.line;
|
|
|
_selectionEndColumn = result.column + result.length;
|
|
|
|
|
|
|
|
|
EnsureCursorVisible();
|
|
|
|
|
|
|
|
|
InvalidateVisual();
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public bool FindPrevious(string searchText, bool matchCase, bool wholeWord)
|
|
|
{
|
|
|
if (string.IsNullOrEmpty(searchText))
|
|
|
return false;
|
|
|
|
|
|
|
|
|
if (searchText != _lastSearchText || matchCase != _lastMatchCase || wholeWord != _lastWholeWord)
|
|
|
{
|
|
|
SearchAll(searchText, matchCase, wholeWord);
|
|
|
}
|
|
|
|
|
|
if (_findResults.Count == 0)
|
|
|
return false;
|
|
|
|
|
|
|
|
|
_currentFindIndex = (_currentFindIndex - 1 + _findResults.Count) % _findResults.Count;
|
|
|
var result = _findResults[_currentFindIndex];
|
|
|
|
|
|
|
|
|
_cursorLine = result.line;
|
|
|
_cursorColumn = result.column;
|
|
|
|
|
|
|
|
|
_selectionStartLine = result.line;
|
|
|
_selectionStartColumn = result.column;
|
|
|
_selectionEndLine = result.line;
|
|
|
_selectionEndColumn = result.column + result.length;
|
|
|
|
|
|
|
|
|
EnsureCursorVisible();
|
|
|
|
|
|
|
|
|
InvalidateVisual();
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void SearchAll(string searchText, bool matchCase, bool wholeWord)
|
|
|
{
|
|
|
_findResults.Clear();
|
|
|
_currentFindIndex = -1;
|
|
|
_lastSearchText = searchText;
|
|
|
_lastMatchCase = matchCase;
|
|
|
_lastWholeWord = wholeWord;
|
|
|
|
|
|
if (string.IsNullOrEmpty(searchText))
|
|
|
return;
|
|
|
|
|
|
StringComparison comparison = matchCase ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;
|
|
|
|
|
|
for (int lineIndex = 0; lineIndex < _lines.Count; lineIndex++)
|
|
|
{
|
|
|
var line = _lines[lineIndex];
|
|
|
string lineText = string.Join("", line.Characters.Select(c => c.Character.ToString()));
|
|
|
|
|
|
int startIndex = 0;
|
|
|
while (startIndex < lineText.Length)
|
|
|
{
|
|
|
int foundIndex = lineText.IndexOf(searchText, startIndex, comparison);
|
|
|
if (foundIndex == -1)
|
|
|
break;
|
|
|
|
|
|
|
|
|
if (wholeWord)
|
|
|
{
|
|
|
bool isWholeWord = true;
|
|
|
|
|
|
|
|
|
if (foundIndex > 0)
|
|
|
{
|
|
|
char prevChar = lineText[foundIndex - 1];
|
|
|
if (char.IsLetterOrDigit(prevChar) || prevChar == '_')
|
|
|
isWholeWord = false;
|
|
|
}
|
|
|
|
|
|
|
|
|
if (foundIndex + searchText.Length < lineText.Length)
|
|
|
{
|
|
|
char nextChar = lineText[foundIndex + searchText.Length];
|
|
|
if (char.IsLetterOrDigit(nextChar) || nextChar == '_')
|
|
|
isWholeWord = false;
|
|
|
}
|
|
|
|
|
|
if (!isWholeWord)
|
|
|
{
|
|
|
startIndex = foundIndex + 1;
|
|
|
continue;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
_findResults.Add((lineIndex, foundIndex, searchText.Length));
|
|
|
startIndex = foundIndex + 1;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
for (int i = 0; i < _findResults.Count; i++)
|
|
|
{
|
|
|
var result = _findResults[i];
|
|
|
if (result.line > _cursorLine ||
|
|
|
(result.line == _cursorLine && result.column >= _cursorColumn))
|
|
|
{
|
|
|
_currentFindIndex = i - 1;
|
|
|
return;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
if (_findResults.Count > 0)
|
|
|
_currentFindIndex = _findResults.Count - 1;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void EnsureCursorVisible()
|
|
|
{
|
|
|
double lineHeight = FontSize * 1.2;
|
|
|
int visibleLineCount = (int)(ActualHeight / lineHeight);
|
|
|
|
|
|
|
|
|
if (_cursorLine < _scrollOffset)
|
|
|
{
|
|
|
_scrollOffset = _cursorLine;
|
|
|
}
|
|
|
|
|
|
else if (_cursorLine >= _scrollOffset + visibleLineCount)
|
|
|
{
|
|
|
_scrollOffset = _cursorLine - visibleLineCount + 1;
|
|
|
}
|
|
|
|
|
|
|
|
|
_scrollOffset = Math.Max(0, _scrollOffset);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void ClearFindHighlight()
|
|
|
{
|
|
|
_findResults.Clear();
|
|
|
_currentFindIndex = -1;
|
|
|
InvalidateVisual();
|
|
|
}
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
|
|
|
|
[Serializable]
|
|
|
public class TextCharacter
|
|
|
{
|
|
|
[JsonIgnore]
|
|
|
public System.Text.Rune Character { get; set; }
|
|
|
|
|
|
|
|
|
[JsonProperty("Character")]
|
|
|
public int CharacterValue
|
|
|
{
|
|
|
get => Character.Value;
|
|
|
set => Character = new System.Text.Rune(value);
|
|
|
}
|
|
|
|
|
|
public Brush Foreground { get; set; } = Brushes.Black;
|
|
|
public bool IsComment { get; set; } = false;
|
|
|
public bool IsKeyword { get; set; } = false;
|
|
|
public bool IsString { get; set; } = false;
|
|
|
public bool IsNumber { get; set; } = false;
|
|
|
|
|
|
|
|
|
public int x { get; set; } = -1;
|
|
|
public int y { get; set; } = -1;
|
|
|
public int w { get; set; } = -1;
|
|
|
public int h { get; set; } = -1;
|
|
|
|
|
|
public string ImageMD5 { get; set; } = "";
|
|
|
public string guid { get; set; } = "";
|
|
|
|
|
|
|
|
|
public TextCharacter()
|
|
|
{
|
|
|
}
|
|
|
|
|
|
public TextCharacter(System.Text.Rune c)
|
|
|
{
|
|
|
Character = c;
|
|
|
}
|
|
|
|
|
|
public TextCharacter(System.Text.Rune c, string guid, int x, int y, int w, int h, string imageMd5)
|
|
|
{
|
|
|
Character = c;
|
|
|
this.guid = guid;
|
|
|
this.x = x;
|
|
|
this.y = y;
|
|
|
this.w = w;
|
|
|
this.h = h;
|
|
|
ImageMD5 = imageMd5;
|
|
|
}
|
|
|
|
|
|
|
|
|
public TextCharacter(char c)
|
|
|
{
|
|
|
Character = new System.Text.Rune(c);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public class TextLine
|
|
|
{
|
|
|
public TextLine()
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
public TextLine(JArray po, JObject rectange)
|
|
|
{
|
|
|
this.pos = po;
|
|
|
this.selected_rectange = rectange;
|
|
|
}
|
|
|
|
|
|
public JArray pos { get; set; } = new JArray();
|
|
|
public JObject selected_rectange { get; set; } = new JObject();
|
|
|
|
|
|
public List<TextCharacter> Characters { get; set; } = new List<TextCharacter>();
|
|
|
}
|
|
|
|
|
|
public class UndoAction
|
|
|
{
|
|
|
public string AddedText { get; set; } = string.Empty;
|
|
|
public int AddedStartLine { get; set; }
|
|
|
public int AddedStartColumn { get; set; }
|
|
|
public int AddedEndLine { get; set; }
|
|
|
public int AddedEndColumn { get; set; }
|
|
|
|
|
|
public string RemovedText { get; set; } = string.Empty;
|
|
|
public int RemovedStartLine { get; set; }
|
|
|
public int RemovedStartColumn { get; set; }
|
|
|
public int RemovedEndLine { get; set; }
|
|
|
public int RemovedEndColumn { get; set; }
|
|
|
|
|
|
public int CursorBeforeLine { get; set; }
|
|
|
public int CursorBeforeColumn { get; set; }
|
|
|
public int CursorAfterLine { get; set; }
|
|
|
public int CursorAfterColumn { get; set; }
|
|
|
}
|
|
|
|
|
|
} |