Search Locate Previous Next Contents

Building a Print Preview for NewLeaf

The job of building a simple Print Preview screen is very similar to displaying charts from Rain. However it also illustrates several other useful techniques, including the use of a progress bar which may be necessary if you are generating a very large report.


This shows a simple sales summary (randomly generated) which runs to a three-page listing. The user can use the VCR control to run through the report or call a secondary dialogue to print it. While the report was building the screen showed a progress bar such as:


This clicks over once per section – we do not know at this stage how many pages the report will run to!

The next few pages run through the code needed to set up and increment the progress bar and build the report with NewLeaf.

Please refer to the NewLeaf User’s Guide for a full explanation of the syntax used to construct the table.

The first requirement is to copy the leaf namespace (and the PostScrp viewer unless you already have it) and check that it works:


      )copy newleaf leaf PostScrp
.\NL78 saved Tue Aug 27 17:54:24 1996
      #.leaf.Use ''
      #.leaf.table.List 12 4½¼123
      PG„#.leaf.Close
      #.leaf.NumPages PG    © How many pages?
1
      #.PostScrp.View PG    © Check it on screen
      )ed PG   © Check preview box in page definition
      .5×842 595   © Paper size in points
421 297.5

The page description of your sample report is held in a simple text variable PG, returned by #.leaf.Close. You can look at this with )ed to check the page size (in points), which gives you the proportions needed for a page-preview. In this case I used A4 portrait paper, so a good size for a VGA monitor is 421 by 298 pixels. Now we can go ahead and design the dialogue box for a simple page preview:


      Dbx 'dbx.pv'
Dialog box dbx.pv updated

The new objects you need are the PostScript Viewer (from the Special menu) and the VCR buttons. You can also add a Close button, and I find that the viewer looks good with a frame – use the Frame button on the lower toolbar to add this. We can return to the Print button when the basic viewer is working nicely.


ÚÎÎÂÂÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÂÎÎÎÎÎÎÎÌ

ÛCRÛÛpn„np„0 Û{} Û

ÃÎÎÏÏÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÏÎÎÎÎÎÎÎÝ

ÛPCÛÛ¸ ’Do ‘Show’ Û{} Û

ÃÎÎÏÏÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÏÎÎÎÎÎÎÎÝ

ÛPCÛÛspl„make_report 7 Û{spl} Û

ÃÎÎÏÏÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÏÎÎÎÎÎÎÎÝ

ÛPCÛÛnp„#.leaf.NumPages spl ª pn„1Û{npÛpn}Û

ÀÎÎÁÁÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÁÎÎÎÎÎÎÎÙ

On creating the form I have initialised a couple of local variables to hold the current page number and the total number of pages The form is then made visible (just to give the user something to see) and the report is constructed. When it completes, it updates the name of the spool file (which is set as the PostScript property of the preview) and checks how many pages were generated.

The properties for the PostScript viewer are set up as:

      Disp œdbx.pv[2;5]

ÚÎÎÎÎÎÎÎÎÎÎÂÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÌ

ÛBehaviour ÛÚÎÎÂÂÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÂÎÎÌÛ

Û ÛÛZMÛÛ¸ ’Do ‘Rescale’ ¾Û{}ÛÛ

Û ÛÀÎÎÁÁÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÁÎÎÙÛ

ÃÎÎÎÎÎÎÎÎÎÎÏÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÝ

ÛPage No ÛÚÎÎÎÎÎÎÎÎÎÎÂÎÎÎÎÌ Û

Û ÛÛExpressionÛpn Û Û

Û ÛÃÎÎÎÎÎÎÎÎÎÎÏÎÎÎÎÝ Û

Û ÛÛDependenceÛ{pn}Û Û

Û ÛÀÎÎÎÎÎÎÎÎÎÎÁÎÎÎÎÙ Û

ÃÎÎÎÎÎÎÎÎÎÎÏÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÝ

