12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- using System.Drawing;
- using System.Runtime.InteropServices;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using Microsoft.International.Converters.TraditionalChineseToSimplifiedConverter;
- using NppChnConvPlugin.NppPluginInfrastructure;
- namespace NppChnConvPlugin
- {
- internal static class PluginBridge
- {
- private static readonly IScintillaGateway CurrentEditor = new ScintillaGateway(PluginBase.GetCurrentScintilla());
- // for get current editing file info
- private static readonly INotepadPPGateway Notepad = new NotepadPPGateway();
- public static bool Enable { get; private set; }
- public static void SetupPluginMenu()
- {
- PluginBase.SetCommand(0, "Convert To CHS", ConvertToChs);
- PluginBase.SetCommand(1, "Convert To CHT", ConvertToCht);
- }
- public static void SetupToolBarIcon()
- {
- var toolbarIcons = new toolbarIcons { hToolbarBmp = Properties.Resources.Bitmap1.GetHbitmap() };
- var pTbIcons = Marshal.AllocHGlobal(Marshal.SizeOf(toolbarIcons));
- Marshal.StructureToPtr(toolbarIcons, pTbIcons, false);
- Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_ADDTOOLBARICON, PluginBase._funcItems.Items[0]._cmdID, pTbIcons);
- Marshal.FreeHGlobal(pTbIcons);
- toolbarIcons = new toolbarIcons { hToolbarBmp = Properties.Resources.Bitmap2.GetHbitmap() };
- pTbIcons = Marshal.AllocHGlobal(Marshal.SizeOf(toolbarIcons));
- Marshal.StructureToPtr(toolbarIcons, pTbIcons, false);
- Win32.SendMessage(PluginBase.nppData._nppHandle, (uint)NppMsg.NPPM_ADDTOOLBARICON, PluginBase._funcItems.Items[1]._cmdID, pTbIcons);
- Marshal.FreeHGlobal(pTbIcons);
- }
- private static void ConvertToChs() => DoConvertSelect(ChineseConversionDirection.TraditionalToSimplified);
- private static void ConvertToCht() => DoConvertSelect(ChineseConversionDirection.SimplifiedToTraditional);
- private static void DoConvertSelect(ChineseConversionDirection chineseConversionDirection)
- {
- CurrentEditor.BeginUndoAction();
- var selections = CurrentEditor.GetSelections();
- for (int i = selections - 1; i >= 0; i--)
- {
- var start = CurrentEditor.GetSelectionNStart(i);
- var end = CurrentEditor.GetSelectionNEnd(i);
- CurrentEditor.SetTargetStart(start);
- CurrentEditor.SetTargetEnd(end);
- var text = CurrentEditor.GetTargetText();
- var convert = ChineseConverter.Convert(text, chineseConversionDirection);
- var len = CurrentEditor.EncodedFromUTF8Len(convert);
- CurrentEditor.ReplaceTarget(len, convert);
- }
- CurrentEditor.EndUndoAction();
- }
- public static void FlushState()
- {
- CurrentEditor.SetScintillaHandle(PluginBase.GetCurrentScintilla());
- }
- public static void UpdateText()
- {
- }
- public static void UpdateScroll()
- {
- }
- public static void Exit()
- {
- }
- }
- }
|