Recent

Author Topic: How can you extract the current line from a TMemo?  (Read 5601 times)

vfclists

  • Hero Member
  • *****
  • Posts: 1147
    • HowTos Considered Harmful?
How can you extract the current line from a TMemo?
« on: May 10, 2015, 03:25:36 pm »

How can you extract the current line from a TMemo?

I want the current line I to be copied whenever I press a particular key, or on some particular event.
Lazarus 3.0/FPC 3.2.2

rvk

  • Hero Member
  • *****
  • Posts: 6726
Re: How can you extract the current line from a TMemo?
« Reply #1 on: May 10, 2015, 03:33:19 pm »
Your current line would be
Memo1.Lines[Memo1.CaretPos.y]

vfclists

  • Hero Member
  • *****
  • Posts: 1147
    • HowTos Considered Harmful?
Re: How can you extract the current line from a TMemo?
« Reply #2 on: May 10, 2015, 09:46:13 pm »
Your current line would be
Memo1.Lines[Memo1.CaretPos.y]

I have tried it and it always returns 0. On checking the source it appears to a virtual method which hasn't been defined
Lazarus 3.0/FPC 3.2.2

Cyrax

  • Hero Member
  • *****
  • Posts: 836
Re: How can you extract the current line from a TMemo?
« Reply #3 on: May 10, 2015, 09:57:40 pm »
It works here alright.

Lazarus SVN trunk r48926 with FPC trunk 30794. Microsoft Windows Server 2008 R2 with SP1.

rvk

  • Hero Member
  • *****
  • Posts: 6726
Re: How can you extract the current line from a TMemo?
« Reply #4 on: May 10, 2015, 10:05:41 pm »
I have tried it and it always returns 0. On checking the source it appears to a virtual method which hasn't been defined
You can't track to that procedure via Lazarus (and find the definition). If you really track the source you get to this point (for Windows):

in lcl/interfaces/win32/win32wsstdctrls.pp
Code: [Select]
class function TWin32WSCustomMemo.GetCaretPos(const ACustomEdit: TCustomEdit): TPoint;
var
  BufferX: Longword;
begin
  { X position calculation }

  { EM_GETSEL returns the char index of the caret, but this index
    doesn't go back to zero in new lines, so we need to subtract
    the char index from the line

    EM_GETSEL expects a pointer to 32-bits buffer in lParam
  }
  Windows.SendMessageW(ACustomEdit.Handle, EM_GETSEL, 0, PtrInt(@BufferX));
  { EM_LINEINDEX returns the char index of a given line
    wParam = -1 indicates the line of the caret
  }
  Result.X := BufferX - Windows.SendMessageW(ACustomEdit.Handle, EM_LINEINDEX, -1, 0);

  { Y position calculation }

  { EM_LINEFROMCHAR returns the number of the line of a given
    char index.
  }
  Result.Y := Windows.SendMessageW(ACustomEdit.Handle, EM_LINEFROMCHAR, BufferX, 0);
end;

Are you using Windows or Linux?

vfclists

  • Hero Member
  • *****
  • Posts: 1147
    • HowTos Considered Harmful?
Re: How can you extract the current line from a TMemo?
« Reply #5 on: May 11, 2015, 12:16:36 am »
I have tried it and it always returns 0. On checking the source it appears to a virtual method which hasn't been defined
You can't track to that procedure via Lazarus (and find the definition). If you really track the source you get to this point (for Windows):

in lcl/interfaces/win32/win32wsstdctrls.pp
...

I am using Qt on Linux. I have switched to TSynMemo, but I am missing wordwrap and simple cat and paste using the right-click menu.
« Last Edit: May 11, 2015, 12:18:25 am by vfclists »
Lazarus 3.0/FPC 3.2.2

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Re: How can you extract the current line from a TMemo?
« Reply #6 on: May 11, 2015, 08:00:05 am »
I am using Qt on Linux.

Then report a bug that it doesn't work with Qt on Linux.

zeljko

  • Hero Member
  • *****
  • Posts: 1752
    • http://wiki.lazarus.freepascal.org/User:Zeljan
Re: How can you extract the current line from a TMemo?
« Reply #7 on: May 11, 2015, 04:02:07 pm »
I've implemented Caret.Y for Qt. Test with r48988

vfclists

  • Hero Member
  • *****
  • Posts: 1147
    • HowTos Considered Harmful?
Re: How can you extract the current line from a TMemo?
« Reply #8 on: May 11, 2015, 06:19:00 pm »
I've implemented Caret.Y for Qt. Test with r48988

Thanks. I will check it out and try it as soon as I can.
Lazarus 3.0/FPC 3.2.2

vfclists

  • Hero Member
  • *****
  • Posts: 1147
    • HowTos Considered Harmful?
Re: How can you extract the current line from a TMemo?
« Reply #9 on: May 16, 2015, 03:54:14 pm »
I've implemented Caret.Y for Qt. Test with r48988

Hi,

I have tested the fix on r49040 and is working fine.

Thanks a lot.
Lazarus 3.0/FPC 3.2.2

 

TinyPortal © 2005-2018