findWindow.xaml.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System.Windows;
  2. using System.Windows.Input;
  3. using FastColoredTextBoxNS;
  4. using System.Text.RegularExpressions;
  5. namespace MPE
  6. {
  7. public partial class findWindow : Window
  8. {
  9. public findWindow() { InitializeComponent(); tbFind.Focus(); }
  10. Place startPlace;
  11. FastColoredTextBox tb { get => DataHelper.field; }
  12. bool firstSearch = true;
  13. void BtFindNext_Click(object sender, RoutedEventArgs e) => FindNext(tbFind.Text);
  14. public void setTextInFindTB(string text) { tbFind.Text = text; }
  15. void FindNext(string pattern)
  16. {
  17. RegexOptions opt = cbMatchCase.IsChecked == true ? RegexOptions.None : RegexOptions.IgnoreCase;
  18. if (!cbRegex.IsChecked == true)
  19. pattern = Regex.Escape(pattern);
  20. if (cbWholeWord.IsChecked == true)
  21. pattern = "\\b" + pattern + "\\b";
  22. Range range = tb.Selection.Clone();
  23. range.Normalize();
  24. if (firstSearch)
  25. {
  26. startPlace = range.Start;
  27. firstSearch = false;
  28. }
  29. range.Start = range.End;
  30. if (range.Start >= startPlace)
  31. range.End = new Place(tb.GetLineLength(tb.LinesCount - 1), tb.LinesCount - 1);
  32. else
  33. {
  34. range.End = startPlace;
  35. firstSearch = true;
  36. }
  37. foreach (var r in range.GetRangesByLines(pattern, opt))
  38. {
  39. tb.Selection = r;
  40. tb.DoSelectionVisible();
  41. tb.Invalidate();
  42. return;
  43. }
  44. if (range.Start >= startPlace && startPlace > Place.Empty)
  45. {
  46. tb.Selection.Start = new Place(0, 0);
  47. FindNext(pattern);
  48. return;
  49. }
  50. tb.Selection.Start = new Place(0, 0);
  51. tb.Selection.End = new Place(0, 0);
  52. }
  53. void TbFind_KeyUp(object sender, KeyEventArgs e) { if (e.Key == Key.Enter) { FindNext(tbFind.Text); e.Handled = true; return; } }
  54. void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) => DragMove();
  55. }
  56. }