123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588 |
- using PaintDotNet.Data.Param;
- using PaintDotNet.Base.SettingModel;
- using PaintDotNet.SystemLayer;
- using System;
- using System.ComponentModel;
- using System.Drawing;
- using System.IO;
- using System.Reflection;
- using System.Windows.Forms;
- namespace PaintDotNet
- {
- internal class PdnMenuItem : ToolStripMenuItem, IFormAssociate
- {
- /// <summary>
- /// 是否可以用在脚本功能里,默认true
- /// </summary>
- private bool canUseInScript = true;
- /// <summary>
- /// true可以自动执行 false只能手动执行,默认true
- /// </summary>
- private bool automaticScript = true;
- /// <summary>
- /// true自动执行下一步 false手动点击下一步执行,默认false
- /// </summary>
- private bool autoNextScript = false;
- /// <summary>
- /// 脚本是否必须打开窗口,并等待执行完成后才能继续的处理(针对通用分析和专用分析)
- /// </summary>
- private bool needOpenDialog = false;
- /// <summary>
- /// 脚本是否需要加等待执行完成后才能继续的处理
- /// </summary>
- private bool needWaitKey = false;
- /// <summary>
- /// 是否可以选中
- /// </summary>
- public bool canChecked = false;
- /// <summary>
- /// AppWorkspace的引用
- /// </summary>
- private AppWorkspace appWorkspace;
- private string textResourceName = null;
- private const char noMnemonicChar = (char)0;
- private const char mnemonicPrefix = '&';
- /// <summary>
- /// icon loaded的标记
- /// </summary>
- private bool iconsLoaded = false;
- /// <summary>
- /// 标题 loaded的标记
- /// </summary>
- private bool namesLoaded = false;
- /// <summary>
- /// 快捷键
- /// </summary>
- private Keys registeredHotKey = Keys.None;
- /// <summary>
- /// 当前菜单的唯一id
- /// </summary>
- private int menuId;
- public int MenuId
- {
- get
- {
- return this.menuId;
- }
- set
- {
- this.menuId = value;
- }
- }
- public bool CanUseInSenseShield
- {
- get
- {
- System.Collections.Generic.List<int> menuIdlists = new System.Collections.Generic.List<int>();
- menuIdlists.Add(this.menuId);
- PdnMenuItem menuItem = this;
- while (menuItem.OwnerItem != null && menuItem.OwnerItem is PdnMenuItem)
- {
- menuIdlists.Insert(0, ((PdnMenuItem)menuItem.OwnerItem).menuId);
- menuItem = (PdnMenuItem)menuItem.OwnerItem;
- }
- return Startup.getMenuIdUsable(menuIdlists, true);
- }
- }
- /// <summary>
- /// 是否授权显示该菜单
- /// </summary>
- public bool CanShowInSenseShield
- {
- get
- {
- return Startup.getMenuIdVisible(this.menuId);
- }
- }
- /// <summary>
- /// 是否可以用在脚本功能里,默认true
- /// </summary>
- public bool CanUseInScript
- {
- get
- {
- return this.canUseInScript;
- }
- set
- {
- this.canUseInScript = value;
- }
- }
- /// <summary>
- /// true可以自动执行 false只能手动执行,默认true
- /// </summary>
- public bool AutomaticScript
- {
- get
- {
- return this.automaticScript;
- }
- set
- {
- this.automaticScript = value;
- }
- }
- /// <summary>
- /// true自动执行下一步 false手动点击下一步执行,默认false
- /// </summary>
- public bool AutoNextScript
- {
- get
- {
- return this.autoNextScript;
- }
- set
- {
- this.autoNextScript = value;
- }
- }
- /// <summary>
- /// 脚本是否必须打开窗口,并等待执行完成后才能继续的处理(针对通用分析和专用分析)
- /// </summary>
- public bool NeedOpenDialog
- {
- get
- {
- return this.needOpenDialog;
- }
- set
- {
- this.needOpenDialog = value;
- }
- }
- /// <summary>
- /// 脚本是否需要加等待执行完成后才能继续的处理
- /// </summary>
- public bool NeedWaitKey
- {
- get
- {
- return this.needWaitKey;
- }
- set
- {
- this.needWaitKey = value;
- }
- }
- [Browsable(false)]
- public AppWorkspace AppWorkspace
- {
- get
- {
- return this.appWorkspace;
- }
- set
- {
- if (value != this.appWorkspace)
- {
- OnAppWorkspaceChanging();
- this.appWorkspace = value;
- OnAppWorkspaceChanged();
- }
- }
- }
- public Form AssociatedForm
- {
- get
- {
- if (this.appWorkspace == null)
- {
- return null;
- }
- else
- {
- return this.appWorkspace.FindForm();
- }
- }
- }
- public new Keys ShortcutKeys
- {
- get
- {
- return base.ShortcutKeys;
- }
- set
- {
- if (ShortcutKeys != Keys.None)
- {
- PdnBaseForm.UnregisterFormHotKey(ShortcutKeys, OnShortcutKeyPressed);
- }
- PdnBaseForm.RegisterFormHotKey(value, OnShortcutKeyPressed);
- base.ShortcutKeys = value;
- }
- }
- public void resetShortcutKeys(string itemHotKeys)
- {
- if (itemHotKeys != null && !itemHotKeys.Equals(""))
- {
- string[] keys = itemHotKeys.Trim().Split('+');
- Keys Key = Keys.None;
- foreach (string key in keys)
- {
- if (Key == Keys.None)
- {
- Key = (Keys)(Enum.Parse(typeof(Keys), key.Trim()));
- }
- else
- {
- Key = (Keys)(Enum.Parse(typeof(Keys), key.Trim())) | Key;
- }
- }
- this.ShortcutKeys = Key;
- }
- else
- {
- this.ShortcutKeys = Keys.None;
- }
- }
- public bool HasMnemonic
- {
- get
- {
- return (Mnemonic != noMnemonicChar);
- }
- }
- public char Mnemonic
- {
- get
- {
- if (string.IsNullOrEmpty(this.Text))
- {
- return noMnemonicChar;
- }
- int mnemonicPrefixIndex = this.Text.IndexOf(mnemonicPrefix);
- if (mnemonicPrefixIndex >= 0 && mnemonicPrefixIndex < this.Text.Length - 1)
- {
- return this.Text[mnemonicPrefixIndex + 1];
- }
- else
- {
- return noMnemonicChar;
- }
- }
- }
- public void PerformClickAsync()
- {
- this.Owner.BeginInvoke(new Procedure(this.PerformClick));
- }
- protected virtual void OnAppWorkspaceChanging()
- {
- foreach (ToolStripItem item in this.DropDownItems)
- {
- PdnMenuItem asPMI = item as PdnMenuItem;
- if (asPMI != null)
- {
- asPMI.AppWorkspace = null;
- }
- }
- }
- protected virtual void OnAppWorkspaceChanged()
- {
- foreach (ToolStripItem item in this.DropDownItems)
- {
- PdnMenuItem asPMI = item as PdnMenuItem;
- if (asPMI != null)
- {
- asPMI.AppWorkspace = AppWorkspace;
- }
- }
- }
- public PdnMenuItem(string name, Image image, EventHandler eventHandler)
- : base(name, image, eventHandler)
- {
- Constructor();
- }
- /// <summary>
- /// 目前用的构造方法
- /// </summary>
- /// <param name="type"></param>
- public PdnMenuItem(ActionType type)
- {
- Constructor();
- //设置快捷键,如果配置了
- SetHotKeyFromConfig((int)type);
- //设置名称
- this.Name = GetDescription(type);
- //设置id
- this.MenuId = (int)type;
- }
- public PdnMenuItem(ActionType type, bool canChecked)
- {
- Constructor();
- //设置快捷键,如果配置了
- SetHotKeyFromConfig((int)type);
- //设置名称
- this.Name = GetDescription(type);
- //设置id
- this.MenuId = (int)type;
- //设置可以被选中
- this.canChecked = canChecked;
- }
- public PdnMenuItem()
- {
- Constructor();
- }
- private void Constructor()
- {
- InitializeComponent();
- }
- private void InitializeComponent()
- {
- this.DropDownOpening += new EventHandler(PdnMenuItem_DropDownOpening);
- }
- private bool OnShortcutKeyPressed(Keys keys)
- {
- //PerformClick();
- return true;
- }
- private bool OnAccessHotKeyPressed(Keys keys)
- {
- ShowDropDown();
- return true;
- }
- protected override void OnTextChanged(EventArgs e)
- {
- if (this.registeredHotKey != Keys.None)
- {
- PdnBaseForm.UnregisterFormHotKey(this.registeredHotKey, OnAccessHotKeyPressed);
- }
- char mnemonic = this.Mnemonic;
- if (mnemonic != noMnemonicChar && !IsOnDropDown)
- {
- Keys hotKey = Utility.LetterOrDigitCharToKeys(mnemonic);//快捷键,通过alt+key打开一级菜单
- PdnBaseForm.RegisterFormHotKey(Keys.Alt | hotKey, OnAccessHotKeyPressed);//快捷键,通过alt+key打开一级菜单
- }
- base.OnTextChanged(e);
- }
- private void PdnMenuItem_DropDownOpening(object sender, EventArgs e)
- {
- OnDropDownOpening(e);
- }
- protected virtual void OnDropDownOpening(EventArgs e)
- {
- if (!this.namesLoaded)
- {
- LoadNames(this.Name);
- }
- if (!this.iconsLoaded)
- {
- LoadIcons();
- }
- }
- /// <summary>
- /// 递归查找菜单,如果脚本正在执行,则菜单不可手动点击
- /// </summary>
- /// <param name="collection"></param>
- /// <param name="step"></param>
- protected void RecursiveData(ToolStripItemCollection collection)
- {
- if (!AppWorkspace.ScriptRunning)
- return;
- Boolean enabled = false;
- for (int i = 0; i < collection.Count; i++)
- {
- if (collection[i] is PdnMenuItem)
- {
- ((PdnMenuItem)collection[i]).Enabled = enabled;
- //RecursiveData(((PdnMenuItem)collection[i]).DropDownItems);
- }
- //if (!collection[i].Name.Equals("OpenRecent") && !collection[i].Name.Equals("CameraSelection"))
- //{
- //}
- }
- }
- public void LoadNames(string baseName)
- {
- foreach (ToolStripItem item in this.DropDownItems)
- {
- if(!string.IsNullOrWhiteSpace(item.Name))
- {
- string itemNameBase = baseName + "." + item.Name;
- string itemNameText = itemNameBase + ".Text";
- string text = PdnResources.GetString(itemNameText);
- if (text != null)
- {
- item.Text = text;
- }
- PdnMenuItem pmi = item as PdnMenuItem;
- if (pmi != null)
- {
- pmi.textResourceName = itemNameText;
- pmi.LoadNames(itemNameBase);
- }
- }
- }
- this.namesLoaded = true;
- }
- public void SetIcon(string imageName)
- {
- this.ImageTransparentColor = Utility.TransparentKey;
- this.Image = PdnResources.GetImageResource(imageName).Reference;
- }
- public void SetIcon(ImageResource image)
- {
- this.ImageTransparentColor = Utility.TransparentKey;
- this.Image = image.Reference;
- }
- public void LoadIcons()
- {
- Type ourType = this.GetType();
- FieldInfo[] fields = ourType.GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
- foreach (FieldInfo fi in fields)
- {
- if (fi.FieldType.IsSubclassOf(typeof(PdnMenuItem)) ||
- fi.FieldType == typeof(PdnMenuItem))
- {
- string iconFileName = "Icons." + fi.Name[0].ToString().ToUpper() + fi.Name.Substring(1) + "Icon.png";
- PdnMenuItem mi = (PdnMenuItem)fi.GetValue(this);
- Stream iconStream = PdnResources.GetResourceStream(iconFileName);
- if (iconStream != null)
- {
- iconStream.Dispose();
- mi.SetIcon(iconFileName);
- }
- else
- {
- Tracing.Ping(iconFileName + " not found");
- }
- }
- }
- this.iconsLoaded = true;
- }
- protected override void OnDropDownClosed(EventArgs e)
- {
- foreach (ToolStripItem item in this.DropDownItems)
- {
- item.Enabled = true;
- }
- base.OnDropDownClosed(e);
- }
- protected override void OnClick(EventArgs e)
- {
- if ((this.DropDownItems == null || this.DropDownItems.Count == 0) && !this.CanUseInSenseShield)
- {
- string featureNameNotShield = this.Name ?? this.Text;
- Tracing.LogFeature(featureNameNotShield + "not shield");
- return;
- }
- var form = Form.ActiveForm;
- if (form != null)
- {
- form.BeginInvoke(new Procedure(PdnBaseForm.UpdateAllForms));
- }
- string featureName = this.Name ?? this.Text;
- Tracing.LogFeature(featureName);
- base.OnClick(e);
- }
- private void SetHotKeyFromConfig(int menuId)
- {
- if (Startup.instance.hotkeyModel != null
- && Startup.instance.hotkeyModel.items != null
- && Startup.instance.hotkeyModel.items.Count > 0)
- {
- HotkeyModel.Item item = Startup.instance.hotkeyModel.items.Find(m => m.MenuId == menuId);
- if (item!=null) {
- if (item.HotKeys != null && !item.HotKeys.Equals(""))
- {
- string[] keys = item.HotKeys.Trim().Split('+');
- Keys Key = Keys.None;
- foreach (string key in keys)
- {
- if (Key == Keys.None)
- {
- Key = (Keys)(Enum.Parse(typeof(Keys), key.Trim()));
- }
- else
- {
- Key = (Keys)(Enum.Parse(typeof(Keys), key.Trim())) | Key;
- }
- }
- this.ShortcutKeys = Key;
- }
- }
- }
- }
- /// <summary>
- /// 获取枚举的描述
- /// </summary>
- /// <param name="en">枚举</param>
- /// <returns>返回枚举的描述</returns>
- public static string GetDescription(Enum en)
- {
- Type type = en.GetType(); //获取类型
- MemberInfo[] memberInfos = type.GetMember(en.ToString()); //获取成员
- if (memberInfos != null && memberInfos.Length > 0)
- {
- DescriptionAttribute[] attrs = memberInfos[0].GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[]; //获取描述特性
- if (attrs != null && attrs.Length > 0)
- {
- return attrs[0].Description; //返回当前描述
- }
- }
- return en.ToString();
- }
- }
- }
|