IDE.MainForm.MRUHelper.pas 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. unit IDE.MainForm.MRUHelper;
  2. {
  3. Inno Setup
  4. Copyright (C) 1997-2025 Jordan Russell
  5. Portions by Martijn Laan
  6. For conditions of distribution and use, see LICENSE.TXT.
  7. Compiler form - MRU helper
  8. Not used by MainForm: it uses IDE.MainForm.FinalHelper instead
  9. }
  10. interface
  11. uses
  12. IDE.MainForm;
  13. type
  14. TMainFormMRUHelper = class helper for TMainForm
  15. procedure ClearMRUMainFilesList;
  16. procedure ReadMRUMainFilesList;
  17. procedure ModifyMRUMainFilesList(const AFilename: String; const AddNewItem: Boolean);
  18. procedure ReadMRUParametersList;
  19. procedure ModifyMRUParametersList(const AParameter: String; const AddNewItem: Boolean);
  20. end;
  21. implementation
  22. uses
  23. Classes, SysUtils, Forms,
  24. PathFunc,
  25. Shared.ConfigIniFile;
  26. type
  27. TMRUItemCompareProc = function(const S1, S2: String): Integer;
  28. procedure ClearMRUList(const MRUList: TStringList; const Section: String);
  29. var
  30. Ini: TConfigIniFile;
  31. begin
  32. Ini := TConfigIniFile.Create;
  33. try
  34. MRUList.Clear;
  35. Ini.EraseSection(Section);
  36. finally
  37. Ini.Free;
  38. end;
  39. end;
  40. procedure ReadMRUList(const MRUList: TStringList; const Section, Ident: String);
  41. { Loads a list of MRU items from the registry }
  42. var
  43. Ini: TConfigIniFile;
  44. I: Integer;
  45. S: String;
  46. begin
  47. Ini := TConfigIniFile.Create;
  48. try
  49. MRUList.Clear;
  50. for I := 0 to MRUListMaxCount-1 do begin
  51. S := Ini.ReadString(Section, Ident + IntToStr(I), '');
  52. if S <> '' then MRUList.Add(S);
  53. end;
  54. finally
  55. Ini.Free;
  56. end;
  57. end;
  58. procedure ModifyMRUList(const MRUList: TStringList; const Section, Ident: String;
  59. const AItem: String; const AddNewItem: Boolean; CompareProc: TMRUItemCompareProc);
  60. var
  61. I: Integer;
  62. Ini: TConfigIniFile;
  63. S: String;
  64. begin
  65. I := 0;
  66. while I < MRUList.Count do begin
  67. if CompareProc(MRUList[I], AItem) = 0 then
  68. MRUList.Delete(I)
  69. else
  70. Inc(I);
  71. end;
  72. if AddNewItem then
  73. MRUList.Insert(0, AItem);
  74. while MRUList.Count > MRUListMaxCount do
  75. MRUList.Delete(MRUList.Count-1);
  76. { Save new MRU items }
  77. Ini := TConfigIniFile.Create;
  78. try
  79. { MRU list }
  80. for I := 0 to MRUListMaxCount-1 do begin
  81. if I < MRUList.Count then
  82. S := MRUList[I]
  83. else
  84. S := '';
  85. Ini.WriteString(Section, Ident + IntToStr(I), S);
  86. end;
  87. finally
  88. Ini.Free;
  89. end;
  90. end;
  91. { TMainFormMRUHelper }
  92. procedure TMainFormMRUHelper.ClearMRUMainFilesList;
  93. begin
  94. try
  95. ClearMRUList(FMRUMainFilesList, 'ScriptFileHistoryNew');
  96. except
  97. { Ignore any exceptions. }
  98. end;
  99. end;
  100. procedure TMainFormMRUHelper.ReadMRUMainFilesList;
  101. begin
  102. try
  103. ReadMRUList(FMRUMainFilesList, 'ScriptFileHistoryNew', 'History');
  104. except
  105. { Ignore any exceptions. }
  106. end;
  107. end;
  108. procedure TMainFormMRUHelper.ModifyMRUMainFilesList(const AFilename: String;
  109. const AddNewItem: Boolean);
  110. begin
  111. { Load most recent items first, just in case they've changed }
  112. try
  113. ReadMRUMainFilesList;
  114. except
  115. { Ignore any exceptions. }
  116. end;
  117. try
  118. ModifyMRUList(FMRUMainFilesList, 'ScriptFileHistoryNew', 'History', AFileName, AddNewItem, TMRUItemCompareProc(@PathCompare));
  119. except
  120. { Handle exceptions locally; failure to save the MRU list should not be
  121. a fatal error. }
  122. Application.HandleException(Self);
  123. end;
  124. end;
  125. procedure TMainFormMRUHelper.ReadMRUParametersList;
  126. begin
  127. try
  128. ReadMRUList(FMRUParametersList, 'ParametersHistory', 'History');
  129. except
  130. { Ignore any exceptions. }
  131. end;
  132. end;
  133. procedure TMainFormMRUHelper.ModifyMRUParametersList(const AParameter: String;
  134. const AddNewItem: Boolean);
  135. begin
  136. { Load most recent items first, just in case they've changed }
  137. try
  138. ReadMRUParametersList;
  139. except
  140. { Ignore any exceptions. }
  141. end;
  142. try
  143. ModifyMRUList(FMRUParametersList, 'ParametersHistory', 'History', AParameter, AddNewItem, TMRUItemCompareProc(@CompareText));
  144. except
  145. { Handle exceptions locally; failure to save the MRU list should not be
  146. a fatal error. }
  147. Application.HandleException(Self);
  148. end;
  149. end;
  150. end.