diff --git a/include/sfx2/namedcolor.hxx b/include/sfx2/namedcolor.hxx index 1c5fe24b5144..6d82de7b30c3 100644 --- a/include/sfx2/namedcolor.hxx +++ b/include/sfx2/namedcolor.hxx @@ -31,7 +31,7 @@ struct SFX2_DLLPUBLIC NamedColor { } - model::ComplexColor getComplexColor() + model::ComplexColor getComplexColor() const { model::ComplexColor aComplexColor; diff --git a/include/sfx2/objsh.hxx b/include/sfx2/objsh.hxx index bad832977f65..14855e8b62f0 100644 --- a/include/sfx2/objsh.hxx +++ b/include/sfx2/objsh.hxx @@ -582,7 +582,7 @@ public: StarBASIC* GetBasic() const; std::optional GetRecentColor(sal_uInt16 nSlotId); - void SetRecentColor(sal_uInt16 nSlotId, const NamedColor& rColor); + void SetRecentColor(sal_uInt16 nSlotId, const NamedColor& rColor, bool bBroadcast = true); virtual std::shared_ptr GetDocumentModelAccessor() const; virtual std::set GetDocColors(); diff --git a/sfx2/source/doc/objcont.cxx b/sfx2/source/doc/objcont.cxx index 1507ad6f810d..d2130979c9a6 100644 --- a/sfx2/source/doc/objcont.cxx +++ b/sfx2/source/doc/objcont.cxx @@ -326,10 +326,11 @@ std::optional SfxObjectShell::GetRecentColor(sal_uInt16 nSlotId) return std::nullopt; } -void SfxObjectShell::SetRecentColor(sal_uInt16 nSlotId, const NamedColor& rColor) +void SfxObjectShell::SetRecentColor(sal_uInt16 nSlotId, const NamedColor& rColor, bool bBroadcast) { pImpl->m_aRecentColors[nSlotId] = rColor; - Broadcast(SfxHint(SfxHintId::ColorsChanged)); + if (bBroadcast) + Broadcast(SfxHint(SfxHintId::ColorsChanged)); } std::set SfxObjectShell::GetDocColors() diff --git a/svx/inc/tbxcolorupdate.hxx b/svx/inc/tbxcolorupdate.hxx index 6633090e0ee9..7e2170c7b599 100644 --- a/svx/inc/tbxcolorupdate.hxx +++ b/svx/inc/tbxcolorupdate.hxx @@ -59,7 +59,7 @@ namespace svx virtual ~ToolboxButtonColorUpdaterBase(); void Notify(SfxBroadcaster& rBC, const SfxHint& rHint) override; - void SetRecentColor(const NamedColor& rNamedColor); + void SetRecentColor(const NamedColor& rNamedColor, bool bBroadcast = true); void Update( const NamedColor& rNamedColor ); void Update( const Color& rColor, bool bForceUpdate = false ); Color const & GetCurrentColor() const { return maCurColor; } diff --git a/svx/source/tbxctrls/tbxcolorupdate.cxx b/svx/source/tbxctrls/tbxcolorupdate.cxx index be2d13c79b3e..c84d37fe73eb 100644 --- a/svx/source/tbxctrls/tbxcolorupdate.cxx +++ b/svx/source/tbxctrls/tbxcolorupdate.cxx @@ -122,6 +122,7 @@ namespace svx o3tl::toUInt32(o3tl::getToken(aUserData, 0, ';', nIdx))); } } + SetRecentColor(aNamedColor, /*Broadcast=*/false); Update(aNamedColor); } else @@ -149,10 +150,10 @@ namespace svx } } - void ToolboxButtonColorUpdaterBase::SetRecentColor(const NamedColor &rNamedColor) + void ToolboxButtonColorUpdaterBase::SetRecentColor(const NamedColor &rNamedColor, bool bBroadcast) { if (rtl::Reference xModel = dynamic_cast(mxFrame->getController()->getModel().get())) - xModel->GetObjectShell()->SetRecentColor(mnSlotId, rNamedColor); + xModel->GetObjectShell()->SetRecentColor(mnSlotId, rNamedColor, bBroadcast); else if (!mbWideButton) Update(rNamedColor); } diff --git a/sw/qa/extras/uiwriter/uiwriter9.cxx b/sw/qa/extras/uiwriter/uiwriter9.cxx index f97d5a572d4d..8f968353bdf8 100644 --- a/sw/qa/extras/uiwriter/uiwriter9.cxx +++ b/sw/qa/extras/uiwriter/uiwriter9.cxx @@ -332,6 +332,22 @@ CPPUNIT_TEST_FIXTURE(SwUiWriterTest9, testTdf159816) xTransfer->PrivateDrop(*pWrtShell, ptTo, /*bMove=*/true, /*bXSelection=*/true); } +CPPUNIT_TEST_FIXTURE(SwUiWriterTest9, testTdf34804) +{ + createSwDoc(); + SwDoc* pDoc = getSwDoc(); + SwWrtShell* pWrtShell = pDoc->GetDocShell()->GetWrtShell(); + + // Simulate a keyboard shortcut to SID_ATTR_CHAR_COLOR2 (which must use the shared button color) + dispatchCommand(mxComponent, u".uno:FontColor"_ustr, {}); + pWrtShell->Insert(u"New World!"_ustr); + + const uno::Reference xRun = getRun(getParagraph(1, "New World!"), 1); + // (This test assumes that nothing in the unit tests has modified the app's recent font color) + // COL_DEFAULT_FONT is the default red color for the fontColor button on the toolbar. + CPPUNIT_ASSERT_EQUAL(COL_DEFAULT_FONT, getProperty(xRun, u"CharColor"_ustr)); +} + CPPUNIT_TEST_FIXTURE(SwUiWriterTest9, testTdf139631) { // Unit test for tdf#139631 diff --git a/sw/source/uibase/shells/textsh1.cxx b/sw/source/uibase/shells/textsh1.cxx index 1c07d8e8f5a3..2ac91aed446c 100644 --- a/sw/source/uibase/shells/textsh1.cxx +++ b/sw/source/uibase/shells/textsh1.cxx @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -1761,6 +1762,21 @@ void SwTextShell::Execute(SfxRequest &rReq) case SID_ATTR_CHAR_COLOR2: { + std::unique_ptr pRecentColor; // manage lifecycle scope + if (!pItem) + { + // no color provided: use the pre-selected color shown in the toolbar/sidebar + const std::optional& oColor + = GetView().GetDocShell()->GetRecentColor(SID_ATTR_CHAR_COLOR); + if (oColor.has_value()) + { + const model::ComplexColor& rCol = (*oColor).getComplexColor(); + pRecentColor = std::make_unique( + rCol.getFinalColor(), rCol, RES_CHRATR_COLOR); + pItem = pRecentColor.get(); + } + } + if (pItem) { auto* pColorItem = static_cast(pItem); @@ -1783,7 +1799,7 @@ void SwTextShell::Execute(SfxRequest &rReq) case SID_ATTR_CHAR_COLOR_BACKGROUND: // deprecated case SID_ATTR_CHAR_COLOR_EXT: { - Color aColor; + Color aColor = COL_TRANSPARENT; model::ComplexColor aComplexColor; if (pItem) @@ -1792,8 +1808,17 @@ void SwTextShell::Execute(SfxRequest &rReq) aColor = pColorItem->GetValue(); aComplexColor = pColorItem->getComplexColor(); } - else - aColor = COL_TRANSPARENT; + else if (nSlot == SID_ATTR_CHAR_BACK_COLOR) + { + // no color provided: use the pre-selected color shown in the toolbar/sidebar + const std::optional& oColor + = GetView().GetDocShell()->GetRecentColor(nSlot); + if (oColor.has_value()) + { + aComplexColor = (*oColor).getComplexColor(); + aColor = aComplexColor.getFinalColor(); + } + } SwEditWin& rEdtWin = GetView().GetEditWin(); if (nSlot != SID_ATTR_CHAR_COLOR_EXT)