include/sfx2/docfilt.hxx                                  |    1 
 officecfg/registry/schema/org/openoffice/Office/Calc.xcs  |    7 
 sc/inc/appoptio.hxx                                       |    4 
 sc/inc/sc.hrc                                             |    1 
 sc/qa/uitest/calc_tests9/tdf156611_hyperlink_interoper.py |  124 ++++++++++++++
 sc/source/core/tool/appoptio.cxx                          |   13 +
 sc/source/ui/app/scmod.cxx                                |   10 +
 sc/source/ui/inc/editsh.hxx                               |    1 
 sc/source/ui/inc/tpcompatibility.hxx                      |    1 
 sc/source/ui/optdlg/tpcompatibility.cxx                   |   15 +
 sc/source/ui/view/editsh.cxx                              |   95 +++++++++-
 sc/uiconfig/scalc/ui/optcompatibilitypage.ui              |   60 ++++++
 12 files changed, 316 insertions(+), 16 deletions(-)

New commits:
commit 24cd55341bc3f3e8ed9d5ff23efd47a53532f283
Author:     Attila Szűcs <attila.sz...@collabora.com>
AuthorDate: Mon Jul 31 04:37:07 2023 +0200
Commit:     Tomaž Vajngerl <qui...@gmail.com>
CommitDate: Wed Aug 23 15:23:16 2023 +0200

    tdf#156611 SC: hyperlink option for MS behaviour
    
    Added an options that can limit Calc to behave like MS excel in case
    of inserting hyperlinks, when MS format document is used.
    Tools -> Options -> LibreOfficeDev Calc -> Compatibility -> Hyperlinks
    
    In MS excel, only cells can have a hyperlink, and only 1.
    In Calc even textfields in a cell can have hyperlinks, so it can have
    multiple links in a cell, but once saved as MS format, and reloaded,
    textfield links will become cell links, if there was 1 link in the cell.
    If there was more links in the cell, then all will be lost.
    
    So, when MS excel format document is edited in calc, the ability to make
    text field links is useless can be missleading, and confuse users.
    
    If this option is set, and an MS file format is opened, then insering a
    hyperlink will work like if we selected the whole cell to insert the
    hyperlink.
    
    Change-Id: I7174216d10cf250d48f23f71ae681c46f7610bbc
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/155079
    Tested-by: Jenkins
    Reviewed-by: Caolán McNamara <caolan.mcnam...@collabora.com>
    Reviewed-by: Tomaž Vajngerl <qui...@gmail.com>

diff --git a/include/sfx2/docfilt.hxx b/include/sfx2/docfilt.hxx
index 9dbbb37b87d2..45dd0efb9b29 100644
--- a/include/sfx2/docfilt.hxx
+++ b/include/sfx2/docfilt.hxx
@@ -80,6 +80,7 @@ public:
     bool GetSupportsSigning() const { return bool(nFormatType & 
SfxFilterFlags::SUPPORTSSIGNING); }
     bool GetGpgEncryption() const { return bool(nFormatType & 
SfxFilterFlags::GPGENCRYPTION); }
     bool IsOwnTemplateFormat() const { return bool(nFormatType & 
SfxFilterFlags::TEMPLATEPATH); }
+    bool IsMSOFormat() const { return aTypeName.startsWith("MS"); }
     /// not our built-in format
     bool IsAlienFormat() const { return bool(nFormatType & 
SfxFilterFlags::ALIEN); }
     /// an unusual/legacy file to be loading
diff --git a/officecfg/registry/schema/org/openoffice/Office/Calc.xcs 
b/officecfg/registry/schema/org/openoffice/Office/Calc.xcs
index eff7ac7aa80a..a41de7a36b8a 100644
--- a/officecfg/registry/schema/org/openoffice/Office/Calc.xcs
+++ b/officecfg/registry/schema/org/openoffice/Office/Calc.xcs
@@ -1861,6 +1861,13 @@
           </constraints>
         </prop>
       </group>
+      <prop oor:name="Links" oor:type="xs:boolean" oor:nillable="false">
+        <info>
+          <desc>Insert link for the cell, instead of text fields in a 
cell.(Excel iteropability options)</desc>
+          <label>Links like Excel</label>
+        </info>
+        <value>false</value>
+      </prop>
     </group>
     <group oor:name="Defaults">
       <info>
