?
Solved

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

Posted on 2003-02-28
8
Medium Priority
?
795 Views
Last Modified: 2013-11-22
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
Comment
Question by:bugbyter
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
8 Comments
 

Author Comment

by:bugbyter
ID: 8045108
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
 

Author Comment

by:bugbyter
ID: 8045124
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
 

Author Comment

by:bugbyter
ID: 8045130
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:loop_until
ID: 8046606
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
 
LVL 3

Expert Comment

by:loop_until
ID: 8046622
0
 
LVL 3

Accepted Solution

by:
loop_until earned 100 total points
ID: 8046624
By the way, your project seems really nice. Hope it'll work for you!
0
 

Author Comment

by:bugbyter
ID: 8120753
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
 

Expert Comment

by:CleanupPing
ID: 9317013
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month12 days, 6 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question