fix the orientation combobox in the print dialog

Changing the orientation value to anything else than 'Automatic'
didn't do anything by default. This was because by default
neither isPaperSizeFromUser() nor getPapersizeFromSetup()
were set, so PrintDialog::setPaperOrientation() did nothing.
It looks to me like 8cbdc6a068ad88fc43a98bd0f88 that introduced it
was rather broken (not just this bug, but also e.g. the ugly
modifying of the paper sizes by non-const reference from a const
function). In fact this whole stuff still looks broken to me, why
does it change paper size instead of just setting the orientation?
It seems like the orientation gets reset, or maybe the setting
was just a band-aid. I don't know how to fix that all though.

Change-Id: If5fdf4c47e06f2b0797d27126d21b3451b8334cf
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/129239
Tested-by: Jenkins
Reviewed-by: Luboš Luňák <l.lunak@collabora.com>
diff --git a/include/vcl/print.hxx b/include/vcl/print.hxx
index 918c205..e919b8e 100644
--- a/include/vcl/print.hxx
+++ b/include/vcl/print.hxx
@@ -468,10 +468,8 @@ public:
    VCL_DLLPRIVATE    void              setReversePrint( bool i_bReverse );
    VCL_DLLPRIVATE    void              setPapersizeFromSetup( bool i_bPapersizeFromSetup );
    VCL_DLLPRIVATE    bool              getPapersizeFromSetup() const;
    VCL_DLLPRIVATE    Size&             getPaperSizeSetup() const;
    VCL_DLLPRIVATE    void              setPaperSizeFromUser( Size i_aUserSize );
    VCL_DLLPRIVATE    Size&             getPaperSizeFromUser() const;
    VCL_DLLPRIVATE    bool              isPaperSizeFromUser() const;
    VCL_DLLPRIVATE    void              setOrientationFromUser( Orientation eOrientation, bool set );
                      void              setPrinterModified( bool i_bPapersizeFromSetup );
                      bool              getPrinterModified() const;
    VCL_DLLPRIVATE    void              pushPropertiesToPrinter();
diff --git a/vcl/inc/printdlg.hxx b/vcl/inc/printdlg.hxx
index 21aab36..5b96417 100644
--- a/vcl/inc/printdlg.hxx
+++ b/vcl/inc/printdlg.hxx
@@ -231,10 +231,9 @@ namespace vcl
        void setupPaperSidesBox();
        void storeToSettings();
        void readFromSettings();
        void setPaperOrientation( Orientation eOrientation );
        void setPaperOrientation( Orientation eOrientation, bool fromUser );
        void updateOrientationBox( bool bAutomatic = true );
        bool hasOrientationChanged() const;
        void checkPaperSize( Size& rPaperSize );
        void setPreviewText();
        void updatePrinterText();
        void checkControlDependencies();
diff --git a/vcl/source/gdi/print3.cxx b/vcl/source/gdi/print3.cxx
index f33d598..aecc43e1 100644
--- a/vcl/source/gdi/print3.cxx
+++ b/vcl/source/gdi/print3.cxx
@@ -158,6 +158,7 @@ public:
    bool                                                        mbReversePageOrder;
    bool                                                        mbPapersizeFromSetup;
    bool                                                        mbPapersizeFromUser;
    bool                                                        mbOrientationFromUser;
    bool                                                        mbPrinterModified;
    css::view::PrintableState                                   meJobState;

@@ -171,6 +172,8 @@ public:
    Size                                                        maDefaultPageSize;
    // set by user through print dialog
    Size                                                        maUserPageSize;
    // set by user through print dialog
    Orientation                                                 meUserOrientation;
    // set by user through printer properties subdialog of printer settings dialog
    sal_Int32                                                   mnDefaultPaperBin;
    // Set by user through printer properties subdialog of print dialog.
@@ -197,6 +200,7 @@ public:
        mbReversePageOrder( false ),
        mbPapersizeFromSetup( false ),
        mbPapersizeFromUser( false ),
        mbOrientationFromUser( false ),
        mbPrinterModified( false ),
        meJobState( css::view::PrintableState_JOB_STARTED ),
        mnDefaultPaperBin( -1 ),
@@ -212,15 +216,27 @@ public:
        }
    }

    const Size& getRealPaperSize( const Size& i_rPageSize, bool bNoNUP ) const
    Size getRealPaperSize( const Size& i_rPageSize, bool bNoNUP ) const
    {
        Size size;
        if ( mbPapersizeFromUser )
            return maUserPageSize;
        if( mbPapersizeFromSetup )
            return maDefaultPageSize;
        if( maMultiPage.nRows * maMultiPage.nColumns > 1 && ! bNoNUP )
            return maMultiPage.aPaperSize;
        return i_rPageSize;
            size = maUserPageSize;
        else if( mbPapersizeFromSetup )
            size =  maDefaultPageSize;
        else if( maMultiPage.nRows * maMultiPage.nColumns > 1 && ! bNoNUP )
            size = maMultiPage.aPaperSize;
        else
            size = i_rPageSize;
        if(mbOrientationFromUser)
        {
            if ( (meUserOrientation == Orientation::Portrait && size.Width() > size.Height()) ||
                 (meUserOrientation == Orientation::Landscape && size.Width() < size.Height()) )
            {
                // coverity[swapped-arguments : FALSE] - this is in the correct order
                size = Size( size.Height(), size.Width() );
            }
        }
        return size;
    }
    PrinterController::PageSize modifyJobSetup( const css::uno::Sequence< css::beans::PropertyValue >& i_rProps );
    void resetPaperToLastConfigured();
