OSDN Git Service

no message
[passer/Passer2.git] / passer / MainFormEvent.cs
1 /**
2  * $Revision: 238 $
3  * $Date: 2010-03-18 20:45:05 +0900 (木, 18 3 2010) $
4 */
5
6 using System;
7 using System.Collections.Generic;
8 using System.Diagnostics;
9 using System.Drawing;
10 using System.IO;
11 using System.Text;
12 using System.Windows.Forms;
13 using System.Net;
14 using System.Web;
15 using Lugens.Components;
16 using Lugens.Utils;
17 using System.Runtime.InteropServices;
18 using Newtonsoft.Json;
19 using Microsoft.WindowsAPICodePack.Shell;
20 using System.Drawing.Imaging;
21
22 namespace Lugens.Passer
23 {
24     public partial class MainForm
25     {
26
27         private void DoResize(int width)
28         {
29             Size size = this.Size;
30             size.Width = width;
31             if (size.Width < 200)
32                 size.Width = 200;
33             else if (size.Width > 600)
34                 size.Width = 600;
35
36             Settings.Set(Settings.PASSER_FORM_MAIN_WIDTH, size.Width);
37
38             this.Size = size;
39             size.Width -= 16;
40             size.Height = this.searchTextBox.Size.Height;
41             this.searchTextBox.Size = size;
42             if (this.searchTextBox.TextBox.DropdownForm.Visible)
43             {
44                 Program.Status = ProgramStatus.MainFormOpen;
45                 this.searchTextBox.TextBox.DropdownForm.DoVisibleChange(false);
46             }
47
48         }
49
50         private void MainForm_MouseDown(object sender, MouseEventArgs e)
51         {
52             if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
53             {
54                 this.mousePoint = new Point(-e.X, -e.Y);
55                 this.windowMoveFlag = true;
56             }
57         }
58
59         private void MainForm_MouseMove(object sender, MouseEventArgs e)
60         {
61             if (this.Cursor == Cursors.SizeWE && (e.Button & MouseButtons.Left) == MouseButtons.Left)
62             {
63                 //ウィンドウの幅変更
64                 this.DoResize(e.X);
65             }
66             else if (this.windowMoveFlag && (e.Button & MouseButtons.Left) == MouseButtons.Left)
67             {
68                 //ウィンドウの移動
69                 this.seenPoint = new Point(this.Left + mousePoint.X + e.X, this.Top + mousePoint.Y + e.Y);
70                 this.Location = this.seenPoint;
71
72                 Settings.Set(Settings.PASSER_FORM_MAIN_X, this.seenPoint.X);
73                 Settings.Set(Settings.PASSER_FORM_MAIN_Y, this.seenPoint.Y);
74
75                 if (this.searchTextBox.TextBox.DropdownForm.Visible)
76                 {
77                     Program.Status = ProgramStatus.MainFormOpen; 
78                     this.searchTextBox.TextBox.DropdownForm.DoVisibleChange(false);
79                 }
80             }
81             else if (e.X >= (this.Size.Width - 4))
82             {
83                 //幅変更カーソルに変更
84                 this.Cursor = Cursors.SizeWE;
85             }
86             else
87             {
88                 //標準カーソルに変更
89                 if (this.Cursor == Cursors.SizeWE)
90                     this.Cursor = Cursors.Default;
91             }
92         }
93
94         private void MainForm_MouseUp(object sender, MouseEventArgs e)
95         {
96             this.windowMoveFlag = false;
97         }
98
99         private void MainForm_Load(object sender, System.EventArgs e)
100         {
101             this.Visible = false;
102             this.Size = new Size(256, 32);
103             this.Location = this.seenPoint;
104             ReadCommandList();
105             this.searchTextBox.TextBox.DropdownForm.DrawType = Lugens.Components.DropdownListForm.DrawMode.IconText;
106             this.searchTextBox.TextBox.DropdownForm.IconCallBack = this.GetIconCallBack;
107             this.searchTextBox.TextBox.DropdownForm.VisibleChanged += this.dropdownListBox_SelectedIndexChanged;
108             this.searchTextBox.TextBox.DropdownForm.DropdownListBox.SelectedIndexChanged += this.dropdownListBox_SelectedIndexChanged;
109             this.searchTextBox.TextBox.DropdownForm.DropdownListBox.VisibleChanged += this.dropdownListBox_SelectedIndexChanged;
110             this.Activated += this.dropdownListBox_SelectedIndexChanged;
111             this.AddOwnedForm(this.searchTextBox.TextBox.DropdownForm);
112
113             if (Program.Main_MemorizeEngine)
114             {
115                 EngineInfo info = Program.GetEngineInfo(Settings.GetInt(Settings.PASSER_MAIN_ENGINE_INDEX));
116                 if(info == null)
117                     this.SetSearchEngine(Program.GetEngineInfo(0));
118                 else
119                     this.SetSearchEngine(info);
120             }
121         }
122
123         private void ShowDropdownForm(bool filter)
124         {
125             switch (this.SelectedEngineType)
126             {
127                 case 0: //Passer
128                     this.SetDropdownList(filter);
129                     if (this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Count > 0)
130                     {
131                         this.searchTextBox.TextBox.SetDropdownFormLocation();
132                         this.searchTextBox.TextBox.DropdownForm.DropdownListBox.SelectedIndex = 0;
133                         this.searchTextBox.TextBox.DropdownForm.DoVisibleChange(true);
134                     }
135                     break;
136                 case 1: //FileSearch
137                     this.SetLocalDropdownList(filter);
138                     if (this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Count > 0)
139                     {
140                         this.searchTextBox.TextBox.SetDropdownFormLocation();
141                         this.searchTextBox.TextBox.DropdownForm.DropdownListBox.SelectedIndex = 0;
142                         this.searchTextBox.TextBox.DropdownForm.DoVisibleChange(true);
143                     }
144                     break;
145
146                 case 2: //WebSearch
147                     if (String.IsNullOrEmpty(this.searchTextBox.TextBox.Text))
148                     {
149                         Program.Status = ProgramStatus.MainFormOpen; 
150                         this.searchTextBox.TextBox.DropdownForm.DoVisibleChange(false);
151                         this.suggestString = "";
152                     }
153                     else
154                     {
155                         this.textChanged = true;
156                     }
157                     break;
158             }
159         }
160
161         private void SetRootDriveList()
162         {
163             Program.RootDriveList.Clear();
164             Program.AliasDic.Clear();
165             foreach (DriveInfo d in DriveInfo.GetDrives())
166             {
167                 IconListBoxItem item = new IconListBoxItem();
168                 item.Name = d.Name;
169                 item.Text = item.Name;
170                 item.ToolTipText = item.Name;
171                 item.Type = IconListBoxType.Directory;
172                 switch (d.DriveType)
173                 {
174                     case DriveType.Fixed:
175                         if (Environment.SystemDirectory.StartsWith(d.Name))
176                             item.IconImage = Properties.Resources.IconDriveWin.ToBitmap();
177                         else
178                             item.IconImage = Properties.Resources.IconDriveFix.ToBitmap();
179                         break;
180                     case DriveType.Network:
181                         item.IconImage = Properties.Resources.IconDriveNet.ToBitmap();
182                         break;
183                     case DriveType.CDRom:
184                         item.IconImage = Properties.Resources.IconDriveCD.ToBitmap();
185                         break;
186                     case DriveType.Removable:
187                         item.IconImage = Properties.Resources.IconDriveRem.ToBitmap();
188                         break;
189                     default:
190                         item.IconImage = Properties.Resources.IconDriveFix.ToBitmap();
191                         break;
192                 }
193                 Program.RootDriveList.Add(item);
194             }
195
196             if (Program.OSVersion >= 6.1)
197             {
198                 //Win7 or later
199                 Bitmap bitmap;
200                 ShellObject lib = (ShellObject)KnownFolders.Libraries;
201                 IconListBoxItem item = new IconListBoxItem();
202                 item.Name = lib.ToString() + "\\";
203                 item.Text = item.Name;
204                 item.ToolTipText = item.Name;
205                 item.Regex = Program.Ime.GetRomaji(item.Name);
206                 item.Type = IconListBoxType.Directory;
207                 lib.Thumbnail.FormatOption = ShellThumbnailFormatOption.IconOnly;
208                 item.IconImage = Program.GetBitmap(lib);
209                 Program.RootDriveList.Add(item);
210                 Program.AliasDic.Add(lib.ToString(), KnownFolders.Libraries.Path);
211             }
212             
213         }
214
215         private void SetDropdownList(bool filter)
216         {
217             string[] commands = Program.ParseCommand(this.searchTextBox.TextBox.Text);
218             string command = "";
219             if(commands.Length > 0)
220                 command = commands[commands.Length - 1];
221
222             this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Clear();
223             if (filter && command.Equals(""))
224             {
225                 this.searchTextBox.TextBox.DropdownForm.DoVisibleChange(false);
226                 return;
227             }
228
229
230             foreach (IconListBoxItem item in this.searchTextBox.TextBox.Items)
231             {
232                 if (item.Text.ToLower().StartsWith(this.searchTextBox.TextBox.Text.ToLower()) || Program.Ime.StartsWith(item.Regex, this.searchTextBox.TextBox.Text))
233                     this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Add(item);
234             }
235             if (command.Length > 0)
236             {
237                 foreach (string str in Program.CommandHistoryList)
238                 {
239                     if (str.StartsWith(command))
240                     {
241                         bool exist = false;
242                         foreach (IconListBoxItem item in this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items)
243                         {
244                             if (str.Equals(item.Text))
245                             {
246                                 exist = true;
247                                 break;
248                             }
249                         }
250
251                         if (exist)
252                             continue;
253                         IconListBoxItem i = new IconListBoxItem();
254                         i.Name = str;
255                         i.Text = str;
256                         i.Type = IconListBoxType.History;
257                         i.IconImage = null;
258                         this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Add(i);
259                     }
260                 }
261             }
262             
263             if (this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Count == 0)
264             {
265                 Program.Status = ProgramStatus.MainFormOpen;
266                 this.searchTextBox.TextBox.DropdownForm.DoVisibleChange(false);
267                 return;
268             }
269         }
270
271
272         /// <summary>
273         /// ローカルのサジェスト
274         /// </summary>
275         /// <param name="text"></param>
276         public void SetLocalDropdownList(bool filter)
277         {
278 /*
279             string[] commands = Program.ParseFile(this.searchTextBox.TextBox.Text);
280             
281
282             foreach(string s in commands)
283             Debug.WriteLine("commands:" + s);
284             Debug.WriteLine("end");
285
286             string command = "";
287             if (commands.Length > 0)
288                 command = commands[commands.Length - 1];
289 */
290             string command = this.searchTextBox.TextBox.Text;
291             string file;
292             this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Clear();
293             if (filter && command.Equals(""))
294             {
295                 this.searchTextBox.TextBox.DropdownForm.DoVisibleChange(false);
296                 return;
297             }
298
299             if (command.LastIndexOf('\\') == -1)
300             {
301                 //ルートドライブ
302                 string str = command.ToLower();
303                 foreach (IconListBoxItem item in Program.RootDriveList)
304                 {
305                     if (item.Text.ToLower().StartsWith(str) || Program.Ime.StartsWith(item.Regex, str))
306                         this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Add(item);
307                 }
308             }
309             else
310             {
311                 if (Program.OSVersion >= 6.1 && command.StartsWith("ライブラリ"))
312                 {
313                     string[] directories = command.Split('\\');
314                     if (directories.Length == 2)
315                     {
316                         file = directories[1];
317                         ShellContainer sc = KnownFolders.Libraries as ShellContainer;
318                         foreach (ShellObject o in sc)
319                         {
320                             string regex = Program.Ime.GetRomaji(o.Name);
321                             if (o.Name.StartsWith(file) || Program.Ime.StartsWith(regex, file))
322                             {
323                                 IconListBoxItem item = new IconListBoxItem();
324                                 item.Name = o.Name + "\\";
325                                 item.Text = item.Name;
326                                 item.ToolTipText = item.Text;
327                                 item.Type = IconListBoxType.Directory;
328                                 item.Regex = regex;
329                                 o.Thumbnail.FormatOption = ShellThumbnailFormatOption.IconOnly;
330                                 item.IconImage = Program.GetBitmap(o);
331                                 this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Add(item);
332                             }
333                         }
334                     }
335                     else if (directories.Length == 3)
336                     {
337                         file = directories[2];
338                         ShellLibrary lib = Program.GetShellLibrary(directories[1]);
339                         if (lib == null)
340                             return;
341
342                         foreach (ShellObject o in lib)
343                         {
344                             if (o is ShellContainer)
345                             {
346                                 ShellContainer sc = o as ShellContainer;
347                                 foreach (ShellObject so in sc)
348                                 {
349                                     string regex = Program.Ime.GetRomaji(so.Name);
350                                     if (so.Name.StartsWith(file) || Program.Ime.StartsWith(regex, file))
351                                     {
352                                         IconListBoxItem item = new IconListBoxItem();
353                                         item.Name = so.Name + "\\";
354                                         item.Text = item.Name;
355                                         item.ToolTipText = item.Text;
356                                         item.Type = IconListBoxType.Directory;
357                                         item.Regex = regex;
358                                         so.Thumbnail.FormatOption = ShellThumbnailFormatOption.IconOnly;
359                                         item.IconImage = Program.GetBitmap(so);
360                                         this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Add(item);
361                                         Debug.WriteLine(so.ParsingName);
362                                     }
363                                 }
364                             }
365                         }
366                         lib.Close();
367                     }
368                     else if (directories.Length > 3)
369                     {
370                         bool bBreak = false;
371                         string fullDir = "";
372                         file = directories[2];
373                         ShellLibrary lib = Program.GetShellLibrary(directories[1]);
374                         if (lib == null)
375                             return;
376
377                         foreach (ShellObject o in lib)
378                         {
379                             if (o is ShellContainer)
380                             {
381                                 ShellContainer sc = o as ShellContainer;
382                                 foreach (ShellObject so in sc)
383                                 {
384                                     if (so.Name.Equals(file))
385                                     {
386                                         fullDir = so.ParsingName + "\\";
387                                         bBreak = true;
388                                     }
389                                     if (bBreak)
390                                         break;
391                                 }
392                             }
393                             if (bBreak)
394                                 break;
395                         }
396                         lib.Close();
397
398                         if (!bBreak)
399                             return;
400
401                         for (int i = 3; i < directories.Length; i++)
402                         {
403                             fullDir += directories[i];
404                             if (i + 1 < directories.Length)
405                                 fullDir += "\\";
406                         }
407
408                         int lindex = fullDir.LastIndexOf("\\") + 1;
409                         if (lindex > 0)
410                         {
411                             string dir = fullDir.Substring(0, lindex);
412                             string files = fullDir.Substring(lindex);
413                             this.AddFileSearchItem(files, dir);
414                         }
415                     }
416                 }
417                 else
418                 {
419                     int lindex = command.LastIndexOf('\\') + 1;
420                     if (lindex > 0)
421                     {
422                         string directories = command.Substring(0, lindex);
423                         string files = command.Substring(lindex);
424                         this.AddFileSearchItem(files, directories);
425                     }
426                 }
427             }
428             if(this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Count == 0)
429                 this.searchTextBox.TextBox.DropdownForm.DoVisibleChange(false);
430         }
431
432         public bool AddFileSearchItem(string files, string directories)
433         {
434             try
435             {
436                 if (Directory.GetFileSystemEntries(directories, "*").Length > 0)
437                 {
438                     this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Clear();
439                     if (this.searchTextBox.TextBox.Text.Equals(Directory.GetDirectoryRoot(directories)))
440                     {
441                         DriveInfo driveInfo = new DriveInfo(this.searchTextBox.TextBox.Text[0].ToString());
442                         if (!driveInfo.IsReady)
443                             return false;
444                     }
445                     foreach (string directory in Directory.GetDirectories(directories))
446                     {
447                         string name = directory.Substring(directories.Length) + "\\";
448                         if (name.StartsWith(files) || Program.Ime.StartsWith(Program.Ime.GetRomaji(name), files))
449                         {
450                             IconListBoxItem item = new IconListBoxItem();
451                             item.Name = name;
452                             item.Text = item.Name;
453                             item.ToolTipText = item.Text;
454                             item.Type = IconListBoxType.Directory;
455                             item.IconImage = Program.DirectoryIcon.ToBitmap();
456                             item.Regex = Program.Ime.GetRomaji(item.Name);
457                             this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Add(item);
458                         }
459                     }
460                     foreach (string s in Directory.GetFiles(directories))
461                     {
462                         string name = s.Substring(directories.Length);
463                         if (name.StartsWith(files) || Program.Ime.StartsWith(Program.Ime.GetRomaji(name), files))
464                         {
465                             int extindex;
466                             string ext;
467                             IconListBoxItem item = new IconListBoxItem();
468                             item.Name = name;
469                             item.Text = item.Name;
470                             item.ToolTipText = item.Name;
471                             item.Type = IconListBoxType.File;
472
473                             extindex = item.Name.LastIndexOf('.');
474                             ext = extindex == -1 ? "" : item.Name.Substring(extindex + 1);
475
476                             if ("exe".Equals(ext) || "lnk".Equals(ext))
477                             {
478                                 Win32.SHFILEINFO shinfo = new Win32.SHFILEINFO();
479                                 Win32.SHGetFileInfo(s, 0, ref shinfo, Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_LARGEICON);
480                                 if (shinfo.hIcon != IntPtr.Zero)
481                                     item.IconImage = System.Drawing.Icon.FromHandle(shinfo.hIcon).ToBitmap();
482                             }
483                             else
484                             {
485                                 if (Program.ExtIconCacheDic.ContainsKey(ext))
486                                 {
487                                     if (Program.ExtIconCacheDic[ext] != null)
488                                         item.IconImage = Program.ExtIconCacheDic[ext].ToBitmap();
489                                 }
490                                 else
491                                 {
492                                     Win32.SHFILEINFO shinfo = new Win32.SHFILEINFO();
493                                     Win32.SHGetFileInfo(s, 0, ref shinfo, Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_LARGEICON);
494                                     if (shinfo.hIcon != IntPtr.Zero)
495                                     {
496                                         Icon icon = System.Drawing.Icon.FromHandle(shinfo.hIcon);
497                                         Program.ExtIconCacheDic.Add(ext, icon);
498                                         item.IconImage = icon.ToBitmap();
499                                     }
500                                     else
501                                     {
502                                         Program.ExtIconCacheDic.Add(ext, null);
503                                     }
504                                 }
505                             }
506                             item.Regex = Program.Ime.GetRomaji(item.Name);
507                             this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Add(item);
508                         }
509                     }
510                 }
511                 return true;
512             }
513             catch (Exception e)
514             {
515                 return false;
516                 //MessageBox.Show(e.ToString());
517                 //this.searchTextBox.TextBox.DropdownForm.DoVisibleChange(false);
518             }
519
520         }
521
522         /// <summary>
523         /// 検索エンジンのサジェスト
524         /// </summary>
525         public void SetEngineDropdownList(string text)
526         {
527             if (!this.suggestSupport || this.suggestString.Equals(text))
528                 return;
529
530             EngineInfo info = Program.GetEngineInfo(this.SelectedEngineIndex);
531             if (info == null)
532                 return;
533
534
535             string url = info.SuggestUrl.Replace("%s", Uri.EscapeDataString(text));
536             HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
537             req.Method = "GET";
538             req.UserAgent = Program.UserAgent;
539
540             AsyncCallback readCallBack = new AsyncCallback(SuggestReadCallBack);
541             req.BeginGetResponse(readCallBack, req);
542             this.suggestString = text;
543
544         }
545
546
547         /// <summary>
548         /// DropdownFormにエンジンリスト一覧を表示する
549         /// </summary>
550         public void ShowEngineList()
551         {
552             if (!this.SearchTextBox.TextBox.DropdownForm.Visible)
553             {
554                 //検索エンジンリストの表示
555                 this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Clear();
556                 foreach (string key in Program.DefaultEngineList)
557                 {
558                     EngineInfo info = Program.DefaultEngineDic[key];
559                     IconListBoxItem item = new IconListBoxItem();
560                     item.Name = info.Name;
561                     item.Text = info.Name;
562                     item.ToolTipText = info.Display;
563                     item.IconImage = info.IconImage;
564                     item.Type = IconListBoxType.SearchEngine;
565                     item.Data = info;
566                     item.Regex = Program.Ime.GetRomaji(item.Name);
567                     this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Add(item);
568                 }
569                 foreach (string key in Program.EngineList)
570                 {
571                     EngineInfo info = Program.EngineDic[key];
572                     IconListBoxItem item = new IconListBoxItem();
573                     item.Name = info.Name;
574                     item.Text = info.Name;
575                     item.ToolTipText = info.Display;
576                     item.IconImage = info.IconImage;
577                     item.Type = IconListBoxType.SearchEngine;
578                     item.Data = info;
579                     item.Regex = Program.Ime.GetRomaji(item.Name);
580                     this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Add(item);
581                 }
582                 this.searchTextBox.TextBox.SetDropdownFormLocation();
583                 this.searchTextBox.TextBox.DropdownForm.DoVisibleChange(true);
584                 this.searchTextBox.TextBox.DropdownForm.DropdownListBox.SelectedIndex = 0;
585                 Program.Status = ProgramStatus.SelectingSearchEngine;
586             }
587         }
588
589
590         private void SuggestReadCallBack(IAsyncResult ar)
591         {
592             try
593             {
594                 HttpWebRequest req = (HttpWebRequest)ar.AsyncState;
595                 HttpWebResponse response = (HttpWebResponse)req.EndGetResponse(ar);
596                 StreamReader streamReader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(response.CharacterSet));
597                 string str = streamReader.ReadToEnd();
598                 streamReader.Close();
599                 Win32.PostMessage(Program.MainFormHandle, Program.WM_SUGGEST_END, IntPtr.Zero, Marshal.StringToHGlobalAuto(str));
600             }
601             catch (Exception ex)
602             {
603                 Debug.WriteLine("error:" + ex);
604             } 
605         } 
606
607         private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
608         {
609             if (Program.HotKeyEngineList.Contains(KeyboardHook.Keycode))
610             {
611                 EngineInfo info = Program.GetEngineInfoFromHotKey(KeyboardHook.Keycode);
612                 if (info == null)
613                     return;
614
615                 this.SetSearchEngine(info);
616                 if (this.searchTextBox.TextBox.DropdownForm.Visible)
617                     this.searchTextBox.TextBox.DropdownForm.DoVisibleChange(false);
618                 e.Handled = true;
619                 return;
620             }
621
622             switch ((Keys)e.KeyChar)
623             {
624                 case Keys.Tab:
625                     e.Handled = true;
626                     if (this.searchTextBox.TextBox.DropdownForm.Visible && this.searchTextBox.TextBox.DropdownForm.DropdownListBox.SelectedIndex >= 0)
627                     {
628                         //タブキーによるコマンド補完
629                         this.searchTextBox.TextBox.Complemented = true;
630                         IconListBoxItem item = (IconListBoxItem)this.searchTextBox.TextBox.DropdownForm.DropdownListBox.SelectedItem;
631                         switch (item.Type)
632                         {
633                             case IconListBoxType.File:
634                             case IconListBoxType.Directory:
635                                 int index = this.searchTextBox.TextBox.Text.LastIndexOf('\\');
636                                 if (index == -1)
637                                     this.searchTextBox.TextBox.Text = item.Name;
638                                 else
639                                     this.searchTextBox.TextBox.Text = this.searchTextBox.TextBox.Text.Substring(0, index + 1) + item.Name;
640                                 this.searchTextBox.TextBox.SelectionStart = this.searchTextBox.TextBox.Text.Length;
641                                 Program.Status = ProgramStatus.MainFormOpen; 
642                                 this.searchTextBox.TextBox.DropdownForm.DoVisibleChange(false);
643                                 break;
644                             case IconListBoxType.Command:
645                             case IconListBoxType.History:
646                             case IconListBoxType.SysCommand:
647                             case IconListBoxType.SearchEngineText:
648                                 this.searchTextBox.TextBox.Text = item.Text;
649                                 this.searchTextBox.TextBox.SelectionStart = this.searchTextBox.TextBox.Text.Length;
650                                 Program.Status = ProgramStatus.MainFormOpen; 
651                                 this.searchTextBox.TextBox.DropdownForm.DoVisibleChange(false);
652                                 break;
653                             case IconListBoxType.SearchEngine:
654                                 EngineInfo info = (EngineInfo)item.Data;
655                                 this.SetSearchEngine(info);
656                                 Program.Status = ProgramStatus.MainFormOpen; 
657                                 this.searchTextBox.TextBox.DropdownForm.DoVisibleChange(false);
658                                 return;
659                         }
660                     }
661
662                     if (this.SearchTextBox.TextBox.Complemented)
663                         return;
664
665                     if (!this.SearchTextBox.TextBox.DropdownForm.Visible)
666                     {
667                         this.ShowDropdownForm(false);
668                         return;
669                     }
670                     break;
671
672                 case Keys.Space:
673                     if ((Control.ModifierKeys & Keys.Control) == Keys.Control)
674                     {
675                         e.Handled = true;
676                         if (!this.searchTextBox.TextBox.DropdownForm.Visible)
677                         {
678                             switch (this.SelectedEngineType)
679                             {
680                                 case 0: //Passer
681                                 case 1: //FileSearch
682                                     if (this.SearchTextBox.TextBox.ShowableDropdownList)
683                                         this.ShowDropdownForm(false);
684                                     break;
685                                 case 2: //WebSearch
686                                     this.suggestString = "";
687                                     this.textChanged = true;
688                                     break;
689                             }
690                         }
691                     }
692                     break;
693
694                 case Keys.Enter:
695                     e.Handled = true;
696                     if (this.searchTextBox.TextBox.DropdownForm.Visible)
697                     {
698                         if (this.searchTextBox.TextBox.DropdownForm.DropdownListBox.SelectedIndex >= 0)
699                         {
700                             IconListBoxItem item = (IconListBoxItem)this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items[this.searchTextBox.TextBox.DropdownForm.DropdownListBox.SelectedIndex];
701                             if (item.Type == IconListBoxType.SearchEngine)
702                             {
703                                 EngineInfo info = (EngineInfo)item.Data;
704                                 this.SetSearchEngine(info);
705                                 Program.Status = ProgramStatus.MainFormOpen;
706                                 this.searchTextBox.TextBox.DropdownForm.DoVisibleChange(false);
707                                 break;
708                             }
709                             else
710                             {
711                                 if (item.Type == IconListBoxType.File || item.Type == IconListBoxType.Directory)
712                                 {
713                                     int index = this.searchTextBox.TextBox.Text.LastIndexOf('\\');
714                                     if (index == -1)
715                                         this.searchTextBox.TextBox.Text = item.Name;
716                                     else
717                                         this.searchTextBox.TextBox.Text = this.searchTextBox.TextBox.Text.Substring(0, index + 1) + item.Name;
718                                     this.searchTextBox.TextBox.SelectionStart = this.searchTextBox.TextBox.Text.Length;
719                                     Program.Status = ProgramStatus.MainFormOpen;
720                                     this.searchTextBox.TextBox.DropdownForm.DoVisibleChange(false);
721                                 }
722                                 else
723                                     this.searchTextBox.TextBox.Text = this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Text;
724                                 this.searchTextBox.TextBox.SelectionStart = this.Text.Length;
725                                 Program.Status = ProgramStatus.MainFormOpen;
726                                 this.searchTextBox.TextBox.DropdownForm.DoVisibleChange(false);
727                             }
728                         }
729                     }
730
731                     string command = this.SearchTextBox.TextBox.Text.Trim();
732                     switch (this.SelectedEngineType)
733                     {
734                         case 0: //COMMAND
735                             if (this.ExecCommand(command))
736                             {
737                                 Program.AddHistoryList(command);
738                             }
739                             break;
740
741                         case 1: //FILE_VIEW
742                             this.ExecFile(command);
743                             break;
744
745                         case 2: //WEB_SEARCH
746                             this.ExecSearch(command);
747                             break;
748                     }
749                     return;
750
751                 case Keys.Escape:
752                     if (this.SearchTextBox.TextBox.DropdownForm.Visible)
753                     {
754                         Program.Status = ProgramStatus.MainFormOpen;
755                         this.SearchTextBox.TextBox.DropdownForm.DoVisibleChange(false);
756                         Program.HideToolTip();
757                         e.Handled = true;
758                         return;
759                     }
760                     if (!"".Equals(searchTextBox.TextBox.Text))
761                     {
762                         this.SearchTextBox.TextBox.Text = "";
763                         this.SearchTextBox.TextBox.SelectionStart = 0;
764                         e.Handled = true;
765                         return;
766                     }
767                     if(this.Visible)
768                     {
769                         this.DoVisibleChange(false);
770                         Program.ForceForegroundWindow(this.beforeHWnd);
771                     }
772                     e.Handled = true;
773                     return;
774
775                 default:
776                     if (Program.Status == ProgramStatus.SelectingSearchEngine)
777                     {
778                         //検索エンジンリストが開いてる場合
779                         string key = e.KeyChar + "";
780                         IconListBoxItem item;
781                         int index = this.SearchTextBox.TextBox.DropdownForm.DropdownListBox.SelectedIndex + 1;
782                         if (index >= this.SearchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Count)
783                             index = 0;
784                         for (int i = 0; i < this.SearchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Count; i++)
785                         {
786                             item = (IconListBoxItem)this.SearchTextBox.TextBox.DropdownForm.DropdownListBox.Items[index];
787                             EngineInfo info = (EngineInfo)item.Data;
788                             if (Program.Ime.StartsWith(item.Regex, key))
789                             {
790                                 this.SearchTextBox.TextBox.DropdownForm.DropdownListBox.SelectedIndex = index;
791                                 break;
792                             }
793                             if (++index >= this.SearchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Count)
794                                 index = 0;
795                         }
796
797                         e.Handled = true;
798                     }
799                     break;
800             }
801         }
802
803         private void textBox_cmd_KeyDown(object sender, KeyEventArgs e)
804         {
805             if (Program.MainForm.Opacity != Program.FormOpacity)
806                 Program.MainForm.Opacity = Program.FormOpacity;
807
808             switch (e.KeyCode)
809             {
810                 case Keys.Up:
811                     if ((Keys.Control & Control.ModifierKeys) == Keys.Control)
812                     {
813                         if (!this.SearchTextBox.TextBox.DropdownForm.Visible)
814                         {
815                             //検索エンジンの切替
816                             this.SelectedEngineIndex--;
817                             if (this.SelectedEngineIndex == -1)
818                                 this.SelectedEngineIndex = Program.DefaultEngineList.Count + Program.EngineList.Count - 1;
819                             this.SetSearchEngine(Program.GetEngineInfo(this.SelectedEngineIndex));
820                         }
821                     }
822                     else
823                     {
824                         if (this.SelectedEngineType == 0)
825                         {
826                             if (!this.SearchTextBox.TextBox.DropdownForm.Visible)
827                             {
828                                 if (Program.CommandHistoryList.Count > 0)
829                                 {
830                                     if (Program.CommandHistoryList.Count > Program.CommandHistoryIndex)
831                                         Program.CommandHistoryIndex++;
832                                     this.showDropdownSupport = false;
833                                     this.SearchTextBox.TextBox.Text = Program.CommandHistoryList[Program.CommandHistoryIndex - 1];
834                                     this.SearchTextBox.TextBox.SelectionStart = this.SearchTextBox.TextBox.Text.Length;
835                                     this.showDropdownSupport = true;
836                                 }
837                             }
838                         }
839                     }
840                     break;
841
842                 case Keys.Down:
843                     if ((Keys.Alt & Control.ModifierKeys) == Keys.Alt)
844                     {
845                         this.ShowEngineList();
846                     }
847                     else if ((Keys.Control & Control.ModifierKeys) == Keys.Control)
848                     {
849                         if (!this.SearchTextBox.TextBox.DropdownForm.Visible)
850                         {
851                             //検索エンジンの切替
852                             this.SelectedEngineIndex++;
853                             int count = Program.DefaultEngineList.Count + Program.EngineList.Count;
854                             if (this.SelectedEngineIndex == count)
855                                 this.SelectedEngineIndex = 0;
856                             this.SetSearchEngine(Program.GetEngineInfo(this.SelectedEngineIndex));
857                         }
858                     }
859                     else
860                     {
861                         switch (this.SelectedEngineType)
862                         {
863                             case 0: //Passer
864                                 if (!this.SearchTextBox.TextBox.DropdownForm.Visible)
865                                 {
866                                     if (Program.CommandHistoryIndex > 0)
867                                     {
868                                         Program.CommandHistoryIndex--;
869                                         this.showDropdownSupport = false;
870                                         if (Program.CommandHistoryIndex == 0)
871                                             this.SearchTextBox.TextBox.Text = "";
872                                         else
873                                             this.SearchTextBox.TextBox.Text = Program.CommandHistoryList[Program.CommandHistoryIndex - 1];
874                                         this.SearchTextBox.TextBox.SelectionStart = this.SearchTextBox.TextBox.Text.Length;
875                                         this.showDropdownSupport = true;
876                                     }
877                                     else
878                                     {
879                                         this.ShowDropdownForm(false);
880                                     }
881                                 }
882                                 break;
883
884                             case 1: //FileSearch
885                                 if (!this.SearchTextBox.TextBox.DropdownForm.Visible)
886                                 {
887                                     this.ShowDropdownForm(false);
888                                 }
889                                 break;
890                             case 2:
891                                 if (!this.SearchTextBox.TextBox.DropdownForm.Visible)
892                                 {
893                                     this.suggestString = "";
894                                     this.textChanged = true;
895                                 }
896                                 break;
897                         }
898                     }
899                     break;
900             }
901         }
902         
903         private void textBox_cmd_TextChanged(object sender, EventArgs e)
904         {
905             this.latestKeyTime = DateTime.Now.Ticks;
906             this.searchTextBox.TextBox.ImeText = this.searchTextBox.TextBox.Text;
907             if (!this.showDropdownSupport || !this.Visible)
908                 return;
909
910             Size s = TextRenderer.MeasureText(this.searchTextBox.TextBox.Text, this.searchTextBox.TextBox.Font);
911             if (s.Width > this.searchTextBox.TextBox.Width)
912             {
913                 //MessageBox.Show("!!!");
914             }
915
916             if(!this.searchTextBox.TextBox.Complemented)
917                 this.ShowDropdownForm(true);
918         }
919
920         private void textBox_cmd_Complement(IconListBoxType type)
921         {
922         }
923
924         private void textBox_cmd_ImeComposition(int type, string text)
925         {
926             if(!String.IsNullOrEmpty(this.searchTextBox.TextBox.Text))
927             {
928                 string str = this.searchTextBox.TextBox.Text;
929                 text = str.Substring(0, this.searchTextBox.TextBox.SelectionStart)
930                      + text
931                      + str.Substring(this.searchTextBox.TextBox.SelectionStart);
932             }
933             this.searchTextBox.TextBox.ImeText = text;
934
935             if (type == 0 && String.IsNullOrEmpty(text))
936             {
937                 Program.Status = ProgramStatus.MainFormOpen;
938                 this.searchTextBox.TextBox.DropdownForm.DoVisibleChange(false);
939                 this.suggestString = "";
940             }
941             else
942             {
943                 if ((type & Win32.GCS_RESULTSTR) != Win32.GCS_RESULTSTR)
944                 {
945                     this.latestKeyTime = DateTime.Now.Ticks;
946                     this.textChanged = true;
947                 }
948             }
949         }
950
951         private void dropdownListBox_SelectedIndexChanged(object sender, EventArgs e)
952         {
953             Program.HideToolTip();
954             if (!this.searchTextBox.TextBox.DropdownForm.Visible)
955                 return;
956
957             IconListBox listBox = this.searchTextBox.TextBox.DropdownForm.DropdownListBox;
958             if (listBox.SelectedIndex == -1)
959                 return;
960
961             string text = listBox.Text;
962             if (text.Equals(this.dropdownListBoxText))
963                 return;
964
965             int lastIndex = listBox.ClientRectangle.Height / listBox.ItemHeight;
966             if (listBox.TopIndex > listBox.SelectedIndex || (listBox.TopIndex + lastIndex) <= listBox.SelectedIndex)
967                 return;
968
969             IconListBoxItem item = (IconListBoxItem)listBox.SelectedItem;
970             if (item.Type == IconListBoxType.SearchEngineText)
971                 return;
972
973             DropdownListForm dropdownForm = this.searchTextBox.TextBox.DropdownForm;
974
975             Rectangle r = dropdownForm.Bounds;
976             r.Y = dropdownForm.Location.Y + listBox.GetItemRectangle(listBox.SelectedIndex).Y;
977             if (this.SelectedEngineType == 0 && (item.ToolTipText == null || item.ToolTipText.Length == 0))
978             {\r
979                 if (Program.CommandDic.ContainsKey(text))\r
980                 {\r
981                     CommandInfo info = Program.CommandDic[text];\r
982                     text = info.Comment == null ? info.Name : info.Comment;\r
983                 }
984             }
985             else
986             {
987                 text = item.ToolTipText;
988             }
989             Program.ShowToolTip(Util.CutText(text, 50, 10), r, 5000, false);
990         }
991
992         private void timer1_Tick(object sender, System.EventArgs e)
993         {
994             if (this.Visible)
995             {
996                 if (this.Opacity + Program.FadeInSpeed < Program.FormOpacity)
997                 {
998                     this.Opacity += Program.FadeInSpeed;
999                     this.searchTextBox.TextBox.DropdownForm.Opacity = this.Opacity;
1000                     this.Update();
1001                 }
1002                 else
1003                 {
1004                     this.Opacity = Program.FormOpacity;
1005                     this.searchTextBox.TextBox.DropdownForm.Opacity = this.Opacity;
1006                     this.timer1.Stop();
1007                 }
1008             }
1009         }
1010
1011         private void timer_keyRecoding_Tick(object sender, EventArgs e)
1012         {
1013             if (Program.Status == ProgramStatus.KeyRecoding && Program.Main_ShowBalloonWhileRecoding && Program.BallonWindowHandle != IntPtr.Zero)
1014                 Win32.PostMessage(Program.BallonWindowHandle, Win32.WM_MOUSEMOVE, (IntPtr)0, (IntPtr)0);
1015         }
1016
1017         private void timer_suggest_Tick(object sender, EventArgs e)
1018         {
1019             if (this.SelectedEngineType == 2 && this.textChanged)
1020             {
1021                 string text = this.searchTextBox.TextBox.ImeText;
1022                 if (String.IsNullOrEmpty(text))
1023                     return;
1024
1025                 if (DateTime.Now.Ticks > this.latestKeyTime + 000000 && !this.suggestString.Equals(text))
1026                 {
1027                     this.textChanged = false;
1028                     this.SetEngineDropdownList(text);
1029                 }
1030             }
1031         }
1032
1033         private void toolTip_Popup(object sender, PopupEventArgs e)
1034         {
1035        
1036         }
1037
1038         private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
1039         {
1040             if (e.Button == MouseButtons.Left)
1041             {
1042                 switch (Program.Status)
1043                 {
1044                     case ProgramStatus.Loading:
1045                     case ProgramStatus.Waiting:
1046                     case ProgramStatus.MainFormOpen:
1047                         HotKeyProcessExecuter.OpenMainForm();
1048                         break;
1049                 }
1050             }
1051         }
1052
1053         private void DropdownListView_MouseDoubleClick(object sender, MouseEventArgs e)
1054         {
1055             this.searchTextBox.TextBox.Text = this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Text;
1056             this.searchTextBox.TextBox.Select(this.searchTextBox.TextBox.Text.Length, 0);
1057             Program.Status = ProgramStatus.MainFormOpen;
1058             this.searchTextBox.TextBox.DropdownForm.DoVisibleChange(false);
1059         }
1060
1061         private void 表示ToolStripMenuItem_Click(object sender, EventArgs e)
1062         {
1063             switch (Program.Status)
1064             {
1065                 case ProgramStatus.Loading:
1066                 case ProgramStatus.Waiting:
1067                 case ProgramStatus.MainFormOpen:
1068                     HotKeyProcessExecuter.OpenMainForm();
1069                     break;
1070             }
1071         }
1072
1073         private void リスト設定ToolStripMenuItem_Click(object sender, EventArgs e)
1074         {
1075             if (!Program.SettingForm.Visible)
1076             {
1077                 this.DoVisibleChange(false);
1078                 Program.SettingForm.DoVisibleChange(true);
1079             }
1080         }
1081
1082         private void バージョン情報ToolStripMenuItem_Click(object sender, EventArgs e)
1083         {
1084             if (Program.Status == ProgramStatus.Waiting)
1085             {
1086                 Program.Status = ProgramStatus.VersionFormOpen;
1087                 VersionForm f = new VersionForm();
1088                 f.ShowDialog();
1089                 Program.Status = ProgramStatus.Waiting;
1090             }
1091         }
1092
1093         private void 終了ToolStripMenuItem_Click(object sender, EventArgs e)
1094         {
1095             this.Close();
1096             Application.Exit();
1097         }
1098     }
1099 }