/src/ChooseToolDefaultsDialog.cs

http://paint-mono.googlecode.com/ · C# · 436 lines · 334 code · 67 blank · 35 comment · 24 complexity · 49f02b30158848f1921729d31b3cf3e7 MD5 · raw file

  1. /////////////////////////////////////////////////////////////////////////////////
  2. // Paint.NET //
  3. // Copyright (C) Rick Brewster, Tom Jackson, and past contributors. //
  4. // Portions Copyright (C) Microsoft Corporation. All Rights Reserved. //
  5. // See src/Resources/Files/License.txt for full licensing and attribution //
  6. // details. //
  7. // . //
  8. /////////////////////////////////////////////////////////////////////////////////
  9. using PaintDotNet.SystemLayer;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Drawing;
  13. using System.Globalization;
  14. using System.Windows.Forms;
  15. namespace PaintDotNet
  16. {
  17. public class ChooseToolDefaultsDialog
  18. : PdnBaseForm
  19. {
  20. private Button cancelButton;
  21. private Button saveButton;
  22. private Label introText;
  23. private Label defaultToolText;
  24. private Button resetButton;
  25. private Button loadFromToolBarButton;
  26. private ToolChooserStrip toolChooserStrip;
  27. private Type toolType = Tool.DefaultToolType;
  28. private AppEnvironment toolBarAppEnvironment;
  29. private Type toolBarToolType;
  30. private HeaderLabel bottomSeparator;
  31. private List<ToolConfigRow> toolConfigRows = new List<ToolConfigRow>();
  32. private sealed class ToolConfigRow
  33. {
  34. private ToolConfigStrip toolConfigStrip;
  35. private HeaderLabel headerLabel;
  36. private ToolBarConfigItems toolBarConfigItems;
  37. public ToolBarConfigItems ToolBarConfigItems
  38. {
  39. get
  40. {
  41. return this.toolBarConfigItems;
  42. }
  43. }
  44. public HeaderLabel HeaderLabel
  45. {
  46. get
  47. {
  48. return this.headerLabel;
  49. }
  50. }
  51. public ToolConfigStrip ToolConfigStrip
  52. {
  53. get
  54. {
  55. return this.toolConfigStrip;
  56. }
  57. }
  58. private string GetHeaderResourceName()
  59. {
  60. string resName1 = this.toolBarConfigItems.ToString();
  61. string resName2 = resName1.Replace(", ", "");
  62. return "ChooseToolDefaultsDialog.ToolConfigRow." + resName2 + ".HeaderLabel.Text";
  63. }
  64. public ToolConfigRow(ToolBarConfigItems toolBarConfigItems)
  65. {
  66. this.toolBarConfigItems = toolBarConfigItems;
  67. this.headerLabel = new HeaderLabel();
  68. this.headerLabel.Name = "headerLabel:" + toolBarConfigItems.ToString();
  69. this.headerLabel.Text = PdnResources.GetString(GetHeaderResourceName());
  70. this.headerLabel.RightMargin = 0;
  71. this.toolConfigStrip = new ToolConfigStrip();
  72. this.toolConfigStrip.Name = "toolConfigStrip:" + toolBarConfigItems.ToString();
  73. this.toolConfigStrip.AutoSize = true;
  74. this.toolConfigStrip.Dock = DockStyle.None;
  75. this.toolConfigStrip.GripStyle = ToolStripGripStyle.Hidden;
  76. this.toolConfigStrip.LayoutStyle = ToolStripLayoutStyle.HorizontalStackWithOverflow;
  77. this.toolConfigStrip.ToolBarConfigItems = this.toolBarConfigItems;
  78. }
  79. }
  80. public void SetToolBarSettings(Type newToolType, AppEnvironment newToolBarAppEnvironment)
  81. {
  82. this.toolBarToolType = newToolType;
  83. this.toolBarAppEnvironment = newToolBarAppEnvironment.Clone();
  84. }
  85. public void LoadUIFromAppEnvironment(AppEnvironment newAppEnvironment)
  86. {
  87. SuspendLayout();
  88. foreach (ToolConfigRow row in this.toolConfigRows)
  89. {
  90. row.ToolConfigStrip.LoadFromAppEnvironment(newAppEnvironment);
  91. }
  92. ResumeLayout();
  93. }
  94. public void SetDefaultToolType(Type newDefaultToolType)
  95. {
  96. this.toolChooserStrip.SelectTool(newDefaultToolType);
  97. }
  98. public AppEnvironment CreateAppEnvironmentFromUI()
  99. {
  100. AppEnvironment newAppEnvironment = new AppEnvironment();
  101. foreach (ToolConfigRow row in this.toolConfigRows)
  102. {
  103. if ((row.ToolBarConfigItems & ToolBarConfigItems.AlphaBlending) != 0)
  104. {
  105. newAppEnvironment.AlphaBlending = row.ToolConfigStrip.AlphaBlending;
  106. }
  107. if ((row.ToolBarConfigItems & ToolBarConfigItems.Antialiasing) != 0)
  108. {
  109. newAppEnvironment.AntiAliasing = row.ToolConfigStrip.AntiAliasing;
  110. }
  111. if ((row.ToolBarConfigItems & ToolBarConfigItems.Brush) != 0)
  112. {
  113. newAppEnvironment.BrushInfo = row.ToolConfigStrip.BrushInfo;
  114. }
  115. if ((row.ToolBarConfigItems & ToolBarConfigItems.ColorPickerBehavior) != 0)
  116. {
  117. newAppEnvironment.ColorPickerClickBehavior = row.ToolConfigStrip.ColorPickerClickBehavior;
  118. }
  119. if ((row.ToolBarConfigItems & ToolBarConfigItems.Gradient) != 0)
  120. {
  121. newAppEnvironment.GradientInfo = row.ToolConfigStrip.GradientInfo;
  122. }
  123. if ((row.ToolBarConfigItems & ToolBarConfigItems.Pen) != 0)
  124. {
  125. newAppEnvironment.PenInfo = row.ToolConfigStrip.PenInfo;
  126. }
  127. if ((row.ToolBarConfigItems & ToolBarConfigItems.Resampling) != 0)
  128. {
  129. newAppEnvironment.ResamplingAlgorithm = row.ToolConfigStrip.ResamplingAlgorithm;
  130. }
  131. if ((row.ToolBarConfigItems & ToolBarConfigItems.ShapeType) != 0)
  132. {
  133. newAppEnvironment.ShapeDrawType = row.ToolConfigStrip.ShapeDrawType;
  134. }
  135. if ((row.ToolBarConfigItems & ToolBarConfigItems.Text) != 0)
  136. {
  137. newAppEnvironment.FontInfo = row.ToolConfigStrip.FontInfo;
  138. newAppEnvironment.FontSmoothing = row.ToolConfigStrip.FontSmoothing;
  139. newAppEnvironment.TextAlignment = row.ToolConfigStrip.FontAlignment;
  140. }
  141. if ((row.ToolBarConfigItems & ToolBarConfigItems.Tolerance) != 0)
  142. {
  143. newAppEnvironment.Tolerance = row.ToolConfigStrip.Tolerance;
  144. }
  145. }
  146. return newAppEnvironment;
  147. }
  148. public Type ToolType
  149. {
  150. get
  151. {
  152. return this.toolType;
  153. }
  154. set
  155. {
  156. this.toolChooserStrip.SelectTool(value);
  157. this.toolType = value;
  158. }
  159. }
  160. public ChooseToolDefaultsDialog()
  161. {
  162. UI.InitScaling(this);
  163. SuspendLayout();
  164. InitializeComponent();
  165. this.toolConfigRows.Add(new ToolConfigRow(ToolBarConfigItems.ShapeType | ToolBarConfigItems.Brush | ToolBarConfigItems.Pen));
  166. this.toolConfigRows.Add(new ToolConfigRow(ToolBarConfigItems.Text));
  167. this.toolConfigRows.Add(new ToolConfigRow(ToolBarConfigItems.Gradient));
  168. this.toolConfigRows.Add(new ToolConfigRow(ToolBarConfigItems.Tolerance));
  169. this.toolConfigRows.Add(new ToolConfigRow(ToolBarConfigItems.ColorPickerBehavior));
  170. this.toolConfigRows.Add(new ToolConfigRow(ToolBarConfigItems.Resampling));
  171. this.toolConfigRows.Add(new ToolConfigRow(ToolBarConfigItems.AlphaBlending | ToolBarConfigItems.Antialiasing));
  172. for (int i = 0; i < this.toolConfigRows.Count; ++i)
  173. {
  174. Controls.Add(this.toolConfigRows[i].HeaderLabel);
  175. Controls.Add(this.toolConfigRows[i].ToolConfigStrip);
  176. }
  177. ResumeLayout();
  178. PerformLayout();
  179. this.toolChooserStrip.SetTools(DocumentWorkspace.ToolInfos);
  180. }
  181. protected override void OnLoad(EventArgs e)
  182. {
  183. this.saveButton.Select();
  184. base.OnLoad(e);
  185. }
  186. public override void LoadResources()
  187. {
  188. this.Text = PdnResources.GetString("ChooseToolDefaultsDialog.Text");
  189. this.Icon = Utility.ImageToIcon(ImageResource.Get("Icons.MenuLayersLayerPropertiesIcon.png").Reference);
  190. this.introText.Text = PdnResources.GetString("ChooseToolDefaultsDialog.IntroText.Text");
  191. this.defaultToolText.Text = PdnResources.GetString("ChooseToolDefaultsDialog.DefaultToolText.Text");
  192. this.loadFromToolBarButton.Text = PdnResources.GetString("ChooseToolDefaultsDialog.LoadFromToolBarButton.Text");
  193. this.cancelButton.Text = PdnResources.GetString("Form.CancelButton.Text");
  194. this.saveButton.Text = PdnResources.GetString("Form.SaveButton.Text");
  195. this.resetButton.Text = PdnResources.GetString("Form.ResetButton.Text");
  196. base.LoadResources();
  197. }
  198. protected override void OnLayout(LayoutEventArgs levent)
  199. {
  200. int leftMargin = UI.ScaleWidth(8);
  201. int rightMargin = UI.ScaleWidth(8);
  202. int topMargin = UI.ScaleHeight(8);
  203. int bottomMargin = UI.ScaleHeight(8);
  204. int buttonHMargin = UI.ScaleWidth(7);
  205. int afterIntroTextVMargin = UI.ScaleHeight(16);
  206. int afterHeaderVMargin = UI.ScaleHeight(3);
  207. int hMargin = UI.ScaleWidth(7);
  208. int vMargin = UI.ScaleHeight(7);
  209. int insetWidth = ClientSize.Width - leftMargin - rightMargin;
  210. this.introText.Location = new Point(leftMargin, topMargin);
  211. this.introText.Width = insetWidth;
  212. this.introText.Height = this.introText.GetPreferredSize(this.introText.Size).Height;
  213. this.defaultToolText.Location = new Point(
  214. leftMargin,
  215. this.introText.Bottom + afterIntroTextVMargin);
  216. this.toolChooserStrip.Location = new Point(
  217. this.defaultToolText.Right + hMargin,
  218. this.defaultToolText.Top + (this.defaultToolText.Height - this.toolChooserStrip.Height) / 2);
  219. int y = vMargin + Math.Max(this.defaultToolText.Bottom, this.toolChooserStrip.Bottom);
  220. int maxInsetWidth = insetWidth;
  221. for (int i = 0; i < this.toolConfigRows.Count; ++i)
  222. {
  223. this.toolConfigRows[i].HeaderLabel.Location = new Point(leftMargin, y);
  224. this.toolConfigRows[i].HeaderLabel.Width = insetWidth;
  225. y = this.toolConfigRows[i].HeaderLabel.Bottom + afterHeaderVMargin;
  226. this.toolConfigRows[i].ToolConfigStrip.Location = new Point(leftMargin + 3, y);
  227. Size preferredSize = this.toolConfigRows[i].ToolConfigStrip.GetPreferredSize(
  228. new Size(this.toolConfigRows[i].ToolConfigStrip.Width, 1));
  229. this.toolConfigRows[i].ToolConfigStrip.Size = preferredSize;
  230. maxInsetWidth = Math.Max(maxInsetWidth, this.toolConfigRows[i].ToolConfigStrip.Width);
  231. y = this.toolConfigRows[i].ToolConfigStrip.Bottom + vMargin;
  232. }
  233. y += vMargin;
  234. this.bottomSeparator.Location = new Point(leftMargin, y);
  235. this.bottomSeparator.Width = insetWidth;
  236. this.bottomSeparator.Visible = false;
  237. y += this.bottomSeparator.Height;
  238. this.cancelButton.Location = new Point(ClientSize.Width - rightMargin - this.cancelButton.Width, y);
  239. this.saveButton.Location = new Point(
  240. this.cancelButton.Left - buttonHMargin - this.saveButton.Width,
  241. this.cancelButton.Top);
  242. this.resetButton.Location = new Point(leftMargin, this.saveButton.Top);
  243. this.loadFromToolBarButton.Location = new Point(this.resetButton.Right + buttonHMargin, this.resetButton.Top);
  244. y = this.resetButton.Bottom + bottomMargin;
  245. this.ClientSize = new Size(leftMargin + maxInsetWidth + rightMargin, y);
  246. if (IsHandleCreated && maxInsetWidth > insetWidth)
  247. {
  248. BeginInvoke(new Procedure(PerformLayout), null);
  249. }
  250. base.OnLayout(levent);
  251. }
  252. private void InitializeComponent()
  253. {
  254. this.cancelButton = new Button();
  255. this.saveButton = new Button();
  256. this.introText = new Label();
  257. this.defaultToolText = new Label();
  258. this.resetButton = new Button();
  259. this.loadFromToolBarButton = new Button();
  260. this.toolChooserStrip = new ToolChooserStrip();
  261. this.bottomSeparator = new HeaderLabel();
  262. this.SuspendLayout();
  263. //
  264. // cancelButton
  265. //
  266. this.cancelButton.Name = "cancelButton";
  267. this.cancelButton.AutoSize = true;
  268. this.cancelButton.Click += new EventHandler(CancelButton_Click);
  269. this.cancelButton.TabIndex = 3;
  270. //
  271. // saveButton
  272. //
  273. this.saveButton.Name = "saveButton";
  274. this.saveButton.AutoSize = true;
  275. this.saveButton.Click += new EventHandler(SaveButton_Click);
  276. this.saveButton.TabIndex = 2;
  277. //
  278. // introText
  279. //
  280. this.introText.Name = "introText";
  281. this.introText.TabStop = false;
  282. //
  283. // defaultToolText
  284. //
  285. this.defaultToolText.Name = "defaultToolText";
  286. this.defaultToolText.AutoSize = true;
  287. this.defaultToolText.TabStop = false;
  288. //
  289. // resetButton
  290. //
  291. this.resetButton.Name = "resetButton";
  292. this.resetButton.AutoSize = true;
  293. this.resetButton.Click += new EventHandler(ResetButton_Click);
  294. this.resetButton.TabIndex = 0;
  295. //
  296. // loadFromToolBarButton
  297. //
  298. this.loadFromToolBarButton.Name = "loadFromToolBarButton";
  299. this.loadFromToolBarButton.AutoSize = true;
  300. this.loadFromToolBarButton.Click += new EventHandler(LoadFromToolBarButton_Click);
  301. this.loadFromToolBarButton.TabIndex = 1;
  302. //
  303. // toolChooserStrip
  304. //
  305. this.toolChooserStrip.Name = "toolChooserStrip";
  306. this.toolChooserStrip.Dock = DockStyle.None;
  307. this.toolChooserStrip.GripStyle = ToolStripGripStyle.Hidden;
  308. this.toolChooserStrip.ShowChooseDefaults = false;
  309. this.toolChooserStrip.UseToolNameForLabel = true;
  310. this.toolChooserStrip.ToolClicked += new ToolClickedEventHandler(ToolChooserStrip_ToolClicked);
  311. //
  312. // bottomSeparator
  313. //
  314. this.bottomSeparator.Name = "bottomSeparator";
  315. this.bottomSeparator.RightMargin = 0;
  316. //
  317. // ChooseToolDefaultsDialog
  318. //
  319. this.AcceptButton = this.saveButton;
  320. this.AutoScaleDimensions = new System.Drawing.SizeF(96F, 96F);
  321. this.AutoScaleMode = AutoScaleMode.Dpi;
  322. this.CancelButton = this.cancelButton;
  323. this.ClientSize = new System.Drawing.Size(448, 373);
  324. this.Controls.Add(this.resetButton);
  325. this.Controls.Add(this.loadFromToolBarButton);
  326. this.Controls.Add(this.introText);
  327. this.Controls.Add(this.defaultToolText);
  328. this.Controls.Add(this.saveButton);
  329. this.Controls.Add(this.cancelButton);
  330. this.Controls.Add(this.toolChooserStrip);
  331. this.Controls.Add(this.bottomSeparator);
  332. this.FormBorderStyle = FormBorderStyle.FixedDialog;
  333. this.Location = new System.Drawing.Point(0, 0);
  334. this.MinimizeBox = false;
  335. this.MaximizeBox = false;
  336. this.Name = "ChooseToolDefaultsDialog";
  337. this.ShowInTaskbar = false;
  338. this.StartPosition = FormStartPosition.CenterParent;
  339. this.ResumeLayout(false);
  340. this.PerformLayout();
  341. }
  342. private void LoadFromToolBarButton_Click(object sender, EventArgs e)
  343. {
  344. ToolType = this.toolBarToolType;
  345. LoadUIFromAppEnvironment(this.toolBarAppEnvironment);
  346. }
  347. private void ToolChooserStrip_ToolClicked(object sender, ToolClickedEventArgs e)
  348. {
  349. ToolType = e.ToolType;
  350. }
  351. private void ResetButton_Click(object sender, EventArgs e)
  352. {
  353. AppEnvironment defaults = new AppEnvironment();
  354. defaults.SetToDefaults();
  355. ToolType = Tool.DefaultToolType;
  356. LoadUIFromAppEnvironment(defaults);
  357. }
  358. private void CancelButton_Click(object sender, EventArgs e)
  359. {
  360. DialogResult = DialogResult.Cancel;
  361. Close();
  362. }
  363. private void SaveButton_Click(object sender, EventArgs e)
  364. {
  365. DialogResult = DialogResult.Yes;
  366. Close();
  367. }
  368. }
  369. }