Recent

Author Topic: Grids shows error when compiling  (Read 266 times)

abtaylr

  • Jr. Member
  • **
  • Posts: 87
Grids shows error when compiling
« on: July 26, 2019, 08:59:46 pm »
I am working on updating an application originally done in Delphi. As I have been working through debugging various issues, the compiler began giving an error from the Grids procedures shown beginning at line 2239:

Code: Pascal  [Select]
  1. procedure CfgSetFontValue(cfg: TXMLConfig; AKey: WideString; AFont: TFont);
  2. begin
  3.   cfg.SetValue(AKey + '/name/value', AFont.Name);
  4.   cfg.SetValue(AKey + '/size/value', AFont.Size);
  5.   cfg.SetValue(AKey + '/color/value', ColorToString(AFont.Color));
  6.   cfg.SetValue(AKey + '/style/value', Integer(AFont.Style));
  7. end;
  8.  
  9. procedure CfgGetFontValue(cfg: TXMLConfig; AKey: WideString; AFont: TFont);
  10. begin
  11.   AFont.Name := cfg.GetValue(AKey + '/name/value', 'default');
  12.   AFont.Size := cfg.GetValue(AKey + '/size/value', 0);
  13.   AFont.Color:= StringToColor(cfg.GetValue(AKey + '/color/value', 'clWindowText'));
  14.    AFont.Style:= TFontStyles(cfg.GetValue(AKey + '/style/value', 0));
  15. end;

The two problems arise from Integer(AFont.Style) shown in the first procedure and  AFont.Style:= TFontStyles(cfg.GetValue(AKey + '/style/value', 0)); in the second procedure.  TFontStyles comes from the Graphics unit shown at lines 71-72:

Code: Pascal  [Select]
  1.   TFontStyle = (fsBold, fsItalic, fsUnderline, fsStrikeOut);
  2.   TFontStyles = set of TFontStyle;

I looked in Pascal Reference Guide and found the following:  "The compiler stores small sets (less than 32 elements) in a LongInt if the Type Range allows it."  The error message that I am getting is : grids.pas(2044,39) Error: Illegal type conversion: "TFontStyles" to "LongInt" and "grids.pas(2052,17) Error: Illegal type conversion: "LongInt" to "TFontStyles".

I am perplexed. I don't see anything wrong in the Grids unit coding, yet I get the errors.

wp

  • Hero Member
  • *****
  • Posts: 6149
Re: Grids shows error when compiling
« Reply #1 on: July 26, 2019, 10:05:20 pm »
Which Lazarus and FPC version are you using? The current release versions have different code beginning at line 2239 of unit grids.pas.
Lazarus trunk / fpc 3.0.4 / all 32-bit on Win-10

howardpc

  • Hero Member
  • *****
  • Posts: 3097
Re: Grids shows error when compiling
« Reply #2 on: July 26, 2019, 10:07:48 pm »
You can rarely if ever cast a set to an integer type, whatever the details of a set's implementation might be, partly because the size the compiler chooses for the set is unknown.
Where you are certain that a small set is involved, you may be able to do an implicit cast something along these lines (untested):
Code: Pascal  [Select]
  1. procedure CfgSetFontValue(cfg: TXMLConfig; AKey: WideString; AFont: TFont);
  2. var
  3.   tmp: TFontStyles;
  4.   tmpDWord: DWord absolute tmp;
  5.   begin
  6.     cfg.SetValue(AKey + '/name/value', AFont.Name);
  7.     cfg.SetValue(AKey + '/size/value', AFont.Size);
  8.     cfg.SetValue(AKey + '/color/value', ColorToString(AFont.Color));
  9.     tmp := AFont.Style;
  10.     cfg.SetValue(AKey + '/style/value', tmpDWord);
  11.   end;
  12.  
  13. procedure CfgGetFontValue(cfg: TXMLConfig; AKey: WideString; AFont: TFont);
  14. var
  15.   tmp: DWord;
  16.   tmpStyles: TFontStyles absolute tmp;
  17. begin
  18.   AFont.Name := cfg.GetValue(AKey + '/name/value', 'default');
  19.   AFont.Size := cfg.GetValue(AKey + '/size/value', 0);
  20.   AFont.Color:= StringToColor(cfg.GetValue(AKey + '/color/value', 'clWindowText'));
  21.   tmp := cfg.GetValue(AKey + '/style/value', 0);
  22.   AFont.Style:= tmpStyles;
  23. end;

abtaylr

  • Jr. Member
  • **
  • Posts: 87
Re: Grids shows error when compiling
« Reply #3 on: July 27, 2019, 02:04:53 am »
Regarding the fpc and lazarus versions used.  FPC is 3.0.5 r42498 and Laz 2.0.2 r61624. I am using Kubuntu 64bit v.19.04

I'll try out the suggested code and see how it fairs.

Thanks
abt

abtaylr

  • Jr. Member
  • **
  • Posts: 87
Re: Grids shows error when compiling
« Reply #4 on: July 27, 2019, 02:07:54 am »
We have a winner! I have successfully moved to my next error.

Thanks for the assistance.