|
| 1 | +using System.Windows.Forms; |
| 2 | +using CSharpRegexTools4Npp.Utils; |
| 3 | + |
| 4 | +namespace CSharpRegexTools4Npp.Forms |
| 5 | +{ |
| 6 | + /// <summary> |
| 7 | + /// various methods that every new form in this app should call.<br></br> |
| 8 | + /// You can inherit FormBase to simplify this by automatically using all these methods in the recommended default way. |
| 9 | + /// </summary> |
| 10 | + public static class NppFormHelper |
| 11 | + { |
| 12 | + /// <summary> |
| 13 | + /// CALL THIS IN YOUR KeyDown HANDLER FOR ALL CONTROLS *except TextBoxes*<br></br> |
| 14 | + /// suppress annoying ding when user hits escape, enter, tab, or space |
| 15 | + /// </summary> |
| 16 | + public static void GenericKeyDownHandler(object sender, KeyEventArgs e) |
| 17 | + { |
| 18 | + if (e.KeyCode == Keys.Enter || e.KeyCode == Keys.Escape || e.KeyCode == Keys.Tab || e.KeyCode == Keys.Space) |
| 19 | + e.SuppressKeyPress = true; |
| 20 | + } |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// CALL THIS IN YOUR KeyPress HANDLER FOR ALL TextBoxes and ComboBoxes<br></br> |
| 24 | + /// suppress annoying ding when user hits tab |
| 25 | + /// </summary> |
| 26 | + public static void TextBoxKeyPressHandler(object sender, KeyPressEventArgs e) |
| 27 | + { |
| 28 | + if (e.KeyChar == '\t') |
| 29 | + e.Handled = true; |
| 30 | + } |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// CALL THIS IN YOUR KeyUp HANDLER FOR ALL CONTROLS (but only add to the form itself *IF NOT isModal*)<br></br> |
| 34 | + /// Enter presses button,<br></br> |
| 35 | + /// escape focuses editor (or closes if isModal),<br></br> |
| 36 | + /// Ctrl+V pastes text into text boxes and combo boxes<br></br> |
| 37 | + /// if isModal:<br></br> |
| 38 | + /// - tab goes through controls,<br></br> |
| 39 | + /// - shift-tab -> go through controls backward<br></br> |
| 40 | + /// </summary> |
| 41 | + /// <param name="form"></param> |
| 42 | + /// <param name="isModal">if true, this blocks the parent application until closed. THIS IS ONLY TRUE OF POP-UP DIALOGS</param> |
| 43 | + public static void GenericKeyUpHandler(Form form, object sender, KeyEventArgs e, bool isModal) |
| 44 | + { |
| 45 | + // enter presses button |
| 46 | + if (e.KeyCode == Keys.Enter) |
| 47 | + { |
| 48 | + e.Handled = true; |
| 49 | + if (sender is Button btn) |
| 50 | + { |
| 51 | + // Enter has the same effect as clicking a selected button |
| 52 | + btn.PerformClick(); |
| 53 | + } |
| 54 | + else |
| 55 | + PressEnterInTextBoxHandler(sender, isModal); |
| 56 | + } |
| 57 | + // Escape -> |
| 58 | + // * if this.IsModal (meaning this is a pop-up dialog), close this. |
| 59 | + // * otherwise, focus the editor component. |
| 60 | + else if (e.KeyData == Keys.Escape) |
| 61 | + { |
| 62 | + if (isModal) |
| 63 | + form.Close(); |
| 64 | + else |
| 65 | + Npp.Editor.GrabFocus(); |
| 66 | + } |
| 67 | + // Tab -> go through controls, Shift+Tab -> go through controls backward |
| 68 | + else if (e.KeyCode == Keys.Tab && isModal) |
| 69 | + { |
| 70 | + GenericTabNavigationHandler(form, sender, e); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + /// <summary> |
| 75 | + /// CALL THIS METHOD IN A KeyUp HANDLER, *UNLESS USING GenericKeyUpHandler ABOVE*<br></br> |
| 76 | + /// Tab -> go through controls, Shift+Tab -> go through controls backward.<br></br> |
| 77 | + /// Ignores invisible or disabled controls. |
| 78 | + /// </summary> |
| 79 | + /// <param name="form">the parent form</param> |
| 80 | + /// <param name="sender">probably a control with a tabstop</param> |
| 81 | + /// <param name="e">the key event that triggered this</param> |
| 82 | + public static void GenericTabNavigationHandler(Form form, object sender, KeyEventArgs e) |
| 83 | + { |
| 84 | + if (sender is TextBox tb && tb.Parent is ListBox) |
| 85 | + return; // ComboBoxes are secretly two controls in one (see https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.combobox?view=windowsdesktop-8.0) |
| 86 | + // this event fires twice for a CombobBox because of this, so we need to suppress the extra one this way |
| 87 | + Control next = form.GetNextControl((Control)sender, !e.Shift); |
| 88 | + while (next == null || !next.TabStop || !next.Visible || !next.Enabled) |
| 89 | + next = form.GetNextControl(next, !e.Shift); |
| 90 | + next.Focus(); |
| 91 | + e.Handled = true; |
| 92 | + } |
| 93 | + |
| 94 | + /// <summary> |
| 95 | + /// NPPM_MODELESSDIALOG consumes the KeyDown and KeyPress events for the Enter key,<br></br> |
| 96 | + /// so our KeyUp handler needs to simulate pressing enter to add a new line in a multiline text box.<br></br> |
| 97 | + /// Note that this does not fully repair the functionality of the Enter key in a multiline text box, |
| 98 | + /// because only one newline can be created for a single keypress of Enter, no matter how long the key is held down. |
| 99 | + /// </summary> |
| 100 | + /// <param name="sender">the text box that sent the message</param> |
| 101 | + /// <param name="isModal">if true, this blocks the parent application until closed. THIS IS ONLY TRUE OF POP-UP DIALOGS</param> |
| 102 | + public static void PressEnterInTextBoxHandler(object sender, bool isModal) |
| 103 | + { |
| 104 | + |
| 105 | + if (!isModal && sender is TextBox tb && tb.Multiline) |
| 106 | + { |
| 107 | + int selstart = tb.SelectionStart; |
| 108 | + tb.SelectedText = ""; |
| 109 | + string text = tb.Text; |
| 110 | + tb.Text = text.Substring(0, selstart) + "\r\n" + text.Substring(selstart); |
| 111 | + tb.SelectionStart = selstart + 2; // after the inserted newline |
| 112 | + tb.SelectionLength = 0; |
| 113 | + tb.ScrollToCaret(); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + /// <summary> |
| 118 | + /// CALL THIS IN YOUR Dispose(bool disposing) METHOD, INSIDE OF THE ".Designer.cs" FILE<br></br> |
| 119 | + /// When this form is initialized, *if it is a modeless dialog* (i.e., !isModal; the form does not block the parent application until closed)<br></br> |
| 120 | + /// this will call Notepad++ with the NPPM_MODELESSDIALOG message to register the form. |
| 121 | + /// <strong>VERY IMPORTANT: in your Designer.cs files, in the part where it says this.Controls.Add(nameOfControl), |
| 122 | + /// you need to make sure the controls are added in tabstop order.</strong><br></br> |
| 123 | + /// This is because the order in which the controls are added controls tab order.<br></br> |
| 124 | + /// For example, if you want to go through your controls in the order<br></br> |
| 125 | + /// 1. FooButton<br></br> |
| 126 | + /// 2. BarTextBox<br></br> |
| 127 | + /// 3. BazCheckBox<br></br> |
| 128 | + /// You must go to your Designer.cs file and make sure that the Form adds the controls in this order:<br></br> |
| 129 | + /// <code> |
| 130 | + /// this.Controls.Add(this.FooButton); |
| 131 | + /// this.Controls.Add(this.BarTextBox); |
| 132 | + /// this.Controls.Add(this.BazCheckBox); |
| 133 | + /// </code> |
| 134 | + /// </summary> |
| 135 | + /// <param name="isModal">if true, this blocks the parent application until closed. THIS IS ONLY TRUE OF POP-UP DIALOGS</param> |
| 136 | + public static void RegisterFormIfModeless(Form form, bool isModal) |
| 137 | + { |
| 138 | + if (!isModal) |
| 139 | + Npp.Notepad.AddModelessDialog(form.Handle); |
| 140 | + } |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | + /// <summary> |
| 145 | + /// CALL THIS IN YOUR Dispose(bool disposing) METHOD, INSIDE OF THE ".Designer.cs" FILE<br></br> |
| 146 | + /// If this was a modeless dialog (i.e., !isModal; a dialog that does not block Notepad++ while open),<br></br> |
| 147 | + /// call Notepad++ with the NPPM_MODELESSDIALOG message to unregister the form. |
| 148 | + /// </summary> |
| 149 | + /// <param name="isModal">if true, this blocks the parent application until closed. THIS IS ONLY TRUE OF POP-UP DIALOGS</param> |
| 150 | + public static void UnregisterFormIfModeless(Form form, bool isModal) |
| 151 | + { |
| 152 | + if (!form.IsDisposed && !isModal) |
| 153 | + Npp.Notepad.RemoveModelessDialog(form.Handle); |
| 154 | + } |
| 155 | + } |
| 156 | +} |
0 commit comments