tdf#39302 add "alt + =" shortcut for autosum

Change-Id: I11e2f77e8d8ec81d9ea6d5bc4e8ef31ec7dedc67
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/94386
Tested-by: Jenkins
Reviewed-by: Eike Rathke <erack@redhat.com>
(cherry picked from commit f4b47f7d335227d037c2c2e9f662724d4b8c73d4)
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/94964
diff --git a/sc/source/ui/app/inputwin.cxx b/sc/source/ui/app/inputwin.cxx
index bdbf32321..cb2b00b 100644
--- a/sc/source/ui/app/inputwin.cxx
+++ b/sc/source/ui/app/inputwin.cxx
@@ -63,7 +63,6 @@
#include <rangeutl.hxx>
#include <docfunc.hxx>
#include <funcdesc.hxx>
#include <formula/opcode.hxx>
#include <editeng/fontitem.hxx>
#include <AccessibleEditObject.hxx>
#include <AccessibleText.hxx>
@@ -821,6 +820,43 @@ void ScInputWindow::MouseButtonUp( const MouseEvent& rMEvt )
    ToolBox::MouseButtonUp( rMEvt );
}

void ScInputWindow::AutoSum( bool& bRangeFinder, bool& bSubTotal, OpCode eCode )
{
    ScModule* pScMod = SC_MOD();
    ScTabViewShell* pViewSh = dynamic_cast<ScTabViewShell*>( SfxViewShell::Current()  );
    if ( pViewSh )
    {
        const OUString aFormula = pViewSh->DoAutoSum(bRangeFinder, bSubTotal, eCode);
        if ( !aFormula.isEmpty() )
        {
            SetFuncString( aFormula );
            const sal_Int32 aOpen = aFormula.indexOf('(');
            const sal_Int32 aLen  = aFormula.getLength();
            if (bRangeFinder && pScMod->IsEditMode())
            {
                ScInputHandler* pHdl = pScMod->GetInputHdl( pViewSh );
                if ( pHdl )
                {
                    pHdl->InitRangeFinder( aFormula );

                    //! SetSelection at the InputHandler?
                    //! Set bSelIsRef?
                    if ( aOpen != -1 && aLen > aOpen )
                    {
                        ESelection aSel( 0, aOpen + (bSubTotal ? 3 : 1), 0, aLen-1 );
                        EditView* pTableView = pHdl->GetTableView();
                        if ( pTableView )
                            pTableView->SetSelection( aSel );
                        EditView* pTopView = pHdl->GetTopView();
                        if ( pTopView )
                            pTopView->SetSelection( aSel );
                    }
                }
            }
        }
    }
}

ScInputBarGroup::ScInputBarGroup(vcl::Window* pParent, ScTabViewShell* pViewSh)
    : ScTextWndBase(pParent, WinBits(WB_HIDE | WB_TABSTOP)),
      maTextWndGroup(VclPtr<ScTextWndGroup>::Create(this, pViewSh)),
