using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
using SPC.Kiosk.Base;
namespace SPC.Kiosk.Common
{
///
/// 다국어 지원 TextBlock
///
public class MutiLanguageTextBlock : TextBlock
{
#region [ Members ]
///
/// Muti Language Data
///
public static readonly DependencyProperty MultiLanguageProperty =
DependencyProperty.Register(nameof(MultiLanguage), typeof(List), typeof(MutiLanguageTextBlock), new PropertyMetadata(default(List), new PropertyChangedCallback(OnMultiLanguagePropertyChanged)));
///
/// Display Language
///
public static readonly DependencyProperty DisplayLanguagePropery =
DependencyProperty.Register(nameof(DisplayLanguage), typeof(SupportLanguageType), typeof(MutiLanguageTextBlock), new PropertyMetadata(SupportLanguageType.ko, new PropertyChangedCallback(OnDisplayLanguageProperyChanged)));
///
/// Guid Text (if Show Text is empty)
///
public static readonly DependencyProperty GuidTextProperty =
DependencyProperty.Register(nameof(GuidText), typeof(List), typeof(MutiLanguageTextBlock), new PropertyMetadata(default(List), new PropertyChangedCallback(OnGuidTextPropertyChanged)));
///
/// Text Blinking Flag
///
public static readonly DependencyProperty BlinkTextProperty =
DependencyProperty.Register(nameof(BlinkText), typeof(bool), typeof(MutiLanguageTextBlock), new PropertyMetadata(false, new PropertyChangedCallback(OnBlinkTextPropertyChanged)));
///
/// Auto Text Trim
///
public static readonly DependencyProperty AutoTextTrimProperty =
DependencyProperty.Register(nameof(AutoTextTrim), typeof(bool), typeof(MutiLanguageTextBlock), new PropertyMetadata(false, new PropertyChangedCallback(OnAutoTextTrimPropertyChanged)));
~MutiLanguageTextBlock()
{
if (this.BlickTimer != null)
{
this.BlickTimer.Stop();
this.BlickTimer.Tick -= this.BlickTimer_Tick;
this.BlickTimer = null;
}
}
///
/// Blinking Timer
///
public DispatcherTimer BlickTimer { get; protected set; }
public bool AutoTextTrim
{
get { return (bool)GetValue(AutoTextTrimProperty); }
set { SetValue(AutoTextTrimProperty, value); }
}
///
/// Text Blinking Flag
///
public bool BlinkText
{
get { return (bool)GetValue(BlinkTextProperty); }
set { SetValue(BlinkTextProperty, value); }
}
///
/// Guid Text (if Show Text is empty)
///
public List GuidText
{
get { return (List)GetValue(GuidTextProperty); }
set { SetValue(GuidTextProperty, value); }
}
///
/// Muti Language Data
///
public List MultiLanguage
{
get { return (List)GetValue(MultiLanguageProperty); }
set { SetValue(MultiLanguageProperty, value); }
}
///
/// Display Language
///
public SupportLanguageType DisplayLanguage
{
get { return (SupportLanguageType)GetValue(DisplayLanguagePropery); }
set { SetValue(DisplayLanguagePropery, value); }
}
#endregion Members
#region [ Methods ]
protected override void OnTextInput(TextCompositionEventArgs e)
{
base.OnTextInput(e);
if (string.IsNullOrEmpty(e.Text))
{
var target = e.OriginalSource as MutiLanguageTextBlock;
target.Text = target.GuidText.GetLanguageData(target.DisplayLanguage);
}
}
private void AutoTextTrimming()
{
if (this.AutoTextTrim)
{
this.Text = this.Text.TextTrimming(this.DisplayLanguage
, this.FontSize
, this.FontFamily
, this.FontStyle
, this.FontWeight
, this.FontStretch
, this.Width);
}
}
#endregion Methods
#region [ Event Handlers ]
private static void OnMultiLanguagePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (e.NewValue != null && !e.NewValue.Equals(e.OldValue))
{
var target = d as MutiLanguageTextBlock;
target.Text = string.Empty;
if (e.NewValue is List multiLnaguage)
{
target.Text = multiLnaguage.GetLanguageData(target.DisplayLanguage);
target.AutoTextTrimming();
}
}
}
private static void OnBlinkTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (!e.NewValue.Equals(e.OldValue))
{
var target = d as MutiLanguageTextBlock;
if ((bool)e.OldValue)
{
if (target.BlickTimer != null)
{
target.BlickTimer.Stop();
target.BlickTimer = null;
}
}
if ((bool)e.NewValue)
{
target.BlickTimer = new DispatcherTimer
{
Interval = TimeSpan.FromSeconds(0.5d),
};
target.BlickTimer.Tick += target.BlickTimer_Tick;
target.BlickTimer.Start();
}
}
}
private void BlickTimer_Tick(object sender, EventArgs e)
{
BlickTimer.Stop();
if (this.Visibility.Equals(Visibility.Visible))
{
this.Visibility = Visibility.Hidden;
}
else
{
this.Visibility = Visibility.Visible;
}
BlickTimer.Start();
}
private static void OnGuidTextPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var target = d as MutiLanguageTextBlock;
if (e.NewValue is List guideText)
{
target.Text = guideText.GetLanguageData(target.DisplayLanguage);
target.AutoTextTrimming();
}
}
private static void OnDisplayLanguageProperyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (e.NewValue != null && !e.NewValue.Equals(e.OldValue))
{
var target = d as MutiLanguageTextBlock;
target.Text = string.Empty;
if (target.MultiLanguage is List multiLnaguage && e.NewValue is SupportLanguageType languageType)
{
target.Text = multiLnaguage.GetLanguageData(languageType);
target.AutoTextTrimming();
}
}
}
private static void OnAutoTextTrimPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if ((bool)e.NewValue)
{
var target = d as MutiLanguageTextBlock;
target.AutoTextTrimming();
}
}
#endregion Event Handlers
}
}