/src/ViewConfigStrip.cs
C# | 478 lines | 389 code | 64 blank | 25 comment | 69 complexity | bf33b589433b6be7a4f4e933d84b25d6 MD5 | raw file
- /////////////////////////////////////////////////////////////////////////////////
- // Paint.NET //
- // Copyright (C) dotPDN LLC, Rick Brewster, Tom Jackson, and contributors. //
- // Portions Copyright (C) Microsoft Corporation. All Rights Reserved. //
- // See src/Resources/Files/License.txt for full licensing and attribution //
- // details. //
- // . //
- /////////////////////////////////////////////////////////////////////////////////
-
- using PaintDotNet.SystemLayer;
- using System;
- using System.ComponentModel;
- using System.Drawing;
- using System.Windows.Forms;
-
- namespace PaintDotNet
- {
- internal sealed class ViewConfigStrip
- : ToolStripEx
- {
- private readonly string _windowText;
- private readonly string _percentageFormat;
- private ToolStripSeparator _separator0;
- private ScaleFactor _scaleFactor;
- private ToolStripButton _zoomOutButton;
- private ToolStripButton _zoomInButton;
- private ToolStripComboBox _zoomComboBox;
- private ToolStripSeparator _separator1;
- private ToolStripButton _gridButton;
- private ToolStripButton _rulersButton;
- private ToolStripLabel _unitsLabel;
- private UnitsComboBoxStrip _unitsComboBox;
-
- private int _scaleFactorRecursionDepth;
- private int _suspendEvents;
- private int _ignoreZoomChanges;
-
- public void SuspendEvents()
- {
- ++_suspendEvents;
- }
-
- public void ResumeEvents()
- {
- --_suspendEvents;
- }
-
- public void BeginZoomChanges()
- {
- ++_ignoreZoomChanges;
- }
-
- public void EndZoomChanges()
- {
- --_ignoreZoomChanges;
- }
-
- private ZoomBasis _zoomBasis;
- public ZoomBasis ZoomBasis
- {
- get
- {
- return _zoomBasis;
- }
-
- set
- {
- if (_zoomBasis == value) return;
- _zoomBasis = value;
- OnZoomBasisChanged();
- }
- }
-
- public bool DrawGrid
- {
- get
- {
- return _gridButton.Checked;
- }
-
- set
- {
- if (_gridButton.Checked == value) return;
- _gridButton.Checked = value;
- OnDrawGridChanged();
- }
- }
-
- public bool RulersEnabled
- {
- get
- {
- return _rulersButton.Checked;
- }
-
- set
- {
- if (_rulersButton.Checked == value) return;
- _rulersButton.Checked = value;
- OnRulersEnabledChanged();
- }
- }
-
- public MeasurementUnit Units
- {
- get
- {
- return _unitsComboBox.Units;
- }
-
- set
- {
- _unitsComboBox.Units = value;
- }
- }
-
- public ScaleFactor ScaleFactor
- {
- get
- {
- return _scaleFactor;
- }
-
- set
- {
- if (_scaleFactor.Ratio == value.Ratio) return;
- _scaleFactor = value;
- ++_scaleFactorRecursionDepth;
-
- // Prevent infinite recursion that was reported by one person.
- // This may cause the scale factor to settle on a less than
- // desirable value, but this is obviously more desirable than
- // a StackOverflow crash.
- if (_scaleFactorRecursionDepth < 100)
- {
- OnZoomScaleChanged();
- }
-
- --_scaleFactorRecursionDepth;
- }
- }
-
- public ViewConfigStrip()
- {
- SuspendLayout();
- InitializeComponent();
-
- _windowText = EnumLocalizer.EnumValueToLocalizedName(typeof(ZoomBasis), ZoomBasis.FitToWindow);
- _percentageFormat = PdnResources.GetString("ZoomConfigWidget.Percentage.Format");
-
- double[] zoomValues = ScaleFactor.PresetValues;
-
- if (_zoomComboBox.ComboBox != null) _zoomComboBox.ComboBox.SuspendLayout();
-
- 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
- for (int i = zoomValues.Length - 1; i >= 0; --i)
- {
- string zoomValueString = (zoomValues[i] * 100.0).ToString();
- string zoomItemString = string.Format(_percentageFormat, zoomValueString);
-
- if (zoomValues[i] == 1.0)
- {
- percent100 = zoomItemString;
- }
-
- _zoomComboBox.Items.Add(zoomItemString);
- }
-
- _zoomComboBox.Items.Add(_windowText);
- if (_zoomComboBox.ComboBox != null) _zoomComboBox.ComboBox.ResumeLayout(false);
- _zoomComboBox.Size = new Size(UI.ScaleWidth(_zoomComboBox.Width), _zoomComboBox.Height);
-
- _unitsLabel.Text = PdnResources.GetString("WorkspaceOptionsConfigWidget.UnitsLabel.Text");
-
- _zoomComboBox.Text = percent100;
- ScaleFactor = ScaleFactor.OneToOne;
-
- _zoomOutButton.Image = PdnResources.GetImageResource("Icons.MenuViewZoomOutIcon.png").Reference;
- _zoomInButton.Image = PdnResources.GetImageResource("Icons.MenuViewZoomInIcon.png").Reference;
- _gridButton.Image = PdnResources.GetImageResource("Icons.MenuViewGridIcon.png").Reference;
- _rulersButton.Image = PdnResources.GetImageResource("Icons.MenuViewRulersIcon.png").Reference;
-
- _zoomOutButton.ToolTipText = PdnResources.GetString("ZoomConfigWidget.ZoomOutButton.ToolTipText");
- _zoomInButton.ToolTipText = PdnResources.GetString("ZoomConfigWidget.ZoomInButton.ToolTipText");
- _gridButton.ToolTipText = PdnResources.GetString("WorkspaceOptionsConfigWidget.DrawGridToggleButton.ToolTipText");
- _rulersButton.ToolTipText = PdnResources.GetString("WorkspaceOptionsConfigWidget.RulersToggleButton.ToolTipText");
-
- _unitsComboBox.Size = new Size(UI.ScaleWidth(_unitsComboBox.Width), _unitsComboBox.Height);
-
- _zoomBasis = ZoomBasis.ScaleFactor;
- ScaleFactor = ScaleFactor.OneToOne;
-
- ResumeLayout(false);
- }
-
- private void InitializeComponent()
- {
- _separator0 = new ToolStripSeparator();
- _zoomOutButton = new ToolStripButton();
- _zoomComboBox = new ToolStripComboBox();
- _zoomInButton = new ToolStripButton();
- _separator1 = new ToolStripSeparator();
- _gridButton = new ToolStripButton();
- _rulersButton = new ToolStripButton();
- _unitsLabel = new ToolStripLabel();
- _unitsComboBox = new UnitsComboBoxStrip();
- SuspendLayout();
- //
- // separator0
- //
- _separator0.Name = "separator0";
- //
- // zoomComboBox
- //
- _zoomComboBox.KeyPress += ZoomComboBoxKeyPress;
- _zoomComboBox.Validating += ZoomComboBoxValidating;
- _zoomComboBox.SelectedIndexChanged += ZoomComboBoxSelectedIndexChanged;
- _zoomComboBox.Size = new Size(75, _zoomComboBox.Height);
- _zoomComboBox.MaxDropDownItems = 99;
- //
- // unitsComboBox
- //
- _unitsComboBox.UnitsChanged += UnitsComboBoxUnitsChanged;
- _unitsComboBox.LowercaseStrings = false;
- _unitsComboBox.UnitsDisplayType = UnitsDisplayType.Plural;
- _unitsComboBox.Units = MeasurementUnit.Pixel;
- _unitsComboBox.Size = new Size(90, _unitsComboBox.Height);
- //
- // ViewConfigStrip
- //
- Items.Add(_separator0);
- Items.Add(_zoomOutButton);
- Items.Add(_zoomComboBox);
- Items.Add(_zoomInButton);
- Items.Add(_separator1);
- Items.Add(_gridButton);
- Items.Add(_rulersButton);
- Items.Add(_unitsLabel);
- Items.Add(_unitsComboBox);
- ResumeLayout(false);
- }
-
- private void UnitsComboBoxUnitsChanged(object sender, EventArgs e)
- {
- OnUnitsChanged();
- }
-
- private void SetZoomText()
- {
- if (_ignoreZoomChanges != 0) return;
- _zoomComboBox.BackColor = SystemColors.Window;
- string newText = _zoomComboBox.Text;
-
- switch (_zoomBasis)
- {
- case ZoomBasis.FitToWindow:
- newText = _windowText;
- break;
-
- case ZoomBasis.ScaleFactor:
- newText = _scaleFactor.ToString();
- break;
- }
-
- if (_zoomComboBox.Text == newText) return;
- _zoomComboBox.Text = newText;
- if (_zoomComboBox.ComboBox != null) _zoomComboBox.ComboBox.Update();
- }
-
- public event EventHandler DrawGridChanged;
- private void OnDrawGridChanged()
- {
- if (DrawGridChanged != null)
- {
- DrawGridChanged(this, EventArgs.Empty);
- }
- }
-
- public event EventHandler RulersEnabledChanged;
- private void OnRulersEnabledChanged()
- {
- if (RulersEnabledChanged != null)
- {
- RulersEnabledChanged(this, EventArgs.Empty);
- }
- }
-
- public event EventHandler UnitsChanged;
- private void OnUnitsChanged()
- {
- if (UnitsChanged != null)
- {
- UnitsChanged(this, EventArgs.Empty);
- }
- }
-
- public event EventHandler ZoomScaleChanged;
- private void OnZoomScaleChanged()
- {
- if (_zoomBasis != ZoomBasis.ScaleFactor) return;
- SetZoomText();
-
- if (ZoomScaleChanged != null)
- {
- ZoomScaleChanged(this, EventArgs.Empty);
- }
- }
-
- public event EventHandler ZoomIn;
- private void OnZoomIn()
- {
- if (ZoomIn != null)
- {
- ZoomIn(this, EventArgs.Empty);
- }
- }
-
- public event EventHandler ZoomOut;
- private void OnZoomOut()
- {
- if (ZoomOut != null)
- {
- ZoomOut(this, EventArgs.Empty);
- }
- }
-
- public void PerformZoomBasisChanged()
- {
- OnZoomBasisChanged();
- }
-
- public event EventHandler ZoomBasisChanged;
- private void OnZoomBasisChanged()
- {
- SetZoomText();
-
- if (ZoomBasisChanged != null)
- {
- ZoomBasisChanged(this, EventArgs.Empty);
- }
- }
-
- public void PerformZoomScaleChanged()
- {
- OnZoomScaleChanged();
- }
-
- private void ZoomComboBoxValidating(object sender, CancelEventArgs e)
- {
- try
- {
- int val = 1;
- e.Cancel = false;
-
- if (_zoomComboBox.Text == _windowText)
- {
- ZoomBasis = ZoomBasis.FitToWindow;
- }
- else
- {
- try
- {
- string text = _zoomComboBox.Text;
-
- if (text.Length == 0)
- {
- e.Cancel = true;
- }
- else
- {
- if (text[text.Length - 1] == '%')
- {
- text = text.Substring(0, text.Length - 1);
- }
- else if (text[0] == '%')
- {
- text = text.Substring(1);
- }
-
- val = (int)Math.Round(double.Parse(text));
- ZoomBasis = ZoomBasis.ScaleFactor;
- }
- }
-
- catch (FormatException)
- {
- e.Cancel = true;
- }
-
- catch (OverflowException)
- {
- e.Cancel = true;
- }
-
- if (e.Cancel)
- {
- _zoomComboBox.BackColor = Color.Red;
- _zoomComboBox.ToolTipText = PdnResources.GetString("ZoomConfigWidget.Error.InvalidNumber");
- }
- else
- {
- if (val < 1)
- {
- e.Cancel = true;
- _zoomComboBox.BackColor = Color.Red;
- _zoomComboBox.ToolTipText = PdnResources.GetString("ZoomConfigWidget.Error.TooSmall");
- }
- else if (val > 3200)
- {
- e.Cancel = true;
- _zoomComboBox.BackColor = Color.Red;
- _zoomComboBox.ToolTipText = PdnResources.GetString("ZoomConfigWidget.Error.TooLarge");
- }
- else
- {
- // Clear the error
- e.Cancel = false;
- _zoomComboBox.ToolTipText = string.Empty;
- _zoomComboBox.BackColor = SystemColors.Window;
- ScaleFactor = new ScaleFactor(val, 100);
- SuspendEvents();
- ZoomBasis = ZoomBasis.ScaleFactor;
- ResumeEvents();
- }
- }
- }
- }
-
- catch (FormatException)
- {
- }
- }
-
- private void ZoomComboBoxSelectedIndexChanged(object sender, EventArgs e)
- {
- if (_suspendEvents == 0)
- {
- ZoomComboBoxValidating(sender, new CancelEventArgs(false));
- }
- }
-
- private void ZoomComboBoxKeyPress(object sender, KeyPressEventArgs e)
- {
- if (e.KeyChar != '\n' && e.KeyChar != '\r') return;
- ZoomComboBoxValidating(sender, new CancelEventArgs(false));
- _zoomComboBox.Select(0, _zoomComboBox.Text.Length);
- }
-
- protected override void OnItemClicked(ToolStripItemClickedEventArgs e)
- {
-
- if (e.ClickedItem == _zoomInButton)
- {
- Tracing.LogFeature("ViewConfigStrip(ZoomIn)");
- OnZoomIn();
- }
- else if (e.ClickedItem == _zoomOutButton)
- {
- Tracing.LogFeature("ViewConfigStrip(ZoomOut)");
- OnZoomOut();
- }
- else if (e.ClickedItem == _rulersButton)
- {
- Tracing.LogFeature("ViewConfigStrip(Rulers)");
- _rulersButton.Checked = !_rulersButton.Checked;
- OnRulersEnabledChanged();
- }
- else if (e.ClickedItem == _gridButton)
- {
- Tracing.LogFeature("ViewConfigStrip(Grid)");
- _gridButton.Checked = !_gridButton.Checked;
- OnDrawGridChanged();
- }
-
- base.OnItemClicked(e);
- }
- }
- }