1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889 |
- 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);
- if (end.Value - start.Value < 1) continue;
- 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()
- {
- }
- }
- }
|