[Example] How to scroll to selection

Demos, code samples. Only questions related to the existing topics are allowed here.
Post Reply
Sergey Tkachenko
Site Admin
Posts: 13398
Joined: Sat Aug 27, 2005 10:28 am

[Example] How to scroll to selection

Post by Sergey Tkachenko » Tue Oct 29, 2013 10:08 am

The function below scrolls to make selection visible.
It works both for TRichView and TRichViewEdit.

This function is especially useful in TRichView, because TRichView does not scroll after making selection (except for selecting with SearchText).

Code: Select all

uses CRVFData, CRVData, Math; 
procedure ShowSelection(rv: TCustomRichView); 
var RVData: TCustomRVFormattedData; 
    StartItemNo, StartOffs, EndItemNo, EndOffs: Integer; 
    i, StartDItemNo, StartDOffs, EndDItemNo, EndDOffs: Integer; 
    R: TRect; 
  // finding subdocument containing selection (a main document or a cell) 
  RVData := RV.RVData; 
  while RVData.GetChosenRVData<>nil do 
    RVData := TCustomRVFormattedData(RVData.GetChosenRVData); 
  // getting selection 
  RVData.GetSelectionBoundsEx(StartItemNo, StartOffs, EndItemNo, EndOffs, True); 
  if (StartItemNo<0) or (EndItemNo<0) then 
  // converting selection position from item indices to drawing item indices 
  RVData.Item2DrawItem(StartItemNo, StartOffs, StartDItemNo, StartDOffs); 
  RVData.Item2DrawItem(EndItemNo, EndOffs, EndDItemNo, EndDOffs); 
  // (roughly) calculating selection coordinates in local coordinates of RVData 
  with RVData.DrawItems[StartDItemNo] do 
    R := Bounds(Left, Top, Width, Height); 
  for i := StartDItemNo+1 to EndDItemNo do 
    with RVData.DrawItems[i] do begin 
      R.Left := Min(R.Left, Left); 
      R.Top := Min(R.Top, Top); 
      R.Right := Max(R.Right, Left+Width); 
      R.Bottom := Max(R.Bottom, Top+Height); 
  // scrolling to the selection 
  RVData.ShowRectangle(R.Left, R.Top, R.Right-R.Left, R.Bottom-R.Top); 

Post Reply