PageRenderTime 53ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/GitUI/CommandsDialogs/SettingsDialog/Pages/ScriptsSettingsPage.cs

https://github.com/qgppl/gitextensions
C# | 340 lines | 308 code | 28 blank | 4 comment | 14 complexity | b864b2ae6829d266f39855dd13ec3f3a MD5 | raw file
Possible License(s): GPL-3.0
  1. using System;
  2. using System.Drawing;
  3. using System.Linq;
  4. using System.Windows.Forms;
  5. using GitCommands;
  6. using GitCommands.Utils;
  7. using GitUI.Script;
  8. using ResourceManager;
  9. namespace GitUI.CommandsDialogs.SettingsDialog.Pages
  10. {
  11. public partial class ScriptsSettingsPage : SettingsPageWithHeader
  12. {
  13. #region translation
  14. private readonly TranslationString _scriptSettingsPageHelpDisplayArgumentsHelp = new TranslationString("Arguments help");
  15. private readonly TranslationString _scriptSettingsPageHelpDisplayContent = new TranslationString(@"User Input:
  16. {UserInput}
  17. Working Dir:
  18. {WorkingDir}
  19. Selected Commits:
  20. {sHashes}
  21. Selected Branch:
  22. {sTag}
  23. {sBranch}
  24. {sLocalBranch}
  25. {sRemoteBranch}
  26. {sRemote}
  27. {sRemoteUrl}
  28. {sRemotePathFromUrl}
  29. {sHash}
  30. {sMessage}
  31. {sAuthor}
  32. {sCommitter}
  33. {sAuthorDate}
  34. {sCommitDate}
  35. Current Branch:
  36. {cTag}
  37. {cBranch}
  38. {cLocalBranch}
  39. {cRemoteBranch}
  40. {cHash}
  41. {cMessage}
  42. {cAuthor}
  43. {cCommitter}
  44. {cAuthorDate}
  45. {cCommitDate}
  46. {cDefaultRemote}
  47. {cDefaultRemoteUrl}
  48. {cDefaultRemotePathFromUrl}");
  49. #endregion translation
  50. private string IconName = "bug";
  51. public ScriptsSettingsPage()
  52. {
  53. InitializeComponent();
  54. Text = "Scripts";
  55. Translate();
  56. }
  57. public override bool IsInstantSavePage
  58. {
  59. get
  60. {
  61. return true;
  62. }
  63. }
  64. public override void OnPageShown()
  65. {
  66. if (EnvUtils.RunningOnWindows())
  67. {
  68. System.Resources.ResourceManager rm =
  69. new System.Resources.ResourceManager("GitUI.Properties.Resources",
  70. System.Reflection.Assembly.GetExecutingAssembly());
  71. // dummy request; for some strange reason the ResourceSets are not loaded untill after the first object request... bug?
  72. rm.GetObject("dummy");
  73. System.Resources.ResourceSet resourceSet = rm.GetResourceSet(System.Globalization.CultureInfo.CurrentUICulture, true, true);
  74. contextMenuStrip_SplitButton.Items.Clear();
  75. foreach (System.Collections.DictionaryEntry icon in resourceSet)
  76. {
  77. //add entry to toolstrip
  78. if (icon.Value.GetType() == typeof(Icon))
  79. {
  80. //contextMenuStrip_SplitButton.Items.Add(icon.Key.ToString(), (Image)((Icon)icon.Value).ToBitmap(), SplitButtonMenuItem_Click);
  81. }
  82. else if (icon.Value.GetType() == typeof(Bitmap))
  83. {
  84. contextMenuStrip_SplitButton.Items.Add(icon.Key.ToString(), (Image)icon.Value, SplitButtonMenuItem_Click);
  85. }
  86. //var aa = icon.Value.GetType();
  87. }
  88. resourceSet.Close();
  89. rm.ReleaseAllResources();
  90. }
  91. }
  92. protected override void SettingsToPage()
  93. {
  94. scriptEvent.DataSource = Enum.GetValues(typeof(ScriptEvent));
  95. LoadScripts();
  96. }
  97. protected override void PageToSettings()
  98. {
  99. SaveScripts();
  100. }
  101. private void SaveScripts()
  102. {
  103. AppSettings.ownScripts = ScriptManager.SerializeIntoXml();
  104. }
  105. private void LoadScripts()
  106. {
  107. ScriptList.DataSource = ScriptManager.GetScripts();
  108. }
  109. private void ClearScriptDetails()
  110. {
  111. nameTextBox.Clear();
  112. commandTextBox.Clear();
  113. argumentsTextBox.Clear();
  114. inMenuCheckBox.Checked = false;
  115. }
  116. private void RefreshScriptDetails()
  117. {
  118. if (ScriptList.SelectedRows.Count == 0)
  119. return;
  120. ScriptInfo scriptInfo = ScriptList.SelectedRows[0].DataBoundItem as ScriptInfo;
  121. nameTextBox.Text = scriptInfo.Name;
  122. commandTextBox.Text = scriptInfo.Command;
  123. argumentsTextBox.Text = scriptInfo.Arguments;
  124. scriptRunInBackground.Checked = scriptInfo.RunInBackground;
  125. inMenuCheckBox.Checked = scriptInfo.AddToRevisionGridContextMenu;
  126. scriptEnabled.Checked = scriptInfo.Enabled;
  127. scriptNeedsConfirmation.Checked = scriptInfo.AskConfirmation;
  128. scriptEvent.SelectedItem = scriptInfo.OnEvent;
  129. sbtn_icon.Image = ResizeForSplitButton(scriptInfo.GetIcon());
  130. IconName = scriptInfo.Icon;
  131. foreach (ToolStripItem item in contextMenuStrip_SplitButton.Items)
  132. {
  133. if (item.ToString() == IconName)
  134. {
  135. item.Font = new Font(item.Font, FontStyle.Bold);
  136. }
  137. }
  138. }
  139. private void addScriptButton_Click(object sender, EventArgs e)
  140. {
  141. ScriptList.ClearSelection();
  142. ScriptManager.GetScripts().AddNew();
  143. ScriptList.Rows[ScriptList.RowCount - 1].Selected = true;
  144. ScriptList_SelectionChanged(null, null); //needed for linux
  145. }
  146. private void removeScriptButton_Click(object sender, EventArgs e)
  147. {
  148. if (ScriptList.SelectedRows.Count > 0)
  149. {
  150. ScriptManager.GetScripts().Remove(ScriptList.SelectedRows[0].DataBoundItem as ScriptInfo);
  151. ClearScriptDetails();
  152. }
  153. }
  154. private void ScriptInfoFromEdits()
  155. {
  156. if (ScriptList.SelectedRows.Count > 0)
  157. {
  158. ScriptInfo selectedScriptInfo = ScriptList.SelectedRows[0].DataBoundItem as ScriptInfo;
  159. selectedScriptInfo.HotkeyCommandIdentifier = ScriptList.SelectedRows[0].Index + 9000;
  160. selectedScriptInfo.Name = nameTextBox.Text;
  161. selectedScriptInfo.Command = commandTextBox.Text;
  162. selectedScriptInfo.Arguments = argumentsTextBox.Text;
  163. selectedScriptInfo.AddToRevisionGridContextMenu = inMenuCheckBox.Checked;
  164. selectedScriptInfo.Enabled = scriptEnabled.Checked;
  165. selectedScriptInfo.RunInBackground = scriptRunInBackground.Checked;
  166. selectedScriptInfo.AskConfirmation = scriptNeedsConfirmation.Checked;
  167. selectedScriptInfo.OnEvent = (ScriptEvent)scriptEvent.SelectedItem;
  168. selectedScriptInfo.Icon = IconName;
  169. }
  170. }
  171. private void moveUpButton_Click(object sender, EventArgs e)
  172. {
  173. if (ScriptList.SelectedRows.Count > 0)
  174. {
  175. ScriptInfo scriptInfo = ScriptList.SelectedRows[0].DataBoundItem as ScriptInfo;
  176. int index = ScriptManager.GetScripts().IndexOf(scriptInfo);
  177. ScriptManager.GetScripts().Remove(scriptInfo);
  178. ScriptManager.GetScripts().Insert(Math.Max(index - 1, 0), scriptInfo);
  179. ScriptList.ClearSelection();
  180. ScriptList.Rows[Math.Max(index - 1, 0)].Selected = true;
  181. ScriptList.Focus();
  182. }
  183. }
  184. private void moveDownButton_Click(object sender, EventArgs e)
  185. {
  186. if (ScriptList.SelectedRows.Count > 0)
  187. {
  188. ScriptInfo scriptInfo = ScriptList.SelectedRows[0].DataBoundItem as ScriptInfo;
  189. int index = ScriptManager.GetScripts().IndexOf(scriptInfo);
  190. ScriptManager.GetScripts().Remove(scriptInfo);
  191. ScriptManager.GetScripts().Insert(Math.Min(index + 1, ScriptManager.GetScripts().Count), scriptInfo);
  192. ScriptList.ClearSelection();
  193. ScriptList.Rows[Math.Max(index + 1, 0)].Selected = true;
  194. ScriptList.Focus();
  195. }
  196. }
  197. private void browseScriptButton_Click(object sender, EventArgs e)
  198. {
  199. using (var ofd = new OpenFileDialog
  200. {
  201. InitialDirectory = "c:\\",
  202. Filter = "Executable files (*.exe)|*.exe|All files (*.*)|*.*",
  203. RestoreDirectory = true
  204. })
  205. {
  206. if (ofd.ShowDialog(this) == DialogResult.OK)
  207. commandTextBox.Text = ofd.FileName;
  208. }
  209. }
  210. private void ScriptList_SelectionChanged(object sender, EventArgs e)
  211. {
  212. if (ScriptList.SelectedRows.Count > 0)
  213. {
  214. RefreshScriptDetails();
  215. removeScriptButton.Enabled = true;
  216. moveDownButton.Enabled = moveUpButton.Enabled = false;
  217. if (ScriptList.SelectedRows[0].Index > 0)
  218. moveUpButton.Enabled = true;
  219. if (ScriptList.SelectedRows[0].Index < ScriptList.RowCount - 1)
  220. moveDownButton.Enabled = true;
  221. }
  222. else
  223. {
  224. removeScriptButton.Enabled = false;
  225. moveUpButton.Enabled = false;
  226. moveDownButton.Enabled = false;
  227. ClearScriptDetails();
  228. }
  229. }
  230. private void ScriptInfoEdit_Validating(object sender, System.ComponentModel.CancelEventArgs e)
  231. {
  232. ScriptInfoFromEdits();
  233. ScriptList.Refresh();
  234. }
  235. private void ScriptList_CellClick(object sender, DataGridViewCellEventArgs e)
  236. {
  237. ScriptList_SelectionChanged(null, null);//needed for linux
  238. }
  239. private void SplitButtonMenuItem_Click(object sender, EventArgs e)
  240. {
  241. //reset bold item to regular
  242. var item = contextMenuStrip_SplitButton.Items.OfType<ToolStripMenuItem>().FirstOrDefault(s => s.Font.Bold);
  243. if (item != null)
  244. item.Font = new Font(contextMenuStrip_SplitButton.Font, FontStyle.Regular);
  245. //make new item bold
  246. ((ToolStripMenuItem)sender).Font = new Font(((ToolStripMenuItem)sender).Font, FontStyle.Bold);
  247. //set new image on button
  248. sbtn_icon.Image = ResizeForSplitButton((Bitmap)((ToolStripMenuItem)sender).Image);
  249. IconName = ((ToolStripMenuItem)sender).Text;
  250. //store variables
  251. ScriptInfoEdit_Validating(sender, new System.ComponentModel.CancelEventArgs());
  252. }
  253. private Bitmap ResizeForSplitButton(Bitmap b)
  254. {
  255. return ResizeBitmap(b, 12, 12);
  256. }
  257. public Bitmap ResizeBitmap(Bitmap b, int nWidth, int nHeight)
  258. {
  259. Bitmap result = new Bitmap(nWidth, nHeight);
  260. using (Graphics g = Graphics.FromImage((Image)result))
  261. g.DrawImage(b, 0, 0, nWidth, nHeight);
  262. return result;
  263. }
  264. private void scriptEvent_SelectedIndexChanged(object sender, EventArgs e)
  265. {
  266. if (scriptEvent.Text == ScriptEvent.ShowInUserMenuBar.ToString())
  267. {
  268. /*
  269. string icon_name = IconName;
  270. if (ScriptList.RowCount > 0)
  271. {
  272. ScriptInfo scriptInfo = ScriptList.SelectedRows[0].DataBoundItem as ScriptInfo;
  273. icon_name = scriptInfo.Icon;
  274. }*/
  275. sbtn_icon.Visible = true;
  276. lbl_icon.Visible = true;
  277. }
  278. else
  279. {
  280. //not a menubar item, so hide the text label and dropdown button
  281. sbtn_icon.Visible = false;
  282. lbl_icon.Visible = false;
  283. }
  284. }
  285. private void buttonShowArgumentsHelp_Click(object sender, EventArgs e)
  286. {
  287. var helpDisplayDialog = new SimpleHelpDisplayDialog();
  288. helpDisplayDialog.DialogTitle = _scriptSettingsPageHelpDisplayArgumentsHelp.Text;
  289. helpDisplayDialog.ContentText = @_scriptSettingsPageHelpDisplayContent.Text.Replace("\n", Environment.NewLine);
  290. helpDisplayDialog.ShowDialog();
  291. }
  292. }
  293. }