diff --git a/sc/inc/appoptio.hxx b/sc/inc/appoptio.hxx
index 793dc707a67f..d9e22c9cba78 100644
--- a/sc/inc/appoptio.hxx
+++ b/sc/inc/appoptio.hxx
@@ -77,6 +77,9 @@ public:
     ScOptionsUtil::KeyBindingType GetKeyBindingType() const { return 
meKeyBindingType; }
     void        SetKeyBindingType( ScOptionsUtil::KeyBindingType e ) { 
meKeyBindingType = e; }
 
+    void        SetLinksInsertedLikeMSExcel(bool bNew) { 
mbLinksInsertedLikeMSExcel = bNew; }
+    bool        GetLinksInsertedLikeMSExcel() const { return 
mbLinksInsertedLikeMSExcel; }
+
     ScAppOptions& operator=   ( const ScAppOptions& rOpt );
 
 private:
@@ -99,6 +102,7 @@ private:
     sal_Int32       nDefaultObjectSizeHeight;
     bool            mbShowSharedDocumentWarning;
     ScOptionsUtil::KeyBindingType meKeyBindingType;
+    bool            mbLinksInsertedLikeMSExcel;
 };
 
 //  Config Item containing app options
diff --git a/sc/inc/sc.hrc b/sc/inc/sc.hrc
index 534ff7ab6795..d1b13b767eb0 100644
--- a/sc/inc/sc.hrc
+++ b/sc/inc/sc.hrc
@@ -621,6 +621,7 @@ static_assert(SID_PREVIEW_END < SID_KEYFUNC_START, "calc 
slots ids trampling inf
 #define SID_GROUP_SPARKLINES        (SID_NEW_SLOTS+114)
 #define SID_UNGROUP_SPARKLINES      (SID_NEW_SLOTS+115)
 #define SID_EDIT_SPARKLINE          (SID_NEW_SLOTS+116)
+#define SID_SC_OPT_LINKS            TypedWhichId<SfxBoolItem>(SID_NEW_SLOTS + 
117)
 
 // idl parameter
 
diff --git a/sc/qa/uitest/calc_tests9/tdf156611_hyperlink_interoper.py 
b/sc/qa/uitest/calc_tests9/tdf156611_hyperlink_interoper.py
new file mode 100644
index 000000000000..a78106c11d66
--- /dev/null
+++ b/sc/qa/uitest/calc_tests9/tdf156611_hyperlink_interoper.py
@@ -0,0 +1,124 @@
+# -*- 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 get_url_for_data_file
+from libreoffice.uno.propertyvalue import mkPropertyValues
+from libreoffice.calc.document import get_cell_by_position
+from uitest.uihelper.calc import enter_text_to_cell
+from uitest.uihelper.common import get_state_as_dict, select_pos
+
+class tdf156611(UITestCase):
+    def test_tdf156611_insert_hyperlink_like_excel(self):
+        # The hyperlink interoperability setting, that this test is testing,
+        # works only if MS document type is opened.
+        # but it does not need any data from the file, any xlsx or xls file 
can be opened for this test
+        with 
self.ui_test.load_file(get_url_for_data_file("tdf126541_GridOff.xlsx")) as 
document:
+
+            # datas that we will check against when hyperlink is inserted
+            urls =[["",""],["https://www.documentfoundation.org/","";]]
+            texts =[["aaa bbb","bbb"],["cccc ddd","ddd"],["eeee","aaa cccc 
eeee"]]
+
+            # 1. run, we want hypelink inserton work like in MS excel (only 1 
hyperlink/cell is allowed)
+            # 2. run, we want hypelink inserton work as it did in calc (more 
hyperlinks can be in 1 cell)
+            for i in range(2):
+                xCalcDoc = self.xUITest.getTopFocusWindow()
+                xGridWindow = xCalcDoc.getChild("grid_window")
+
+                #Change hyperlink interoperability setting
+                #Go to Tools -> Options -> LibreofficeDev Calc -> Compatibility
+                with 
self.ui_test.execute_dialog_through_command(".uno:OptionsTreeDialog", 
close_button="cancel") as xDialogOpt:
+
+                    xPages = xDialogOpt.getChild("pages")
+                    xChartEntry = xPages.getChild('3')                 # 
LibreofficeDev Calc
+                    xChartEntry.executeAction("EXPAND", tuple())
+                    xChartGeneralEntry = xChartEntry.getChild('7')
+                    xChartGeneralEntry.executeAction("SELECT", tuple())        
  #Compatibility
