Skip to content

Commit 0b22db4

Browse files
committed
Update N++ plugin platform yet again (sigh)
1 parent 12961c8 commit 0b22db4

15 files changed

Lines changed: 173 additions & 73 deletions

CodeStats/CodeStatsPackage.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class CodeStatsPackage
2626

2727
static int idMyDlg = -1;
2828
static Bitmap tbBmp = Properties.Resources.CodeStats;
29-
29+
3030
static ConfigFile _CodeStatsConfigFile;
3131
public static CodeStats.Forms.SettingsForm _settingsForm;
3232
public static CodeStats.Forms.ApiKeyForm _apikeyForm; // should be null if not needed
@@ -70,7 +70,7 @@ class CodeStatsPackage
7070

7171
internal static void CommandMenuInit()
7272
{
73-
73+
7474
// must add menu item in foreground thread
7575
PluginBase.SetCommand(0, "Code::Stats settings", SettingsPopup, new ShortcutKey(false, false, false, Keys.None));
7676
idMyDlg = 0;
@@ -312,8 +312,8 @@ public static void OnNotification(ScNotification notification)
312312
Logger.Info("1. File: " + GetCurrentFile() + ", char: " + notification.character + ", nppstarted: " + nppStarted.ToString() + ", flags: " + notification.ModificationType.ToString("X"));
313313
}*/ // Too unstable - 0x2012 (ModificationType) might be used for deleting of selected text, but it's still nowhere near good
314314

315-
// TODO: Could use SCN_MODIFIED perhaps, but check for file status with NPPM_READY (don't count before this one),
316-
// NPPM_FILE_BEFORE_LOAD, (NPPM_FILE_LOAD_FAILED), NPPM_FILE_OPENED (then start counting?), NPPM_FILEBEFORECLOSE [http://docs.notepad-plus-plus.org/index.php/Messages_And_Notifications]
315+
// TODO: Could use SCN_MODIFIED perhaps, but check for file status with NPPM_READY (don't count before this one),
316+
// NPPM_FILE_BEFORE_LOAD, (NPPM_FILE_LOAD_FAILED), NPPM_FILE_OPENED (then start counting?), NPPM_FILEBEFORECLOSE [http://docs.notepad-plus-plus.org/index.php/Messages_And_Notifications]
317317

318318
if (notification.Header.Code == (uint)SciMsg.SCN_CHARADDED) // our best bet
319319
{
@@ -396,7 +396,7 @@ private static void ProcessPulses(object sender, ElapsedEventArgs e)
396396
{
397397
pulseProcessor = ProcessPulses(pulseProcessor_tokensource);
398398
}
399-
catch (OperationCanceledException) {}
399+
catch (OperationCanceledException) { }
400400
}
401401
//ProcessPulses();
402402
}
@@ -729,7 +729,8 @@ public static void GetSettings(bool skipRead = false)
729729
private static void PromptApiKey()
730730
{
731731
Logger.Info("Please input your API token into the Code::Stats window.");
732-
/*var form*/_apikeyForm = new CodeStats.Forms.ApiKeyForm();
732+
/*var form*/
733+
_apikeyForm = new CodeStats.Forms.ApiKeyForm();
733734
_apikeyForm.ShowDialog();
734735
}
735736

@@ -883,6 +884,6 @@ internal static void PluginCleanUp()
883884
}
884885
}
885886
}
886-
887+
887888
}
888889
}

CodeStats/PluginInfrastructure/ClikeStringArray.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,4 @@ public void Dispose()
7474
Dispose();
7575
}
7676
}
77-
}
77+
}

CodeStats/PluginInfrastructure/DllExport/DllExportAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ public DllExportAttribute(string exportName, CallingConvention callingConvention
2626

2727
public string ExportName { get; set; }
2828
}
29-
}
29+
}

CodeStats/PluginInfrastructure/GatewayDomain.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// NPP plugin platform for .Net v0.94.00 by Kasper B. Graversen etc.
22
using System;
33
using System.Runtime.InteropServices;
4-
using System.Text;
54

