using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Windows.Threading; using System.Threading.Tasks; using System.Windows; using System.Windows.Input; using System.Globalization; using SPC.Kiosk.Base; using System.Windows.Media; using System.Collections.ObjectModel; using Cosmos.UserFrame; namespace SPC.Kiosk.Common { public static class CommonFunction { /// /// Reregister Name an Object /// /// /// /// public static void ReregisterName(this FrameworkElement _frameworkElement, string _registerName,object _registerObject) { try { if (_frameworkElement.FindName(_registerName) != null) { _frameworkElement.UnregisterName(_registerName); } _frameworkElement.RegisterName(_registerName, _registerObject); } catch { } } /// /// Find Parent object by name /// /// /// /// public static object FindParent(this object _child, string _findName) { object result = null; try { var _parent = _child; while (_parent != null) { var getType = _parent.GetType(); var nameProperty = getType.GetProperty("Name"); var getName = nameProperty.GetValue(_parent, null); if (getName.Equals(_findName) && getType.Name.Equals("Grid")) { result = _parent; break; } var parentProperty = getType.GetProperty("Parent"); _parent = parentProperty.GetValue(_parent, null); } } catch { result = null; } return result; } /// /// String Text Show Rander Size Caculate /// /// /// /// /// /// /// /// /// public static Size GetFormatedTextSize(this string _inString , SupportLanguageType _languageType , double _fontSize , FontFamily _fontFamily , FontStyle _fontStyle , FontWeight _fontWeight , FontStretch _fontStretch) { var result = new Size(0,0); try { if (string.IsNullOrEmpty(_inString)) { result = new Size(0, 0); } else { var formattedText = new FormattedText(_inString, CultureInfo.GetCultureInfo(_languageType.ToString()), FlowDirection.LeftToRight, new Typeface(_fontFamily, _fontStyle, _fontWeight, _fontStretch), _fontSize, Brushes.Black); result = new Size(formattedText.Width, formattedText.Height); } } catch { result = new Size(0, 0); } return result; } /// /// 문자열 길이를 목적 폭에 마춰서 자르기 (... 뒤에 추가) /// /// /// /// /// /// /// /// /// /// public static string TextTrimming(this string _inString , SupportLanguageType _languageType , double _fontSize , FontFamily _fontFamily , FontStyle _fontStyle , FontWeight _fontWeight , FontStretch _fontStretch , double _targetWidth) { var result = _inString; try { if (!string.IsNullOrEmpty(_inString) && !_targetWidth.Equals(double.NaN) && _targetWidth > 0) { var sourceWidth = _inString.GetFormatedTextSize(_languageType , _fontSize , _fontFamily , _fontStyle , _fontWeight , _fontStretch).Width; if (sourceWidth > _targetWidth) { int subCount = 1; var subString = _inString; var newStringWidth = 0d; do { subCount++; if (subString.Length > subCount) { subString = subString.Substring(0, _inString.Length - subCount) + "..."; newStringWidth = subString.GetFormatedTextSize(_languageType , _fontSize , _fontFamily , _fontStyle , _fontWeight , _fontStretch).Width; } else { break; } } while (newStringWidth > _targetWidth); result = subString; } } } catch { result = _inString; } return result; } /// /// Make Option Label /// /// /// public static List GetOptionLabel(List _option) { List result = null; try { if (_option is List) { var index = 0; result = new List(); foreach (var aGroup in _option) { foreach (var aOption in aGroup.Options) { if (aOption.IsSelected) { if (aOption.Name is List) { if (index.Equals(0)) { foreach (var aName in aOption.Name) { result.Add(aName.Clone()); } } else { foreach (var aName in result) { var addLanguageData = Languages.GetLanguageData(aOption.Name, aName.Type); aName.LanguageData += "/" + addLanguageData; if (aOption.Kind.Equals(OptionKind.Count)) { aName.LanguageData += string.Format("({0})", aOption.SelectValue.ToString("#,##0")); } } } index++; } } } } } } catch { result = null; } return result; } /// /// GetDate from string /// /// /// public static DateTime GetDate(string _dateString) { var result = DateTime.MinValue; try { if (_dateString.Length.Equals(6)) { result = DateTime.Parse(String.Format("{0}-{1}-{2}" , _dateString.Substring(0, 2), _dateString.Substring(2, 2), _dateString.Substring(4, 2))).Date; } else if (_dateString.Length.Equals(8)) { result = DateTime.Parse(String.Format("{0}-{1}-{2}" , _dateString.Substring(0, 4), _dateString.Substring(4, 2), _dateString.Substring(6, 2))).Date; } else { result = DateTime.Parse(_dateString).Date; } } catch { result = DateTime.MinValue; } if (result.Equals(DateTime.MinValue)) { result = DateTime.Now.Date; } return result; } } }