@@ -24,9 +24,9 @@ interface
2424
2525
2626type
27- // / <summary>Class that saves information about a snippet to file in rich
28- // / text format. The snippet is obtained from a view. Only snippet views are
29- // / supported.</summary>
27+ // / <summary>Class that saves information about a snippet to file a user
28+ // / specified format. The snippet is obtained from a view. Only snippet views
29+ // / are supported.</summary>
3030 TSaveInfoMgr = class (TNoPublicConstructObject)
3131 strict private
3232 var
@@ -39,6 +39,10 @@ TSaveInfoMgr = class(TNoPublicConstructObject)
3939 class function GenerateRichText (View : IView; const AUseHiliting: Boolean):
4040 TEncodedData; static;
4141
42+ // / <summary>Returns encoded data containing a plain text representation of
43+ // / information about the snippet represented by the given view.</summary>
44+ function GeneratePlainText : TEncodedData;
45+
4246 // / <summary>Returns type of file selected in the associated save dialogue
4347 // / box.</summary>
4448 function SelectedFileType : TSourceFileType;
@@ -120,7 +124,8 @@ implementation
120124 UPreferences,
121125 URTFSnippetDoc,
122126 URTFUtils,
123- USourceGen;
127+ USourceGen,
128+ UTextSnippetDoc;
124129
125130{ TSaveInfoMgr }
126131
@@ -195,6 +200,23 @@ function TSaveInfoMgr.GenerateOutput(const FileType: TSourceFileType):
195200 TFileHiliter.IsHilitingSupported(FileType);
196201 case FileType of
197202 sfRTF: Result := GenerateRichText(fView, UseHiliting);
203+ sfText: Result := GeneratePlainText;
204+ end ;
205+ end ;
206+
207+ function TSaveInfoMgr.GeneratePlainText : TEncodedData;
208+ var
209+ Doc: TTextSnippetDoc; // object that generates RTF document
210+ HiliteAttrs: IHiliteAttrs; // syntax highlighter formatting attributes
211+ begin
212+ Assert(Supports(fView, ISnippetView),
213+ ClassName + ' .GeneratePlainText: View is not a snippet view' );
214+ HiliteAttrs := THiliteAttrsFactory.CreateNulAttrs;
215+ Doc := TTextSnippetDoc.Create;
216+ try
217+ Result := Doc.Generate((fView as ISnippetView).Snippet);
218+ finally
219+ Doc.Free;
198220 end ;
199221end ;
200222
@@ -235,20 +257,35 @@ constructor TSaveInfoMgr.InternalCreate(AView: IView);
235257 sDlgCaption = ' Save Snippet Information' ;
236258 // descriptions of supported encodings
237259 sASCIIEncoding = ' ASCII' ;
260+ sANSIDefaultEncoding = ' ANSI (Default)' ;
261+ sUTF8Encoding = ' UTF-8' ;
262+ sUTF16LEEncoding = ' Unicode (Little Endian)' ;
263+ sUTF16BEEncoding = ' Unicode (Big Endian)' ;
238264 // descriptions of supported file filter strings
239265 sRTFDesc = ' Rich text file' ;
266+ sTextDesc = ' Plain text file' ;
240267begin
241268 inherited InternalCreate;
242269 fView := AView;
243270 fSourceFileInfo := TSourceFileInfo.Create;
244- // only RTF file type supported at present
271+ // RTF and plain text files supported at present
245272 fSourceFileInfo.FileTypeInfo[sfRTF] := TSourceFileTypeInfo.Create(
246273 ' .rtf' ,
247274 sRTFDesc,
248275 [
249276 TSourceFileEncoding.Create(etASCII, sASCIIEncoding)
250277 ]
251- );
278+ );
279+ fSourceFileInfo.FileTypeInfo[sfText] := TSourceFileTypeInfo.Create(
280+ ' .txt' ,
281+ sTextDesc,
282+ [
283+ TSourceFileEncoding.Create(etUTF8, sUTF8Encoding),
284+ TSourceFileEncoding.Create(etUTF16LE, sUTF16LEEncoding),
285+ TSourceFileEncoding.Create(etUTF16BE, sUTF16BEEncoding),
286+ TSourceFileEncoding.Create(etSysDefault, sANSIDefaultEncoding)
287+ ]
288+ );
252289 fSourceFileInfo.DefaultFileName := sDefFileName;
253290
254291 fSaveDlg := TSaveSourceDlg.Create(nil );
@@ -266,13 +303,36 @@ constructor TSaveInfoMgr.InternalCreate(AView: IView);
266303procedure TSaveInfoMgr.PreviewHandler (Sender: TObject);
267304resourcestring
268305 sDocTitle = ' "%0:s" snippet' ;
306+ var
307+ // Type of snippet information document to preview: this is not always the
308+ // same as the selected file type, because preview dialogue box doesn't
309+ // support some types & we have to use an alternate.
310+ PreviewFileType: TSourceFileType;
311+ // Type of preview document supported by preview dialogue box
312+ PreviewDocType: TPreviewDocType;
269313begin
314+ case SelectedFileType of
315+ sfRTF:
316+ begin
317+ PreviewDocType := dtRTF;
318+ PreviewFileType := sfRTF;
319+ end ;
320+ sfText:
321+ begin
322+ PreviewDocType := dtPlainText;
323+ PreviewFileType := sfText;
324+ end ;
325+ else
326+ raise Exception.Create(
327+ ClassName + ' .PreviewHandler: unsupported file type'
328+ );
329+ end ;
270330 // Display preview dialog box. We use save dialog as owner to ensure preview
271331 // dialog box is aligned over save dialog box
272332 TPreviewDlg.Execute(
273333 fSaveDlg,
274- GenerateOutput(sfRTF ),
275- dtRTF ,
334+ GenerateOutput(PreviewFileType ),
335+ PreviewDocType ,
276336 Format(sDocTitle, [fView.Description])
277337 );
278338end ;
0 commit comments