NppPluginNETHelper.cs 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // NPP plugin platform for .Net v0.94.00 by Kasper B. Graversen etc.
  2. using System;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using System.Collections.Generic;
  6. using System.Runtime.InteropServices;
  7. namespace Kbg.NppPluginNET.PluginInfrastructure
  8. {
  9. [StructLayout(LayoutKind.Sequential)]
  10. public struct NppData
  11. {
  12. public IntPtr _nppHandle;
  13. public IntPtr _scintillaMainHandle;
  14. public IntPtr _scintillaSecondHandle;
  15. }
  16. public delegate void NppFuncItemDelegate();
  17. [StructLayout(LayoutKind.Sequential)]
  18. public struct ShortcutKey
  19. {
  20. public ShortcutKey(bool isCtrl, bool isAlt, bool isShift, Keys key)
  21. {
  22. // the types 'bool' and 'char' have a size of 1 byte only!
  23. _isCtrl = Convert.ToByte(isCtrl);
  24. _isAlt = Convert.ToByte(isAlt);
  25. _isShift = Convert.ToByte(isShift);
  26. _key = Convert.ToByte(key);
  27. }
  28. public byte _isCtrl;
  29. public byte _isAlt;
  30. public byte _isShift;
  31. public byte _key;
  32. }
  33. [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
  34. public struct FuncItem
  35. {
  36. [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
  37. public string _itemName;
  38. public NppFuncItemDelegate _pFunc;
  39. public int _cmdID;
  40. public bool _init2Check;
  41. public ShortcutKey _pShKey;
  42. }
  43. public class FuncItems : IDisposable
  44. {
  45. List<FuncItem> _funcItems;
  46. int _sizeFuncItem;
  47. List<IntPtr> _shortCutKeys;
  48. IntPtr _nativePointer;
  49. bool _disposed = false;
  50. public FuncItems()
  51. {
  52. _funcItems = new List<FuncItem>();
  53. _sizeFuncItem = Marshal.SizeOf(typeof(FuncItem));
  54. _shortCutKeys = new List<IntPtr>();
  55. }
  56. [DllImport("kernel32")]
  57. static extern void RtlMoveMemory(IntPtr Destination, IntPtr Source, int Length);
  58. public void Add(FuncItem funcItem)
  59. {
  60. int oldSize = _funcItems.Count * _sizeFuncItem;
  61. _funcItems.Add(funcItem);
  62. int newSize = _funcItems.Count * _sizeFuncItem;
  63. IntPtr newPointer = Marshal.AllocHGlobal(newSize);
  64. if (_nativePointer != IntPtr.Zero)
  65. {
  66. RtlMoveMemory(newPointer, _nativePointer, oldSize);
  67. Marshal.FreeHGlobal(_nativePointer);
  68. }
  69. IntPtr ptrPosNewItem = (IntPtr)(newPointer.ToInt64() + oldSize);
  70. byte[] aB = Encoding.Unicode.GetBytes(funcItem._itemName + "\0");
  71. Marshal.Copy(aB, 0, ptrPosNewItem, aB.Length);
  72. ptrPosNewItem = (IntPtr)(ptrPosNewItem.ToInt64() + 128);
  73. IntPtr p = (funcItem._pFunc != null) ? Marshal.GetFunctionPointerForDelegate(funcItem._pFunc) : IntPtr.Zero;
  74. Marshal.WriteIntPtr(ptrPosNewItem, p);
  75. ptrPosNewItem = (IntPtr)(ptrPosNewItem.ToInt64() + IntPtr.Size);
  76. Marshal.WriteInt32(ptrPosNewItem, funcItem._cmdID);
  77. ptrPosNewItem = (IntPtr)(ptrPosNewItem.ToInt64() + 4);
  78. Marshal.WriteInt32(ptrPosNewItem, Convert.ToInt32(funcItem._init2Check));
  79. ptrPosNewItem = (IntPtr)(ptrPosNewItem.ToInt64() + 4);
  80. if (funcItem._pShKey._key != 0)
  81. {
  82. IntPtr newShortCutKey = Marshal.AllocHGlobal(4);
  83. Marshal.StructureToPtr(funcItem._pShKey, newShortCutKey, false);
  84. Marshal.WriteIntPtr(ptrPosNewItem, newShortCutKey);
  85. }
  86. else Marshal.WriteIntPtr(ptrPosNewItem, IntPtr.Zero);
  87. _nativePointer = newPointer;
  88. }
  89. public void RefreshItems()
  90. {
  91. IntPtr ptrPosItem = _nativePointer;
  92. for (int i = 0; i < _funcItems.Count; i++)
  93. {
  94. FuncItem updatedItem = new FuncItem();
  95. updatedItem._itemName = _funcItems[i]._itemName;
  96. ptrPosItem = (IntPtr)(ptrPosItem.ToInt64() + 128);
  97. updatedItem._pFunc = _funcItems[i]._pFunc;
  98. ptrPosItem = (IntPtr)(ptrPosItem.ToInt64() + IntPtr.Size);
  99. updatedItem._cmdID = Marshal.ReadInt32(ptrPosItem);
  100. ptrPosItem = (IntPtr)(ptrPosItem.ToInt64() + 4);
  101. updatedItem._init2Check = _funcItems[i]._init2Check;
  102. ptrPosItem = (IntPtr)(ptrPosItem.ToInt64() + 4);
  103. updatedItem._pShKey = _funcItems[i]._pShKey;
  104. ptrPosItem = (IntPtr)(ptrPosItem.ToInt64() + IntPtr.Size);
  105. _funcItems[i] = updatedItem;
  106. }
  107. }
  108. public IntPtr NativePointer { get { return _nativePointer; } }
  109. public List<FuncItem> Items { get { return _funcItems; } }
  110. public void Dispose()
  111. {
  112. if (!_disposed)
  113. {
  114. foreach (IntPtr ptr in _shortCutKeys) Marshal.FreeHGlobal(ptr);
  115. if (_nativePointer != IntPtr.Zero) Marshal.FreeHGlobal(_nativePointer);
  116. _disposed = true;
  117. }
  118. }
  119. ~FuncItems()
  120. {
  121. Dispose();
  122. }
  123. }
  124. public enum winVer
  125. {
  126. WV_UNKNOWN, WV_WIN32S, WV_95, WV_98, WV_ME, WV_NT, WV_W2K,
  127. WV_XP, WV_S2003, WV_XPX64, WV_VISTA, WV_WIN7, WV_WIN8, WV_WIN81, WV_WIN10
  128. }
  129. [Flags]
  130. public enum DockMgrMsg : uint
  131. {
  132. IDB_CLOSE_DOWN = 137,
  133. IDB_CLOSE_UP = 138,
  134. IDD_CONTAINER_DLG = 139,
  135. IDC_TAB_CONT = 1027,
  136. IDC_CLIENT_TAB = 1028,
  137. IDC_BTN_CAPTION = 1050,
  138. DMM_MSG = 0x5000,
  139. DMM_CLOSE = (DMM_MSG + 1),
  140. DMM_DOCK = (DMM_MSG + 2),
  141. DMM_FLOAT = (DMM_MSG + 3),
  142. DMM_DOCKALL = (DMM_MSG + 4),
  143. DMM_FLOATALL = (DMM_MSG + 5),
  144. DMM_MOVE = (DMM_MSG + 6),
  145. DMM_UPDATEDISPINFO = (DMM_MSG + 7),
  146. DMM_GETIMAGELIST = (DMM_MSG + 8),
  147. DMM_GETICONPOS = (DMM_MSG + 9),
  148. DMM_DROPDATA = (DMM_MSG + 10),
  149. DMM_MOVE_SPLITTER = (DMM_MSG + 11),
  150. DMM_CANCEL_MOVE = (DMM_MSG + 12),
  151. DMM_LBUTTONUP = (DMM_MSG + 13),
  152. DMN_FIRST = 1050,
  153. DMN_CLOSE = (DMN_FIRST + 1),
  154. //nmhdr.Code = DWORD(DMN_CLOSE, 0));
  155. //nmhdr.hwndFrom = hwndNpp;
  156. //nmhdr.IdFrom = ctrlIdNpp;
  157. DMN_DOCK = (DMN_FIRST + 2),
  158. DMN_FLOAT = (DMN_FIRST + 3)
  159. //nmhdr.Code = DWORD(DMN_XXX, int newContainer);
  160. //nmhdr.hwndFrom = hwndNpp;
  161. //nmhdr.IdFrom = ctrlIdNpp;
  162. }
  163. [StructLayout(LayoutKind.Sequential)]
  164. public struct toolbarIcons
  165. {
  166. public IntPtr hToolbarBmp;
  167. public IntPtr hToolbarIcon;
  168. }
  169. }