Get the word selected

General TRichView support forum. Please post your questions here
Post Reply
alexandreq
Posts: 184
Joined: Wed Jan 18, 2012 6:22 pm

Get the word selected

Post by alexandreq »

Hello serge,

I am trying to do something that must be very simple but I am not getting it and I need your help.

I have a text with 3 or more pages, there is a word in my text named “sightseeing” that appears 9 times in all my text. When I highlight the word I need to know what word “sightseeing” was selected / highlighted among other words “sightseeing”.

Getting the word position in my text I will keep the coordinates in a table and when I close the text any change on it will be discarded. When I open the text again, the word that was selected / highlighted must be found and be highlighted automatic.

Why should I keep the word highlighted in a list table?

Because the text will be used for many users and the words highlighted can’t change the original document, in this table that I will keep the words and positions of them highlighted will be kept with a user code.

How can I do that?

Thanks
Alexandre
Sergey Tkachenko
Site Admin
Posts: 17554
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

Sorry, I am not sure that I understand the problem.

Do you simply need to select all occurrences of the same word?

1) The simplest way is using TRichViewEdit, and a cycle

Code: Select all

while rve.SearchText(...) do
  rve.ApplyStyleConversion(...)
ApplyStyleConversion requires implementation of OnStyleConversion event.
If you use RichViewActions, you can use TrvActionFontEx instead:

Code: Select all

  rvActionFontEx1.UserInterface := False;
  rve.BeginUpdate;
  while rve.SearchText(...) do
  begin
    rvActionFontEx1.Font.Color := clYellow;
    rvActionFontEx1.BackColor := clBlue;
    rvActionFontEx1.ValidProperties := [rvfimColor, rvfimBackColor];
    rvActionFontEx1.ExecuteTarget(rve);
  end;
  rve.EndUpdate;
  rvActionFontEx1.UserInterface := True;
If the document must be read-only, you can temporary assign rve.ReadOnly = False while executing this code.

This method modifies the document. However, as I understand, you can simply discard these changes.
Sergey Tkachenko
Site Admin
Posts: 17554
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

2) Another option to mark words with color.
RichViewActions includes MarkSearch.pas unit. It has the function MarkSubStringW:

Code: Select all

function MarkSubStringW(const s: TRVUnicodeString; Color, BackColor: TColor;
  IgnoreCase, WholeWords: Boolean; rv: TCustomRichView;
  RVData: TCustomRVData = nil): Integer;
This unit can be used separately from RichViewActions.
It does the same work as the solution (1), but faster, and it does not require TRichViewEdit (it can work both for TRichView and TRichViewEdit).
Disadvantage: it searches in each item separately, so it cannot find words written using multiple fonts.
Sergey Tkachenko
Site Admin
Posts: 17554
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

3) This solution highlights words using the same mechanism that is used to draw wavy underlines for misspelled words.

http://www.trichview.com/support/files/rectmarks.zip contains a unit with the following functions:

procedure AddRectMark(RVData: TCustomRVData; ItemNo, Offs, Len: Integer; Color: TColor); - adds a mark at the specified position. A mark is displayed as a rectangle of the specified Color.

procedure MarkSubstring(rv: TCustomRichView; const s: String; Color: TColor); - marks all occurences of s. This is a sample function, it does not have necessary options (case sensitivity, whole words, etc.). You can modify it yourself.

procedure ClearRectMarks(rv: TCustomRichView); - clears all marks.

Unlike the previous methods, this methods does not change a document structure.
This example draws rectangles around words. I can modify it to shade words (drawing a semitransparent rectangle above them)
Disadvantage: it searches in each item separately, so it cannot find words written using multiple fonts
alexandreq
Posts: 184
Joined: Wed Jan 18, 2012 6:22 pm

Post by alexandreq »

Hello Sergey

I am returning this issue, thanks for your help so far.

But my needs are different from those solutions, although it also helped me with another task.

explanation the problem

I have a text in 3 pages and in all this text the word "sightseeing" appears 9 times in all text. When I click on the word "sightseeing" I collect the word and put in a StringList that will be saved into a table, any change will happen in the original text. Now, imagine that I only collected 5 words "sightseeing" from all my text and all of them were collected from page 2 and 3. In the page 1 there are also the same words, but I didn't collect them.

Now I close the editor and open it again with that same text, I will also open the table that the list of my collected words saved and I want to find those words in the text and color them. Remember that, none word from page 1 were collected, only from page 2 and 3.

to do that, I need something like this:

I need to collect the word with an id or some position of it in my text, so that when I will highlight the words that were collected, I need to know exactly what word must be highlighted.

did you understand?
Sergey Tkachenko
Site Admin
Posts: 17554
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

What's the problem exactly, how to store position in a file?

You can use the functions from RVLinear.pas, they allow to get selection as two values: SelStart and SelLength. Use RVGetSelection function.
If you need to convert position specified as (RVData, ItemNo, OffsetInItem) to a numeric position (which can be used as SelStart), use RichViewToLinear function (the second parameter must be rv.RVData, where rv is TRichView passed to the first parameter)
alexandreq
Posts: 184
Joined: Wed Jan 18, 2012 6:22 pm

Post by alexandreq »

You are right, I forgot the Selstart and sellength, I'll do that.

thanks Sergey

if you need some more translation to portuguese, you can count on me.
Post Reply