@@ -90,10 +90,24 @@ TSourceFileInfo = class(TObject)
9090 strict private
9191 var
9292 // / <summary>Stores information about the different source code output
93- // types required by save source dialog boxes.</summary>
93+ // / types required by save source dialog boxes.</summary>
9494 fFileTypeInfo: TDictionary<TSourceFileType,TSourceFileTypeInfo>;
95- // <summary>Value of DefaultFileName property.</summary>
95+ // / <summary>Maps a one-based index of a file filter within the current
96+ // / filter string to the corresponding <c>TSourceFileType</c> that was
97+ // / used to create the filter string entry.</summary>
98+ fFilterIdxToFileTypeMap: TDictionary<Integer,TSourceFileType>;
99+ // / <summary>Value of DefaultFileName property.</summary>
96100 fDefaultFileName: string;
101+ // / <summary>Filter string for use in open / save dialog boxes from
102+ // / descriptions and file extensions of each supported file type.
103+ // / </summary>
104+ fFilterString: string;
105+ // / <summary>Generates a new filter string and filter index to file type
106+ // / map from the current state of the <c>FileTypeInfo</c> property.
107+ // / </summary>
108+ // / <remarks>This method MUST be called every time the <c>FileTypeInfo</c>
109+ // / property is updated.</remarks>
110+ procedure GenerateFilterInfo ;
97111 // / <summary>Read accessor for FileTypeInfo property.</summary>
98112 // / <exception>Raises <c>EListError</c> if <c>FileType</c> is not contained
99113 // / in the property.</exception>
@@ -110,9 +124,14 @@ TSourceFileInfo = class(TObject)
110124 constructor Create;
111125 destructor Destroy; override;
112126
113- // / <summary>Builds filter string for use in open / save dialog boxes from
127+ // / <summary>Returns filter string for use in open / save dialog boxes from
114128 // / descriptions and file extensions of each supported file type.</summary>
115129 function FilterString : string;
130+
131+ // / <summary>Returns the file type associated with a file filter at the
132+ // / given one-based index within the current filter string.</summary>
133+ function FileTypeFromFilterIdx (const Idx: Integer): TSourceFileType;
134+
116135 // / <summary>Information about each supported file type that is of use to
117136 // / save source dialog boxes.</summary>
118137 // / <exception>A <c>EListError</c> exception is raised if no information
@@ -144,30 +163,48 @@ constructor TSourceFileInfo.Create;
144163begin
145164 inherited Create;
146165 fFileTypeInfo := TDictionary<TSourceFileType,TSourceFileTypeInfo>.Create;
166+ fFilterIdxToFileTypeMap := TDictionary<Integer,TSourceFileType>.Create;
147167end ;
148168
149169destructor TSourceFileInfo.Destroy;
150170begin
171+ fFilterIdxToFileTypeMap.Free;
151172 fFileTypeInfo.Free;
152173 inherited ;
153174end ;
154175
176+ function TSourceFileInfo.FileTypeFromFilterIdx (
177+ const Idx: Integer): TSourceFileType;
178+ begin
179+ Result := fFilterIdxToFileTypeMap[Idx];
180+ end ;
181+
155182function TSourceFileInfo.FilterString : string;
183+ begin
184+ Result := fFilterString;
185+ end ;
186+
187+ procedure TSourceFileInfo.GenerateFilterInfo ;
156188const
157189 cFilterFmt = ' %0:s (*%1:s)|*%1:s' ; // format string for creating file filter
158190var
159191 FT: TSourceFileType; // loops thru all source file types
192+ FilterIdx: Integer; // current index in filter string
160193begin
161- Result := ' ' ;
194+ fFilterIdxToFileTypeMap.Clear;
195+ FilterIdx := 1 ; // filter index is one based
196+ fFilterString := ' ' ;
162197 for FT := Low(TSourceFileType) to High(TSourceFileType) do
163198 begin
164199 if not fFileTypeInfo.ContainsKey(FT) then
165200 Continue;
166- if Result <> ' ' then
167- Result := Result + ' |' ;
168- Result := Result + Format(
201+ if fFilterString <> ' ' then
202+ fFilterString := fFilterString + ' |' ;
203+ fFilterString := fFilterString + Format(
169204 cFilterFmt, [fFileTypeInfo[FT].DisplayName, fFileTypeInfo[FT].Extension]
170205 );
206+ fFilterIdxToFileTypeMap.Add(FilterIdx, FT);
207+ Inc(FilterIdx);
171208 end ;
172209end ;
173210
@@ -203,6 +240,7 @@ procedure TSourceFileInfo.SetFileTypeInfo(const FileType: TSourceFileType;
203240 fFileTypeInfo[FileType] := Info
204241 else
205242 fFileTypeInfo.Add(FileType, Info);
243+ GenerateFilterInfo;
206244end ;
207245
208246{ TSourceFileTypeInfo }
0 commit comments