vcl/osx/salinst.cxx       |   44 +++++++++++++++++++++++++++++++++++++++++---
 vcl/source/gdi/print3.cxx |   10 ++++++++++
 2 files changed, 51 insertions(+), 3 deletions(-)

New commits:
commit 25040486c00c96198165ff15d461815067d2efcc
Author:     Patrick Luby <[email protected]>
AuthorDate: Wed Dec 7 11:40:18 2022 -0500
Commit:     Xisco Fauli <[email protected]>
CommitDate: Wed Dec 14 08:25:26 2022 +0000

    tdf#151700 Display native print panel even if there are no printers
    
    Prevent the non-native LibreOffice PrintDialog from displaying by creating 
a fake printer if there are no printers. This will allow the LibreOffice 
printing code to proceed with native NSPrintOperation which will display the 
native print panel.
    
    Change-Id: Iee13305520360b0165464889f0ee51b1207dd5ce
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/143794
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <[email protected]>
    (cherry picked from commit 229b0ce8d8453960c213da59770b8bb7b6dca895)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/144032
    Reviewed-by: Xisco Fauli <[email protected]>

diff --git a/vcl/osx/salinst.cxx b/vcl/osx/salinst.cxx
index 22a024bc265d..e8a4a94efc08 100644
--- a/vcl/osx/salinst.cxx
+++ b/vcl/osx/salinst.cxx
@@ -60,6 +60,7 @@
 #include <osx/runinmain.hxx>
 
 #include <print.h>
+#include <strings.hrc>
 
 #include <comphelper/processfactory.hxx>
 
@@ -125,6 +126,20 @@ public:
 
 }
 
+static OUString& getFallbackPrinterName()
+{
+    static OUString aFallbackPrinter;
+
+    if ( aFallbackPrinter.isEmpty() )
+    {
+        aFallbackPrinter = VclResId( SV_PRINT_DEFPRT_TXT );
+        if ( aFallbackPrinter.isEmpty() )
+            aFallbackPrinter = "Printer";
+    }
+
+    return aFallbackPrinter;
+}
+
 void AquaSalInstance::delayedSettingsChanged( bool bInvalidate )
 {
     osl::Guard< comphelper::SolarMutex > aGuard( *GetYieldMutex() );
@@ -765,6 +780,20 @@ void AquaSalInstance::GetPrinterQueueInfo( 
ImplPrnQueueList* pList )
             pList->Add( std::move(pInfo) );
         }
     }
+
+    // tdf#151700 Prevent the non-native LibreOffice PrintDialog from
+    // displaying by creating a fake printer if there are no printers. This
+    // will allow the LibreOffice printing code to proceed with native
+    // NSPrintOperation which will display the native print panel.
+    if ( !nNameCount )
+    {
+        std::unique_ptr<SalPrinterQueueInfo> pInfo(new SalPrinterQueueInfo);
+        pInfo->maPrinterName    = getFallbackPrinterName();
+        pInfo->mnStatus         = PrintQueueFlags::NONE;
+        pInfo->mnJobs           = 0;
+
+        pList->Add( std::move(pInfo) );
+    }
 }
 
 void AquaSalInstance::GetPrinterQueueState( SalPrinterQueueInfo* )
@@ -776,7 +805,9 @@ OUString AquaSalInstance::GetDefaultPrinter()
     // #i113170# may not be the main thread if called from UNO API
     SalData::ensureThreadAutoreleasePool();
 
-    if( maDefaultPrinter.isEmpty() )
+    // WinSalInstance::GetDefaultPrinter() fetches current default printer
+    // on every call so do the same here
+    OUString aDefaultPrinter;
     {
         NSPrintInfo* pPI = [NSPrintInfo sharedPrintInfo];
         SAL_WARN_IF( !pPI, "vcl", "no print info" );
@@ -786,13 +817,20 @@ OUString AquaSalInstance::GetDefaultPrinter()
             SAL_WARN_IF( !pPr, "vcl", "no printer in default info" );
             if( pPr )
             {
+                // Related: tdf#151700 Return the name of the fake printer if
+                // there are no printers so that the LibreOffice printing code
+                // will be able to find the the fake printer returned by
+                // AquaSalInstance::GetPrinterQueueInfo()
                 NSString* pDefName = [pPr name];
                 SAL_WARN_IF( !pDefName, "vcl", "printer has no name" );
-                maDefaultPrinter = GetOUString( pDefName );
+                if ( pDefName && [pDefName length])
+                    aDefaultPrinter = GetOUString( pDefName );
+                else
+                    aDefaultPrinter = getFallbackPrinterName();
             }
         }
     }
-    return maDefaultPrinter;
+    return aDefaultPrinter;
 }
 
 SalInfoPrinter* AquaSalInstance::CreateInfoPrinter( SalPrinterQueueInfo* 
pQueueInfo,
diff --git a/vcl/source/gdi/print3.cxx b/vcl/source/gdi/print3.cxx
index e8acbcd38297..dc2fc5e74d35 100644
--- a/vcl/source/gdi/print3.cxx
+++ b/vcl/source/gdi/print3.cxx
@@ -538,6 +538,16 @@ bool 
Printer::PreparePrintJob(std::shared_ptr<PrinterController> xController,
         {
         }
     }
+#ifdef MACOSX
+    else
+    {
+        // The PrintDialog updates the printer list in its constructor so do
+        // the same for printers that bring up their own dialog since. Not
+        // sure if this is needed or not on Windows or X11, so limit only to
+        // macOS for now.
+        Printer::updatePrinters();
+    }
+#endif
 
     xController->pushPropertiesToPrinter();
     return true;

Reply via email to