Page 1 of 1

Adding special characters

Posted: Wed Jan 04, 2012 3:19 pm
by BennieC
Hi,
I wish to allow editors to add reference numbers to documents eg (123), in superscript and a special colour, at specific locations.
However, I would like to have the ability to display or not display such numbers. Is there an easy way to do this or must the document be parsed fully to allow or disallow display. Also, how do I insert a special character to identify the location of such text.
Bennie

Posted: Thu Jan 05, 2012 2:14 pm
by Sergey Tkachenko
The simplest solution is to mark this text as hidden (adding rvteoHidden in TextStyle.Options). They can be shown only if you include rvoShowHiddenText in RichViewEdit.Options.
This text will have a special dotted underline when displayed.

If you do not want to use a hidden text, then yes, you need to search and delete them. You can assign some special tag value to these items to distinguish them from the rest of text.

Posted: Fri Jan 06, 2012 10:55 am
by BennieC
Thanks.
I am struggling to identify positions.
I click in the document and use insertText(InsertedText,true) to place the reference text in the location. I now need to change the format of the reference text to superscript, red and hidden, but I can't locate the specific string. I assume I need to select it to change it but to select it (SetSelection) I need to know the Item number which I can't locate.
I assume I should do the following

Click in the document at the insert location
Insert the text string (InsertText)
Select the text string (SetSelection)
Change it's style (??)

And then when displaying it I need to toggle hidden on or off as desired

Is this more or less correct and how do I achieve this

Posted: Fri Jan 06, 2012 3:18 pm
by Sergey Tkachenko
It's simpler.
All inserted text use the style defined in RichViewEdit.CurTextStyleNo.
Assign it before calling InsertText.
Do you use RichViewActions?

Posted: Mon Jan 09, 2012 12:51 pm
by BennieC
OK, I have tried setting the style and it does work. However, when adding my new style, using textstyles.add, how do I know what the index of the addition is. I tried to use .count, but there appears to be an offset of 2 ie count would say there are 8 styles, but styleno 10 is the one I added.
Is there a default styleset?

Answer to the other question - yes I do use Richview actions as my editor is basically the sample as provided by you - for now at least.
Bennie

Posted: Mon Jan 09, 2012 1:59 pm
by Sergey Tkachenko
Collection items are indexed from 0 to Count-1, so, after calling TextStyles.Add, the new style has the index TextStyles.Count-1.

You need to make sure that you do not create a style identical to the style that already exists. Use TextStyles.FindSuchStyle to find an existing style with the required properties. Add a new style only if FindSuchStyle returns -1.
The example can be found in Demos\*\Editors\Editor 2\

If you use RichViewActions, you can use TrvActionFontEx instead of working directly with styles.
For example, you want to retain all the properties of the current text style, but make it all-caps, red, arial, 10pt:

Code: Select all

rvActionFontEx1.UserInterface := False;
rvActionFontEx1.ValidProperties := [rvfimFontName, rvfimSize, rvfimAllCaps, rvfimColor];
rvActionFontEx1.Font.Name := 'Arial';
rvActionFontEx1.Font.Size := 10;
rvActionFontEx1.Font.Color := clRed;
rvActionFontEx1.FontStyleEx := [rvfsAllCaps];
rvActionFontEx1.ExecuteTarget(RichViewEdit1);
rvActionFontEx1.UserInterface := True;

Posted: Tue Jan 10, 2012 1:43 pm
by BennieC
Well there is some progress. I can add a new style consistently. The next problem is that, when I use aTRichview component to display the edited text, it does not recognise the same style. I then proceeded to save the style after creation in a ini file but finding the style in the saved file always returns -1 and hence a new style is always created.

Code: Select all


unit EditUtils;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, ComCtrls, ExtCtrls, ImgList, ContNrs,
  RVEdit, RVStyle;

procedure CreateUserStyles(CurrStyle : integer; var AStyle: TRVStyle);
procedure GetRefFont(CurrStyle : integer; var FontInfo: TFontInfo;
                      var AStyle: TRVStyle; var StyleNo : integer);

var
  RefStyleNo : integer;    // The style number for references

implementation

uses L5_Globals;

procedure GetRefFont(CurrStyle : integer; var FontInfo: TFontInfo;
                      var AStyle: TRVStyle; var StyleNo : integer);
begin
  // creating text style with the desired attributes
  FontInfo := TFontInfo.Create(nil);
  try
    FontInfo.Assign(AStyle.TextStyles[CurrStyle]);
    FontInfo.FontName := 'Arial';
    FontInfo.Size := 12;
    FontInfo.Color := clBlue;
    FontInfo.SubSuperScriptType := rvsssSuperScript;
    // if such text style already exists, reusing it
    StyleNo := AStyle.TextStyles.FindSuchStyle(CurrStyle, FontInfo, RVAllFontInfoProperties);
  finally
    FontInfo.Free;
  end;
end;

procedure CreateUserStyles(CurrStyle : integer; var AStyle: TRVStyle);
var
  FontInfo : TFontInfo;
begin
  // First check if a Style.ini exists, if so load it, else create a new one and save it
  if FileExists(ApplicationPath + '\EdtStyle.ini') then
  begin
    AStyle.LoadINI(ApplicationPath + '\EdtStyle.ini', 'TextStyles');
    GetRefFont(CurrStyle, FontInfo, AStyle, RefStyleNo);
  end
  else
  begin
    GetRefFont(CurrStyle, FontInfo, AStyle, RefStyleNo);
  end;

  if RefStyleNo < 0 then
  try
    begin
      // if not, adding it
      AStyle.TextStyles.Add;
      RefStyleNo := AStyle.TextStyles.Count - 1;
      AStyle.TextStyles[RefStyleNo].Assign(FontInfo);
      AStyle.TextStyles[RefStyleNo].Standard := False;
    end;
  finally
    AStyle.SaveINI(ApplicationPath + '\EdtStyle.ini', 'TextStyles');
  end;

end;


end.
The above is called with

Code: Select all

     
      myEdStyle := TRVStyle.Create(self);
      myEdStyle := aRVComponent.Style;
      myEditor.RichViewEdit1.Style := myEdStyle;
      CreateUserStyles(myEditor.RichViewEdit1.CurItemStyle, myEdStyle);
      myEditor.RichViewEdit1.Format;

Hope my code is readable

Posted: Wed Jan 11, 2012 5:38 pm
by Sergey Tkachenko
Can you reproduce this problem in a simple project?
Send it to richviewgmailcom.