1
1
// NPP plugin platform for .Net v0.94.00 by Kasper B. Graversen etc.
2
2
using System ;
3
+ using System . Drawing ;
4
+ using System . Runtime . InteropServices ;
3
5
using System . Text ;
4
6
using NppPluginNET . PluginInfrastructure ;
5
7
@@ -9,9 +11,14 @@ public interface INotepadPPGateway
9
11
{
10
12
void FileNew ( ) ;
11
13
14
+ void AddToolbarIcon ( int funcItemsIndex , toolbarIcons icon ) ;
15
+ void AddToolbarIcon ( int funcItemsIndex , Bitmap icon ) ;
16
+ string GetNppPath ( ) ;
17
+ string GetPluginConfigPath ( ) ;
12
18
string GetCurrentFilePath ( ) ;
13
19
unsafe string GetFilePath ( int bufferId ) ;
14
20
void SetCurrentLanguage ( LangType language ) ;
21
+ bool OpenFile ( string path ) ;
15
22
}
16
23
17
24
/// <summary>
@@ -27,16 +34,73 @@ public void FileNew()
27
34
Win32 . SendMessage ( PluginBase . nppData . _nppHandle , ( uint ) NppMsg . NPPM_MENUCOMMAND , Unused , NppMenuCmd . IDM_FILE_NEW ) ;
28
35
}
29
36
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
+
30
59
/// <summary>
31
60
/// Gets the path of the current document.
32
61
/// </summary>
33
62
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 )
34
74
{
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 ( ) ;
38
83
}
39
84
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
+
40
104
/// <summary>
41
105
/// Gets the path of the current document.
42
106
/// </summary>
0 commit comments