Kmemo Manual
Kmemo Manual
Kmemo Manual
Contents
1
Introduction 1
Compatibility issues
Creating documents
Clipboard operations
7
8
1 Introduction
KMemo is native rich edit control written in Object Pascal. It has been written
from scratch. It replaces the standard TMemo/TRichedit controls from Delphi
and works both under Delphi and Lazarus on every platform with the same
features. It unifies the behavior of different rich edit controls available in
different OSes.
This document describes only important features of KMemo. For more
detailed info see the help file or study the demos.
All used code snippets are present in the KMemoDemo example.
2 Compatibility issues
KMemo is not compatible with Delphi TRichEdit control. This is because
KMemo uses its own, very powerful concept of document skeleton, which is
built on TKMemoBlocks containers and TKMemoBlock objects. Once you
decide to abandon TRichEdit and replace it by TKMemo, you do your work
only once and then, if you use your project in different IDE or OS, the code
stays as is. Porting from TRichEdit should not be very difficult, because
KMemo is really easy to use.
3 Creating documents
One of the most important features of KMemo is the very simple way of
creating documents directly with Object Pascal code. You dont need to
create some intermediate documents to show it in KMemo. You just build up
your document by adding text blocks, image blocks, tables etc.
Note: Once you have your document created by code and let the
user to edit it, the block pointers referred by snippets in chapters
below cannot be used anymore. This is because the user may insert
new blocks, delete selections, split text blocks as a result of
formatting actions etc.
In the above example, a simple text block is added at the end of the
document. If you want clear everything and add new text use following:
procedure TMainForm.Test2;
begin
KMemo1.Blocks.Clear;
KMemo1.Blocks.AddTextBlock('Hello world!');
end;
Please note each such operation will force the KMemo update which is often a
time consuming operation, especially for large documents. For such cases,
update locking has been introduced. For sequential operations on KMemo
Above example will show red bold text in KMemo, its font will be Arial. Many
text formatting features are available.
Above example will center the paragraph in KMemo and the paragraph will
have an additional space of 20 points at the bottom. Also for paragraphs
many formatting features are available.
procedure TMainForm.Test7;
var
TB: TKMemoTextBlock;
PA: TKMemoParagraph;
begin
TB := KMemo1.Blocks.AddTextBlock('Hello world!');
PA := KMemo1.Blocks.AddParagraph;
PA.Numbering := pnuArabic;
end;
Above example turns on Arabic numbering for the paragraph. Many list styles
are available, but an empty document has no list styles defined (in the above
example, the numbering assignment automatically creates a new list for the
document if the KMemo was empty).
Creating a more complex numbering shows the Test8 method in the demo.
Above example adds the image to KMemo. The image will be positioned in
text flow in original size. To change the position to allow text to float around
the image just use:
procedure TMainForm.Test10;
var
IB: TKMemoImageBlock;
begin
IB := KMemo1.Blocks.AddImageBlock('image.png');
IB.Position := mbpRelative;
IB.LeftOffset := 50;
end;
Above example will set relative position to the image. The image anchor
stays where the image was originally inserted. The LeftOffset property moves
the image 50 points to the right of its anchor. Similar meaning has the
TopOffset property.
Note: Containers should always have fixed width. Otherwise they will
be resized when KMemos ClientWidth changes. For nonzero
LeftOffset it means the container would reach beyond KMemo right
edge, causing the horizontal scrollbar to appear.
Above example adds a table to KMemo. It contains two rows and two
columns. Each cell contains one text box. The table border has default
properties and width of 1 point. Specifying the border width to CellStyle
property does not alone update the table, to do this you must call
ApplyDefaultCellStyle.
Creating a more complex table shows the Test13 method in the demo.
KMemo1.ParaStyle.HAlign := halCenter;
end;
Above example sets default font to Arial and 20 point size. Default
paragraphs will have centered alignment. Setting default properties will
reformat the entire document but it affects only text blocks and/or
paragraphs. Furthermore, only blocks whose styles were not explicitly
changed in code or edited by user will be reformatted.
5 Clipboard operations
KMemo supports clipboard operations. These work with both with simple
TEXT format and with the RICH TEXT FORMAT defined by Microsoft. Clipboard
operations can be called programmatically:
procedure TMainForm.Test17;
begin
KMemo1.ExecuteCommand(ecSelectAll);
KMemo1.ExecuteCommand(ecCopy);
end;
Above example sets custom text and paragraph style to the selection.
Attributes can also be read. In such case the attributes are returned, which
correspond to caret position or selection end position in the innermost
container.
KPrintPreviewDialog1.Execute;
end;
To print the document with page and printer setup, drop a TKPrintSetupDialog
component to a form and then use this code:
procedure TMainForm.Test20;
begin
KPrintSetupDialog1.Control := KMemo1;
KPrintSetupDialog1.Execute;
end;