OSDN Git Service

no message
[passer/Passer2.git] / passer / AddCommandForm.cs
1 using System;
2 using System.Collections.Generic;
3 using System.ComponentModel;
4 using System.Data;
5 using System.Drawing;
6 using System.Runtime.InteropServices;
7 using System.Text;
8 using System.Windows.Forms;
9 using Lugens.Utils;
10
11 namespace Lugens.Passer
12 {
13     public partial class AddCommandForm : Form
14     {
15         public AddCommandForm()
16         {
17             InitializeComponent();
18             this.Init();
19         }
20
21         public AddCommandForm(string targetFile) : this()
22         {
23             SetSelectedFile(targetFile);
24         }
25
26         public void Init()
27         {
28             this.textBox_command.Text = "\90V\82µ\82¢\83R\83}\83\93\83h";
29             this.textBox_target.Text = "";
30             this.textBox_path.Text = "";
31             this.textBox_arguments.Text = "";
32             this.textBox_comment.Text = "";
33         }
34
35         private string ExtractCommandName(string filename)
36         {
37             int fileIndex = filename.LastIndexOf('\\');
38             if (fileIndex == -1)
39             {
40                 fileIndex = 0;
41             }
42             string command = filename.Substring(fileIndex + 1);
43             if (command.LastIndexOf('.') != -1)
44             {
45                 command = command.Substring(0, command.LastIndexOf('.'));
46             }
47             return command;
48         }
49
50         private string ExtraCommandPath(string filename)
51         {
52             int fileIndex = filename.LastIndexOf('\\');
53             if (fileIndex == -1)
54             {
55                 fileIndex = 0;
56             }
57             return filename.Substring(0, fileIndex);
58         }
59
60         public void SetSelectedFile(string filename)
61         {
62             IntPtr ret;
63             Win32.SHFILEINFO info = new Win32.SHFILEINFO();
64
65             ret = Win32.SHGetFileInfo(filename, 0, ref info, Marshal.SizeOf(info), Win32.SHGFI_ATTRIBUTES);
66             if (ret == IntPtr.Zero) return;
67
68
69             if ((info.dwAttributes & Win32.SFGAO_LINK) != 0)
70             {
71                 //\83V\83\87\81[\83g\83J\83b\83g\82Ì\8fê\8d\87
72                 ShellLink shellLink = new ShellLink(filename);
73                 string targetfile = shellLink.TargetPath;
74                 this.textBox_command.Text = ExtractCommandName(filename);
75                 this.textBox_target.Text = targetfile;
76                 this.textBox_path.Text = shellLink.WorkingDirectory;
77                 this.textBox_arguments.Text = shellLink.Arguments;
78                 this.textBox_comment.Text = shellLink.Description;
79                 if (this.textBox_path.Text.Length == 0)
80                 {
81                     this.textBox_path.Text = ExtraCommandPath(targetfile);
82                 }
83                 shellLink.Close();
84                 shellLink = null;
85             }
86             else
87             {
88                 //\83t\83@\83C\83\8b\81A\83t\83H\83\8b\83_\82Ì\8fê\8d\87
89                 this.textBox_command.Text = ExtractCommandName(filename);
90                 this.textBox_target.Text = filename;
91                 this.textBox_path.Text = ExtraCommandPath(filename);
92             }
93             this.textBox_command.Text = this.textBox_command.Text.Replace(' ', '_');
94
95             if(Program.SettingForm.CommandDicTmp.ContainsKey(this.textBox_command.Text))
96             {
97                 string text;
98                 for (int i = 2; i < 256; i++)
99                 {
100                     text = this.textBox_command.Text + i;
101                     if (!Program.SettingForm.CommandDicTmp.ContainsKey(text))
102                     {
103                         this.textBox_command.Text = text;
104                         break;
105                     }
106
107                 }
108             }
109
110         }
111
112         private void button_view_Click(object sender, EventArgs e)
113         {
114
115             if (DialogResult.OK == openFileDialog1.ShowDialog(this))
116             {
117                 SetSelectedFile(openFileDialog1.FileName);
118             }
119         }
120
121         private void textBox_command_TextChanged(object sender, EventArgs e)
122         {
123             if (Program.SettingForm == null)
124                 return;
125             if (this.textBox_command.Text.Length == 0)
126             {
127                 this.label_duplicate.Visible = false;
128                 this.button_ok.Enabled = false;
129                 return;
130             }
131             if (Program.DefaultCommand.Equals(this.textBox_command.Text))
132             {
133                 this.label_duplicate.Visible = false;
134                 this.button_ok.Enabled = true;
135                 return;
136             }
137             if (this.textBox_command.Text[0] == '\\' || this.textBox_command.Text[0] == '!' || this.textBox_command.Text[0] == '$' || this.textBox_command.Text[0] == ':' || this.textBox_command.Text[0] == '@')
138             {
139                 this.label_duplicate.Text = "\81¦\93ü\97Í\82³\82ê\82½\8bL\8d\86\82Í\8eg\97p\82Å\82«\82Ü\82¹\82ñ";
140                 this.label_duplicate.Visible = true;
141                 this.button_ok.Enabled = false;
142                 return;
143             }
144             if (this.textBox_command.Text.IndexOf(' ') != -1)
145             {
146                 this.label_duplicate.Text = "\81¦\83X\83y\81[\83X\82Í\8eg\97p\82Å\82«\82Ü\82¹\82ñ";
147                 this.label_duplicate.Visible = true;
148                 this.button_ok.Enabled = false;
149                 return;
150             }
151             if (Program.SettingForm.CommandDicTmp.ContainsKey(this.textBox_command.Text))
152             {
153                 this.label_duplicate.Text = "\81¦\8fd\95¡\82µ\82Ä\82¢\82Ü\82·";
154                 this.label_duplicate.Visible = true;
155                 this.button_ok.Enabled = false;
156             }
157             else
158             {
159                 this.label_duplicate.Visible = false;
160                 this.button_ok.Enabled = true;
161             }
162         }
163
164         private void AddCommandForm_Load(object sender, EventArgs e)
165         {
166             //this.textBox_command.Text = "\90V\82µ\82¢\83R\83}\83\93\83h";
167         }
168
169         private void button_ok_Click(object sender, EventArgs e)
170         {
171             if (Program.SettingForm.CommandDicTmp.ContainsKey(this.textBox_command.Text))
172             {
173                 MessageBox.Show(this, "\83R\83}\83\93\83h\96¼\82ª\8fd\95¡\82µ\82Ä\82¢\82Ü\82·", "\93o\98^\83G\83\89\81[", MessageBoxButtons.OK, MessageBoxIcon.Warning);
174                 return;
175             }
176
177             if (String.IsNullOrEmpty(this.textBox_target.Text))
178             {
179                 MessageBox.Show(this, "\83t\83@\83C\83\8b\82ª\8ew\92è\82³\82ê\82Ä\82¢\82Ü\82¹\82ñ", "\93o\98^\83G\83\89\81[", MessageBoxButtons.OK, MessageBoxIcon.Warning);
180                 return;
181             }
182
183             CommandInfo info = new CommandInfo();
184             info.Name = this.textBox_command.Text;
185             info.Target = this.textBox_target.Text;
186             info.Path = this.textBox_path.Text;
187             info.Arguments = this.textBox_arguments.Text;
188             info.Comment = this.textBox_comment.Text.Length == 0 ? null : this.textBox_comment.Text;
189             Program.AddCommand(Program.SettingForm.CommandDicTmp, Program.SettingForm.CommandListTmp, info);
190             Program.SettingForm.ReadCommandList(true);
191             Program.SettingForm.RequestCommandListViewSelect(info.Name);
192
193             this.Visible = false;
194         }
195
196         private void button_cancel_Click(object sender, EventArgs e)
197         {
198             this.Close();
199         }
200
201         private void AddCommandForm_DragEnter(object sender, DragEventArgs e)
202         {
203             if (e.Data.GetDataPresent(DataFormats.FileDrop))
204             {
205                 e.Effect = DragDropEffects.Copy;
206             }
207         }
208
209         private void AddCommandForm_DragDrop(object sender, DragEventArgs e)
210         {
211             string[] files = (String[])e.Data.GetData(DataFormats.FileDrop);
212             if (files == null || files.Length == 0)
213             {
214                 return;
215             }
216             string file = files[0];
217
218             this.SetSelectedFile(file);
219         }
220
221         private void AddCommandForm_VisibleChanged(object sender, EventArgs e)
222         {
223             if (this.Visible)
224                 Program.ForceForegroundWindow(this.Handle);
225         }
226     }
227 }