trichview.com Forum Index trichview.com
TRichView support forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

[Example] Modification of "Search and Mark" demo

 
Post new topic   Reply to topic    trichview.com Forum Index -> Examples, Demos
View previous topic :: View next topic  
Author Message
Sergey Tkachenko
Site Admin


Joined: 27 Aug 2005
Posts: 6599

PostPosted: Tue Sep 06, 2005 5:18 pm    Post subject: [Example] Modification of "Search and Mark" demo Reply with quote

"Search & Mark" - the last subdemo of the demo located in Demos\Delphi\Multidemo\.
It searches for the given word in text and marks all its occurences by applying the specified text style:
Code:
rve.SetSelectionBounds(0,rve.GetOffsBeforeItem(0), 0, rve.GetOffsBeforeItem(0));
while rve.SearchText(txt.Text, [rvseoDown]) do
  rve.ApplyTextStyle(sncomMarked);

It's good if you have only a fixed set of styles (like in this demo: two styles - for normal text and for marked text). But what if you need to mark text in arbitrary document? ApplyTextStyle ignores all attributes of the selected text, so it may have different font size and name after applying. The solution is using ApplyStyleConversion instead of ApplyTextStyle:
Code:
rve.SetSelectionBounds(0,rve.GetOffsBeforeItem(0), 0, rve.GetOffsBeforeItem(0));
while rve.SearchText(txt.Text, [rvseoDown]) do
  rve.ApplyStyleConversion(0);

// making the selected text white-on-green,
// other font attributes are not changed
procedure TfrmDemo7.rveStyleConversion(Sender: TCustomRichViewEdit;
  StyleNo, UserData: Integer; AppliedToText: Boolean;
  var NewStyleNo: Integer);
var FontStyle: TFontInfo;
begin
  FontStyle := TFontInfo.Create(nil);
  FontStyle.Assign(Sender.Style.TextStyles[StyleNo]);
  FontStyle.BackColor := clGreen;
  FontStyle.Color     := clWhite;
  NewStyleNo := Sender.Style.TextStyles.FindSuchStyle(StyleNo, FontStyle,
    RVAllFontInfoProperties);
  if NewStyleNo<0 then begin
    NewStyleNo := Sender.Style.TextStyles.Count;
    Sender.Style.TextStyles.Add;
    Sender.Style.TextStyles[NewStyleNo].Assign(FontStyle);
    Sender.Style.TextStyles[NewStyleNo].Standard := False;
  end;
end;
Back to top
View user's profile Send private message Visit poster's website
Sergey Tkachenko
Site Admin


Joined: 27 Aug 2005
Posts: 6599

PostPosted: Tue Feb 28, 2006 8:40 pm    Post subject: Reply with quote

And a new version of code marking all occurences of the given string in text.
It is:
1) very fast
2) works even in TRichView.
So this is an ideal function for applications which need to display multiple searching results.

2008-Dec-11: this code is removed because it obsolete. Use MarkSearch.pas from RichViewActions, see below.

This procedure cannot be undone. If you call it for TRichViewEdit, call RichViewEdit.ClearUndo.


Last edited by Sergey Tkachenko on Thu Dec 11, 2008 3:14 pm; edited 3 times in total
Back to top
View user's profile Send private message Visit poster's website
Pieter E.



Joined: 28 Aug 2005
Posts: 339

PostPosted: Mon May 29, 2006 6:35 pm    Post subject: Reply with quote

Is there a C++ example available of this last post?

Thanks.
Back to top
View user's profile Send private message
Sergey Tkachenko
Site Admin


Joined: 27 Aug 2005
Posts: 6599

PostPosted: Sat Jun 03, 2006 9:20 am    Post subject: Reply with quote

You can create a pascal unit with this function and include it in C++Builder project.
Back to top
View user's profile Send private message Visit poster's website
Sergey Tkachenko
Site Admin


Joined: 27 Aug 2005
Posts: 6599

PostPosted: Thu Jun 15, 2006 12:50 pm    Post subject: Reply with quote

Code of the last example is modified (LastPos implementation is changed)
Back to top
View user's profile Send private message Visit poster's website
syntetic



Joined: 20 Nov 2006
Posts: 1

PostPosted: Tue Nov 21, 2006 10:11 am    Post subject: Reply with quote

I'm not familiar Delphi. Please, explain me how to make it work with unicode strings, WideString in C++Builder?
Back to top
View user's profile Send private message
mamouri



Joined: 19 Aug 2006
Posts: 64

PostPosted: Wed Nov 22, 2006 5:39 am    Post subject: Reply with quote

syntetic,

For unicode version of this function check here: http://www.trichview.com/forums/viewtopic.php?t=1383

In C++ Builder you can make a pas unit file and include it in your project. There is no need to convert it to C++ Builder

Thank you
Back to top
View user's profile Send private message
Sergey Tkachenko
Site Admin


Joined: 27 Aug 2005
Posts: 6599

PostPosted: Thu Nov 23, 2006 1:47 pm    Post subject: Reply with quote

The link is deleted, because newer version is included in RichViewActions

This unit contains new functions for fast substring highlighting:

Code:
{
  The functions mark all occurences of s in RVData with (Color, BackColor).
  If RVData is nil (or omitted), rv.RVData is used.
  Both functions search both in Unicode and ANSI text items.
  Options:
    WholeWords (words are defined as characters between two characters from
      rv.Delimiters)
    IgnoreCase (for Unicode text, works only on WinNT-based systems
      (Win2000, WinXP...))
  Return value:
    number of marks. If it is positive, call rv.Format.
    If you call this function for TRichViewEdit, call ClearUndo.
}


function MarkSubStringA(const s: String;
  Color, BackColor: TColor; IgnoreCase, WholeWords: Boolean; rv: TCustomRichView;
  RVData: TCustomRVData=nil): Integer;
function MarkSubStringW(const s: WideString;
  Color, BackColor: TColor; IgnoreCase, WholeWords: Boolean; rv: TCustomRichView;
  RVData: TCustomRVData=nil): Integer;


To use in C++Builder, just include this unit in your project.

The following improvements were made comparing to the old code:
- supporting WholeWords and IgnoreCase options;
- for text in RichView, no conversion from Unicode to ANSI (or vice versa) is performed, so no characters are lost;
- not only String, but also WideString substring parameter.


Last edited by Sergey Tkachenko on Tue Jul 01, 2008 2:02 pm; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
Sergey Tkachenko
Site Admin


Joined: 27 Aug 2005
Posts: 6599

PostPosted: Wed May 02, 2007 6:16 pm    Post subject: Reply with quote

Updated. New (marked) items retain tag of the original text item.

Note: this unit is included in RichViewActions. If you use RichViewActions, copy this unit over the RichViewActions' one.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    trichview.com Forum Index -> Examples, Demos All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group