ViewConfigStrip.cs 14 KB

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