StringMap_frMain.pas 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. 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. FMapHandle: Pointer;
  60. FList: TStrings;
  61. procedure DoMapChange(ASender: TObject;
  62. AOperation: TBrookStringMapOperation);
  63. end;
  64. var
  65. frMain: TfrMain;
  66. implementation
  67. {$R *.fmx}
  68. procedure TfrMain.FormCreate(Sender: TObject);
  69. begin
  70. FMap := TBrookStringMap.Create(@FMapHandle);
  71. FList := TStringList.Create;
  72. FMap.OnChange := DoMapChange;
  73. end;
  74. procedure TfrMain.FormDestroy(Sender: TObject);
  75. begin
  76. FMap.Free;
  77. FList.Free;
  78. end;
  79. procedure TfrMain.btAddClick(Sender: TObject);
  80. var
  81. S: string;
  82. begin
  83. S := Succ(FMap.Count).ToString;
  84. FMap.Add(Concat('Name', S), Concat('Value', S));
  85. end;
  86. procedure TfrMain.btRemoveClick(Sender: TObject);
  87. begin
  88. FMap.Remove(TStringGridModel(grMap.Model).Cells[0, grMap.Row]);
  89. end;
  90. procedure TfrMain.btClearClick(Sender: TObject);
  91. begin
  92. FMap.Clear;
  93. end;
  94. procedure TfrMain.DoMapChange(ASender: TObject;
  95. AOperation: TBrookStringMapOperation);
  96. var
  97. R: Integer;
  98. P: TBrookStringPair;
  99. begin
  100. R := grMap.Row;
  101. grMap.RowCount := 0;
  102. grMap.RowCount := FMap.Count;
  103. FList.Clear;
  104. for P in FMap do
  105. FList.AddPair(P.Name, P.Value);
  106. case AOperation of
  107. sgmoAdd: grMap.Row := Pred(grMap.RowCount);
  108. sgmoRemove:
  109. if R > 0 then
  110. grMap.Row := Pred(R)
  111. else
  112. grMap.Row := 0;
  113. end;
  114. btRemove.Enabled := FMap.Count > 0;
  115. btClear.Enabled := btRemove.Enabled;
  116. end;
  117. procedure TfrMain.grMapGetValue(Sender: TObject; const ACol, ARow: Integer;
  118. var AValue: TValue);
  119. begin
  120. if FList.Count = 0 then
  121. AValue := nil
  122. else
  123. begin
  124. if grMap.Columns[ACol] = coKey then
  125. AValue := FList.Names[ARow]
  126. else if grMap.Columns[ACol] = coValue then
  127. AValue := FList.ValueFromIndex[ARow];
  128. end;
  129. end;
  130. end.