@@ -824,6 +840,7 @@ void PrinterController::setPrinter( const VclPtr<Printer>& i_rPrinter )
    }

    mpImplData->mbPapersizeFromUser = false;
    mpImplData->mbOrientationFromUser = false;
    mpImplData->mxPrinter->Pop();
    mpImplData->mnFixedPaperBin = -1;
}
@@ -1407,7 +1424,10 @@ void PrinterController::setPapersizeFromSetup( bool i_bPapersizeFromSetup )
    mpImplData->mbPapersizeFromSetup = i_bPapersizeFromSetup;
    mpImplData->mxPrinter->SetPrinterSettingsPreferred( i_bPapersizeFromSetup );
    if ( i_bPapersizeFromSetup )
        mpImplData->mbPapersizeFromUser = !i_bPapersizeFromSetup;
    {
        mpImplData->mbPapersizeFromUser = false;
        mpImplData->mbOrientationFromUser = false;
    }
}

bool PrinterController::getPapersizeFromSetup() const
@@ -1415,11 +1435,6 @@ bool PrinterController::getPapersizeFromSetup() const
    return mpImplData->mbPapersizeFromSetup;
}

Size& PrinterController::getPaperSizeSetup() const
{
    return mpImplData->maDefaultPageSize;
}

void PrinterController::setPaperSizeFromUser( Size i_aUserSize )
{
    mpImplData->mbPapersizeFromUser = true;
@@ -1429,14 +1444,10 @@ void PrinterController::setPaperSizeFromUser( Size i_aUserSize )
    mpImplData->maUserPageSize = i_aUserSize;
}

Size& PrinterController::getPaperSizeFromUser() const
void PrinterController::setOrientationFromUser( Orientation eOrientation, bool set )
{
    return mpImplData->maUserPageSize;
}

bool PrinterController::isPaperSizeFromUser() const
{
    return mpImplData->mbPapersizeFromUser;
    mpImplData->mbOrientationFromUser = set;
    mpImplData->meUserOrientation = eOrientation;
}

void PrinterController::setPrinterModified( bool i_bPrinterModified )
diff --git a/vcl/source/window/printdlg.cxx b/vcl/source/window/printdlg.cxx
index e98afe8..bb89ebb 100644
--- a/vcl/source/window/printdlg.cxx
+++ b/vcl/source/window/printdlg.cxx
@@ -1025,35 +1025,12 @@ bool PrintDialog::hasOrientationChanged() const
        || (nOrientation == ORIENTATION_PORTRAIT && eOrientation == Orientation::Landscape);
}

// make sure paper size matches paper orientation
void PrintDialog::checkPaperSize( Size& rPaperSize )
{
    Orientation eOrientation = maPController->getPrinter()->GetOrientation();
    if ( (eOrientation == Orientation::Portrait && rPaperSize.Width() > rPaperSize.Height()) ||
         (eOrientation == Orientation::Landscape && rPaperSize.Width() < rPaperSize.Height()) )
    {
        // coverity[swapped-arguments : FALSE] - this is in the correct order
        rPaperSize = Size( rPaperSize.Height(), rPaperSize.Width() );
    }
}

// Always use this function to set paper orientation to make sure everything behaves well
void PrintDialog::setPaperOrientation( Orientation eOrientation )
void PrintDialog::setPaperOrientation( Orientation eOrientation, bool fromUser )
{
    VclPtr<Printer> aPrt( maPController->getPrinter() );
    aPrt->SetOrientation( eOrientation );

    // check if it's necessary to swap width and height of paper
    if ( maPController->isPaperSizeFromUser() )
    {
        Size& aPaperSize = maPController->getPaperSizeFromUser();
        checkPaperSize( aPaperSize );
    }
    else if ( maPController->getPapersizeFromSetup() )
    {
        Size& aPaperSize = maPController->getPaperSizeSetup();
        checkPaperSize( aPaperSize );
    }
    maPController->setOrientationFromUser( eOrientation, fromUser );
}

void PrintDialog::checkControlDependencies()
@@ -1174,12 +1151,12 @@ void PrintDialog::updateNup( bool i_bMayUseCache )
        if( aMultiSize.Width() > aMultiSize.Height() ) // fits better on landscape
        {
            aMPS.aPaperSize = maNupLandscapeSize;
            setPaperOrientation( Orientation::Landscape );
            setPaperOrientation( Orientation::Landscape, false );
        }
        else
        {
            aMPS.aPaperSize = maNupPortraitSize;
            setPaperOrientation( Orientation::Portrait );
            setPaperOrientation( Orientation::Portrait, false );
        }
    }

@@ -2000,7 +1977,7 @@ IMPL_LINK( PrintDialog, SelectHdl, weld::ComboBox&, rBox, void )
    {
        int nOrientation = mxOrientationBox->get_active();
        if ( nOrientation != ORIENTATION_AUTOMATIC )
            setPaperOrientation( static_cast<Orientation>( nOrientation - 1 ) );
            setPaperOrientation( static_cast<Orientation>( nOrientation - 1 ), true );

        updateNup( false );
    }
@@ -2026,9 +2003,7 @@ IMPL_LINK( PrintDialog, SelectHdl, weld::ComboBox&, rBox, void )
        else
            aPrt->SetPaper( mePaper );

        Size aPaperSize( aInfo.getWidth(), aInfo.getHeight() );
        checkPaperSize( aPaperSize );
        maPController->setPaperSizeFromUser( aPaperSize );
        maPController->setPaperSizeFromUser( Size( aInfo.getWidth(), aInfo.getHeight() ) );

        maUpdatePreviewIdle.Start();
    }