123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381 |
- using System;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Text.RegularExpressions;
- using System.Threading;
- using System.Windows.Forms;
- using Demo.Forms;
- namespace Demo
- {
- partial class PluginBase
- {
- #region " Fields "
- internal const string PluginName = "Offical .Net Demo";
- static string _iniFilePath;
- static readonly string SectionName = "Insert Extension";
- static readonly string KeyName = "doCloseTag";
- static bool _doCloseTag;
- static readonly string SessionFilePath = @"C:\text.session";
- static FrmGoToLine _frmGoToLine;
- internal static int IdFrmGotToLine = -1;
- static readonly Bitmap TbBmp = Properties.Resources.star;
- static readonly Bitmap TbBmpTbTab = Properties.Resources.star_bmp;
- static Icon _tbIcon;
- #endregion
- #region " Startup/CleanUp "
- internal static 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
- var sbIniFilePath = new StringBuilder(Win32.MAX_PATH);
- Win32.SendMessage(NppData._nppHandle, 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
- // );
- SetCommand(0, "Hello Notepad++", Hello);
- SetCommand(1, "Hello (with FX)", HelloFx);
- SetCommand(2, "What is Notepad++?", WhatIsNpp);
- // Here you insert a separator
- SetCommand(3, "---", null);
- // Shortcut :
- // Following makes the command bind to the shortcut Alt-F
- SetCommand(4, "Current Full Path", InsertCurrentFullPath, new ShortcutKey(false, true, false, Keys.F));
- SetCommand(5, "Current File Name", InsertCurrentFileName);
- SetCommand(6, "Current Directory", InsertCurrentDirectory);
- SetCommand(7, "Date && Time - short format", InsertShortDateTime);
- SetCommand(8, "Date && Time - long format", InsertLongDateTime);
- SetCommand(9, "Close HTML/XML tag automatically", CheckInsertHtmlCloseTag, new ShortcutKey(false, true, false, Keys.Q), _doCloseTag);
- SetCommand(10, "---", null);
- SetCommand(11, "Get File Names Demo", GetFileNamesDemo);
- SetCommand(12, "Get Session File Names Demo", GetSessionFileNamesDemo);
- SetCommand(13, "Save Current Session Demo", SaveCurrentSessionDemo);
- SetCommand(14, "---", null);
- SetCommand(15, "Dockable Dialog Demo", DockableDlgDemo); IdFrmGotToLine = 15;
- }
- internal static void SetToolBarIcon()
- {
- var tbIcons = new ToolbarIcons();
- tbIcons.hToolbarBmp = TbBmp.GetHbitmap();
- var pTbIcons = Marshal.AllocHGlobal(Marshal.SizeOf(tbIcons));
- Marshal.StructureToPtr(tbIcons, pTbIcons, false);
- Win32.SendMessage(NppData._nppHandle, NppMsg.NPPM_ADDTOOLBARICON, FuncItems.Items[IdFrmGotToLine]._cmdID, pTbIcons);
- Marshal.FreeHGlobal(pTbIcons);
- }
- internal static void PluginCleanUp()
- {
- Win32.WritePrivateProfileString(SectionName, KeyName, _doCloseTag ? "1" : "0", _iniFilePath);
- }
- #endregion
- #region " Menu functions "
- static void Hello()
- {
- // Open a new document
- Win32.SendMessage(NppData._nppHandle, NppMsg.NPPM_MENUCOMMAND, 0, NppMenuCmd.IDM_FILE_NEW);
- // Say hello now :
- // Scintilla control has no Unicode mode, so we use ANSI here (marshalled as ANSI by default)
- Win32.SendMessage(GetCurrentScintilla(), SciMsg.SCI_SETTEXT, 0, "Hello, Notepad++... from .NET!");
- }
- static void HelloFx()
- {
- Hello();
- new Thread(CallbackHelloFx).Start();
- }
- static void CallbackHelloFx()
- {
- var curScintilla = GetCurrentScintilla();
- var currentZoomLevel = (int)Win32.SendMessage(curScintilla, SciMsg.SCI_GETZOOM, 0, 0);
- var i = currentZoomLevel;
- for (var j = 0 ; j < 4 ; j++)
- {
- for ( ; i >= -10; i--)
- {
- Win32.SendMessage(curScintilla, SciMsg.SCI_SETZOOM, i, 0);
- Thread.Sleep(30);
- }
- Thread.Sleep(100);
- for ( ; i <= 20 ; i++)
- {
- Thread.Sleep(30);
- Win32.SendMessage(curScintilla, SciMsg.SCI_SETZOOM, i, 0);
- }
- Thread.Sleep(100);
- }
- for ( ; i >= currentZoomLevel ; i--)
- {
- Thread.Sleep(30);
- Win32.SendMessage(curScintilla, SciMsg.SCI_SETZOOM, i, 0);
- }
- }
- static void WhatIsNpp()
- {
- var 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(CallbackWhatIsNpp).Start(text2Display);
- }
- static void CallbackWhatIsNpp(object data)
- {
- var text2Display = (string)data;
- // Open a new document
- Win32.SendMessage(NppData._nppHandle, NppMsg.NPPM_MENUCOMMAND, 0, NppMenuCmd.IDM_FILE_NEW);
- // Get the current scintilla
- var curScintilla = GetCurrentScintilla();
- var srand = new Random(DateTime.Now.Millisecond);
- var rangeMin = 0;
- var rangeMax = 250;
- for (var i = 0; i < text2Display.Length; i++)
- {
- var charToShow = new StringBuilder(text2Display[i].ToString());
- var ranNum = srand.Next(rangeMin, rangeMax);
- Thread.Sleep(ranNum + 30);
- Win32.SendMessage(curScintilla, SciMsg.SCI_APPENDTEXT, 1, charToShow);
- Win32.SendMessage(curScintilla, SciMsg.SCI_GOTOPOS, (int)Win32.SendMessage(curScintilla, SciMsg.SCI_GETLENGTH, 0, 0), 0);
- }
- }
- 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)
- {
- var msg = NppMsg.NPPM_GETFULLCURRENTPATH;
- if (which == NppMsg.FILE_NAME)
- msg = NppMsg.NPPM_GETFILENAME;
- else if (which == NppMsg.CURRENT_DIRECTORY)
- msg = NppMsg.NPPM_GETCURRENTDIRECTORY;
- var path = new StringBuilder(Win32.MAX_PATH);
- Win32.SendMessage(NppData._nppHandle, msg, 0, path);
- Win32.SendMessage(GetCurrentScintilla(), SciMsg.SCI_REPLACESEL, 0, path);
- }
- static void InsertShortDateTime()
- {
- InsertDateTime(false);
- }
- static void InsertLongDateTime()
- {
- InsertDateTime(true);
- }
- static void InsertDateTime(bool longFormat)
- {
- var dateTime = string.Format("{0} {1}",
- DateTime.Now.ToShortTimeString(),
- longFormat ? DateTime.Now.ToLongDateString() : DateTime.Now.ToShortDateString());
- Win32.SendMessage(GetCurrentScintilla(), SciMsg.SCI_REPLACESEL, 0, dateTime);
- }
- static void CheckInsertHtmlCloseTag()
- {
- _doCloseTag = !_doCloseTag;
- var i = Win32.CheckMenuItem(Win32.GetMenu(NppData._nppHandle), FuncItems.Items[9]._cmdID,
- Win32.MF_BYCOMMAND | (_doCloseTag ? Win32.MF_CHECKED : Win32.MF_UNCHECKED));
- }
- internal static void DoInsertHtmlCloseTag(char newChar)
- {
- var docType = LangType.L_TEXT;
- Win32.SendMessage(NppData._nppHandle, NppMsg.NPPM_GETCURRENTLANGTYPE, 0, ref docType);
- var isDocTypeHtml = (docType == LangType.L_HTML || docType == LangType.L_XML || docType == LangType.L_PHP);
- if (_doCloseTag && isDocTypeHtml)
- {
- if (newChar == '>')
- {
- var bufCapacity = 512;
- var hCurrentEditView = GetCurrentScintilla();
- var currentPos = (int)Win32.SendMessage(hCurrentEditView, SciMsg.SCI_GETCURRENTPOS, 0, 0);
- var beginPos = currentPos - (bufCapacity - 1);
- var startPos = (beginPos > 0) ? beginPos : 0;
- var size = currentPos - startPos;
- if (size >= 3)
- {
- using (var tr = new SciTextRange(startPos, currentPos, bufCapacity))
- {
- Win32.SendMessage(hCurrentEditView, SciMsg.SCI_GETTEXTRANGE, 0, tr.NativePointer);
- var buf = tr.LpstrText;
- if (buf[size - 2] != '/')
- {
- var insertString = new StringBuilder("</");
- var pCur = size - 2;
- for (; (pCur > 0) && (buf[pCur] != '<') && (buf[pCur] != '>'); )
- pCur--;
- if (buf[pCur] == '<')
- {
- pCur++;
- var regex = new Regex(@"[\._\-:\w]");
- while (regex.IsMatch(buf[pCur].ToString()))
- {
- insertString.Append(buf[pCur]);
- pCur++;
- }
- insertString.Append('>');
- if (insertString.Length > 3)
- {
- Win32.SendMessage(hCurrentEditView, SciMsg.SCI_BEGINUNDOACTION, 0, 0);
- Win32.SendMessage(hCurrentEditView, SciMsg.SCI_REPLACESEL, 0, insertString);
- Win32.SendMessage(hCurrentEditView, SciMsg.SCI_SETSEL, currentPos, currentPos);
- Win32.SendMessage(hCurrentEditView, SciMsg.SCI_ENDUNDOACTION, 0, 0);
- }
- }
- }
- }
- }
- }
- }
- }
- static void GetFileNamesDemo()
- {
- var nbFile = (int)Win32.SendMessage(NppData._nppHandle, NppMsg.NPPM_GETNBOPENFILES, 0, 0);
- MessageBox.Show(nbFile.ToString(), "Number of opened files:");
- using (var cStrArray = new ClikeStringArray(nbFile, Win32.MAX_PATH))
- {
- if (Win32.SendMessage(NppData._nppHandle, NppMsg.NPPM_GETOPENFILENAMES, cStrArray.NativePointer, nbFile) != IntPtr.Zero)
- foreach (var file in cStrArray.ManagedStringsUnicode) MessageBox.Show(file);
- }
- }
- static void GetSessionFileNamesDemo()
- {
- var nbFile = (int)Win32.SendMessage(NppData._nppHandle, 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 (var cStrArray = new ClikeStringArray(nbFile, Win32.MAX_PATH))
- {
- if (Win32.SendMessage(NppData._nppHandle, NppMsg.NPPM_GETSESSIONFILES, cStrArray.NativePointer, SessionFilePath) != IntPtr.Zero)
- foreach (var file in cStrArray.ManagedStringsUnicode) MessageBox.Show(file);
- }
- }
- static void SaveCurrentSessionDemo()
- {
- var sessionPath = Marshal.PtrToStringUni(Win32.SendMessage(NppData._nppHandle, 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();
- using (var newBmp = new Bitmap(16, 16))
- {
- var g = Graphics.FromImage(newBmp);
- var colorMap = new ColorMap[1];
- colorMap[0] = new ColorMap();
- colorMap[0].OldColor = Color.Fuchsia;
- colorMap[0].NewColor = Color.FromKnownColor(KnownColor.ButtonFace);
- var attr = new ImageAttributes();
- attr.SetRemapTable(colorMap);
- g.DrawImage(TbBmpTbTab, new Rectangle(0, 0, 16, 16), 0, 0, 16, 16, GraphicsUnit.Pixel, attr);
- _tbIcon = Icon.FromHandle(newBmp.GetHicon());
- }
-
- var 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;
- var ptrNppTbData = Marshal.AllocHGlobal(Marshal.SizeOf(nppTbData));
- Marshal.StructureToPtr(nppTbData, ptrNppTbData, false);
- Win32.SendMessage(NppData._nppHandle, NppMsg.NPPM_DMMREGASDCKDLG, 0, ptrNppTbData);
- // Following message will toogle both menu item state and toolbar button
- Win32.SendMessage(NppData._nppHandle, NppMsg.NPPM_SETMENUITEMCHECK, FuncItems.Items[IdFrmGotToLine]._cmdID, 1);
- }
- else
- {
- if (!_frmGoToLine.Visible)
- {
- Win32.SendMessage(NppData._nppHandle, NppMsg.NPPM_DMMSHOW, 0, _frmGoToLine.Handle);
- Win32.SendMessage(NppData._nppHandle, NppMsg.NPPM_SETMENUITEMCHECK, FuncItems.Items[IdFrmGotToLine]._cmdID, 1);
- }
- else
- {
- Win32.SendMessage(NppData._nppHandle, NppMsg.NPPM_DMMHIDE, 0, _frmGoToLine.Handle);
- Win32.SendMessage(NppData._nppHandle, NppMsg.NPPM_SETMENUITEMCHECK, FuncItems.Items[IdFrmGotToLine]._cmdID, 0);
- }
- }
- _frmGoToLine.textBox1.Focus();
- }
- #endregion
- }
- }
|