Recent

Author Topic: TDrawGrid/TStringGrid character entry  (Read 928 times)

VTwin

  • Hero Member
  • *****
  • Posts: 793
  • Former Turbo Pascal 3 user
TDrawGrid/TStringGrid character entry
« on: September 18, 2019, 03:50:24 am »
When entering data into a TDrawGrid cell, the first keystroke enters the character, but also selects it. The second keystroke therefore deletes the first character.

This did not happen in Carbon.

I will try to write a simple example showing this behavior, but would like to know if others have observed this.
« Last Edit: October 24, 2019, 10:47:46 pm by VTwin »
“Talk is cheap. Show me the code.” -Linus Torvalds

macOS 10.13.6: Lazarus 2.0.7 fixes svn 62300 (64 bit Cocoa)
Ubuntu 18.04.3: Lazarus 2.0.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.0.6 (64 bit on VBox)
fpc 3.0.4

VTwin

  • Hero Member
  • *****
  • Posts: 793
  • Former Turbo Pascal 3 user
Re: TDrawGrid character entry
« Reply #1 on: September 18, 2019, 04:04:11 pm »
It happens in a stringgrid as well. Try to enter data in cells.

“Talk is cheap. Show me the code.” -Linus Torvalds

macOS 10.13.6: Lazarus 2.0.7 fixes svn 62300 (64 bit Cocoa)
Ubuntu 18.04.3: Lazarus 2.0.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.0.6 (64 bit on VBox)
fpc 3.0.4

VTwin

  • Hero Member
  • *****
  • Posts: 793
  • Former Turbo Pascal 3 user
Re: TDrawGrid character entry
« Reply #2 on: September 20, 2019, 03:25:04 am »
I have no option but to release software with this bug. If anyone can provide a patch I would be very grateful.
« Last Edit: September 20, 2019, 03:39:32 pm by VTwin »
“Talk is cheap. Show me the code.” -Linus Torvalds

macOS 10.13.6: Lazarus 2.0.7 fixes svn 62300 (64 bit Cocoa)
Ubuntu 18.04.3: Lazarus 2.0.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.0.6 (64 bit on VBox)
fpc 3.0.4

VTwin

  • Hero Member
  • *****
  • Posts: 793
  • Former Turbo Pascal 3 user
Re: TDrawGrid character entry
« Reply #3 on: September 27, 2019, 03:49:04 pm »
Can anyone verify this bug?

I can file a bug report, but would appreciate confirmation first.
“Talk is cheap. Show me the code.” -Linus Torvalds

macOS 10.13.6: Lazarus 2.0.7 fixes svn 62300 (64 bit Cocoa)
Ubuntu 18.04.3: Lazarus 2.0.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.0.6 (64 bit on VBox)
fpc 3.0.4

VTwin

  • Hero Member
  • *****
  • Posts: 793
  • Former Turbo Pascal 3 user
Re: TDrawGrid character entry
« Reply #4 on: September 28, 2019, 04:12:23 pm »
Reported.
“Talk is cheap. Show me the code.” -Linus Torvalds

macOS 10.13.6: Lazarus 2.0.7 fixes svn 62300 (64 bit Cocoa)
Ubuntu 18.04.3: Lazarus 2.0.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.0.6 (64 bit on VBox)
fpc 3.0.4

VTwin

  • Hero Member
  • *****
  • Posts: 793
  • Former Turbo Pascal 3 user
Re: TDrawGrid character entry
« Reply #5 on: October 24, 2019, 10:46:52 pm »
Here is the report:

https://bugs.freepascal.org/view.php?id=36111

It is an extremely annoying bug if you are using TStringGrid or TDrawGrid for data entry.
“Talk is cheap. Show me the code.” -Linus Torvalds

macOS 10.13.6: Lazarus 2.0.7 fixes svn 62300 (64 bit Cocoa)
Ubuntu 18.04.3: Lazarus 2.0.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.0.6 (64 bit on VBox)
fpc 3.0.4