+
+                    xLinks = xDialogOpt.getChild("cellLinkCB")
+                    xLinks.executeAction("CLICK", tuple())
+                    xApply = xDialogOpt.getChild("apply")
+                    xApply.executeAction("CLICK", tuple())
+
+                enter_text_to_cell(xGridWindow, "A1", "aaa bbb")
+
+                # Select last word of the cell text: "bbb"
+                xGridWindow.executeAction("SELECT", mkPropertyValues({"CELL": 
"A1"}))
+                xGridWindow.executeAction("TYPE", 
mkPropertyValues({"KEYCODE":"F2"}))
+                xGridWindow.executeAction("TYPE", 
mkPropertyValues({"KEYCODE":"SHIFT+CTRL+LEFT"}))
+
+                # Insert hyperlink
+                with 
self.ui_test.execute_dialog_through_command(".uno:HyperlinkDialog") as xDialog:
+                    xTab = xDialog.getChild("tabcontrol")
+                    select_pos(xTab, "0")
+
+                    xTarget = xDialog.getChild("target")
+                    self.assertEqual(get_state_as_dict(xTarget)["Text"], "")
+                    xIndication = xDialog.getChild("indication")
+                    self.assertEqual(get_state_as_dict(xIndication)["Text"], 
texts[0][i])
+                    # 1. run "aaa bbb" The whole cell text
+                    # 2. run "bbb" Only the selected text
+
+                    # Insert a sample hyperlink, and change text
+                    xTarget.executeAction("TYPE", 
mkPropertyValues({"KEYCODE":"CTRL+A"}))
+                    xTarget.executeAction("TYPE", mkPropertyValues({"TEXT": 
"https://www.documentfoundation.org/"}))
+                    xIndication.executeAction("TYPE", 
mkPropertyValues({"KEYCODE":"CTRL+A"}))
+                    xIndication.executeAction("TYPE", 
mkPropertyValues({"TEXT": "cccc"}))
+
+                # Edit cell text: insert " ddd" in the end
+                xGridWindow.executeAction("SELECT", mkPropertyValues({"CELL": 
"A2"}))
+                xGridWindow.executeAction("SELECT", mkPropertyValues({"CELL": 
"A1"}))
+                xGridWindow.executeAction("TYPE", 
mkPropertyValues({"KEYCODE":"F2"}))
+                xGridWindow.executeAction("TYPE", mkPropertyValues({"TEXT": " 
ddd"}))
+                xGridWindow.executeAction("TYPE", 
mkPropertyValues({"KEYCODE":"RETURN"}))
+
+                # Select the last word of cell text: "ddd"
+                xGridWindow.executeAction("SELECT", mkPropertyValues({"CELL": 
"A2"}))
+                xGridWindow.executeAction("SELECT", mkPropertyValues({"CELL": 
"A1"}))
+                xGridWindow.executeAction("TYPE", 
mkPropertyValues({"KEYCODE":"F2"}))
+                xGridWindow.executeAction("TYPE", 
mkPropertyValues({"KEYCODE":"SHIFT+CTRL+LEFT"}))
+
+                # Insert hyperlink
+                with 
self.ui_test.execute_dialog_through_command(".uno:HyperlinkDialog") as xDialog2:
+                    xTab = xDialog2.getChild("tabcontrol")
+                    select_pos(xTab, "0")
+
+                    xTarget = xDialog2.getChild("target")
+                    self.assertEqual(get_state_as_dict(xTarget)["Text"], 
urls[1][i])
+                    # 1. run: "https://www.documentfoundation.org/"; the cell 
already have this url.
+                    # 2. run: "" The selected text is not a hyperlink yet.
+                    xIndication = xDialog2.getChild("indication")
+                    self.assertEqual(get_state_as_dict(xIndication)["Text"], 
texts[1][i])
+                    # 1. run: "cccc ddd" The whole cell text
+                    # 2. run: "ddd" Only the selected text
+
+                    # Insert a sample hyperlink, and change text
+                    xTarget.executeAction("TYPE", 
mkPropertyValues({"KEYCODE":"CTRL+A"}))
+                    xTarget.executeAction("TYPE", mkPropertyValues({"TEXT": 
"https://aWrongLink/"}))
+                    xIndication.executeAction("TYPE", 
mkPropertyValues({"KEYCODE":"CTRL+A"}))
+                    xIndication.executeAction("TYPE", 
mkPropertyValues({"TEXT": "eeee"}))
+
+                # Move focus to ensure cell is not in edit mode
+                xGridWindow.executeAction("SELECT", mkPropertyValues({"CELL": 
"A2"}))
+                xGridWindow.executeAction("SELECT", mkPropertyValues({"CELL": 
"A1"}))
+
+                # Check contents of the cell
+                xCell = get_cell_by_position(document, 0, 0, 0)
+                self.assertEqual(xCell.getString(), texts[2][i])
+                # 1. run: "eeee" last hyperlink insertion overwritten the whol 
cell text with "eeee"
+                # 2. run: "aaa cccc eeee" as every hypelink insertion only 
overwritten the actually selected text
+                xTextFields = xCell.getTextFields()
+                self.assertEqual(xTextFields.getCount(), i+1)
+                self.assertEqual(xTextFields.getByIndex(i).URL, 
"https://aWrongLink/";)
+                if (i==1):
+                    self.assertEqual(xTextFields.getByIndex(0).URL, 
"https://www.documentfoundation.org/";)
+                # 1. run: only the last inserted hyperlink will remain: 
"https://aWrongLink/";
+                # 2. run: both links will be in the cell
+
+# vim: set shiftwidth=4 softtabstop=4 expandtab:
diff --git a/sc/source/core/tool/appoptio.cxx b/sc/source/core/tool/appoptio.cxx
index 9dbc85f310ae..516c1a67e91f 100644
--- a/sc/source/core/tool/appoptio.cxx
+++ b/sc/source/core/tool/appoptio.cxx
@@ -80,6 +80,7 @@ void ScAppOptions::SetDefaults()
     mbShowSharedDocumentWarning = true;
 
     meKeyBindingType     = ScOptionsUtil::KEY_DEFAULT;
+    mbLinksInsertedLikeMSExcel = false;
 }
 
 ScAppOptions& ScAppOptions::operator=( const ScAppOptions& rCpy )
@@ -101,6 +102,7 @@ ScAppOptions& ScAppOptions::operator=( const ScAppOptions& 
rCpy )
     nDefaultObjectSizeHeight = rCpy.nDefaultObjectSizeHeight;
     mbShowSharedDocumentWarning = rCpy.mbShowSharedDocumentWarning;
     meKeyBindingType  = rCpy.meKeyBindingType;
+    mbLinksInsertedLikeMSExcel = rCpy.mbLinksInsertedLikeMSExcel;
     return *this;
 }
 
@@ -192,6 +194,7 @@ constexpr OUStringLiteral CFGPATH_MISC = 
u"Office.Calc/Misc";
 constexpr OUStringLiteral CFGPATH_COMPAT = u"Office.Calc/Compatibility";
 
 #define SCCOMPATOPT_KEY_BINDING     0
+#define SCCOMPATOPT_LINK_LIKE_MS    1
 
 // Default value of Layout/Other/StatusbarMultiFunction
 #define SCLAYOUTOPT_STATUSBARMULTI_DEFAULTVAL         514
@@ -258,7 +261,8 @@ Sequence<OUString> ScAppCfg::GetMiscPropertyNames()
 
 Sequence<OUString> ScAppCfg::GetCompatPropertyNames()
 {
-    return {"KeyBindings/BaseGroup"};   // SCCOMPATOPT_KEY_BINDING
+    return {"KeyBindings/BaseGroup",    // SCCOMPATOPT_KEY_BINDING
+            "Links" };                  // SCCOMPATOPT_LINK_LIKE_MS
 }
 
 ScAppCfg::ScAppCfg() :
@@ -455,6 +459,10 @@ void ScAppCfg::ReadCompatCfg()
     sal_Int32 nIntVal = 0; // 0 = 'Default'
     aValues[SCCOMPATOPT_KEY_BINDING] >>= nIntVal;
     SetKeyBindingType(static_cast<ScOptionsUtil::KeyBindingType>(nIntVal));
+
+    if (aValues.getLength() > SCCOMPATOPT_LINK_LIKE_MS)
+        SetLinksInsertedLikeMSExcel(
+            
ScUnoHelpFunctions::GetBoolFromAny(aValues[SCCOMPATOPT_LINK_LIKE_MS]));
 }
 
 IMPL_LINK_NOARG(ScAppCfg, LayoutCommitHdl, ScLinkConfigItem&, void)