@@ -962,63 +998,31 @@ IMPL_LINK( ScInputWindow, MenuHdl, Menu *, pMenu, bool )
    OString aCommand = pMenu->GetCurItemIdent();
    if (!aCommand.isEmpty())
    {
        ScModule* pScMod = SC_MOD();
        ScTabViewShell* pViewSh = dynamic_cast<ScTabViewShell*>( SfxViewShell::Current()  );
        if ( pViewSh )
        bool bSubTotal = false;
        bool bRangeFinder = false;
        OpCode eCode = ocSum;
        if ( aCommand ==  "sum" )
        {
            bool bSubTotal = false;
            bool bRangeFinder = false;
            OpCode eCode = ocSum;
            if ( aCommand ==  "sum" )
            {
                eCode = ocSum;
            }
            else if ( aCommand == "average" )
            {
                eCode = ocAverage;
            }
            else if ( aCommand == "max" )
            {
                eCode = ocMax;
            }
            else if ( aCommand == "min" )
            {
                eCode = ocMin;
            }
            else if ( aCommand == "count" )
            {
                eCode = ocCount;
            }

            const OUString aFormula = pViewSh->DoAutoSum(bRangeFinder, bSubTotal, eCode);
            if ( !aFormula.isEmpty() )
            {
                SetFuncString( aFormula );
                const sal_Int32 aOpen = aFormula.indexOf('(');
                const sal_Int32 aLen  = aFormula.getLength();
                if (bRangeFinder && pScMod->IsEditMode())
                {
                    ScInputHandler* pHdl = pScMod->GetInputHdl( pViewSh );
                    if ( pHdl )
                    {
                        pHdl->InitRangeFinder( aFormula );

                        //! SetSelection at the InputHandler?
                        //! Set bSelIsRef?
                        if ( aOpen != -1 && aLen > aOpen )
                        {
                            ESelection aSel( 0, aOpen + (bSubTotal ? 3 : 1), 0, aLen-1 );
                            EditView* pTableView = pHdl->GetTableView();
                            if ( pTableView )
                                pTableView->SetSelection( aSel );
                            EditView* pTopView = pHdl->GetTopView();
                            if ( pTopView )
                                pTopView->SetSelection( aSel );
                        }
                    }
                }
            }
            eCode = ocSum;
        }
        else if ( aCommand == "average" )
        {
            eCode = ocAverage;
        }
        else if ( aCommand == "max" )
        {
            eCode = ocMax;
        }
        else if ( aCommand == "min" )
        {
            eCode = ocMin;
        }
        else if ( aCommand == "count" )
        {
            eCode = ocCount;
        }

        AutoSum( bRangeFinder, bSubTotal, eCode );
    }
    return false;
}
diff --git a/sc/source/ui/inc/inputwin.hxx b/sc/source/ui/inc/inputwin.hxx
index 30eb93b..c461c8f 100644
--- a/sc/source/ui/inc/inputwin.hxx
+++ b/sc/source/ui/inc/inputwin.hxx
@@ -31,6 +31,7 @@
#include <vcl/window.hxx>
#include <vcl/transfer.hxx>
#include <vcl/menu.hxx>
#include <formula/opcode.hxx>

class EditView;
class ScAccessibleEditLineTextData;
@@ -309,6 +310,8 @@ public:
    DECL_LINK( MenuHdl, Menu *, bool );
    DECL_LINK( DropdownClickHdl, ToolBox*, void );

    void            AutoSum( bool& bRangeFinder, bool& bSubTotal, OpCode eCode );

private:
    bool IsPointerAtResizePos();

diff --git a/sc/source/ui/view/tabvwsh4.cxx b/sc/source/ui/view/tabvwsh4.cxx
index b00cebc..e57caf3 100644
--- a/sc/source/ui/view/tabvwsh4.cxx
+++ b/sc/source/ui/view/tabvwsh4.cxx
@@ -1344,6 +1344,27 @@ bool ScTabViewShell::TabKeyInput(const KeyEvent& rKEvt)
            case KEY_PAGEDOWN:
                nSlotId = bShift ? SID_CURSORPAGERIGHT_SEL : SID_CURSORPAGERIGHT_;
                break;
            case KEY_EQUAL:
            {
                // #tdf39302: Use "Alt + =" for autosum
                if ( !bAnyEdit ) // Ignore shortcut if currently editing a cell
                {
                    ScInputHandler* pHdl = pScMod->GetInputHdl(this);
                    if ( pHdl )
                    {
                        ScInputWindow* pWin = pHdl->GetInputWindow();
                        if ( pWin )
                        {
                            bool bRangeFinder = false;
                            bool bSubTotal = false;
                            pWin->AutoSum( bRangeFinder, bSubTotal, ocSum );
                        }
                    }

                    bUsed = true;
                    break;
                }
            }
        }
        if ( nSlotId )
        {