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

Commit 8682490

Browse files
authored
Merge pull request #76 from alex-ilin/master
Some minor cleanup and a couple of new Npp gateway methods
2 parents c12f4ed + 37867f1 commit 8682490

File tree

8 files changed

+73
-18
lines changed

8 files changed

+73
-18
lines changed

Visual Studio Project Template C#/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+
}

Visual Studio Project Template C#/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+
}

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

Lines changed: 0 additions & 1 deletion
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
{

Visual Studio Project Template C#/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),

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>

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

Lines changed: 3 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();

Visual Studio Project Template C#/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
{

Visual Studio Project Template C#/PluginInfrastructure/resource_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
using Kbg.NppPluginNET.PluginInfrastructure;
128

139
namespace NppPluginNET.PluginInfrastructure

0 commit comments

Comments
 (0)