winni

  • Hero Member
  • *****
  • Posts: 549
Re: TDrawGrid/TStringGrid character entry
« Reply #6 on: October 25, 2019, 12:25:27 am »
Hi!

You call from a TStringGrid the TStringCellEditor.
Inherited from TCustomEdit from TWinControl

So you got OnKeyDown/OnKeyUp/OnKeypress which should notice every char.

And there is a property SelLength.

So set the SelLength := 0 on every key event. That is the length of the selection.
If I havnt overlooked something very tricky this should work.

Winni


VTwin

  • Hero Member
  • *****
  • Posts: 793
  • Former Turbo Pascal 3 user
Re: TDrawGrid/TStringGrid character entry
« Reply #7 on: October 27, 2019, 07:26:52 pm »
Thanks Winni!

The following code:

Code: Pascal  [Select]
  1. procedure TForm1.StringGrid1KeyPress(Sender: TObject; var Key: char);
  2. var
  3.   ed: TStringCellEditor;
  4. begin
  5.   {$IFDEF DARWIN}
  6.   if StringGrid1.Editor is TStringCellEditor then begin
  7.     ed := TStringCellEditor(StringGrid1.Editor);
  8.     if Length(ed.EditText) = 1 then begin
  9.       ed.SelStart := 1;
  10.       ed.SelLength := 0;
  11.     end;
  12.   end;
  13.   {$ENDIF}
  14. end;

seems to be a workaround for a StringGrid. However, OnEditingDone is incorrectly called for the first key press, which has broken the code for my DrawGrid. OnEditingDone is not called for subsequent key presses, until the editing is actually done (e.g., by pressing Enter).

“Talk is cheap. Show me the code.” -Linus Torvalds

macOS 10.13.6: Lazarus 2.0.7 fixes svn 62300 (64 bit Cocoa)
Ubuntu 18.04.3: Lazarus 2.0.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.0.6 (64 bit on VBox)
fpc 3.0.4

VTwin

  • Hero Member
  • *****
  • Posts: 793
  • Former Turbo Pascal 3 user
Re: TDrawGrid/TStringGrid character entry
« Reply #8 on: October 30, 2019, 05:58:39 pm »
A better kludge:

Code: Pascal  [Select]
  1. procedure TDataForm.grdDataKeyDown(Sender: TObject; var Key: Word;
  2.   Shift: TShiftState);
  3. var
  4.   ed: TStringCellEditor;
  5. begin
  6.   {$IFDEF DARWIN}
  7.   if Key in [VK_RETURN, VK_LEFT, VK_UP, VK_RIGHT, VK_DOWN{, VK_TAB}] then begin // done
  8.     grdData.OnEditingDone := fOnEditingDone;
  9.   end else begin // not done
  10.     grdData.OnEditingDone := nil;
  11.     if grdData.Editor is TStringCellEditor then begin
  12.       ed := TStringCellEditor(grdData.Editor);
  13.       if Length(ed.EditText) = 1 then begin
  14.         ed.SelStart := 1;
  15.         ed.SelLength := 0;
  16.       end;
  17.     end;
  18.   end;
  19.   {$ENDIF}
  20. end;  

This prevents OnEditingDone from firing until editing is actually done, so works with a TDrawGrid.

A separate question is, how to make VK_TAB act like VK_RIGHT, and call OnEditingDone as is common in a spreadsheet. I can't seem to make that work.
“Talk is cheap. Show me the code.” -Linus Torvalds

macOS 10.13.6: Lazarus 2.0.7 fixes svn 62300 (64 bit Cocoa)
Ubuntu 18.04.3: Lazarus 2.0.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.0.6 (64 bit on VBox)
fpc 3.0.4

winni

  • Hero Member
  • *****
  • Posts: 549
Re: TDrawGrid/TStringGrid character entry
« Reply #9 on: October 30, 2019, 06:25:17 pm »
Hi!

A separate question is, how to make VK_TAB act like VK_RIGHT

