Recent

Author Topic: Issue with TGridPrinter and no printer  (Read 1237 times)

sfeinst

  • Full Member
  • ***
  • Posts: 237
Issue with TGridPrinter and no printer
« on: January 29, 2024, 07:48:40 pm »
I am using Lazarus 3.99 on Debian Stable (Bookworm).  I pulled Lazarus from GitHub and compiled it.  I pulled TGridPrinter from SourceForge (v0.50) and installed/built the package.

I added a gridprinter and gridprinterpreview to my app, connected everything and ran, but when I try to print or execute preview, the app crashes (actually it hangs for a long time as it prints out the stack trace).

After much fiddling around, I finally figured out it's because Debian by default does not install a printer (I thought it installed a PDF printer as I saw it available in some apps, but I think those are either created by the app or is part of Gnome).  After I installed cups-pdf so that I actually had a printer, it all worked.

So, to my question.  Is there a way for me to determine if no printer is either selected or is default, prior to when TGridPrinter would get to whatever code is crashing (when I traced it seemed to be somewhere in the area of: line 1084 of source/gridprn.pas) according to the stack trace.

I'm thinking maybe an event that gets called after the printer selection dialog but before prepare canvas and maybe a property I can check.  Or possibly I missed a setting which would tell the gridprinter not to conintue without a valid printer?

wp

  • Hero Member
  • *****
  • Posts: 12761
Re: Issue with TGridPrinter and no printer
« Reply #1 on: January 29, 2024, 10:25:26 pm »
I think the component should check Printer.Printers.Count which is zero when no printer is installed. A possible place for this check is at the beginning of the TGridPrinter.Print method, and I think it should fire an exception so that the application writer can decide in the exception handler how to react:

Code: Pascal  [Select][+][-]
  1. procedure TGridPrinter.Print;
  2. ...
  3. begin
  4.   if FGrid = nil then
  5.     exit;
  6.  
  7.   if Printer.Printers.Count = 0 then
  8.     raise EPrinter.Create('No printer defined.');
  9.  
  10.   SetGrid(FGrid);
  11.   ...

sfeinst

  • Full Member
  • ***
  • Posts: 237
Re: Issue with TGridPrinter and no printer
« Reply #2 on: January 30, 2024, 02:11:48 pm »
I tested this out and it works if I call gridprinter.Print.  But if I call gridPrintPreview.Execute, the app crashes.  So the print preview must be hitting the no printer before gridprinter.print does anything.

Instead of modifying code in multiple paces, I just added:
  if Printer.Printers.Count = 0 then

directly to my code before calling anything related to TGridPrinter or preview.

wp

  • Hero Member
  • *****
  • Posts: 12761
Re: Issue with TGridPrinter and no printer
« Reply #3 on: January 30, 2024, 06:52:47 pm »
Instead of modifying code in multiple paces, I just added:
  if Printer.Printers.Count = 0 then

directly to my code before calling anything related to TGridPrinter or preview.
Just for completeness: I just committed a version in which the preview performs the same check in its Execute method, and I think (hope...) that it's all set now. But to perform the check for installed printers by yourself is probably a better option than to handle the exception which I have to raise in this error case.

sfeinst

  • Full Member
  • ***
  • Posts: 237
Re: Issue with TGridPrinter and no printer
« Reply #4 on: January 30, 2024, 07:55:14 pm »
I'll try to test this once I get thru with some coding.  Appreciate the turnaround

 

TinyPortal © 2005-2018