ec_showchoices.pas 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. unit ec_showchoices;
  2. {$mode objfpc}{$H+}
  3. interface
  4. uses
  5. Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, Buttons,
  6. StdCtrls, ec_base, fpjson;
  7. type
  8. { TECShowChoicesFrame }
  9. TECShowChoicesFrame = class(TECBaseFrame)
  10. BackgroundComboBox: TComboBox;
  11. BackgroundLabel: TLabel;
  12. BackgroundPanel: TPanel;
  13. Choice1Edit: TEdit;
  14. Choice1Label: TLabel;
  15. Choice1Panel: TPanel;
  16. Choice2Edit: TEdit;
  17. Choice2Label: TLabel;
  18. Choice2Panel: TPanel;
  19. Choice3Edit: TEdit;
  20. Choice3Label: TLabel;
  21. Choice3Panel: TPanel;
  22. Choice4Edit: TEdit;
  23. Choice4Label: TLabel;
  24. Choice4Panel: TPanel;
  25. Choice5Edit: TEdit;
  26. Choice5Label: TLabel;
  27. Choice5Panel: TPanel;
  28. Choice6Edit: TEdit;
  29. Choice6Label: TLabel;
  30. Choice6Panel: TPanel;
  31. ChoicesGroupBox: TGroupBox;
  32. PositionComboBox: TComboBox;
  33. DefaultComboBox: TComboBox;
  34. CancellationComboBox: TComboBox;
  35. PositionLabel: TLabel;
  36. DefaultLabel: TLabel;
  37. CancellationLabel: TLabel;
  38. PositionPanel: TPanel;
  39. DefaultPanel: TPanel;
  40. CancellationPanel: TPanel;
  41. ChoicesScrollBox: TScrollBox;
  42. SettingsGroupBox: TGroupBox;
  43. ShowChoicesMainPanel: TPanel;
  44. ChoiceSettingSplitter: TSplitter;
  45. private
  46. function GetEdit(Index: Integer): TEdit;
  47. function GetChoiceText(Index: Integer): String;
  48. procedure InitialSetUp;
  49. public
  50. procedure InitNew; override;
  51. procedure InitExisting; override;
  52. procedure MakeResultingCommand; override;
  53. end;
  54. var
  55. ECShowChoicesFrame: TECShowChoicesFrame;
  56. implementation
  57. uses
  58. comboboxhelper, constants;
  59. {$R *.lfm}
  60. resourcestring
  61. rsChoiceNumber = 'Choice #%d';
  62. rsOwnCancelBranch = 'Own branch';
  63. rsCancelDisabled = 'Disabled';
  64. rsNoDefaultBranch = 'None';
  65. { TECShowChoicesFrame }
  66. function TECShowChoicesFrame.GetEdit(Index: Integer): TEdit;
  67. begin
  68. case Index of
  69. 1: GetEdit := Choice1Edit;
  70. 2: GetEdit := Choice2Edit;
  71. 3: GetEdit := Choice3Edit;
  72. 4: GetEdit := Choice4Edit;
  73. 5: GetEdit := Choice5Edit;
  74. 6: GetEdit := Choice6Edit;
  75. else
  76. GetEdit := nil;
  77. end;
  78. end;
  79. function TECShowChoicesFrame.GetChoiceText(Index: Integer): String;
  80. var
  81. ChoiceEdit: TEdit;
  82. begin
  83. ChoiceEdit := GetEdit(Index);
  84. GetChoiceText := ChoiceEdit.Text;
  85. end;
  86. procedure TECShowChoicesFrame.InitialSetUp;
  87. procedure FillOnCancel;
  88. var
  89. I: Integer;
  90. begin
  91. with CancellationComboBox.Items do begin
  92. BeginUpdate;
  93. Clear;
  94. Add(rsOwnCancelBranch);
  95. Add(rsCancelDisabled);
  96. for I := 1 to 6 do begin
  97. Add(rsChoiceNumber.Format([I]));
  98. end;
  99. EndUpdate;
  100. end;
  101. end;
  102. procedure FillDefault;
  103. var
  104. I: Integer;
  105. begin
  106. with DefaultComboBox.Items do begin
  107. BeginUpdate;
  108. Clear;
  109. Add(rsNoDefaultBranch);
  110. for I := 1 to 6 do begin
  111. Add(rsChoiceNumber.Format([I]));
  112. end;
  113. EndUpdate;
  114. end;
  115. end;
  116. begin
  117. FillMessageBgTypeCombobox(BackgroundComboBox);
  118. FillChoicePosCombobox(PositionComboBox);
  119. FillDefault;
  120. FillOnCancel;
  121. end;
  122. procedure TECShowChoicesFrame.InitNew;
  123. begin
  124. InitialSetUp;
  125. BackgroundComboBox.ItemIndex := 0;
  126. PositionComboBox.ItemIndex := 2;
  127. DefaultComboBox.ItemIndex := 1;
  128. CancellationComboBox.ItemIndex := 3;
  129. Choice1Edit.Text := rsYes;
  130. Choice2Edit.Text := rsNo;
  131. end;
  132. procedure TECShowChoicesFrame.InitExisting;
  133. var
  134. Params: TJSONArray;
  135. procedure SetChoiceTexts;
  136. var
  137. I: Integer;
  138. ChoiceTexts: TJSONArray;
  139. Edit: TEdit;
  140. begin
  141. if (Params.Count < 1) or (Params[0].JSONType <> jtArray) then
  142. Exit;
  143. ChoiceTexts := Params.Arrays[0];
  144. for I := 0 to ChoiceTexts.Count -1 do
  145. if ChoiceTexts[I].JSONType = jtString then begin
  146. Edit := GetEdit(I + 1);
  147. Edit.Text := ChoiceTexts.Strings[I];
  148. end;
  149. end;
  150. procedure SetUpSettings;
  151. procedure SingleParam(I: Integer; CBox: TComboBox; Shift: Integer = 0);
  152. var
  153. Value: Integer = 0;
  154. begin
  155. if (Params <> nil) and (Params.Count > I)
  156. and (Params[I].JSONType = jtNumber) then
  157. Value := Params.Integers[I];
  158. CBox.ItemIndex := Value + Shift;
  159. end;
  160. begin
  161. SingleParam(1, CancellationComboBox, 2);
  162. SingleParam(2, DefaultComboBox, 1);
  163. SingleParam(3, PositionComboBox);
  164. SingleParam(4, BackgroundComboBox);
  165. end;
  166. begin
  167. InitialSetUp;
  168. Params := GetFirstParams;
  169. if Params <> nil then begin
  170. SetChoiceTexts;
  171. SetUpSettings
  172. end else
  173. InitNew;
  174. end;
  175. procedure TECShowChoicesFrame.MakeResultingCommand;
  176. var
  177. NumChoices: Integer;
  178. procedure CountChoices;
  179. begin
  180. NumChoices := 6;
  181. while NumChoices >= 1 do begin
  182. if Trim(GetChoiceText(NumChoices)) = '' then
  183. Dec(NumChoices)
  184. else
  185. Break;
  186. end;
  187. end;
  188. function FirstCommand: TJSONObject;
  189. function GetChoiceTexts: TJSONArray;
  190. var
  191. ChoiceTexts: TJSONArray;
  192. I: Integer;
  193. begin
  194. ChoiceTexts := TJSONArray.Create;
  195. for I := 1 to NumChoices do
  196. ChoiceTexts.Add(GetChoiceText(I));
  197. GetChoiceTexts := ChoiceTexts;
  198. end;
  199. begin
  200. FirstCommand := TJSONObject.Create([
  201. 'code', SHOW_CHOICES_START_EC_CODE,
  202. 'indent', Indent,
  203. 'parameters', TJSONArray.Create([
  204. GetChoiceTexts,
  205. CancellationComboBox.ItemIndex - 2,
  206. DefaultComboBox.ItemIndex - 1,
  207. PositionComboBox.ItemIndex,
  208. BackgroundComboBox.ItemIndex
  209. ])
  210. ]);
  211. end;
  212. procedure AddEmptyCommand;
  213. begin
  214. ResultingCommand.Add(TJSONObject.Create([
  215. 'code', 0,
  216. 'indent', Indent + 1,
  217. 'parameters', TJSONArray.Create()
  218. ]));
  219. end;
  220. procedure AddExitingSubCommands(IsCancel: Boolean; Index: Integer = -1);
  221. var
  222. CodeToFind: Integer = SHOW_CHOICES_BRANCH_EC_CODE;
  223. IndexToFind: Integer;
  224. Start: Integer;
  225. function FindStart: Integer;
  226. var
  227. I: Integer;
  228. Cmd: TJSONObject;
  229. begin
  230. I := ListIndex + 1;
  231. while I < EcList.Count do begin
  232. Cmd := EcList.Objects[I];
  233. if (Cmd.Integers['code'] = CodeToFind) and
  234. (Cmd.Arrays['parameters'].Integers[0] = IndexToFind) and
  235. (Cmd.Integers['indent'] = Indent) then begin
  236. Exit(I + 1);
  237. end;
  238. Inc(I);
  239. end;
  240. Exit(-1);
  241. end;
  242. procedure AddCommandsFromStart;
  243. var
  244. I: Integer;
  245. Cmd: TJSONObject;
  246. begin
  247. I := Start;
  248. while I < EcList.Count do begin
  249. Cmd := EcList.Objects[I];
  250. if Cmd.Integers['indent'] <= Indent then
  251. Break
  252. else
  253. ResultingCommand.Add(Cmd.Clone);
  254. Inc(I)
  255. end;
  256. end;
  257. begin
  258. if IsCancel then begin
  259. CodeToFind := SHOW_CHOICES_CANCEL_BRANCH_EC_CODE;
  260. IndexToFind := 6;
  261. end else begin
  262. CodeToFind := SHOW_CHOICES_BRANCH_EC_CODE;
  263. IndexToFind := Index - 1; {JSON indexes are zero-based}
  264. end;
  265. Start := FindStart;
  266. if Start = -1 then
  267. AddEmptyCommand
  268. else
  269. AddCommandsFromStart;
  270. end;
  271. procedure AddBranchCommands(Index: Integer);
  272. begin
  273. ResultingCommand.Add(TJSONObject.Create([
  274. 'code', SHOW_CHOICES_BRANCH_EC_CODE,
  275. 'indent', Indent,
  276. 'parameters', TJSONArray.Create([
  277. Index - 1,
  278. GetChoiceText(Index)
  279. ])
  280. ]));
  281. if EcList <> nil then
  282. AddExitingSubCommands(False, Index)
  283. else
  284. AddEmptyCommand;
  285. end;
  286. procedure AddCancelCommands;
  287. begin
  288. ResultingCommand.Add(TJSONObject.Create([
  289. 'code', SHOW_CHOICES_CANCEL_BRANCH_EC_CODE,
  290. 'indent', Indent,
  291. 'parameters', TJSONArray.Create([
  292. 6,
  293. nil
  294. ])
  295. ]));
  296. if EcList <> nil then
  297. AddExitingSubCommands(True)
  298. else
  299. AddEmptyCommand;
  300. end;
  301. procedure AddEndCommand;
  302. begin
  303. ResultingCommand.Add(TJSONObject.Create([
  304. 'code', SHOW_CHOICES_END_EC_CODE,
  305. 'indent', Indent,
  306. 'parameters', TJSONArray.Create()
  307. ]));
  308. end;
  309. var
  310. I: Integer;
  311. begin
  312. CountChoices;
  313. ResultingCommand := TJSONArray.Create();
  314. ResultingCommand.Add(FirstCommand);
  315. for I := 1 to NumChoices do
  316. AddBranchCommands(I);
  317. if CancellationComboBox.ItemIndex = 0 then
  318. AddCancelCommands;
  319. AddEndCommand;
  320. end;
  321. end.