65
namespace Kbg.NppPluginNET.PluginInfrastructure
76
{
@@ -189,9 +188,9 @@ public int Value
189188
[StructLayout(LayoutKind.Sequential)]
190189
public struct CharacterRange
191190
{
192-
public CharacterRange(int cpmin, int cpmax) { cpMin = cpmin; cpMax = cpmax; }
193-
public int cpMin;
194-
public int cpMax;
191+
public CharacterRange(int cpmin, int cpmax) { cpMin = new IntPtr(cpmin); cpMax = new IntPtr(cpmax); }
192+
public IntPtr cpMin;
193+
public IntPtr cpMax;
195194
}
196195

197196
public class Cells
@@ -218,8 +217,8 @@ public TextRange(CharacterRange chrRange, int stringCapacity)
218217
}
219218
public TextRange(int cpmin, int cpmax, int stringCapacity)
220219
{
221-
_sciTextRange.chrg.cpMin = cpmin;
222-
_sciTextRange.chrg.cpMax = cpmax;
220+
_sciTextRange.chrg.cpMin = new IntPtr(cpmin);
221+
_sciTextRange.chrg.cpMax = new IntPtr(cpmax);
223222
_sciTextRange.lpstrText = Marshal.AllocHGlobal(stringCapacity);
224223
}
225224

CodeStats/PluginInfrastructure/MenuCmdID_h.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public enum NppMenuCmd : uint
2020
IDM_FILE_SAVE = (IDM_FILE + 6),
2121
IDM_FILE_SAVEALL = (IDM_FILE + 7),
2222
IDM_FILE_SAVEAS = (IDM_FILE + 8),
23-
//IDM_FILE_ASIAN_LANG = (IDM_FILE + 9),
23+
//IDM_FILE_ASIAN_LANG = (IDM_FILE + 9),
2424
IDM_FILE_PRINT = (IDM_FILE + 10),
2525
IDM_FILE_PRINTNOW = 1001,
2626
IDM_FILE_EXIT = (IDM_FILE + 11),

CodeStats/PluginInfrastructure/Msgs_h.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,39 @@ public enum NppMsg : uint
527527
/// </summary>
528528
NPPM_GETPLUGINHOMEPATH = Constants.NPPMSG + 97,
529529

530+
/// <summary>
531+
/// INT NPPM_GETSETTINGSCLOUDPATH(size_t strLen, TCHAR *settingsOnCloudPath)
532+
/// Get settings on cloud path. It's useful if plugins want to store its settings on Cloud, if this path is set.
533+
/// Returns the number of TCHAR copied/to copy. If the return value is 0, then this path is not set, or the "strLen" is not enough to copy the path.
534+
/// Users should call it with settingsCloudPath be NULL to get the required number of TCHAR (not including the terminating nul character),
535+
/// allocate settingsCloudPath buffer with the return value + 1, then call it again to get the path.
536+
/// </summary>
537+
NPPM_GETSETTINGSONCLOUDPATH = Constants.NPPMSG + 98,
538+
539+
/// <summary>
540+
/// BOOL NPPM_SETLINENUMBERWIDTHMODE(0, INT widthMode)
541+
/// Set line number margin width in dynamic width mode (LINENUMWIDTH_DYNAMIC) or constant width mode (LINENUMWIDTH_CONSTANT)
542+
/// It may help some plugins to disable non-dynamic line number margins width to have a smoothly visual effect while vertical scrolling the content in Notepad++
543+
/// If calling is successful return TRUE, otherwise return FALSE.
544+
/// </summary>
545+
NPPM_SETLINENUMBERWIDTHMODE = Constants.NPPMSG + 99,
546+
LINENUMWIDTH_DYNAMIC = 0,
547+
LINENUMWIDTH_CONSTANT = 1,
548+
549+
/// <summary>
550+
/// INT NPPM_GETLINENUMBERWIDTHMODE(0, 0)
551+
/// Get line number margin width in dynamic width mode (LINENUMWIDTH_DYNAMIC) or constant width mode (LINENUMWIDTH_CONSTANT)
552+
/// </summary>
553+
NPPM_GETLINENUMBERWIDTHMODE = Constants.NPPMSG + 100,
554+
555+
/// <summary>
556+
/// VOID NPPM_ADDTOOLBARICON_FORDARKMODE(UINT funcItem[X]._cmdID, toolbarIconsWithDarkMode iconHandles)
557+
/// Use NPPM_ADDTOOLBARICON_FORDARKMODE instead obsolete NPPM_ADDTOOLBARICON which doesn't support the dark mode
558+
/// 2 formats / 3 icons are needed: 1 * BMP + 2 * ICO
559+
/// 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
560+
/// </summary>
561+
NPPM_ADDTOOLBARICON_FORDARKMODE = Constants.NPPMSG + 101,
562+
530563
RUNCOMMAND_USER = Constants.WM_USER + 3000,
531564
NPPM_GETFULLCURRENTPATH = RUNCOMMAND_USER + FULL_CURRENT_PATH,
532565
NPPM_GETCURRENTDIRECTORY = RUNCOMMAND_USER + CURRENT_DIRECTORY,

