Skip to content
This repository was archived by the owner on Jan 13, 2024. It is now read-only.

Commit 37867f1

Browse files
committed
Add some new methods to INotepadPPGateway
Rewrite the GetCurrentFilePath method using the new GetString method.
1 parent 5861f8e commit 37867f1

File tree

1 file changed

+67
-3
lines changed

1 file changed

+67
-3
lines changed

Visual Studio Project Template C#/PluginInfrastructure/NotepadPPGateway.cs

Lines changed: 67 additions & 3 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,9 +11,14 @@ 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);
21+
bool OpenFile(string path);
1522
}
1623

1724
/// <summary>
@@ -27,16 +34,73 @@ public void FileNew()
2734
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint) NppMsg.NPPM_MENUCOMMAND, Unused, NppMenuCmd.IDM_FILE_NEW);
2835
}
2936

37+
public void AddToolbarIcon(int funcItemsIndex, toolbarIcons icon)
38+
{
39+
IntPtr pTbIcons = Marshal.AllocHGlobal(Marshal.SizeOf(icon));
40+
try {
41+
Marshal.StructureToPtr(icon, pTbIcons, false);
42+
_ = Win32.SendMessage(
43+
PluginBase.nppData._nppHandle,
44+
(uint) NppMsg.NPPM_ADDTOOLBARICON,
45+
PluginBase._funcItems.Items[funcItemsIndex]._cmdID,
46+
pTbIcons);
47+
} finally {
48+
Marshal.FreeHGlobal(pTbIcons);
49+
}
50+
}
51+
52+
public void AddToolbarIcon(int funcItemsIndex, Bitmap icon)
53+
{
54+
var tbi = new toolbarIcons();
55+
tbi.hToolbarBmp = icon.GetHbitmap();
56+
AddToolbarIcon(funcItemsIndex, tbi);
57+
}
58+
3059
/// <summary>
3160
/// Gets the path of the current document.
3261
/// </summary>
3362
public string GetCurrentFilePath()
63+
=> GetString(NppMsg.NPPM_GETFULLCURRENTPATH);
64+
65+
/// <summary>
66+
/// This method incapsulates a common pattern in the Notepad++ API: when
67+
/// you need to retrieve a string, you can first query the buffer size.
68+
/// This method queries the necessary buffer size, allocates the temporary
69+
/// memory, then returns the string retrieved through that buffer.
70+
/// </summary>
71+
/// <param name="message">Message ID of the data string to query.</param>
72+
/// <returns>String returned by Notepad++.</returns>
73+
public string GetString(NppMsg message)
3474
{
35-
var path = new StringBuilder(2000);
36-
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint) NppMsg.NPPM_GETFULLCURRENTPATH, 0, path);
37-
return path.ToString();
75+
int len = Win32.SendMessage(
76+
PluginBase.nppData._nppHandle,
77+
(uint) message, Unused, Unused).ToInt32()
78+
+ 1;
79+
var res = new StringBuilder(len);
80+
_ = Win32.SendMessage(
81+
PluginBase.nppData._nppHandle, (uint) message, len, res);
82+
return res.ToString();
3883
}
3984

85+
/// <returns>The path to the Notepad++ executable.</returns>
86+
public string GetNppPath()
87+
=> GetString(NppMsg.NPPM_GETNPPDIRECTORY);
88+
89+
/// <returns>The path to the Config folder for plugins.</returns>
90+
public string GetPluginConfigPath()
91+
=> GetString(NppMsg.NPPM_GETPLUGINSCONFIGDIR);
92+
93+
/// <summary>
94+
/// Open a file for editing in Notepad++, pretty much like using the app's
95+
/// File - Open menu.
96+
/// </summary>
97+
/// <param name="path">The path to the file to open.</param>
98+
/// <returns>True on success.</returns>
99+
public bool OpenFile(string path)
100+
=> Win32.SendMessage(
101+
PluginBase.nppData._nppHandle, (uint) NppMsg.NPPM_DOOPEN, Unused, path).ToInt32()
102+
!= 0;
103+
40104
/// <summary>
41105
/// Gets the path of the current document.
42106
/// </summary>

0 commit comments

Comments
 (0)