-
Notifications
You must be signed in to change notification settings - Fork 4
/
TextViewWindow.cs
135 lines (109 loc) · 3.23 KB
/
TextViewWindow.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using EnvDTE;
using Microsoft.VisualStudio.TextManager.Interop;
using WordLight.NativeMethods;
using WordLight.EventAdapters;
using WordLight.Extensions;
using WordLight.Search;
namespace WordLight
{
public class TextViewWindow : NativeWindow, IDisposable
{
private TextView _view;
private string _previousSelectedText;
private object _paintSync = new object();
public event PaintEventHandler Paint;
public event EventHandler PaintEnd;
public TextView View
{
get { return _view; }
}
public TextViewWindow(TextView view)
{
if (view == null) throw new ArgumentNullException("view");
_view = view;
AssignHandle(_view.WindowHandle);
Log.Debug("TextViewWindow is created");
}
public void Dispose()
{
ReleaseHandle();
}
protected override void WndProc(ref Message m)
{
try
{
switch (m.Msg)
{
case WinProcMessages.WM_KEYUP:
case WinProcMessages.WM_KEYDOWN:
case WinProcMessages.WM_LBUTTONUP:
case WinProcMessages.WM_RBUTTONUP:
case WinProcMessages.WM_MBUTTONUP:
case WinProcMessages.WM_XBUTTONUP:
case WinProcMessages.WM_LBUTTONDOWN:
case WinProcMessages.WM_MBUTTONDOWN:
case WinProcMessages.WM_RBUTTONDOWN:
case WinProcMessages.WM_XBUTTONDOWN:
case WinProcMessages.WM_LBUTTONDBLCLK:
case WinProcMessages.WM_MBUTTONDBLCLK:
case WinProcMessages.WM_RBUTTONDBLCLK:
case WinProcMessages.WM_XBUTTONDBLCLK:
base.WndProc(ref m);
HandleUserInput();
break;
case WinProcMessages.WM_PAINT:
Rectangle clipRect = User32.GetUpdateRect(Handle, false).ToRectangle();
base.WndProc(ref m);
if (clipRect != Rectangle.Empty)
{
OnPaint(clipRect);
}
break;
default:
base.WndProc(ref m);
break;
}
}
catch (Exception ex)
{
Log.Error("Unhandled exception during processing window messages", ex);
}
}
private void HandleUserInput()
{
string text = _view.GetSelectedText();
if (text != _previousSelectedText)
{
_previousSelectedText = text;
_view.SearchText(text);
}
}
private void OnPaint(Rectangle clipRect)
{
Monitor.Enter(_paintSync);
User32.HideCaret(Handle);
using (Graphics g = Graphics.FromHwnd(Handle))
{
if (clipRect == Rectangle.Empty)
{
clipRect = Rectangle.Truncate(g.VisibleClipBounds);
}
g.SetClip(clipRect);
var evt = Paint;
if (evt != null) evt(this, new PaintEventArgs(g, clipRect));
}
User32.ShowCaret(Handle);
var paintEndEvent = PaintEnd;
if (paintEndEvent != null) paintEndEvent(this, EventArgs.Empty);
Monitor.Exit(_paintSync);
}
}
}