Recent

Author Topic: SynEdit get Byte pos at caret  (Read 2415 times)

BubikolRamios

  • Full Member
  • ***
  • Posts: 188
SynEdit get Byte pos at caret
« on: December 30, 2017, 04:35:01 pm »
If one loads text file from byte pos x to y and then click somewhere  inside text - how to get byte pos of clicked position ?

Was playing with this, but seems overload of code
Code: Pascal  [Select]
  1. procedure TForm1.SynEdit1Click(Sender: TObject);
  2. var p1,zeroPoint: TPoint;
  3. begin      
  4. //http://wiki.lazarus.freepascal.org/SynEdit#Logical.2FPhysical_caret_position
  5.   p1 := SynEdit1.LogicalCaretXY;//logical = byte position
  6.   zeroPoint := Point(1,1);
  7.   ShowMessage(SynEdit1.TextBetweenPoints(zeroPoint ,p1));
  8.  // with  plan that, byte length of that text would be my byte pos
  9. end;  
  10.  

and even it does not compile - wrong num of params for  TextBetweenPoints !?
« Last Edit: December 30, 2017, 04:40:05 pm by BubikolRamios »
lazarus-2.0.2-fpc-3.0.4-win32

Cyrax

  • Hero Member
  • *****
  • Posts: 745
Re: SynEdit get Byte pos at caret
« Reply #1 on: December 30, 2017, 08:03:08 pm »
Replace parentheses with square brackets in SynEdit1.TextBetweenPoints clause , then it should compile. TextBetweenPoints is property field, not subroutine. Thus it needs square brackets.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 5573
    • wiki
Re: SynEdit get Byte pos at caret
« Reply #2 on: December 30, 2017, 08:48:48 pm »
There are several questions.
1) getting the byte pos
2) getting the text (answered by Cyrax)

To get the pos...

SynEdit works with x/y.

And in x/y you can use either char, or byte pos. (TextBetweenPoints takes byte pos)

You can use
OnMouseUp / OnMouseDown   // pixel pos
SynEDit.RegisterBeforeMouseDownHandler()     // pixel pos
or MouseActions....
to get notified when the mouse is pressed

To get line/byte from pixels
SynEdit.PixelsToRowColumn()  // screen pos
SynEdit.PixelsToLogicalPos()   // text pos (that is what you want)

-------------
To get a byte index into the entire text (like memo does): http://wiki.freepascal.org/Lazarus_1.6.0_release_notes#SynMemo_is_now_Deprecated
(This may not be accurate for line endings.)

« Last Edit: December 30, 2017, 09:03:22 pm by Martin_fr »

BubikolRamios

  • Full Member
  • ***
  • Posts: 188
Re: SynEdit get Byte pos at caret
« Reply #3 on: December 30, 2017, 09:04:14 pm »
Thanks to both.
lazarus-2.0.2-fpc-3.0.4-win32

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 5573
    • wiki
Re: SynEdit get Byte pos at caret
« Reply #4 on: December 30, 2017, 09:34:50 pm »
You may want to test...

SynEdit may not store line endings. (not sure, maybe some times it does, but it is not guaranteed)

Therefore it may return line endings different from your original file. So the length may be diff.

Same is for the result of RowColToCharIndex (which returns a byte index afaik). It may count line endings as crlf or just lf, even if they are diff in your text.