From 0af4eaa856255616bf42a7ac609d48cd6e9b8173 Mon Sep 17 00:00:00 2001 From: Ferdinand Gassauer Date: Wed, 8 Apr 2026 10:56:46 +0200 Subject: [PATCH] Implement InfoBar for #REF! errors and fix indentation --- sc/inc/document.hxx | 69 ++++++++++++++++ sc/inc/refupdatecontext.hxx | 9 ++ sc/inc/sc.hrc | 4 + sc/inc/strings.hrc | 4 + sc/source/core/data/document.cxx | 93 +++++++++++++++++++++ sc/source/core/tool/token.cxx | 138 ++++++++++++++++++++++++++++++- sc/source/ui/inc/tabvwsh.hxx | 6 ++ sc/source/ui/view/tabvwsh3.cxx | 20 +++++ sc/source/ui/view/tabvwsh5.cxx | 78 +++++++++++++++++ sc/source/ui/view/tabvwshb.cxx | 10 +++ sc/source/ui/view/viewfunc.cxx | 42 ++++++++++ 11 files changed, 472 insertions(+), 1 deletion(-) diff --git a/sc/inc/document.hxx b/sc/inc/document.hxx index 4e24d00fe4345..aac18515829a7 100644 --- a/sc/inc/document.hxx +++ b/sc/inc/document.hxx @@ -61,7 +61,16 @@ #include "SheetViewTypes.hxx" #include +#include +#include +#include + +// REF_CODE_BEGIN +namespace sc { + struct RefErrorResult; +} +// REF_CODE_END namespace com::sun::star::chart2 { class XChartDocument; } namespace oox { @@ -99,6 +108,25 @@ class CopyFromClipContext; class ColumnSpanSet; struct ColumnBlockPosition; struct RefUpdateContext; +// REF_CODE_BEGIN +//struct RefUpdateResult; +// REF_CODE_END +// REF_CODE_BEGIN +struct RefErrorResult { + bool mbRefErrorCreated; + OUString maDeletedAddressStr; // For %1 (The "Source" of the error) + OUString maErrorAddressStr; // For %2 (The "Target" cell containing #REF!) + + RefErrorResult() : mbRefErrorCreated(false) {} + + void reset() { + mbRefErrorCreated = false; + maDeletedAddressStr = OUString(); + maErrorAddressStr = OUString(); + //maFirstErrorAddress.SetInvalid(); + } + }; +// REF_CODE_END class EditTextIterator; struct NoteEntry; class DocumentLinkManager; @@ -218,6 +246,9 @@ class ScTableStyles; namespace sc { typedef std::map IconSetBitmapMap; +// REF_CODE_BEGIN +//struct RefUpdateResult; +// REF_CODE_END } @@ -354,8 +385,43 @@ friend class sc::EditTextIterator; friend struct ScMutationGuard; friend struct ScMutationDisable; +// REF_CODE_BEGIN +private: + // ... other members ... + sc::RefErrorResult maPendingRefError; // This stores the error +public: + // Setter (used by token.cxx) + //void SetPendingRefError(const ScAddress& rAddr) { + // maPendingRefError.mbRefErrorCreated = true; + // maPendingRefError.maFirstErrorAddress = rAddr; + // } + void SetPendingRefError(const sc::RefErrorResult& rRes) { + maPendingRefError = rRes; + } + + // Getter (used by viewfunc.cxx) + const sc::RefErrorResult& GetPendingRefError() const { return maPendingRefError; } + void ResetPendingRefError(); + + // Clear (used by viewfunc.cxx) + void ClearPendingRefError() { + maPendingRefError.mbRefErrorCreated = false; + //maPendingRefError.maFirstErrorAddress.SetInvalid(); + maPendingRefError.reset(); + } + bool HasPendingRefError() const { return maPendingRefError.mbRefErrorCreated; } + + void BroadcastPendingRefError() { + if (maPendingRefError.mbRefErrorCreated) { + BroadcastRefError(maPendingRefError); + // Clear it so it doesn't pop up again on the next click + maPendingRefError.mbRefErrorCreated = false; + } + } +// REF_CODE_END public: + enum class HardRecalcState { OFF, /// normal calculation of dependencies @@ -1869,6 +1935,9 @@ public: void UpdateReference( sc::RefUpdateContext& rCxt, ScDocument* pUndoDoc = nullptr, bool bIncludeDraw = true, bool bUpdateNoteCaptionPos = true ); + // REF_CODE_BEGIN + void BroadcastRefError(const sc::RefErrorResult& rRes); + // REF_CODE_END /** * @param pClipDoc original clipboard doc, i.e. non-transposed * This clip doc is used to check references pointing to cut cells. diff --git a/sc/inc/refupdatecontext.hxx b/sc/inc/refupdatecontext.hxx index 7789dfdc3a9e0..17f51224a1d87 100644 --- a/sc/inc/refupdatecontext.hxx +++ b/sc/inc/refupdatecontext.hxx @@ -115,6 +115,15 @@ struct RefUpdateResult SCTAB mnTab; + // REF_CODE_BEGIN + /** * Flagged true if a previously valid reference becomes #REF! + * during this specific update operation. + */ + bool mbRefErrorCreated; + /** Stores the address of the cell containing the formula that errored. */ + ScAddress maFirstErrorAddress; + // REF_CODE_END + RefUpdateResult(); }; diff --git a/sc/inc/sc.hrc b/sc/inc/sc.hrc index 82f78f6607f05..047549ebc2007 100644 --- a/sc/inc/sc.hrc +++ b/sc/inc/sc.hrc @@ -33,6 +33,10 @@ class SvxZoomSliderItem; // SC_VIEW_END (SID_SC_START + 99) #define SC_MESSAGE_START (SID_SC_START + 100) // SC_MESSAGE_END (SID_SC_START + 199) +// REF_CODE_BEGIN +#define SC_HINT_REF_ERROR_CREATED (SC_MESSAGE_START + 70) +#define SID_REF_ERROR_NOTIFY (SID_SC_START + 1234) +// REF_CODE_END #ifndef SC_FUNCTION_START // duplicated in sfx2/sfxsids.hrc #define SC_FUNCTION_START (SID_SC_START + 200) #endif diff --git a/sc/inc/strings.hrc b/sc/inc/strings.hrc index 89ca3a06793ab..070952f959b51 100644 --- a/sc/inc/strings.hrc +++ b/sc/inc/strings.hrc @@ -467,4 +467,8 @@ #define STR_AUTOCALC_OFF NC_("STR_AUTOCALC_OFF", "AutoCalculate: Off\nClick to enable.") +// REF_CODE_BEGIN +#define STR_LOGIC_DESTROYED NC_("STR_LOGIC_DESTROYED", "Deleting former cell <%1> broke a formula in cell <%2>. Click UNDO twice(!) to restore the formula.") +// REF_CODE_END +// /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sc/source/core/data/document.cxx b/sc/source/core/data/document.cxx index 6182aa0816138..ba69edc84a70e 100644 --- a/sc/source/core/data/document.cxx +++ b/sc/source/core/data/document.cxx @@ -104,6 +104,19 @@ #include #include +#include +#include +#include + + +// REF_CODE_BEGIN +#include // Provides the definition of SC_HINT_REF_ERROR_CREATED +#include // Provides the FULL definition of class ScHint +#include // Provides the definition of sc::RefUpdateResult +#include // Provides the definition of ScDocShell +#include // For the ScHint definition +// REF_CODE_END + using ::editeng::SvxBorderLine; using namespace ::com::sun::star; @@ -777,6 +790,24 @@ bool ScDocument::DeleteTab( SCTAB nTab ) bValid = true; } } + + // REF_CODE_BEGIN + if (bValid) + { + // --- START OF INFOBAR TRIGGER LOGIC --- + // This logic detects if deleting the sheet caused any formula to break (producing #REF!) + // and broadcasts a hint to show an InfoBar in the UI. + + if (HasPendingRefError()) + { + // Debug log for development tracking + fprintf(stderr, "===[ DEBUG ]=== DeleteTab: Logic error detected in cell: %s\n", + maPendingRefError.maErrorAddressStr.toUtf8().getStr()); + + BroadcastPendingRefError(); + } + } + // REF_CODE_END return bValid; } @@ -867,6 +898,36 @@ bool ScDocument::DeleteTabs( SCTAB nTab, SCTAB nSheets ) bValid = true; } } + // REF_CODE_BEGIN + if (bValid) + { + // --- START OF INFOBAR TRIGGER LOGIC --- + + // 1. Create a standard Reference Update Context (Base Class) + sc::RefUpdateContext aRefCxt(*this); + aRefCxt.meMode = URM_INSDEL; + + // 2. Use 'nSheets' as the verified parameter name. + aRefCxt.mnTabDelta = -(static_cast(nSheets)); + + // 3. Define the affected range: from the deleted tab to the new end of the document. + aRefCxt.maRange = ScRange(0, 0, nTab, MaxCol(), MaxRow(), GetTableCount()); + + // 4. Trigger the document-wide update to find formulas that now point to #REF! + UpdateReference(aRefCxt); + + // 5. If a logic error was caught, broadcast it to trigger the InfoBar in the UI + if (HasPendingRefError()) + { + fprintf(stderr, "===[ DEBUG ]=== DeleteTabs: Logic error detected in cell: %s\n", + maPendingRefError.maErrorAddressStr.toUtf8().getStr()); + + + BroadcastPendingRefError(); + } + } + // REF_CODE_END + return bValid; } @@ -6946,4 +7007,36 @@ void ScDocument::MergeContextBackIntoNonThreadedContext(ScInterpreterContext& th threadedContext.MergeDefaultFormatKeys(*GetFormatTable()); } +// REF_CODE_BEGIN +void ScDocument::BroadcastRefError(const sc::RefErrorResult& rResErr) + { + if (!rResErr.mbRefErrorCreated) + return; + + // Create the hint + //ScHint aHint(static_cast(SC_HINT_REF_ERROR_CREATED), rResErr.maFirstErrorAddress); + ScHint aHint(static_cast(SC_HINT_REF_ERROR_CREATED), ScAddress()); + + // Get the DocShell (the UI container) + ScDocShell* pDocSh = GetDocumentShell(); + if (pDocSh) + { + fprintf(stderr, "===[ DEBUG ]=== Broadcasting via DocShell for %s\n", + rResErr.maErrorAddressStr.toUtf8().getStr()); + if (pDocSh) pDocSh->Broadcast(aHint); // This is the "channel" the UI listens to + } + else + { + fprintf(stderr, "===[ DEBUG ]=== No DocShell! Falling back to Document Broadcast.\n"); + Broadcast(aHint); + } + fflush(stderr); +} +void ScDocument::ResetPendingRefError() +{ + maPendingRefError.reset(); +} +// REF_CODE_END + + /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sc/source/core/tool/token.cxx b/sc/source/core/tool/token.cxx index bad83b4ebb97c..ac1372a3af890 100644 --- a/sc/source/core/tool/token.cxx +++ b/sc/source/core/tool/token.cxx @@ -3196,8 +3196,61 @@ bool isDBDataModified( const ScDocument& rDoc, const formula::FormulaToken& rTok } +// REF_CODE_BEGIN +namespace { + +/** + * Updates the RefUpdateResult if a transition from "No Error" to "REF Error" is detected. + */ +//void lcl_updateRefError(sc::RefErrorResult& rResErr, const ScAddress& rPos, bool bOldError, bool bNewError) +//void lcl_updateResult(sc::RefErrorResult& rRes, const ScAddress& rPos, bool bOldError, bool bNewError) +//void lcl_updateRefError(sc::RefErrorResult& rResErr, const ScAddress& rPos, +// bool bOldError, bool bNewError, ScDocument* pDoc) +void lcl_updateRefError(sc::RefErrorResult& rResErr, const ScAddress& rPos, + const ScAddress& rOldRefAddr, bool bOldError, bool bNewError, ScDocument* pDoc) +{ + fprintf(stderr, "===[ DEBUG ]=== File: %s | Line: %d | Function: %s\n", __FILE__, __LINE__, __func__); + rPos.Format(ScRefFlags::ADDR_ABS).toUtf8().getStr(); + fflush(stderr); + if (!bOldError && bNewError) + { + if (!bOldError && bNewError) + { + if (!rResErr.mbRefErrorCreated) + { + rResErr.mbRefErrorCreated = true; + + if (pDoc) + { + // %1: The DELETED cell (The "Source") + // We format the address we calculated from the old reference + rResErr.maDeletedAddressStr = rOldRefAddr.Format(ScRefFlags::ADDR_ABS_3D, pDoc); + // %2: The FORMULA cell (The "Target") + // This is the cell currently being processed (rPos) + rResErr.maErrorAddressStr = rPos.Format(ScRefFlags::ADDR_ABS_3D, pDoc); + + // Persist to document + pDoc->SetPendingRefError(rResErr); + } + + fprintf(stderr, "===[ DEBUG ]=== Radiology: DELETE of %s destroyed logic in %s\n", + rResErr.maDeletedAddressStr.toUtf8().getStr(), + rResErr.maErrorAddressStr.toUtf8().getStr()); + fflush(stderr); + } + } + } + +} + +} // namespace +// REF_CODE_END + sc::RefUpdateResult ScTokenArray::AdjustReferenceOnShift( const sc::RefUpdateContext& rCxt, const ScAddress& rOldPos ) { +// REF_CODE_BEGIN + sc::RefErrorResult rResErr; // This object is populated by lcl_updateRefError calls inside this function +// REF_CODE_END ScRange aSelectedRange = getSelectedRange(rCxt); sc::RefUpdateResult aRes; @@ -3232,9 +3285,22 @@ sc::RefUpdateResult ScTokenArray::AdjustReferenceOnShift( const sc::RefUpdateCon if (rCxt.isDeleted() && aSelectedRange.Contains(aAbs)) { + // REF_CODE_BEGIN + fprintf(stderr, "===[ DEBUG ]=== File: %s | Line: %d | Function: %s\n", __FILE__, __LINE__, __func__); + fflush(stderr); + + bool bOldError = rRef.IsDeleted(); + // REF_CODE_END + // // This reference is in the deleted region. setRefDeleted(rRef, rCxt); aRes.mbValueChanged = true; + // REF_CODE_BEGIN + //lcl_updateRefError(rResErr, rOldPos, bOldError, rRef.IsDeleted(), &rCxt.mrDoc); + //lcl_updateRefError(rResErr, rOldPos, aAbs, bOldError, rRef.IsDeleted(), &rCxt.mrDoc); + //lcl_updateRefError(rResErr, rOldPos, aAbs.aStart, bOldError, rRef.IsDeleted(), &rCxt.mrDoc); + lcl_updateRefError(rResErr, rOldPos, aAbs, bOldError, rRef.IsDeleted(), &rCxt.mrDoc); + // REF_CODE_END break; } @@ -3424,6 +3490,21 @@ sc::RefUpdateResult ScTokenArray::AdjustReferenceOnShift( const sc::RefUpdateCon } } + // REF_CODE_BEGIN + if (rResErr.mbRefErrorCreated) + { + // rCxt.mrDoc is the reference to the ScDocument + // We "persist" the error here so the ViewFunc can find it later + //rCxt.mrDoc.SetPendingRefError(aRes.maFirstErrorAddress); + rCxt.mrDoc.SetPendingRefError(rResErr); + + // Debugging to confirm the engine has passed the data to the document + fprintf(stderr, "===[ DEBUG ]=== Core: Ref Error at %s saved to Document.\n", + rResErr.maErrorAddressStr.toUtf8().getStr()); + fflush(stderr); + } + // REF_CODE_END + return aRes; } @@ -3431,6 +3512,10 @@ sc::RefUpdateResult ScTokenArray::AdjustReferenceOnMove( const sc::RefUpdateContext& rCxt, const ScAddress& rOldPos, const ScAddress& rNewPos ) { sc::RefUpdateResult aRes; + // REF_CODE_BEGIN + sc::RefErrorResult rResErr; // This object is populated by lcl_updateRefError calls inside this function + // REF_CODE_END + if (!rCxt.mnColDelta && !rCxt.mnRowDelta && !rCxt.mnTabDelta) // The cell hasn't moved at all. @@ -4237,12 +4322,27 @@ sc::RefUpdateResult ScTokenArray::AdjustReferenceInMovedName( const sc::RefUpdat namespace { -bool adjustSingleRefOnDeletedTab( const ScSheetLimits& rLimits, ScSingleRefData& rRef, SCTAB nDelPos, SCTAB nSheets, const ScAddress& rOldPos, const ScAddress& rNewPos ) +bool adjustSingleRefOnDeletedTab( const ScSheetLimits& rLimits, ScSingleRefData& rRef, SCTAB nDelPos, SCTAB nSheets, const ScAddress& rOldPos, const ScAddress& rNewPos, ScDocument* pDoc = nullptr) { ScAddress aAbs = rRef.toAbs(rLimits, rOldPos); if (nDelPos <= aAbs.Tab() && aAbs.Tab() < nDelPos + nSheets) { rRef.SetTabDeleted(true); + // --- REF_CODE_BEGIN + if (pDoc && pDoc->GetDocumentShell()) + { + sc::RefErrorResult aResLocal; + aResLocal.mbRefErrorCreated = true; +// aResLocal.maFirstErrorAddress = rOldPos; + aResLocal.maErrorAddressStr = rOldPos.Format(ScRefFlags::ADDR_ABS_3D, pDoc); + + fprintf(stderr, "===[ DEBUG ]=== Core stored string: %s\n", + aResLocal.maErrorAddressStr.toUtf8().getStr()); + + pDoc->SetPendingRefError(aResLocal); + pDoc->BroadcastRefError(aResLocal); + } + // --- REF_CODE_END return true; } @@ -4323,6 +4423,10 @@ bool adjustDoubleRefOnDeleteTab(const ScSheetLimits& rLimits, ScComplexRefData& sc::RefUpdateResult ScTokenArray::AdjustReferenceOnDeletedTab( const sc::RefUpdateDeleteTabContext& rCxt, const ScAddress& rOldPos ) { +// REF_CODE_BEGIN + sc::RefErrorResult rResErr; // This object is populated by lcl_updateRefError calls inside this function +// REF_CODE_END + sc::RefUpdateResult aRes; ScAddress aNewPos = rOldPos; ScRangeUpdater::UpdateDeleteTab( aNewPos, rCxt); @@ -4342,15 +4446,46 @@ sc::RefUpdateResult ScTokenArray::AdjustReferenceOnDeletedTab( const sc::RefUpda { case svSingleRef: { + fprintf(stderr, "===[ DEBUG ]=== File: %s | Line: %d | Function: %s\n", __FILE__, __LINE__, __func__); + fflush(stderr); ScSingleRefData& rRef = *p->GetSingleRef(); + ScAddress aAbs = rRef.toAbs(*mxSheetLimits, rOldPos); + // REF_CODE_BEGIN + bool bOldError = rRef.IsTabDeleted(); + // REF_CODE_END + if (adjustSingleRefOnDeletedTab(*mxSheetLimits, rRef, rCxt.mnDeletePos, rCxt.mnSheets, rOldPos, aNewPos)) aRes.mbReferenceModified = true; + + // REF_CODE_BEGIN_NOK + //lcl_updateRefError(rResErr, rOldPos, bOldError, rRef.IsTabDeleted()); + // lcl_updateRefError(rResErr, rOldPos, bOldError, rRef.IsTabDeleted(), &rCxt.mrDoc); + //lcl_updateRefError(rResErr, rOldPos, aAbs.aStart, bOldError, rRef.IsDeleted(), &rCxt.mrDoc); + //lcl_updateRefError(rResErr, rOldPos, aAbs, bOldError, rRef.IsDeleted(), &rCxt.mrDoc); + lcl_updateRefError(rResErr, rOldPos, aAbs, bOldError, rRef.IsTabDeleted(), &rCxt.mrDoc); + // REF_CODE_END } break; case svDoubleRef: { ScComplexRefData& rRef = *p->GetDoubleRef(); + ScRange aAbs = rRef.toAbs(*mxSheetLimits, rOldPos); + fprintf(stderr, "===[ DEBUG ]=== File: %s | Line: %d | Function: %s\n", __FILE__, __LINE__, __func__); + fflush(stderr); + + // REF_CODE_BEGIN + // A range is in error if either end point has a deleted tab + bool bOldError = rRef.Ref1.IsTabDeleted() || rRef.Ref2.IsTabDeleted(); + // REF_CODE_END + aRes.mbReferenceModified |= adjustDoubleRefOnDeleteTab(*mxSheetLimits, rRef, rCxt.mnDeletePos, rCxt.mnSheets, rOldPos, aNewPos); + + // REF_CODE_BEGIN_NOK + bool bNewError = rRef.Ref1.IsTabDeleted() || rRef.Ref2.IsTabDeleted(); + //lcl_updateRefError(rResErr, rOldPos, bOldError, bNewError); + //lcl_updateRefError(rResErr, rOldPos, bOldError, bNewError, &rCxt.mrDoc); + lcl_updateRefError(rResErr, rOldPos, aAbs.aStart, bOldError, bNewError, &rCxt.mrDoc); + // REF_CODE_END } break; default: @@ -5479,4 +5614,5 @@ void ScTokenArray::Dump() const } #endif + /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sc/source/ui/inc/tabvwsh.hxx b/sc/source/ui/inc/tabvwsh.hxx index 57eaade2927b6..92e6e7568d6f9 100644 --- a/sc/source/ui/inc/tabvwsh.hxx +++ b/sc/source/ui/inc/tabvwsh.hxx @@ -207,6 +207,11 @@ private: // Chart insert wizard's mark to make sure it undoes the correct thing in LOK case UndoStackMark m_InsertWizardUndoMark = MARK_INVALID; + // REF_CODE_BEGIN + // void UpdateRefErrorInfoBar(sc::RefErrorResult& rRes); + void UpdateRefErrorInfoBar(const sc::RefErrorResult& rResErr); + // REF_CODE_BEGIN + private: void Construct( TriState nForceDesignMode ); @@ -490,6 +495,7 @@ public: const std::shared_ptr& getScCondFormatDlgData() const { return m_pScCondFormatDlgData; } void SetInsertWizardUndoMark(); + void UpdateRefErrorInfoBar(); private: void ExecuteMoveTable( SfxRequest& rReq ); diff --git a/sc/source/ui/view/tabvwsh3.cxx b/sc/source/ui/view/tabvwsh3.cxx index ef488e2d911a8..2fcb618173453 100644 --- a/sc/source/ui/view/tabvwsh3.cxx +++ b/sc/source/ui/view/tabvwsh3.cxx @@ -70,6 +70,8 @@ #include #include +#include // For InfobarType + namespace { void collectUIInformation(const OUString& aZoom) @@ -1646,6 +1648,24 @@ void ScTabViewShell::Execute( SfxRequest& rReq ) } break; + case SID_REF_ERROR_NOTIFY: + { + // Extract the message we passed in the StringItem + const SfxStringItem* pItem = rReq.GetArg(SID_REF_ERROR_NOTIFY); + if (pItem) + { + // This call now happens safely AFTER the deletion command has finished. + // It will NOT be recorded in the Undo/Redo stack. + GetViewFrame().AppendInfoBar( + u"ref_logic_error"_ustr, + u"Ref Error"_ustr, // Title + pItem->GetValue(), // The message (%1 destroyed %2...) + InfobarType::DANGER, + true + ); + } + break; + } default: OSL_FAIL("Unknown Slot at ScTabViewShell::Execute"); break; diff --git a/sc/source/ui/view/tabvwsh5.cxx b/sc/source/ui/view/tabvwsh5.cxx index 4799465aa4b9f..e3d2f5f9c75b2 100644 --- a/sc/source/ui/view/tabvwsh5.cxx +++ b/sc/source/ui/view/tabvwsh5.cxx @@ -38,9 +38,50 @@ #include #include #include +#include + +// REF_CODE_BEGIN +#include +#include +#include + +#include +#include // Provides the definition of SC_HINT_REF_ERROR_CREATED +#include // Provides the FULL definition of class ScHint +#include // Provides the definition of sc::RefUpdateResult +#include // Provides the definition of ScDocShell +#include // For the ScHint definition + + +// REF_CODE_END void ScTabViewShell::Notify( SfxBroadcaster& rBC, const SfxHint& rHint ) { + // REF_CODE_BEGIN +SfxViewShell::Notify(rBC, rHint); +if (static_cast(rHint.GetId()) == SC_HINT_REF_ERROR_CREATED) +{ + const ScHint* pScHint = dynamic_cast(&rHint); + if (pScHint) + { + fprintf(stderr, "===[ DEBUG ]=== UI: Notify caught RefError\n"); + + // FIX: Get the document and the ACTUAL result we stored in the Core + ScDocument& rDoc = GetViewData().GetDocument(); + //sc::RefErrorResult& rStoredErr = rDoc.GetPendingRefError(); + const sc::RefErrorResult& rStoredErr = rDoc.GetPendingRefError(); + + // Safety log: see if the string actually made it here + fprintf(stderr, "===[ DEBUG ]=== UI: String found in Doc: %s\n", + rStoredErr.maErrorAddressStr.toUtf8().getStr()); + + // Pass the actual stored error (with the string) to the InfoBar function + UpdateRefErrorInfoBar(rStoredErr); + + return; + } +} + // REF_CODE_END if (rHint.GetId() == SfxHintId::ScPaint) // draw new { const ScPaintHint* pPaintHint = static_cast(&rHint); @@ -397,4 +438,41 @@ void ScTabViewShell::UpdateNumberFormatter( rInfoItem.GetNumberFormatter()->DeleteEntry( key ); } + +// REF_CODE_BEGIN +using namespace com::sun::star; + +void ScTabViewShell::UpdateRefErrorInfoBar(const sc::RefErrorResult& rResErr) +{ + // 1. Safety check: If no error was created or the string is empty, do nothing. + if (!rResErr.mbRefErrorCreated || rResErr.maErrorAddressStr.isEmpty()) + return; + + // 2. Clean up both addresses (e.g., Sheet1.$A$1 -> Sheet1.A1) + // %1 is the Deleted Source, %2 is the Broken Target + OUString sDisplaySource = rResErr.maDeletedAddressStr.replaceAll("$", ""); + OUString sDisplayTarget = rResErr.maErrorAddressStr.replaceAll("$", ""); + + // 3. Debug log showing both sides of the "Radiology" report + fprintf(stderr, "===[ DEBUG ]=== UI InfoBar: DELETE of %s destroyed %s\n", + sDisplaySource.toUtf8().getStr(), + sDisplayTarget.toUtf8().getStr()); + + // 4. Format the final message: "DELETE of %1 destroyed the logic in cell %2..." + // Note: replaceFirst is called twice to fill both %1 and %2 + OUString sMsg = ScResId(STR_LOGIC_DESTROYED) + .replaceFirst(u"%1", sDisplaySource) + .replaceFirst(u"%2", sDisplayTarget); + + // 5. Append to the ViewFrame + GetViewFrame().AppendInfoBar( + u"ref_logic_error"_ustr, + u"Ref Error"_ustr, + sMsg, + InfobarType::DANGER, + true + ); +} + +// REF_CODE_END /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/sc/source/ui/view/tabvwshb.cxx b/sc/source/ui/view/tabvwshb.cxx index 6039bcdadf3a7..849e4ea70f6da 100644 --- a/sc/source/ui/view/tabvwshb.cxx +++ b/sc/source/ui/view/tabvwshb.cxx @@ -828,6 +828,16 @@ void ScTabViewShell::ExecuteUndo(SfxRequest& rReq) else pUndoManager->RedoWithContext(aUndoRedoContext); } + // 2. RADIOLOGY CLEANUP: Execute immediately after the loop + if ( bIsUndo ) + { + ScDocument& rDoc = GetViewData().GetDocument(); + rDoc.ResetPendingRefError(); + + // Remove the InfoBar using the unique ID + GetViewFrame().RemoveInfoBar(u"ref_logic_error"_ustr); + } + // --- RADIOLOGY CLEANUP END --- } catch ( const uno::Exception& ) { diff --git a/sc/source/ui/view/viewfunc.cxx b/sc/source/ui/view/viewfunc.cxx index fcb22f0a624e0..bca9de3ae6420 100644 --- a/sc/source/ui/view/viewfunc.cxx +++ b/sc/source/ui/view/viewfunc.cxx @@ -2038,10 +2038,16 @@ void ScViewFunc::DeleteCells( DelCellCmd eCmd ) ScDocShell* pDocSh = GetViewData().GetDocShell(); const ScMarkData& rMark = GetViewData().GetMarkData(); + #if HAVE_FEATURE_MULTIUSER_ENVIRONMENT // #i94841# [Collaboration] if deleting rows is rejected, the content is sometimes wrong if ( pDocSh->IsDocShared() && ( eCmd == DelCellCmd::Rows || eCmd == DelCellCmd::Cols ) ) { + // --- REF_CODE_BEGIN --- + fprintf(stderr, "===[ DEBUG ]=== ViewFunc: Error detected. Triggering Broadcast 1.\n"); + fflush(stderr); + // --- REF_CODE_BEEND --- + ScRange aDelRange( aRange.aStart ); SCCOLROW nCount = 0; if ( eCmd == DelCellCmd::Rows ) @@ -2067,6 +2073,10 @@ void ScViewFunc::DeleteCells( DelCellCmd eCmd ) ResetAutoSpellForContentChange(); pDocSh->UpdateOle(GetViewData()); CellContentChanged(); + // --- REF_CODE_BEGIN --- + fprintf(stderr, "===[ DEBUG ]=== ViewFunc: Error detected. Triggering Broadcast 3.\n"); + fflush(stderr); + // --- REF_CODE_BEEND --- if ( eCmd == DelCellCmd::Rows || eCmd == DelCellCmd::Cols ) { @@ -2100,8 +2110,40 @@ void ScViewFunc::DeleteCells( DelCellCmd eCmd ) true /* bHidden */, true /* bFiltered */, true /* bGroups */, GetViewData().GetTabNumber()); } + + // --- REF_CODE_BEGIN --- + ScDocument& rDoc = GetViewData().GetDocument(); + const sc::RefErrorResult& rResErr = rDoc.GetPendingRefError(); + + if (rResErr.mbRefErrorCreated) + { + fprintf(stderr, "===[ DEBUG ]=== ViewFunc: Error detected. Triggering Broadcast Cells.\n"); + fflush(stderr); + + // 1. Shouts the message to the UI + rDoc.BroadcastRefError(rResErr); + + // 2. IMPORTANT: Clear the error so it doesn't fire again + // on the next unrelated UI refresh/paint. + rDoc.ClearPendingRefError(); + } + if (GetViewData().GetDocShell()->GetDocument().HasPendingRefError()) +{ + fprintf(stderr, "===[ DEBUG ]=== ViewFunc: Error detected. Triggering Broadcast.\n"); + // This will call the BroadcastRefError function you wrote earlier + GetViewData().GetDocShell()->GetDocument().BroadcastPendingRefError(); +} + // --- REF_CODE_BEEND --- + + } else + // --- REF_CODE_BEGIN --- + fprintf(stderr, "===[ DEBUG ]=== ViewFunc: NO Error detected. Triggering Broadcast 4\n"); + fflush(stderr); + // --- REF_CODE_BEEND --- + + { if (eCmd == DelCellCmd::Cols) DeleteMulti( false );