@@ -626,6 +634,9 @@ IMPL_LINK_NOARG(ScAppCfg, CompatCommitHdl, 
ScLinkConfigItem&, void)
             case SCCOMPATOPT_KEY_BINDING:
                 pValues[nProp] <<= static_cast<sal_Int32>(GetKeyBindingType());
             break;
+            case SCCOMPATOPT_LINK_LIKE_MS:
+                pValues[nProp] <<= GetLinksInsertedLikeMSExcel();
+                break;
         }
     }
     aCompatItem.PutProperties(aNames, aValues);
diff --git a/sc/source/ui/app/scmod.cxx b/sc/source/ui/app/scmod.cxx
index b78d9fea6750..3cab82d17de6 100644
--- a/sc/source/ui/app/scmod.cxx
+++ b/sc/source/ui/app/scmod.cxx
@@ -982,6 +982,12 @@ void ScModule::ModifyOptions( const SfxItemSet& rOptSet )
         }
     }
 
+    if (const SfxBoolItem* pItem = rOptSet.GetItemIfSet(SID_SC_OPT_LINKS))
+    {
+        aAppOptions.SetLinksInsertedLikeMSExcel(pItem->GetValue());
+        bSaveAppOptions = true;
+    }
+
     // DefaultsOptions
     if (const ScTpDefaultsItem* pItem = 
rOptSet.GetItemIfSet(SID_SCDEFAULTSOPTIONS))
     {
@@ -1973,7 +1979,8 @@ std::optional<SfxItemSet> ScModule::CreateItemSet( 
sal_uInt16 nId )
                 SID_SC_INPUT_TEXTWYSIWYG, SID_SC_INPUT_TEXTWYSIWYG,
                 SID_SC_INPUT_REPLCELLSWARN, SID_SC_INPUT_REPLCELLSWARN,
                 // TP_VIEW:
-                SID_SC_OPT_SYNCZOOM, SID_SC_OPT_KEY_BINDING_COMPAT>);
+                SID_SC_OPT_SYNCZOOM, SID_SC_OPT_KEY_BINDING_COMPAT,
+                SID_SC_OPT_LINKS, SID_SC_OPT_LINKS>);
 
         const ScAppOptions& rAppOpt = GetAppOptions();
 
