Skip to content

Commit cb25153

Browse files
author
Sébastien Geiser
committed
Toolbar Icon for darkmode for Npp >= 8
1 parent 3b7f385 commit cb25153

File tree

5 files changed

+74
-27
lines changed

5 files changed

+74
-27
lines changed

CSharpRegexTools4Npp/Main.cs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,34 @@ internal static void CommandMenuInit()
7777

7878
internal static void SetToolBarIcon()
7979
{
80-
toolbarIcons tbIcons = new toolbarIcons
80+
if (!string.IsNullOrEmpty(BNpp.NotepadPP.NppBinVersion)
81+
&& int.TryParse(BNpp.NotepadPP.NppBinVersion.Split('.')[0], out int majorVersion)
82+
&& majorVersion >= 8)
8183
{
82-
hToolbarBmp = tbBmp.GetHbitmap()
83-
};
84+
toolbarIconsWithDarkMode tbIcons = new toolbarIconsWithDarkMode
85+
{
86+
hToolbarBmp = tbBmp.GetHbitmap(),
87+
hToolbarIcon = tbBmp.GetHicon(),
88+
hToolbarIconDarkMode = tbBmp.GetHicon()
89+
};
90+
91+
IntPtr pTbIcons = Marshal.AllocHGlobal(Marshal.SizeOf(tbIcons));
92+
Marshal.StructureToPtr(tbIcons, pTbIcons, false);
93+
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_ADDTOOLBARICON_FORDARKMODE, PluginBase._funcItems.Items[idMyDlg]._cmdID, pTbIcons);
94+
Marshal.FreeHGlobal(pTbIcons);
95+
}
96+
else
97+
{
98+
toolbarIcons tbIcons = new toolbarIcons
99+
{
100+
hToolbarBmp = tbBmp.GetHbitmap()
101+
};
84102

85-
IntPtr pTbIcons = Marshal.AllocHGlobal(Marshal.SizeOf(tbIcons));
86-
Marshal.StructureToPtr(tbIcons, pTbIcons, false);
87-
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_ADDTOOLBARICON, PluginBase._funcItems.Items[idMyDlg]._cmdID, pTbIcons);
88-
Marshal.FreeHGlobal(pTbIcons);
103+
IntPtr pTbIcons = Marshal.AllocHGlobal(Marshal.SizeOf(tbIcons));
104+
Marshal.StructureToPtr(tbIcons, pTbIcons, false);
105+
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_ADDTOOLBARICON, PluginBase._funcItems.Items[idMyDlg]._cmdID, pTbIcons);
106+
Marshal.FreeHGlobal(pTbIcons);
107+
}
89108
}
90109

91110
private static async void CheckUpdates(RegExToolDialog dialog)

CSharpRegexTools4Npp/PluginInfrastructure/Msgs_h.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,13 @@ public enum NppMsg : uint
172172
/// </summary>
173173
NPPM_ADDTOOLBARICON = Constants.NPPMSG + 41,
174174

175+
/// <summary>
176+
/// void NPPM_ADDTOOLBARICON_FORDARKMODE(UINT funcItem[X]._cmdID, toolbarIconsWithDarkMode iconHandles)
177+
/// Use NPPM_ADDTOOLBARICON_FORDARKMODE instead obsolete NPPM_ADDTOOLBARICON which doesn't support the dark mode
178+
/// 2 formats / 3 icons are needed: 1 * BMP + 2 * ICO
179+
/// </summary>
180+
NPPM_ADDTOOLBARICON_FORDARKMODE = Constants.NPPMSG + 101,
181+
175182
/// <summary>
176183
///winVer NPPM_GETWINDOWSVERSION(0, 0)
177184
/// </summary>

CSharpRegexTools4Npp/PluginInfrastructure/NotepadPPGateway.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// NPP plugin platform for .Net v0.94.00 by Kasper B. Graversen etc.
22
using System;
33
using System.Collections.Generic;
4+
using System.Diagnostics;
45
using System.IO;
56
using System.Runtime.InteropServices;
67
using System.Text;
@@ -217,6 +218,21 @@ public string NppBinDirectoryPath
217218
}
218219
}
219220

