/// <summary> /// Constructor, initializes the RibbonControlLength and specifies what kind of value /// it will hold. /// </summary> public RibbonControlLength(double value, RibbonControlLengthUnitType type) { if (double.IsNaN(value)) { throw new ArgumentException(Microsoft.Windows.Controls.SR.Get(Microsoft.Windows.Controls.SRID.InvalidCtorParameterNoNaN, "value")); } if (type == RibbonControlLengthUnitType.Star && double.IsInfinity(value)) { throw new ArgumentException(Microsoft.Windows.Controls.SR.Get(Microsoft.Windows.Controls.SRID.InvalidCtorParameterNoInfinityForStarSize, "value")); } if (type != RibbonControlLengthUnitType.Auto && type != RibbonControlLengthUnitType.Pixel && type != RibbonControlLengthUnitType.Item && type != RibbonControlLengthUnitType.Star) { throw new ArgumentException(Microsoft.Windows.Controls.SR.Get(Microsoft.Windows.Controls.SRID.InvalidCtorParameterUnknownRibbonControlLengthUnitType, "type")); } _unitValue = (type == RibbonControlLengthUnitType.Auto) ? 0.0 : value; _unitType = type; }
/// <summary> /// Parses a RibbonControlLength from a string given the CultureInfo. /// </summary> internal static RibbonControlLength FromString(string s, CultureInfo cultureInfo) { string goodString = s.Trim().ToLowerInvariant(); double value = 0.0; RibbonControlLengthUnitType unit = RibbonControlLengthUnitType.Pixel; int i; int strLen = goodString.Length; int strLenUnit = 0; double unitFactor = 1.0; // this is where we would handle trailing whitespace on the input string. // peel [unit] off the end of the string i = 0; if (goodString == _unitStrings[i]) { strLenUnit = _unitStrings[i].Length; unit = (RibbonControlLengthUnitType)i; } else { for (i = 1; i < _unitStrings.Length; ++i) { // Note: this is NOT a culture specific comparison. // this is by design: we want the same unit string table to work across all cultures. if (goodString.EndsWith(_unitStrings[i], StringComparison.Ordinal)) { strLenUnit = _unitStrings[i].Length; unit = (RibbonControlLengthUnitType)i; break; } } } // we couldn't match a real unit from RibbonControlLengthUnitTypes. // try again with a converter-only unit (a pixel equivalent). if (i >= _unitStrings.Length) { for (i = 0; i < _pixelUnitStrings.Length; ++i) { // Note: this is NOT a culture specific comparison. // this is by design: we want the same unit string table to work across all cultures. if (goodString.EndsWith(_pixelUnitStrings[i], StringComparison.Ordinal)) { strLenUnit = _pixelUnitStrings[i].Length; unitFactor = _pixelUnitFactors[i]; break; } } } // this is where we would handle leading whitespace on the input string. // this is also where we would handle whitespace between [value] and [unit]. // check if we don't have a [value]. This is acceptable for certain UnitTypes. if (strLen == strLenUnit && (unit == RibbonControlLengthUnitType.Auto || unit == RibbonControlLengthUnitType.Star)) { value = 1; } // we have a value to parse. else { Debug.Assert(unit == RibbonControlLengthUnitType.Pixel || unit == RibbonControlLengthUnitType.Item || DoubleUtil.AreClose(unitFactor, 1.0)); ReadOnlySpan <char> valueString = goodString.AsSpan(0, strLen - strLenUnit); value = double.Parse(valueString, provider: cultureInfo) * unitFactor; } return(new RibbonControlLength(value, unit)); }