Database, TDBWPRichText

Top  Previous  Next

WPTools contains a data sensitive control, the TDBWPRichText. As usual, all you need to make it show the text in a blob field is to set the fieldname and the datasource property.

 

Although the format (ANSI, HTML, RTF ...) is detected automatically at load time, you probably want to control, the format in which the text is saved to the database. This is done by property TextSaveFormat. Property TextLoadFormat is used when loading text, it may be set to 'AUTO';

 

You can also change the code page which is used to save ansi text.

 

To load/save cyrillic ANSI texts use:

 

  DBWPRichText1.TextLoadFormat := 'ANSI-codepage1251';

  DBWPRichText1.TextSaveFormat := 'ANSI-codepage1251';

 

Please note that unless the property NoUpdateOnExit has been set to true the database will be updated when the editor looses the focus.

 

Important: Before you do any change to the text under program control, i.e. InputString, it is required to check with function Changing if the text may be edited. Changing returns false if the datafield or dataset is readonly.

 

Attention: If you make changes to the text using your code (InputString, AddTable etc) and do not call Changing before, this changes my not be saved to the database.

 

In case you work on selected images (CurrObj) use SaveChanging since calling Changing can cause the text to be reloaded which invalidates the selection.SaveChanging would return false after the text was reloaded.

 

 

When saving HTML please note, that images need to be in PNG or JPEG format to be embedded. If images are linked to files, they will never be embedded and maybe missing at load time. So it is necessary to compress images which are not compressed already. You can do this in the event PrepareImageForSaving.

 

But in general better save images in a separate database and load the only when needed. Otherwise you create a big network traffic and it is not possible to scroll through the data-records as wanted. Also see wpInstantDisplayAfterLoad in property ViewOptionsEx.

 

To avoid this, You can load liked images when they are painted using event OnTextObjectPaint.

 

procedure TForm.WPRichTextTextObjectPaint(Sender: TObject;

 pobj: TWPTextObj; toCanvas: TCanvas; XRes, YRes, X, Y, W, H, BASE: Integer;

 PageRef: TWPVirtPage; Modes: TWPTextObjectPaintModes;

const CanvasExtraAttr: TWPPaintExtraParams;

var ContinueMode: TWPTextObjectPaintResult);

var s : String;

begin

if (pobj.ObjType=wpobjImage) and (pobj.ObjRef=nil) then

begin

   s := pobj.Source; //  get the path to the image

  if (s<>'') and FileExists(s) then

  begin

    pobj.LoadObjFromFile(s);

    pobj.GetWHFromContents(1);

  end;

   ContinueMode := ContinueMode - [wpobjPaintRedCross];

end;

end;

 

Also note event OnRequestHTTPImage which is called during loading data to load images which are not embedded directly.

 

The TDBWPRichText inherits from TWPCustomRichText which is also the ancestor of TWPRichText.

Both classes inherit from TWPCustomRTFEdit. So to make code which works with any editor, you can use code like this:

 

if (SomeObject is TWPCustomRTFEdit) and TWPCustomRTFEdit(SomeObject).Changing then

   TWPCustomRTFEdit(SomeObject).InputString('some text');

 

 

It is also possible to avoid the TDBWPRichText and use a TWPCustomRTFEdit (created in code) or TWPRichText instead.

 

You will need to load and save the data to the database using at least two events of the data set to make it work:

 

BeforePost(...)

 SomeBlobfield.AsString := WPRichText.AsString;

 

AfterScroll(...)

 WPRichText.AsString := SomeBlobfield.AsString;

 

The advantages of this approach are:

- You have full control when to save.

- You can also switch the dataset to edit mode before you save.

- You can ask the user if he or she really wants to update the field.

- You can save a backup copy, (i.e. save one copy in HTML and one in WPT format)

- Save additional information like the cursor position in a separate field.

- It is also possible to only save a big datablock when it really was changed, and not any time the dataset switches from edit to browse state.

- Compress or encrypt the text before saving

- You also do not have to worry about function "Changing" mentioned above.