ToolkitControl.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. using PaintDotNet.Annotation.Enum;
  2. using PaintDotNet.Base.CommTool;
  3. using PaintDotNet.Base.SettingModel;
  4. using PaintDotNet.SystemLayer;
  5. using System;
  6. using System.Linq;
  7. using System.Collections.Generic;
  8. using System.Drawing;
  9. using System.Windows.Forms;
  10. using PaintDotNet.DbOpreate.DbModel;
  11. using PaintDotNet.DbOpreate.DbBll;
  12. namespace PaintDotNet.Preview2
  13. {
  14. internal class ToolkitControl : UserControl
  15. {
  16. private ToolStripEx toolStripEx;
  17. private ImageList imageList;
  18. private const int tbWidth = 2;
  19. private Control onePxSpacingLeft;
  20. private ShortcutbarModel _shortcutbarModel;
  21. private System.ComponentModel.Container components = null;
  22. private DocumentView _docv;
  23. private AppWorkspace _app;
  24. public ToolkitControl(AppWorkspace app, DocumentView dv)
  25. {
  26. _docv = dv;
  27. _app = app;
  28. InitializeComponent();
  29. this.imageList = new ImageList();
  30. this.imageList.ColorDepth = ColorDepth.Depth32Bit;
  31. this.imageList.TransparentColor = Utility.TransparentKey;
  32. this.toolStripEx.ImageList = this.imageList;
  33. }
  34. public void UpdateTools(ShortcutbarModel model)
  35. {
  36. _shortcutbarModel = model;
  37. this.toolStripEx.Items.Clear();
  38. if (_shortcutbarModel != null && _shortcutbarModel.Menus.Count > 0)
  39. {
  40. List<ToolStripItem> buttons = new List<ToolStripItem>();
  41. // for (int i = 0; i < shortcutbarModel.Menus.Count; i++)
  42. foreach (var item1 in _shortcutbarModel.Menus)
  43. {
  44. ToolStripButton button = new ToolStripButton();
  45. if (item1.Id == 1211)//脚本 script
  46. {
  47. button = new ToolStripButton();
  48. var img = PdnResources.GetImageResource("Icons.Script.png").Reference;
  49. button.ToolTipText = item1.Name;
  50. button.TextImageRelation = TextImageRelation.ImageAboveText;
  51. button.ImageIndex = imageList.Images.Add(img, imageList.TransparentColor); ;
  52. button.Tag = item1;
  53. buttons.Add(button);
  54. continue;
  55. }
  56. int imageIndex = -1;
  57. ToolStripItem[] items = _app.ToolBar.MainMenu.Items.Find(item1.Description, true);
  58. if (items != null && items.Length > 0)
  59. {
  60. var item = (PdnMenuItem)(items[0]);
  61. if (item.Image != null)
  62. {
  63. imageIndex = this.imageList.Images.Add(item.Image, imageList.TransparentColor);
  64. button.ToolTipText = item.Text;
  65. }
  66. else
  67. {
  68. imageIndex = this.imageList.Images.Add(PdnResources.GetImageResource("Icons.MenuImageRotate90CWIcon.png").Reference, imageList.TransparentColor);
  69. //imageIndex = this.imageList.Images.Add(new Bitmap(16, 16), imageList.TransparentColor);
  70. button.ToolTipText = item1.Name;// getShowName(Startup.instance.configModel.Language);
  71. }
  72. }
  73. else
  74. {
  75. imageIndex = this.imageList.Images.Add(PdnResources.GetImageResource("Icons.MenuImageRotate90CWIcon.png").Reference, imageList.TransparentColor);
  76. //imageIndex = this.imageList.Images.Add(new Bitmap(16, 16), imageList.TransparentColor);
  77. button.ToolTipText = item1.Name;// getShowName(Startup.instance.configModel.Language);
  78. }
  79. button.Enabled = Startup.getMenuIdVisible(item1.Id);
  80. button.ImageIndex = imageIndex;
  81. button.Tag = item1;
  82. buttons.Add(button);
  83. if (item1.Id == 704)
  84. {
  85. _buttonContinue = button;
  86. }
  87. }
  88. this.toolStripEx.Items.AddRange(buttons.ToArray());
  89. }
  90. }
  91. protected override void OnLayout(LayoutEventArgs e)
  92. {
  93. int buttonWidth;
  94. if (this.toolStripEx.Items.Count > 0)
  95. {
  96. buttonWidth = this.toolStripEx.Items[0].Width;
  97. }
  98. else
  99. {
  100. buttonWidth = 0;
  101. }
  102. this.toolStripEx.Width =
  103. this.toolStripEx.Padding.Left +
  104. (buttonWidth * tbWidth) +
  105. (this.toolStripEx.Margin.Horizontal * tbWidth) +
  106. this.toolStripEx.Padding.Right;
  107. this.toolStripEx.Height = this.toolStripEx.GetPreferredSize(this.toolStripEx.Size).Height;
  108. this.Width = this.toolStripEx.Width + this.onePxSpacingLeft.Width;
  109. this.Height = this.toolStripEx.Height;
  110. base.OnLayout(e);
  111. }
  112. /// <summary>
  113. /// Clean up any resources being used.
  114. /// </summary>
  115. protected override void Dispose(bool disposing)
  116. {
  117. if (disposing)
  118. {
  119. if (components != null)
  120. {
  121. components.Dispose();
  122. components = null;
  123. }
  124. }
  125. base.Dispose(disposing);
  126. }
  127. /// <summary>
  128. /// Required method for Designer support - do not modify
  129. /// the contents of this method with the code editor.
  130. /// </summary>
  131. private void InitializeComponent()
  132. {
  133. this.toolStripEx = new PaintDotNet.SystemLayer.ToolStripEx();
  134. this.onePxSpacingLeft = new System.Windows.Forms.Control();
  135. this.SuspendLayout();
  136. //
  137. // toolStripEx
  138. //
  139. this.toolStripEx.ClickThrough = false;
  140. this.toolStripEx.GripStyle = System.Windows.Forms.ToolStripGripStyle.Hidden;
  141. this.toolStripEx.LayoutStyle = System.Windows.Forms.ToolStripLayoutStyle.Flow;
  142. this.toolStripEx.Location = new System.Drawing.Point(1, 0);
  143. this.toolStripEx.ManagedFocus = true;
  144. this.toolStripEx.Name = "toolStripEx";
  145. this.toolStripEx.Size = new System.Drawing.Size(47, 0);
  146. this.toolStripEx.TabIndex = 0;
  147. this.toolStripEx.RelinquishFocus += new System.EventHandler(this.ToolStripEx_RelinquishFocus);
  148. this.toolStripEx.ItemClicked += new System.Windows.Forms.ToolStripItemClickedEventHandler(this.ToolStripEx_ItemClicked);
  149. //
  150. // onePxSpacingLeft
  151. //
  152. this.onePxSpacingLeft.Dock = System.Windows.Forms.DockStyle.Left;
  153. this.onePxSpacingLeft.Location = new System.Drawing.Point(0, 0);
  154. this.onePxSpacingLeft.Name = "onePxSpacingLeft";
  155. this.onePxSpacingLeft.Size = new System.Drawing.Size(1, 328);
  156. this.onePxSpacingLeft.TabIndex = 1;
  157. //
  158. // ToolkitControl
  159. //
  160. this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
  161. this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Dpi;
  162. this.Controls.Add(this.toolStripEx);
  163. this.Controls.Add(this.onePxSpacingLeft);
  164. this.Margin = new System.Windows.Forms.Padding(0);
  165. this.Name = "ToolkitControl";
  166. this.Size = new System.Drawing.Size(48, 328);
  167. this.ResumeLayout(false);
  168. this.PerformLayout();
  169. }
  170. private void ToolStripEx_KeyDown(object sender, KeyEventArgs e)
  171. {
  172. if (e.KeyCode == Keys.Delete)
  173. { _docv.MouseEvent_Del(null, null); }
  174. }
  175. public event EventHandler RelinquishFocus;
  176. private void OnRelinquishFocus()
  177. {
  178. if (RelinquishFocus != null)
  179. {
  180. RelinquishFocus(this, EventArgs.Empty);
  181. }
  182. }
  183. private void ToolStripEx_RelinquishFocus(object sender, EventArgs e)
  184. {
  185. OnRelinquishFocus();
  186. }
  187. private void ToolStripEx_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
  188. {
  189. if (_btnActionMap == null) InitActionMap();
  190. _buttonNow = e.ClickedItem as ToolStripButton;
  191. var tag = e.ClickedItem.Tag as ShortcutbarModel.Item;
  192. var id = tag.Id;
  193. if (_btnActionMap.Keys.Contains(id))
  194. {
  195. _btnActionMap[id]();
  196. }
  197. else
  198. {
  199. if (_docv.Document != null)
  200. _docv.ActiveTool = (DrawToolType)id;
  201. }
  202. }
  203. ToolStripButton _buttonNow;
  204. ToolStripButton _buttonContinue;
  205. Dictionary<int, Action> _btnActionMap;
  206. void InitActionMap()
  207. {
  208. _btnActionMap = new Dictionary<int, Action>();
  209. _btnActionMap.Add(701, SelectClicked);
  210. _btnActionMap.Add(702, SelectAllClicked);
  211. _btnActionMap.Add(704, ContinueDrawClicked);
  212. _btnActionMap.Add(705, DeleteAllClicked);
  213. _btnActionMap.Add(1211, ScriptClicked);
  214. }
  215. private void ScriptClicked()
  216. {
  217. List<mic_script> list = mic_script_BLL.FindAll();
  218. var scp = list.Find((e) => e.name == _buttonNow.ToolTipText);
  219. //获取当前选中的脚本步骤
  220. List<mic_script_step> steps = mic_script_step_BLL.FindAllByScripId(scp.id);
  221. var paramss = mic_script_step_param_BLL.FindAllByScriptId(scp.id);
  222. //循环脚本步骤
  223. foreach (mic_script_step step in steps)
  224. {
  225. //递归的方式
  226. this.RecursiveData(_app.ToolBar.MainMenu.Items/*this.collection*/, step);
  227. }
  228. }
  229. /// <summary>
  230. /// 递归查找菜单,暂时不用,改成直接用name查找
  231. /// </summary>
  232. /// <param name="collection"></param>
  233. /// <param name="step"></param>
  234. private void RecursiveData(ToolStripItemCollection collection, mic_script_step step)
  235. {
  236. for (int i = 0; i < collection.Count; i++)
  237. {
  238. if (!collection[i].Name.Equals("OpenRecent") && !collection[i].Name.Equals("CameraSelection"))
  239. {
  240. if (collection[i] is PdnMenuItem)
  241. {
  242. if (step.menu_id == ((PdnMenuItem)collection[i]).MenuId)
  243. {
  244. ((PdnMenuItem)collection[i]).PerformClick();
  245. }
  246. RecursiveData(((PdnMenuItem)collection[i]).DropDownItems, step);
  247. }
  248. }
  249. }
  250. }
  251. private void DeleteAllClicked()
  252. {
  253. _docv.GraphicsList.Clear();
  254. }
  255. private void ContinueDrawClicked()
  256. {
  257. _docv.ContinuousDrawingMeasure = !_docv.ContinuousDrawingMeasure;
  258. _buttonNow.Checked = _docv.ContinuousDrawingMeasure;
  259. }
  260. public void UpdateDisplay()
  261. {
  262. if (_buttonContinue != null)
  263. _buttonContinue.Checked = _docv.ContinuousDrawingMeasure;
  264. }
  265. private void SelectClicked()
  266. {
  267. _docv.ActiveTool = DrawToolType.Pointer;
  268. }
  269. private void SelectAllClicked()
  270. {
  271. _docv.GraphicsList.SelectAll();
  272. }
  273. }
  274. }