Text for the control
In the Window painter, you do not enter text in the control.
Instead, in your application you can programmatically insert text
or let the user enter text using the editing tools.
The Font tab page in the Properties view for a RichTextEdit
control lets you set default font characteristics for the control.
When the control first displays at runtime, and you include the
toolbar with a RichTextEdit control, the toolbar indicates the default
font characteristics that you selected on the Font tab page at design
time. Although the application user can change fonts at runtime,
or you can use PowerScript to change the font style, you can set
the default font at design time only.
Inserting text
From a file
If you have prepared a text file for your application, you
can insert it with the InsertDocument function.
The file can be rich text or ASCII:
1 |
li_rtn = rte_1.InsertDocument  &<br>   ("c:mydircontacts.rtf", FALSE, FileTypeRichText!) |
The boolean clearflag argument lets you
specify whether to insert the file into existing text or replace
it. If you want to include headers and footers from a document that
you insert, you must replace the existing text by setting the clearflag argument
to TRUE. (The InsertFile command
on the runtime pop-up menu is equivalent to the InsertDocument function
with the clearflag argument set to FALSE.)
You cannot insert a document into a rich text control when
the control’s DisplayOnly property is set to true. If you
try to do this, PowerBuilder displays a runtime error message.
From a database
If you have saved rich text as a string in a database, you can
use a DataStore to retrieve the text.
After retrieving data, paste the string into the RichTextEdit
control:
1 |
ls_desc = dw_1.Object.prod_desc.Primary[1]<br>rte_1.PasteRTF(ls_desc) |
The CopyRTF and PasteRTF functions
let you get rich text with formatting instructions and
store it in a string. If you use the clipboard by means of the Copy, Cut,
and Paste functions, you get the text only—the
formatting is lost.
Example of saving rich text in a database
Suppose you have a database table that records tech support
calls. Various fields record each call’s date, support
engineer, and customer. Another field stores notes about the call.
You can let the user record notes with bold and italic formatting
for emphasis by storing rich text instead of plain text.
The window for editing call information includes these controls:
-
A DataWindow control that
retrieves all the data and displays everything except the call notes -
A RichTextEdit control that displays the call notes
-
A button for updating the database
RowFocusChanged event
As row focus changes, the notes for the current row are pasted
into the RichTextEdit control. The RowFocusChanged event has this
script:
1 |
string ls_richtext<br> <br>// Get the string from the call_notes column<br>ls_richtext = dw_1.Object.call_notes[currentrow]<br> <br>// Prevent flicker<br>rte_1.SetRedraw(FALSE) <br> <br>// Replace the old text with text for the current row<br>rte_1.SelectTextAll()<br>rte_1.Clear()<br>rte_1.PasteRTF(ls_richtext)<br>rte_1.SetRedraw(TRUE) |
LoseFocus event
When the user makes changes, the changes are transferred to
the DataWindow control. It is assumed that the user will click on
the button or the DataWindow control when the user is through editing,
triggering the LoseFocus event, which has this script:
1 |
string ls_richtext<br>long l_currow<br>GraphicObject l_control<br> <br>// Check whether RichTextEdit still has focus<br>// If so, don't transfer the text<br>l_control = GetFocus()<br> <br>IF TypeOf(l_control) = RichTextEdit! THEN RETURN 0<br> <br>// Prevent flicker<br>rte_1.SetRedraw(FALSE) <br> <br>// Store all the text in string ls_richtext<br>ls_richtext = rte_1.CopyRTF()<br> <br>// Assign the rich text to the call_notes column<br>// in the current row<br>l_currow = dw_1.GetRow()<br>dw_1.Object.call_notes[l_currow] = ls_richtext<br>rte_1.SetRedraw(TRUE) |
A LoseFocus event occurs for the RichTextEdit control even
when the user clicks a RichTextEdit toolbar. Technically, this is
because the toolbars are in their own windows. However, the RichTextEdit
control still has focus, which you can check with the GetFocus function.
Saving rich text in a file
You can save the rich text in the control, with the input
field definitions, with the SaveDocument function.
You have the choice of rich text format (RTF) or ASCII:
1 |
rte_1.SaveDocument("c:...contacts.rtf", &<br>   FileTypeRichText!) |
SaveDocument does not save the data in
the input fields. It saves the document template.
Does the file exist?
If the file exists, calling SaveDocument triggers
the FileExists event. In the event script, you might ask users if
they want to overwrite the file.
To cancel the saving process, specify a return code of 1 in
the event script.
Are there changes that need saving?
The Modified property indicates whether any changes have been
made to the contents of the control. It indicates that the contents
are in an unsaved state. When the first change occurs, PowerBuilder
triggers the Modified event and sets the Modified property to TRUE.
Calling SaveDocument sets Modified to FALSE,
indicating that the document is clean.
Opening a file triggers the Modified event and sets the property
because the control’s contents changed. Usually, though,
what you really want to know is whether the contents of the control
still correspond to the contents of the file. Therefore, in the
script that opens the file, you can set the Modified property to FALSE yourself.
Then when the user begins editing, the Modified event is triggered
again and the property is reset to TRUE.