3 * $Date: 2010-03-18 20:45:05 +0900 (木, 18 3 2010) $
7 using System.Collections.Generic;
8 using System.Diagnostics;
12 using System.Windows.Forms;
15 using Lugens.Components;
17 using System.Runtime.InteropServices;
18 using Newtonsoft.Json;
20 namespace Lugens.Passer
22 public partial class MainForm
25 private void DoResize(int width)
27 Size size = this.Size;
31 else if (size.Width > 600)
34 Settings.Set(Settings.PASSER_FORM_MAIN_WIDTH, size.Width);
38 size.Height = this.searchTextBox.Size.Height;
39 this.searchTextBox.Size = size;
40 if (this.searchTextBox.TextBox.DropdownForm.Visible)
41 this.searchTextBox.TextBox.DropdownForm.DoVisibleChange(false);
44 //IntPtr hRgn = Win32.CreateRoundRectRgn(0, 0, this.Size.Width, this.Size.Height, 6, 6);
45 //Win32.SetWindowRgn(this.Handle, hRgn, true);
50 private void MainForm_MouseDown(object sender, MouseEventArgs e)
52 if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
54 this.mousePoint = new Point(-e.X, -e.Y);
55 this.windowMoveFlag = true;
59 private void MainForm_MouseMove(object sender, MouseEventArgs e)
61 if (this.Cursor == Cursors.SizeWE && (e.Button & MouseButtons.Left) == MouseButtons.Left)
66 else if (this.windowMoveFlag && (e.Button & MouseButtons.Left) == MouseButtons.Left)
69 this.seenPoint = new Point(this.Left + mousePoint.X + e.X, this.Top + mousePoint.Y + e.Y);
70 this.Location = this.seenPoint;
72 Settings.Set(Settings.PASSER_FORM_MAIN_X, this.seenPoint.X);
73 Settings.Set(Settings.PASSER_FORM_MAIN_Y, this.seenPoint.Y);
75 if (this.searchTextBox.TextBox.DropdownForm.Visible)
76 this.searchTextBox.TextBox.DropdownForm.DoVisibleChange(false);
78 else if (e.X >= (this.Size.Width - 4))
81 this.Cursor = Cursors.SizeWE;
86 if (this.Cursor == Cursors.SizeWE)
87 this.Cursor = Cursors.Default;
91 private void MainForm_MouseUp(object sender, MouseEventArgs e)
93 this.windowMoveFlag = false;
96 private void MainForm_Load(object sender, System.EventArgs e)
99 this.Size = new Size(256, 32);
100 this.Location = this.seenPoint;
102 this.searchTextBox.TextBox.DropdownForm.DrawType = Lugens.Components.DropdownListForm.DrawMode.IconText;
103 this.searchTextBox.TextBox.DropdownForm.IconCallBack = this.GetIconCallBack;
104 this.searchTextBox.TextBox.DropdownForm.VisibleChanged += this.dropdownListBox_SelectedIndexChanged;
105 this.searchTextBox.TextBox.DropdownForm.DropdownListBox.SelectedIndexChanged += this.dropdownListBox_SelectedIndexChanged;
106 this.searchTextBox.TextBox.DropdownForm.DropdownListBox.VisibleChanged += this.dropdownListBox_SelectedIndexChanged;
107 this.Activated += this.dropdownListBox_SelectedIndexChanged;
108 this.AddOwnedForm(this.searchTextBox.TextBox.DropdownForm);
111 private void ShowDropdownForm(bool filter)
113 switch (this.SelectedEngineType)
117 this.SetDropdownList(filter);
118 if (this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Count > 0)
120 this.searchTextBox.TextBox.DropdownForm.DropdownListBox.SelectedIndex = 0;
121 this.searchTextBox.TextBox.DropdownForm.DoVisibleChange(true);
126 if(String.IsNullOrEmpty(this.searchTextBox.TextBox.Text))
127 this.searchTextBox.TextBox.DropdownForm.DoVisibleChange(false);
129 this.SetEngineDropdownList();
134 private void SetDropdownList()
136 this.SetDropdownList(true);
139 private void SetRootDriveList()
141 Program.RootDriveList.Clear();
142 foreach (DriveInfo d in DriveInfo.GetDrives())
144 IconListViewItem item = new IconListViewItem();
147 item.Type = IconListViewType.Directory;
150 case DriveType.Fixed:
151 if (Environment.SystemDirectory.StartsWith(d.Name))
152 item.Icon = Properties.Resources.IconDriveWin;
154 item.Icon = Properties.Resources.IconDriveFix;
156 case DriveType.Network:
157 item.Icon = Properties.Resources.IconDriveNet;
159 case DriveType.CDRom:
160 item.Icon = Properties.Resources.IconDriveCD;
162 case DriveType.Removable:
163 item.Icon = Properties.Resources.IconDriveRem;
166 item.Icon = Properties.Resources.IconDriveFix;
169 Program.RootDriveList.Add(item);
170 this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Add(item);
174 private void SetDropdownList(bool filter)
176 string[] commands = Program.ParseCommand(this.searchTextBox.TextBox.Text);
178 if(commands.Length > 0)
179 command = commands[commands.Length - 1];
181 this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Clear();
182 if (filter && command.Equals(""))
184 this.searchTextBox.TextBox.DropdownForm.DoVisibleChange(false);
188 // if ("c:\\".Equals(command))
190 // //command = command;
193 if (!command.Equals("") && command[0] == '\\')
196 if (command.Length == 1)
198 this.SetRootDriveList();
200 else if (command.LastIndexOf('\\') == 0)
203 string str = command.Substring(1);
204 foreach (ListViewItem item in Program.RootDriveList)
206 if (this.textBox_cmd.CaseSensitive)
208 if (item.Text.StartsWith(str))
209 this.textBox_cmd.DropdownForm.DropdownListView.Items.Add(item);
213 if (item.Text.ToLower().StartsWith(str.ToLower()))
214 this.textBox_cmd.DropdownForm.DropdownListView.Items.Add(item);
219 else if (!command.Equals("") && command[0] == '!')
221 if (command.Length == 1)
223 foreach (string str in Program.CommandHistoryList)
225 IconListBoxItem item = new IconListBoxItem();
228 item.Type = IconListBoxType.History;
229 item.IconImage = Properties.Resources.IconHistory.ToBitmap();
230 this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Add(item);
235 foreach (string str in Program.CommandHistoryList)
237 string text = command.Substring(1);
238 //if (item.Text.ToLower().StartsWith(this.textBox_cmd.Text.ToLower()) || Program.Ime.StartsWith(item.Regex, this.textBox_cmd.Text))
240 if (str.ToLower().StartsWith(text.ToLower()))
242 IconListBoxItem item = new IconListBoxItem();
245 item.Type = IconListBoxType.History;
246 item.IconImage = Properties.Resources.IconHistory.ToBitmap();
247 this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Add(item);
255 int lindex = command.LastIndexOf('\\') + 1;
258 string directories = command.Substring(0, lindex);
259 string files = command.Substring(lindex);
263 if (Directory.GetFileSystemEntries(directories, files + "*").Length > 0)
265 this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Clear();
266 if (this.searchTextBox.TextBox.Text.Equals(Directory.GetDirectoryRoot(command)))
268 DriveInfo driveInfo = new DriveInfo(this.searchTextBox.TextBox.Text[0].ToString());
269 if (!driveInfo.IsReady)
272 foreach (string directory in Directory.GetDirectories(directories, files + "*"))
274 IconListBoxItem item = new IconListBoxItem();
275 item.Name = directory.Substring(directories.Length) + "\\";
276 item.Text = directory + "\\";
277 item.Type = IconListBoxType.Directory;
278 item.IconImage = Program.DirectoryIcon.ToBitmap();
279 this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Add(item);
281 foreach (string file in Directory.GetFiles(directories, files + "*"))
285 IconListBoxItem item = new IconListBoxItem();
286 item.Name = file.Substring(directories.Length);
288 item.Type = IconListBoxType.Directory;
290 extindex = item.Name.LastIndexOf('.');
291 ext = extindex == -1 ? "" : item.Name.Substring(extindex + 1);
293 if ("exe".Equals(ext) || "lnk".Equals(ext))
295 Win32.SHFILEINFO shinfo = new Win32.SHFILEINFO();
296 Win32.SHGetFileInfo(file, 0, ref shinfo, Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_LARGEICON);
297 if (shinfo.hIcon != IntPtr.Zero)
298 item.IconImage = System.Drawing.Icon.FromHandle(shinfo.hIcon).ToBitmap();
302 if (Program.ExtIconCacheDic.ContainsKey(ext))
304 if (Program.ExtIconCacheDic[ext] != null)
305 item.IconImage = Program.ExtIconCacheDic[ext].ToBitmap();
309 Win32.SHFILEINFO shinfo = new Win32.SHFILEINFO();
310 Win32.SHGetFileInfo(file, 0, ref shinfo, Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_LARGEICON);
311 if (shinfo.hIcon != IntPtr.Zero)
313 Icon icon = System.Drawing.Icon.FromHandle(shinfo.hIcon);
314 Program.ExtIconCacheDic.Add(ext, icon);
315 item.IconImage = icon.ToBitmap();
319 Program.ExtIconCacheDic.Add(ext, null);
323 this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Add(item);
331 foreach (IconListBoxItem item in this.searchTextBox.TextBox.Items)
333 if (item.Text.ToLower().StartsWith(this.searchTextBox.TextBox.Text.ToLower()) || Program.Ime.StartsWith(item.Regex, this.searchTextBox.TextBox.Text))
334 this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Add(item);
336 if (command.Length > 0)
338 foreach (string str in Program.CommandHistoryList)
340 if (str.StartsWith(command))
343 foreach (IconListBoxItem item in this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items)
345 if (str.Equals(item.Text))
354 IconListBoxItem i = new IconListBoxItem();
357 i.Type = IconListBoxType.History;
359 this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Add(i);
366 if (this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Count == 0)
368 this.searchTextBox.TextBox.DropdownForm.DoVisibleChange(false);
371 this.searchTextBox.TextBox.SetDropdownFormLocation();
377 public void SetEngineDropdownList()
379 string url = "http://suggestqueries.google.com/complete/search?client=firefox&q=" + HttpUtility.UrlEncode(this.searchTextBox.TextBox.Text);
380 WebRequest webReq = HttpWebRequest.Create(url);
381 webReq.Method = "GET";
382 webReq.ContentType = "application/x-www-form-urlencoded";
384 AsyncCallback readCallBack = new AsyncCallback(SuggestReadCallBack);
385 webReq.BeginGetResponse(readCallBack, webReq);
389 private void SuggestReadCallBack(IAsyncResult ar)
391 HttpWebRequest req = (HttpWebRequest)ar.AsyncState;
392 HttpWebResponse response = (HttpWebResponse)req.EndGetResponse(ar);
393 StreamReader streamReader = new StreamReader(response.GetResponseStream());
394 string str = streamReader.ReadToEnd();
395 streamReader.Close();
396 List<string> list = new List<string>();
397 using (StringReader stringReader = new StringReader(str))
399 using (JsonReader jsonReader = new JsonTextReader(stringReader))
401 while (jsonReader.Read())
403 if (jsonReader.TokenType == JsonToken.String)
404 list.Add(jsonReader.Value.ToString());
408 Win32.PostMessage(Program.MainFormHandle, Program.WM_SUGGEST_END, IntPtr.Zero, Marshal.StringToHGlobalAuto(str));
411 private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
413 if (Program.HotKeyEngineList.Contains(KeyboardHook.Keycode))
415 EngineInfo info = Program.GetEngineInfoFromHotKey(KeyboardHook.Keycode);
419 this.SetSearchEngine(info);
424 switch ((Keys)e.KeyChar)
428 if (this.SearchTextBox.TextBox.Complemented)
431 if (!this.SearchTextBox.TextBox.DropdownForm.Visible)
433 this.ShowDropdownForm(false);
439 if ((Control.ModifierKeys & Keys.Control) == Keys.Control)
441 if (this.SearchTextBox.TextBox.ShowableDropdownList)
442 this.ShowDropdownForm(false);
448 string command = this.SearchTextBox.TextBox.Text.Trim();
449 switch (this.SelectedEngineType)
452 if (this.ExecCommand(command))
454 Program.AddHistoryList(command);
462 this.ExecSearch(command);
469 if (this.SearchTextBox.TextBox.DropdownForm.Visible)
471 this.SearchTextBox.TextBox.DropdownForm.DoVisibleChange(false);
472 Program.HideToolTip();
476 if (!"".Equals(searchTextBox.TextBox.Text))
478 this.SearchTextBox.TextBox.Text = "";
479 this.SearchTextBox.TextBox.SelectionStart = 0;
485 this.DoVisibleChange(false);
486 Program.ForceForegroundWindow(this.beforeHWnd);
493 private void textBox_cmd_KeyDown(object sender, KeyEventArgs e)
495 if (Program.MainForm.Opacity != Program.FormOpacity)
496 Program.MainForm.Opacity = Program.FormOpacity;
501 if ((Keys.Control & Control.ModifierKeys) == Keys.Control)
504 this.SelectedEngineIndex--;
505 if(this.SelectedEngineIndex == -1)
506 this.SelectedEngineIndex = Program.DefaultEngineList.Count + Program.EngineList.Count - 1;
507 this.SetSearchEngine(Program.GetEngineInfo(this.SelectedEngineIndex));
511 if (!this.SearchTextBox.TextBox.DropdownForm.Visible)
513 if (Program.CommandHistoryList.Count > 0)
515 if (Program.CommandHistoryList.Count > Program.CommandHistoryIndex)
516 Program.CommandHistoryIndex++;
517 this.showDropdownSupport = false;
518 this.SearchTextBox.TextBox.Text = Program.CommandHistoryList[Program.CommandHistoryIndex - 1];
519 this.SearchTextBox.TextBox.SelectionStart = this.SearchTextBox.TextBox.Text.Length;
520 this.showDropdownSupport = true;
527 if ((Keys.Control & Control.ModifierKeys) == Keys.Control)
530 this.SelectedEngineIndex++;
531 int count = Program.DefaultEngineList.Count + Program.EngineList.Count;
532 if (this.SelectedEngineIndex == count)
533 this.SelectedEngineIndex = 0;
534 this.SetSearchEngine(Program.GetEngineInfo(this.SelectedEngineIndex));
538 if (!this.SearchTextBox.TextBox.DropdownForm.Visible)
540 if (Program.CommandHistoryIndex > 0)
542 Program.CommandHistoryIndex--;
543 this.showDropdownSupport = false;
544 if (Program.CommandHistoryIndex == 0)
545 this.SearchTextBox.TextBox.Text = "";
547 this.SearchTextBox.TextBox.Text = Program.CommandHistoryList[Program.CommandHistoryIndex - 1];
548 this.SearchTextBox.TextBox.SelectionStart = this.SearchTextBox.TextBox.Text.Length;
549 this.showDropdownSupport = true;
553 this.ShowDropdownForm(false);
561 private void textBox_cmd_TextChanged(object sender, EventArgs e)
563 if (!this.showDropdownSupport)
566 Size s = TextRenderer.MeasureText(this.searchTextBox.TextBox.Text, this.searchTextBox.TextBox.Font);
567 if (s.Width > this.searchTextBox.TextBox.Width)
569 //MessageBox.Show("!!!");
572 if(!this.searchTextBox.TextBox.Complemented)
573 this.ShowDropdownForm(true);
576 private void textBox_cmd_Complement(IconListBoxType type)
578 //this.ShowFileList();
581 private void dropdownListBox_SelectedIndexChanged(object sender, EventArgs e)
583 Program.HideToolTip();
584 if (!this.searchTextBox.TextBox.DropdownForm.Visible)
587 IconListBox listBox = this.searchTextBox.TextBox.DropdownForm.DropdownListBox;
589 string text = listBox.Text;
590 if (text.Equals(this.dropdownListBoxText))
593 if (!(text.StartsWith(":") || Program.CommandDic.ContainsKey(text)))
596 int lastIndex = listBox.ClientRectangle.Height / listBox.ItemHeight;
597 if (listBox.TopIndex > listBox.SelectedIndex || (listBox.TopIndex + lastIndex) <= listBox.SelectedIndex)
600 IconListBoxItem item = (IconListBoxItem)listBox.SelectedItem;
601 DropdownListForm dropdownForm = this.searchTextBox.TextBox.DropdownForm;
603 Rectangle r = dropdownForm.Bounds;
604 r.Y = dropdownForm.Location.Y + listBox.GetItemRectangle(listBox.SelectedIndex).Y;
605 if (item.ToolTipText == null || item.ToolTipText.Length == 0)
607 CommandInfo info = Program.CommandDic[text];
608 text = info.Comment == null ? info.Name : info.Comment;
612 text = item.ToolTipText;
614 Program.ShowToolTip(Util.CutText(text, 50, 10), r, 5000, false);
617 private void timer1_Tick(object sender, System.EventArgs e)
621 if (this.Opacity + Program.FadeInSpeed < Program.FormOpacity)
623 this.Opacity += Program.FadeInSpeed;
624 this.searchTextBox.TextBox.DropdownForm.Opacity = this.Opacity;
629 this.Opacity = Program.FormOpacity;
630 this.searchTextBox.TextBox.DropdownForm.Opacity = this.Opacity;
636 private void timer_keyRecoding_Tick(object sender, EventArgs e)
638 if (Program.Status == ProgramStatus.KeyRecoding && Program.Main_ShowBalloonWhileRecoding && Program.BallonWindowHandle != IntPtr.Zero)
639 Win32.PostMessage(Program.BallonWindowHandle, Win32.WM_MOUSEMOVE, (IntPtr)0, (IntPtr)0);
642 private void toolTip_Popup(object sender, PopupEventArgs e)
647 private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
649 if (e.Button == MouseButtons.Left)
651 switch (Program.Status)
653 case ProgramStatus.Loading:
654 case ProgramStatus.Waiting:
655 case ProgramStatus.MainFormOpen:
656 HotKeyProcessExecuter.OpenMainForm();
662 private void DropdownListView_MouseDoubleClick(object sender, MouseEventArgs e)
664 this.searchTextBox.TextBox.Text = this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Text;
665 this.searchTextBox.TextBox.Select(this.searchTextBox.TextBox.Text.Length, 0);
666 this.searchTextBox.TextBox.DropdownForm.DoVisibleChange(false);
669 private void 表示ToolStripMenuItem_Click(object sender, EventArgs e)
671 switch (Program.Status)
673 case ProgramStatus.Loading:
674 case ProgramStatus.Waiting:
675 case ProgramStatus.MainFormOpen:
676 HotKeyProcessExecuter.OpenMainForm();
681 private void リスト設定ToolStripMenuItem_Click(object sender, EventArgs e)
683 if (!Program.SettingForm.Visible)
685 this.DoVisibleChange(false);
686 Program.SettingForm.DoVisibleChange(true);
690 private void バージョン情報ToolStripMenuItem_Click(object sender, EventArgs e)
692 if (Program.Status == ProgramStatus.Waiting)
694 Program.Status = ProgramStatus.VersionFormOpen;
695 VersionForm f = new VersionForm();
697 Program.Status = ProgramStatus.Waiting;
701 private void 終了ToolStripMenuItem_Click(object sender, EventArgs e)