Skip to content

Commit 7e4c8de

Browse files
author
Sébastien Geiser
committed
Better selection
1 parent 8cb7efc commit 7e4c8de

File tree

2 files changed

+51
-10
lines changed

2 files changed

+51
-10
lines changed

RegexDialog/RegExToolDialog.xaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
<ControlTemplate TargetType="{x:Type local:SearchableTextControl}">
4848
<Border Background="{TemplateBinding Background}"
4949
BorderBrush="{TemplateBinding BorderBrush}"
50-
BorderThickness="{TemplateBinding BorderThickness}">
50+
BorderThickness="{TemplateBinding BorderThickness}"
51+
Padding="{TemplateBinding Padding}">
5152
<TextBlock TextWrapping="Wrap" x:Name="PART_TEXT"/>
5253
</Border>
5354
</ControlTemplate>
@@ -635,7 +636,9 @@
635636
VerticalAlignment="Stretch"
636637
HorizontalAlignment="Stretch"/>
637638
<local:SearchableTextControl x:Name="tbxRegexLanguageElementDescription"
639+
Padding="2,0"
638640
Grid.Row="2"
641+
Focusable="True"
639642
BorderBrush="Gray"
640643
BorderThickness="1"
641644
Background="WhiteSmoke"

RegexDialog/SearchableTextControl.cs

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
* Modified By Coding Seb for the purpose of this project
1818
\***********************************************************************************/
1919

20+
using System.Linq;
21+
using System.Text.RegularExpressions;
2022
using System.Windows;
2123
using System.Windows.Controls;
2224
using System.Windows.Documents;
@@ -43,36 +45,67 @@ static SearchableTextControl()
4345
new FrameworkPropertyMetadata(typeof(SearchableTextControl)));
4446
}
4547

48+
public SearchableTextControl()
49+
{
50+
CommandBindings.Add(new CommandBinding(ApplicationCommands.Copy, (sender, e) => Clipboard.SetText(SelectedText), (sender, e) => e.CanExecute = IsSelectable && SelectedText.Length > 0));
51+
}
52+
4653
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
4754
{
55+
base.OnMouseLeftButtonDown(e);
56+
4857
if (displayTextBlock != null && IsSelectable)
4958
{
50-
base.OnMouseLeftButtonDown(e);
51-
displayTextBlock.Focusable = false;
52-
displayTextBlock.IsHitTestVisible = false;
53-
ResetSelectionTextRange();
54-
Point mouseDownPoint = e.GetPosition(this);
55-
StartSelectPosition = displayTextBlock.GetPositionFromPoint(mouseDownPoint, true);
56-
isSelecting = true;
59+
if (e.ClickCount == 3)
60+
{
61+
StartSelectPosition = displayTextBlock.ContentStart;
62+
EndSelectPosition = displayTextBlock.ContentEnd;
63+
64+
SelectFromStartToEnd();
65+
}
66+
if (e.ClickCount == 2)
67+
{
68+
int index = StartSelectPosition.DocumentStart.GetOffsetToPosition(StartSelectPosition);
69+
Match wordMatch = Regex.Matches(displayTextBlock.Text, @"\w+", RegexOptions.Singleline)
70+
.Cast<Match>()
71+
.First(match => match.Index <= index - 1 && match.Index + match.Length >= index - 1);
72+
73+
StartSelectPosition = displayTextBlock.ContentStart.GetPositionAtOffset(wordMatch.Index + 1);
74+
EndSelectPosition = displayTextBlock.ContentStart.GetPositionAtOffset(wordMatch.Index + wordMatch.Length + 1);
75+
76+
SelectFromStartToEnd();
77+
}
78+
else
79+
{
80+
CaptureMouse();
81+
Focus();
82+
displayTextBlock.Focusable = false;
83+
displayTextBlock.IsHitTestVisible = false;
84+
ResetSelectionTextRange();
85+
Point mouseDownPoint = e.GetPosition(this);
86+
StartSelectPosition = displayTextBlock.GetPositionFromPoint(mouseDownPoint, true);
87+
isSelecting = true;
88+
}
5789
}
5890
}
5991

6092
protected override void OnMouseMove(MouseEventArgs e)
6193
{
94+
base.OnMouseMove(e);
6295
if (isSelecting && displayTextBlock != null && IsSelectable)
6396
{
64-
base.OnMouseMove(e);
6597
SelectTextFromMouseAction(e);
6698
}
6799
}
68100

69101
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
70102
{
71103
base.OnMouseLeftButtonUp(e);
104+
ReleaseMouseCapture();
72105
if (displayTextBlock != null && IsSelectable && isSelecting)
73106
{
74-
base.OnMouseUp(e);
75107
SelectTextFromMouseAction(e);
108+
76109
isSelecting = false;
77110
}
78111
}
@@ -81,6 +114,11 @@ private void SelectTextFromMouseAction(MouseEventArgs e)
81114
{
82115
Point mouseUpPoint = e.GetPosition(this);
83116
EndSelectPosition = displayTextBlock.GetPositionFromPoint(mouseUpPoint, true);
117+
SelectFromStartToEnd();
118+
}
119+
120+
private void SelectFromStartToEnd()
121+
{
84122
ResetSelectionTextRange();
85123
TextRange textRange = new TextRange(StartSelectPosition, EndSelectPosition);
86124
textRange.ApplyPropertyValue(TextElement.ForegroundProperty, SystemColors.HighlightTextBrush);

0 commit comments

Comments
 (0)