Recent

Author Topic: About text hightlighting  (Read 3522 times)

calebs

  • Full Member
  • ***
  • Posts: 121
About text hightlighting
« on: March 07, 2016, 11:11:20 pm »
Hello all, i've installed this component and tried to read forums and understand but i can't figure how to do this.
I want to use the component to show an informatory read-only window with customizable text and highlightning and possibility to copy the text, showing some text and color it and set bold characters but not like an text editor but with code using some tags like html.

Claryfying.
I assign to the component the next text:

Hello <b>all how</b> do you do?
nice to <co:clred>meet </co> you
this text should look <g>bigger</g>

and later, change all between <b> in bold or color between <co> or bigger text with <g> for example and later remove the tags from the text (i know how to do that, lol).
I quite don understand how to search or parse  the text component to change the attributes once the text is assigned to the control.

Any hint or advice? Thanks!

howardpc

  • Hero Member
  • *****
  • Posts: 3205
Re: About text hightlighting
« Reply #1 on: March 07, 2016, 11:20:43 pm »
Hello all, i've installed this component

What component? Is this a Lazarus (LCL) component, and if so which one, or a third party richedit control...or what?

calebs

  • Full Member
  • ***
  • Posts: 121
Re: About text hightlighting
« Reply #2 on: March 07, 2016, 11:43:26 pm »
Sorry, it was using richmemo, i thinked this forum is for that component

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2290
    • havefunsoft.com
Re: About text hightlighting
« Reply #3 on: March 08, 2016, 02:49:31 am »
@calebs, please forgive howardpc, he didn't pay attention that you created the thread under "richmemo" subforum.

Long-story short. You'll need to create your own html parser (which is not so hard, if you're looking into the limited set of tags). The component itself supports only RTF at the moment.

Since you need read-only component you could try to use THTMLPort.

But as for rich memo, if you need to change the formatting, you could use GetStyleRange method. It will return the length of the characters that are sharing the same style.
This allows you to loop through the whole text and change styles.

For example.
Code: [Select]
  i:=0;
  while not end_of_text do begin
    GetStyleRange(i, start_idx, chars_len);
    if isBold(start_idx) then
      MakeItalic(start_idx, chars_len);
    i=start_idx+chars_len;
  end;
OR, if you've an html parser ready, then you could reapply the parsed html to RichMemo using a different set of styles (some sort of self-written css).
« Last Edit: March 08, 2016, 02:58:58 am by skalogryz »
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2290
    • havefunsoft.com
Re: About text hightlighting
« Reply #4 on: March 08, 2016, 03:52:58 pm »
Here's an example! (to proof that this is an easy task)
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

calebs

  • Full Member
  • ***
  • Posts: 121
Re: About text hightlighting
« Reply #5 on: March 10, 2016, 12:25:32 am »
Here's an example! (to proof that this is an easy task)

Thanks , i've downloaded your project and runned fine. But when i try to use it on my unit

