1

In another question, I asked about importing an RTF file into InnoSetup to use for a custom wizard page:

Import external RTF file for TRichEditViewer?

Unfortunately, I was only able to figure out how to use the answer so that the external file would be loaded if that file existed on the user's system, and in this case, it doesn't exist on the user's system; it's a file I created to display the installer.

I couldn't figure out how to load the external file so that it would be saved inside the compiled installer script and be visible on someone else's system.

Here is the code I put together. I've experimented with creating separate procedures for loading the string, but haven't been able to figure out how to make it work. I'll be grateful for any help:

procedure CreateTheWizardPages;

var
#ifndef UNICODE
  rtfstr: string;
#else
  rtfstr: AnsiString;
#endif

var
     Page: TWizardPage;
     RichEditViewer: TRichEditViewer;
     vDosFolder: String; 
begin

  LoadStringFromFile('c:\dropbox\vdosinst\custom.rtf', rtfstr);

  if RegQueryStringValue(HKEY_CURRENT_USER, 'Software\WPDOS.org','vDosDir', vDosFolder) then 
     begin

    if ((DirExists(vDosFolder + '\62Config')) OR (DirExists(vDosFolder + '\61Config')) OR (DirExists(vDosFolder + '\51Config'))) then
      begin

    Page := CreateCustomPage(wpInfoBefore, 'How to install vDosWP-VDM with an existing vDosWP system', 'Read this message for important information!'); 

    RichEditViewer := TRichEditViewer.Create(Page);
    RichEditViewer.Width := Page.SurfaceWidth;
    RichEditViewer.Height := Page.SurfaceHeight;
    RichEditViewer.Parent := Page.Surface;
    RichEditViewer.ScrollBars := ssVertical;
    RichEditViewer.UseRichEdit := True;
    RichEditViewer.RTFText := rtfstr;
    RichEditViewer.ReadOnly := True;

    end;
  end; 
end;

procedure InitializeWizard();         // ISSI_ added to string
begin
  CreateTheWizardPages;
end;
Community
  • 1
  • 1
emendelson
  • 416
  • 5
  • 18
  • You can make a `[Files]` entry with the `dontcopy` flag and use e.g. `ExtractTemporaryFile` function to extract the file into the setup's temporary folder (the one targeted by `{tmp}` constant). But I don't get why to load the RTF text from a variable filled by a `LoadStringFromFile` function call. Wouldn't be easier to just load the RTF file directly into the rich edit control from file ? – TLama Oct 11 '14 at 14:53
  • QUOTE: "Wouldn't be easier to just load the RTF file directly into the rich edit control from file?" - Yes, it would be MUCH easier! I didn't know it was possible, and I'm too much of a beginner to figure out which syntax to use. I know I'm asking a lot, but if you could let me know the syntax, I'd be very grateful. Thank you. – emendelson Oct 11 '14 at 16:08
  • Hm, sorry, I was hoping that the `TRichEditViewer` supports loading of an RTF text directly from file. Taking back that note... – TLama Oct 11 '14 at 16:29
  • 1
    @TLama, Your initial comment was the answer I needed: thank you. I added an entry for the RTF file in [Files] with the `dontcopy` flag and then used `ExtractTemporaryFile` in the Code section to read from the file in the {Tmp} folder. Works perfectly - and this did exactly what I was trying to do. Thanks again. – emendelson Oct 12 '14 at 12:12

1 Answers1

2

You can add this compile time macro before your code block:

#pragma parseroption -p- 


#define FileHandle
#define FileLine
#define FileName
#define Result
#sub ProcessFileLine
  #define FileLine = FileRead(FileHandle) 
  #if Len(Result) > 0 && !FileEof(FileHandle)
    #expr Result = Result + "#10#13 +  \n"
  #endif
  #if FileLine != '\0'
    #expr Result = Result + "'" + FileLine + "'"
  #endif
#endsub
#sub ProcessFile
  #for {FileHandle = FileOpen(FileName); \
    FileHandle && !FileEof(FileHandle); ""} \
    ProcessFileLine
  #if FileHandle
    #expr FileClose(FileHandle)
  #endif   
#endsub

#define ReadFileAsStr(str AFileName) \
  Result = '', FileName = AFileName, ProcessFile, Result

This macro outputs file content as string constant. This works for most RTF files, but some characters inside RTF can broke this code. To fix this you need to escape ', " and may be some other characters inside ProcessFileLine sub.

Then you can use this macro in [Code] block this way:

RichEditViewer.RTFText := {#emit ReadFileAsStr("custom.rtf")};
Eugene Mala
  • 1,081
  • 12
  • 25