tdf#111707 Move duplicated code into shared method

Change-Id: I4b61ed876410662b5f85be0032b2fc8def456dd9
Reviewed-on: https://gerrit.libreoffice.org/75286
Tested-by: Jenkins
Reviewed-by: Samuel Mehrbrodt <Samuel.Mehrbrodt@cib.de>
diff --git a/sd/source/ui/inc/DrawViewShell.hxx b/sd/source/ui/inc/DrawViewShell.hxx
index 713cac9..64bcabc 100644
--- a/sd/source/ui/inc/DrawViewShell.hxx
+++ b/sd/source/ui/inc/DrawViewShell.hxx
@@ -485,6 +485,9 @@ private:

    void ConfigureAppBackgroundColor( svtools::ColorConfig* pColorConfig = nullptr );

    // Select and return the field at the current cursor position
    const SvxFieldData* GetFieldAtCursor();

    // The colour of the area behind the slide (used to be called "Wiese")
    Color mnAppBackgroundColor;
};
diff --git a/sd/source/ui/view/drviews2.cxx b/sd/source/ui/view/drviews2.cxx
index 988fae4f..0cc9fef 100644
--- a/sd/source/ui/view/drviews2.cxx
+++ b/sd/source/ui/view/drviews2.cxx
@@ -1154,37 +1154,11 @@ void DrawViewShell::FuTemporary(SfxRequest& rReq)
                OutlinerView* pOLV = mpDrawView->GetTextEditOutlinerView();
                if (pOutl && pOLV)
                {
                    const SvxFieldItem* pFieldItem = pOLV->GetFieldAtSelection();
                    if (pFieldItem)
                    {
                        // Make sure the whole field is selected
                        ESelection aSel = pOLV->GetSelection();
                        if (aSel.nStartPos == aSel.nEndPos)
                        {
                            aSel.nEndPos++;
                            pOLV->SetSelection(aSel);
                        }
                    }
                    if (!pFieldItem)
                    {
                        // Cursor probably behind the field - extend selection to select the field
                        ESelection aSel = pOLV->GetSelection();
                        if (aSel.nStartPos == aSel.nEndPos)
                        {
                            aSel.nStartPos--;
                            pOLV->SetSelection(aSel);
                            pFieldItem = pOLV->GetFieldAtSelection();
                        }
                    }

                    if (pFieldItem)
                    const SvxFieldData* pField = GetFieldAtCursor();
                    if( auto pUrlField = dynamic_cast< const SvxURLField *>( pField ) )
                    {
                        ESelection aSel = pOLV->GetSelection();
                        const SvxFieldData* pField = pFieldItem->GetField();
                        if( auto pUrlField = dynamic_cast< const SvxURLField *>( pField ) )
                        {
                            pOutl->QuickInsertText(pUrlField->GetRepresentation(), aSel);
                        }
                        pOutl->QuickInsertText(pUrlField->GetRepresentation(), aSel);
                    }
                }
            }
diff --git a/sd/source/ui/view/drviewsf.cxx b/sd/source/ui/view/drviewsf.cxx
index dc6d8f9..4ae0444 100644
--- a/sd/source/ui/view/drviewsf.cxx
+++ b/sd/source/ui/view/drviewsf.cxx
@@ -89,41 +89,14 @@ void DrawViewShell::GetCtrlState(SfxItemSet &rSet)

        if (pOLV)
        {
            bool bField = false;
            const SvxFieldItem* pFieldItem = pOLV->GetFieldAtSelection();
            if (pFieldItem)
            const SvxFieldData* pField = GetFieldAtCursor();
            if( auto pUrlField = dynamic_cast< const SvxURLField *>( pField ) )
            {
                // Make sure the whole field is selected
                ESelection aSel = pOLV->GetSelection();
                if (aSel.nStartPos == aSel.nEndPos)
                {
                    aSel.nEndPos++;
                    pOLV->SetSelection(aSel);
                }
                aHLinkItem.SetName(pUrlField->GetRepresentation());
                aHLinkItem.SetURL(pUrlField->GetURL());
                aHLinkItem.SetTargetFrame(pUrlField->GetTargetFrame());
            }
            if (!pFieldItem)
            {
                // Cursor probably behind the field - extend selection to select the field
                ESelection aSel = pOLV->GetSelection();
                if (aSel.nStartPos == aSel.nEndPos)
                {
                    aSel.nStartPos--;
                    pOLV->SetSelection(aSel);
                    pFieldItem = pOLV->GetFieldAtSelection();
                }
            }
            if (pFieldItem)
            {
                const SvxFieldData* pField = pFieldItem->GetField();
                if( auto pUrlField = dynamic_cast< const SvxURLField *>( pField ) )
                {
                    aHLinkItem.SetName(pUrlField->GetRepresentation());
                    aHLinkItem.SetURL(pUrlField->GetURL());
                    aHLinkItem.SetTargetFrame(pUrlField->GetTargetFrame());
                    bField = true;
                }
            }
            if (!bField)
            else
            {
                // use selected text as name for urls
                OUString sReturn = pOLV->GetSelected();
@@ -823,6 +796,38 @@ bool DrawViewShell::HasSelection(bool bText) const
    return bReturn;
}

const SvxFieldData* DrawViewShell::GetFieldAtCursor()
{
    OutlinerView* pOLV = mpDrawView->GetTextEditOutlinerView();
    if (!pOLV)
        return nullptr;

    const SvxFieldItem* pFieldItem = pOLV->GetFieldAtSelection();
    if (pFieldItem)
    {
        // Make sure the whole field is selected
        ESelection aSel = pOLV->GetSelection();
        if (aSel.nStartPos == aSel.nEndPos)
        {
            aSel.nEndPos++;
            pOLV->SetSelection(aSel);
        }
    }
    if (!pFieldItem)
    {
        // Cursor probably behind the field - extend selection to select the field
        ESelection aSel = pOLV->GetSelection();
        if (aSel.nStartPos == aSel.nEndPos)
        {
            aSel.nStartPos--;
            pOLV->SetSelection(aSel);
            pFieldItem = pOLV->GetFieldAtSelection();
        }
    }

    return pFieldItem ? pFieldItem->GetField() : nullptr;
}

} // end of namespace sd

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */