Processing current select text...is it possible?

General TRichView support forum. Please post your questions here
Post Reply
guybrush
Posts: 2
Joined: Thu Nov 10, 2005 6:50 am

Processing current select text...is it possible?

Post by guybrush »

As stated on the title, is it possible that i'm able to perform some operations on the current selected text? :roll: If yes, can anyone give an example on how to do so?

A simple example would be like when i want to make current text in a memo to uppercase, i just write like this "memo1.SelText := UpperCase(memo1.SelText)"..I read the help file for TRichView, the only method that i am able to find is "RichEdit.GetSelText" but it is a read only method..i can't do further processing on the selected text..
Michel
Posts: 92
Joined: Fri Oct 14, 2005 2:56 pm
Contact:

Post by Michel »

I can think of 2 ways.

One: See Help -> How to -> Implement commands like "make bold", "apply font", etc. Basically, you'll be calling ApplyStyleConversion(some int representing uppercase or whatever), and then in the OnStyleConversion() event that will fire for every selected item or portion thereof, you'll do whatever conversion you like. There's a Demo referenced in the above Help page.

Two: See Help -> Overview -> Selecting. GetSelectionBounds() will tell you what's selected in terms of items and offsets. However, I don't think you'll be able to directly convert the selection to uppercase (or what have you) if some item is selected only partially. Unless you start splitting items on selection boundaries and such. Basically, scary stuff, so take a look at this for general background on how RichView deals with the selection, but stick to "way One".

Hope this helps,

Michel
Sergey Tkachenko
Site Admin
Posts: 17310
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

memo1.SelText := UpperCase(memo1.SelText)
in TRichViewEdit will be:

rv1.InsertText(UpperCase(memo1.GetSelText));

But if the selection contain pictures, tables, or multiple fonts, it will be lost.
To avoid it, more complex processing it required - item by item. Some text may be Unicode, it must be taken into account.

Below is a fragment of RVCharCase.pas from RichViewActions (a key function changing the character case).

Code: Select all

procedure RVChangeCharCase(rve: TCustomRichViewEdit; CharCase: TRVCharCase);
var i, ItemNo1, ItemNo2, Offs1, Offs2: Integer;
    AItemNo1, AItemNo2, AOffs1, AOffs2: Integer;
    TextStyles: TFontInfos;
    ItemOptions: TRVItemOptions;
    s, s1, s2: String;
begin
  TextStyles := rve.Style.TextStyles;
  rve := rve.TopLevelEditor;
  rve.BeginUndoGroup(rvutModifyItem);
  rve.SetUndoGroupMode(True);
  LockWindowUpdate(rve.Handle);
  try
    rve.GetSelectionBounds(ItemNo1, Offs1, ItemNo2, Offs2, True);
    rve.GetSelectionBounds(AItemNo1, AOffs1, AItemNo2, AOffs2, False);
    if ItemNo2<>ItemNo1 then begin
      if rve.GetItemStyle(ItemNo1)>=0 then begin
        ItemOptions := rve.GetItem(ItemNo1).ItemOptions;
        s  := rve.GetItemText(ItemNo1);
        s1 := RVU_Copy(s, 1, Offs1-1, ItemOptions);
        s2 := RVU_Copy(s, Offs1, Length(s), ItemOptions);
        if s2<>'' then begin
          s2 := ChangeCharCase(s1, s2, TextStyles[rve.GetItemStyle(ItemNo1)], CharCase);
          rve.SetItemTextEd(ItemNo1, s1+s2);
        end;
      end;
      for i := ItemNo1+1 to ItemNo2-1 do
        if rve.GetItemStyle(i)>=0 then begin
          s  := rve.GetItemText(i);
          s := ChangeCharCase('', s, TextStyles[rve.GetItemStyle(i)], CharCase);
          rve.SetItemTextEd(i, s);
        end;
      if rve.GetItemStyle(ItemNo2)>=0 then begin
        ItemOptions := rve.GetItem(ItemNo2).ItemOptions;
        s  := rve.GetItemText(ItemNo2);
        s1 := RVU_Copy(s, 1, Offs2-1, ItemOptions);
        s2 := RVU_Copy(s, Offs2, Length(s), ItemOptions);
        if s1<>'' then begin
          s1 := ChangeCharCase('', s1, TextStyles[rve.GetItemStyle(ItemNo2)], CharCase);
          rve.SetItemTextEd(ItemNo2, s1+s2);
        end;
      end
      end
    else begin
      ItemOptions := rve.GetItem(ItemNo1).ItemOptions;
      s  := rve.GetItemText(ItemNo1);
      s1 := RVU_Copy(s, 1, Offs1-1, ItemOptions);
      s2 := RVU_Copy(s, Offs2, Length(s), ItemOptions);
      s  := RVU_Copy(s, Offs1, Offs2-Offs1, ItemOptions);
      s := ChangeCharCase(s1, s, TextStyles[rve.GetItemStyle(ItemNo1)], CharCase);
      rve.SetItemTextEd(ItemNo1, s1+s+s2);
    end;
  finally
    LockWindowUpdate(0);
    rve.SetUndoGroupMode(False);
    rve.SetSelectionBounds(AItemNo1, AOffs1, AItemNo2, AOffs2);
  end;
end;
Post Reply