Recent

Author Topic: Stringgrid Edit works differently in Lazarus Version 1.6.4.  (Read 6764 times)

Gary Randall

  • Jr. Member
  • **
  • Posts: 70
Stringgrid Edit works differently in Lazarus Version 1.6.4.
« on: April 20, 2017, 10:48:38 pm »
I just installed Version 1.6.4 dated 2017-02-27 and was pleased to find all problems I previously found on upgrades since Version 1.4 dated 2015-04-18 have been resolved.  Everything now compiles but the stringgrid editing behavior is different.   I use the standard TSringCellEditor which is called upon entry of any data cell. I set the editor values as shown in the following code.  In Version 1.4 The selected cell is entirely filled with yellow and its text is highlighted in blue.  As soon as a new value is typed the text changes to red.

Under Version 1.6.4 the actions are the same except the cell is not entirely filled with the yellow background color.  Instead, only the text background is yellow and the text height is clipped a little.

This must just be a wrong setting or option.  Any help will be appreciated.

Code: Pascal  [Select][+][-]
  1. procedure TMainForm.sgMainSelectEditor(Sender: TObject; aCol, aRow: integer;
  2.   var Editor: TWinControl);
  3.  
  4. begin
  5.   if (Editor is TStringCellEditor) then begin
  6.     TStringCellEditor(Editor).Font.Color:=clRed;
  7.     TStringCellEditor(Editor).Color:=clYellow;
  8.     TStringCellEditor(Editor).Font.Name := 'Comic Sans MS';
  9.     TStringCellEditor(Editor).Font.Height := -17;
  10.     TStringCellEditor(Editor).Font.Size := 13;
  11.     TStringCellEditor(Editor).Font.CharSet := ANSI_CHARSET;
  12.     TStringCellEditor(Editor).Font.Orientation := 0;
  13.     TStringCellEditor(Editor).Font.Pitch := fpVariable;
  14.     TStringCellEditor(Editor).Font.Quality := fqDefault;
  15.     TStringCellEditor(Editor).Font.Style := [fsBold];
  16.     case aCol of
  17.       0:
  18.         begin
  19.           TStringCellEditor(Editor).MaxLength := 5;
  20.         end;
  21.       1:
  22.         begin
  23.           TStringCellEditor(Editor).MaxLength := 44;
  24.         end;
  25.       2:
  26.         begin
  27.           TStringCellEditor(Editor).MaxLength := 3;
  28.         end;
  29.       3:
  30.         begin
  31.           TStringCellEditor(Editor).MaxLength := 3;
  32.         end;
  33.     end;
  34.  end;
  35. end;
  36.  
« Last Edit: April 21, 2017, 01:10:58 am by Gary Randall »
Windows 7 Home Premium 64 bit - SP 1
Lazarus Version #: 1.8.0; FPC Version: 3.0.4
SVN Revision 56594
i386-win32-win32/win64

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4459
  • I like bugs.
Re: Stringgrid Edit works differently in Lazarus Version 1.6.4.
« Reply #1 on: April 21, 2017, 01:11:45 am »
Please test with the new fixes_1_8 branch. It has many changes related to scaling.
 http://wiki.freepascal.org/Lazarus_1.8_fixes_branch
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

wp

  • Hero Member
  • *****
  • Posts: 11854
Re: Stringgrid Edit works differently in Lazarus Version 1.6.4.
« Reply #2 on: April 21, 2017, 01:14:29 am »
Are the cells with the yellow background customdrawn? If yes then post your painting code.

Gary Randall

  • Jr. Member
  • **
  • Posts: 70
Re: Stringgrid Edit works differently in Lazarus Version 1.6.4.
« Reply #3 on: April 21, 2017, 02:11:09 am »
Are the cells with the yellow background customdrawn? If yes then post your painting code.
No, the first four data cells are not custom drawn.  They do get looked at by several events to set the proper context menu and to wrap to the next line when a tab, arrow, or return goes beyond the fourth cell.  The remaining calendar cells are not edited but filled by seperate forms.  The heading cells are not edited.  The "Percent Complete" bar is custom drawn by OnDrawCell but the data is edited the same as the first three cells.  I did try setting the Editor Height to the row height but it made no difference.  It looks like the text rectangle is being filled with yellow instead of the cell rectangle.

