Changeset 14 for Common/RichTextBoxEx.cs
- Timestamp:
- Aug 2, 2022, 11:46:25 AM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Common/RichTextBoxEx.cs
r13 r14 9 9 public partial class RichTextBoxEx : RichTextBox 10 10 { 11 private bool linksActive = false;11 private bool linksActive; 12 12 public delegate void LinkClickHandler(string link); 13 13 public event LinkClickHandler LinkClick; 14 14 public string Title { get; set; } 15 public Form parentForm;16 public List<LinkMatch> linkMatches;17 public FormFind formFind;18 public string previousRtf;15 public Form ParentForm; 16 public List<LinkMatch> LinkMatches; 17 public FormFind FormFind; 18 public string PreviousRtf; 19 19 private ToolStripMenuItem tsmiUndo; 20 20 private ToolStripMenuItem tsmiRedo; … … 31 31 { 32 32 //InitializeComponent(); 33 KeyDown += new System.Windows.Forms.KeyEventHandler(this.HandleKeyDown);34 KeyUp += new System.Windows.Forms.KeyEventHandler(this.HandleKeyUp);35 MouseClick += new System.Windows.Forms.MouseEventHandler(this.HandleMouseClick);36 MouseLeave += new EventHandler(this.HandleMouseLeave);37 Leave += new EventHandler(this.HandleLeave);38 SelectionChanged += delegate (object sender, EventArgs e)33 KeyDown += HandleKeyDown; 34 KeyUp += HandleKeyUp; 35 MouseClick += HandleMouseClick; 36 MouseLeave += HandleMouseLeave; 37 Leave += HandleLeave; 38 SelectionChanged += delegate 39 39 { 40 40 if (interfaceUpdateEnabled) UpdateInterface(); 41 41 }; 42 42 AddContextMenu(); 43 linkMatches = new List<LinkMatch>();43 LinkMatches = new List<LinkMatch>(); 44 44 } 45 45 … … 54 54 ReadOnly = false; 55 55 interfaceUpdateEnabled = false; 56 previousRtf = Rtf;56 PreviousRtf = Rtf; 57 57 RichTextBoxContext context = new RichTextBoxContext(); 58 58 context.SaveContext(this); 59 59 List<int> linkMatchStart = new List<int>(); 60 foreach (var linkMatch in linkMatches) 60 foreach (var linkMatch in LinkMatches) 61 { 61 62 linkMatchStart.Add(-2); 63 } 64 62 65 List<string> linkMatchStartString = new List<string>(); 63 foreach (var linkMatch in linkMatches)64 linkMatchStartString.Add(linkMatch. startString.ToLowerInvariant());66 foreach (var linkMatch in LinkMatches) 67 linkMatchStartString.Add(linkMatch.StartString.ToLowerInvariant()); 65 68 66 69 string content = richTextBox.Text; … … 71 74 72 75 int contentStart = 0; 73 int selectionStart = 0;74 76 do 75 77 { … … 79 81 { 80 82 int i = 0; 81 foreach (var linkMatch in linkMatches)83 foreach (var linkMatch in LinkMatches) 82 84 { 83 85 if ((linkMatchStart[i] < contentStart) && (linkMatchStart[i] != -1)) 84 86 { 85 if (linkMatch. caseSensitive)87 if (linkMatch.CaseSensitive) 86 88 { 87 linkMatchStart[i] = content.IndexOf(linkMatch. startString, contentStart, StringComparison.Ordinal);89 linkMatchStart[i] = content.IndexOf(linkMatch.StartString, contentStart, StringComparison.Ordinal); 88 90 } else 89 91 { … … 103 105 104 106 int index = firstIndex; 105 string startString = firstMatch. startString;107 string startString = firstMatch.StartString; 106 108 107 109 int linkLength = startString.Length; 108 selectionStart = index; 109 110 string linkTextAfter = ""; 111 if (firstMatch.ExecuteMatch(content, index + startString.Length, out linkTextAfter)) 112 { 113 if ((firstMatch.startString != "") || ((firstMatch.startString == "") && (linkTextAfter.Length == 5) && 110 var selectionStart = index; 111 112 if (firstMatch.ExecuteMatch(content, index + startString.Length, out var linkTextAfter)) 113 { 114 if ((firstMatch.StartString != "") || ((firstMatch.StartString == "") && (linkTextAfter.Length == 5) && 114 115 ((index < 1) || 115 116 ((index >= 1) && ((content[index - 1] == ' ') || (content[index - 1] == '\n')))) … … 175 176 public void UndoUnknownActions() 176 177 { 178 int i = 0; 177 179 while (CanUndo && (UndoActionId == UndoNameId.Unknown)) 178 180 { 179 181 Undo(); 180 } 181 } 182 183 private void HideRichTextBoxLinks(RichTextBox richTextBox) 182 i++; 183 if (i > 1000) break; 184 } 185 } 186 187 private void HideRichTextBoxLinks() 184 188 { 185 189 bool lastReadOnlyState = ReadOnly; … … 188 192 RichTextBoxContext context = new RichTextBoxContext(); 189 193 context.SaveContext(this); 190 Rtf = previousRtf;194 Rtf = PreviousRtf; 191 195 UndoUnknownActions(); 192 196 … … 219 223 if ((e.KeyCode == Keys.ControlKey) && linksActive) 220 224 { 221 HideRichTextBoxLinks( this);225 HideRichTextBoxLinks(); 222 226 linksActive = false; 223 227 } … … 228 232 if ((e.Button == MouseButtons.Left) && linksActive) 229 233 { 230 int linkStart = 0;231 int linkEnd = 0;234 int linkStart; 235 int linkEnd; 232 236 int mousePointerCharIndex = GetCharIndexFromPosition(e.Location); 233 237 SelectionStart = mousePointerCharIndex; … … 238 242 { 239 243 SelectionStart -= 1; 240 continue;241 244 } 242 245 else … … 257 260 { 258 261 SelectionStart += 1; 259 continue;260 262 } 261 263 else … … 273 275 if (link != "") 274 276 { 275 foreach (var linkMatch in linkMatches)277 foreach (var linkMatch in LinkMatches) 276 278 { 277 if ((link.Length >= linkMatch. startString.Length) && (278 (linkMatch. caseSensitive && (link.Substring(0, linkMatch.startString.Length) == linkMatch.startString)) ||279 (!linkMatch. caseSensitive && (link.ToLowerInvariant().Substring(0, linkMatch.startString.Length) == linkMatch.startString.ToLower()))))279 if ((link.Length >= linkMatch.StartString.Length) && ( 280 (linkMatch.CaseSensitive && (link.Substring(0, linkMatch.StartString.Length) == linkMatch.StartString)) || 281 (!linkMatch.CaseSensitive && (link.ToLowerInvariant().Substring(0, linkMatch.StartString.Length) == linkMatch.StartString.ToLower())))) 280 282 { 281 string linkNumber = link.Substring(linkMatch. startString.Length).Trim(new char[] { ' ', '#' });283 string linkNumber = link.Substring(linkMatch.StartString.Length).Trim(new char[] { ' ', '#' }); 282 284 if (int.TryParse(linkNumber, out int number)) 283 285 { … … 287 289 } 288 290 } 289 if (LinkClick != null) LinkClick(link); 290 HideRichTextBoxLinks(this); 291 292 LinkClick?.Invoke(link); 293 HideRichTextBoxLinks(); 291 294 linksActive = false; 292 295 } … … 297 300 public void ShowInWindow() 298 301 { 299 Form textForm = new Form ()302 Form textForm = new Form 300 303 { 301 304 Name = "FormRichTextBox", … … 304 307 FormBorderStyle = FormBorderStyle.Sizable, 305 308 Text = Title, 306 Font = new Font( this.Font.FontFamily, this.Font.Size),309 Font = new Font(Font.FontFamily, Font.Size), 307 310 StartPosition = FormStartPosition.CenterScreen, 311 Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath), 308 312 }; 309 textForm.Icon = Icon.ExtractAssociatedIcon(Application.ExecutablePath); 310 RichTextBoxEx richTextBox = new RichTextBoxEx(); 311 richTextBox.parentForm = textForm; 312 richTextBox.Dock = DockStyle.Fill; 313 richTextBox.Text = this.Text; 314 richTextBox.Title = this.Title; 315 richTextBox.linkMatches = this.linkMatches; 316 richTextBox.ReadOnly = this.ReadOnly; 317 richTextBox.LinkClick = delegate (string linkText) 318 { 319 if (this.LinkClick != null) this.LinkClick.Invoke(linkText); 313 RichTextBoxEx richTextBox = new RichTextBoxEx 314 { 315 ParentForm = textForm, 316 Dock = DockStyle.Fill, 317 Text = Text, 318 Title = Title, 319 LinkMatches = LinkMatches, 320 ReadOnly = ReadOnly, 321 LinkClick = delegate(string linkText) { LinkClick?.Invoke(linkText); } 320 322 }; 321 323 textForm.Controls.Add(richTextBox); 322 textForm.Load += delegate (object sender3, EventArgs e3)324 textForm.Load += delegate 323 325 { 324 326 Theme.UseTheme(textForm); 325 327 DpiScaling.Apply(textForm); 326 new FormDimensions().Load(textForm, parentForm);328 new FormDimensions().Load(textForm, ParentForm); 327 329 richTextBox.ClearUndo(); 328 330 }; 329 textForm.FormClosing += delegate (object sender2, FormClosingEventArgs e2)330 { 331 new FormDimensions().Save(textForm, parentForm);331 textForm.FormClosing += delegate 332 { 333 new FormDimensions().Save(textForm, ParentForm); 332 334 }; 333 335 textForm.Show(); … … 336 338 private void UpdateInterface() 337 339 { 338 tsmiUndo.Enabled = CanUndo && ! this.ReadOnly;339 tsmiRedo.Enabled = CanRedo && ! this.ReadOnly;340 tsmiCut.Enabled = (SelectionLength != 0) && ! this.ReadOnly;340 tsmiUndo.Enabled = CanUndo && !ReadOnly; 341 tsmiRedo.Enabled = CanRedo && !ReadOnly; 342 tsmiCut.Enabled = (SelectionLength != 0) && !ReadOnly; 341 343 tsmiCopy.Enabled = SelectionLength != 0; 342 tsmiPaste.Enabled = Clipboard.ContainsText() && ! this.ReadOnly;343 tsmiDelete.Enabled = (SelectionLength != 0) && ! this.ReadOnly;344 tsmiPaste.Enabled = Clipboard.ContainsText() && !ReadOnly; 345 tsmiDelete.Enabled = (SelectionLength != 0) && !ReadOnly; 344 346 tsmiSelectAll.Enabled = (TextLength > 0) && (SelectionLength < TextLength); 345 347 } … … 349 351 if (linksActive) 350 352 { 351 HideRichTextBoxLinks( this);353 HideRichTextBoxLinks(); 352 354 linksActive = false; 353 355 } … … 435 437 tsmiFind.Click += (sender, e) => { 436 438 HideLinks(); 437 if ( formFind == null)438 { 439 formFind = new FormFind();440 formFind.richTextBox = this;441 formFind.Owner = parentForm;442 } 443 formFind.Show();444 formFind.BringToFront();439 if (FormFind == null) 440 { 441 FormFind = new FormFind(); 442 FormFind.richTextBox = this; 443 FormFind.Owner = ParentForm; 444 } 445 FormFind.Show(); 446 FormFind.BringToFront(); 445 447 }; 446 448 tsmiFind.ShortcutKeys = Keys.F | Keys.Control; … … 514 516 public class LinkMatch 515 517 { 516 public string startString;517 public bool caseSensitive;518 public string StartString; 519 public bool CaseSensitive; 518 520 public delegate bool MatchHandler(string inContent, int startIndex, out string outContent); 519 521 public delegate void LinkActionHandler(int number); … … 523 525 public LinkMatch(string startString, MatchHandler matchHandler, LinkActionHandler action) 524 526 { 525 this.startString = startString;527 StartString = startString; 526 528 Match = matchHandler; 527 529 LinkAction = action; … … 551 553 552 554 // Try to connect to following number 553 string number;554 555 numberStart = startIndex; 555 556 while (((content.Length - numberStart) >= 1) && ((content[numberStart] == ' ') || (content[numberStart] == '#'))) … … 564 565 i++; 565 566 } 566 number = content.Substring(numberStart, i);567 var number = content.Substring(numberStart, i); 567 568 568 569 if (int.TryParse(number, out int intNumber))
Note:
See TracChangeset
for help on using the changeset viewer.