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);
45 private void MainForm_MouseDown(object sender, MouseEventArgs e)
47 if ((e.Button & MouseButtons.Left) == MouseButtons.Left)
49 this.mousePoint = new Point(-e.X, -e.Y);
50 this.windowMoveFlag = true;
54 private void MainForm_MouseMove(object sender, MouseEventArgs e)
56 if (this.Cursor == Cursors.SizeWE && (e.Button & MouseButtons.Left) == MouseButtons.Left)
61 else if (this.windowMoveFlag && (e.Button & MouseButtons.Left) == MouseButtons.Left)
64 this.seenPoint = new Point(this.Left + mousePoint.X + e.X, this.Top + mousePoint.Y + e.Y);
65 this.Location = this.seenPoint;
67 Settings.Set(Settings.PASSER_FORM_MAIN_X, this.seenPoint.X);
68 Settings.Set(Settings.PASSER_FORM_MAIN_Y, this.seenPoint.Y);
70 if (this.searchTextBox.TextBox.DropdownForm.Visible)
71 this.searchTextBox.TextBox.DropdownForm.DoVisibleChange(false);
73 else if (e.X >= (this.Size.Width - 4))
76 this.Cursor = Cursors.SizeWE;
81 if (this.Cursor == Cursors.SizeWE)
82 this.Cursor = Cursors.Default;
86 private void MainForm_MouseUp(object sender, MouseEventArgs e)
88 this.windowMoveFlag = false;
91 private void MainForm_Load(object sender, System.EventArgs e)
94 this.Size = new Size(256, 32);
95 this.Location = this.seenPoint;
97 this.searchTextBox.TextBox.DropdownForm.DrawType = Lugens.Components.DropdownListForm.DrawMode.IconText;
98 this.searchTextBox.TextBox.DropdownForm.IconCallBack = this.GetIconCallBack;
99 this.searchTextBox.TextBox.DropdownForm.VisibleChanged += this.dropdownListBox_SelectedIndexChanged;
100 this.searchTextBox.TextBox.DropdownForm.DropdownListBox.SelectedIndexChanged += this.dropdownListBox_SelectedIndexChanged;
101 this.searchTextBox.TextBox.DropdownForm.DropdownListBox.VisibleChanged += this.dropdownListBox_SelectedIndexChanged;
102 this.Activated += this.dropdownListBox_SelectedIndexChanged;
103 this.AddOwnedForm(this.searchTextBox.TextBox.DropdownForm);
105 if (Program.Main_MemorizeEngine)
107 EngineInfo info = Program.GetEngineInfo(Settings.GetInt(Settings.PASSER_MAIN_ENGINE_INDEX));
109 this.SetSearchEngine(Program.GetEngineInfo(0));
111 this.SetSearchEngine(info);
115 private void ShowDropdownForm(bool filter)
117 switch (this.SelectedEngineType)
121 this.SetDropdownList(filter);
122 if (this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Count > 0)
124 this.searchTextBox.TextBox.DropdownForm.DropdownListBox.SelectedIndex = 0;
125 this.searchTextBox.TextBox.DropdownForm.DoVisibleChange(true);
130 if (String.IsNullOrEmpty(this.searchTextBox.TextBox.Text))
132 this.searchTextBox.TextBox.DropdownForm.DoVisibleChange(false);
133 this.suggestString = "";
137 this.textChanged = true;
143 private void SetDropdownList()
145 this.SetDropdownList(true);
148 private void SetRootDriveList()
150 Program.RootDriveList.Clear();
151 foreach (DriveInfo d in DriveInfo.GetDrives())
153 IconListViewItem item = new IconListViewItem();
156 item.Type = IconListViewType.Directory;
159 case DriveType.Fixed:
160 if (Environment.SystemDirectory.StartsWith(d.Name))
161 item.Icon = Properties.Resources.IconDriveWin;
163 item.Icon = Properties.Resources.IconDriveFix;
165 case DriveType.Network:
166 item.Icon = Properties.Resources.IconDriveNet;
168 case DriveType.CDRom:
169 item.Icon = Properties.Resources.IconDriveCD;
171 case DriveType.Removable:
172 item.Icon = Properties.Resources.IconDriveRem;
175 item.Icon = Properties.Resources.IconDriveFix;
178 Program.RootDriveList.Add(item);
179 this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Add(item);
183 private void SetDropdownList(bool filter)
185 string[] commands = Program.ParseCommand(this.searchTextBox.TextBox.Text);
187 if(commands.Length > 0)
188 command = commands[commands.Length - 1];
190 this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Clear();
191 if (filter && command.Equals(""))
193 this.searchTextBox.TextBox.DropdownForm.DoVisibleChange(false);
197 // if ("c:\\".Equals(command))
199 // //command = command;
202 if (!command.Equals("") && command[0] == '\\')
205 if (command.Length == 1)
207 this.SetRootDriveList();
209 else if (command.LastIndexOf('\\') == 0)
212 string str = command.Substring(1);
213 foreach (ListViewItem item in Program.RootDriveList)
215 if (this.textBox_cmd.CaseSensitive)
217 if (item.Text.StartsWith(str))
218 this.textBox_cmd.DropdownForm.DropdownListView.Items.Add(item);
222 if (item.Text.ToLower().StartsWith(str.ToLower()))
223 this.textBox_cmd.DropdownForm.DropdownListView.Items.Add(item);
228 else if (!command.Equals("") && command[0] == '!')
230 if (command.Length == 1)
232 foreach (string str in Program.CommandHistoryList)
234 IconListBoxItem item = new IconListBoxItem();
237 item.Type = IconListBoxType.History;
238 item.IconImage = Properties.Resources.IconHistory.ToBitmap();
239 this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Add(item);
244 foreach (string str in Program.CommandHistoryList)
246 string text = command.Substring(1);
247 //if (item.Text.ToLower().StartsWith(this.textBox_cmd.Text.ToLower()) || Program.Ime.StartsWith(item.Regex, this.textBox_cmd.Text))
249 if (str.ToLower().StartsWith(text.ToLower()))
251 IconListBoxItem item = new IconListBoxItem();
254 item.Type = IconListBoxType.History;
255 item.IconImage = Properties.Resources.IconHistory.ToBitmap();
256 this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Add(item);
264 int lindex = command.LastIndexOf('\\') + 1;
267 string directories = command.Substring(0, lindex);
268 string files = command.Substring(lindex);
272 if (Directory.GetFileSystemEntries(directories, files + "*").Length > 0)
274 this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Clear();
275 if (this.searchTextBox.TextBox.Text.Equals(Directory.GetDirectoryRoot(command)))
277 DriveInfo driveInfo = new DriveInfo(this.searchTextBox.TextBox.Text[0].ToString());
278 if (!driveInfo.IsReady)
281 foreach (string directory in Directory.GetDirectories(directories, files + "*"))
283 IconListBoxItem item = new IconListBoxItem();
284 item.Name = directory.Substring(directories.Length) + "\\";
285 item.Text = directory + "\\";
286 item.Type = IconListBoxType.Directory;
287 item.IconImage = Program.DirectoryIcon.ToBitmap();
288 this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Add(item);
290 foreach (string file in Directory.GetFiles(directories, files + "*"))
294 IconListBoxItem item = new IconListBoxItem();
295 item.Name = file.Substring(directories.Length);
297 item.Type = IconListBoxType.Directory;
299 extindex = item.Name.LastIndexOf('.');
300 ext = extindex == -1 ? "" : item.Name.Substring(extindex + 1);
302 if ("exe".Equals(ext) || "lnk".Equals(ext))
304 Win32.SHFILEINFO shinfo = new Win32.SHFILEINFO();
305 Win32.SHGetFileInfo(file, 0, ref shinfo, Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_LARGEICON);
306 if (shinfo.hIcon != IntPtr.Zero)
307 item.IconImage = System.Drawing.Icon.FromHandle(shinfo.hIcon).ToBitmap();
311 if (Program.ExtIconCacheDic.ContainsKey(ext))
313 if (Program.ExtIconCacheDic[ext] != null)
314 item.IconImage = Program.ExtIconCacheDic[ext].ToBitmap();
318 Win32.SHFILEINFO shinfo = new Win32.SHFILEINFO();
319 Win32.SHGetFileInfo(file, 0, ref shinfo, Marshal.SizeOf(shinfo), Win32.SHGFI_ICON | Win32.SHGFI_LARGEICON);
320 if (shinfo.hIcon != IntPtr.Zero)
322 Icon icon = System.Drawing.Icon.FromHandle(shinfo.hIcon);
323 Program.ExtIconCacheDic.Add(ext, icon);
324 item.IconImage = icon.ToBitmap();
328 Program.ExtIconCacheDic.Add(ext, null);
332 this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Add(item);
336 catch (Exception ex) { MessageBox.Show("ex: " + ex.ToString()); }
340 foreach (IconListBoxItem item in this.searchTextBox.TextBox.Items)
342 if (item.Text.ToLower().StartsWith(this.searchTextBox.TextBox.Text.ToLower()) || Program.Ime.StartsWith(item.Regex, this.searchTextBox.TextBox.Text))
343 this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Add(item);
345 if (command.Length > 0)
347 foreach (string str in Program.CommandHistoryList)
349 if (str.StartsWith(command))
352 foreach (IconListBoxItem item in this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items)
354 if (str.Equals(item.Text))
363 IconListBoxItem i = new IconListBoxItem();
366 i.Type = IconListBoxType.History;
368 this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Add(i);
375 if (this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Items.Count == 0)
377 this.searchTextBox.TextBox.DropdownForm.DoVisibleChange(false);
380 this.searchTextBox.TextBox.SetDropdownFormLocation();
386 public void SetEngineDropdownList(string text)
388 if (this.suggestString.Equals(text))
391 EngineInfo info = Program.GetEngineInfo(this.SelectedEngineIndex);
396 string url = info.SuggestUrl.Replace("%s", Uri.EscapeDataString(text));
397 HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
399 req.UserAgent = Program.UserAgent;
401 AsyncCallback readCallBack = new AsyncCallback(SuggestReadCallBack);
402 req.BeginGetResponse(readCallBack, req);
403 this.suggestString = text;
407 private void SuggestReadCallBack(IAsyncResult ar)
411 HttpWebRequest req = (HttpWebRequest)ar.AsyncState;
412 HttpWebResponse response = (HttpWebResponse)req.EndGetResponse(ar);
413 StreamReader streamReader = new StreamReader(response.GetResponseStream(), Encoding.GetEncoding(response.CharacterSet));
414 string str = streamReader.ReadToEnd();
415 streamReader.Close();
416 Win32.PostMessage(Program.MainFormHandle, Program.WM_SUGGEST_END, IntPtr.Zero, Marshal.StringToHGlobalAuto(str));
420 Debug.WriteLine("error:" + ex);
421 string s = ex.ToString();
426 private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
428 if (Program.HotKeyEngineList.Contains(KeyboardHook.Keycode))
430 EngineInfo info = Program.GetEngineInfoFromHotKey(KeyboardHook.Keycode);
434 this.SetSearchEngine(info);
439 switch ((Keys)e.KeyChar)
443 if (this.SearchTextBox.TextBox.Complemented)
446 if (!this.SearchTextBox.TextBox.DropdownForm.Visible)
448 this.ShowDropdownForm(false);
454 if ((Control.ModifierKeys & Keys.Control) == Keys.Control)
456 switch (this.SelectedEngineType)
459 if (this.SearchTextBox.TextBox.ShowableDropdownList)
460 this.ShowDropdownForm(false);
464 this.suggestString = "";
465 this.textChanged = true;
473 string command = this.SearchTextBox.TextBox.Text.Trim();
474 switch (this.SelectedEngineType)
477 if (this.ExecCommand(command))
479 Program.AddHistoryList(command);
487 this.ExecSearch(command);
494 if (this.SearchTextBox.TextBox.DropdownForm.Visible)
496 this.SearchTextBox.TextBox.DropdownForm.DoVisibleChange(false);
497 Program.HideToolTip();
501 if (!"".Equals(searchTextBox.TextBox.Text))
503 this.SearchTextBox.TextBox.Text = "";
504 this.SearchTextBox.TextBox.SelectionStart = 0;
510 this.DoVisibleChange(false);
511 Program.ForceForegroundWindow(this.beforeHWnd);
518 private void textBox_cmd_KeyDown(object sender, KeyEventArgs e)
520 if (Program.MainForm.Opacity != Program.FormOpacity)
521 Program.MainForm.Opacity = Program.FormOpacity;
526 if ((Keys.Control & Control.ModifierKeys) == Keys.Control)
529 this.SelectedEngineIndex--;
530 if(this.SelectedEngineIndex == -1)
531 this.SelectedEngineIndex = Program.DefaultEngineList.Count + Program.EngineList.Count - 1;
532 this.SetSearchEngine(Program.GetEngineInfo(this.SelectedEngineIndex));
536 if (this.SelectedEngineType == 0)
538 if (!this.SearchTextBox.TextBox.DropdownForm.Visible)
540 if (Program.CommandHistoryList.Count > 0)
542 if (Program.CommandHistoryList.Count > Program.CommandHistoryIndex)
543 Program.CommandHistoryIndex++;
544 this.showDropdownSupport = false;
545 this.SearchTextBox.TextBox.Text = Program.CommandHistoryList[Program.CommandHistoryIndex - 1];
546 this.SearchTextBox.TextBox.SelectionStart = this.SearchTextBox.TextBox.Text.Length;
547 this.showDropdownSupport = true;
555 if ((Keys.Control & Control.ModifierKeys) == Keys.Control)
558 this.SelectedEngineIndex++;
559 int count = Program.DefaultEngineList.Count + Program.EngineList.Count;
560 if (this.SelectedEngineIndex == count)
561 this.SelectedEngineIndex = 0;
562 this.SetSearchEngine(Program.GetEngineInfo(this.SelectedEngineIndex));
566 switch(this.SelectedEngineType)
569 if (!this.SearchTextBox.TextBox.DropdownForm.Visible)
571 if (Program.CommandHistoryIndex > 0)
573 Program.CommandHistoryIndex--;
574 this.showDropdownSupport = false;
575 if (Program.CommandHistoryIndex == 0)
576 this.SearchTextBox.TextBox.Text = "";
578 this.SearchTextBox.TextBox.Text = Program.CommandHistoryList[Program.CommandHistoryIndex - 1];
579 this.SearchTextBox.TextBox.SelectionStart = this.SearchTextBox.TextBox.Text.Length;
580 this.showDropdownSupport = true;
584 this.ShowDropdownForm(false);
590 if (!this.SearchTextBox.TextBox.DropdownForm.Visible)
592 this.suggestString = "";
593 this.textChanged = true;
602 private void textBox_cmd_TextChanged(object sender, EventArgs e)
604 this.latestKeyTime = DateTime.Now.Ticks;
605 this.searchTextBox.TextBox.ImeText = this.searchTextBox.TextBox.Text;
606 if (!this.showDropdownSupport)
609 Size s = TextRenderer.MeasureText(this.searchTextBox.TextBox.Text, this.searchTextBox.TextBox.Font);
610 if (s.Width > this.searchTextBox.TextBox.Width)
612 //MessageBox.Show("!!!");
615 if(!this.searchTextBox.TextBox.Complemented)
616 this.ShowDropdownForm(true);
619 private void textBox_cmd_Complement(IconListBoxType type)
621 //this.ShowFileList();
624 private void textBox_cmd_ImeComposition(int type, string text)
626 if(!String.IsNullOrEmpty(this.searchTextBox.TextBox.Text))
628 string str = this.searchTextBox.TextBox.Text;
629 text = str.Substring(0, this.searchTextBox.TextBox.SelectionStart)
631 + str.Substring(this.searchTextBox.TextBox.SelectionStart);
633 this.searchTextBox.TextBox.ImeText = text;
635 if (type == 0 && String.IsNullOrEmpty(text))
637 this.searchTextBox.TextBox.DropdownForm.DoVisibleChange(false);
638 this.suggestString = "";
642 if ((type & Win32.GCS_RESULTSTR) != Win32.GCS_RESULTSTR)
644 this.latestKeyTime = DateTime.Now.Ticks;
645 this.textChanged = true;
650 private void dropdownListBox_SelectedIndexChanged(object sender, EventArgs e)
652 Program.HideToolTip();
653 if (!this.searchTextBox.TextBox.DropdownForm.Visible)
656 IconListBox listBox = this.searchTextBox.TextBox.DropdownForm.DropdownListBox;
657 if (listBox.SelectedIndex == -1)
660 string text = listBox.Text;
661 if (text.Equals(this.dropdownListBoxText))
664 if (!(text.StartsWith(":") || Program.CommandDic.ContainsKey(text)))
667 int lastIndex = listBox.ClientRectangle.Height / listBox.ItemHeight;
668 if (listBox.TopIndex > listBox.SelectedIndex || (listBox.TopIndex + lastIndex) <= listBox.SelectedIndex)
671 IconListBoxItem item = (IconListBoxItem)listBox.SelectedItem;
672 DropdownListForm dropdownForm = this.searchTextBox.TextBox.DropdownForm;
674 Rectangle r = dropdownForm.Bounds;
675 r.Y = dropdownForm.Location.Y + listBox.GetItemRectangle(listBox.SelectedIndex).Y;
676 if (item.ToolTipText == null || item.ToolTipText.Length == 0)
678 CommandInfo info = Program.CommandDic[text];
679 text = info.Comment == null ? info.Name : info.Comment;
683 text = item.ToolTipText;
685 Program.ShowToolTip(Util.CutText(text, 50, 10), r, 5000, false);
688 private void timer1_Tick(object sender, System.EventArgs e)
692 if (this.Opacity + Program.FadeInSpeed < Program.FormOpacity)
694 this.Opacity += Program.FadeInSpeed;
695 this.searchTextBox.TextBox.DropdownForm.Opacity = this.Opacity;
700 this.Opacity = Program.FormOpacity;
701 this.searchTextBox.TextBox.DropdownForm.Opacity = this.Opacity;
707 private void timer_keyRecoding_Tick(object sender, EventArgs e)
709 if (Program.Status == ProgramStatus.KeyRecoding && Program.Main_ShowBalloonWhileRecoding && Program.BallonWindowHandle != IntPtr.Zero)
710 Win32.PostMessage(Program.BallonWindowHandle, Win32.WM_MOUSEMOVE, (IntPtr)0, (IntPtr)0);
713 private void timer_suggest_Tick(object sender, EventArgs e)
715 if (this.SelectedEngineType == 2 && this.textChanged)
717 string text = this.searchTextBox.TextBox.ImeText;
718 if (String.IsNullOrEmpty(text))
721 if (DateTime.Now.Ticks > this.latestKeyTime + 000000 && !this.suggestString.Equals(text))
723 this.textChanged = false;
724 this.SetEngineDropdownList(text);
729 private void toolTip_Popup(object sender, PopupEventArgs e)
734 private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
736 if (e.Button == MouseButtons.Left)
738 switch (Program.Status)
740 case ProgramStatus.Loading:
741 case ProgramStatus.Waiting:
742 case ProgramStatus.MainFormOpen:
743 HotKeyProcessExecuter.OpenMainForm();
749 private void DropdownListView_MouseDoubleClick(object sender, MouseEventArgs e)
751 this.searchTextBox.TextBox.Text = this.searchTextBox.TextBox.DropdownForm.DropdownListBox.Text;
752 this.searchTextBox.TextBox.Select(this.searchTextBox.TextBox.Text.Length, 0);
753 this.searchTextBox.TextBox.DropdownForm.DoVisibleChange(false);
756 private void 表示ToolStripMenuItem_Click(object sender, EventArgs e)
758 switch (Program.Status)
760 case ProgramStatus.Loading:
761 case ProgramStatus.Waiting:
762 case ProgramStatus.MainFormOpen:
763 HotKeyProcessExecuter.OpenMainForm();
768 private void リスト設定ToolStripMenuItem_Click(object sender, EventArgs e)
770 if (!Program.SettingForm.Visible)
772 this.DoVisibleChange(false);
773 Program.SettingForm.DoVisibleChange(true);
777 private void バージョン情報ToolStripMenuItem_Click(object sender, EventArgs e)
779 if (Program.Status == ProgramStatus.Waiting)
781 Program.Status = ProgramStatus.VersionFormOpen;
782 VersionForm f = new VersionForm();
784 Program.Status = ProgramStatus.Waiting;
788 private void 終了ToolStripMenuItem_Click(object sender, EventArgs e)