@@ -2046,6 +2053,7 @@ std::optional<SfxItemSet> ScModule::CreateItemSet( 
sal_uInt16 nId )
         // TP_COMPATIBILITY
         pRet->Put( SfxUInt16Item( SID_SC_OPT_KEY_BINDING_COMPAT,
                                    rAppOpt.GetKeyBindingType() ) );
+        pRet->Put( SfxBoolItem( SID_SC_OPT_LINKS, 
rAppOpt.GetLinksInsertedLikeMSExcel()));
 
         // TP_DEFAULTS
         pRet->Put( ScTpDefaultsItem( GetDefaultsOptions() ) );
diff --git a/sc/source/ui/inc/editsh.hxx b/sc/source/ui/inc/editsh.hxx
index 5fb8a3c9ade8..01457c3b7f2a 100644
--- a/sc/source/ui/inc/editsh.hxx
+++ b/sc/source/ui/inc/editsh.hxx
@@ -49,6 +49,7 @@ private:
     std::optional<bool> moAtContextMenu_DisableEditHyperlink;
 
     const SvxURLField* GetURLField();
+    const SvxURLField* GetFirstURLFieldFromCell();
     ScInputHandler* GetMyInputHdl();
 
     DECL_LINK( ClipboardChanged, TransferableDataHelper*, void );
diff --git a/sc/source/ui/inc/tpcompatibility.hxx 
b/sc/source/ui/inc/tpcompatibility.hxx
index c2fe28358928..942564f02436 100644
--- a/sc/source/ui/inc/tpcompatibility.hxx
+++ b/sc/source/ui/inc/tpcompatibility.hxx
@@ -24,6 +24,7 @@ public:
 
 private:
     std::unique_ptr<weld::ComboBox> m_xLbKeyBindings;
+    std::unique_ptr<weld::CheckButton> m_xBtnLink;
 };
 
 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
diff --git a/sc/source/ui/optdlg/tpcompatibility.cxx 
b/sc/source/ui/optdlg/tpcompatibility.cxx
index f468a68f679e..a9333e3153ad 100644
--- a/sc/source/ui/optdlg/tpcompatibility.cxx
+++ b/sc/source/ui/optdlg/tpcompatibility.cxx
@@ -10,6 +10,7 @@
 #undef SC_DLLIMPLEMENTATION
 
 #include <svl/intitem.hxx>
