• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 821
  • Last Modified:

richedit / rtf: how to format text by hand? (fast coloring)

hello there, i'm really desperate about this one: i want to write a logfile viewer which should provide highlighting of specific words. so far so good, i know how to do it (CHARFORMAT2 etc.). but the logs i deal with become _huge_ (several MBs), so that loading them into the richedit control and then parsing them using the control's functions (even selstart, selend, selcolor) is by far too slow to use them.
so my idea would be to do the coloring myself, formatting the rtf sourcecode. there are very fast (assembler optimized) string controls out there, i grabbed one (faststrings) and it fulfils my needs.

the problem is the richedit control itself, it's making me feel dizzy... i need to copy the RAW rtf control codes to a buffer, then modify it and write it back (as RAW rtf data), the final goal is a richedit text with nicely colored words.

i learnt the rtf format, it's quite easy. at the beginning of the file, you can insert a colortable with all colors you need (or extend an existing one), in the file you can write in a specific color with \cfX where X is the index in the colortable, get back to normal vision using \cf0.
so this is not the problem.

but however i try to do it, i can't get the pure raw rtf text from a richedit control, than modify it and send it back...

help is really appreciated! thank you so much in advance!
gx, buggy
0
bugbyter
Asked:
bugbyter
  • 4
  • 3
1 Solution
 
bugbyterAuthor Commented:
hello there, i finally managed to get it done... but still really lacks in performance :/
here how i do it:

function GetRTFText(ARichEdit: TRichedit): string;
var
    ss: TStringStream;
    emptystr: string;
begin
    emptystr := '';
    ss := TStringStream.Create(emptystr);
    try
        ARichEdit.PlainText := False;
        ARichEdit.Lines.SaveToStream(ss);
        Result := ss.DataString;
    finally
        ss.Free
    end;
end;

procedure SetRTFText(ARichEdit: TRichedit; stringo: string);
var
    ss: TStringStream;
    emptystr: string;
begin
    emptystr := '';
    ss := TStringStream.Create(stringo);
    try
        ARichEdit.PlainText := False;
        ARichEdit.Lines.LoadFromStream(ss);
    finally
        ss.Free
    end;
end;

procedure TForm1.Button10Click(Sender: TObject);
var s: string;
begin
    richedit1.Lines.BeginUpdate;
    RichEdit1.Lines.LoadFromFile(ExtractFilePath(ParamStr(0)) + 'bla.txt');
    s := richedit1.Text;
    richedit1.Text := fastreplace(s, 'item', 'STARTAitemENDA');
    s := GetRTFText(RichEdit1);
    s := fastreplace(s, 'STARTA', '\ul\b ');
    s := fastreplace(s, 'ENDA', '\b0\ul0 ');
    SetRTFText(richedit1, s);
    richedit1.Lines.EndUpdate;
end;

...at least i can treat some 100kbs like this... but still to slow. does anybody have any better idea on how to solve this? how do coding IDEs solve this? buffering? just coloring the visible parts? please tell me how you think it might be done... :)

thx!
gx, buggy
0
 
bugbyterAuthor Commented:
oh, and sorry for the fastreplace - it's a unit i found in the web and i didn't manage to find out where the standard "replace" function hides so i couldn't rewrite it. but it should only be a matter of seconds if you know how ;)
0
 
bugbyterAuthor Commented:
oh, and sorry for the fastreplace - it's a unit i found in the web and i didn't manage to find out where the standard "replace" function hides so i couldn't rewrite it. but it should only be a matter of seconds if you know how ;)

btw this example searches for the string 'item' and makes all occurences bold and underlined.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
loop_untilCommented:
Well, you could first not load the RTF in the richedit at the start because you don't need it shown *before* it is parsed aren't you? The raw RTF codes are visible in plain text.

This might work: just load your plain text log file using a TMemoryStream...

var
  TempStream: TMemoryStream;
begin
  TempStream := TMemoryStream.Create;
  try
    TempStream.LoadFromFile('thelog.txt');

    // Insert the RTF header here...

    // Parse here...

    // Insert the RTF footer here...

    ARichEdit.Lines.LoadFromStream(TempStream);
  finally
    TempStream.Free;
  end;
end;


Something like that. Note that it has not been tested so you'll have to work it out a little bit. It's been a while since I've played with RTF and I don't have Delphi nearby at the moment.

Hope it helps.
Have a nice day! :-)
0
 
loop_untilCommented:
0
 
loop_untilCommented:
By the way, your project seems really nice. Hope it'll work for you!
0
 
bugbyterAuthor Commented:
hello, i'll give you the points as nobody else helps anyway ;) for all others of you out there: there is an OPENSOURCE project (c source) of a code editing control which is not only generic in means of syntax coloring but also takes care of so much more. search&replace, macros, margin, even FOLDING (make lines invisible to make code better readable) is all supported by this thingy. there is even a GTK+ version for your linux/unix needs out there. every new version brings even more features to this fantastic library. you can also link it with your prog statically so that you don't have to ship an extra dll with it. i think it's a really amazing piece of work and everybody trying to do something like this should look here first:
http://www.scintilla.org/

have a nice day,
gx, buggy
0
 
CleanupPingCommented:
bugbyter:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now