CodeStats/PluginInfrastructure/NotepadPPGateway.cs

Lines changed: 95 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// NPP plugin platform for .Net v0.94.00 by Kasper B. Graversen etc.
22
using System;
3+
using System.Drawing;
4+
using System.Runtime.InteropServices;
35
using System.Text;
46
using NppPluginNET.PluginInfrastructure;
57

@@ -9,12 +11,18 @@ public interface INotepadPPGateway
911
{
1012
void FileNew();
1113

14+
void AddToolbarIcon(int funcItemsIndex, toolbarIcons icon);
15+
void AddToolbarIcon(int funcItemsIndex, Bitmap icon);
16+
string GetNppPath();
17+
string GetPluginConfigPath();
1218
string GetCurrentFilePath();
1319
unsafe string GetFilePath(int bufferId);
1420
void SetCurrentLanguage(LangType language);
15-
string GetCurrentFileName();
16-
string GetCurrentDirectory();
17-
}
21+
bool OpenFile(string path);
22+
23+
string GetCurrentFileName();
24+
string GetCurrentDirectory();
25+
}
1826

1927
/// <summary>
2028
/// This class holds helpers for sending messages defined in the Msgs_h.cs file. It is at the moment
@@ -29,16 +37,77 @@ public void FileNew()
2937
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint) NppMsg.NPPM_MENUCOMMAND, Unused, NppMenuCmd.IDM_FILE_NEW);
3038
}
3139

40+
public void AddToolbarIcon(int funcItemsIndex, toolbarIcons icon)
41+
{
42+
IntPtr pTbIcons = Marshal.AllocHGlobal(Marshal.SizeOf(icon));
43+
try {
44+
Marshal.StructureToPtr(icon, pTbIcons, false);
45+
_ = Win32.SendMessage(
46+
PluginBase.nppData._nppHandle,
47+
(uint) NppMsg.NPPM_ADDTOOLBARICON,
48+
PluginBase._funcItems.Items[funcItemsIndex]._cmdID,
49+
pTbIcons);
50+
} finally {
51+
Marshal.FreeHGlobal(pTbIcons);
52+
}
53+
}
54+
55+
public void AddToolbarIcon(int funcItemsIndex, Bitmap icon)
56+
{
57+
var tbi = new toolbarIcons();
58+
tbi.hToolbarBmp = icon.GetHbitmap();
59+
AddToolbarIcon(funcItemsIndex, tbi);
60+
}
61+
3262
/// <summary>
3363
/// Gets the path of the current document.
3464
/// </summary>
3565
public string GetCurrentFilePath()
3666
{
3767
var path = new StringBuilder(2000);
38-
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint) NppMsg.NPPM_GETFULLCURRENTPATH, 0, path);
68+
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_GETFULLCURRENTPATH, 0, path);
3969
return path.ToString();
4070
}
4171

72+
/// <summary>
73+
/// This method incapsulates a common pattern in the Notepad++ API: when
74+
/// you need to retrieve a string, you can first query the buffer size.
75+
/// This method queries the necessary buffer size, allocates the temporary
76+
/// memory, then returns the string retrieved through that buffer.
77+
/// </summary>
78+
/// <param name="message">Message ID of the data string to query.</param>
79+
/// <returns>String returned by Notepad++.</returns>
80+
public string GetString(NppMsg message)
81+
{
82+
int len = Win32.SendMessage(
83+
PluginBase.nppData._nppHandle,
84+
(uint) message, Unused, Unused).ToInt32()
85+
+ 1;
86+
var res = new StringBuilder(len);
87+
_ = Win32.SendMessage(
88+
PluginBase.nppData._nppHandle, (uint) message, len, res);
89+
return res.ToString();
90+
}
91+
92+
/// <returns>The path to the Notepad++ executable.</returns>
93+
public string GetNppPath()
94+
=> GetString(NppMsg.NPPM_GETNPPDIRECTORY);
95+
96+
/// <returns>The path to the Config folder for plugins.</returns>
97+
public string GetPluginConfigPath()
98+
=> GetString(NppMsg.NPPM_GETPLUGINSCONFIGDIR);
99+
100+
/// <summary>
101+
/// Open a file for editing in Notepad++, pretty much like using the app's
102+
/// File - Open menu.
103+
/// </summary>
104+
/// <param name="path">The path to the file to open.</param>
105+
/// <returns>True on success.</returns>
106+
public bool OpenFile(string path)
107+
=> Win32.SendMessage(
108+
PluginBase.nppData._nppHandle, (uint) NppMsg.NPPM_DOOPEN, Unused, path).ToInt32()
109+
!= 0;
110+
42111
/// <summary>
43112
/// Gets the path of the current document.
44113
/// </summary>
@@ -54,28 +123,28 @@ public void SetCurrentLanguage(LangType language)
54123
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint) NppMsg.NPPM_SETCURRENTLANGTYPE, Unused, (int) language);
55124
}
56125

