PageRenderTime 47ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/CommandLauncher/Form1.cs

https://github.com/tosik/TeaLauncher
C# | 322 lines | 247 code | 43 blank | 32 comment | 13 complexity | a8293e75803def2b809ea10de004a9c2 MD5 | raw file
  1. /*
  2. * TeaLauncher. Simple command launcher.
  3. * Copyright (C) Toshiyuki Hirooka <toshi.hirooka@gmail.com> http://wasabi.in/
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. using System;
  20. using System.Collections;
  21. using System.Collections.Generic;
  22. using System.ComponentModel;
  23. using System.Data;
  24. using System.Drawing;
  25. using System.Linq;
  26. using System.Text;
  27. using System.Windows.Forms;
  28. using System.Diagnostics;
  29. namespace CommandLauncher
  30. {
  31. public partial class MainWindow
  32. : Form
  33. , ICommandManagerInitializer
  34. , ICommandManagerFinalizer
  35. , ICommandManagerDialogShower
  36. {
  37. // コマンド管理
  38. CommandManager m_CommandManager;
  39. // 読み込んだコンフィグのファイル名
  40. string m_ConfigFileName;
  41. // ホットキー
  42. HotKey m_Hotkey;
  43. string LICENSE = @"TeaLauncher. Simple command launcher.
  44. Copyright (C) Toshiyuki Hirooka <toshi.hirooka@gmail.com> http://wasabi.in/
  45. This program is free software; you can redistribute it and/or
  46. modify it under the terms of the GNU General Public License
  47. as published by the Free Software Foundation; either version 2
  48. of the License, or (at your option) any later version.
  49. This program is distributed in the hope that it will be useful,
  50. but WITHOUT ANY WARRANTY; without even the implied warranty of
  51. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  52. GNU General Public License for more details.
  53. You should have received a copy of the GNU General Public License
  54. along with this program; if not, write to the Free Software
  55. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.";
  56. public MainWindow(string conf_filename)
  57. {
  58. m_CommandManager = new CommandManager(this, this, this);
  59. InitializeComponent();
  60. try
  61. {
  62. // 設定を読み込んで反映する
  63. InitializeByConfigFile(conf_filename);
  64. }
  65. catch (MainWindowNotExistsLinkToException e)
  66. {
  67. MessageBox.Show(e.Message + "\n" + "filename : "+ conf_filename, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  68. }
  69. catch (Exception e)
  70. {
  71. MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  72. }
  73. #if DEBUG
  74. m_Hotkey = new HotKey(MOD_KEY.ALT, Keys.Space);
  75. #else
  76. m_Hotkey = new HotKey(MOD_KEY.CONTROL, Keys.Space);
  77. #endif
  78. m_Hotkey.HotKeyPressed += new EventHandler(PressHotkey);
  79. }
  80. void InitializeByConfigFile(string filename)
  81. {
  82. m_ConfigFileName = filename;
  83. m_CommandManager.ClearCommands();
  84. ConfigLoader conf = new ConfigLoader();
  85. conf.LoadConfigFile(filename);
  86. List<string> sections = conf.GetSections();
  87. foreach (string section in sections)
  88. {
  89. Hashtable hashtable = conf.GetConfig(section);
  90. Command cmd = new Command();
  91. cmd.command = section;
  92. cmd.execution = (string)hashtable["linkto"];
  93. if (cmd.execution == null)
  94. throw new MainWindowNotExistsLinkToException();
  95. m_CommandManager.RegisterCommand(cmd);
  96. }
  97. }
  98. public void Reinitialize()
  99. {
  100. InitializeByConfigFile(m_ConfigFileName);
  101. }
  102. public void Exit()
  103. {
  104. Application.Exit();
  105. m_Hotkey.Dispose();
  106. }
  107. public void ShowVersionInfo()
  108. {
  109. System.Diagnostics.FileVersionInfo version =
  110. System.Diagnostics.FileVersionInfo.GetVersionInfo(
  111. System.Reflection.Assembly.GetExecutingAssembly().Location);
  112. MessageBox.Show(version.FileDescription + " version " + version.FileVersion + "\n" +
  113. "--------------------------------" + "\n" +
  114. LICENSE, version.FileDescription);
  115. }
  116. public void ShowError(string message)
  117. {
  118. MessageBox.Show(message);
  119. }
  120. void PressHotkey(object sender, EventArgs e)
  121. {
  122. // 非表示なら表示してアクティブにする。アクティブなら非アクティブにして非表示にする。
  123. if (IsWindowShown())
  124. {
  125. if (Form.ActiveForm != this)
  126. {
  127. this.Activate();
  128. }
  129. else
  130. {
  131. HideWindow();
  132. }
  133. }
  134. else
  135. {
  136. ShowWindow();
  137. this.Activate();
  138. }
  139. }
  140. bool IsWindowShown()
  141. {
  142. return this.Visible;
  143. }
  144. private void ShowWindow()
  145. {
  146. this.Show();
  147. IMEController ime = new IMEController();
  148. ime.Off();
  149. ime.On();
  150. ime.Off();
  151. }
  152. private void HideWindow()
  153. {
  154. this.Hide();
  155. ClearCommandBox();
  156. }
  157. private void command_box_PreviewKeyDown_1(object sender, PreviewKeyDownEventArgs e)
  158. {
  159. switch (e.KeyCode)
  160. {
  161. case Keys.Escape:
  162. case Keys.Tab:
  163. e.IsInputKey = true;
  164. break;
  165. }
  166. }
  167. private void command_box_KeyDown(object sender, KeyEventArgs e)
  168. {
  169. switch (e.KeyCode)
  170. {
  171. case Keys.Tab:
  172. {
  173. e.Handled = true;
  174. // 補完する
  175. CompleteCommandBox();
  176. }
  177. break;
  178. case Keys.Escape:
  179. {
  180. e.Handled = true;
  181. if (command_box.Text != "")
  182. {
  183. ClearCommandBox();
  184. }
  185. else
  186. {
  187. HideWindow();
  188. }
  189. }
  190. break;
  191. case Keys.Enter:
  192. {
  193. e.Handled = true;
  194. RunCommand(command_box.Text);
  195. }
  196. break;
  197. }
  198. }
  199. private void CompleteCommandBox()
  200. {
  201. // 補完候補など取得
  202. string input_str = this.command_box.Text;
  203. List<string> candidates = m_CommandManager.GetCandidates(input_str);
  204. string compl_str = m_CommandManager.AutoCompleteWord(input_str);
  205. // 候補があればコマンドボックスを補完する
  206. if (candidates.Count != 0)
  207. {
  208. // 候補が2つ以上あるなら
  209. if (candidates.Count >= 2)
  210. {
  211. // コンボボックスに候補を表示する
  212. command_box.Items.Clear();
  213. command_box.DroppedDown = true;
  214. foreach (string str in candidates)
  215. {
  216. command_box.Items.Add(str);
  217. }
  218. }
  219. else
  220. {
  221. ClearDropBox();
  222. }
  223. // 補完文字列を表示
  224. command_box.Text = compl_str;
  225. command_box.SelectionStart = command_box.Text.Length;
  226. }
  227. else
  228. {
  229. ClearDropBox();
  230. }
  231. }
  232. private void ClearCommandBox()
  233. {
  234. // クリアする
  235. ClearDropBox();
  236. command_box.Text = "";
  237. }
  238. private void ClearDropBox()
  239. {
  240. // クリアする
  241. command_box.DroppedDown = false;
  242. command_box.Items.Clear();
  243. command_box.SelectionStart = command_box.Text.Length; // Items.Clear するとおかしくなるから
  244. }
  245. private void RunCommand(string command)
  246. {
  247. if (m_CommandManager.HasCommand(command))
  248. {
  249. Debug.WriteLine("Run : " + command);
  250. ClearCommandBox();
  251. HideWindow();
  252. try
  253. {
  254. m_CommandManager.Run(command);
  255. }
  256. catch (Exception e)
  257. {
  258. MessageBox.Show(e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  259. }
  260. }
  261. }
  262. private void MainWindow_Shown(object sender, EventArgs e)
  263. {
  264. // 起動時は非表示
  265. HideWindow();
  266. }
  267. }
  268. class MainWindowNotExistsLinkToException
  269. : Exception
  270. {
  271. public override string Message
  272. {
  273. get { return "設定ファイルに \"linkto\" キーが存在しません。"; }
  274. }
  275. }
  276. }