/src/Core/UnitsComboBoxHandler.cs
C# | 289 lines | 236 code | 43 blank | 10 comment | 27 complexity | c4638a61e8f89ee33ff241e35d93fe70 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 System;
- using System.Collections;
- using System.ComponentModel;
- using System.Windows.Forms;
-
- namespace PaintDotNet
- {
- public sealed class UnitsComboBoxHandler
- : IUnitsComboBox
- {
- private readonly ComboBox _comboBox;
-
- [Browsable(false)]
- public ComboBox ComboBox
- {
- get
- {
- return _comboBox;
- }
- }
-
- public UnitsComboBoxHandler(ComboBox comboBox)
- {
- _comboBox = comboBox;
- _comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
- _comboBox.SelectedIndexChanged += ComboBoxSelectedIndexChanged;
- ReloadItems();
- }
-
- private bool _lowercase = true;
-
- private Hashtable _unitsToString;
- private Hashtable _stringToUnits;
-
- // maps from MeasurementUnit->bool for whether that item should be in the list or not
- private Hashtable _measurementItems;
-
- private UnitsDisplayType _unitsDisplayType = UnitsDisplayType.Plural;
-
- [DefaultValue(UnitsDisplayType.Plural)]
- public UnitsDisplayType UnitsDisplayType
- {
- get
- {
- return _unitsDisplayType;
- }
-
- set
- {
- if (_unitsDisplayType == value) return;
- _unitsDisplayType = value;
- ReloadItems();
- }
- }
-
- [DefaultValue(true)]
- public bool LowercaseStrings
- {
- get
- {
- return _lowercase;
- }
-
- set
- {
- if (_lowercase == value) return;
- _lowercase = value;
- ReloadItems();
- }
- }
-
- [DefaultValue(MeasurementUnit.Pixel)]
- public MeasurementUnit Units
- {
- get
- {
- object selected = _stringToUnits[ComboBox.SelectedItem];
- return (MeasurementUnit)selected;
- }
-
- set
- {
- object selectMe = _unitsToString[value];
- ComboBox.SelectedItem = selectMe;
- }
- }
-
- [Browsable(false)]
- public string UnitsText
- {
- get
- {
- if (ComboBox.SelectedItem == null)
- {
- return string.Empty;
- }
- return (string)ComboBox.SelectedItem;
- }
- }
-
- [DefaultValue(true)]
- public bool PixelsAvailable
- {
- get
- {
- return (bool)_measurementItems[MeasurementUnit.Pixel];
- }
-
- set
- {
- if (value == PixelsAvailable) return;
- if (value)
- {
- AddUnit(MeasurementUnit.Pixel);
- }
- else
- {
- if (Units == MeasurementUnit.Pixel)
- {
- if (InchesAvailable)
- {
- Units = MeasurementUnit.Inch;
- }
- else if (CentimetersAvailable)
- {
- Units = MeasurementUnit.Centimeter;
- }
- }
-
- RemoveUnit(MeasurementUnit.Pixel);
- }
- }
- }
-
- [DefaultValue(true)]
- public bool InchesAvailable
- {
- get
- {
- return (bool)_measurementItems[MeasurementUnit.Inch];
- }
- }
-
- [DefaultValue(true)]
- public bool CentimetersAvailable
- {
- get
- {
- return (bool)_measurementItems[MeasurementUnit.Centimeter];
- }
- }
-
- public void RemoveUnit(MeasurementUnit removeMe)
- {
- InitMeasurementItems();
- _measurementItems[removeMe] = false;
- ReloadItems();
- }
-
- public void AddUnit(MeasurementUnit addMe)
- {
- InitMeasurementItems();
- _measurementItems[addMe] = true;
- ReloadItems();
- }
-
- private void InitMeasurementItems()
- {
- if (_measurementItems == null)
- {
- _measurementItems = new Hashtable
- {
- {MeasurementUnit.Pixel, true},
- {MeasurementUnit.Centimeter, true},
- {MeasurementUnit.Inch, true}
- };
- }
- }
-
- private void ReloadItems()
- {
- string suffix;
-
- switch (_unitsDisplayType)
- {
- case UnitsDisplayType.Plural:
- suffix = ".Plural";
- break;
-
- case UnitsDisplayType.Singular:
- suffix = string.Empty;
- break;
-
- case UnitsDisplayType.Ratio:
- suffix = ".Ratio";
- break;
-
- default:
- throw new InvalidEnumArgumentException("UnitsDisplayType");
- }
-
- InitMeasurementItems();
-
- MeasurementUnit oldUnits = _unitsToString == null ? MeasurementUnit.Pixel : Units;
-
- ComboBox.Items.Clear();
-
- string pixelsString = PdnResources.GetString("MeasurementUnit.Pixel" + suffix);
- string inchesString = PdnResources.GetString("MeasurementUnit.Inch" + suffix);
- string centimetersString = PdnResources.GetString("MeasurementUnit.Centimeter" + suffix);
-
- if (_lowercase)
- {
- // TODO: we shouldn't really be using ToLower() here, these should be separately localizable strings
-
- pixelsString = pixelsString.ToLower();
- inchesString = inchesString.ToLower();
- centimetersString = centimetersString.ToLower();
- }
-
- _unitsToString = new Hashtable
- {
- {MeasurementUnit.Pixel, pixelsString},
- {MeasurementUnit.Inch, inchesString},
- {MeasurementUnit.Centimeter, centimetersString}
- };
-
- _stringToUnits = new Hashtable();
-
- if ((bool)_measurementItems[MeasurementUnit.Pixel])
- {
- _stringToUnits.Add(pixelsString, MeasurementUnit.Pixel);
- ComboBox.Items.Add(pixelsString);
- }
-
- if ((bool)_measurementItems[MeasurementUnit.Inch])
- {
- _stringToUnits.Add(inchesString, MeasurementUnit.Inch);
- ComboBox.Items.Add(inchesString);
- }
-
- if ((bool)_measurementItems[MeasurementUnit.Centimeter])
- {
- _stringToUnits.Add(centimetersString, MeasurementUnit.Centimeter);
- ComboBox.Items.Add(centimetersString);
- }
-
- if (!(bool)_measurementItems[oldUnits])
- {
- if (ComboBox.Items.Count == 0)
- {
- ComboBox.SelectedItem = null;
- }
- else
- {
- ComboBox.SelectedIndex = 0;
- }
- }
- else
- {
- Units = oldUnits;
- }
- }
-
- public event EventHandler UnitsChanged;
-
- private void OnUnitsChanged()
- {
- if (UnitsChanged != null)
- {
- UnitsChanged(this, EventArgs.Empty);
- }
- }
-
- private void ComboBoxSelectedIndexChanged(object sender, EventArgs e)
- {
- OnUnitsChanged();
- }
- }
- }