[How to] How to make Unicode editor

Demos, code samples. Only questions related to the existing topics are allowed here.
Post Reply
Sergey Tkachenko
Site Admin
Posts: 17602
Joined: Sat Aug 27, 2005 10:28 am
Contact:

[How to] How to make Unicode editor

Post by Sergey Tkachenko »

This information is about TRichView versions prior to 11. Information about TRichView 11 and newer is in the next post.

1) Set Unicode property to True for all TextStyles in TRVStyle
2) Set RichViewEdit1.RTFReadProperties.UnicodeMode = rvruOnlyUnicode
3) Some methods cannot be used if the document is in Unicode.
- Add, AddFmt, AddNL, AddNLTag, AddTextBlockNL (use AddNLATag or AddNLWTag instead)
- AddTextNL (use AddTextNLA or AddTextNLW instead);
- SetItemText (use SetItemTextA or SetItemTextW instead)
- SetItemTextEd (use SetItemTextEdA or SetItemTextEdW instead)
- SetCurrentItemText (use SetCurrentItemTextA or SetCurrentItemTextW
instead)
4) Existing non-Unicode RVF documents must be converted to Unicode by
calling ConvertToUnicode after loading them (see below).
It's safe to call this procedure for Unicode documents - it will do nothing.

Code: Select all

uses CRVData, RVItem, RVUni;

procedure ConvertRVToUnicode(RVData: TCustomRVData);
var i,r,c, StyleNo: Integer;
    table: TRVTableItemInfo;
begin
  for i := 0 to RVData.ItemCount-1 do begin
    StyleNo := RVData.GetItemStyle(i);
    if StyleNo>=0 then begin
      if not RVData.GetRVStyle.TextStyles[StyleNo].Unicode then begin
        RVData.SetItemText(i, RVU_GetRawUnicode(RVData.GetItemTextW(i)));
        Include(RVData.GetItem(i).ItemOptions, rvioUnicode);
      end;
      end
    else if RVData.GetItemStyle(i)=rvsTable then begin
      table := TRVTableItemInfo(RVData.GetItem(i));
      for r := 0 to table.Rows.Count-1 do
        for c := 0 to table.Rows[r].Count-1 do
          if table.Cells[r,c]<>nil then
            ConvertRVToUnicode(table.Cells[r,c].GetRVData);
    end;
  end;
end;

procedure ConvertToUnicode(rv: TCustomRichView);
var i: Integer;
begin
  ConvertRVToUnicode(rv.RVData);
  for i := 0 to rv.Style.TextStyles.Count-1 do
    rv.Style.TextStyles[i].Unicode := True;
end;
Last edited by Sergey Tkachenko on Thu Dec 11, 2008 12:09 pm, edited 1 time in total.
Sergey Tkachenko
Site Admin
Posts: 17602
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

This information is about TRichView versions prior to 17.3. Information about TRichView 17.3 and newer is in the next post.

How to make Unicode editor in TRichView v11 or newer in Delphi 4-2007

1) Set Unicode property to True for all TextStyles in TRVStyle.
2) Set RichViewEdit1.RTFReadProperties.UnicodeMode = rvruOnlyUnicode.
3) Methods working with text usually have 3 variants: default (e.g. SetItemText), A-method (e.g. SetItemTextA), W-method (e.g. SetItemTextW). All methods can be used for Unicode text, but to avoid losses and unnecessary conversions use W-methods.
4) Existing non-Unicode RVF documents must be converted to Unicode by calling ConvertToUnicode after loading them (see below).

Code: Select all

uses CRVData, RVItem, RVUni; 
// changes required for TRichView v11+ are marked with red
procedure ConvertRVToUnicode(RVData: TCustomRVData); 
var i,r,c, StyleNo: Integer; 
    table: TRVTableItemInfo; 
begin 
  for i := 0 to RVData.ItemCount-1 do begin 
    StyleNo := RVData.GetItemStyle(i); 
    if StyleNo>=0 then begin 
      if not RVData.GetRVStyle.TextStyles[StyleNo].Unicode then begin 
        RVData.SetItemText[color=red]R[/color](i, RVU_GetRawUnicode(RVData.GetItemTextW(i))); 
        Include(RVData.GetItem(i).ItemOptions, rvioUnicode); 
      end; 
      end 
    else if RVData.GetItemStyle(i)=rvsTable then begin 
      table := TRVTableItemInfo(RVData.GetItem(i)); 
      for r := 0 to table.Rows.Count-1 do 
        for c := 0 to table.Rows[r].Count-1 do 
          if table.Cells[r,c]<>nil then 
            ConvertRVToUnicode(table.Cells[r,c].GetRVData); 
    end; 
  end; 
end; 

procedure ConvertToUnicode(rv: TCustomRichView); 
var i: Integer; 
begin 
  ConvertRVToUnicode(rv.RVData); 
  for i := 0 to rv.Style.TextStyles.Count-1 do 
    rv.Style.TextStyles[i].Unicode := True; 
end; 
How to make Unicode editor in TRichView v11 or newer in Delphi 2009 or newer
1) Unicode property of text styles and RTFReadProperties.UnicodeMode property of TRichView have the proper values by default. If you convert old projects, check them.
2) Methods working with text usually have 3 variants: default (e.g. SetItemText), A-method (e.g. SetItemTextA), W-method (e.g. SetItemTextW). Default method work with String, so you can use them for Unicode text.
2) Existing RVF documents saved by applications compiled with older versions of Delphi are automatically converted to Unicode when loading.

For all versions of Delphi
There are some events having TRVRawByteString parameters: OnItemAction, OnReadHyperlink, OnRVDblClick, OnRVRightClick, OnSaveItemToFile, OnItemTextEdit, OnDrawStyleText.
These parameters contain text in internal representation. For Unicode text, this is a "raw Unicode", with each Unicode character represented by 2 adjacent 1-byte characters.
You can use this function to convert them to String:

Code: Select all

uses RVTypes, RVUni;
{$I RV_Defs.inc}
// Converting text from internal representation to String
function ConvertItemTextToString(const ItemText: TRVRawByteString;
  UnicodeItem: Boolean; CodePage: Cardinal): String;
begin
  {$IFDEF RVUNICODESTR} // <-- declared in RV_Defs.inc
  // Delphi 2009+: String is Unicode
  if UnicodeItem then
    Result := RVU_RawUnicodeToWideString(ItemText)
  else
    Result := RVU_RawUnicodeToWideString(
      RVU_AnsiToUnicode(CodePage, ItemText));
  {$ELSE}
  // Delphi 4-2007: String is ANSI
  if UnicodeItem then
    Result := RVU_UnicodeToAnsi(CodePage, ItemText)
  else
    Result := ItemText;
  {$ENDIF}
end;
ItemText - text to convert.
UnicodeItem - pass True if this text is from Unicode text item.
CodePage - code page for conversion, you can use RVStyle1.DefCodePage.

Additional information:
http://www.trichview.com/help/index.htm ... hview.html
Sergey Tkachenko
Site Admin
Posts: 17602
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: [How to] How to make Unicode editor

Post by Sergey Tkachenko »

Since version 17.3, all text in TRichView is Unicode.

There are still some method accepting ANSI string parameters (such as AddTextNLA), but these methods simply convert ANSI string to Unicode string internally.
Post Reply