Mastering PowerBuilder

HomePrevious Lesson: Course 3:: Session 21 :: Page 240
Next Lesson: DataWindow Dot Notation - Error Handling
Programming RichTextEdit Control

If you want to learn about RichTextEdit control properties, please refer to RichTextEdit control under the 'Window Painter' section. This section concentrates only on the programming aspectes of RTE control.

Opening & Saving a Document in RTE Control

There is no function to open a file in RTE control; However, you can insert either an ASCII file or rich-text file into the RTE control. If you are inserting a RTF document make sure the version of RTF is below or equal to 1.2. PowerBuilder supports only 1.2 RTF version. That means, you can not insert a document produced in MS-Word 6 or 7 or 97. The following code fragment taken from the example application demonstrates inserting a document.

String ls_Path, ls_File
If GetFileOpenName("Select File", ls_Path, &
   ls_File, "rtf", "Rich Text (*.rtf), *.rtf") = 1 Then
   is_FileName = ls_Path
   // is_FileName is a instant string variable
   rte_1.InsertDocument(is_FileName, True, &
        FileTypeRichText!)
End If

In the above code we prompt the user for the RTF file name using GetFileOpenName() and open that document in the RTE control using InsertDocument(). The second argument to the InsertDocument() says whether you want to clear the content of RTE control before you insert the document or insert the document at the cursor location.

// is_FileName is a instance string variable
rte_1.
SaveDocument(is_FileName, FileTypeRichText!)

Call SaveDocument() to save the document from the RTE control. In the above example we are saving the document to the same document. If you want to save the file as another file and want to get the file name from the user you can call GetFileSaveName().

Previewing & Printing Document from RTE Control

Print previewing a RTE document is simpler than DataWindow print previewing. You need to just call Preview() for the RTE control.

rte_1.Preview( True )

Similar to the DataWindow print preview, at run-time user can't edit the document in the RTE control when the RTE control is in the preview mode. However, you can modify the document through scripting. If you want to check whether the RTE is in the preview mode call IsPreview().

Call Print() to print the document from RTE control.

rte_1.Print(1, "", True, True)

The first argument specifies the number of copies and second one says the page range. If you do not specify the page range it will print all the pages. If you need to find out the number of pages it is going to print, call PageCount().

Editing Document in RTE Control

Like any other editable control such as Multiline Edit control, RTE has a lot of editable functions such as Cut(), Copy(), Clear(), Paste(). You can call SelectAll() to select all the content of the RTE control. Similarly Undo() function also. If you enable the popup menu for the RTE control, all these options will display on the popup menu and PowerBuilder will automatically execute the appropriate operation. However, if you do not want to show the popup menu and want to control through script then you can use the above functions.

You can also search the RTE control using Find() and continue searching by calling FindNext(). Using these functions you can search in different directions i.e., forward/backward/all. You can make the search to find whole words only. You can also make the search case sensitive. These two functions Find() and FindNext() are not available on the RTE control popup menu. However FindAndReplace() function is missing for the RTE control. If you wish to provide this functionality to the user you need to write the code. The code you need to write is not that complex and you can even take the code from the example application. The following code finds and replaces all those that match the specified string. This code is taken from the Clicked event of the cb_ReplaceAll CommandButton on the 'w_rte_replace' window. You can find this window in the pbexamw3.pbl library.

Integer  li_Loc, li_Changes
irte_1.SetRedraw(False)
If ib_FindNext Then
   li_Loc = irte_1.FindNext()
Else
   li_Loc = irte_1.Find(sle_find.text, True, &
                   Not cbx_matchcase.Checked, &
                   cbx_wholeword.Checked, True )
   ib_FindNext = False
End If
Do While li_Loc > 0
   If irte_1.SelectedLength() > 0 And &
             ((cbx_matchcase.Checked And &
             irte_1.SelectedText() = sle_find.Text) Or &
             ((Not cbx_matchcase.Checked) And
             Lower(irte_1.SelectedText()) = &
             Lower(sle_find.Text))) Then
      irte_1.ReplaceText(sle_replace.Text )
      li_Changes++
   Else
      MessageBox("Replace", &
         "This is The Exit. The Find must have failed!!!!")
      Exit
   End If
   li_Loc = irte_1.FindNext()
Loop
irte_1.SetRedraw(True)
If li_Changes > 0 Then
   MessageBox("Replace", &
        String(li_Changes) + " replacements made.")
Else
    MessageBox("Replace", "No replacements made.")
End If

The above code is replacing all the occurrences of the specified string. Even though the code is so long the logic is very simple. The above code is calling Find() for the first time and calling FindNext() in a loop. As you might have seen in any word processor, when your search matches a string the matched string is selected and highlighted. Then calling ReplaceText() will replace that selected (highlighted) text.

Sharing DataWindow/DataStore with a RTE Control

You may recall that except for DataWindow and RTE controls all other window controls do not have data awareness. For example, you can't populate a ListBox or TreeView with data directly from the database unless you retrieve the data from the database separately and populate these controls in a loop. However in RTE, you can share DataWindow/DataStore fields with the RTE control input fields. The following code shares a DataStore with the RTE control by calling DataSource().

rte_1.DataSource( ids_DataStore )

Once you associate a DataWindow/DataStore with a RTE control, you can use the DataWindow/DataStore field names in the RTE control using InputFieldInsert(). As long as the field names in the DataWindow/DataStore and RTE control are same. the DataWindow/DataStore data is available in the RTE control.

When a RTE control is not associated with a DataWindow/DataStore, there will be only one instance of the document. However, when you associate a DataWindow/DataStore with the RTE control, PowerBuilder creates the document instances equal to the number of rows in the DataWindow/DataStore. The text you type or picture you insert in the document is repeated for each row in the DataWindow/DataStore. However, the content of the input fields in the RTE control reflects the values of DataWindow/DataStore.

All RTE document instances are exactly same except for the content of the input fields. It is similar to the mail merge you do in a word-processor. By default, PowerBuilder displays the content of the field in the RTE control however, you can choose to display the input field name instead of the input field value by setting 'InputFieldNamesVisible' property to TRUE.

References

The example application has a very good example that demonstrates all the features of RTE control. That's why we are using that example through out this topic instead of rebuilding a separate example. Please refer to the following

Window Objects: w_rte, w_rte_find, w_rte_replace, w_rte_mail_merge

Menu Objects: m_rte

HomePrevious Lesson: Course 3:: Session 21 :: Page 240
Next Lesson: DataWindow Dot Notation - Error Handling