Changing the Rave 5 and later Preview Form

Category

Code Based - General

Question

How do I replace the default preview form that comes with Rave 5 and later?

Solution

There are several methods you can use to modify the preview screen. The method you choose will largely depend upon whether you have source code (BEX version) or not (BE version).

METHOD 1 (quickest but NOT recommended)

The method requires source code (BEX version)
1) Start Delphi (or other Borland product (CBuilder etc)
2) Open the form RpFormPreview located in the Rave5\Source directory
3) Make the desired changes and save them
4) Close Delphi (or other Borland product)
5) Run the appropriate batch file located in Rave5\Source (Full??.BAT). For Delphi 7, it would be FullD7.Bat.

That's it, you have your own preview. However, this method is NOT recommended. The primary disadvantage to using this approach is problems when new patches for Rave Reports are released. The updated Rave might not recognize the modifications to the source code and you might run into problems.

METHOD 2 (source code available - BEX version)

This is the recommended method for those that have Rave Reports source code (BEX customers). This method shows how to override the built-in preview system. This approach is more flexible and therefore, more desirable. In simple terms, you copy the existing preview source (RpFormPreview) to a different name (say MyPreview). Then you use the RpSystem source to see how the OverridePreviewProc is structured and copy appropriate parts to your RvSystem component overridepreview event. Now here are the steps.

PART 1

1. Copy RpFormPreview.PAS and RpFormPreview.DFM to new files, let's call them MyPreview.PAS and MyPreview.DFM.
2. Open MyPreview.PAS inside Delphi (preferably with your project already open) and rename the unit heading to MyPreview.
3. Rename the form associated with MyPreview.PAS to MyPreviewForm in the Object Inspector.
4. Add the unit MyPreview to your project using the project manager.

PART 2

5. Open the file RPSYSTEM.PAS from the source subdirectory and go to the procedure OverridePreviewProc. This block of code will be used in the next step.
6. Go to one of your ReportSystem components and open event tab. Then create (double-click) on the OverridePreview event. In OverridePreview code area copy the corresponding code from the RPSYSTEM.PAS OverridePreviewProc block of code.
7. Replace all occurrences of TRPPreviewForm (standard preview) with TMyPreviewForm (your custom preview) throughout the new OverridePreview procedure.
8. Make some simple change to your MyPreview source. For example, change the caption to read "New Custom Preview".
9. Recompile your project using Build All.

Run your program and generate a report that uses the modified ReportSystem component. If you see your "New Custom Preview", then you are well on your way. Now, you can modify your custom preview form as you wish.

METHOD 3 (NO source code available - BE version)

This is the only method available to those that do not have Rave Reports source code (BE customers). This method shows how to override the built-in preview system. This approach is flexible and allows you to create a preview form of your own design. We will create a very simple preview form using a name MyPreview. Then you place some override statements in your RvSystem component overridepreview event. Now here are the steps for a simple preview system.

PART 1

1. Create a new form and call it say -- MyPreview.PAS and MyPreview.DFM.
2. Add the unit MyPreview to your project using the project manager.
3. Open MyPreview.PAS inside Delphi and add the following components.
a) tPanel and set the align property to alTop
b) Drop 5 buttons on the tPanel area
c) Drop an "Edit" box on the tPanel area
d) Drop a "ScrollBox" on the form (set align property to alClient)
e) Go back to the buttons and assign the following caption properties and OnClick events. The simple way to create an OnClick event shell is to double-click on that button.

BUTTONS

1st button -- First page
 caption property set to "&First"
First Page button onClick event -- add following code
   RvRenderPreview.RenderPage(1);

2nd button -- Previous Page
 caption property set to "&Previous"
Previous page button onClick event -- add following code
  If RvRenderPreview.CurrentPage > 1 Then Begin
    RvRenderPreview.PrevPage;
  End;

3rd button -- Next Page
 caption property set to "&Next"
Next page button onClick event -- add following code
  If (RvRenderPreview.CurrentPage < RvRenderPreview.Pages) Then Begin
    RvRenderPreview.NextPage;
  End;

4th button -- Last Page
 caption property set to "&Last"
