Recent

Author Topic: TRichMemo - getting click position as row/column value  (Read 3649 times)

BosseB

  • Sr. Member
  • ****
  • Posts: 372
TRichMemo - getting click position as row/column value
« on: December 11, 2021, 06:16:04 pm »
I am converting a Delphi program to Lazarus/Fpc.
The Delphi program uses a TRichEdit control to display binary data in a hex array with the ability to highlight the range of bytes for a selected record within the data selected in a record number spin-edit.

Since TRichEdit did not exist in Lazarus I was advised to use TRichMemo instead and it works fine for that purpose. Selecting the data based on a record number in code is OK.

But, now I want to add a way to figure out which record number a particular value in the grid belongs to and highlight that record by clicking in the grid.
For this I need to be able to get the clicked character position expressed in line number/column number somehow.

So what can I do in the OnClick mouse event for the TRichMemo component?

See the image for an idea on how it looks like:
--
Bo Berglund
Sweden

Sieben

  • Sr. Member
  • ****
  • Posts: 302
Re: TRichMemo - getting click position as row/column value
« Reply #1 on: December 11, 2021, 07:05:21 pm »
Assuming that data for a specific record is of fixed length CharAtPos might be a start...?
Lazarus 2.2.0, FPC 3.2.2, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

wp

  • Hero Member
  • *****
  • Posts: 10030
Re: TRichMemo - getting click position as row/column value
« Reply #2 on: December 11, 2021, 08:00:42 pm »
Besides your question, let me draw your attention to the point that the selection in you screenshot contains also the address column. I think you should select only data columns.

I would not recommend to "abuse" the RichMemo as a hex editor. MPHexeditor is an excellent alternative and easy to use. I wrote a full-fledged HexEditor based on it (https://github.com/wp-xyz/Hex). Of course you can only use it to only view data.

BosseB

  • Sr. Member
  • ****
  • Posts: 372
Re: TRichMemo - getting click position as row/column value
« Reply #3 on: December 11, 2021, 09:38:11 pm »
Assuming that data for a specific record is of fixed length CharAtPos might be a start...?
Well, I already saw this but it returns the character at the coordinate and this will in a hex display always be one of 0..9, A..F plus space.
What I need to know is where the clicked character is located in the text regardless of the scroll state.

I.e. if I get to know that I clicked on the char in line 5 column 19 of the text loaded into the memo then I could immediately calculate to which data it belongs and highlight the record accordingly.

BTW: My highlighting function at the moment is not capable of skipping the address column, but this was also true of the Delphi program.
I tried back then to use two TRichEdits and sync the scrolling of them so they would always be parallel. But it really did not work well.
After I get the click to position function done I can have a look at doing multiple selections so that the address stays unselected.

UPDATE:
I assumed that the function would return the character itself by the name of the function but it seems to do what I want, namely return the character number in the text loaded.
Now I just have to check if the line feeds are included in the count too...
« Last Edit: December 11, 2021, 09:57:08 pm by BosseB »
--
Bo Berglund
Sweden

BosseB

  • Sr. Member
  • ****
  • Posts: 372
Re: TRichMemo - getting click position as row/column value
« Reply #4 on: December 12, 2021, 09:04:00 pm »
Besides your question, let me draw your attention to the point that the selection in you screenshot contains also the address column. I think you should select only data columns.
OK, but I could not find a way to set multiple SelStart/SelLength values in order to select multiple regions...
Is there a way to mark selected areas such that there can be non-marked areas in between?

Reply to self:
It is possible to use pbsHexData.SetRangeColor multiple times on different regions.
But this command does not ensure that the colored section is visible like the selection setting does...

Reply 2 to self:
By using:
Code: [Select]
panel.SelStart := nPos + nLength div 2;
panel.SelLength := 0;
The cursor is put at the middle of the selected data area, which ensures that at least a part of the selection is always visible.
« Last Edit: December 13, 2021, 09:13:11 am by BosseB »
--
Bo Berglund
Sweden

 

TinyPortal © 2005-2018