OSDN Git Service

.Net4対応
[filenamechenger/filenamechanger.git] / FileNameChenger / WindowMain.xaml.cs
1 //TODO:スレッドの廃棄処理を考慮
2 //TODO:フェイドアウト フェイドインで処理中のバーを表示する。
3 //TODO:処理ファイルをバックグラウンドに流せないか
4 //TODO:コンソールアプリ、WEBアプリを作成したい
5 using System;
6 using System.Collections.Generic;
7 using System.Linq;
8 using System.Text;
9 using System.Windows;
10 using System.Windows.Controls;
11 using System.Windows.Data;
12 using System.Windows.Documents;
13 using System.Windows.Input;
14 using System.Windows.Media;
15 using System.Windows.Media.Imaging;
16 using System.Windows.Navigation;
17 using System.Windows.Shapes;
18 using System.IO;
19
20 namespace FileNameChenger
21 {
22     /// <summary>
23     /// WindowMain.xaml の相互作用ロジック
24     /// </summary>
25     public partial class WindowMain : Window
26     {
27         const string RunText = "変換実行";
28         const string CancelText = "中断";
29
30         //FileNameChengerCore.EStateFlg eStateFlg = FileNameChengerCore.EStateFlg.Non;
31         bool isRun = false;
32         System.Windows.Forms.Timer stateTimer = null;
33         FileNameChengerCore.CFileCopy cFileCopy = null;
34         FileNameChengerCore.CConfig cConfig = null;
35
36         public WindowMain()
37         {
38             InitializeComponent();
39
40             progressBarWork.Opacity = 0;
41             //imageWork.Opacity = 0.25;
42             imageWork.Opacity = 0.0;
43
44             //progressBarWork.Visibility = Visibility.Hidden;
45             //imageWork.Visibility = Visibility.Hidden;
46
47             //btnStoryboard.Storyboard.Completed += new EventHandler(Storyboard_Completed);
48
49             stateTimer = new System.Windows.Forms.Timer();
50             stateTimer.Interval = 100;
51             stateTimer.Tick += new EventHandler(stateTimer_Tick);
52
53
54             cConfig = new FileNameChengerCore.CConfig(FileNameChenger.Properties.Resources.ConfigFileName);
55             cConfig.getConfig();
56
57             textBoxFrom.Text = cConfig.FileNameFrom;
58             textBoxTo.Text = cConfig.FileNameTo;
59         }
60
61         bool IsWidthUp = false;
62         private void labelSetup_MouseEnter(object sender, MouseEventArgs e)
63         {
64             if (IsWidthUp)
65             {
66                 Width -= 200;
67                 Grid.Width -= 200;
68             }
69             else
70             {
71                 Width += 200;
72                 Grid.Width += 200;
73             }
74             IsWidthUp = !IsWidthUp;
75         }
76
77
78
79         private void setImage()
80         {
81             string dir = @".\img";
82
83             if (!Directory.Exists(dir))
84             {
85                 Directory.CreateDirectory(dir);
86             }
87
88             string[] files = Directory.GetFiles(dir, "*.jpg", SearchOption.AllDirectories);
89
90
91             BitmapImage myBitmapImage = new BitmapImage();
92
93             if (files.Length == 0)
94             {
95                 myBitmapImage.BeginInit();
96                 myBitmapImage.UriSource = new Uri(@"FileNameChenger.ico", UriKind.RelativeOrAbsolute);
97                 myBitmapImage.EndInit();
98             }
99             else
100             {
101                 Random r = new Random();
102                 int index = r.Next(files.Length - 1);
103                 myBitmapImage.BeginInit();
104                 myBitmapImage.UriSource = new Uri(@"file://" + Directory.GetCurrentDirectory() + files[index], UriKind.RelativeOrAbsolute);
105                 myBitmapImage.EndInit();
106             }
107
108             imageWork.Source = myBitmapImage;
109
110         }
111
112
113         private void buttonRun_Click(object sender, RoutedEventArgs e)
114         {
115             /*
116             switch (eStateFlg)
117             {
118                 case FileNameChengerCore.EStateFlg.Non:
119                     buttonRun.Content = RunText;
120                     break;
121                 case FileNameChengerCore.EStateFlg.Run:
122                     buttonRun.Content = CancelText;
123                     break;
124                 case FileNameChengerCore.EStateFlg.End:
125                 case FileNameChengerCore.EStateFlg.Cancel:
126                     buttonRun.Content = RunText;
127                     break;
128                 default:
129                     break;
130                 
131             }
132              */
133
134             //WorkView view = new WorkView();
135             //view.ShowDialog();
136             
137             /*
138              */
139
140
141             
142             if (isRun)
143             {
144                 /*
145                 labelFrom.Visibility = Visibility.Visible;
146                 labelTo.Visibility = Visibility.Visible;
147                 textBoxFrom.Visibility = Visibility.Visible;
148                 textBoxTo.Visibility = Visibility.Visible;
149                  */
150
151                 //checkBoxViewControl_Run.IsChecked = !checkBoxViewControl_Run.IsChecked;
152
153                 if (cFileCopy != null)
154                 {
155                     cFileCopy.Cancel();
156                 }
157                 isRun = false;
158                 buttonRun.Content = RunText;
159                 Title = FileNameChenger.Properties.Resources.Title;
160             }
161             else
162             {
163                 /*
164                 progressBarWork.Visibility = Visibility.Visible;
165                 imageWork.Visibility = Visibility.Visible;
166                  */
167
168                 //checkBoxViewControl.IsChecked = !checkBoxViewControl.IsChecked;
169
170                 cFileCopy = new FileNameChengerCore.CFileCopy(
171                     textBoxFrom.Text, textBoxTo.Text, cConfig.SerchPattern, cConfig.FileNamePattern);
172                 if (cFileCopy.Count > 0)
173                 {
174                     state = 0;
175                     buttonRun.Content = CancelText;
176                     if (cFileCopy.FreeSpace)
177                     {
178                         labelNonFreeSpace.Visibility = Visibility.Hidden;
179                     }
180                     else
181                     {
182                         labelNonFreeSpace.Visibility = Visibility.Visible;
183                     }
184                     files.Clear();
185                     workingImage.Angle = 0;
186                     setImage();
187                     progressBarWork.Minimum = 0;
188                     progressBarWork.Maximum = cFileCopy.Count;
189                     progressBarWork.Value = 0;
190
191                     cFileCopy.DoWorkEndEvent += new EventHandler<EventArgs>(cFileCopy_DoWorkEndEvent);
192                     cFileCopy.DoWorkingEvent += new EventHandler<FileNameChengerCore.DoWorkingEventArgs>(cFileCopy_DoWorkingEvent);
193
194                     isRun = true;
195                     cFileCopy.Run();
196
197
198                     stateTimer.Start();
199
200                 }
201                 else
202                 {
203                     cFileCopy = null;
204                 }
205             }
206             //btnStoryboard.Storyboard.Begin(this);
207         }
208
209         List<string> files = new List<string>();
210         void cFileCopy_DoWorkingEvent(object sender, FileNameChengerCore.DoWorkingEventArgs e)
211         {
212             //throw new NotImplementedException();
213             if (files != null)
214             {
215                 files.Add(e.FileNameFrom + "→" + e.FileNameTo);
216             }
217
218             state++;
219         }
220
221         void cFileCopy_DoWorkEndEvent(object sender, EventArgs e)
222         {
223             //throw new NotImplementedException();
224            //buttonRun.Content = CancelText;
225            isRun = false;
226         }
227
228
229         int state = 0;
230
231         void stateTimer_Tick(object sender, EventArgs e)
232         {
233             //throw new NotImplementedException();
234             Title = FileNameChenger.Properties.Resources.Title
235                 + "(" + state.ToString() + "/" + progressBarWork.Maximum.ToString() + ")";
236             /*
237             if (files.Count > 0)
238             {
239                 Title = files[0];
240                 files.RemoveAt(0);
241             }
242             else
243             {
244                 Title = FileNameChenger.Properties.Resources.Title;
245             }
246              */
247
248             if (isRun)
249             {
250
251                 lock (workingImage)
252                 {
253                     if (workingImage.Angle >= 360)
254                     {
255                         workingImage.Angle = 0;
256                         setImage();
257                     }
258                     else
259                     {
260                         workingImage.Angle++;
261                     }
262                 }
263
264                 //フェードイン
265                 lock (progressBarWork)
266                 {
267                     if (progressBarWork.Opacity <= 1)
268                     {
269                         progressBarWork.Opacity += 0.1;
270                     }
271                     progressBarWork.Value = state;
272                 }
273                 if (imageWork.Opacity <= 1)
274                 {
275                     imageWork.Opacity += 0.1;
276                 }
277
278
279                 //フェードアウト
280                 if (labelFrom.Opacity >= 0)
281                 {
282                     labelFrom.Opacity -= 0.1;
283                 }
284                 if (labelTo.Opacity >= 0)
285                 {
286                     labelTo.Opacity -= 0.1;
287                 }
288                 if (textBoxFrom.Opacity >= 0)
289                 {
290                     textBoxFrom.Opacity -= 0.1;
291                 }
292                 if (textBoxTo.Opacity >= 0)
293                 {
294                     textBoxTo.Opacity -= 0.1;
295                 }
296             }
297             else
298             {
299                 buttonRun.Content = RunText;
300
301
302                 //フェードアウト
303                 if (progressBarWork.Opacity >= 0)
304                 {
305                     progressBarWork.Opacity -= 0.1;
306                 }
307                 if (imageWork.Opacity >= 0)
308                 {
309                     imageWork.Opacity -= 0.1;
310                 }
311
312                 //フェードイン
313                 if (labelFrom.Opacity <= 1)
314                 {
315                     labelFrom.Opacity += 0.1;
316                 }
317                 if (labelTo.Opacity <= 1)
318                 {
319                     labelTo.Opacity += 0.1;
320                 }
321                 if (textBoxFrom.Opacity <= 1)
322                 {
323                 textBoxFrom.Opacity += 0.1;
324                 }
325                 if (textBoxTo.Opacity <= 1)
326                 {
327                     textBoxTo.Opacity += 0.1;
328                 }
329
330                 
331                 if (labelTo.Opacity == 1)
332                 {
333                     files.Clear();
334                     stateTimer.Stop();
335                 }
336             }
337
338         }
339
340         void Storyboard_Completed(object sender, EventArgs e)
341         {
342
343             //throw new NotImplementedException();
344             /*
345             daLabelFrom.From = labelFrom.Opacity;
346             daLabelTo.From = labelTo.Opacity;
347             daTextBoxFrom.From = textBoxFrom.Opacity;
348             daTextBoxTo.From = textBoxTo.Opacity;
349             daButtonFrom.From = buttonFrom.Opacity;
350             daButtonTo.From = buttonTo.Opacity;
351
352             daProgressBarWork.From = progressBarWork.Opacity;
353             daImageWork.From = imageWork.Opacity;
354             */
355             if (isRun)
356             {
357                 labelFrom.Visibility = Visibility.Hidden;
358                 labelTo.Visibility = Visibility.Hidden;
359                 textBoxFrom.Visibility = Visibility.Hidden;
360                 textBoxTo.Visibility = Visibility.Hidden;
361
362                 /*
363                 daLabelFrom.To = 1;
364                 daLabelTo.To = 1;
365                 daTextBoxFrom.To = 1;
366                 daTextBoxTo.To = 1;
367                 daButtonFrom.To = 1;
368                 daButtonTo.To = 1;
369
370                 daProgressBarWork.To = 0;
371                 daImageWork.To = 0;
372                  */
373             }
374             else
375             {
376                 progressBarWork.Visibility = Visibility.Hidden;
377                 imageWork.Visibility = Visibility.Hidden;
378                 /*
379                 daLabelFrom.To = 0;
380                 daLabelTo.To = 0;
381                 daTextBoxFrom.To = 0;
382                 daTextBoxTo.To = 0;
383                 daButtonFrom.To = 0;
384                 daButtonTo.To = 0;
385
386                 daProgressBarWork.To = 1;
387                 daImageWork.To = 1;
388                  */
389             }
390         }
391
392         private void Window_Unloaded(object sender, RoutedEventArgs e)
393         {
394         }
395
396         private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
397         {
398             if (cFileCopy != null)
399             {
400                 cFileCopy.Cancel();
401             }
402
403             cConfig.FileNameFrom = textBoxFrom.Text;
404             cConfig.FileNameTo = textBoxTo.Text;
405             cConfig.putConfig();
406         }
407
408         private void Grid_DragEnter(object sender, DragEventArgs e)
409         {
410             if (e.Data.GetDataPresent(System.Windows.DataFormats.FileDrop)
411                 || e.Data.GetDataPresent(System.Windows.DataFormats.Text)
412                 || e.Data.GetDataPresent(System.Windows.DataFormats.UnicodeText))
413             {
414                 e.Effects = System.Windows.DragDropEffects.Copy;
415             }
416
417         }
418
419         private void Grid_Drop(object sender, DragEventArgs e)
420         {
421             // ファイルドロップした場合のみ処理
422             if (e.Data.GetDataPresent(DataFormats.FileDrop))
423             {
424                 foreach (string fileName in (string[])e.Data.GetData(DataFormats.FileDrop))
425                 {
426                     Point p = e.GetPosition(this);
427                     if (p.Y < (Height/2))
428                     {
429                         textBoxFrom.Text = fileName;
430                     }
431                     else
432                     {
433                     //TextBox textBox = (TextBox)sender;
434                     //textBox.Text = fileName;
435                         textBoxTo.Text = fileName;
436                     }
437
438                     break;
439                 }
440             }
441
442         }
443     }
444 }