vcl/source/gdi/print3.cxx      |   35 +++++++++++++++++++++++++++++++++--
 vcl/source/window/printdlg.cxx |   18 ++++++++++++++++++
 2 files changed, 51 insertions(+), 2 deletions(-)

New commits:
commit 489563aa93bff1a175006d77a917f10e4493d727
Author:     Patrick Luby <guibmac...@gmail.com>
AuthorDate: Mon Dec 30 17:12:13 2024 -0500
Commit:     Adolfo Jayme Barrientos <fit...@ubuntu.com>
CommitDate: Wed Jan 1 07:54:58 2025 +0100

    Related: tdf#159995 allow setting the page size when printing to file
    
    On macOS, printing to file just saves to PDF using a native print
    job just like the native print dialog's PDF > Save as PDF... option.
    Also, as noted in tdf#163558, the native print dialog could support
    changing the page size eventually (and already does in Apple's
    Safari and TextEdit applications) so allow changing of the page
    size in the non-native print dialog on macOS as well.
    
    Also, changing a listbox's active item does not trigger an update
    to the preview so force an update to the preview by calling each
    active listbox's select handler after changing the active printer.
    
    Lastly, when printing to file with the non-native print dialog, try
    to mimic the save dialog behavior when using the native print dialog
    to save to PDF.
    
    Change-Id: I5f42d818701cf64fdf3ecb616d1eef7b835eb2f8
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/179573
    Tested-by: Jenkins
    Reviewed-by: Patrick Luby <guibomac...@gmail.com>
    (cherry picked from commit 61f8fb648ecf9a20ee8abec0e8d3fad3e666db5e)
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/179586
    Reviewed-by: Adolfo Jayme Barrientos <fit...@ubuntu.com>

diff --git a/vcl/source/gdi/print3.cxx b/vcl/source/gdi/print3.cxx
index b813f1899e42..0cef1d915af2 100644
--- a/vcl/source/gdi/print3.cxx
+++ b/vcl/source/gdi/print3.cxx
@@ -52,6 +52,11 @@
 #include <unordered_map>
 #include <unordered_set>
 
+#ifdef MACOSX
+#include <com/sun/star/ui/dialogs/ExtendedFilePickerElementIds.hpp>
+#include <com/sun/star/ui/dialogs/XFilePickerControlAccess.hpp>
+#endif
+
 using namespace vcl;
 
 namespace {
@@ -253,7 +258,7 @@ PrinterController::PrinterController(const VclPtr<Printer>& 
i_xPrinter, weld::Wi
     mpImplData->mpWindow = i_pWindow;
 }
 
-static OUString queryFile( Printer const * pPrinter )
+static OUString queryFile( Printer const * pPrinter, const OUString & rJobName 
)
 {
     OUString aResult;
 
@@ -262,6 +267,27 @@ static OUString queryFile( Printer const * pPrinter )
 
     try
     {
+#ifdef MACOSX
+        // Try to mimic the save dialog behavior when using the native
+        // print dialog to save to PDF.
+        if( pPrinter )
+        {
+            // Set the suggested file name if possible
+            if( !rJobName.isEmpty() )
+                xFilePicker->setDefaultName( rJobName );
+
+            // macOS normally saves only to PDF
+            if( pPrinter->GetCapabilities( PrinterCapType::PDF ) )
+            {
+                xFilePicker->appendFilter( u"Portable Document Format"_ustr, 
u"*.pdf"_ustr );
+
+                css::uno::Reference< 
css::ui::dialogs::XFilePickerControlAccess > xControlAccess( xFilePicker, 
css::uno::UNO_QUERY );
+                if( xControlAccess.is() )
+                    xControlAccess->setValue( 
css::ui::dialogs::ExtendedFilePickerElementIds::CHECKBOX_AUTOEXTENSION, 0, 
css::uno::Any( true ) );
+            }
+        }
+#else
+        (void)rJobName;
 #ifdef UNX
         // add PostScript and PDF
         bool bPS = true, bPDF = true;
@@ -282,6 +308,7 @@ static OUString queryFile( Printer const * pPrinter )
 #endif
         // add arbitrary files
         xFilePicker->appendFilter(VclResId(SV_STDTEXT_ALLFILETYPES), 
u"*.*"_ustr);
+#endif
     }
     catch (const css::lang::IllegalArgumentException&)
     {
@@ -522,7 +549,11 @@ bool 
Printer::PreparePrintJob(std::shared_ptr<PrinterController> xController,
             }
             if (aDlg.isPrintToFile())
             {
-                OUString aFile = queryFile( xController->getPrinter().get() );
+                OUString aJobName;
+                css::beans::PropertyValue* pJobNameVal = 
xController->getValue( u"JobName"_ustr );
+                if( pJobNameVal )
+                    pJobNameVal->Value >>= aJobName;
+                OUString aFile = queryFile( xController->getPrinter().get(), 
aJobName );
                 if( aFile.isEmpty() )
                 {
                     xController->abortJob();
diff --git a/vcl/source/window/printdlg.cxx b/vcl/source/window/printdlg.cxx
index e194190a7d02..a06fba332d96 100644
--- a/vcl/source/window/printdlg.cxx
+++ b/vcl/source/window/printdlg.cxx
@@ -848,11 +848,20 @@ void PrintDialog::setPaperSizes()
     VclPtr<Printer> aPrt( maPController->getPrinter() );
     mePaper = aPrt->GetPaper();
 
+    // Related: tdf#159995 allow setting the page size when printing to file
+    // On macOS, printing to file just saves to PDF using a native print
+    // job just like the native print dialog's PDF > Save as PDF... option.
+    // Also, as noted in tdf#163558, the native print dialog could support
+    // changing the page size eventually (and already does in Apple's
+    // Safari and TextEdit applications) so allow changing of the page
+    // size in the non-native print dialog on macOS as well.
+#ifndef MACOSX
     if ( isPrintToFile() )
     {
         mxPaperSizeBox->set_sensitive( false );
     }
     else
+#endif
     {
         int nExactMatch = -1;
         int nSizeMatch = -1;
@@ -2029,6 +2038,15 @@ IMPL_LINK( PrintDialog, SelectHdl, weld::ComboBox&, 
rBox, void )
         }
 
         setupPaperSidesBox();
+
+        // Related: tdf#159995 changing a listbox's active item does not
+        // trigger an update to the preview so force an update to the
+        // preview by calling each active listbox's select handler after
+        // changing the active printer.
+        if ( mxOrientationBox->get_sensitive() )
+            LINK( this, PrintDialog, SelectHdl ).Call( *mxOrientationBox );
+        if ( mxPaperSizeBox->get_sensitive() )
+            LINK( this, PrintDialog, SelectHdl ).Call( *mxPaperSizeBox );
     }
     else if ( &rBox == mxPaperSidesBox.get() )
     {

Reply via email to