using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using System.Text.RegularExpressions;

namespace CSRender
{
    public partial class MainWindow : Window
    {

        ////////////////////////////////////////////////////////////////////////////
        // 解像度のUpDownメニュー機能の記述
        ////////////////////////////////////////////////////////////////////////////

        //TextBoxフォーカス時にテキスト全選択
        private void NumericTextBox_GotFocusSelectAll(object sender, RoutedEventArgs e)
        {
            TextBox ResolutionBox = (TextBox)sender;
            //box.SelectAll();
            this.Dispatcher.InvokeAsync(() => { Task.Delay(10); ResolutionBox.SelectAll(); });
        }

        //正規表現で数値以外は削除using System.Text.RegularExpressions;
        private void NumericTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox ResolutionBox = (TextBox)sender;
            double d;
            if (!double.TryParse(ResolutionBox.Text, out d))
            {
                ResolutionBox.Text = Regex.Replace(ResolutionBox.Text, "[^0-9-]", "");
            }
        }

        //数字の下限;
        private void NumericTextBox_NumChanged(object sender, TextChangedEventArgs e)
        {
            TextBox ResolutionBox = (TextBox)sender;
            if (ResolutionBox.Text == "")
            {
                ResolutionBox.Text = "72";
            }
            NumericScroll1.Value = Int32.Parse(ResolutionBox.Text);
        }

        //TextBox上でマウスホイールを回転させた時にスクロールバーの値を上下させる
        private void NumericTextBox_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            TextBox ResolutionBox = (TextBox)sender;
            Binding binding = BindingOperations.GetBinding(ResolutionBox, TextBox.TextProperty);
            ScrollBar NumericScroll1 = (ScrollBar)this.FindName(binding.ElementName);//名前から取得
            NumericScroll1.Value = Int32.Parse(ResolutionBox.Text);
            if (e.Delta > 0)
            {
                NumericScroll1.Value++;
            }
            else
            {
                NumericScroll1.Value--;
            }
        }
        //ScrollBar上でマウスホイールを回転させた時にScrollBarの値を上下させる
        private void NumericScroll1_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            ScrollBar NumericScroll1 = (ScrollBar)sender;
            NumericScroll1.Value = Int32.Parse(ResolutionBox.Text);
            if (e.Delta > 0)
            {
                NumericScroll1.Value++;
            }
            else
            {
                NumericScroll1.Value--;
            }
        }

        ////////////////////////////////////////////////////////////////////////////
        // 位置ズレ許容量のUpDownメニュー機能の記述
        ////////////////////////////////////////////////////////////////////////////
        //数字の下限;
        private void PosiTextBox_NumChanged(object sender, TextChangedEventArgs e)
        {
            TextBox PositionBox = (TextBox)sender;
            if (PositionBox.Text == "")
            {
                PositionBox.Text = "2";
            }
            PosiScroll1.Value = Int32.Parse(PositionBox.Text);
        }
        //ScrollBar上でマウスホイールを回転させた時にScrollBarの値を上下させる
        private void PosiScroll1_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            ScrollBar PosiScroll1 = (ScrollBar)sender;
            PosiScroll1.Value = Int32.Parse(PositionBox.Text);
            if (e.Delta > 0)
            {
                PosiScroll1.Value++;
            }
            else
            {
                PosiScroll1.Value--;
            }
        }

        ////////////////////////////////////////////////////////////////////////////
        // 諧調差許容量のUpDownメニュー機能の記述
        ////////////////////////////////////////////////////////////////////////////
        //数字の下限;
        private void GradTextBox_NumChanged(object sender, TextChangedEventArgs e)
        {
            TextBox GradationBox = (TextBox)sender;
            if (GradationBox.Text == "")
            {
                GradationBox.Text = "60";
            }
            GradScroll.Value = Int32.Parse(GradationBox.Text);
        }
        //ScrollBar上でマウスホイールを回転させた時にScrollBarの値を上下させる
        private void GradScroll_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            ScrollBar GradScroll = (ScrollBar)sender;
            GradScroll.Value = Int32.Parse(GradationBox.Text);
            if (e.Delta > 0)
            {
                GradScroll.Value++;
            }
            else
            {
                GradScroll.Value--;
            }
        }

        ////////////////////////////////////////////////////////////////////////////
        // 微小差異許容量のUpDownメニュー機能の記述
        ////////////////////////////////////////////////////////////////////////////
        //数字の下限;
        private void SmallDiffTextBox_NumChanged(object sender, TextChangedEventArgs e)
        {
            TextBox SmallDiffBox = (TextBox)sender;
            if (SmallDiffBox.Text == "")
            {
                SmallDiffBox.Text = "60";
            }
            SmallDiffScroll.Value = Int32.Parse(SmallDiffBox.Text);
        }
        //ScrollBar上でマウスホイールを回転させた時にScrollBarの値を上下させる
        private void SmallDiffScroll_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            ScrollBar SmallDiffScroll = (ScrollBar)sender;
            SmallDiffScroll.Value = Int32.Parse(SmallDiffBox.Text);
            if (e.Delta > 0)
            {
                SmallDiffScroll.Value++;
            }
            else
            {
                SmallDiffScroll.Value--;
            }
        }

        ////////////////////////////////////////////////////////////////////////////
        // 視覚的あいまい量のUpDownメニュー機能の記述
        ////////////////////////////////////////////////////////////////////////////
        //数字の下限;
        private void VisualTextBox_NumChanged(object sender, TextChangedEventArgs e)
        {
            TextBox VisualBox = (TextBox)sender;
            if (VisualBox.Text == "")
            {
                VisualBox.Text = "60";
            }
            VisualScroll.Value = Int32.Parse(VisualBox.Text);
        }
        //ScrollBar上でマウスホイールを回転させた時にScrollBarの値を上下させる
        private void VisualScroll_MouseWheel(object sender, MouseWheelEventArgs e)
        {
            ScrollBar VisualScroll = (ScrollBar)sender;
            VisualScroll.Value = Int32.Parse(VisualBox.Text);
            if (e.Delta > 0)
            {
                VisualScroll.Value++;
            }
            else
            {
                VisualScroll.Value--;
            }
        }
    }
}
