123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345 |
- using System;
- using System.Collections;
- using System.ComponentModel;
- using System.Windows.Forms;
- namespace PaintDotNet
- {
- public sealed class UnitsComboBoxHandler
- : IUnitsComboBox
- {
- private ComboBox comboBox;
- [Browsable(false)]
- public ComboBox ComboBox
- {
- get
- {
- return this.comboBox;
- }
- }
- public UnitsComboBoxHandler(ComboBox comboBox)
- {
- this.comboBox = comboBox;
- this.comboBox.DropDownStyle = ComboBoxStyle.DropDownList;
- this.comboBox.SelectedIndexChanged += ComboBox_SelectedIndexChanged;
- 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 this.unitsDisplayType;
- }
- set
- {
- if (this.unitsDisplayType != value)
- {
- this.unitsDisplayType = value;
- ReloadItems();
- }
- }
- }
- [DefaultValue(true)]
- public bool LowercaseStrings
- {
- get
- {
- return this.lowercase;
- }
- set
- {
- if (this.lowercase != value)
- {
- this.lowercase = value;
- ReloadItems();
- }
- }
- }
- [DefaultValue(MeasurementUnit.Pixel)]
- public MeasurementUnit Units
- {
- get
- {
- object selected = this.stringToUnits[ComboBox.SelectedItem];
- return (MeasurementUnit)selected;
- }
- set
- {
- object selectMe = this.unitsToString[value];
- ComboBox.SelectedItem = selectMe;
- }
- }
- [Browsable(false)]
- public string UnitsText
- {
- get
- {
- if (ComboBox.SelectedItem == null)
- {
- return string.Empty;
- }
- else
- {
- return (string)ComboBox.SelectedItem;
- }
- }
- }
- [DefaultValue(true)]
- public bool PixelsAvailable
- {
- get
- {
- return (bool)this.measurementItems[MeasurementUnit.Pixel];
- }
- set
- {
- if (value != this.PixelsAvailable)
- {
- if (value)
- {
- AddUnit(MeasurementUnit.Pixel);
- }
- else
- {
- if (this.Units == MeasurementUnit.Pixel)
- {
- if (this.InchesAvailable)
- {
- this.Units = MeasurementUnit.Inch;
- }
- else if (this.MilsAvailable)
- {
- this.Units = MeasurementUnit.Mil;
- }
- else if (this.CentimetersAvailable)
- {
- this.Units = MeasurementUnit.Centimeter;
- }
- }
- RemoveUnit(MeasurementUnit.Pixel);
- }
- }
- }
- }
- [DefaultValue(true)]
- public bool InchesAvailable
- {
- get
- {
- return (bool)this.measurementItems[MeasurementUnit.Inch];
- }
- }
- [DefaultValue(true)]
- public bool MilsAvailable
- {
- get
- {
- return (bool)this.measurementItems[MeasurementUnit.Mil];
- }
- }
- [DefaultValue(true)]
- public bool CentimetersAvailable
- {
- get
- {
- return (bool)this.measurementItems[MeasurementUnit.Centimeter];
- }
- }
- public void RemoveUnit(MeasurementUnit removeMe)
- {
- InitMeasurementItems();
- this.measurementItems[removeMe] = false;
- ReloadItems();
- }
- public void AddUnit(MeasurementUnit addMe)
- {
- InitMeasurementItems();
- this.measurementItems[addMe] = true;
- ReloadItems();
- }
- private void InitMeasurementItems()
- {
- if (this.measurementItems == null)
- {
- this.measurementItems = new Hashtable();
- this.measurementItems.Add(MeasurementUnit.Pixel, true);
- this.measurementItems.Add(MeasurementUnit.Inch, true);
- this.measurementItems.Add(MeasurementUnit.Mil, true);
- this.measurementItems.Add(MeasurementUnit.Centimeter, true);
- this.measurementItems.Add(MeasurementUnit.Millimeter, true);
- this.measurementItems.Add(MeasurementUnit.Micron, true);
- this.measurementItems.Add(MeasurementUnit.Nano, true);
- }
- }
- private void ReloadItems()
- {
- string suffix;
- switch (this.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;
- if (this.unitsToString == null)
- {
- oldUnits = MeasurementUnit.Pixel;
- }
- else
- {
- oldUnits = this.Units;
- }
- ComboBox.Items.Clear();
- string pixelsString = PdnResources.GetString("MeasurementUnit.Pixel" + suffix);
- string inchesString = PdnResources.GetString("MeasurementUnit.Inch" + suffix);
- string milsString = PdnResources.GetString("MeasurementUnit.Mil" + suffix);
- string centimetersString = PdnResources.GetString("MeasurementUnit.Centimeter" + suffix);
- string millimeterString = PdnResources.GetString("Menu.Mm.text") + suffix;
- string micronString = PdnResources.GetString("Menu.Micron.text") + suffix;
- string nanoString = PdnResources.GetString("Menu.nanometer.text") + suffix;
- if (lowercase)
- {
- pixelsString = pixelsString.ToLower();
- inchesString = inchesString.ToLower();
- milsString = milsString.ToLower();
- centimetersString = centimetersString.ToLower();
- millimeterString = millimeterString.ToLower();
- micronString = micronString.ToLower();
- nanoString = nanoString.ToLower();
- }
- this.unitsToString = new Hashtable();
- this.unitsToString.Add(MeasurementUnit.Pixel, pixelsString);
- this.unitsToString.Add(MeasurementUnit.Inch, inchesString);
- this.unitsToString.Add(MeasurementUnit.Mil, milsString);
- this.unitsToString.Add(MeasurementUnit.Centimeter, centimetersString);
- this.unitsToString.Add(MeasurementUnit.Millimeter, millimeterString);
- this.unitsToString.Add(MeasurementUnit.Micron, micronString);
- this.unitsToString.Add(MeasurementUnit.Nano, nanoString);
- this.stringToUnits = new Hashtable();
- if ((bool)this.measurementItems[MeasurementUnit.Pixel])
- {
- this.stringToUnits.Add(pixelsString, MeasurementUnit.Pixel);
- ComboBox.Items.Add(pixelsString);
- }
- if ((bool)this.measurementItems[MeasurementUnit.Inch])
- {
- this.stringToUnits.Add(inchesString, MeasurementUnit.Inch);
- ComboBox.Items.Add(inchesString);
- }
- if ((bool)this.measurementItems[MeasurementUnit.Mil])
- {
- this.stringToUnits.Add(milsString, MeasurementUnit.Mil);
- ComboBox.Items.Add(milsString);
- }
- if ((bool)this.measurementItems[MeasurementUnit.Centimeter])
- {
- this.stringToUnits.Add(centimetersString, MeasurementUnit.Centimeter);
- ComboBox.Items.Add(centimetersString);
- }
- if ((bool)this.measurementItems[MeasurementUnit.Millimeter])
- {
- this.stringToUnits.Add(millimeterString, MeasurementUnit.Millimeter);
- ComboBox.Items.Add(millimeterString);
- }
- if ((bool)this.measurementItems[MeasurementUnit.Micron])
- {
- this.stringToUnits.Add(micronString, MeasurementUnit.Micron);
- ComboBox.Items.Add(micronString);
- }
- if ((bool)this.measurementItems[MeasurementUnit.Nano])
- {
- this.stringToUnits.Add(nanoString, MeasurementUnit.Nano);
- ComboBox.Items.Add(nanoString);
- }
- if (!(bool)this.measurementItems[oldUnits])
- {
- if (ComboBox.Items.Count == 0)
- {
- ComboBox.SelectedItem = null;
- }
- else
- {
- ComboBox.SelectedIndex = 0;
- }
- }
- else
- {
- this.Units = oldUnits;
- }
- }
- public event EventHandler UnitsChanged;
- private void OnUnitsChanged()
- {
- if (UnitsChanged != null)
- {
- UnitsChanged(this, EventArgs.Empty);
- }
- }
- private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
- {
- this.OnUnitsChanged();
- }
- }
- }
|