1717* Modified By Coding Seb for the purpose of this project
1818\***********************************************************************************/
1919
20+ using System . Linq ;
21+ using System . Text . RegularExpressions ;
2022using System . Windows ;
2123using System . Windows . Controls ;
2224using 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