Skip to content

Commit

Permalink
fix docking form bug, add form superclass
Browse files Browse the repository at this point in the history
FIX: issue 83 in CsvLint, which caused Notepad++ to hang forever
    whenever a button in a container of an undocked docking form
    was used to open another form.
ADD: new FormBase superclass, which forms can subclass to add many
    desirable behaviors and fix some bugs related to NPP interop
  • Loading branch information
molsonkiko committed Mar 11, 2024
1 parent 574e196 commit 74862af
Show file tree
Hide file tree
Showing 17 changed files with 458 additions and 76 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ UpgradeLog*.XML

Visual Studio Project Template C#/$projectname$.sln
**/.vs
**/.vscode
!NppCSharpPluginPack/Dependencies/x64
NppCSharpPluginPack/UpgradeLog.htm
!testfiles/**/*example*.log
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
### Added

1. Make it much easier to [include third-party dependencies](/docs/README.md#loading-third-party-dependencies) in your plugin.
2. Made it so all forms subclass a base class, making it easier to implement recommended methods.

### Fixed

1. Fixed issue where clicking buttons on floating docking dialogs could sometimes cause Notepad++ to hang forever (see [CsvLint issue 83](https://github.com/BdR76/CSVLint/issues/83) for a detailed explanation).

## [0.0.3] - 2024-02-26

Expand Down
1 change: 0 additions & 1 deletion NppCSharpPluginPack/Forms/DarkModeTestForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions NppCSharpPluginPack/Forms/DarkModeTestForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@

namespace NppDemo.Forms
{
public partial class DarkModeTestForm : Form
public partial class DarkModeTestForm : FormBase
{
private SelectionRememberingForm selectionRememberingForm;

public DarkModeTestForm(SelectionRememberingForm selectionRememberingForm)
public DarkModeTestForm(SelectionRememberingForm selectionRememberingForm) : base(false, false)
{
InitializeComponent();
NppFormHelper.RegisterFormIfModeless(this, false);
this.selectionRememberingForm = selectionRememberingForm;
selectionRememberingForm.AddOwnedForm(this);
FormStyle.ApplyStyle(this, Main.settings.use_npp_styling);
comboBox1.SelectedIndex = 0;
DataGridViewRow row = new DataGridViewRow();
row.CreateCells(dataGridView1);
Expand Down
48 changes: 48 additions & 0 deletions NppCSharpPluginPack/Forms/FormBase.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

173 changes: 173 additions & 0 deletions NppCSharpPluginPack/Forms/FormBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
using Kbg.NppPluginNET;
using Kbg.NppPluginNET.PluginInfrastructure;
using NppDemo.Utils;
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace NppDemo.Forms
{
public partial class FormBase : Form
{
/// <summary>
/// if true, this blocks the parent application until closed.<br></br>
/// THIS IS ONLY TRUE OF POP-UP DIALOGS.
/// </summary>
public bool IsModal { get; private set; }
/// <summary>
/// if true, this form's default appearance is docked (attached) to the left, right, bottom, or top of the Notepad++ window.
/// </summary>
public bool IsDocking { get; private set; }
/// <summary>
/// indicates whether the form became visible for the first time<br></br>
/// this is an unprincipled hack to deal with weirdness surrounding the opening of docking forms<br></br>
/// since the Load and Shown events are suppressed on docking form startup.
/// </summary>
private bool IsLoaded = false;

private static Win32.WindowLongGetter _wndLongGetter;
private static Win32.WindowLongSetter _wndLongSetter;

/// <summary>
/// superclass of all forms in the application.<br></br>
/// Implements many useful handlers, and deals with some weird behaviors induced by interoperating with Notepad++.
/// </summary>
/// <param name="isModal">if true, this blocks the parent application until closed. THIS IS ONLY TRUE OF POP-UP DIALOGS</param>
/// <param name="isDocking">if true, this form's default appearance is docked (attached) to the left, right, bottom, or top of the Notepad++ window.</param>
public FormBase(bool isModal, bool isDocking)
{
InitializeComponent();
IsModal = isModal;
IsDocking = isDocking;
NppFormHelper.RegisterFormIfModeless(this, isModal);
if (IsDocking)
{
if (Marshal.SizeOf(typeof(IntPtr)) == 8) // we are 64-bit
{
_wndLongGetter = Win32.GetWindowLongPtr;
_wndLongSetter = Win32.SetWindowLongPtr;
}
else // we are 32-bit
{
_wndLongGetter = Win32.GetWindowLong;
_wndLongSetter = Win32.SetWindowLong;
}
}
}

/// <summary>
/// this is called every time the form's visibility changes,
/// but it only does anything once, before the form is loaded for the first time.<br></br>
/// This adds KeyUp, KeyDown, and KeyPress event handlers to all controls according to the recommendations in NppFormHelper.<br></br>
/// It also styles the form using FormStyle.ApplyStyle
/// </summary>
private void FormBase_VisibleChanged(object sender, EventArgs e)
{
if (IsLoaded || !Visible)
return;
IsLoaded = true;
// we can't put this in the base constructor
// because it must be called *after* the subclass constructor adds all child controls
// and the base constructor must be called first (that's just how C# works)
AddKeyUpDownPressHandlers(this);
FormStyle.ApplyStyle(this, Main.settings.use_npp_styling);
}

/// <summary>
/// This adds KeyUp, KeyDown, and KeyPress event handlers to all controls according to the recommendations in NppFormHelper.
/// </summary>
/// <param name="ctrl"></param>
private void AddKeyUpDownPressHandlers(Control ctrl = null)
{
if (ctrl is null)
ctrl = this;
ctrl.KeyUp += (sender, e) => NppFormHelper.GenericKeyUpHandler(this, sender, e, IsModal);
if (ctrl is TextBox tb)
tb.KeyPress += NppFormHelper.TextBoxKeyPressHandler;
else
ctrl.KeyDown += NppFormHelper.GenericKeyDownHandler;
if (ctrl.HasChildren)
{
foreach (Control child in ctrl.Controls)
AddKeyUpDownPressHandlers(child);
}
}

[Obsolete("Designer only", true)]
public FormBase()
{
// this only exists to make the Visual Studio Windows Forms designer happy
}

private void FormBase_KeyUp(object sender, KeyEventArgs e)
{
NppFormHelper.GenericKeyUpHandler(this, sender, e, IsModal);
}

private void FormBase_KeyDown(object sender, KeyEventArgs e)
{
NppFormHelper.GenericKeyDownHandler(sender, e);
}

/// <summary>
/// suppress the default response to the Tab key
/// </summary>
protected override bool ProcessDialogKey(Keys keyData)
{
if (keyData.HasFlag(Keys.Tab)) // this covers Tab with or without modifiers
return true;
return base.ProcessDialogKey(keyData);
}


/// <summary>
/// this fixes a bug where Notepad++ can hang in the following situation:<br></br>
/// 1. you are in a docking form<br></br>
/// 2. you click a button that is a child of another control (e.g., a GroupBox)<br></br>
/// 3. that button would cause a new form to appear<br></br>
/// see https://github.com/BdR76/CSVLint/pull/88/commits
/// </summary>
protected override void WndProc(ref Message m)
{
if (IsDocking)
{
switch (m.Msg)
{
case Win32.WM_NOTIFY:
var nmdr = (Win32.TagNMHDR)Marshal.PtrToStructure(m.LParam, typeof(Win32.TagNMHDR));
if (nmdr.hwndFrom == PluginBase.nppData._nppHandle)
{
switch ((DockMgrMsg)(nmdr.code & 0xFFFFU))
{
case DockMgrMsg.DMN_DOCK: // we are being docked
break;
case DockMgrMsg.DMN_FLOAT: // we are being _un_docked
RemoveControlParent(this);
break;
case DockMgrMsg.DMN_CLOSE: // we are being closed
break;
}
}
break;
}
}
base.WndProc(ref m);
}

private void RemoveControlParent(Control parent)
{
if (parent.HasChildren)
{
long extAttrs = (long)_wndLongGetter(parent.Handle, Win32.GWL_EXSTYLE);
if (Win32.WS_EX_CONTROLPARENT == (extAttrs & Win32.WS_EX_CONTROLPARENT))
{
_wndLongSetter(parent.Handle, Win32.GWL_EXSTYLE, new IntPtr(extAttrs & ~Win32.WS_EX_CONTROLPARENT));
}
foreach (Control c in parent.Controls)
{
RemoveControlParent(c);
}
}
}
}
}
120 changes: 120 additions & 0 deletions NppCSharpPluginPack/Forms/FormBase.resx
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>
Loading

0 comments on commit 74862af

Please sign in to comment.