Last page button onClick event -- add following code
  RvRenderPreview.RenderPage(RvRenderPreview.Pages);

5th button -- Close
 caption property set to "&Close"
Close button onClick event -- add following code
  Close;

MYPREVIEW FORM

MyPreview OnClose event -- add following code
 "FormClosed := true;"

MyPreview OnCreate event -- add following code
  FPageNum := 1;
  RvRenderPreview := TRvRenderPreview.Create(self);
  With RvRenderPreview Do Begin
    OnPageChange := NDRPreviewPageChange;
    ShadowDepth := 2;
    MarginPercent := 5;
    ZoomFactor := 100;
    ScrollBox := ScrollBox1;
  End; { with }

  RvRenderPrinter := TRvRenderPrinter.Create(self);
  With RvRenderPrinter Do Begin
  End; { with }

MyPreview OnDestroy event -- add following code
  FreeAndNil(RvRenderPreview);

MyPreview form -- add following procedure
procedure TFormMyPreview.NDRPreviewPageChange(Sender: TObject);
begin
  with RvRenderPreview do begin
    lblPageNo.Text:= IntToStr(CurrentPage);
  end; { with }
end;


MyPreview form -- be sure the following is part of your uses statement
  RpDefine, RpRenderPreview, RpSystem;

4. That is it for your sample preview form for now. Later, you can go back and change it to anything you want.

PART 2

This is the last thing to do to create an override of the built-in preview. Go to one of your ReportSystem components and look at the event tab. Then create (double-click) on the OverridePreview event. In OverridePreview code area copy the following code.

ReportSystem OverridePreview event -- add following code
Procedure TFormOverride.RvSystem1OverridePreview(ReportSystem: TRvSystem;
  OverrideMode: TOverrideMode; Var OverrideForm: TForm);
Begin
  Case OverrideMode Of
    omCreate: Begin
        OverrideForm := TFormMyPreview.Create(self);
        OverrideForm.Caption := ReportSystem.TitlePreview;
        OverrideForm.Width := ReportSystem.SystemPreview.FormWidth;
        OverrideForm.Height := ReportSystem.SystemPreview.FormHeight;
        OverrideForm.WindowState := ReportSystem.SystemPreview.FormState;
        (OverrideForm As TFormMyPreview).ReportSystem := ReportSystem;
      End;

    omShow: Begin
        ReportSystem.SystemPreview.InitPreview((OverrideForm As TFormMyPreview).RvRenderPreview);
        If Assigned(ReportSystem.OnPreviewSetup) Then Begin
          ReportSystem.OnPreviewSetup((OverrideForm As TFormMyPreview).RvRenderPreview);
        End; { if }

        (OverrideForm As TFormMyPreview).InputFileName := ReportSystem.SystemFiler.Filename;
        (OverrideForm As TFormMyPreview).InputStream := ReportSystem.SystemFiler.Stream;
        (OverrideForm As TFormMyPreview).InitFromRPSystem;
    (* *)
        If soPreviewModal In ReportSystem.SystemOptions Then Begin
          OverrideForm.ShowModal;
        End Else Begin
          OverrideForm.Show;
        End; { else }
      End;

    omWait: Begin
        If Not (soPreviewModal In ReportSystem.SystemOptions) Then Begin
      // Wait for close
          Repeat
            Sleep(250);
            Application.ProcessMessages;
          Until Not OverrideForm.Visible;
        End; { if }
      End;
    omFree: Begin
        If (ReportSystem.SystemFiler.StreamMode In [smTempFile, smFile]) Then Begin
          (OverrideForm As TFormMyPreview).RvRenderPreview.NDRStream.Free;
          (OverrideForm As TFormMyPreview).RvRenderPreview.NDRStream := Nil;
        End; { if }
        FreeAndNil(OverrideForm);
      End;
  End; { case }
End;

5. Recompile your project using Build All.

Run your program and generate a report that uses the modified ReportSystem component. If you see your "New Custom Preview", then you are well on your way. Now, you can go back and modify your custom preview form as you wish.

You can download a working sample MyPreview.zip of the simple form.