tdf#150457 Fix crash on hovering removed footnote reference

The regression was caused by 402f36efb215338ad545caa65d39fb8a39685ea1.
Before that, 0aa0fda64057647219954480ac1bab86b0f0e433 changed the cursor
jumping behavior to fix tdf#81226. The goal was to make LibreOffice
behave as described in the description of the issue:

"When your cursor is on the last line but not at the end of it, simply
press the down arrow to go at the end of the file (end of that line)."

The same behavior was achieved for pressing down on the last line.

The above patches allowed removing the footnotes and their numbers
(except the first number) while the reference were still present. Then,
on hovering a removed footnote reference, a crash was happening. It
should be noted that even with this patch, removing the first reference
number was not possible.

This fix limits this behavior to anywhere other than inside a footnote.
In this way, it prevents crashing.

With this fix, the behavior of the cursor goes back to what it was
before 0aa0fda64057647219954480ac1bab86b0f0e433. The user will not be
able to select all the footnotes at once. Also, removing footnote number
will not be possible.

It is worth noting that not being able to select all the footnotes at
once prevents the user from changing the characteristics of the
footnotes all at the same time. Also, not being able to remove the
footnote numbers, prevents the user from having footnotes with custom
numbers written manually. Both of these actions are possible in MS Word.
The other difference is that by going up, one can go out of the footnote
section in MS Word, but this is not the case for LibreOffice.

A UI test is added to make sure the crash will not happen again. The
test can be run by:

 cd sw && make -srj1 UITest_writer_tests2 \
  UITEST_TEST_NAME="deleteFootnotes.tdf150457.test_delete_footnotes" \
  SAL_USE_VCLPLUGIN=gen

Change-Id: I1ce7d2189355f6763b6f31219e00516ff7a8164e
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/138562
Tested-by: Jenkins
Reviewed-by: Hossein <[email protected]>
diff --git a/sw/qa/uitest/writer_tests2/deleteFootnotes.py b/sw/qa/uitest/writer_tests2/deleteFootnotes.py
new file mode 100644
index 0000000..162cc7d
--- /dev/null
+++ b/sw/qa/uitest/writer_tests2/deleteFootnotes.py
@@ -0,0 +1,46 @@
# -*- tab-width: 4; indent-tabs-mode: nil; py-indent-offset: 4 -*-
#
# This file is part of the LibreOffice project.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#

from uitest.framework import UITestCase
from uitest.uihelper.common import type_text
from libreoffice.uno.propertyvalue import mkPropertyValues
import time


class tdf150457(UITestCase):

   def test_delete_footnotes(self):
        with self.ui_test.create_doc_in_start_center("writer") as document:
            xWriterDoc = self.xUITest.getTopFocusWindow()
            xWriterEdit = xWriterDoc.getChild("writer_edit")

            type_text(xWriterEdit, "a")
            self.xUITest.executeCommand(".uno:InsertFootnote")
            type_text(xWriterEdit, "abc")
            self.assertEqual(document.Footnotes[0].String, "abc")
            self.assertEqual(document.Footnotes.getCount(), 1)

            xWriterEdit.executeAction("TYPE", mkPropertyValues({"KEYCODE": "PAGEUP"}))
            xWriterEdit.executeAction("TYPE", mkPropertyValues({"KEYCODE": "RETURN"}))
            type_text(xWriterEdit, "d")

            self.xUITest.executeCommand(".uno:InsertFootnote")
            type_text(xWriterEdit, "def")
            self.assertEqual(document.Footnotes[1].String, "def")
            self.assertEqual(document.Footnotes.getCount(), 2)

            xWriterEdit.executeAction("TYPE", mkPropertyValues({"KEYCODE": "UP"}))
            xWriterEdit.executeAction("TYPE", mkPropertyValues({"KEYCODE": "HOME"}))
            xWriterEdit.executeAction("TYPE", mkPropertyValues({"KEYCODE": "SHIFT+DOWN"}))
            xWriterEdit.executeAction("TYPE", mkPropertyValues({"KEYCODE": "DELETE"}))
            xWriterEdit.executeAction("TYPE", mkPropertyValues({"KEYCODE": "PAGEUP"}))
            xWriterEdit.executeAction("TYPE", mkPropertyValues({"KEYCODE": "CTRL+A"}))
            xWriterEdit.executeAction("TYPE", mkPropertyValues({"KEYCODE": "DELETE"}))

# vim: set shiftwidth=4 softtabstop=4 expandtab:
diff --git a/sw/source/core/crsr/swcrsr.cxx b/sw/source/core/crsr/swcrsr.cxx
index b379fe6..06c73af 100644
--- a/sw/source/core/crsr/swcrsr.cxx
+++ b/sw/source/core/crsr/swcrsr.cxx
@@ -2088,7 +2088,9 @@ bool SwCursor::UpDown( bool bUp, sal_uInt16 nCnt,
            }
            bRet = !IsSelOvr( SwCursorSelOverFlags::Toggle | SwCursorSelOverFlags::ChangePos );
        }
        else
        else if (!pFrame->IsInFootnote()) // tdf#150457 Jump to the begin/end
                                          // of the first/last line only if the
                                          // cursor is not inside a footenote
        {
            sal_Int32 nOffset = 0;

@@ -2114,6 +2116,8 @@ bool SwCursor::UpDown( bool bUp, sal_uInt16 nCnt,
            }

        }
        else
            *GetPoint() = aOldPos;

        DoSetBidiLevelUpDown(); // calculate cursor bidi level
    }