OSDN Git Service

960a3793588a3d9fea0e56da42f1a1c56a1820fc
[buragesnap/BurageSnap.git] / BurageSnap / OptionDialog.cs
1 // Copyright (C) 2015 Kazuhiro Fujieda <fujieda@users.osdn.me>
2 //
3 // This program is part of BurageSnap.
4 //
5 // BurageSnap is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, see <http://www.gnu.org/licenses/>.
17
18 using System;
19 using System.ComponentModel;
20 using System.Linq;
21 using System.Windows.Forms;
22 using BurageSnap.Properties;
23
24 namespace BurageSnap
25 {
26     public partial class OptionDialog : Form
27     {
28         private readonly Config _config;
29         private readonly ErrorProvider _errorProvider = new ErrorProvider();
30
31         public OptionDialog(Config config)
32         {
33             InitializeComponent();
34             _config = config;
35         }
36
37         private void buttonOk_Click(object sender, EventArgs e)
38         {
39             var ringbuffer = int.Parse(textBoxRingBuffer.Text);
40             if (checkBoxAnimationGif.Checked && ringbuffer == 0)
41             {
42                 DialogResult = DialogResult.None;
43                 _errorProvider.SetError(textBoxRingBuffer,
44                     Resources.OptionDialog_buttonOk_Click_Ring_buffer_for_animation_GIF);
45                 return;
46             }
47             _errorProvider.SetError(textBoxRingBuffer, "");
48             _config.TopMost = checkBoxTopMost.Checked;
49             _config.Interval = int.Parse(textBoxInterval.Text);
50             _config.RingBuffer = ringbuffer;
51             var title = comboBoxWindowTitle.Text;
52             if (title != "")
53             {
54                 comboBoxWindowTitle.Items.Remove(title);
55                 comboBoxWindowTitle.Items.Insert(0, title);
56                 for (var i = comboBoxWindowTitle.Items.Count; i > 10; i--)
57                     comboBoxWindowTitle.Items.RemoveAt(10);
58             }
59             _config.TitleHistory = (from object item in comboBoxWindowTitle.Items select item.ToString()).ToArray();
60             _config.Folder = textBoxFolder.Text;
61             _config.Format = radioButtonJpg.Checked ? OutputFormat.Jpg : OutputFormat.Png;
62             _config.AnimationGif = checkBoxAnimationGif.Checked;
63         }
64
65         private void OptionDialog_Load(object sender, EventArgs e)
66         {
67             checkBoxTopMost.Checked = _config.TopMost;
68             textBoxInterval.Text = _config.Interval.ToString();
69             textBoxRingBuffer.Text = _config.RingBuffer.ToString();
70             comboBoxWindowTitle.Items.Clear();
71             // ReSharper disable once CoVariantArrayConversion
72             comboBoxWindowTitle.Items.AddRange(_config.TitleHistory);
73             comboBoxWindowTitle.Text = _config.TitleHistory[0];
74             textBoxFolder.Text = _config.Folder;
75             textBoxFolder.Select(textBoxFolder.TextLength, 0);
76             radioButtonJpg.Checked = _config.Format == OutputFormat.Jpg;
77             radioButtonPng.Checked = _config.Format == OutputFormat.Png;
78             checkBoxAnimationGif.Checked = _config.AnimationGif;
79         }
80
81         private void buttonBrowse_Click(object sender, EventArgs e)
82         {
83             folderBrowserDialog.SelectedPath = textBoxFolder.Text;
84             if (folderBrowserDialog.ShowDialog(this) != DialogResult.OK)
85                 return;
86             textBoxFolder.Text = folderBrowserDialog.SelectedPath;
87             textBoxFolder.Select(textBoxFolder.TextLength, 0);
88         }
89
90         private void textBoxInterval_Validating(object sender, CancelEventArgs e)
91         {
92             int interval;
93             if (int.TryParse(textBoxInterval.Text, out interval) && 0 < interval && interval < 1000 * 1000)
94             {
95                 _errorProvider.SetError(textBoxInterval, "");
96                 return;
97             }
98             e.Cancel = true;
99             _errorProvider.SetError(textBoxInterval, Resources.OptionDialog_textBoxInterval_Validating_Interval);
100         }
101
102         private void textBoxRingBuffer_Validating(object sender, CancelEventArgs e)
103         {
104             int frames;
105             if (int.TryParse(textBoxRingBuffer.Text, out frames) && 0 <= frames && frames <= 100)
106             {
107                 _errorProvider.SetError(textBoxRingBuffer, "");
108                 return;
109             }
110             e.Cancel = true;
111             _errorProvider.SetError(textBoxRingBuffer, Resources.OptionDialog_textBoxRingBuffer_Validating);
112         }
113     }
114 }