tdf#124983 In calc make printable page borders initially visible

If option "LibreOfficeDev Calc/View/Page breaks"
is enabled, breaks should be visible. But if the document
is opened the first time or a tab is opened the first time,
the breaks are not calculated yet and therefore not visible.
To avoid calculations during the load of the document, a
timer will be triggered to calculate the breaks after loading
is finished.

Change-Id: I87219c62e20882b19d36042692efd2e6a8d8190a
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/90101
Tested-by: Jenkins
Reviewed-by: Eike Rathke <erack@redhat.com>
(cherry picked from commit 86e300df241312f6152da1cfa3cb2b8c668d1df5)
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/117718
diff --git a/sc/source/ui/inc/gridwin.hxx b/sc/source/ui/inc/gridwin.hxx
index 09e46d7..daf6c35 100644
--- a/sc/source/ui/inc/gridwin.hxx
+++ b/sc/source/ui/inc/gridwin.hxx
@@ -309,6 +309,10 @@ class SAL_DLLPUBLIC_RTTI ScGridWindow : public vcl::Window, public DropTargetHel
    void            InvalidateLOKViewCursor(const tools::Rectangle& rCursorRect,
                                            const Fraction aScaleX, const Fraction aScaleY);

    Timer           maShowPageBreaksTimer;
    bool            bInitialPageBreaks;
    void            SetupInitialPageBreaks(ScDocument& rDoc, SCTAB nTab, bool bSetup);
    DECL_LINK(InitiatePageBreaksTimer, Timer*, void);
protected:
    virtual void    PrePaint(vcl::RenderContext& rRenderContext) override;
    virtual void    Paint(vcl::RenderContext& rRenderContext, const tools::Rectangle& rRect) override;
@@ -490,6 +494,8 @@ public:
    void updateLOKValListButton(bool bVisible, const ScAddress& rPos) const;
    void updateLOKInputHelp(const OUString& title, const OUString& content) const;

    void initiatePageBreaks();

protected:
    void ImpCreateOverlayObjects();
    void ImpDestroyOverlayObjects();
diff --git a/sc/source/ui/view/gridwin.cxx b/sc/source/ui/view/gridwin.cxx
index b6412ae..c2c115c 100644
--- a/sc/source/ui/view/gridwin.cxx
+++ b/sc/source/ui/view/gridwin.cxx
@@ -424,6 +424,10 @@ ScGridWindow::ScGridWindow( vcl::Window* pParent, ScViewData& rData, ScSplitPos 

    GetOutDev()->SetDigitLanguage( SC_MOD()->GetOptDigitLanguage() );
    EnableRTL( false );

    bInitialPageBreaks = true;
    maShowPageBreaksTimer.SetInvokeHandler(LINK(this, ScGridWindow, InitiatePageBreaksTimer));
    maShowPageBreaksTimer.SetTimeout(1);
}

ScGridWindow::~ScGridWindow()
diff --git a/sc/source/ui/view/gridwin4.cxx b/sc/source/ui/view/gridwin4.cxx
index 9c1ff47..f1e53ed 100644
--- a/sc/source/ui/view/gridwin4.cxx
+++ b/sc/source/ui/view/gridwin4.cxx
@@ -76,6 +76,9 @@
#include <vcl/virdev.hxx>
#include <svx/sdrpaintwindow.hxx>
#include <drwlayer.hxx>
#include <columnspanset.hxx>
#include <docfunc.hxx>
#include <printfun.hxx>

static void lcl_LimitRect( tools::Rectangle& rRect, const tools::Rectangle& rVisible )
{
@@ -1267,6 +1270,30 @@ void ScGridWindow::DrawContent(OutputDevice &rDevice, const ScTableInfo& rTableI

    if (mpNoteMarker)
        mpNoteMarker->Draw(); // Above the cursor, in drawing map mode

    SetupInitialPageBreaks(rDoc, nTab, bPage&& bInitialPageBreaks);
}


void ScGridWindow::SetupInitialPageBreaks(ScDocument& rDoc, SCTAB nTab, bool bSetup)
{
    // tdf#124983, if option LibreOfficeDev Calc/View/Visual Aids/Page breaks
    // is enabled, breaks should be visible. If the document is opened the first
    // time, the breaks are not calculated yet, so for this initialization
    // a timer will be triggered here.
    if (bSetup)
    {
        std::set<SCCOL> aColBreaks;
        std::set<SCROW> aRowBreaks;
        rDoc.GetAllColBreaks(aColBreaks, nTab, true, false);
        rDoc.GetAllRowBreaks(aRowBreaks, nTab, true, false);
        if (aColBreaks.size() == 0 || aRowBreaks.size() == 0)
        {
            maShowPageBreaksTimer.SetPriority(TaskPriority::DEFAULT_IDLE);
            maShowPageBreaksTimer.Start();
            bInitialPageBreaks = false;
        }
    }
}

namespace
@@ -2316,4 +2343,39 @@ void ScGridWindow::DataChanged( const DataChangedEvent& rDCEvt )
    Invalidate();
}

void ScGridWindow::initiatePageBreaks()
{
    bInitialPageBreaks = true;
}

IMPL_LINK(ScGridWindow, InitiatePageBreaksTimer, Timer*, pTimer, void)
{
    if (pTimer == &maShowPageBreaksTimer)
    {
        ScDocument& rDoc = mrViewData.GetDocument();
        const ScViewOptions& rOpts = mrViewData.GetOptions();
        bool bPage = rOpts.GetOption(VOPT_PAGEBREAKS);
        ScDocShell* pDocSh = mrViewData.GetDocShell();
        bool bModified = pDocSh->IsModified();
        // tdf#124983, if option LibreOfficeDev Calc/View/Visual Aids/Page breaks
        // is enabled, breaks should be visible. If the document is opened the first
        // time or a tab is activated the first time, the breaks are not calculated
        // yet, so this initialization is done here.
        if (bPage)
        {
            SCTAB nCurrentTab = mrViewData.GetTabNo();
            Size pagesize = rDoc.GetPageSize(nCurrentTab);
            if (pagesize.IsEmpty())
            {
                ScPrintFunc(pDocSh, pDocSh->GetPrinter(), nCurrentTab);
                rDoc.SetPageSize(nCurrentTab, rDoc.GetPageSize(nCurrentTab));
            }
            rDoc.UpdatePageBreaks(nCurrentTab);
            pDocSh->PostPaint(0, 0, nCurrentTab, rDoc.MaxCol(), rDoc.MaxRow(), nCurrentTab, PaintPartFlags::Grid);
            pDocSh->SetModified(bModified);
        }
    }
}


/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sc/source/ui/view/tabview5.cxx b/sc/source/ui/view/tabview5.cxx
index 3de36af..f0b6ed6 100644
--- a/sc/source/ui/view/tabview5.cxx
+++ b/sc/source/ui/view/tabview5.cxx
@@ -320,6 +320,13 @@ void ScTabView::TabChanged( bool bSameTabButMoved )
        }
    }

    for (int i = 0; i < 4; i++)
        if (pGridWin[i])
        {
            pGridWin[i]->initiatePageBreaks();
        }


    if (!comphelper::LibreOfficeKit::isActive())
        return;