You have to disable TabStop  in the object inspector!!!

Then onKeyDown/Up:

Code: Pascal  [Select]
  1. if key = VK_Tab then key := VK_Right
  2.  

Winni

Soner

  • Full Member
  • ***
  • Posts: 165
Re: TDrawGrid/TStringGrid character entry
« Reply #10 on: October 30, 2019, 06:43:01 pm »
Hi!

A separate question is, how to make VK_TAB act like VK_RIGHT

You have to disable TabStop  in the object inspector!!!

Then onKeyDown/Up:

Code: Pascal  [Select]
  1. if key = VK_Tab then key := VK_Right
  2.  

Winni
There is better way in TStringGrid for this.
Add goTAbs to TStringGrid.Options then you can control with TStringGrid.TabAdvance how do you want it.

VTwin

  • Hero Member
  • *****
  • Posts: 793
  • Former Turbo Pascal 3 user
Re: TDrawGrid/TStringGrid character entry
« Reply #11 on: October 30, 2019, 08:46:38 pm »
Thanks.

I had already tried all of these suggestions:

TabStop disabled.

Code: Pascal  [Select]
  1. if key = VK_Tab then key := VK_Right
set in KeyUp/KeyDown

TabAdvance aaRight

It does not work in Mac, Win, or Lin. I have attached a project, perhaps you can point out the problem?

« Last Edit: October 30, 2019, 08:54:25 pm by VTwin »
“Talk is cheap. Show me the code.” -Linus Torvalds

macOS 10.13.6: Lazarus 2.0.7 fixes svn 62300 (64 bit Cocoa)
Ubuntu 18.04.3: Lazarus 2.0.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.0.6 (64 bit on VBox)
fpc 3.0.4

VTwin

  • Hero Member
  • *****
  • Posts: 793
  • Former Turbo Pascal 3 user
Re: TDrawGrid/TStringGrid character entry
« Reply #12 on: October 30, 2019, 09:11:58 pm »
I also tried goTabs = true.

Once in the StringCellEditor, only the arrow and enter keys trigger EditDone, and leave the editor. As far as I can tell, the above suggestions apply to the Grid, not the StringCellEditor.
“Talk is cheap. Show me the code.” -Linus Torvalds

macOS 10.13.6: Lazarus 2.0.7 fixes svn 62300 (64 bit Cocoa)
Ubuntu 18.04.3: Lazarus 2.0.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.0.6 (64 bit on VBox)
fpc 3.0.4

VTwin

  • Hero Member
  • *****
  • Posts: 793
  • Former Turbo Pascal 3 user
Re: TDrawGrid/TStringGrid character entry
« Reply #13 on: October 31, 2019, 07:22:49 pm »
The trick to using tab seems to be to create a simple descendant of TStringCellEditor:

Code: Pascal  [Select]
  1. TTabStringCellEditor = class(TStringCellEditor)
  2. protected
  3.   procedure KeyDown(var Key : Word; Shift : TShiftState); override;
  4. end;
  5.  
  6. procedure TTabStringCellEditor.KeyDown(var Key : Word; Shift : TShiftState);
  7. begin
  8.   if Key = VK_TAB then
  9.     Key := VK_RIGHT;
  10.   inherited;
  11. end;

The attached project gives a workaround for the cocoa bug, and implements tab editing done. Tested on Mac, Win, and Lin for StringGrid and DrawGrid.

EDIT: Enable TabStop to set focus on Grid, so a mouse click is not required.
« Last Edit: November 01, 2019, 02:50:41 am by VTwin »
“Talk is cheap. Show me the code.” -Linus Torvalds

macOS 10.13.6: Lazarus 2.0.7 fixes svn 62300 (64 bit Cocoa)
Ubuntu 18.04.3: Lazarus 2.0.6 (64 bit on VBox)
Windows 7 Pro SP1: Lazarus 2.0.6 (64 bit on VBox)
fpc 3.0.4