StringMap_frMain.pas 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. FMapHandle: Pointer;
  50. procedure DoMapChange(ASender: TObject;
  51. AOperation: TBrookStringMapOperation);
  52. end;
  53. var
  54. frMain: TfrMain;
  55. implementation
  56. {$R *.lfm}
  57. procedure TfrMain.FormCreate(Sender: TObject);
  58. begin
  59. FMap := TBrookStringMap.Create(@FMapHandle);
  60. FMap.OnChange := DoMapChange;
  61. end;
  62. procedure TfrMain.FormDestroy(Sender: TObject);
  63. begin
  64. FMap.Free;
  65. end;
  66. procedure TfrMain.btAddClick(Sender: TObject);
  67. var
  68. S: string;
  69. begin
  70. S := Succ(FMap.Count).ToString;
  71. FMap.Add(Concat('Name', S), Concat('Value', S));
  72. end;
  73. procedure TfrMain.btRemoveClick(Sender: TObject);
  74. begin
  75. FMap.Remove(veMap.Keys[veMap.Row]);
  76. end;
  77. procedure TfrMain.btClearClick(Sender: TObject);
  78. begin
  79. FMap.Clear;
  80. end;
  81. procedure TfrMain.DoMapChange(ASender: TObject;
  82. AOperation: TBrookStringMapOperation);
  83. var
  84. P: TBrookStringPair;
  85. R: Integer;
  86. begin
  87. R := veMap.Row;
  88. veMap.Clear;
  89. for P in FMap do
  90. veMap.Strings.AddPair(P.Name, P.Value);
  91. case AOperation of
  92. sgmoAdd: veMap.Row := Pred(veMap.RowCount);
  93. sgmoRemove:
  94. if R > 0 then
  95. veMap.Row := Pred(R)
  96. else
  97. veMap.Row := 0;
  98. end;
  99. btRemove.Enabled := FMap.Count > 0;
  100. btClear.Enabled := btRemove.Enabled;
  101. end;
  102. end.