+#include <svl/eitem.hxx>
 
 #include <tpcompatibility.hxx>
 #include <sc.hrc>
@@ -18,6 +19,7 @@
 ScTpCompatOptions::ScTpCompatOptions(weld::Container* pPage, 
weld::DialogController* pController, const SfxItemSet &rCoreAttrs)
     : SfxTabPage(pPage, pController, 
"modules/scalc/ui/optcompatibilitypage.ui", "OptCompatibilityPage", &rCoreAttrs)
     , m_xLbKeyBindings(m_xBuilder->weld_combo_box("keybindings"))
+    , m_xBtnLink(m_xBuilder->weld_check_button("cellLinkCB"))
 {
 }
 
@@ -40,6 +42,12 @@ bool ScTpCompatOptions::FillItemSet(SfxItemSet *rCoreAttrs)
                 SID_SC_OPT_KEY_BINDING_COMPAT, 
m_xLbKeyBindings->get_active()));
         bRet = true;
     }
+    if (m_xBtnLink->get_state_changed_from_saved())
+    {
+        rCoreAttrs->Put(SfxBoolItem(SID_SC_OPT_LINKS, 
m_xBtnLink->get_active()));
+        bRet = true;
+    }
+
     return bRet;
 }
 
@@ -62,8 +70,13 @@ void ScTpCompatOptions::Reset(const SfxItemSet *rCoreAttrs)
                 ;
         }
     }
-
     m_xLbKeyBindings->save_value();
+
+    if (const SfxBoolItem* pbItem = rCoreAttrs->GetItemIfSet(SID_SC_OPT_LINKS))
+    {
+        m_xBtnLink->set_active(pbItem->GetValue());
+    }
+    m_xBtnLink->save_state();
 }
 
 DeactivateRC ScTpCompatOptions::DeactivatePage(SfxItemSet* /*pSet*/)
diff --git a/sc/source/ui/view/editsh.cxx b/sc/source/ui/view/editsh.cxx
index 34057df0e05f..99d84ca1518f 100644
--- a/sc/source/ui/view/editsh.cxx
+++ b/sc/source/ui/view/editsh.cxx
@@ -44,6 +44,8 @@
 #include <editeng/wghtitem.hxx>
 #include <sfx2/bindings.hxx>
 #include <sfx2/dispatch.hxx>
+#include <sfx2/docfile.hxx>
+#include <sfx2/docfilt.hxx>
 #include <sfx2/msg.hxx>
 #include <sfx2/objface.hxx>
 #include <sfx2/objsh.hxx>
@@ -59,6 +61,8 @@
 
 #include <editsh.hxx>
 #include <global.hxx>
+#include <appoptio.hxx>
+#include <scmod.hxx>
 #include <sc.hrc>
 #include <scmod.hxx>
 #include <inputhdl.hxx>
@@ -567,8 +571,12 @@ void ScEditShell::Execute( SfxRequest& rReq )
                     const OUString& rTarget   = pHyper->GetTargetFrame();
                     SvxLinkInsertMode eMode = pHyper->GetInsertMode();
 
+                    bool bCellLinksOnly
+                        = 
SC_MOD()->GetAppOptions().GetLinksInsertedLikeMSExcel()
+                          && 
rViewData.GetSfxDocShell()->GetMedium()->GetFilter()->IsMSOFormat();
+
                     bool bDone = false;