221+
public string NppBinVersion
222+
{
223+
get
224+
{
225+
try
226+
{
227+
return FileVersionInfo.GetVersionInfo(Path.Combine(NppBinDirectoryPath, "notepad++.exe")).FileVersion;
228+
}
229+
catch
230+
{
231+
return null;
232+
}
233+
}
234+
}
235+
220236
/// <summary>
221237
/// Gets the path of the current document.
222238
/// </summary>

CSharpRegexTools4Npp/PluginInfrastructure/NppPluginNETHelper.cs

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -183,28 +183,28 @@ public enum DockMgrMsg : uint
183183
IDC_BTN_CAPTION = 1050,
184184

185185
DMM_MSG = 0x5000,
186-
DMM_CLOSE = (DMM_MSG + 1),
187-
DMM_DOCK = (DMM_MSG + 2),
188-
DMM_FLOAT = (DMM_MSG + 3),
189-
DMM_DOCKALL = (DMM_MSG + 4),
190-
DMM_FLOATALL = (DMM_MSG + 5),
191-
DMM_MOVE = (DMM_MSG + 6),
192-
DMM_UPDATEDISPINFO = (DMM_MSG + 7),
193-
DMM_GETIMAGELIST = (DMM_MSG + 8),
194-
DMM_GETICONPOS = (DMM_MSG + 9),
195-
DMM_DROPDATA = (DMM_MSG + 10),
196-
DMM_MOVE_SPLITTER = (DMM_MSG + 11),
197-
DMM_CANCEL_MOVE = (DMM_MSG + 12),
198-
DMM_LBUTTONUP = (DMM_MSG + 13),
186+
DMM_CLOSE = DMM_MSG + 1,
187+
DMM_DOCK = DMM_MSG + 2,
188+
DMM_FLOAT = DMM_MSG + 3,
189+
DMM_DOCKALL = DMM_MSG + 4,
190+
DMM_FLOATALL = DMM_MSG + 5,
191+
DMM_MOVE = DMM_MSG + 6,
192+
DMM_UPDATEDISPINFO = DMM_MSG + 7,
193+
DMM_GETIMAGELIST = DMM_MSG + 8,
194+
DMM_GETICONPOS = DMM_MSG + 9,
195+
DMM_DROPDATA = DMM_MSG + 10,
196+
DMM_MOVE_SPLITTER = DMM_MSG + 11,
197+
DMM_CANCEL_MOVE = DMM_MSG + 12,
198+
DMM_LBUTTONUP = DMM_MSG + 13,
199199

200200
DMN_FIRST = 1050,
201-
DMN_CLOSE = (DMN_FIRST + 1),
201+
DMN_CLOSE = DMN_FIRST + 1,
202202
//nmhdr.Code = DWORD(DMN_CLOSE, 0));
203203
//nmhdr.hwndFrom = hwndNpp;
204204
//nmhdr.IdFrom = ctrlIdNpp;
205205

206-
DMN_DOCK = (DMN_FIRST + 2),
207-
DMN_FLOAT = (DMN_FIRST + 3)
206+
DMN_DOCK = DMN_FIRST + 2,
207+
DMN_FLOAT = DMN_FIRST + 3
208208
//nmhdr.Code = DWORD(DMN_XXX, int newContainer);
209209
//nmhdr.hwndFrom = hwndNpp;
210210
//nmhdr.IdFrom = ctrlIdNpp;
@@ -216,4 +216,13 @@ public struct toolbarIcons
216216
public IntPtr hToolbarBmp;
217217
public IntPtr hToolbarIcon;
218218
}
219+
220+
// All 3 handles below should be set so the icon will be displayed correctly if toolbar icon sets are changed by users, also in dark mode
221+
[StructLayout(LayoutKind.Sequential)]
222+
public struct toolbarIconsWithDarkMode
223+
{
224+
public IntPtr hToolbarBmp;
225+
public IntPtr hToolbarIcon;
226+
public IntPtr hToolbarIconDarkMode;
227+
}
219228
}

CSharpRegexTools4Npp/PluginInfrastructure/Preference_h.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
// "notepad-plus-plus/scintilla/include/Scintilla.iface"
55
// found at
66
// https://github.com/notepad-plus-plus/notepad-plus-plus/blob/master/scintilla/include/Scintilla.iface
7-
using System;
8-
using System.Collections.Generic;
9-
using System.Linq;
10-
using System.Text;
117

128
namespace NppPluginNET.PluginInfrastructure
139
{

0 commit comments

Comments
 (0)