StringMap_frMain.pas 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. (* _ _
  2. * | |__ _ __ ___ ___ | | __
  3. * | '_ \| '__/ _ \ / _ \| |/ /
  4. * | |_) | | | (_) | (_) | <
  5. * |_.__/|_| \___/ \___/|_|\_\
  6. *
  7. * Microframework which helps to develop web Pascal applications.
  8. *
  9. * Copyright (c) 2012-2021 Silvio Clecio <[email protected]>
  10. *
  11. * Brook framework is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU Lesser General Public
  13. * License as published by the Free Software Foundation; either
  14. * version 2.1 of the License, or (at your option) any later version.
  15. *
  16. * Brook framework is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * Lesser General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Lesser General Public
  22. * License along with Brook framework; if not, write to the Free Software
  23. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  24. *)
  25. unit StringMap_frMain;
  26. {$MODE DELPHI}
  27. interface
  28. uses
  29. SysUtils,
  30. StdCtrls,
  31. ExtCtrls,
  32. ValEdit,
  33. Forms,
  34. BrookStringMap;
  35. type
  36. TfrMain = class(TForm)
  37. btAdd: TButton;
  38. btRemove: TButton;
  39. btClear: TButton;
  40. pnTop: TPanel;
  41. veMap: TValueListEditor;
  42. procedure btAddClick(Sender: TObject);
  43. procedure btClearClick(Sender: TObject);
  44. procedure btRemoveClick(Sender: TObject);
  45. procedure FormCreate(Sender: TObject);
  46. procedure FormDestroy(Sender: TObject);
  47. private
  48. FMap: TBrookStringMap;
  49. procedure DoMapChange(ASender: TObject;
  50. AOperation: TBrookStringMapOperation);
  51. end;
  52. var
  53. frMain: TfrMain;
  54. implementation
  55. {$R *.lfm}
  56. procedure TfrMain.FormCreate(Sender: TObject);
  57. begin
  58. FMap := TBrookStringMap.Create(nil);
  59. FMap.OnChange := DoMapChange;
  60. end;
  61. procedure TfrMain.FormDestroy(Sender: TObject);
  62. begin
  63. FMap.Free;
  64. end;
  65. procedure TfrMain.btAddClick(Sender: TObject);
  66. var
  67. S: string;
  68. begin
  69. S := Succ(FMap.Count).ToString;
  70. FMap.Add(Concat('Name', S), Concat('Value', S));
  71. end;
  72. procedure TfrMain.btRemoveClick(Sender: TObject);
  73. begin
  74. FMap.Remove(veMap.Keys[veMap.Row]);
  75. end;
  76. procedure TfrMain.btClearClick(Sender: TObject);
  77. begin
  78. FMap.Clear;
  79. end;
  80. procedure TfrMain.DoMapChange(ASender: TObject;
  81. AOperation: TBrookStringMapOperation);
  82. var
  83. P: TBrookStringPair;
  84. R: Integer;
  85. begin
  86. R := veMap.Row;
  87. veMap.Clear;
  88. for P in FMap do
  89. veMap.Strings.AddPair(P.Name, P.Value);
  90. case AOperation of
  91. sgmoAdd: veMap.Row := Pred(veMap.RowCount);
  92. sgmoRemove:
  93. if R > 0 then
  94. veMap.Row := Pred(R)
  95. else
  96. veMap.Row := 0;
  97. end;
  98. btRemove.Enabled := FMap.Count > 0;
  99. btClear.Enabled := btRemove.Enabled;
  100. end;
  101. end.