ToolsControl.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. using PaintDotNet.Base.SettingModel;
  2. using PaintDotNet.SystemLayer;
  3. using System;
  4. using System.Drawing;
  5. using System.Windows.Forms;
  6. namespace PaintDotNet
  7. {
  8. internal class ToolsControl : UserControl
  9. {
  10. private ToolStripEx toolStripEx;
  11. private ImageList imageList;
  12. private const int tbWidth = 2;
  13. private int ignoreToolClicked = 0;
  14. private Control onePxSpacingLeft;
  15. private ShortcutbarModel shortcutbarModel = Startup.instance.shortcutbarModel;
  16. private System.ComponentModel.Container components = null;
  17. private AppWorkspace appWorkspace;
  18. public ToolsControl(AppWorkspace appWorkspace)
  19. {
  20. this.appWorkspace = appWorkspace;
  21. InitializeComponent();
  22. SetTools();
  23. }
  24. public void SetTools()
  25. {
  26. if (this.toolStripEx != null)
  27. {
  28. this.toolStripEx.Items.Clear();
  29. }
  30. this.imageList = new ImageList();
  31. this.imageList.ColorDepth = ColorDepth.Depth32Bit;
  32. this.imageList.TransparentColor = Utility.TransparentKey;
  33. this.toolStripEx.ImageList = this.imageList;
  34. /**
  35. ToolStripItem[] buttons = new ToolStripItem[toolInfos.Length];
  36. string toolTipFormat = PdnResources.GetString("ToolsControl.ToolToolTip.Format");
  37. for (int i = 0; i < toolInfos.Length; ++i)
  38. {
  39. ToolInfo toolInfo = toolInfos[i];
  40. ToolStripButton button = new ToolStripButton();
  41. int imageIndex = imageList.Images.Add(
  42. toolInfo.Image.Reference,
  43. imageList.TransparentColor);
  44. button.ImageIndex = imageIndex;
  45. button.Tag = toolInfo.ToolType;
  46. button.ToolTipText = string.Format(toolTipFormat, toolInfo.Name, char.ToUpperInvariant(toolInfo.HotKey).ToString());
  47. buttons[i] = button;
  48. }
  49. this.toolStripEx.Items.AddRange(buttons);
  50. **/
  51. SetCustomizeTools();
  52. }
  53. public void RefreshTools()
  54. {
  55. int numold = 0;
  56. if (this.toolStripEx != null)
  57. {
  58. numold = this.toolStripEx.Items.Count;
  59. }
  60. SetCustomizeTools();
  61. for (int i = 0; i < numold; i++)
  62. {
  63. this.toolStripEx.Items.RemoveAt(0);
  64. }
  65. //this.toolStripEx.Enabled = (!this.appWorkspace.ScriptRunning);
  66. }
  67. public void SetCustomizeTools()
  68. {
  69. if (shortcutbarModel != null)
  70. {
  71. if (shortcutbarModel.Menus.Count > 0)
  72. {
  73. ToolStripItem[] buttons = new ToolStripItem[shortcutbarModel.Menus.Count];
  74. for (int i = 0; i < shortcutbarModel.Menus.Count; i++)
  75. {
  76. ToolStripButton button = new ToolStripButton();
  77. int imageIndex = -1;
  78. ToolStripItem[] items = this.appWorkspace.ToolBar.MainMenu.Items.Find(shortcutbarModel.Menus[i].Description, true);
  79. if (items != null && items.Length > 0)
  80. {
  81. if (((PdnMenuItem)(items[0])).Image != null)
  82. {
  83. imageIndex = this.imageList.Images.Add(((PdnMenuItem)(items[0])).Image, imageList.TransparentColor);
  84. button.ToolTipText = ((PdnMenuItem)(items[0])).Text;
  85. }
  86. else
  87. {
  88. imageIndex = this.imageList.Images.Add(PdnResources.GetImageResource("Icons.MenuImageRotate90CWIcon.png").Reference, imageList.TransparentColor);
  89. //imageIndex = this.imageList.Images.Add(new Bitmap(16, 16), imageList.TransparentColor);
  90. button.ToolTipText = shortcutbarModel.Menus[i].Name;// getShowName(Startup.instance.configModel.Language);
  91. }
  92. }
  93. else
  94. {
  95. imageIndex = this.imageList.Images.Add(PdnResources.GetImageResource("Icons.MenuImageRotate90CWIcon.png").Reference, imageList.TransparentColor);
  96. //imageIndex = this.imageList.Images.Add(new Bitmap(16, 16), imageList.TransparentColor);
  97. button.ToolTipText = shortcutbarModel.Menus[i].Name;// getShowName(Startup.instance.configModel.Language);
  98. }
  99. button.Enabled = Startup.getMenuIdVisible(shortcutbarModel.Menus[i].Id);
  100. button.ImageIndex = imageIndex;
  101. button.Tag = shortcutbarModel.Menus[i].Description;
  102. buttons[i] = button;
  103. if (this.appWorkspace.ActiveDocumentWorkspace != null)
  104. {
  105. if (button.Tag.ToString().Equals(this.type))
  106. button.Checked = this.check;
  107. }
  108. }
  109. this.toolStripEx.Items.AddRange(buttons);
  110. appWorkspace.SetTopLeftCheckState();
  111. }
  112. }
  113. }
  114. public void SelectTool(Type toolType)
  115. {
  116. SelectTool(toolType, true);
  117. }
  118. public void SelectTool(Type toolType, bool raiseEvent)
  119. {
  120. if (!raiseEvent)
  121. {
  122. ++this.ignoreToolClicked;
  123. }
  124. try
  125. {
  126. foreach (ToolStripButton button in this.toolStripEx.Items)
  127. {
  128. if ((Type)button.Tag == toolType)
  129. {
  130. this.ToolStripEx_ItemClicked(this, new ToolStripItemClickedEventArgs(button));
  131. return;
  132. }
  133. }
  134. throw new ArgumentException("Tool type not found");
  135. }
  136. finally
  137. {
  138. if (!raiseEvent)
  139. {
  140. --this.ignoreToolClicked;
  141. }
  142. }
  143. }
  144. protected override void OnLayout(LayoutEventArgs e)
  145. {
  146. int buttonWidth;
  147. if (this.toolStripEx.Items.Count > 0)
  148. {
  149. buttonWidth = this.toolStripEx.Items[0].Width;
  150. }
  151. else
  152. {
  153. buttonWidth = 0;
  154. }
  155. this.toolStripEx.Width =
  156. this.toolStripEx.Padding.Left +
  157. (buttonWidth * tbWidth) +
  158. (this.toolStripEx.Margin.Horizontal * tbWidth) +
  159. this.toolStripEx.Padding.Right;
  160. this.toolStripEx.Height = this.toolStripEx.GetPreferredSize(this.toolStripEx.Size).Height;
  161. this.Width = this.toolStripEx.Width + this.onePxSpacingLeft.Width;
  162. this.Height = this.toolStripEx.Height;
  163. base.OnLayout(e);
  164. }
  165. /// <summary>
  166. /// Clean up any resources being used.
  167. /// </summary>
  168. protected override void Dispose(bool disposing)
  169. {
  170. if (disposing)
  171. {
  172. if (components != null)
  173. {
  174. components.Dispose();
  175. components = null;
  176. }
  177. }
  178. base.Dispose(disposing);
  179. }
  180. /// <summary>
  181. /// Required method for Designer support - do not modify
  182. /// the contents of this method with the code editor.
  183. /// </summary>
  184. private void InitializeComponent()
  185. {
  186. this.toolStripEx = new ToolStripEx();
  187. this.onePxSpacingLeft = new Control();
  188. this.SuspendLayout();
  189. //
  190. // toolStripEx
  191. //
  192. this.toolStripEx.Dock = System.Windows.Forms.DockStyle.Top;
  193. this.toolStripEx.GripStyle = System.Windows.Forms.ToolStripGripStyle.Hidden;
  194. this.toolStripEx.LayoutStyle = System.Windows.Forms.ToolStripLayoutStyle.Flow;
  195. this.toolStripEx.ItemClicked += new ToolStripItemClickedEventHandler(ToolStripEx_ItemClicked);
  196. this.toolStripEx.Name = "toolStripEx";
  197. this.toolStripEx.AutoSize = true;
  198. this.toolStripEx.RelinquishFocus += new EventHandler(ToolStripEx_RelinquishFocus);
  199. //
  200. // onePxSpacingLeft
  201. //
  202. this.onePxSpacingLeft.Dock = System.Windows.Forms.DockStyle.Left;
  203. this.onePxSpacingLeft.Width = 1;
  204. this.onePxSpacingLeft.Name = "onePxSpacingLeft";
  205. //
  206. // MainToolBar
  207. //
  208. this.Controls.Add(this.toolStripEx);
  209. this.Controls.Add(this.onePxSpacingLeft);
  210. this.AutoScaleDimensions = new SizeF(96F, 96F);
  211. this.AutoScaleMode = AutoScaleMode.Dpi;
  212. this.Name = "MainToolBar";
  213. this.Size = new System.Drawing.Size(48, 328);
  214. this.ResumeLayout(false);
  215. }
  216. public event EventHandler RelinquishFocus;
  217. private void OnRelinquishFocus()
  218. {
  219. if (RelinquishFocus != null)
  220. {
  221. RelinquishFocus(this, EventArgs.Empty);
  222. }
  223. }
  224. private void ToolStripEx_RelinquishFocus(object sender, EventArgs e)
  225. {
  226. OnRelinquishFocus();
  227. }
  228. private void ToolStripEx_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
  229. {
  230. if (e.ClickedItem.GetType() != typeof(ToolStripButton))
  231. {
  232. return;
  233. }
  234. ToolStripItem[] menu = this.appWorkspace.ToolBar.MainMenu.Items.Find(e.ClickedItem.Tag.ToString(), true);
  235. if (menu != null && menu.Length > 0)
  236. {
  237. if (menu != null && menu.Length > 0)
  238. {
  239. if (((PdnMenuItem)menu[0]).canChecked)
  240. ((ToolStripButton)e.ClickedItem).Checked = !((ToolStripButton)e.ClickedItem).Checked;
  241. ((PdnMenuItem)menu[0]).PerformClick();
  242. appWorkspace.UpdateBottomButtonSelectionStatus();
  243. }
  244. }
  245. }
  246. private string type;
  247. private bool check;
  248. public void RefreshBtnSelect(bool check, string type)
  249. {
  250. this.type = type;
  251. this.check = check;
  252. if (toolStripEx == null)
  253. return;
  254. for (int j = 0; j < this.toolStripEx.Items.Count; j++)
  255. {
  256. if (this.toolStripEx.Items[j] is ToolStripButton && ((ToolStripButton)this.toolStripEx.Items[j]).Tag.ToString().Equals(type))
  257. ((ToolStripButton)this.toolStripEx.Items[j]).Checked = check;
  258. }
  259. this.Refresh();
  260. }
  261. }
  262. }