tdf#72955 - Modified Bibliography entry dialog to work inside bibl. table

Change-Id: I5d4072e136eb321f92973ca8e3cd749e4ce06bd5
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/147001
Tested-by: Jenkins
Reviewed-by: Miklos Vajna <vmiklos@collabora.com>
diff --git a/sw/source/uibase/shells/textidx.cxx b/sw/source/uibase/shells/textidx.cxx
index 299de33..29904ee 100644
--- a/sw/source/uibase/shells/textidx.cxx
+++ b/sw/source/uibase/shells/textidx.cxx
@@ -35,23 +35,103 @@
#include <toxmgr.hxx>
#include <swabstdlg.hxx>

#include <ndtxt.hxx>
#include <fmtfld.hxx>
#include <IDocumentFieldsAccess.hxx>

void SwTextShell::ExecIdx(SfxRequest const &rReq)
{
    const SfxItemSet *pArgs = rReq.GetArgs();
    const SfxItemSet* pArgs = rReq.GetArgs();
    const SfxPoolItem* pItem = nullptr;
    const sal_uInt16 nSlot = rReq.GetSlot();
    if(pArgs)
       pArgs->GetItemState(nSlot, false, &pItem );
    if (pArgs)
        pArgs->GetItemState(nSlot, false, &pItem);

    SfxViewFrame* pVFrame = GetView().GetViewFrame();

    switch( nSlot )
    switch (nSlot)
    {
        case FN_EDIT_AUTH_ENTRY_DLG :
        case FN_EDIT_AUTH_ENTRY_DLG:
        {
            SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
            ScopedVclPtr<VclAbstractDialog> pDlg(pFact->CreateSwAutoMarkDialog(GetView().GetFrameWeld(), GetShell()));
            pDlg->Execute();
            SwWrtShell& rShell = GetShell();

            const bool bWasViewLocked = rShell.IsViewLocked();
            rShell.LockView(true);

            if (const SwField* const pCurrentField = rShell.GetCurField();
                !rShell.HasReadonlySel() && pCurrentField != nullptr
                && pCurrentField->GetTyp()->Which() == SwFieldIds::TableOfAuthorities)
            {
                // Since the cursor is on a bibliography mark (e.g. "[1]"), open the edit dialog as usual
                SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
                ScopedVclPtr<VclAbstractDialog> pDlg(
                    pFact->CreateSwAutoMarkDialog(GetView().GetFrameWeld(), rShell));
                pDlg->Execute();
            }
            else if (const SwTOXBase* const pCurrentTOX = rShell.GetCurTOX();
                     pCurrentTOX != nullptr && pCurrentTOX->GetType() == TOX_AUTHORITIES
                     && (rShell.GetCursor()->GetPoint()->GetNode()
                            .FindSectionNode()->GetSection().GetType()
                         == SectionType::ToxContent))
            {
                // Since the cursor is in the bibliography table, find the first mark that would match the given row
                const SwNode* const pTableRowNode = &rShell.GetCursor()->GetPoint()->GetNode();
                const OUString& rTableRowText
                    = static_cast<const SwTextNode*>(pTableRowNode)->GetText();

                const SwFieldType* pAuthField
                    = rShell.GetDoc()->getIDocumentFieldsAccess().GetFieldType(
                        SwFieldIds::TableOfAuthorities, OUString(), false);

                if (pAuthField)
                {
                    bool bMatchingMarkFound = false;
                    {
                        std::vector<SwFormatField*> vFields;
                        pAuthField->GatherFields(vFields);
                        for (auto pFormatField : vFields)
                        {
                            if (const SwField* pIteratedField = nullptr;
                                pFormatField != nullptr
                                && (pIteratedField = pFormatField->GetField()) != nullptr
                                && (pIteratedField->GetTyp()->Which()
                                    == SwFieldIds::TableOfAuthorities))
                            {
                                OUString sMarkText
                                    = static_cast<const SwAuthorityField*>(pIteratedField)
                                          ->GetAuthority(rShell.GetLayout(),
                                                         &pCurrentTOX->GetTOXForm());

                                if (sMarkText == rTableRowText)
                                {
                                    // Since the text generated from the mark would match the given row
                                    //  move cursor to it, set bMatchingMarkFound and break
                                    rShell.GotoFormatField(*pFormatField);
                                    bMatchingMarkFound = true;
                                    break;
                                }
                            }
                        }
                    }

                    if (bMatchingMarkFound)
                    {
                        // Since matching mark has been found and cursor has been moved to it,
                        //  open the edit dialog
                        SwAbstractDialogFactory* pFact = SwAbstractDialogFactory::Create();
                        ScopedVclPtr<VclAbstractDialog> pDlg(
                            pFact->CreateSwAutoMarkDialog(GetView().GetFrameWeld(), rShell));
                        pDlg->Execute();

                        // Refresh TOX
                        rShell.GetCursor_()->GetPoint()->Assign(*pTableRowNode);
                        rShell.UpdateTableOf(*pCurrentTOX);
                    }
                }
            }

            if (!bWasViewLocked)
                rShell.LockView(false);
        }
        break;
        case FN_INSERT_AUTH_ENTRY_DLG:
@@ -169,7 +249,10 @@ void SwTextShell::GetIdxState(SfxItemSet &rSet)
        }

        rSet.DisableItem( FN_EDIT_IDX_ENTRY_DLG );
        rSet.DisableItem( FN_EDIT_AUTH_ENTRY_DLG );
        if(pBase == nullptr // tdf#72955: Hide the "Bibliography Entry" command if there is no TOX in the selection
            || pBase->GetType() != TOX_AUTHORITIES // or if it is not a bibliography table
            || (rSh.GetCursor()->GetPoint()->GetNode().FindSectionNode()->GetSection().GetType() != SectionType::ToxContent)) // or if it's the heading
            rSet.DisableItem(FN_EDIT_AUTH_ENTRY_DLG);

        if(!pIdxMrk)
            rSet.DisableItem( FN_INSERT_IDX_ENTRY_DLG );