ÛPostScriptÛÚÎÎÎÎÎÎÎÎÎÎÂÎÎÎÎÎÌ Û

Û ÛÛExpressionÛspl Û Û

Û ÛÃÎÎÎÎÎÎÎÎÎÎÏÎÎÎÎÎÝ Û

Û ÛÛDependenceÛ{spl}Û Û

Û ÛÃÎÎÎÎÎÎÎÎÎÎÏÎÎÎÎÎÝ Û

Û ÛÛForced Û1 Û Û

Û ÛÀÎÎÎÎÎÎÎÎÎÎÁÎÎÎÎÎÙ Û

ÀÎÎÎÎÎÎÎÎÎÎÁÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÎÙ

Note that again the refresh has been forced, this time because we are passing the name of the spool file, which may not change even though the content could be completely different.

Operating a Progress Bar
One of the namespaces you may have noticed in the Causeway workspace is #.GuiPbar. This contains the functions to set up a simple progress bar, update it as required and check for a cancel request from the user. To start a new progress bar, simply:


 #.GuiPbar.Open'Preparing Sales Summary'
 #.GuiPbar.Set 0 total

... where total is the number you will be counting up to. If you leave this out, the default is 100, and the progress bar will show progress as a percentage.

The effect is to make a form centred on the screen with the initial state:


To get rid of it (pressing the Cancel button has no effect):


#.GuiPbar.Close

... will remove the form. To update it (remember that it cannot be in a ŒDQ as your application will wait for it and stop processing!) you call:


#.GuiPbar.Set 23

... and to check if the user hit the Cancel button:


cancel„#.GuiPbar.State

To see what it might look like in context, here is the complete function to build a big report, but allow the user to abandon processing mid-way:


PG„make_report n;m;sect;mat
© Generate random report of n sections of up to m rows
 #.GuiPbar.Open'Preparing Sales Summary'
 #.GuiPbar.Set 1 n
 #.leaf.Use''
 'heading'#.leaf.Place'Sales Report for 1998'
© Set up table properties
 #.leaf.table.Titles'Product' 'Description' 'Sales' 'Units' 'Value'
 #.leaf.table.TDepth 0 0 0 1 1
 #.leaf.table.Align'left' 'left' 'dec' 'dec2'
 #.leaf.table.TAlign'left' 'left' 'center' 'right' 'right'
 #.leaf.table.Qfmt'' '' 'I4' 'F7.2'
 #.leaf.table.Spacing 3 3
 #.leaf.table.Gridlines 1 0.4 0
 :For sect :In ¼n
   m„?23
   :If #.GuiPbar.State ª :Leave ª :End
   #.GuiPbar.Set sect n
   'subhead'#.leaf.Place'Section ',•sect
   mat„m 2½'Widgets' 'High class widgets' 'Sprockets' 'Cheap bits'
   mat„mat,?m 2½123 2345
   #.leaf.table.List mat
   #.leaf.Group 2
:End
 #.GuiPbar.Close
 PG„#.leaf.Spool''

Printing the Previewed Page
NewLeaf supports several methods of printing (and clipboarding) the previewed page. The simple option is just to add an SL action to the Print button to call the standard Print function:


:SL: :#.PostScrp.Print spl::

This will print the entire document on the default printer with the orientation shown in the preview. Note that this may be different from the default printer setup. You can also choose the page numbers to be printed (call a suitable dialogue box to ask the user) and pass a vector of page numbers as the left argument of PostScrp.RunOff, which will otherwise print the entire spool file:


:SL: :req PostScrp.RunOff PG::

Some other possibilities which you might consider offering the user are:

All the printing commands assume you have a black and white printer, and will map coloured text to solid black. If you want coloured output, set the global flag PostScrp.‘rgb to 1; you might also like to adjust the RGB mappings which you will find in PostScrp.‘chrgb. If you change either of these, make sure to take a copy so that you can re-apply your changes having upgraded the PostScrp namespace.



Continue to: Adding a New Class
© Copyright Causeway Graphical Systems Ltd 2001