[Unit] Adding a table of contents
Posted: Thu Mar 18, 2010 5:54 pm
Overview of "table of context" solutions for TRichView and ScaleRichView:
http://www.trichview.com/forums/viewtop ... 702#p34702
Unit RVTOC.pas:
It has the functions:
These procedures add a table of contents (TOC) to RichView.
Version for TRVPrint
TOC is added in rv.
TOC is inserted in the position specified in ItemNo.
Possible values of ItemNo:
* 0 - inserting at the beginning of rv;
* rv.ItemCount - inserting at the end of rv
* any value from 1 to rv.ItemCount-1 - inserting before the item with this index; this item must have a page break (i.e. rv.PageBreaksBeforeItems[ItemNo] must be True), otherwise this function does nothing and returns False.
This function returns True on successful inserting. If the document does not have headings with levels in range 1..Depth, this function does nothing, but still returns True.
RVPrint must be formatted before the call of this function. After the call, it is not formatted (call RVPrint.FormatPages again before printing).
Rv is not formatted after the call of this function (call rv.Format before displaying it).
Title is added before the TOC, using TitleStyleNo and TitleParaNo styles. Depth is a maximal heading level for TOC.
TextStyleNos and ParaStyleNos are arrays containing styles for TOC. They must have Depth items.
TitleStyleNo[0] and TitleParaNo[0] are used for adding heading level 1 in TOC,
TitleStyleNo[1] and TitleParaNo[2] are used for adding heading level 2 in TOC,
and so on.
TOC is added not as an editing operation! Undo is not possible. If called for TRichViewEdit, call ClearUndo method.
// TitleStyleNo, TextStyleNos[] are indices in rv.Style.TextStyles collection.
// TitleParaNo, ParaStyleNos[] are indices in rv.Style.ParaStyles collection.
TOC is added as Unicode strings in Delphi 2009-2010, and as ANSI strings in older version of Delphi (so foreign characters may be lost on conversion)
Version for TRVReportHelper
In this version of the function, the document is contained in RVReportHelper.RichView.
Additional parameters:
Canvas - a canvas that was used for RVReportHelper.Init(), or another canvas with the same resolution;
PageWidth - width of pages (used in Init).
FirstPageHeight - height of the first page (used in FormatNextPage)
PageHeight - height of other pages (used in FormatNextPage)
These parameters are used only if ItemNo<RVReportHelper.RichView.ItemCount (i.e. if TOC is added not to the end). If the TOC is added to the end, you can pass any values to these parameters.
RVReportHelper must be formatted before the call of this function.
After the call, it is not formatted.
Additional procedure
This procedure searches for the first occurence of the paragraph with OutlineLevel=Level, and returns:
- the style of this paragraph (in ParaNo),
- the style of text in this paragraph (in StyleNo).
Paragraphs without text are ignored. If there is no such paragraph, this procedure returns 0, 0.
These values can be used as TitleStyleNo, TitleParaNo parameters for AddTableOfContents.
http://www.trichview.com/forums/viewtop ... 702#p34702
Unit RVTOC.pas:
It has the functions:
Code: Select all
function AddTableOfContents(rv: TCustomRichView; RVPrint: TRVPrint;
Depth: Integer; const TextStyleNos, ParaStyleNos: array of Integer;
ItemNo: Integer; const Title: TRVUnicodeString; TitleStyleNo, TitleParaNo: Integer): Boolean; overload;
function AddTableOfContents(RVReportHelper: TRVReportHelper; Canvas: TCanvas;
PageWidth, FirstPageHeight, PageHeight: Integer;
Depth: Integer; const TextStyleNos, ParaStyleNos: array of Integer;
ItemNo: Integer; const Title: TRVUnicodeString; TitleStyleNo, TitleParaNo: Integer): Boolean; overload;
Version for TRVPrint
TOC is added in rv.
TOC is inserted in the position specified in ItemNo.
Possible values of ItemNo:
* 0 - inserting at the beginning of rv;
* rv.ItemCount - inserting at the end of rv
* any value from 1 to rv.ItemCount-1 - inserting before the item with this index; this item must have a page break (i.e. rv.PageBreaksBeforeItems[ItemNo] must be True), otherwise this function does nothing and returns False.
This function returns True on successful inserting. If the document does not have headings with levels in range 1..Depth, this function does nothing, but still returns True.
RVPrint must be formatted before the call of this function. After the call, it is not formatted (call RVPrint.FormatPages again before printing).
Rv is not formatted after the call of this function (call rv.Format before displaying it).
Title is added before the TOC, using TitleStyleNo and TitleParaNo styles. Depth is a maximal heading level for TOC.
TextStyleNos and ParaStyleNos are arrays containing styles for TOC. They must have Depth items.
TitleStyleNo[0] and TitleParaNo[0] are used for adding heading level 1 in TOC,
TitleStyleNo[1] and TitleParaNo[2] are used for adding heading level 2 in TOC,
and so on.
TOC is added not as an editing operation! Undo is not possible. If called for TRichViewEdit, call ClearUndo method.
// TitleStyleNo, TextStyleNos[] are indices in rv.Style.TextStyles collection.
// TitleParaNo, ParaStyleNos[] are indices in rv.Style.ParaStyles collection.
TOC is added as Unicode strings in Delphi 2009-2010, and as ANSI strings in older version of Delphi (so foreign characters may be lost on conversion)
Version for TRVReportHelper
In this version of the function, the document is contained in RVReportHelper.RichView.
Additional parameters:
Canvas - a canvas that was used for RVReportHelper.Init(), or another canvas with the same resolution;
PageWidth - width of pages (used in Init).
FirstPageHeight - height of the first page (used in FormatNextPage)
PageHeight - height of other pages (used in FormatNextPage)
These parameters are used only if ItemNo<RVReportHelper.RichView.ItemCount (i.e. if TOC is added not to the end). If the TOC is added to the end, you can pass any values to these parameters.
RVReportHelper must be formatted before the call of this function.
After the call, it is not formatted.
Additional procedure
Code: Select all
procedure GetStylesForOutlineLevel(rv: TCustomRichView; Level: Integer;
var StyleNo, ParaNo: Integer);
- the style of this paragraph (in ParaNo),
- the style of text in this paragraph (in StyleNo).
Paragraphs without text are ignored. If there is no such paragraph, this procedure returns 0, 0.
These values can be used as TitleStyleNo, TitleParaNo parameters for AddTableOfContents.