Please test with the new fixes_1_8 branch. It has many changes related to scaling.
 http://wiki.freepascal.org/Lazarus_1.8_fixes_branch
Thanks Juha.  I looked at the fixes but didn't see anything that looks related.  However, there could be an answer in the many changes.  It's been 14 years since I used SVN so I'll wait a little longer before trying your suggestion.  I'm still trying to master GIT.



« Last Edit: April 21, 2017, 02:17:40 am by Gary Randall »
Windows 7 Home Premium 64 bit - SP 1
Lazarus Version #: 1.8.0; FPC Version: 3.0.4
SVN Revision 56594
i386-win32-win32/win64

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4459
  • I like bugs.
Re: Stringgrid Edit works differently in Lazarus Version 1.6.4.
« Reply #4 on: April 21, 2017, 02:45:57 pm »
Thanks Juha.  I looked at the fixes but didn't see anything that looks related.  However, there could be an answer in the many changes.
Lazarus 1.6 was forked at November 2015, almost 1.5 years ago. All the over 4000 commits since then are in the 1.8 branch but most not in the 1.6 branch.
I doubt you checked them all.

Quote
It's been 14 years since I used SVN so I'll wait a little longer before trying your suggestion.
It is extremely easy. Just:
 $ svn co http://svn.freepascal.org/svn/lazarus/branches/fixes_1_8 lazarus_1_8
and later:
 $ svn up
With TortoiseSVN it is equally easy.
Then do initial build with "make".

Quote
I'm still trying to master GIT.
You don't need to master Git to clone a repository. For example get a trunk mirror:
 git clone https://github.com/alrieckert/lazarus.git
Studying the revision history is very handy with a Git repo + "gitk".
« Last Edit: April 21, 2017, 05:28:04 pm by JuhaManninen »
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

wp

  • Hero Member
  • *****
  • Posts: 11854
Re: Stringgrid Edit works differently in Lazarus Version 1.6.4.
« Reply #5 on: April 21, 2017, 03:25:02 pm »
Are the cells with the yellow background customdrawn? If yes then post your painting code.
No, the first four data cells are not custom drawn.  They do get looked at by several events to set the proper context menu and to wrap to the next line when a tab, arrow, or return goes beyond the fourth cell.  The remaining calendar cells are not edited but filled by seperate forms.  The heading cells are not edited.  The "Percent Complete" bar is custom drawn by OnDrawCell but the data is edited the same as the first three cells.  I did try setting the Editor Height to the row height but it made no difference.  It looks like the text rectangle is being filled with yellow instead of the cell rectangle.
I don't understand... Can you post the OnDrawCell code? It this where the yellow cell (the one with "Survey and Layout") is painted? I am almost 100% sure that the version change of Lazarus not the problem. The following code works fine on 1.6.4:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.StringGrid1DrawCell(Sender: TObject; aCol, aRow: Integer;
  2.   aRect: TRect; aState: TGridDrawState);
  3. begin
  4.   if (ACol = 2) and (ARow = 1) then
  5.   begin
  6.     StringGrid1.Canvas.Brush.Color := clYellow;
  7.     StringGrid1.Canvas.Brush.Style := bsSolid;
  8.     StringGrid1.Canvas.FillRect(ARect);
  9.     StringGrid1.Canvas.TextOut(
  10.       ARect.Left + constCellPadding, ARect.Top + constCellpadding,
  11.       StringGrid1.Cells[ACol, ARow]);
  12.   end;
  13. end;  
BTW, just for changing the background color of a cell, there's no need to custom-draw a cell. Just assign the new color to the grid's Canvas.Brush.color in the OnPrepareCanvas event for the corresponding cell.

Gary Randall

  • Jr. Member
  • **
  • Posts: 70
