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 53
/
Copy pathDemo.cs
484 lines (417 loc) · 20.1 KB
/
Demo.cs
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
// NPP plugin platform for .Net v0.91.57 by Kasper B. Graversen etc.
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using Kbg.NppPluginNET.PluginInfrastructure;
using static Kbg.NppPluginNET.PluginInfrastructure.Win32;
namespace Kbg.NppPluginNET
{
/// <summary>
/// Integration layer as the demo app uses the pluginfiles as soft-links files.
/// This is different to normal plugins that would use the project template and get the files directly.
/// </summary>
class Main
{
static internal void CommandMenuInit()
{
Kbg.Demo.Namespace.Main.CommandMenuInit();
}
static internal void PluginCleanUp()
{
Kbg.Demo.Namespace.Main.PluginCleanUp();
}
static internal void SetToolBarIcon()
{
Kbg.Demo.Namespace.Main.SetToolBarIcon();
}
public static void OnNotification(ScNotification notification)
{
if (notification.Header.Code == (uint)SciMsg.SCN_CHARADDED)
{
Kbg.Demo.Namespace.Main.doInsertHtmlCloseTag((char)notification.Character);
}
else if (notification.Header.Code == (uint)NppMsg.NPPN_FILEBEFORECLOSE)
{
Kbg.Demo.Namespace.Main.addFileToFilesClosed(notification.Header.IdFrom);
}
}
internal static string PluginName { get { return Kbg.Demo.Namespace.Main.PluginName; }}
}
}
namespace Kbg.Demo.Namespace
{
class Main
{
#region " Fields "
internal const string PluginName = "NppManagedPluginDemo";
static string iniFilePath = null;
static string sectionName = "Insert Extension";
static string keyName = "doCloseTag";
static bool doCloseTag = false;
static List<string> filesClosedThisSession = new List<string>();
static string sessionFilePath = @"C:\text.session";
static frmGoToLine frmGoToLine = null;
static internal int idFrmGotToLine = -1;
// toolbar icons
static Bitmap tbBmp = Properties.Resources.star;
static Bitmap tbBmp_tbTab = Properties.Resources.star_bmp;
static Icon tbIco = Properties.Resources.star_black_ico;
static Icon tbIcoDM = Properties.Resources.star_white_ico;
static Icon tbIcon = null;
static IScintillaGateway editor = new ScintillaGateway(PluginBase.GetCurrentScintilla());
static INotepadPPGateway notepad = new NotepadPPGateway();
#endregion
#region " Startup/CleanUp "
static internal void CommandMenuInit()
{
// Initialization of your plugin commands
// You should fill your plugins commands here
//
// Firstly we get the parameters from your plugin config file (if any)
//
// get path of plugin configuration
StringBuilder sbIniFilePath = new StringBuilder(Win32.MAX_PATH);
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint) NppMsg.NPPM_GETPLUGINSCONFIGDIR, Win32.MAX_PATH, sbIniFilePath);
iniFilePath = sbIniFilePath.ToString();
// if config path doesn't exist, we create it
if (!Directory.Exists(iniFilePath))
{
Directory.CreateDirectory(iniFilePath);
}
// make your plugin config file full file path name
iniFilePath = Path.Combine(iniFilePath, PluginName + ".ini");
// get the parameter value from plugin config
doCloseTag = (Win32.GetPrivateProfileInt(sectionName, keyName, 0, iniFilePath) != 0);
// with function :
// SetCommand(int index, // zero based number to indicate the order of command
// string commandName, // the command name that you want to see in plugin menu
// NppFuncItemDelegate functionPointer, // the symbol of function (function pointer) associated with this command. The body should be defined below. See Step 4.
// ShortcutKey *shortcut, // optional. Define a shortcut to trigger this command
// bool check0nInit // optional. Make this menu item be checked visually
// );
PluginBase.SetCommand(0, "Hello Notepad++", hello);
PluginBase.SetCommand(1, "Hello (with FX)", helloFX);
PluginBase.SetCommand(2, "What is Notepad++?", WhatIsNpp);
// Here you insert a separator
PluginBase.SetCommand(3, "---", null);
// Shortcut :
// Following makes the command bind to the shortcut Alt-F
PluginBase.SetCommand(4, "Current Full Path", insertCurrentFullPath, new ShortcutKey(false, true, false, Keys.F));
PluginBase.SetCommand(5, "Current File Name", insertCurrentFileName);
PluginBase.SetCommand(6, "Current Directory", insertCurrentDirectory);
PluginBase.SetCommand(7, "Date && Time - short format", insertShortDateTime);
PluginBase.SetCommand(8, "Date && Time - long format", insertLongDateTime);
PluginBase.SetCommand(9, "Close HTML/XML tag automatically", checkInsertHtmlCloseTag, new ShortcutKey(false, true, false, Keys.Q), doCloseTag);
PluginBase.SetCommand(10, "---", null);
PluginBase.SetCommand(11, "Get File Names Demo", getFileNamesDemo);
PluginBase.SetCommand(12, "Get Session File Names Demo", getSessionFileNamesDemo);
PluginBase.SetCommand(13, "Save Current Session Demo", saveCurrentSessionDemo);
PluginBase.SetCommand(14, "---", null);
PluginBase.SetCommand(15, "Dockable Dialog Demo", DockableDlgDemo); idFrmGotToLine = 15;
PluginBase.SetCommand(16, "---", null);
PluginBase.SetCommand(17, "Print Scroll and Row Information", PrintScrollInformation);
PluginBase.SetCommand(18, "Show files closed this session", filesClosedDemo);
PluginBase.SetCommand(19, "Use NanInf class for -inf, inf, nan!!", PrintNanInf);
}
/// <summary>
///
/// </summary>
static void PrintScrollInformation()
{
ScrollInfo scrollInfo = editor.GetScrollInfo(ScrollInfoMask.SIF_RANGE | ScrollInfoMask.SIF_TRACKPOS | ScrollInfoMask.SIF_PAGE, ScrollInfoBar.SB_VERT);
var scrollRatio = (double)scrollInfo.nTrackPos / (scrollInfo.nMax - scrollInfo.nPage);
var scrollPercentage = Math.Min(scrollRatio, 1) * 100;
editor.ReplaceSel($@"The maximum row in the current document was {scrollInfo.nMax+1}.
A maximum of {scrollInfo.nPage} rows is visible at a time.
The current scroll ratio is {Math.Round(scrollPercentage, 2)}%.
");
}
static internal void SetToolBarIcon()
{
// create struct
toolbarIcons tbIcons = new toolbarIcons();
// add bmp icon
tbIcons.hToolbarBmp = tbBmp.GetHbitmap();
tbIcons.hToolbarIcon = tbIco.Handle; // icon with black lines
tbIcons.hToolbarIconDarkMode = tbIcoDM.Handle; // icon with light grey lines
// convert to c++ pointer
IntPtr pTbIcons = Marshal.AllocHGlobal(Marshal.SizeOf(tbIcons));
Marshal.StructureToPtr(tbIcons, pTbIcons, false);
// call Notepad++ api
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_ADDTOOLBARICON_FORDARKMODE, PluginBase._funcItems.Items[idFrmGotToLine]._cmdID, pTbIcons);
// release pointer
Marshal.FreeHGlobal(pTbIcons);
}
static internal void PluginCleanUp()
{
Win32.WritePrivateProfileString(sectionName, keyName, doCloseTag ? "1" : "0", iniFilePath);
}
#endregion
#region " Menu functions "
static void hello()
{
notepad.FileNew();
editor.SetText("Hello, Notepad++...from.NET!");
var rest = editor.GetLine(0);
editor.SetText(rest+rest+rest);
}
static void helloFX()
{
hello();
new Thread(callbackHelloFX).Start();
}
static void callbackHelloFX()
{
int currentZoomLevel = editor.GetZoom();
int i = currentZoomLevel;
for (int j = 0 ; j < 4 ; j++)
{
for ( ; i >= -10; i--)
{
editor.SetZoom(i);
Thread.Sleep(30);
}
Thread.Sleep(100);
for ( ; i <= 20 ; i++)
{
Thread.Sleep(30);
editor.SetZoom(i);
}
Thread.Sleep(100);
}
for ( ; i >= currentZoomLevel ; i--)
{
Thread.Sleep(30);
editor.SetZoom(i);
}
}
static void WhatIsNpp()
{
string text2display = "Notepad++ is a free (as in \"free speech\" and also as in \"free beer\") " +
"source code editor and Notepad replacement that supports several languages.\n" +
"Running in the MS Windows environment, its use is governed by GPL License.\n\n" +
"Based on a powerful editing component Scintilla, Notepad++ is written in C++ and " +
"uses pure Win32 API and STL which ensures a higher execution speed and smaller program size.\n" +
"By optimizing as many routines as possible without losing user friendliness, Notepad++ is trying " +
"to reduce the world carbon dioxide emissions. When using less CPU power, the PC can throttle down " +
"and reduce power consumption, resulting in a greener environment.";
new Thread(new ParameterizedThreadStart(callbackWhatIsNpp)).Start(text2display);
}
static void callbackWhatIsNpp(object data)
{
string text2display = (string)data;
notepad.FileNew();
Random srand = new Random(DateTime.Now.Millisecond);
int rangeMin = 0;
int rangeMax = 250;
for (int i = 0; i < text2display.Length; i++)
{
Thread.Sleep(srand.Next(rangeMin, rangeMax) + 30);
editor.AppendTextAndMoveCursor(text2display[i].ToString());
}
}
static void insertCurrentFullPath()
{
insertCurrentPath(NppMsg.FULL_CURRENT_PATH);
}
static void insertCurrentFileName()
{
insertCurrentPath(NppMsg.FILE_NAME);
}
static void insertCurrentDirectory()
{
insertCurrentPath(NppMsg.CURRENT_DIRECTORY);
}
static void insertCurrentPath(NppMsg which)
{
NppMsg msg = NppMsg.NPPM_GETFULLCURRENTPATH;
if (which == NppMsg.FILE_NAME)
msg = NppMsg.NPPM_GETFILENAME;
else if (which == NppMsg.CURRENT_DIRECTORY)
msg = NppMsg.NPPM_GETCURRENTDIRECTORY;
StringBuilder path = new StringBuilder(Win32.MAX_PATH);
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint) msg, 0, path);
editor.ReplaceSel(path.ToString());
}
static void insertShortDateTime()
{
insertDateTime(false);
}
static void insertLongDateTime()
{
insertDateTime(true);
}
static void insertDateTime(bool longFormat)
{
string dateTime = string.Format("{0} {1}", DateTime.Now.ToShortTimeString(), longFormat ? DateTime.Now.ToLongDateString() : DateTime.Now.ToShortDateString());
editor.ReplaceSel(dateTime);
}
static void checkInsertHtmlCloseTag()
{
PluginBase.CheckMenuItemToggle(9, ref doCloseTag); // 9 = menu item index
}
static Regex regex = new Regex(@"[\._\-:\w]", RegexOptions.Compiled);
static internal void doInsertHtmlCloseTag(char newChar)
{
LangType docType = LangType.L_TEXT;
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint) NppMsg.NPPM_GETCURRENTLANGTYPE, 0, ref docType);
bool isDocTypeHTML = (docType == LangType.L_HTML || docType == LangType.L_XML || docType == LangType.L_PHP);
if (!doCloseTag || !isDocTypeHTML)
return;
if (newChar != '>')
return;
int bufCapacity = 512;
var pos = editor.GetCurrentPos();
int currentPos = pos;
int beginPos = currentPos - (bufCapacity - 1);
int startPos = (beginPos > 0) ? beginPos : 0;
int size = currentPos - startPos;
if (size < 3)
return;
using (TextRange tr = new TextRange(startPos, currentPos, bufCapacity))
{
editor.GetTextRange(tr);
string buf = tr.lpstrText;
if (buf[size - 2] == '/')
return;
int pCur = size - 2;
while ((pCur > 0) && (buf[pCur] != '<') && (buf[pCur] != '>'))
pCur--;
if (buf[pCur] == '<')
{
pCur++;
var insertString = new StringBuilder("</");
while (regex.IsMatch(buf[pCur].ToString()))
{
insertString.Append(buf[pCur]);
pCur++;
}
insertString.Append('>');
if (insertString.Length > 3)
{
editor.BeginUndoAction();
editor.ReplaceSel(insertString.ToString());
editor.SetSel(pos, pos);
editor.EndUndoAction();
}
}
}
}
static internal void addFileToFilesClosed(IntPtr bufClosedId)
{
var bufClosedName = notepad.GetFilePath(bufClosedId);
filesClosedThisSession.Add(bufClosedName);
}
static void filesClosedDemo()
{
var filesClosed = string.Join("\r\n", filesClosedThisSession);
MessageBox.Show($"Files closed this session:\r\n{filesClosed}",
"Files closed this session");
}
static void getFileNamesDemo()
{
int nbFile = (int)Win32.SendMessage(PluginBase.nppData._nppHandle, (uint) NppMsg.NPPM_GETNBOPENFILES, 0, 0);
MessageBox.Show(nbFile.ToString(), "Number of opened files:");
using (ClikeStringArray cStrArray = new ClikeStringArray(nbFile, Win32.MAX_PATH))
{
if (Win32.SendMessage(PluginBase.nppData._nppHandle, (uint) NppMsg.NPPM_GETOPENFILENAMES, cStrArray.NativePointer, nbFile) != IntPtr.Zero)
foreach (string file in cStrArray.ManagedStringsUnicode) MessageBox.Show(file);
}
}
static void getSessionFileNamesDemo()
{
int nbFile = (int)Win32.SendMessage(PluginBase.nppData._nppHandle, (uint) NppMsg.NPPM_GETNBSESSIONFILES, 0, sessionFilePath);
if (nbFile < 1)
{
MessageBox.Show("Please modify \"sessionFilePath\" in \"Demo.cs\" in order to point to a valid session file", "Error");
return;
}
MessageBox.Show(nbFile.ToString(), "Number of session files:");
using (ClikeStringArray cStrArray = new ClikeStringArray(nbFile, Win32.MAX_PATH))
{
if (Win32.SendMessage(PluginBase.nppData._nppHandle, (uint) NppMsg.NPPM_GETSESSIONFILES, cStrArray.NativePointer, sessionFilePath) != IntPtr.Zero)
foreach (string file in cStrArray.ManagedStringsUnicode) MessageBox.Show(file);
}
}
static void saveCurrentSessionDemo()
{
string sessionPath = Marshal.PtrToStringUni(Win32.SendMessage(PluginBase.nppData._nppHandle, (uint) NppMsg.NPPM_SAVECURRENTSESSION, 0, sessionFilePath));
if (!string.IsNullOrEmpty(sessionPath))
MessageBox.Show(sessionPath, "Saved Session File :", MessageBoxButtons.OK);
}
static void DockableDlgDemo()
{
// Dockable Dialog Demo
//
// This demonstration shows you how to do a dockable dialog.
// You can create your own non dockable dialog - in this case you don't nedd this demonstration.
if (frmGoToLine == null)
{
frmGoToLine = new frmGoToLine(editor);
using (Bitmap newBmp = new Bitmap(16, 16))
{
Graphics g = Graphics.FromImage(newBmp);
ColorMap[] colorMap = new ColorMap[1];
colorMap[0] = new ColorMap();
colorMap[0].OldColor = Color.Fuchsia;
colorMap[0].NewColor = Color.FromKnownColor(KnownColor.ButtonFace);
ImageAttributes attr = new ImageAttributes();
attr.SetRemapTable(colorMap);
g.DrawImage(tbBmp_tbTab, new Rectangle(0, 0, 16, 16), 0, 0, 16, 16, GraphicsUnit.Pixel, attr);
tbIcon = Icon.FromHandle(newBmp.GetHicon());
}
NppTbData _nppTbData = new NppTbData();
_nppTbData.hClient = frmGoToLine.Handle;
_nppTbData.pszName = "Go To Line #";
// the dlgDlg should be the index of funcItem where the current function pointer is in
// this case is 15.. so the initial value of funcItem[15]._cmdID - not the updated internal one !
_nppTbData.dlgID = idFrmGotToLine;
// define the default docking behaviour
_nppTbData.uMask = NppTbMsg.DWS_DF_CONT_RIGHT | NppTbMsg.DWS_ICONTAB | NppTbMsg.DWS_ICONBAR;
_nppTbData.hIconTab = (uint)tbIcon.Handle;
_nppTbData.pszModuleName = PluginName;
IntPtr _ptrNppTbData = Marshal.AllocHGlobal(Marshal.SizeOf(_nppTbData));
Marshal.StructureToPtr(_nppTbData, _ptrNppTbData, false);
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint) NppMsg.NPPM_DMMREGASDCKDLG, 0, _ptrNppTbData);
// Following message will toogle both menu item state and toolbar button
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint) NppMsg.NPPM_SETMENUITEMCHECK, PluginBase._funcItems.Items[idFrmGotToLine]._cmdID, 1);
}
else
{
if (!frmGoToLine.Visible)
{
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint) NppMsg.NPPM_DMMSHOW, 0, frmGoToLine.Handle);
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint) NppMsg.NPPM_SETMENUITEMCHECK, PluginBase._funcItems.Items[idFrmGotToLine]._cmdID, 1);
}
else
{
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint) NppMsg.NPPM_DMMHIDE, 0, frmGoToLine.Handle);
Win32.SendMessage(PluginBase.nppData._nppHandle, (uint) NppMsg.NPPM_SETMENUITEMCHECK, PluginBase._funcItems.Items[idFrmGotToLine]._cmdID, 0);
}
}
frmGoToLine.textBox1.Focus();
}
static void PrintNanInf()
{
bool neginf_correct = double.IsNegativeInfinity(NanInf.neginf);
bool inf_correct = double.IsPositiveInfinity(NanInf.inf);
bool nan_correct = double.IsNaN(NanInf.nan);
string naninf = $@"-infinity == NanInf.neginf: {neginf_correct}
infinity == NanInf.inf: {inf_correct}
NaN == NanInf.nan: {nan_correct}
If you want these constants in your plugin, you can find them in the NanInf class in PluginInfrastructure.
DO NOT USE double.PositiveInfinity, double.NegativeInfinity, or double.NaN.
You will get a compiler error if you do.";
notepad.FileNew();
editor.AppendTextAndMoveCursor(naninf);
}
#endregion
}
}