PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/ViewConfigStrip.cs

https://bitbucket.org/tuldok89/openpdn
C# | 478 lines | 389 code | 64 blank | 25 comment | 69 complexity | bf33b589433b6be7a4f4e933d84b25d6 MD5 | raw file
  1. /////////////////////////////////////////////////////////////////////////////////
  2. // Paint.NET //
  3. // Copyright (C) dotPDN LLC, Rick Brewster, Tom Jackson, and 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.ComponentModel;
  12. using System.Drawing;
  13. using System.Windows.Forms;
  14. namespace PaintDotNet
  15. {
  16. internal sealed class ViewConfigStrip
  17. : ToolStripEx
  18. {
  19. private readonly string _windowText;
  20. private readonly string _percentageFormat;
  21. private ToolStripSeparator _separator0;
  22. private ScaleFactor _scaleFactor;
  23. private ToolStripButton _zoomOutButton;
  24. private ToolStripButton _zoomInButton;
  25. private ToolStripComboBox _zoomComboBox;
  26. private ToolStripSeparator _separator1;
  27. private ToolStripButton _gridButton;
  28. private ToolStripButton _rulersButton;
  29. private ToolStripLabel _unitsLabel;
  30. private UnitsComboBoxStrip _unitsComboBox;
  31. private int _scaleFactorRecursionDepth;
  32. private int _suspendEvents;
  33. private int _ignoreZoomChanges;
  34. public void SuspendEvents()
  35. {
  36. ++_suspendEvents;
  37. }
  38. public void ResumeEvents()
  39. {
  40. --_suspendEvents;
  41. }
  42. public void BeginZoomChanges()
  43. {
  44. ++_ignoreZoomChanges;
  45. }
  46. public void EndZoomChanges()
  47. {
  48. --_ignoreZoomChanges;
  49. }
  50. private ZoomBasis _zoomBasis;
  51. public ZoomBasis ZoomBasis
  52. {
  53. get
  54. {
  55. return _zoomBasis;
  56. }
  57. set
  58. {
  59. if (_zoomBasis == value) return;
  60. _zoomBasis = value;
  61. OnZoomBasisChanged();
  62. }
  63. }
  64. public bool DrawGrid
  65. {
  66. get
  67. {
  68. return _gridButton.Checked;
  69. }
  70. set
  71. {
  72. if (_gridButton.Checked == value) return;
  73. _gridButton.Checked = value;
  74. OnDrawGridChanged();
  75. }
  76. }
  77. public bool RulersEnabled
  78. {
  79. get
  80. {
  81. return _rulersButton.Checked;
  82. }
  83. set
  84. {
  85. if (_rulersButton.Checked == value) return;
  86. _rulersButton.Checked = value;
  87. OnRulersEnabledChanged();
  88. }
  89. }
  90. public MeasurementUnit Units
  91. {
  92. get
  93. {
  94. return _unitsComboBox.Units;
  95. }
  96. set
  97. {
  98. _unitsComboBox.Units = value;
  99. }
  100. }
  101. public ScaleFactor ScaleFactor
  102. {
  103. get
  104. {
  105. return _scaleFactor;
  106. }
  107. set
  108. {
  109. if (_scaleFactor.Ratio == value.Ratio) return;
  110. _scaleFactor = value;
  111. ++_scaleFactorRecursionDepth;
  112. // Prevent infinite recursion that was reported by one person.
  113. // This may cause the scale factor to settle on a less than
  114. // desirable value, but this is obviously more desirable than
  115. // a StackOverflow crash.
  116. if (_scaleFactorRecursionDepth < 100)
  117. {
  118. OnZoomScaleChanged();
  119. }
  120. --_scaleFactorRecursionDepth;
  121. }
  122. }
  123. public ViewConfigStrip()
  124. {
  125. SuspendLayout();
  126. InitializeComponent();
  127. _windowText = EnumLocalizer.EnumValueToLocalizedName(typeof(ZoomBasis), ZoomBasis.FitToWindow);
  128. _percentageFormat = PdnResources.GetString("ZoomConfigWidget.Percentage.Format");
  129. double[] zoomValues = ScaleFactor.PresetValues;
  130. if (_zoomComboBox.ComboBox != null) _zoomComboBox.ComboBox.SuspendLayout();
  131. string percent100 = null; // ScaleFactor.PresetValues guarantees that 1.0, or "100%" is in the list, but the compiler can't be shown this so we must assign a value here
  132. for (int i = zoomValues.Length - 1; i >= 0; --i)
  133. {
  134. string zoomValueString = (zoomValues[i] * 100.0).ToString();
  135. string zoomItemString = string.Format(_percentageFormat, zoomValueString);
  136. if (zoomValues[i] == 1.0)
  137. {
  138. percent100 = zoomItemString;
  139. }
  140. _zoomComboBox.Items.Add(zoomItemString);
  141. }
  142. _zoomComboBox.Items.Add(_windowText);
  143. if (_zoomComboBox.ComboBox != null) _zoomComboBox.ComboBox.ResumeLayout(false);
  144. _zoomComboBox.Size = new Size(UI.ScaleWidth(_zoomComboBox.Width), _zoomComboBox.Height);
  145. _unitsLabel.Text = PdnResources.GetString("WorkspaceOptionsConfigWidget.UnitsLabel.Text");
  146. _zoomComboBox.Text = percent100;
  147. ScaleFactor = ScaleFactor.OneToOne;
  148. _zoomOutButton.Image = PdnResources.GetImageResource("Icons.MenuViewZoomOutIcon.png").Reference;
  149. _zoomInButton.Image = PdnResources.GetImageResource("Icons.MenuViewZoomInIcon.png").Reference;
  150. _gridButton.Image = PdnResources.GetImageResource("Icons.MenuViewGridIcon.png").Reference;
  151. _rulersButton.Image = PdnResources.GetImageResource("Icons.MenuViewRulersIcon.png").Reference;
  152. _zoomOutButton.ToolTipText = PdnResources.GetString("ZoomConfigWidget.ZoomOutButton.ToolTipText");
  153. _zoomInButton.ToolTipText = PdnResources.GetString("ZoomConfigWidget.ZoomInButton.ToolTipText");
  154. _gridButton.ToolTipText = PdnResources.GetString("WorkspaceOptionsConfigWidget.DrawGridToggleButton.ToolTipText");
  155. _rulersButton.ToolTipText = PdnResources.GetString("WorkspaceOptionsConfigWidget.RulersToggleButton.ToolTipText");
  156. _unitsComboBox.Size = new Size(UI.ScaleWidth(_unitsComboBox.Width), _unitsComboBox.Height);
  157. _zoomBasis = ZoomBasis.ScaleFactor;
  158. ScaleFactor = ScaleFactor.OneToOne;
  159. ResumeLayout(false);
  160. }
  161. private void InitializeComponent()
  162. {
  163. _separator0 = new ToolStripSeparator();
  164. _zoomOutButton = new ToolStripButton();
  165. _zoomComboBox = new ToolStripComboBox();
  166. _zoomInButton = new ToolStripButton();
  167. _separator1 = new ToolStripSeparator();
  168. _gridButton = new ToolStripButton();
  169. _rulersButton = new ToolStripButton();
  170. _unitsLabel = new ToolStripLabel();
  171. _unitsComboBox = new UnitsComboBoxStrip();
  172. SuspendLayout();
  173. //
  174. // separator0
  175. //
  176. _separator0.Name = "separator0";
  177. //
  178. // zoomComboBox
  179. //
  180. _zoomComboBox.KeyPress += ZoomComboBoxKeyPress;
  181. _zoomComboBox.Validating += ZoomComboBoxValidating;
  182. _zoomComboBox.SelectedIndexChanged += ZoomComboBoxSelectedIndexChanged;
  183. _zoomComboBox.Size = new Size(75, _zoomComboBox.Height);
  184. _zoomComboBox.MaxDropDownItems = 99;
  185. //
  186. // unitsComboBox
  187. //
  188. _unitsComboBox.UnitsChanged += UnitsComboBoxUnitsChanged;
  189. _unitsComboBox.LowercaseStrings = false;
  190. _unitsComboBox.UnitsDisplayType = UnitsDisplayType.Plural;
  191. _unitsComboBox.Units = MeasurementUnit.Pixel;
  192. _unitsComboBox.Size = new Size(90, _unitsComboBox.Height);
  193. //
  194. // ViewConfigStrip
  195. //
  196. Items.Add(_separator0);
  197. Items.Add(_zoomOutButton);
  198. Items.Add(_zoomComboBox);
  199. Items.Add(_zoomInButton);
  200. Items.Add(_separator1);
  201. Items.Add(_gridButton);
  202. Items.Add(_rulersButton);
  203. Items.Add(_unitsLabel);
  204. Items.Add(_unitsComboBox);
  205. ResumeLayout(false);
  206. }
  207. private void UnitsComboBoxUnitsChanged(object sender, EventArgs e)
  208. {
  209. OnUnitsChanged();
  210. }
  211. private void SetZoomText()
  212. {
  213. if (_ignoreZoomChanges != 0) return;
  214. _zoomComboBox.BackColor = SystemColors.Window;
  215. string newText = _zoomComboBox.Text;
  216. switch (_zoomBasis)
  217. {
  218. case ZoomBasis.FitToWindow:
  219. newText = _windowText;
  220. break;
  221. case ZoomBasis.ScaleFactor:
  222. newText = _scaleFactor.ToString();
  223. break;
  224. }
  225. if (_zoomComboBox.Text == newText) return;
  226. _zoomComboBox.Text = newText;
  227. if (_zoomComboBox.ComboBox != null) _zoomComboBox.ComboBox.Update();
  228. }
  229. public event EventHandler DrawGridChanged;
  230. private void OnDrawGridChanged()
  231. {
  232. if (DrawGridChanged != null)
  233. {
  234. DrawGridChanged(this, EventArgs.Empty);
  235. }
  236. }
  237. public event EventHandler RulersEnabledChanged;
  238. private void OnRulersEnabledChanged()
  239. {
  240. if (RulersEnabledChanged != null)
  241. {
  242. RulersEnabledChanged(this, EventArgs.Empty);
  243. }
  244. }
  245. public event EventHandler UnitsChanged;
  246. private void OnUnitsChanged()
  247. {
  248. if (UnitsChanged != null)
  249. {
  250. UnitsChanged(this, EventArgs.Empty);
  251. }
  252. }
  253. public event EventHandler ZoomScaleChanged;
  254. private void OnZoomScaleChanged()
  255. {
  256. if (_zoomBasis != ZoomBasis.ScaleFactor) return;
  257. SetZoomText();
  258. if (ZoomScaleChanged != null)
  259. {
  260. ZoomScaleChanged(this, EventArgs.Empty);
  261. }
  262. }
  263. public event EventHandler ZoomIn;
  264. private void OnZoomIn()
  265. {
  266. if (ZoomIn != null)
  267. {
  268. ZoomIn(this, EventArgs.Empty);
  269. }
  270. }
  271. public event EventHandler ZoomOut;
  272. private void OnZoomOut()
  273. {
  274. if (ZoomOut != null)
  275. {
  276. ZoomOut(this, EventArgs.Empty);
  277. }
  278. }
  279. public void PerformZoomBasisChanged()
  280. {
  281. OnZoomBasisChanged();
  282. }
  283. public event EventHandler ZoomBasisChanged;
  284. private void OnZoomBasisChanged()
  285. {
  286. SetZoomText();
  287. if (ZoomBasisChanged != null)
  288. {
  289. ZoomBasisChanged(this, EventArgs.Empty);
  290. }
  291. }
  292. public void PerformZoomScaleChanged()
  293. {
  294. OnZoomScaleChanged();
  295. }
  296. private void ZoomComboBoxValidating(object sender, CancelEventArgs e)
  297. {
  298. try
  299. {
  300. int val = 1;
  301. e.Cancel = false;
  302. if (_zoomComboBox.Text == _windowText)
  303. {
  304. ZoomBasis = ZoomBasis.FitToWindow;
  305. }
  306. else
  307. {
  308. try
  309. {
  310. string text = _zoomComboBox.Text;
  311. if (text.Length == 0)
  312. {
  313. e.Cancel = true;
  314. }
  315. else
  316. {
  317. if (text[text.Length - 1] == '%')
  318. {
  319. text = text.Substring(0, text.Length - 1);
  320. }
  321. else if (text[0] == '%')
  322. {
  323. text = text.Substring(1);
  324. }
  325. val = (int)Math.Round(double.Parse(text));
  326. ZoomBasis = ZoomBasis.ScaleFactor;
  327. }
  328. }
  329. catch (FormatException)
  330. {
  331. e.Cancel = true;
  332. }
  333. catch (OverflowException)
  334. {
  335. e.Cancel = true;
  336. }
  337. if (e.Cancel)
  338. {
  339. _zoomComboBox.BackColor = Color.Red;
  340. _zoomComboBox.ToolTipText = PdnResources.GetString("ZoomConfigWidget.Error.InvalidNumber");
  341. }
  342. else
  343. {
  344. if (val < 1)
  345. {
  346. e.Cancel = true;
  347. _zoomComboBox.BackColor = Color.Red;
  348. _zoomComboBox.ToolTipText = PdnResources.GetString("ZoomConfigWidget.Error.TooSmall");
  349. }
  350. else if (val > 3200)
  351. {
  352. e.Cancel = true;
  353. _zoomComboBox.BackColor = Color.Red;
  354. _zoomComboBox.ToolTipText = PdnResources.GetString("ZoomConfigWidget.Error.TooLarge");
  355. }
  356. else
  357. {
  358. // Clear the error
  359. e.Cancel = false;
  360. _zoomComboBox.ToolTipText = string.Empty;
  361. _zoomComboBox.BackColor = SystemColors.Window;
  362. ScaleFactor = new ScaleFactor(val, 100);
  363. SuspendEvents();
  364. ZoomBasis = ZoomBasis.ScaleFactor;
  365. ResumeEvents();
  366. }
  367. }
  368. }
  369. }
  370. catch (FormatException)
  371. {
  372. }
  373. }
  374. private void ZoomComboBoxSelectedIndexChanged(object sender, EventArgs e)
  375. {
  376. if (_suspendEvents == 0)
  377. {
  378. ZoomComboBoxValidating(sender, new CancelEventArgs(false));
  379. }
  380. }
  381. private void ZoomComboBoxKeyPress(object sender, KeyPressEventArgs e)
  382. {
  383. if (e.KeyChar != '\n' && e.KeyChar != '\r') return;
  384. ZoomComboBoxValidating(sender, new CancelEventArgs(false));
  385. _zoomComboBox.Select(0, _zoomComboBox.Text.Length);
  386. }
  387. protected override void OnItemClicked(ToolStripItemClickedEventArgs e)
  388. {
  389. if (e.ClickedItem == _zoomInButton)
  390. {
  391. Tracing.LogFeature("ViewConfigStrip(ZoomIn)");
  392. OnZoomIn();
  393. }
  394. else if (e.ClickedItem == _zoomOutButton)
  395. {
  396. Tracing.LogFeature("ViewConfigStrip(ZoomOut)");
  397. OnZoomOut();
  398. }
  399. else if (e.ClickedItem == _rulersButton)
  400. {
  401. Tracing.LogFeature("ViewConfigStrip(Rulers)");
  402. _rulersButton.Checked = !_rulersButton.Checked;
  403. OnRulersEnabledChanged();
  404. }
  405. else if (e.ClickedItem == _gridButton)
  406. {
  407. Tracing.LogFeature("ViewConfigStrip(Grid)");
  408. _gridButton.Checked = !_gridButton.Checked;
  409. OnDrawGridChanged();
  410. }
  411. base.OnItemClicked(e);
  412. }
  413. }
  414. }