Re: Stringgrid Edit works differently in Lazarus Version 1.6.4.
« Reply #6 on: April 21, 2017, 06:53:12 pm »
... Can you post the OnDrawCell code? Is this where the yellow cell (the one with "Survey and Layout") is painted? I am almost 100% sure that the version change of Lazarus not the problem.
Thanks for your interest wp.  The first four columns are the only manually editable cells  The yellow background is set in all four columns by OnSetEditor when the cell is entered and not by OnDrawCell or any other event.  The cell returns to its default black on white format when the cell is exited.  OnDrawCell only formats column 3 by hiding the cell's numeric value and displaying the Percent Complete value and graphic bar.  Following is the code for that.  Since the code compiles and runs under both versions but the display is incorrect on the latest version is there possibly a project/IDE setting I'm missing?  One that was set differently on the old version.  Whenever I install a new version I'm careful to check those settings.  It looks like I need to try Version 1.8.
Code: Pascal  [Select][+][-]
  1.     == procedure TMainForm.sgMainDrawCell(Sender: TObject; aCol, aRow: integer; aRect: TRect; aState: TGridDrawState); ==
  2.     ...
  3.     {Display Percent Complete value/bar if required}
  4.     if (aCol = 3) and not IsHdr and not IsTimeLine then begin
  5.       if (Cells[aCol, aRow] <> '') then begin
  6.         {Format display text and compute bar width}
  7.         CellText := sgMain.Cells[aCol, aRow];
  8.         Percent := Round(StrToFloat(CellText));
  9.         CellText := FloatToStr(Percent) + '%';
  10.         Percent := Percent / 100;
  11.         BarWidth := Round((aRect.Right - aRect.Left)  * Percent);
  12.         MergeRect := Rect(aRect.Left + 1, aRect.Top + 0,
  13.                      aRect.Right - 2, aRect.Bottom - 1);
  14.         x := MergeRect.Left;
  15.         y := MergeRect.Top;
  16.         BarRect := Rect(aRect.Left, aRect.Top + 12,
  17.                    MergeRect.Left + BarWidth, MergeRect.Bottom - dx);
  18.        {Hide the cell value}
  19.        Canvas.Brush.Color := clWhite;
  20.        Canvas.Brush.Style := bsSolid;
  21.        Canvas.FillRect(MergeRect);
  22.        {Display the cell text}
  23.        Canvas.Font.Name := 'Schedule';
  24.        Canvas.Font.Size := 8;
  25.        Canvas.TextOut(x, y, CellText);
  26.        {Display bar}
  27.        Canvas.Brush.Color := clBlack;
  28.        Canvas.FillRect(BarRect);
  29.       end;
  30.     end;
  31.     ...
  32.  
Windows 7 Home Premium 64 bit - SP 1
Lazarus Version #: 1.8.0; FPC Version: 3.0.4
SVN Revision 56594
i386-win32-win32/win64

wp

  • Hero Member
  • *****
  • Posts: 11854
Re: Stringgrid Edit works differently in Lazarus Version 1.6.4.
« Reply #7 on: April 21, 2017, 07:11:55 pm »
The yellow background is set in all four columns by OnSetEditor when the cell is entered and not by OnDrawCell or any other event.  The cell returns to its default black on white format when the cell is exited. 
This looks strange. Painting code should only be executed within an event called from the Paint method. This is OnDrawCell or OnPrepareCanvas, but not OnSetEditor. It is pure incidence that the cell is painted in yellow at all.

What you can do is:
  • Set a status variable "EditingYellowCell"  to true in OnSetEditor and set it to false in OnHideEditor. Every time call Grid.Invalidate afterwards.
  • In OnPrepareCanvas you can set Grid.Canvas.Brush.Color to clYellow for those cells if  that variable is true, it will be called during the repaint cycle triggered by Grid.Invalidate.
[EDIT]
There's no need for status variable, there is already StringGrid.EditorMode which does the same.
« Last Edit: April 21, 2017, 10:04:42 pm by wp »

wp

  • Hero Member
  • *****
  • Posts: 11854
Re: Stringgrid Edit works differently in Lazarus Version 1.6.4.
« Reply #8 on: April 21, 2017, 10:05:32 pm »
I think i know now what you do: you set the Color of the Grid's Editor to clYellow? Well, the cell editor is a bit smaller than the cell. And I remember there were some changes with positioning of the editor within the cell. Therefore, there is a white margin around the editor

One way to get a uniform yellow background of the editing cell would be to paint also the cell background in yellow while the grid is in edit mode.

