StringMap_frMain.pas 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. interface
  27. uses
  28. System.SysUtils,
  29. System.Classes,
  30. System.Rtti,
  31. FMX.Types,
  32. FMX.Controls,
  33. FMX.StdCtrls,
  34. FMX.ScrollBox,
  35. FMX.Forms,
  36. FMX.Grid,
  37. FMX.Grid.Style,
  38. FMX.Controls.Presentation,
  39. BrookHandledClasses,
  40. BrookStringMap;
  41. type
  42. TfrMain = class(TForm)
  43. pnTop: TPanel;
  44. btAdd: TButton;
  45. btRemove: TButton;
  46. btClear: TButton;
  47. grMap: TGrid;
  48. coKey: TStringColumn;
  49. coValue: TStringColumn;
  50. procedure FormCreate(Sender: TObject);
  51. procedure FormDestroy(Sender: TObject);
  52. procedure btAddClick(Sender: TObject);
  53. procedure btRemoveClick(Sender: TObject);
  54. procedure btClearClick(Sender: TObject);
  55. procedure grMapGetValue(Sender: TObject; const ACol, ARow: Integer;
  56. var AValue: TValue);
  57. private
  58. FMap: TBrookStringMap;
  59. FList: TStrings;
  60. procedure DoMapChange(ASender: TObject;
  61. AOperation: TBrookStringMapOperation);
  62. end;
  63. var
  64. frMain: TfrMain;
  65. implementation
  66. {$R *.fmx}
  67. procedure TfrMain.FormCreate(Sender: TObject);
  68. begin
  69. FMap := TBrookStringMap.Create(nil);
  70. FList := TStringList.Create;
  71. FMap.OnChange := DoMapChange;
  72. end;
  73. procedure TfrMain.FormDestroy(Sender: TObject);
  74. begin
  75. FMap.Free;
  76. FList.Free;
  77. end;
  78. procedure TfrMain.btAddClick(Sender: TObject);
  79. var
  80. S: string;
  81. begin
  82. S := Succ(FMap.Count).ToString;
  83. FMap.Add(Concat('Name', S), Concat('Value', S));
  84. end;
  85. procedure TfrMain.btRemoveClick(Sender: TObject);
  86. begin
  87. FMap.Remove(TStringGridModel(grMap.Model).Cells[0, grMap.Row]);
  88. end;
  89. procedure TfrMain.btClearClick(Sender: TObject);
  90. begin
  91. FMap.Clear;
  92. end;
  93. procedure TfrMain.DoMapChange(ASender: TObject;
  94. AOperation: TBrookStringMapOperation);
  95. var
  96. R: Integer;
  97. P: TBrookStringPair;
  98. begin
  99. R := grMap.Row;
  100. grMap.RowCount := 0;
  101. grMap.RowCount := FMap.Count;
  102. FList.Clear;
  103. for P in FMap do
  104. FList.AddPair(P.Name, P.Value);
  105. case AOperation of
  106. sgmoAdd: grMap.Row := Pred(grMap.RowCount);
  107. sgmoRemove:
  108. if R > 0 then
  109. grMap.Row := Pred(R)
  110. else
  111. grMap.Row := 0;
  112. end;
  113. btRemove.Enabled := FMap.Count > 0;
  114. btClear.Enabled := btRemove.Enabled;
  115. end;
  116. procedure TfrMain.grMapGetValue(Sender: TObject; const ACol, ARow: Integer;
  117. var AValue: TValue);
  118. begin
  119. if FList.Count = 0 then
  120. AValue := nil
  121. else
  122. begin
  123. if grMap.Columns[ACol] = coKey then
  124. AValue := FList.Names[ARow]
  125. else if grMap.Columns[ACol] = coValue then
  126. AValue := FList.ValueFromIndex[ARow];
  127. end;
  128. end;
  129. end.