workaround segfault in compiler on macos-clang-intel
[LibreOffice.git] / cui / source / tabpages / backgrnd.cxx
blobaa53dcd64afec7ab7739ab530e0a101432526b17
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <memory>
21 #include <sfx2/objsh.hxx>
22 #include <svx/svxids.hrc>
23 #include <editeng/colritem.hxx>
24 #include <backgrnd.hxx>
25 #include <svx/drawitem.hxx>
26 #include <svx/xfillit0.hxx>
27 #include <svx/xflclit.hxx>
28 #include <svx/flagsdef.hxx>
29 #include <svl/intitem.hxx>
30 #include <svx/unobrushitemhelper.hxx>
32 using namespace css;
34 // table background
35 #define TBL_DEST_CELL 0
36 #define TBL_DEST_ROW 1
37 #define TBL_DEST_TBL 2
39 const WhichRangesContainer SvxBkgTabPage::pBkgRanges(svl::Items<
40 SID_ATTR_BRUSH, SID_ATTR_BRUSH,
41 SID_ATTR_BRUSH_CHAR, SID_ATTR_BRUSH_CHAR
42 >);
44 static sal_uInt16 lcl_GetTableDestSlot(sal_Int32 nTblDest)
46 switch (nTblDest)
48 default:
49 case TBL_DEST_CELL:
51 return SID_ATTR_BRUSH;
53 case TBL_DEST_ROW:
55 return SID_ATTR_BRUSH_ROW;
57 case TBL_DEST_TBL:
59 return SID_ATTR_BRUSH_TABLE;
64 SvxBkgTabPage::SvxBkgTabPage(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet& rInAttrs)
65 : SvxAreaTabPage(pPage, pController, rInAttrs),
66 m_aAttrSet(*rInAttrs.GetPool(),
67 rInAttrs.GetRanges().MergeRange(XATTR_FILL_FIRST, XATTR_FILL_LAST))
69 m_xBtnGradient->hide();
70 m_xBtnHatch->hide();
71 m_xBtnBitmap->hide();
72 m_xBtnPattern->hide();
75 SvxBkgTabPage::~SvxBkgTabPage()
77 m_xTblLBox.reset();
80 void SvxBkgTabPage::ActivatePage( const SfxItemSet& )
82 SvxAreaTabPage::ActivatePage(m_aAttrSet);
85 DeactivateRC SvxBkgTabPage::DeactivatePage( SfxItemSet* _pSet )
87 if (DeactivateRC::KeepPage == SvxAreaTabPage::DeactivatePage(&m_aAttrSet))
88 return DeactivateRC::KeepPage;
90 if ( _pSet )
91 FillItemSet( _pSet );
93 return DeactivateRC::LeavePage;
96 void SvxBkgTabPage::Reset(const SfxItemSet* pItemSet)
98 if (m_xTblLBox && m_xTblLBox->get_visible())
100 if (m_nActPos == -1) // initial reset
102 m_nActPos = 0;
103 if (const SfxUInt16Item* pDestItem = pItemSet->GetItemIfSet(SID_BACKGRND_DESTINATION, false))
104 m_nActPos = pDestItem->GetValue();
105 m_xTblLBox->set_active(m_nActPos);
107 SetActiveTableDestinationBrushItem();
108 return;
110 else if (m_bCharBackColor)
112 sal_uInt16 nWhich(pItemSet->GetPool()->GetWhichIDFromSlotID(SID_ATTR_CHAR_BACK_COLOR));
113 Color aBackColor(static_cast<const SvxColorItem&>(pItemSet->Get(nWhich)).GetValue());
114 SvxBrushItem aBrushItem(SvxBrushItem(aBackColor, SID_ATTR_BRUSH_CHAR));
115 setSvxBrushItemAsFillAttributesToTargetSet(aBrushItem, m_aAttrSet);
117 else
119 sal_uInt16 nWhich(pItemSet->GetPool()->GetWhichIDFromSlotID(m_bHighlighting ? SID_ATTR_BRUSH_CHAR : SID_ATTR_BRUSH));
120 SvxBrushItem aBrushItem(static_cast<const SvxBrushItem&>(pItemSet->Get(nWhich)));
121 setSvxBrushItemAsFillAttributesToTargetSet(aBrushItem, m_aAttrSet);
123 SvxAreaTabPage::Reset(&m_aAttrSet);
126 bool SvxBkgTabPage::FillItemSet(SfxItemSet* pCoreSet)
128 sal_uInt16 nSlot = SID_ATTR_BRUSH;
129 if (m_xTblLBox && m_xTblLBox->get_visible())
130 nSlot = lcl_GetTableDestSlot(m_xTblLBox->get_active());
131 else if (m_bHighlighting)
132 nSlot = SID_ATTR_BRUSH_CHAR;
133 else if (m_bCharBackColor)
134 nSlot = SID_ATTR_CHAR_BACK_COLOR;
136 sal_uInt16 nWhich = GetWhich(nSlot);
138 drawing::FillStyle eFillType = m_aAttrSet.Get(XATTR_FILLSTYLE).GetValue();
139 switch( eFillType )
141 case drawing::FillStyle_NONE:
143 if ( IsBtnClicked() )
145 if ( SID_ATTR_CHAR_BACK_COLOR == nSlot )
147 pCoreSet->Put(SvxColorItem(COL_TRANSPARENT, nWhich));
149 else
151 pCoreSet->Put(SvxBrushItem(COL_TRANSPARENT, nWhich));
154 break;
156 case drawing::FillStyle_SOLID:
158 XFillColorItem aColorItem(m_aAttrSet.Get(XATTR_FILLCOLOR));
160 // tdf#157801 - don't change direct formatting when color is unchanged
161 if (const SfxPoolItem* pOldItem = GetOldItem(*pCoreSet, nSlot))
163 const SvxBrushItem* pOldBrushItem = static_cast<const SvxBrushItem*>(pOldItem);
164 if (pOldBrushItem->GetColor() == aColorItem.GetColorValue())
165 break;
168 if ( SID_ATTR_CHAR_BACK_COLOR == nSlot )
170 pCoreSet->Put(SvxColorItem(aColorItem.GetColorValue(), aColorItem.getComplexColor(), nWhich));
172 else
174 pCoreSet->Put(SvxBrushItem(aColorItem.GetColorValue(), aColorItem.getComplexColor(), nWhich));
176 break;
178 case drawing::FillStyle_BITMAP:
180 std::unique_ptr<SvxBrushItem> aBrushItem(getSvxBrushItemFromSourceSet(m_aAttrSet, nWhich));
181 if ( GraphicType::NONE != aBrushItem->GetGraphicObject()->GetType() )
182 pCoreSet->Put(std::move(aBrushItem));
183 break;
185 default:
186 break;
189 if (!m_xTblLBox || !m_xTblLBox->get_visible())
190 return true;
192 pCoreSet->Put(SfxUInt16Item(SID_BACKGRND_DESTINATION, m_xTblLBox->get_active()));
194 // *Put* in the core set all table brushes that are *SET* in the m_aAttrSet
195 if (nSlot != SID_ATTR_BRUSH)
197 nWhich = m_aAttrSet.GetPool()->GetWhichIDFromSlotID(SID_ATTR_BRUSH);
198 if (SfxItemState::SET == m_aAttrSet.GetItemState(nWhich))
200 SvxBrushItem aBrushItem(static_cast<const SvxBrushItem&>(m_aAttrSet.Get(nWhich)));
201 pCoreSet->Put(aBrushItem);
204 if (nSlot != SID_ATTR_BRUSH_ROW)
206 if (SfxItemState::SET == m_aAttrSet.GetItemState(SID_ATTR_BRUSH_ROW))
208 SvxBrushItem aBrushItem(m_aAttrSet.Get(SID_ATTR_BRUSH_ROW));
209 pCoreSet->Put(aBrushItem);
212 if (nSlot != SID_ATTR_BRUSH_TABLE)
214 if (SfxItemState::SET == m_aAttrSet.GetItemState(SID_ATTR_BRUSH_TABLE))
216 SvxBrushItem aBrushItem(m_aAttrSet.Get(SID_ATTR_BRUSH_TABLE));
217 pCoreSet->Put(aBrushItem);
221 return true;
224 std::unique_ptr<SfxTabPage> SvxBkgTabPage::Create(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet* rAttrs)
226 auto xRet = std::make_unique<SvxBkgTabPage>(pPage, pController, *rAttrs);
227 xRet->SetOptimalSize(pController);
228 return xRet;
231 void SvxBkgTabPage::PageCreated(const SfxAllItemSet& aSet)
233 const SfxUInt32Item* pFlagItem = aSet.GetItem<SfxUInt32Item>(SID_FLAG_TYPE, false);
234 if (pFlagItem)
236 SvxBackgroundTabFlags nFlags = static_cast<SvxBackgroundTabFlags>(pFlagItem->GetValue());
237 if ( nFlags & SvxBackgroundTabFlags::SHOW_TBLCTL )
239 m_xBtnBitmap->show();
240 m_xTblLBox = m_xBuilder->weld_combo_box(u"tablelb"_ustr);
241 m_xTblLBox->connect_changed(LINK(this, SvxBkgTabPage, TblDestinationHdl_Impl));
242 m_xTblLBox->show();
244 if ((nFlags & SvxBackgroundTabFlags::SHOW_HIGHLIGHTING) ||
245 (nFlags & SvxBackgroundTabFlags::SHOW_CHAR_BKGCOLOR))
247 m_bHighlighting = bool(nFlags & SvxBackgroundTabFlags::SHOW_HIGHLIGHTING);
248 m_bCharBackColor = bool(nFlags & SvxBackgroundTabFlags::SHOW_CHAR_BKGCOLOR);
250 if (nFlags & SvxBackgroundTabFlags::SHOW_SELECTOR)
251 m_xBtnBitmap->show();
252 SetOptimalSize(GetDialogController());
255 SfxObjectShell* pObjSh = SfxObjectShell::Current();
257 // we always have the color page
258 XColorListRef xColorTable;
259 if (pObjSh)
261 const SvxColorListItem* pItem = pObjSh->GetItem(SID_COLOR_TABLE);
262 if (pItem)
263 xColorTable = pItem->GetColorList();
265 if (!xColorTable.is())
266 xColorTable = XColorList::CreateStdColorList();
267 SetColorList(xColorTable);
269 // sometimes we have the bitmap page
270 if (m_xBtnBitmap->get_visible())
272 XBitmapListRef xBitmapList;
273 if (pObjSh)
274 if (const SvxBitmapListItem* pItem = pObjSh->GetItem(SID_BITMAP_LIST))
275 xBitmapList = pItem->GetBitmapList();
276 SetBitmapList(xBitmapList);
280 IMPL_LINK(SvxBkgTabPage, TblDestinationHdl_Impl, weld::ComboBox&, rBox, void)
282 sal_Int32 nSelPos = rBox.get_active();
284 if (m_nActPos == nSelPos)
285 return;
287 // Fill the local item set with XATTR_FILL settings gathered from the tab page, convert to
288 // SvxBrushItem and store in table destination slot Which. Do this so cell, row, and table
289 // brush items can be set together.
290 SvxAreaTabPage::FillItemSet(&m_aAttrSet);
291 m_aAttrSet.Put(getSvxBrushItemFromSourceSet(m_aAttrSet, GetWhich(lcl_GetTableDestSlot(m_nActPos))));
293 m_nActPos = nSelPos;
295 SetActiveTableDestinationBrushItem();
298 void SvxBkgTabPage::SetActiveTableDestinationBrushItem()
300 // set the table destination (cell, row, table) brush item as a fill item in the local item set
301 sal_uInt16 nWhich = GetWhich(lcl_GetTableDestSlot(m_nActPos));
302 if (SfxItemState::SET == GetItemSet().GetItemState(nWhich))
304 SvxBrushItem aBrushItem(static_cast<const SvxBrushItem&>(GetItemSet().Get(nWhich)));
305 setSvxBrushItemAsFillAttributesToTargetSet(aBrushItem, m_aAttrSet);
307 else
309 SelectFillType(*m_xBtnNone, &m_aAttrSet);
310 return;
313 XFillStyleItem aFillStyleItem(m_aAttrSet.Get(m_aAttrSet.GetPool()->GetWhichIDFromSlotID(XATTR_FILLSTYLE)));
314 drawing::FillStyle eXFS = aFillStyleItem.GetValue();
315 switch(eXFS)
317 default:
318 case drawing::FillStyle_NONE:
320 SelectFillType(*m_xBtnNone, &m_aAttrSet);
321 break;
323 case drawing::FillStyle_SOLID:
325 SelectFillType(*m_xBtnColor, &m_aAttrSet);
326 break;
328 case drawing::FillStyle_BITMAP:
330 SelectFillType(*m_xBtnBitmap, &m_aAttrSet);
331 break;
336 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */