MacroManager.pas 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. unit MacroManager;
  2. interface
  3. uses
  4. Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5. Dialogs, StdCtrls, Buttons, Mask, JvExMask, JvToolEdit, JvDotNetControls,
  6. Registry, VirtualTrees;
  7. type
  8. PMacroData = ^TMacroData;
  9. TMacroData = record
  10. Name: String;
  11. FileName: String;
  12. Caption: String;
  13. Shortcut: String;
  14. DebugMode: Boolean;
  15. IsNew: Boolean;
  16. end;
  17. TfrmMacroManager = class(TForm)
  18. GroupBox1: TGroupBox;
  19. Button2: TButton;
  20. Label1: TLabel;
  21. Label2: TLabel;
  22. Label3: TLabel;
  23. cboShortcut: TComboBox;
  24. sbtnAdd: TSpeedButton;
  25. sbtnDelete: TSpeedButton;
  26. sbtnSave: TSpeedButton;
  27. chkDebug: TCheckBox;
  28. txtCaption: TEdit;
  29. txtFileName: TEdit;
  30. btnBrowseFile: TButton;
  31. odlgSelectFile: TOpenDialog;
  32. vstMacros: TVirtualStringTree;
  33. Label4: TLabel;
  34. txtName: TEdit;
  35. procedure btnBrowseFileClick(Sender: TObject);
  36. procedure vstMacrosGetText(Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType; var CellText: WideString);
  37. procedure vstMacrosGetNodeDataSize(Sender: TBaseVirtualTree; var NodeDataSize: Integer);
  38. procedure FormShow(Sender: TObject);
  39. procedure sbtnDeleteClick(Sender: TObject);
  40. procedure sbtnSaveClick(Sender: TObject);
  41. procedure sbtnAddClick(Sender: TObject);
  42. procedure vstMacrosFocusChanging(Sender: TBaseVirtualTree; OldNode, NewNode: PVirtualNode; OldColumn, NewColumn: TColumnIndex; var Allowed: Boolean);
  43. procedure vstMacrosFocusChanged(Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex);
  44. procedure txtNameChange(Sender: TObject);
  45. procedure txtFileNameChange(Sender: TObject);
  46. procedure txtCaptionChange(Sender: TObject);
  47. procedure cboShortcutClick(Sender: TObject);
  48. procedure chkDebugClick(Sender: TObject);
  49. private
  50. { Private declarations }
  51. NeedSave: Boolean;
  52. public
  53. { Public declarations }
  54. procedure FillMacroList();
  55. function SaveMacro(OldName: String): Boolean;
  56. end;
  57. var
  58. frmMacroManager: TfrmMacroManager;
  59. implementation
  60. uses Main, Misc;
  61. {$R *.dfm}
  62. procedure TfrmMacroManager.FormShow(Sender: TObject);
  63. var
  64. pNode: PVirtualNode;
  65. begin
  66. // Initialize dialog
  67. FillMacroList();
  68. txtCaption.Text := '';
  69. txtName.Text := '';
  70. txtFileName.Text := '';
  71. cboShortcut.ItemIndex := 0;
  72. chkDebug.Checked := False;
  73. // Attempt to select first node in the tree (if any)
  74. pNode := vstMacros.GetFirstChild(vstMacros.RootNode);
  75. if Assigned(pNode) then
  76. begin
  77. vstMacros.Selected[pNode] := True;
  78. vstMacrosFocusChanged(vstMacros, pNode, 0);
  79. end;
  80. end;
  81. procedure TfrmMacroManager.btnBrowseFileClick(Sender: TObject);
  82. begin
  83. odlgSelectFile.InitialDir := GetLuaEditInstallPath();
  84. if odlgSelectFile.Execute then
  85. txtFileName.Text := odlgSelectFile.FileName;
  86. end;
  87. procedure TfrmMacroManager.FillMacroList();
  88. var
  89. x: Integer;
  90. pReg: TRegistry;
  91. lstKeyList: TStringList;
  92. pNode: PVirtualNode;
  93. pNodeData: PMacroData;
  94. begin
  95. vstMacros.Clear;
  96. vstMacros.BeginUpdate;
  97. pReg := TRegistry.Create();
  98. // Open registry key to read all macros' datas
  99. if pReg.OpenKey('\Software\LuaEdit\Macros', False) then
  100. begin
  101. lstKeyList := TStringList.Create();
  102. pReg.GetKeyNames(lstKeyList);
  103. for x := 0 to lstKeyList.Count - 1 do
  104. begin
  105. // Open current macro registry key to read macro's values
  106. if pReg.OpenKey('\Software\LuaEdit\Macros\' + lstKeyList.Strings[x], False) then
  107. begin
  108. // Add node in virtual tree
  109. pNode := vstMacros.AddChild(vstMacros.RootNode);
  110. pNodeData := vstMacros.GetNodeData(pNode);
  111. // Fill data record with registry values
  112. pNodeData.Name := lstKeyList.Strings[x];
  113. pNodeData.FileName := pReg.ReadString('FileName');
  114. pNodeData.Caption := pReg.ReadString('Caption');
  115. pNodeData.Shortcut := pReg.ReadString('Shortcut');
  116. pNodeData.DebugMode := pReg.ReadBool('DebugMode');
  117. pNodeData.IsNew := False;
  118. end;
  119. end;
  120. lstKeyList.Free;
  121. end;
  122. vstMacros.EndUpdate;
  123. pReg.Free;
  124. // Attempt to select first node in the tree (if any)
  125. pNode := vstMacros.GetFirstChild(vstMacros.RootNode);
  126. if Assigned(pNode) then
  127. begin
  128. vstMacros.Selected[pNode] := True;
  129. vstMacrosFocusChanged(vstMacros, pNode, 0);
  130. end;
  131. end;
  132. function TfrmMacroManager.SaveMacro(OldName: String): Boolean;
  133. var
  134. pReg: TRegistry;
  135. begin
  136. Result := False;
  137. pReg := TRegistry.Create();
  138. if txtName.Text = '' then
  139. begin
  140. Application.MessageBox('The marco''s name cannot be empty!', 'LuaEdit', MB_OK+MB_ICONERROR);
  141. txtName.SetFocus;
  142. end
  143. else if pReg.KeyExists('\Software\LuaEdit\Macros\'+txtName.Text) and (OldName <> txtName.Text) and (OldName <> '') then
  144. begin
  145. Application.MessageBox(PChar('A macro with name "'+txtName.Text+'" already exists!'), 'LuaEdit', MB_OK+MB_ICONERROR);
  146. txtName.SetFocus;
  147. end
  148. else if not FileExistsAbs(txtFileName.Text) then
  149. begin
  150. Application.MessageBox(PChar('The file "'+txtFileName.Text+'" does not exists!'), 'LuaEdit', MB_OK+MB_ICONERROR);
  151. txtFileName.SetFocus;
  152. end
  153. else if txtCaption.Text = '' then
  154. begin
  155. Application.MessageBox('The macro''s caption cannot be empty!', 'LuaEdit', MB_OK+MB_ICONERROR);
  156. txtCaption.SetFocus;
  157. end
  158. else
  159. begin
  160. // Open/create the registry key to write datas
  161. if pReg.OpenKey('\Software\LuaEdit\Macros\'+txtName.Text, True) then
  162. begin
  163. // Write macro's datas
  164. pReg.WriteString('FileName', txtFileName.Text);
  165. pReg.WriteString('Caption', txtCaption.Text);
  166. pReg.WriteString('Shortcut', cboShortcut.Text);
  167. pReg.WriteBool('DebugMode', chkDebug.Checked);
  168. end;
  169. // Delete previous registry key (the one with the old name... the name might have changed)
  170. if (OldName <> '') and (txtName.Text <> OldName) then
  171. begin
  172. if pReg.KeyExists('\Software\LuaEdit\Macros\'+OldName) then
  173. pReg.DeleteKey('\Software\LuaEdit\Macros\'+OldName);
  174. end;
  175. Result := True;
  176. NeedSave := False;
  177. end;
  178. pReg.Free;
  179. end;
  180. procedure TfrmMacroManager.vstMacrosGetText(Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex; TextType: TVSTTextType; var CellText: WideString);
  181. var
  182. pData: PMacroData;
  183. begin
  184. // Set text to display for all nodes
  185. if TextType = ttNormal then
  186. begin
  187. case Column of
  188. 0:
  189. begin
  190. pData := Sender.GetNodeData(Node);
  191. CellText := pData.Name;
  192. end;
  193. 1:
  194. begin
  195. pData := Sender.GetNodeData(Node);
  196. CellText := pData.FileName;
  197. end;
  198. 2:
  199. begin
  200. pData := Sender.GetNodeData(Node);
  201. CellText := pData.Caption;
  202. end;
  203. 3:
  204. begin
  205. pData := Sender.GetNodeData(Node);
  206. CellText := pData.Shortcut;
  207. end;
  208. 4:
  209. begin
  210. pData := Sender.GetNodeData(Node);
  211. if pData.DebugMode then
  212. CellText := 'Debug'
  213. else
  214. CellText := 'Release';
  215. end;
  216. end;
  217. end;
  218. end;
  219. procedure TfrmMacroManager.vstMacrosGetNodeDataSize(Sender: TBaseVirtualTree; var NodeDataSize: Integer);
  220. begin
  221. NodeDataSize := SizeOf(TMacroData);
  222. end;
  223. procedure TfrmMacroManager.vstMacrosFocusChanging(Sender: TBaseVirtualTree; OldNode, NewNode: PVirtualNode; OldColumn, NewColumn: TColumnIndex; var Allowed: Boolean);
  224. var
  225. pNodeData: PMacroData;
  226. IsNew: Boolean;
  227. begin
  228. IsNew := False;
  229. if OldNode <> NewNode then
  230. begin
  231. if Assigned(OldNode) then
  232. begin
  233. pNodeData := vstMacros.GetNodeData(OldNode);
  234. IsNew := pNodeData.IsNew;
  235. end;
  236. // Make sure saving is necessary
  237. if NeedSave or IsNew then
  238. begin
  239. // Prompt user to save changes
  240. if Application.MessageBox(PChar('Save changes to the "'+txtName.Text+'" macro?'), 'LuaEdit', MB_YESNO+MB_ICONQUESTION) = IDYES then
  241. begin
  242. // Save macro
  243. if Assigned(OldNode) and not IsNew then
  244. Allowed := SaveMacro(pNodeData.Name)
  245. else
  246. Allowed := SaveMacro('');
  247. // Rebuild the list of macros...
  248. FillMacroList();
  249. end;
  250. end;
  251. end
  252. else if OldNode = NewNode then
  253. Allowed := False;
  254. end;
  255. procedure TfrmMacroManager.vstMacrosFocusChanged(Sender: TBaseVirtualTree; Node: PVirtualNode; Column: TColumnIndex);
  256. var
  257. pNode: PVirtualNode;
  258. pNodeData: PMacroData;
  259. begin
  260. pNode := vstMacros.GetFirstSelected();
  261. if Assigned(pNode) then
  262. begin
  263. pNodeData := vstMacros.GetNodeData(pNode);
  264. txtName.Text := pNodeData.Name;
  265. txtFileName.Text := pNodeData.FileName;
  266. txtCaption.Text := pNodeData.Caption;
  267. cboShortcut.ItemIndex := cboShortcut.Items.IndexOf(pNodeData.Shortcut);
  268. chkDebug.Checked := pNodeData.DebugMode;
  269. NeedSave := False;
  270. end;
  271. end;
  272. procedure TfrmMacroManager.sbtnDeleteClick(Sender: TObject);
  273. var
  274. pNode: PVirtualNode;
  275. pNodeData: PMacroData;
  276. pReg: TRegistry;
  277. begin
  278. pNode := vstMacros.GetFirstSelected();
  279. if Assigned(pNode) then
  280. begin
  281. pNodeData := vstMacros.GetNodeData(pNode);
  282. // Make sure the user really wants to do this
  283. if Application.MessageBox(PChar('Are you sure you want to delete the "'+pNodeData.Name+'" macro?'), 'LuaEdit', MB_YESNO+MB_ICONQUESTION) = IDYES then
  284. begin
  285. pReg := TRegistry.Create();
  286. // Delete the related registry key (so in other words, delete the macro)
  287. if pReg.OpenKey('\Software\LuaEdit\Macros\'+pNodeData.Name, False) then
  288. pReg.DeleteKey('\Software\LuaEdit\Macros\'+pNodeData.Name);
  289. // Rebuild the list of macros...
  290. FillMacroList();
  291. end;
  292. end;
  293. end;
  294. procedure TfrmMacroManager.sbtnSaveClick(Sender: TObject);
  295. var
  296. pNode: PVirtualNode;
  297. pNodeData: PMacroData;
  298. pReg: TRegistry;
  299. begin
  300. pNode := vstMacros.GetFirstSelected();
  301. if Assigned(pNode) then
  302. begin
  303. pNodeData := vstMacros.GetNodeData(pNode);
  304. // Make sure the user really wants to do this
  305. if Application.MessageBox(PChar('Save changes to the "'+txtName.Text+'" macro?'), 'LuaEdit', MB_YESNO+MB_ICONQUESTION) = IDYES then
  306. begin
  307. SaveMacro(pNodeData.Name);
  308. // Rebuild the list of macros...
  309. FillMacroList();
  310. end;
  311. end;
  312. end;
  313. procedure TfrmMacroManager.sbtnAddClick(Sender: TObject);
  314. var
  315. pNode: PVirtualNode;
  316. pNodeData: PMacroData;
  317. begin
  318. // Create node and retrieve data pointer
  319. pNode := vstMacros.AddChild(vstMacros.RootNode);
  320. pNodeData := vstMacros.GetNodeData(pNode);
  321. pNodeData.Name := 'New Macro';
  322. pNodeData.FileName := '';
  323. pNodeData.Caption := 'New Macro';
  324. pNodeData.Shortcut := '[None]';
  325. pNodeData.DebugMode := False;
  326. pNodeData.IsNew := True;
  327. // Select newly created node
  328. vstMacros.Selected[pNode] := True;
  329. vstMacrosFocusChanged(vstMacros, pNode, 0);
  330. NeedSave := True;
  331. end;
  332. procedure TfrmMacroManager.txtNameChange(Sender: TObject);
  333. begin
  334. NeedSave := True;
  335. end;
  336. procedure TfrmMacroManager.txtFileNameChange(Sender: TObject);
  337. begin
  338. NeedSave := True;
  339. end;
  340. procedure TfrmMacroManager.txtCaptionChange(Sender: TObject);
  341. begin
  342. NeedSave := True;
  343. end;
  344. procedure TfrmMacroManager.cboShortcutClick(Sender: TObject);
  345. begin
  346. NeedSave := True;
  347. end;
  348. procedure TfrmMacroManager.chkDebugClick(Sender: TObject);
  349. begin
  350. NeedSave := True;
  351. end;
  352. end.