StringMap_frMain.pas 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. (* _ _
  2. * | |__ _ __ ___ ___ | | __
  3. * | '_ \| '__/ _ \ / _ \| |/ /
  4. * | |_) | | | (_) | (_) | <
  5. * |_.__/|_| \___/ \___/|_|\_\
  6. *
  7. * Microframework which helps to develop web Pascal applications.
  8. *
  9. * Copyright (c) 2012-2020 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. {$WARN 5024 OFF}
  28. interface
  29. uses
  30. SysUtils,
  31. StdCtrls,
  32. ExtCtrls,
  33. ValEdit,
  34. Forms,
  35. BrookStringMap;
  36. type
  37. TfrMain = class(TForm)
  38. btAdd: TButton;
  39. btRemove: TButton;
  40. btClear: TButton;
  41. pnTop: TPanel;
  42. veMap: TValueListEditor;
  43. procedure btAddClick(Sender: TObject);
  44. procedure btClearClick(Sender: TObject);
  45. procedure btRemoveClick(Sender: TObject);
  46. procedure FormCreate(Sender: TObject);
  47. procedure FormDestroy(Sender: TObject);
  48. private
  49. FMap: TBrookStringMap;
  50. FMapHandle: Pointer;
  51. procedure DoMapChange(ASender: TObject;
  52. AOperation: TBrookStringMapOperation);
  53. end;
  54. var
  55. frMain: TfrMain;
  56. implementation
  57. {$R *.lfm}
  58. procedure TfrMain.FormCreate(Sender: TObject);
  59. begin
  60. FMap := TBrookStringMap.Create(@FMapHandle);
  61. FMap.OnChange := DoMapChange;
  62. end;
  63. procedure TfrMain.FormDestroy(Sender: TObject);
  64. begin
  65. FMap.Free;
  66. end;
  67. procedure TfrMain.btAddClick(Sender: TObject);
  68. var
  69. S: string;
  70. begin
  71. S := Succ(FMap.Count).ToString;
  72. FMap.Add(Concat('Name', S), Concat('Value', S));
  73. end;
  74. procedure TfrMain.btRemoveClick(Sender: TObject);
  75. begin
  76. FMap.Remove(veMap.Keys[veMap.Row]);
  77. end;
  78. procedure TfrMain.btClearClick(Sender: TObject);
  79. begin
  80. FMap.Clear;
  81. end;
  82. procedure TfrMain.DoMapChange(ASender: TObject;
  83. AOperation: TBrookStringMapOperation);
  84. var
  85. P: TBrookStringPair;
  86. R: Integer;
  87. begin
  88. R := veMap.Row;
  89. veMap.Clear;
  90. for P in FMap do
  91. veMap.Strings.AddPair(P.Name, P.Value);
  92. case AOperation of
  93. sgmoAdd: veMap.Row := Pred(veMap.RowCount);
  94. sgmoRemove:
  95. if R > 0 then
  96. veMap.Row := Pred(R)
  97. else
  98. veMap.Row := 0;
  99. end;
  100. btRemove.Enabled := FMap.Count > 0;
  101. btClear.Enabled := btRemove.Enabled;
  102. end;
  103. end.