Code: Pascal  [Select]
  1. unit ventanatextomejorada;
  2.  
  3. {$mode objfpc}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  9.   ExtCtrls, Buttons, StdCtrls, RichMemo, richmemoml;
  10.  
  11. type
  12.  
  13.   { TForm2 }
  14.  
  15.   TForm2 = class(TForm)
  16.     BitBtn1: TBitBtn;
  17.     BitBtn2: TBitBtn;
  18.     BitBtn3: TBitBtn;
  19.     Panel1: TPanel;
  20.     RichMemo1: TRichMemo;
  21.     procedure BitBtn3Click(Sender: TObject);
  22.     procedure FormCreate(Sender: TObject);
  23.     procedure FormShow(Sender: TObject);
  24.   private
  25.     { private declarations }
  26.   public
  27.     texto : tstringlist;
  28.     procesar : boolean;
  29.     procedure TagFormat(Sender: TObject;  var atagName: string; tagattr: TStrings; var afont: TFontParams; var txt: string; var tagCloses: Boolean );
  30.     procedure EntReplace(Sender: TObject; var txt: string; tagsStack: TStrings);
  31.     { public declarations }
  32.   end;
  33.  
  34. var
  35.   ventex: TForm2;
  36.   tamano : integer;
  37.   colorf : string;
  38.   memon : TMemo;
  39.  
  40. implementation
  41.  
  42. { TForm2 }
  43.  
  44. procedure TForm2.TagFormat(Sender: TObject; var atagName: string; tagattr: TStrings; var afont: TFontParams; var txt: string; var tagCloses: Boolean);
  45. begin
  46.   if atagName='b' then
  47.     Include(afont.Style, fsBold)
  48.   else if atagName='i' then
  49.     Include(afont.Style, fsItalic)
  50.   else if atagName='s' then
  51.     Include(afont.Style, fsStrikeOut)
  52.   else if atagName='u' then
  53.     Include(afont.Style, fsUnderline)
  54.   else if atagName='h1' then begin
  55.     Include(afont.Style, fsBold);
  56.     afont.Size:=afont.Size*2;
  57.   end else if atagName='h2' then begin
  58.     Include(afont.Style, fsBold);
  59.     afont.Size:=round(afont.Size*1.5)
  60.   end else if atagName='pre' then begin
  61.     afont.Name:='Courier New'
  62.   end;
  63. end;
  64.  
  65. procedure TForm2.EntReplace(Sender: TObject; var txt: string;tagsStack: TStrings);
  66. begin
  67.   txt:=StringReplace(txt, '&lt;', '<', [rfReplaceAll, rfIgnoreCase]);
  68.   txt:=StringReplace(txt, '&gt;', '>', [rfReplaceAll, rfIgnoreCase]);
  69.   txt:=StringReplace(txt, '&quot;', '"', [rfReplaceAll, rfIgnoreCase]);
  70. end;
  71.  
  72. procedure TForm2.FormShow(Sender: TObject);
  73. begin
  74.   RichMemo1.Clear;
  75.   if procesar then begin
  76.     memon.Lines:=texto;
  77.     Parse(memon.Text, RichMemo1, @TagFormat, @EntReplace);
  78.   end;
  79. end;
  80.  
  81. procedure TForm2.FormCreate(Sender: TObject);
  82. begin
  83.   texto:=TStringList.Create;
  84.   texto.Clear;
  85.   tamano:=10;
  86.   colorf:='clblack';
  87.   procesar:=false;
  88.   memon:=TMemo.Create;
  89. end;
  90.  
  91. procedure TForm2.BitBtn3Click(Sender: TObject);
  92. begin
  93.   close;
  94. end;
  95.  
  96. initialization
  97.   {$I ventanatextomejorada.lrs}
  98.  
  99. end.        

it gives me this error:

ventanatextomejorada.pas(77,57) Error: Incompatible type for arg no. 4: Got "<procedure variable type of procedure(TObject;var ShortString;TStrings) of object;Register>", expected "<procedure variable type of procedure(TObject;var AnsiString;TStrings) of object;Register>"
Line 77 is the implementation of EntReplace.
BTW, i use a tstrlinglist variable to exchange the text, you use a memo object. I think that i can create a memo object in memory and paste the text but is a workaround, any advice to this?
Thanks!!

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2290
    • havefunsoft.com
Re: About text hightlighting
« Reply #6 on: March 10, 2016, 01:37:58 am »
it gives me this error:

ventanatextomejorada.pas(77,57) Error: Incompatible type for arg no. 4: Got "<procedure variable type of procedure(TObject;var ShortString;TStrings) of object;Register>", expected "<procedure variable type of procedure(TObject;var AnsiString;TStrings) of object;Register>"
Just add
Code: Pascal  [Select]
  1. {$H+}
next to {$mode objfpc}
And it should help with the issue.
or replace string with AnsiString in parameters of TagFormat and EntReplace of TForm1

BTW, i use a tstrlinglist variable to exchange the text, you use a memo object. I think that i can create a memo object in memory and paste the text but is a workaround, any advice to this?
Well, don't use TMemo at all :)
Code: Pascal  [Select]
  1. procedure TForm2.FormShow(Sender: TObject);
  2. begin
  3.   RichMemo1.Clear;
  4.   if procesar then begin
  5.     Parse(texto.Text, RichMemo1, @TagFormat, @EntReplace);
  6.   end;
  7. end;
  8.  
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz

calebs

  • Full Member
  • ***
  • Posts: 121
Re: About text hightlighting
« Reply #7 on: March 10, 2016, 11:15:40 pm »
It worked like a charm! Thank you for your support all!
I've thinked a way to add an atribute for color like color=clred but it was too much to think right now so i used directly predefined tags for color like <red> <green>.
I don't need too much colors.
Thanks again!