This repository was archived by the owner on Jan 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathNotepadPPGateway.cs
More file actions
137 lines (122 loc) · 4.26 KB
/
NotepadPPGateway.cs
File metadata and controls
137 lines (122 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// NPP plugin platform for .Net v0.94.00 by Kasper B. Graversen etc.
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using NppPluginNET.PluginInfrastructure;
namespace Kbg.NppPluginNET.PluginInfrastructure
{
public interface INotepadPPGateway
{
void FileNew();
void AddToolbarIcon(int funcItemsIndex, toolbarIcons icon);
void AddToolbarIcon(int funcItemsIndex, Bitmap icon);
string GetNppPath();
string GetPluginConfigPath();
string GetCurrentFilePath();
unsafe string GetFilePath(IntPtr bufferId);
void SetCurrentLanguage(LangType language);
bool OpenFile(string path);
}
/// <summary>
/// This class holds helpers for sending messages defined in the Msgs_h.cs file. It is at the moment
/// incomplete. Please help fill in the blanks.
/// </summary>
public class NotepadPPGateway : INotepadPPGateway
{
private const int Unused = 0;
public void FileNew()
{
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint) NppMsg.NPPM_MENUCOMMAND, Unused, NppMenuCmd.IDM_FILE_NEW);
}
public void AddToolbarIcon(int funcItemsIndex, toolbarIcons icon)
{
IntPtr pTbIcons = Marshal.AllocHGlobal(Marshal.SizeOf(icon));
try {
Marshal.StructureToPtr(icon, pTbIcons, false);
_ = Win32.SendMessage(
PluginBase.nppData._nppHandle,
(uint) NppMsg.NPPM_ADDTOOLBARICON_FORDARKMODE,
PluginBase._funcItems.Items[funcItemsIndex]._cmdID,
pTbIcons);
} finally {
Marshal.FreeHGlobal(pTbIcons);
}
}
public void AddToolbarIcon(int funcItemsIndex, Bitmap icon)
{
var tbi = new toolbarIcons();
tbi.hToolbarBmp = icon.GetHbitmap();
AddToolbarIcon(funcItemsIndex, tbi);
}
/// <summary>
/// Gets the path of the current document.
/// </summary>
public string GetCurrentFilePath()
{
var path = new StringBuilder(2000);
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_GETFULLCURRENTPATH, 0, path);
return path.ToString();
}
/// <summary>
/// This method incapsulates a common pattern in the Notepad++ API: when
/// you need to retrieve a string, you can first query the buffer size.
/// This method queries the necessary buffer size, allocates the temporary
/// memory, then returns the string retrieved through that buffer.
/// </summary>
/// <param name="message">Message ID of the data string to query.</param>
/// <returns>String returned by Notepad++.</returns>
public string GetString(NppMsg message)
{
int len = Win32.SendMessage(
PluginBase.nppData._nppHandle,
(uint) message, Unused, Unused).ToInt32()
+ 1;
var res = new StringBuilder(len);
_ = Win32.SendMessage(
PluginBase.nppData._nppHandle, (uint) message, len, res);
return res.ToString();
}
/// <returns>The path to the Notepad++ executable.</returns>
public string GetNppPath()
=> GetString(NppMsg.NPPM_GETNPPDIRECTORY);
/// <returns>The path to the Config folder for plugins.</returns>
public string GetPluginConfigPath()
=> GetString(NppMsg.NPPM_GETPLUGINSCONFIGDIR);
/// <summary>
/// Open a file for editing in Notepad++, pretty much like using the app's
/// File - Open menu.
/// </summary>
/// <param name="path">The path to the file to open.</param>
/// <returns>True on success.</returns>
public bool OpenFile(string path)
=> Win32.SendMessage(
PluginBase.nppData._nppHandle, (uint) NppMsg.NPPM_DOOPEN, Unused, path).ToInt32()
!= 0;
/// <summary>
/// Gets the path of the current document.
/// </summary>
public unsafe string GetFilePath(IntPtr bufferId)
{
var path = new StringBuilder(2000);
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint) NppMsg.NPPM_GETFULLPATHFROMBUFFERID, bufferId, path);
return path.ToString();
}
public void SetCurrentLanguage(LangType language)
{
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint) NppMsg.NPPM_SETCURRENTLANGTYPE, Unused, (int) language);
}
}
/// <summary>
/// This class holds helpers for sending messages defined in the Resource_h.cs file. It is at the moment
/// incomplete. Please help fill in the blanks.
/// </summary>
class NppResource
{
private const int Unused = 0;
public void ClearIndicator()
{
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint) Resource.NPPM_INTERNAL_CLEARINDICATOR, Unused, Unused);
}
}
}