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.

NoteSetting a default font 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:

li_rtn = rte_1.InsertDocument  &
   ("c:\mydir\contacts.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.)

NoteDisplayOnly property must be 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:

ls_desc = dw_1.Object.prod_desc.Primary[1]
rte_1.PasteRTF(ls_desc)

NoteRich text and the clipboard 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:

RowFocusChanged event As row focus changes, the notes for the current row are pasted into the RichTextEdit control. The RowFocusChanged event has this script:

string ls_richtext

// Get the string from the call_notes column
ls_richtext = dw_1.Object.call_notes[currentrow]

// Prevent flicker
rte_1.SetRedraw(FALSE) 

// Replace the old text with text for the current row
rte_1.SelectTextAll()
rte_1.Clear()
rte_1.PasteRTF(ls_richtext)
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:

string ls_richtext
long l_currow
GraphicObject l_control

// Check whether RichTextEdit still has focus
// If so, don't transfer the text
l_control = GetFocus()

IF TypeOf(l_control) = RichTextEdit! THEN RETURN 0

// Prevent flicker
rte_1.SetRedraw(FALSE) 

// Store all the text in string ls_richtext
ls_richtext = rte_1.CopyRTF()

// Assign the rich text to the call_notes column
// in the current row
l_currow = dw_1.GetRow()
dw_1.Object.call_notes[l_currow] = ls_richtext
rte_1.SetRedraw(TRUE)

NoteLoseFocus and the toolbars 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:

rte_1.SaveDocument("c:\...\contacts.rtf", &
   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.