Combining HypertextLinks and TextFormating in TRichviewEdt

General TRichView support forum. Please post your questions here
Post Reply
ENatter
Posts: 3
Joined: Mon Jul 12, 2010 4:05 pm

Combining HypertextLinks and TextFormating in TRichviewEdt

Post by ENatter »

Since I wan't to integrate in my Delphi Application a TRichViewEdit, that allows to integrate Hyperlinks like in the Demo "CreateHyperlink" and TextFormating (Bold, Italic, Underline) like in the Demo "Editor 2" I got to the Point where the declaration of constant redefine actions:

Code: Select all

const
  TEXT_BOLD       = 1;
  TEXT_ITALIC     = 2;
  TEXT_UNDERLINE  = 3;
  CONVERT_TO_HYPERTEXT    = 1;
  CONVERT_TO_NONHYPERTEXT = 2;
So every Bold is going to be a link. Probably solution is easy but currently I don't have a clue?!

Please help....
Sergey Tkachenko
Site Admin
Posts: 17569
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

How to combine these two demos.
1) All constants used for calls of ApplyStyleConversion must be unique.
Since "Editor 2" already uses constants from 1 to 8:

Code: Select all

  TEXT_BOLD       = 1;
  TEXT_ITALIC     = 2;
  TEXT_UNDERLINE  = 3;
  TEXT_APPLYFONTNAME  = 4;
  TEXT_APPLYFONT      = 5;
  TEXT_APPLYFONTSIZE  = 6;
  TEXT_COLOR      = 7;
  TEXT_BACKCOLOR  = 8;
redefine

Code: Select all

  CONVERT_TO_HYPERTEXT    = 9; 
  CONVERT_TO_NONHYPERTEXT = 10;


2) Add these new constants to TForm1.rveStyleConversion (of "Editor 2" demo). I copied code from TForm1.GetNonHypertextStyleNo and TForm1.GetHypertextStyleNo directly in this event:

Code: Select all

procedure TForm1.rveStyleConversion(Sender: TCustomRichViewEdit; StyleNo,
  UserData: Integer; AppliedToText: Boolean; var NewStyleNo: Integer);
var FontInfo: TFontInfo;
begin
  FontInfo := TFontInfo.Create(nil);
  try
    FontInfo.Assign(rvs.TextStyles[StyleNo]);
    case UserData of
      TEXT_BOLD:
        if btnBold.Down then
          FontInfo.Style := FontInfo.Style+[fsBold]
        else
          FontInfo.Style := FontInfo.Style-[fsBold];
      TEXT_ITALIC:
        if btnItalic.Down then
          FontInfo.Style := FontInfo.Style+[fsItalic]
        else
          FontInfo.Style := FontInfo.Style-[fsItalic];
      TEXT_UNDERLINE:
        if btnUnderline.Down then
          FontInfo.Style := FontInfo.Style+[fsUnderline]
        else
          FontInfo.Style := FontInfo.Style-[fsUnderline];
      TEXT_APPLYFONTNAME:
        FontInfo.FontName := FontName;
      TEXT_APPLYFONTSIZE:
        FontInfo.Size     := FontSize;
      TEXT_APPLYFONT:
        FontInfo.Assign(fd.Font);
      TEXT_COLOR:
        FontInfo.Color := cd.Color;
      TEXT_BACKCOLOR:
        FontInfo.BackColor := cd.Color;
[color=red]      CONVERT_TO_HYPERTEXT:
        begin
          FontInfo.Color := clBlue;
          FontInfo.Style := FontInfo.Style + [fsUnderline];
          FontInfo.Jump  := True;
        end;
      CONVERT_TO_NONHYPERTEXT:
        begin
          FontInfo.Color := clWindowText;
          FontInfoStyle := FontInfo.Style - [fsUnderline];
          FontInfo.Jump  := False;
        end;[/color]
      // add your code here....
    end;
    NewStyleNo := rvs.TextStyles.FindSuchStyle(StyleNo,FontInfo,RVAllFontInfoProperties);
    if NewStyleNo=-1 then begin
      rvs.TextStyles.Add;
      NewStyleNo := rvs.TextStyles.Count-1;
      rvs.TextStyles[NewStyleNo].Assign(FontInfo);
      rvs.TextStyles[NewStyleNo].Standard := False;
    end;
  finally
    FontInfo.Free;
  end;
end;
Now you can use commands from "CreateHyperlink" demo to add hyperlinks.
ENatter
Posts: 3
Joined: Mon Jul 12, 2010 4:05 pm

Post by ENatter »

Thanks a lot, this solves the Issue. But I've encountered two additional things.

1. While exporting with SaveHTML I always get complete HTML-Files. Means I also have a HTML Head and Body Tags. But I only wan't the pure HTML-Code between the BODY-Tags. I wrote a small Routine cropping the rest off, but isn't there a chance to get only the Part converted without HTML around it?

2. When adding a link like in the Example, and exporting it as HTML (see 1.) I have the Links formated Blue and Underlined. I understand that these Formating are neccessary in Editor to display Links, but oi they have to get exported??
Sergey Tkachenko
Site Admin
Posts: 17569
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

1) Include rvsoMiddleOnly in the Options parameter of SaveHTML/SaveHTMLEx

2) The current version of TRichView always export all font attributes of hyperlinks. Probably, an option will be added in future.
ENatter
Posts: 3
Joined: Mon Jul 12, 2010 4:05 pm

Post by ENatter »

To 1: Thanks a lot he solution. Only on thing, while editing you get a font tag (font size="2" face="Microsoft San Serif" into the generated HTML. Sure this is the font used in the Edit, but I don't think that this should be in the final HTML

To 2: Well I think for a proper HTML-Export this shouldn't be in that way
Sergey Tkachenko
Site Admin
Posts: 17569
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

1) You can include rvsoDefault0Style in the Options parameter.
With this option, properties of TextStyles[0], including font size and name, will not be written in HTML. For other text styles, their properties will be written in HTML only if they are different from properties of TextStyles[0].

2) It's arguable. In this way, the output does not depend on the browser settings (links are not necessary blue and underlined by default) and looks like in the editor.
Post Reply