How to make a particular line to topmost?
Posted: Fri Mar 25, 2022 5:44 am
How can I make the line where the cursor is, to the topmost of a TRichViewEdit?
Support forums for TRichView, ScaleRichView, Report Workshop and RVMedia components
https://reportworkshop.com/forums/
Code: Select all
procedure ScrollItemToTop(rve: TCustomRichViewEdit);
var
DItem, DOffs: integer;
begin
rve.RVData.Item2DrawItem(rve.CurItemNo, rve.OffsetInCurItem, DItem, DOffs );
rve.ScrollToDrawItem( rve.RVData, DItem, false, false );
rve.SetFocusSilent;
end;
Code: Select all
procedure ScrollItemToTop(rve: TCustomRichViewEdit);
var
DItem, DOffs: integer;
tle: TCustomRichViewEdit;
begin
tle := rve.TopLevelEditor;
tle.RVData.Item2DrawItem(tle.CurItemNo, tle.OffsetInCurItem, DItem, DOffs );
rve.ScrollToDrawItem( tle.RVData, DItem, false, false );
rve.SetFocusSilent;
end;
Code: Select all
uses CRVFData, RVERVData, DLines, RVCoords;
// Returns a range of drawing items on the RVData's line containing
// the DItemNo-th drawing item
procedure GetDItemsInLine(RVData: TCustomRVFormattedData;
DItemNo: Integer; out DItemNo1, DItemNo2: Integer);
begin
DItemNo1 := DItemNo;
while not RVData.DrawItems[DItemNo1].FromNewLine do
dec(DItemNo1);
DItemNo2 := DItemNo + 1;
while (DItemNo2 < RVData.DrawItems.Count) and
not RVData.DrawItems[DItemNo2].FromNewLine do
inc(DItemNo2);
dec(DItemNo2);
end;
// Returns the top coordinate of the RVData's line containing
// the DItemNo-th drawing item, relative to the top corner of the rv's document.
function GetLineTop(rv: TCustomRichView; RVData: TCustomRVFormattedData;
DItemNo: Integer): TRVCoord;
var
i, DItemNo1, DItemNo2: Integer;
R: TRVCoordRect;
function GetDrawItemCoords(Index: Integer): TRVCoordRect;
var
DItem: TRVDrawLineInfo;
begin
DItem := RVData.DrawItems[Index];
Result := RVCoordBounds(DItem.Left, RVData.GetTransformedDItemTop(Index),
DItem.Width, DItem.Height);
end;
begin
GetDItemsInLine(RVData, DItemNo, DItemNo1, DItemNo2);
R := GetDrawItemCoords(DItemNo);
for i := DItemNo1 + 1 to DItemNo2 do
UnionRect(R, R, GetDrawItemCoords(i));
RVData.RotateRectFromDocToScreen(R, rvtrbAbsRoot);
Result := R.Top;
end;
Code: Select all
var
Y: TRVCoord;
begin
Y := GetLineTop(RichViewEdit1, RichViewEdit1.TopLevelEditor.RVData,
TRVEditRVData(RichViewEdit1.TopLevelEditor.RVData).CaretDrawItemNo);
RichViewEdit1.ScrollTo(Y);
end;
Your code can be modified to avoid this problem:if a line is wrapped, and not on a space character, the position at the end of this line and at the beginning of the next line has the same (rve.CurItemNo, rve.OffsetInCurItem).
Code: Select all
uses RVERVData;
procedure ScrollItemToTop(rve: TCustomRichViewEdit);
var
DItem: integer;
tle: TCustomRichViewEdit;
begin
tle := rve.TopLevelEditor;
DItem := TRVEditRVData(tle.RVData).CaretDrawItemNo;
rve.ScrollToDrawItem( tle.RVData, DItem, false, false );
rve.SetFocusSilent;
end;