From a6c0dcddd07983474be29177d86765393fb453b3 Mon Sep 17 00:00:00 2001 From: Rich Date: Tue, 21 Dec 2010 20:33:24 +0000 Subject: [PATCH] A number of fixes so the selector for RichTextBoxes will work properly. * If you press a left arrow when a link starts a line, the cursor will go to the end of the previous line. * If you press the END key, the cursor will not go beyond the end of the line. * If you place the cursor at the beginning of a line, and then click beyond the end of the previous line, the cursor will go to the end of the previous line --- PROMS/Volian.Controls.Library/StepRTB.cs | 30 ++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/PROMS/Volian.Controls.Library/StepRTB.cs b/PROMS/Volian.Controls.Library/StepRTB.cs index ef094604..82ef388a 100644 --- a/PROMS/Volian.Controls.Library/StepRTB.cs +++ b/PROMS/Volian.Controls.Library/StepRTB.cs @@ -489,6 +489,7 @@ namespace Volian.Controls.Library if (!_ContextMenuStripChanged && this.MyStepItem != null) this.MyStepItem.MyStepPanel.MyStepTabPanel.MyStepTabRibbon.SetContextMenu(); _ContextMenuStripChanged = false; + CorrectSelectionAtEndOfLine(); } void StepRTB_MouseUp(object sender, MouseEventArgs e) { @@ -1110,6 +1111,9 @@ namespace Volian.Controls.Library //vlnStackTrace.ShowStackLocal("HandleSelectionChangeStack", 1, 10); bool startingValue = _AdjustingSelection; if (_IdentifyingLinks || _ProcessingDelete) return; + // If the cursor is beyond the end of the line then move the cursor back one character + if (CorrectSelectionAtEndOfLine()) + return; if (ProcessKeystrokes) { if (!_MouseDown && !_AdjustingSelection) @@ -1130,7 +1134,7 @@ namespace Volian.Controls.Library { // If the cursor is at the beginning of the line and a link is at the beginning of the line // then select the link rather than postitioning the cursor at the end of the previous line - if (GetFirstCharIndexOfCurrentLine() == SelectionStart) + if (!_LastWasLeftArrow && GetFirstCharIndexOfCurrentLine() == SelectionStart) SetSelection(ll); else { @@ -1187,6 +1191,22 @@ namespace Volian.Controls.Library else MyLinkText = null; OnRTBSelectionChanged(this, new EventArgs()); + _LastWasLeftArrow = false; + } + /// + /// If the Selection is beyond the end of the line, move the selection back one character + /// + /// + private bool CorrectSelectionAtEndOfLine() + { + if (SelectionStart > 0 && SelectionLength == 0 && + GetFirstCharIndexFromLine(GetLineFromCharIndex(SelectionStart)) > + GetFirstCharIndexOfCurrentLine()) + { + SelectionStart--; // Move the cursor back one character + return true; + } + return false; } //private void HandleOverWrite() @@ -2634,12 +2654,18 @@ namespace Volian.Controls.Library DebugPrint(where, ": {0} {1} '{2}'", SelectionStart, SelectionLength, SelectedText); } #endregion - + private bool _LastWasLeftArrow = false; private void StepRTB_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e) { //Console.WriteLine("StepRTB_PreviewKeyDown"); if ((e.KeyCode == Keys.Tab) && (!e.Alt && !e.Control)) e.IsInputKey = true; + // Remember if the last key pressed was a left arrow. This is used in HandleSelectionChanged + // When a Link overlaps the beginning of a line. Normally a left arrow will move the cursor + // back one character, or if a Link is selected, it will move the cursor to the space before + // the link. Since the Link overlaps the line, special consideration must be given to assure + // that the cursor goes to the proper place. + _LastWasLeftArrow = (!e.Shift && !e.Control && !e.Alt && e.KeyCode == Keys.Left); } } public partial class StepRTBModeChangeEventArgs : EventArgs