Code: Pascal  [Select][+][-]
  1. procedure TForm1.StringGrid2PrepareCanvas(sender: TObject; aCol, aRow: Integer;
  2.   aState: TGridDrawState);
  3. begin
  4.   if StringGrid2.EditorMode and (StringGrid2.Row = aRow) and (StringGrid2.Col = ACol) then
  5.     StringGrid2.Canvas.Brush.Color := clYellow;
  6. end;
  7.  
  8. procedure TForm1.StringGrid2SelectEditor(Sender: TObject; aCol, aRow: Integer;
  9.   var Editor: TWinControl);
  10. begin
  11.   Editor.Color := clYellow;
  12. end;
  13.  
« Last Edit: April 21, 2017, 11:50:52 pm by wp »

Gary Randall

  • Jr. Member
  • **
  • Posts: 70
Re: Stringgrid Edit works differently in Lazarus Version 1.6.4.
« Reply #9 on: April 23, 2017, 04:40:30 pm »
I think i know now what you do: you set the Color of the Grid's Editor to clYellow? Well, the cell editor is a bit smaller than the cell. And I remember there were some changes with positioning of the editor within the cell. Therefore, there is a white margin around the editor

One way to get a uniform yellow background of the editing cell would be to paint also the cell background in yellow while the grid is in edit mode.

Yes wp, that nailed it (almost)!  Painting the background in OnPrepareCanvas fills the yellow background properly.  However, the editor positioning is still clipping the text.  I made a test stringgrid with data and added just my OnSelectEditor.  I should have done that earlier.  The editor positioning and coloring works the same as the earlier Lazarus version.  Changes to the editor in the latest version may be causing the difference but now I'm not convinced it's the editor.  In my test I used the stringgrid's default scrolling keys.  I have so many things going on in my program to get the cell selection to react to various keys, select the proper context menu, and focus the data cells and calendar cells differently that I'm pretty sure it's somewhere else.  I'll try to isolate my code that might be doing that and post my findings.  Thanks for all your help.
Windows 7 Home Premium 64 bit - SP 1
Lazarus Version #: 1.8.0; FPC Version: 3.0.4
SVN Revision 56594
i386-win32-win32/win64

jesusr

  • Sr. Member
  • ****
  • Posts: 484
Re: Stringgrid Edit works differently in Lazarus Version 1.6.4.
« Reply #10 on: April 23, 2017, 10:36:05 pm »
The current behavior is the result of fixing this bug: http://bugs.freepascal.org/view.php?id=29196

The problem with the fix is that it uses the grid.canvas properties for resizing the editor, but the grid canvas properties are different depending on if you are in the paint cycle (and this is where the OnPrepareCanvas may modify the canvas) or not. Also the grid now checks the text layout of the cell and it will position the editor according to this text layout (note that this text layout can only be defined if you use custom columns, check grids reference page: http://wiki.lazarus.freepascal.org/Grids_Reference_Page, if you are not using custom columns, then the default text layout is always tlCenter). I mention the text layout because in the old situation it was always assumed to be tlTop. If you are not using custom columns, you can also modify the text layout in the OnPrepareCanvas event, but again it will be ignored just like with the font properties. On top on that, OnSelectEditor is not triggered while the grid is in the paint cycle but that doesn't matter because the new code will not use the editor font for resizing the editor, it want to use the grid.canvas :).

How to fix it. Well, according to the code, if you instead of adjusting the font properties of the editor change the font of the grid with the same values AND use custom columns and you set (for the affected column) a TextLayout=tlTop, then it should work more or less like before.
« Last Edit: April 23, 2017, 10:52:14 pm by jesusr »

Gary Randall

  • Jr. Member
  • **
  • Posts: 70
Re: Stringgrid Edit works differently in Lazarus Version 1.6.4.
« Reply #11 on: April 24, 2017, 03:38:32 am »
The current behavior is the result of fixing this bug: http://bugs.freepascal.org/view.php?id=29196 . . . .

Thanks jesusr!  This explains a lot and fits my findings.  My test program that works does not use custom columns and works perfectly, just like my program on the older Lazarus version.  My program does use custom columns so it sounds like the text alignment is affected in the latest version.  I will try adjusting the text alignment as you suggest.
Windows 7 Home Premium 64 bit - SP 1
Lazarus Version #: 1.8.0; FPC Version: 3.0.4
SVN Revision 56594
i386-win32-win32/win64

 

TinyPortal © 2005-2018