57-
/// <summary>
58-
/// Returns the response from NPPM_GETCURRENTDIRECTORY
59-
/// </summary>
60-
/// <returns></returns>
61-
public string GetCurrentDirectory()
62-
{
63-
var path = new StringBuilder(Win32.MAX_PATH);
64-
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_GETCURRENTDIRECTORY, 0, path);
65-
return path.ToString();
66-
}
67-
68-
/// <summary>
69-
/// Returns the response from NPPM_GETFILENAME
70-
/// </summary>
71-
/// <returns></returns>
72-
public string GetCurrentFileName()
73-
{
74-
var fileName = new StringBuilder(Win32.MAX_PATH);
75-
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_GETFILENAME, 0, fileName);
76-
return fileName.ToString();
77-
}
78-
}
126+
/// <summary>
127+
/// Returns the response from NPPM_GETCURRENTDIRECTORY
128+
/// </summary>
129+
/// <returns></returns>
130+
public string GetCurrentDirectory()
131+
{
132+
var path = new StringBuilder(Win32.MAX_PATH);
133+
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_GETCURRENTDIRECTORY, 0, path);
134+
return path.ToString();
135+
}
136+
137+
/// <summary>
138+
/// Returns the response from NPPM_GETFILENAME
139+
/// </summary>
140+
/// <returns></returns>
141+
public string GetCurrentFileName()
142+
{
143+
var fileName = new StringBuilder(Win32.MAX_PATH);
144+
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_GETFILENAME, 0, fileName);
145+
return fileName.ToString();
146+
}
147+
}
79148

80149
/// <summary>
81150
/// This class holds helpers for sending messages defined in the Resource_h.cs file. It is at the moment

CodeStats/PluginInfrastructure/NppPluginNETBase.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ internal static void SetCommand(int index, string commandName, NppFuncItemDelega
1212
{
1313
SetCommand(index, commandName, functionPointer, new ShortcutKey(), false);
1414
}
15-
15+
1616
internal static void SetCommand(int index, string commandName, NppFuncItemDelegate functionPointer, ShortcutKey shortcut)
1717
{
1818
SetCommand(index, commandName, functionPointer, shortcut, false);
1919
}
20-
20+
2121
internal static void SetCommand(int index, string commandName, NppFuncItemDelegate functionPointer, bool checkOnInit)
2222
{
2323
SetCommand(index, commandName, functionPointer, new ShortcutKey(), checkOnInit);
2424
}
25-
25+
2626
internal static void SetCommand(int index, string commandName, NppFuncItemDelegate functionPointer, ShortcutKey shortcut, bool checkOnInit)
2727
{
2828
FuncItem funcItem = new FuncItem();
@@ -36,6 +36,15 @@ internal static void SetCommand(int index, string commandName, NppFuncItemDelega
3636
_funcItems.Add(funcItem);
3737
}
3838

39+
// menuitem with checkmark, toggle visible checkmark on/off
40+
internal static void CheckMenuItemToggle(int idx, ref bool value)
41+
{
42+
// toggle value
43+
value = !value;
44+
45+
Win32.CheckMenuItem(Win32.GetMenu(nppData._nppHandle), _funcItems.Items[idx]._cmdID, Win32.MF_BYCOMMAND | (value ? Win32.MF_CHECKED : Win32.MF_UNCHECKED));
46+
}
47+
3948
internal static IntPtr GetCurrentScintilla()
4049
{
4150
int curScintilla;

CodeStats/PluginInfrastructure/NppPluginNETHelper.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ public enum DockMgrMsg : uint
185185
[StructLayout(LayoutKind.Sequential)]
186186
public struct toolbarIcons
187187
{
188-
public IntPtr hToolbarBmp;
189-
public IntPtr hToolbarIcon;
188+
public IntPtr hToolbarBmp; // standard icon (color)
189+
public IntPtr hToolbarIcon; // Fluent UI icon (black)
190+
public IntPtr hToolbarIconDarkMode; // Fluent UI icon (white)
190191
}
191192
}

CodeStats/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)