Change style of tect to type

General TRichView support forum. Please post your questions here
Post Reply
wvd_vegt
Posts: 83
Joined: Tue Aug 30, 2005 7:39 am

Change style of tect to type

Post by wvd_vegt »

Hi,

How do i change the textstyle of the next text the user types?

I want to be able to select a predefined style with a combobox and apply that at the caret location so when i start typing it is shown in the selected style.
Sergey Tkachenko
Site Admin
Posts: 17520
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

ApplyTextStyle or assignment to CurTextStyleNo property
(a difference is noticeable only if the selecton is not empty: ApplyTextStyle is applied to the selection too)
wvd_vegt
Posts: 83
Joined: Tue Aug 30, 2005 7:39 am

Post by wvd_vegt »

Hi Sergey,

It doesn't work in my app as expected.

I have a combobox in a TB2000 toolbar that is tied to an RVEvent so it's updated constantly with the styles/currentstyle of the active RVE. That part works nice due to a AfterUpdate event

Code: Select all

procedure T_MainForm.rvActionStylesAfterUpdate(Sender: TObject;
  Editor: TCustomRichViewEdit; var enable: Boolean);
var
  i                 : Integer;
  sl                : TStringList;
begin
//Optimize by only changing the items when different!
  StyleCombo.Enabled := Enabled;                                                // and Editor.SelectionExists;

  sl := TStringList.Create;
  with Sender as TCustomRichViewEdit do
    for i := 0 to Pred(Style.TextStyles.Count) do
      sl.Add(Style.TextStyles[i].DisplayName);
  if (sl.Text <> StyleCombo.Items.Text) then
    StyleCombo.Items := sl;
  sl.Free;

  if (StyleCombo.ItemIndex <> Editor.CurTextStyleNo {GetCurrentItem.StyleNo}) then
    StyleCombo.ItemIndex := Editor.CurTextStyleNo {GetCurrentItem.StyleNo};

//Hack to keep the editor around when a
  fCrv := Editor;
end;
and

Code: Select all

procedure T_MainForm.StyleComboSelect(Sender: TObject);
begin
  if Assigned(fCrv) then
    begin
      if fCrv.SelectionExists then
        fCrv.ApplyTextStyle(StyleCombo.ItemIndex)
      else
        begin
          fCrv.CurTextStyleNo := StyleCombo.ItemIndex;
//The next line make it 'work' but ruins the text on reload. 
//The 'x' also doesn't show up.
          fCrv.AddText('x', fCrv.CurTextStyleNo);
        end;
      fCrv.Format;
    end;
end;
Difference between regular toolbar buttons and the combobox is that the last one steals the focus. So when I set the CurTextStyleNo property and I click the RVE again to restore the focus it's last style is lost.

So i think what I need is a way to insert someting invisible so the style is changed at the carets location, so when I return focus it stays as selected.
Sergey Tkachenko
Site Admin
Posts: 17520
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Post by Sergey Tkachenko »

I am not familiar with TB2000 combobox, but may be you should use OnClick event instead of OnSelect and call fCrv.SetFocus before calling ApplyTextStyle?
Do not call Format after ApplyTextStyle.

(and AddText adds the text to the end of document, not at the caret position, and really requires Format before displaying. Also, it does not update undo buffer, so it's not appropriate here)
Post Reply