-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRichTextBoxContextMenuManager.cs
276 lines (244 loc) · 11 KB
/
RichTextBoxContextMenuManager.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace EditClipboardContents
{
public class RichTextBoxContextMenuManager
{
// Custom event to signal when the parent form should modify its suppression variable
public event EventHandler<bool> SetSuppressionVariableForParentForm;
// Helper method to raise event to tell parent form to suppress selection change events
private void RaiseSuppressionEvent(bool suppressValue)
{
SetSuppressionVariableForParentForm?.Invoke(this, suppressValue);
}
private ContextMenuStrip contextMenu;
private ToolStripMenuItem menuItemCopy;
private ToolStripMenuItem menuItemCopyAllPlaintext;
private ToolStripMenuItem menuItemSelectAll;
private ToolStripMenuItem menuItemCopyLink;
private ToolStripMenuItem menuItemOpenLink;
private ToolStripSeparator separatorCopy;
private ToolStripSeparator separatorLinks;
private string lastSelectedHyperlink;
public RichTextBoxContextMenuManager()
{
InitializeContextMenu();
}
private void InitializeContextMenu()
{
contextMenu = new ContextMenuStrip();
menuItemCopy = new ToolStripMenuItem("Copy");
menuItemCopyAllPlaintext = new ToolStripMenuItem("Copy All");
menuItemSelectAll = new ToolStripMenuItem("Select All");
menuItemCopyLink = new ToolStripMenuItem("Copy Link");
menuItemOpenLink = new ToolStripMenuItem("Open Link");
separatorCopy = new ToolStripSeparator();
separatorLinks = new ToolStripSeparator();
contextMenu.Items.AddRange(new ToolStripItem[] {
menuItemOpenLink,
menuItemCopyLink,
separatorLinks,
menuItemCopy,
separatorCopy,
menuItemSelectAll,
menuItemCopyAllPlaintext,
});
// Attach event handlers
contextMenu.Opening += ContextMenu_Opening;
menuItemCopy.Click += MenuItemCopy_Click;
menuItemCopyAllPlaintext.Click += MenuItemCopyAllPlaintext_Click;
menuItemSelectAll.Click += MenuItemSelectAll_Click;
menuItemCopyLink.Click += MenuItemCopyLink_Click;
menuItemOpenLink.Click += MenuItemOpenLink_Click;
}
public void AttachToRichTextBox(RichTextBox richTextBox)
{
richTextBox.ContextMenuStrip = contextMenu;
richTextBox.MouseDown += RichTextBox_MouseDown;
}
// ------------------------------ Contents Text Boxes Context Menu Events ---------------------------------
// Decide which options to show in the menu
private void ContextMenu_Opening(object sender, CancelEventArgs e)
{
// Conditions depending on if anything is selected
if ( sender is ContextMenuStrip contextMenu && contextMenu.SourceControl is RichTextBox richTextBox )
{
// No Text Selected - Disable the regular 'copy' button, but enable the 'copy all' buttons
if ( string.IsNullOrEmpty(richTextBox.SelectedText) )
{
menuItemCopy.Visible = false;
separatorCopy.Visible = false;
menuItemCopyAllPlaintext.Visible = true;
}
// Text Selected - Enable the regular 'copy' button, but disable the 'copy all' buttons
else
{
menuItemCopy.Visible = true;
separatorCopy.Visible = true;
menuItemCopyAllPlaintext.Visible = false;
}
}
}
private void MenuItemCopyAllPlaintext_Click(object sender, EventArgs e)
{
// Get the text box that the context menu was opened from
if ( sender is ToolStripMenuItem menuItem
&& menuItem.Owner is ContextMenuStrip contextMenu
&& contextMenu.SourceControl is RichTextBox richTextBox )
{
// Since we're copying everything, we can use the Text property instead of SelectedText, which gives us the contents in plaintext
CopyIfValid(richTextBox.Text);
}
}
private void MenuItemCopy_Click(object sender, EventArgs e)
{
// Get the text box that the context menu was opened from
if ( sender is ToolStripMenuItem menuItem
&& menuItem.Owner is ContextMenuStrip contextMenu
&& contextMenu.SourceControl is RichTextBox richTextBox )
{
// Use SelectedText property to get the plaintext, instead of doing .Copy() method which would copy with formatting
CopyIfValid(richTextBox.SelectedText);
}
}
private void MenuItemSelectAll_Click(object sender, EventArgs e)
{
// Get the text box that the context menu was opened from
if ( sender is ToolStripMenuItem menuItem
&& menuItem.Owner is ContextMenuStrip contextMenu
&& contextMenu.SourceControl is RichTextBox richTextBox )
{
richTextBox.SelectAll();
}
}
private void MenuItemOpenLink_Click(object sender, EventArgs e)
{
if ( !String.IsNullOrEmpty(lastSelectedHyperlink) ) // Should not be null or empty here but just in case
{
try
{
System.Diagnostics.Process.Start(lastSelectedHyperlink);
}
catch ( Exception ex )
{
MessageBox.Show($"Error opening link: {lastSelectedHyperlink}\n\nError:\n{ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void MenuItemCopyLink_Click(object sender, EventArgs e)
{
CopyIfValid(lastSelectedHyperlink);
}
private void RichTextBox_MouseDown(object sender, MouseEventArgs e)
{
// Decide whether to show the 'Copy Hyperlink' context menu item depending on if the mouse is over a hyperlink when right clicking
if ( e.Button == MouseButtons.Right && sender is RichTextBox richTextBox )
{
// Set the default visibility to false unless it's a link for the link related menu items
menuItemCopyLink.Visible = false;
menuItemOpenLink.Visible = false;
separatorLinks.Visible = false;
// Get the indexes range of the current selection
int start = richTextBox.SelectionStart;
int length = richTextBox.SelectionLength;
// Get the character index at the mouse position
int charIndex = richTextBox.GetCharIndexFromPosition(e.Location);
// Signal to the parent form to suppress selection change events
RaiseSuppressionEvent(true);
// Create a temporary selection to check formatting
richTextBox.Select(charIndex, 1);
// Check if it's a hyperlink indirectly, by checking if the 'selectionrtf' has a blue color table, but selected text is not blue
// This signals the blue formatting is from a hyperlink, not just a blue font color
string lookForStr = @"{\colortbl ;\red0\green0\blue255;}";
// If it is a hyperlink
if ( richTextBox.SelectedRtf.Contains(lookForStr) && !richTextBox.SelectionColor.Equals(Color.Blue) )
{
string url = GetLinkAtPosition(richTextBox, charIndex);
if ( !String.IsNullOrEmpty(url) )
{
menuItemCopyLink.Visible = true;
menuItemOpenLink.Visible = true;
separatorLinks.Visible = true;
lastSelectedHyperlink = url;
}
}
// Restore the original selection. If it fails for some reason set a default selection
try
{
richTextBox.Select(start, length);
}
catch
{
richTextBox.Select(0, 0);
}
// Signal to the parent form to restore selection change events
RaiseSuppressionEvent(false);
}
}
private string GetLinkAtPosition(RichTextBox richTextBox, int charIndex)
{
// Store original selection
int originalStart = richTextBox.SelectionStart;
int originalLength = richTextBox.SelectionLength;
try
{
// First scan backwards to previous whitespace or start of document
int start = charIndex;
while ( start > 0 && !char.IsWhiteSpace(richTextBox.Text[start - 1]) )
{
start--;
}
// Then scan forwards to next whitespace or end of document
int end = charIndex;
while ( end < richTextBox.TextLength && !char.IsWhiteSpace(richTextBox.Text[end]) )
{
end++;
}
// Select the word and check if it's a hyperlink
richTextBox.Select(start, end - start);
string rtf = richTextBox.SelectedRtf;
if ( rtf.Contains("HYPERLINK") )
{
int linkStart = rtf.IndexOf(@"HYPERLINK ");
if ( linkStart != -1 )
{
linkStart += @"HYPERLINK ".Length;
int linkEnd = rtf.IndexOf(' ', linkStart); // Find the index of the next space after the link
if ( linkEnd != -1 )
{
return rtf.Substring(linkStart, linkEnd - linkStart);
}
}
}
return "";
}
finally
{
// Restore original selection
try
{
richTextBox.Select(originalStart, originalLength);
}
catch
{
richTextBox.Select(0, 0);
}
}
}
private static void CopyIfValid(string text)
{
if ( !string.IsNullOrEmpty(text) )
Clipboard.SetText(text);
else
{
// Show a tooltip if the user tries to copy an empty string
ToolTip tt = new ToolTip();
Point cursorPosition = Form.ActiveForm.PointToClient(Cursor.Position);
cursorPosition.Y += 20; // Offset the Y coordinate by 20 pixels downward
tt.Show("Nothing to copy", Form.ActiveForm, cursorPosition, 1500);
}
}
}
}