Search code examples
delphimousecoordinatesfiremonkeytmemo

Mouse Coordinates To Caret Position in Firemonkey TMemo Component


Handling Mouse / Drag&Drop Events on Firemonkeys TMemo Component offers Mousecursor Coordinates. Is there a way to calculate the CaretPosition out of the Mouse Coordinates?

I want to drag a text into a TMemo and this text should be inserted at current MouseCoordinates.


Solution

  • Try calling GetPointPosition.

    Unfortunately, this seems to have been removed from TMemo in XE3. As a quick and dirty workaround, you could try this:

    function GetPointPosition(Memo: TMemo; Pt: TPointF; RoundToWord: Boolean = False): TCaretPosition;
    var
      I, LPos: Integer;
      Rgn: TRegion;
    begin
      Result.Line := -1;
      Result.Pos := -1;
      for I := 0 to Memo.Lines.Count - 1 do
      begin
        if Memo.Lines.Objects[I] is TTextLayout then
        begin
          LPos := TTextLayout(Memo.Lines.Objects[I]).PositionAtPoint(Pt, RoundToWord);
          if LPos >= 0 then
          begin
            if LPos > 0 then
            begin
              Rgn := TTextLayout(Memo.Lines.Objects[I]).RegionForRange(TTextRange.Create(LPos, 1), RoundToWord);
              if (Length(Rgn) > 0) and (Rgn[0].Top > Pt.Y) then
                Dec(LPos);
            end;
            Result.Line := I;
            Result.Pos := LPos;
            Break;
          end;
        end;
      end;
    end;