-                    if ( eMode == HLINK_DEFAULT || eMode == HLINK_FIELD )
+                    if ( (eMode == HLINK_DEFAULT || eMode == HLINK_FIELD) && 
!bCellLinksOnly )
                     {
                         const SvxURLField* pURLField = GetURLField();
                         if ( pURLField )
@@ -607,6 +615,17 @@ void ScEditShell::Execute( SfxRequest& rReq )
 
                     if (!bDone)
                     {
+                        if (bCellLinksOnly)
+                        {
+                            sal_Int32 nPar = pEngine->GetParagraphCount();
+                            if (nPar)
+                            {
+                                sal_Int32 nLen = pEngine->GetTextLen(nPar - 1);
+                                pTableView->SetSelection(ESelection(0, 0, nPar 
- 1, nLen));
+                                if (pTopView)
+                                    pTopView->SetSelection(ESelection(0, 0, 
nPar - 1, nLen));
+                            }
+                        }
                         rViewData.GetViewShell()->
                             InsertURL( rName, rURL, rTarget, 
static_cast<sal_uInt16>(eMode) );
 
@@ -771,19 +790,43 @@ void ScEditShell::GetState( SfxItemSet& rSet )
             case SID_HYPERLINK_GETLINK:
                 {
                     SvxHyperlinkItem aHLinkItem;
+                    bool bCellLinksOnly
+                        = 
SC_MOD()->GetAppOptions().GetLinksInsertedLikeMSExcel()
+                          && 
rViewData.GetSfxDocShell()->GetMedium()->GetFilter()->IsMSOFormat();
                     const SvxURLField* pURLField = GetURLField();
-                    if ( pURLField )
+                    if (!bCellLinksOnly)
                     {
-                        aHLinkItem.SetName( pURLField->GetRepresentation() );
-                        aHLinkItem.SetURL( pURLField->GetURL() );
-                        aHLinkItem.SetTargetFrame( pURLField->GetTargetFrame() 
);
+                        if (pURLField)
+                        {
+                            aHLinkItem.SetName(pURLField->GetRepresentation());
+                            aHLinkItem.SetURL(pURLField->GetURL());
+                            
aHLinkItem.SetTargetFrame(pURLField->GetTargetFrame());
+                        }
+                        else if (pActiveView)
+                        {
+                            // use selected text as name for urls
+                            OUString sReturn = pActiveView->GetSelected();
+                            sReturn = sReturn.copy(
+                                0, std::min(sReturn.getLength(), 
static_cast<sal_Int32>(255)));
+                            
aHLinkItem.SetName(comphelper::string::stripEnd(sReturn, ' '));
+                        }
                     }
-                    else if ( pActiveView )
+                    else
                     {
-                        // use selected text as name for urls
-                        OUString sReturn = pActiveView->GetSelected();
-                        sReturn = sReturn.copy(0, 
std::min(sReturn.getLength(), static_cast<sal_Int32>(255)));
-                        
aHLinkItem.SetName(comphelper::string::stripEnd(sReturn, ' '));
+                        if (!pURLField)
+                        {
+                            pURLField = GetFirstURLFieldFromCell();
+                        }
+                        if (pURLField)
+                        {
+                            aHLinkItem.SetURL(pURLField->GetURL());
+                            
aHLinkItem.SetTargetFrame(pURLField->GetTargetFrame());
+                        }
+                        ScDocument& rDoc = rViewData.GetDocument();
+                        SCCOL nPosX = rViewData.GetCurX();
+                        SCROW nPosY = rViewData.GetCurY();
+                        SCTAB nTab = rViewData.GetTabNo();
+                        aHLinkItem.SetName(rDoc.GetString(nPosX, nPosY, nTab));
                     }
                     rSet.Put(aHLinkItem);
                 }
@@ -865,6 +908,38 @@ const SvxURLField* ScEditShell::GetURLField()
     return nullptr;
 }
 
+const SvxURLField* ScEditShell::GetFirstURLFieldFromCell()
+{
+    EditEngine* pEE = GetEditView()->GetEditEngine();
+    sal_Int32 nParaCount = pEE->GetParagraphCount();
+    for (sal_Int32 nPara = 0; nPara < nParaCount; ++nPara)
+    {
+        ESelection aSel(nPara, 0);
+        std::vector<sal_Int32> aPosList;
+        pEE->GetPortions(nPara, aPosList);
+        for (const auto& rPos : aPosList)
+        {
+            aSel.nEndPos = rPos;
+
+            SfxItemSet aEditSet(pEE->GetAttribs(aSel));
+            if (aSel.nStartPos + 1 == aSel.nEndPos)
+            {
+                // test if the character is a text field
+                if (const SvxFieldItem* pItem = 
aEditSet.GetItemIfSet(EE_FEATURE_FIELD, false))
+                {
+                    const SvxFieldData* pField = pItem->GetField();
+                    if (const SvxURLField* pUrlField = dynamic_cast<const 
SvxURLField*>(pField))
+                    {
+                        return pUrlField;
+                    }
+                }
+            }
+            aSel.nStartPos = aSel.nEndPos;
+        }
+    }
+    return nullptr;
+}
+
 IMPL_LINK( ScEditShell, ClipboardChanged, TransferableDataHelper*, 
pDataHelper, void )
 {
     bPastePossible = ( pDataHelper->HasFormat( SotClipboardFormatId::STRING )
diff --git a/sc/uiconfig/scalc/ui/optcompatibilitypage.ui 
b/sc/uiconfig/scalc/ui/optcompatibilitypage.ui
index fca728c590d2..c28caccea047 100644
--- a/sc/uiconfig/scalc/ui/optcompatibilitypage.ui
+++ b/sc/uiconfig/scalc/ui/optcompatibilitypage.ui
@@ -13,7 +13,6 @@
         <property name="visible">True</property>
         <property name="can_focus">False</property>
         <property name="hexpand">True</property>
-        <property name="vexpand">True</property>
         <property name="label_xalign">0</property>
         <property name="shadow_type">none</property>
         <child>
@@ -22,7 +21,6 @@
             <property name="visible">True</property>
             <property name="can_focus">False</property>
             <property name="hexpand">True</property>
-            <property name="vexpand">True</property>
             <property name="row_spacing">6</property>
             <property name="column_spacing">12</property>
             <property name="margin-start">12</property>
@@ -32,7 +30,6 @@
                 <property name="visible">True</property>
                 <property name="can_focus">False</property>
                 <property name="hexpand">True</property>
-                <property name="vexpand">True</property>
                 <property name="label" translatable="yes" 
context="optcompatibilitypage|label2">Select desired _key binding type. 
Changing the key binding type may overwrite some of the existing key 
bindings.</property>
                 <property name="use_underline">True</property>
                 <property name="wrap">True</property>
@@ -81,6 +78,63 @@
         <property name="position">0</property>
       </packing>
     </child>
+    <child>
+      <object class="GtkFrame" id="frame2">
+        <property name="visible">True</property>
+        <property name="can_focus">False</property>
+        <property name="hexpand">True</property>
+        <property name="vexpand">True</property>
+        <property name="label_xalign">0</property>
+        <property name="shadow_type">none</property>
+        <child>
+          <!-- n-columns=1 n-rows=1 -->
+          <object class="GtkGrid" id="grid2">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="hexpand">True</property>
+            <property name="row_spacing">6</property>
+            <property name="column_spacing">12</property>
+            <property name="margin-start">12</property>
+            <property name="margin-top">6</property>
+            <child>
+              <object class="GtkCheckButton" id="cellLinkCB">
+                <property name="label" translatable="yes" 
context="optcompatibilitypage|cellLinkCB">Insert _hyperlink for the cell, not 
for the text in the cell. By default, Excel allows only one hyperlink per cell. 
(Works only if Excel file format is used.)</property>
+                <property name="visible">True</property>
+                <property name="can_focus">True</property>
+                <property name="receives_default">False</property>
+                <property name="use_underline">True</property>
+                <property name="draw_indicator">True</property>
+                <child internal-child="accessible">
+                  <object class="AtkObject" id="cellLinkCB-atkobject">
+                   <property name="AtkObject::accessible-description" 
translatable="yes" context="extended_tip|cellLinkCB">%PRODUCTNAME can insert 
multiple hyperlinks in a cell as text fields, but multiple hyperlinks in a cell 
cannot be saved to Excel file formats. This option prevents inserting multiple 
hyperlinks in a cell when Excel files are edited, in order to be interoperable 
with Excel.</property>
+                  </object>
+                </child>
+              </object>
+              <packing>
+                <property name="left_attach">0</property>
+                <property name="top_attach">0</property>
+                <property name="width">2</property>
+              </packing>
+            </child>
+          </object>
+        </child>
+        <child type="label">
+          <object class="GtkLabel" id="label3">
+            <property name="visible">True</property>
+            <property name="can_focus">False</property>
+            <property name="label" translatable="yes" 
context="optcompatibilitypage|label3">Hyperlinks</property>
+            <attributes>
+              <attribute name="weight" value="bold"/>
+            </attributes>
+          </object>
+        </child>
+      </object>
+      <packing>
+        <property name="expand">False</property>
+        <property name="fill">True</property>
+        <property name="position">1</property>
+      </packing>
+    </child>
     <child internal-child="accessible">
       <object class="AtkObject" id="OptCompatibilityPage-atkobject">
         <property name="AtkObject::accessible-description" translatable="yes" 
context="extended_tip|OptCompatibilityPage">Defines compatibility options for 
Calc.</property>

Reply via email to