Related: tdf#137577 Display (sheetname) with sheet-local names in Name Box

... if current cell selection matches a sheet-local name, so it
can be differentiated from an identically named global name. Which
is already the case when listing and picking a name from the list.

Made it necessary to adapt an UI test checking for Name Box
content.

Change-Id: Ia90b8961c3ae213cf7bb53f3b610a65805bba6b9
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/113330
Reviewed-by: Eike Rathke <erack@redhat.com>
Tested-by: Jenkins
diff --git a/sc/inc/document.hxx b/sc/inc/document.hxx
index 72f84cd..0d30783 100644
--- a/sc/inc/document.hxx
+++ b/sc/inc/document.hxx
@@ -820,7 +820,8 @@
    void                         RefreshDirtyTableColumnNames();
    SC_DLLPUBLIC sc::ExternalDataMapper& GetExternalDataMapper();

    SC_DLLPUBLIC const ScRangeData* GetRangeAtBlock( const ScRange& rBlock, OUString* pName ) const;
    SC_DLLPUBLIC const ScRangeData* GetRangeAtBlock( const ScRange& rBlock, OUString* pName,
                                                     bool* pSheetLocal = nullptr ) const;

    SC_DLLPUBLIC bool                  HasPivotTable() const;
    SC_DLLPUBLIC ScDPCollection*       GetDPCollection();
diff --git a/sc/qa/uitest/range_name/create_range_name.py b/sc/qa/uitest/range_name/create_range_name.py
index e4fab4e..989532b 100644
--- a/sc/qa/uitest/range_name/create_range_name.py
+++ b/sc/qa/uitest/range_name/create_range_name.py
@@ -114,7 +114,8 @@

        # tdf#67007: Without the fix in place, this test would have failed with
        # AssertionError: 'localRangeName' != 'A1'
        self.assertEqual('localRangeName', get_state_as_dict(xPosWindow)['Text'])
        # Additionally, newly check a sheet-local scoped name has " (sheetname)" appended.
        self.assertEqual('localRangeName (Sheet1)', get_state_as_dict(xPosWindow)['Text'])

        gridwin = calcDoc.getChild("grid_window")
        enter_text_to_cell(gridwin, "A1", "1")
diff --git a/sc/source/core/data/documen3.cxx b/sc/source/core/data/documen3.cxx
index 798fa46..6f6a9a6 100644
--- a/sc/source/core/data/documen3.cxx
+++ b/sc/source/core/data/documen3.cxx
@@ -234,7 +234,7 @@
    return pLocalNames->insert(pName);
}

const ScRangeData* ScDocument::GetRangeAtBlock( const ScRange& rBlock, OUString* pName ) const
const ScRangeData* ScDocument::GetRangeAtBlock( const ScRange& rBlock, OUString* pName, bool* pSheetLocal ) const
{
    const ScRangeData* pData = nullptr;
    if (rBlock.aStart.Tab() == rBlock.aEnd.Tab())
@@ -247,6 +247,8 @@
            {
                if (pName)
                    *pName = pData->GetName();
                if (pSheetLocal)
                    *pSheetLocal = true;
                return pData;
            }
        }
@@ -254,8 +256,13 @@
    if ( pRangeName )
    {
        pData = pRangeName->findByRange( rBlock );
        if (pData && pName)
            *pName = pData->GetName();
        if (pData)
        {
            if (pName)
                *pName = pData->GetName();
            if (pSheetLocal)
                *pSheetLocal = false;
        }
    }
    return pData;
}
diff --git a/sc/source/ui/app/inputhdl.cxx b/sc/source/ui/app/inputhdl.cxx
index 540d342..109035c 100644
--- a/sc/source/ui/app/inputhdl.cxx
+++ b/sc/source/ui/app/inputhdl.cxx
@@ -4103,13 +4103,14 @@
                if ( pInputWin || comphelper::LibreOfficeKit::isActive())                        // Named range input
                {
                    OUString aPosStr;
                    bool bSheetLocal = false;
                    const ScAddress::Details aAddrDetails( rDoc, aCursorPos );

                    // Is the range a name?
                    //! Find by Timer?
                    if ( pActiveViewSh )
                        pActiveViewSh->GetViewData().GetDocument().
                            GetRangeAtBlock( ScRange( rSPos, rEPos ), &aPosStr );
                            GetRangeAtBlock( ScRange( rSPos, rEPos ), &aPosStr, &bSheetLocal);

                    if ( aPosStr.isEmpty() )           // Not a name -> format
                    {
@@ -4125,6 +4126,12 @@
                        else
                            aPosStr = aCursorPos.Format(ScRefFlags::VALID | nFlags, &rDoc, aAddrDetails);
                    }
                    else if (bSheetLocal)
                    {
                        OUString aName;
                        if (rDoc.GetName( rSPos.Tab(), aName))
                            aPosStr = ScPosWnd::createLocalRangeName( aPosStr, aName);
                    }

                    if (pInputWin)
                    {
diff --git a/sc/source/ui/app/inputwin.cxx b/sc/source/ui/app/inputwin.cxx
index c7beb52..3473ffc 100644
--- a/sc/source/ui/app/inputwin.cxx
+++ b/sc/source/ui/app/inputwin.cxx
@@ -2185,15 +2185,12 @@
    }
}

namespace {

OUString createLocalRangeName(std::u16string_view rName, std::u16string_view rTableName)
// static
OUString ScPosWnd::createLocalRangeName(std::u16string_view rName, std::u16string_view rTableName)
{
    return OUString::Concat(rName) + " (" + rTableName + ")";
}

}

void ScPosWnd::FillRangeNames()
{
    m_xWidget->clear();
diff --git a/sc/source/ui/inc/inputwin.hxx b/sc/source/ui/inc/inputwin.hxx
index aadad8b..4767d09 100644
--- a/sc/source/ui/inc/inputwin.hxx
+++ b/sc/source/ui/inc/inputwin.hxx
@@ -177,6 +177,8 @@
    void            SetPos( const OUString& rPosStr );        // Displayed Text
    void            SetFormulaMode( bool bSet );

    static OUString createLocalRangeName(std::u16string_view rName, std::u16string_view rTableName);

private:
    DECL_LINK(OnAsyncGetFocus, void*, void);
    DECL_LINK(KeyInputHdl, const KeyEvent&, bool);