Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sw / source / uibase / sidebar / PageStylesPanel.cxx
blob53cf43fb742ee9700f74bbfcae644adfb9f5c5d4
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 .
19 #include <sal/config.h>
20 #include <svl/intitem.hxx>
21 #include <svx/colorbox.hxx>
22 #include <svx/drawitem.hxx>
23 #include <svx/xfillit0.hxx>
24 #include <svx/xflclit.hxx>
25 #include <svx/xflgrit.hxx>
26 #include <svx/xflhtit.hxx>
27 #include <svx/xbtmpit.hxx>
28 #include <svx/itemwin.hxx>
29 #include <svx/SvxNumOptionsTabPageHelper.hxx>
30 #include "PageStylesPanel.hxx"
31 #include <sfx2/sidebar/Panel.hxx>
32 #include <sfx2/dispatch.hxx>
33 #include <sfx2/bindings.hxx>
34 #include <sfx2/objsh.hxx>
35 #include <cmdid.h>
37 using namespace ::com::sun::star;
39 namespace sw::sidebar{
41 namespace {
43 enum eFillStyle
45 NONE,
46 SOLID,
47 GRADIENT,
48 HATCH,
49 BITMAP,
50 PATTERN
55 const SvxPageUsage aArr[] =
57 SvxPageUsage::All,
58 SvxPageUsage::Mirror,
59 SvxPageUsage::Right,
60 SvxPageUsage::Left
64 static sal_uInt16 PageUsageToPos_Impl( SvxPageUsage nUsage )
66 for ( size_t i = 0; i < SAL_N_ELEMENTS(aArr); ++i )
67 if ( aArr[i] == nUsage )
68 return i;
69 return 3;
72 static SvxPageUsage PosToPageUsage_Impl( sal_uInt16 nPos )
74 if ( nPos >= SAL_N_ELEMENTS(aArr) )
75 return SvxPageUsage::NONE;
76 return aArr[nPos];
79 std::unique_ptr<PanelLayout> PageStylesPanel::Create(weld::Widget* pParent, SfxBindings* pBindings)
81 if( pParent == nullptr )
82 throw ::com::sun::star::lang::IllegalArgumentException("no parent window given to PageStylesPanel::Create", nullptr, 0);
83 if( pBindings == nullptr )
84 throw ::com::sun::star::lang::IllegalArgumentException("no SfxBindings given to PageStylesPanel::Create", nullptr, 0);
86 return std::make_unique<PageStylesPanel>(pParent, pBindings);
89 PageStylesPanel::PageStylesPanel(
90 weld::Widget* pParent,
91 SfxBindings* pBindings
92 ) :
93 PanelLayout(pParent, "PageStylesPanel", "modules/swriter/ui/pagestylespanel.ui"),
94 mpBindings( pBindings ),
95 mpPageColumnItem( new SfxInt16Item(SID_ATTR_PAGE_COLUMN) ),
96 mpPageItem( new SvxPageItem(SID_ATTR_PAGE) ),
97 maPageColumnControl(SID_ATTR_PAGE_COLUMN, *pBindings, *this),
98 maPageNumFormatControl( SID_ATTR_PAGE, *pBindings, *this ),
99 maBgColorControl( SID_ATTR_PAGE_COLOR, *pBindings, *this ),
100 maBgHatchingControl( SID_ATTR_PAGE_HATCH, *pBindings, *this ),
101 maBgGradientControl( SID_ATTR_PAGE_GRADIENT, *pBindings, *this ),
102 maBgBitmapControl( SID_ATTR_PAGE_BITMAP, *pBindings, *this ),
103 maBgFillStyleControl(SID_ATTR_PAGE_FILLSTYLE, *pBindings, *this),
104 mxBgColorLB(new ColorListBox(m_xBuilder->weld_menu_button("lbcolor"), [this]{ return GetFrameWeld(); })),
105 mxBgHatchingLB(m_xBuilder->weld_combo_box("lbhatching")),
106 mxBgGradientLB(new ColorListBox(m_xBuilder->weld_menu_button("lbgradient"), [this]{ return GetFrameWeld(); })),
107 mxBgBitmapLB(m_xBuilder->weld_combo_box("lbbitmap")),
108 mxLayoutSelectLB(m_xBuilder->weld_combo_box("layoutbox")),
109 mxColumnCount(m_xBuilder->weld_combo_box("columnbox")),
110 mxNumberSelectLB(new SvxPageNumberListBox(m_xBuilder->weld_combo_box("numberbox"))),
111 mxBgFillType(m_xBuilder->weld_combo_box("bgselect")),
112 mxCustomEntry(m_xBuilder->weld_label("customlabel"))
114 Initialize();
117 PageStylesPanel::~PageStylesPanel()
119 mxColumnCount.reset();
120 mxNumberSelectLB.reset();
121 mxBgFillType.reset();
122 mxBgColorLB.reset();
123 mxBgHatchingLB.reset();
124 mxBgGradientLB.reset();
125 mxBgBitmapLB.reset();
126 mxLayoutSelectLB.reset();
127 mxCustomEntry.reset();
129 maBgBitmapControl.dispose();
130 maBgColorControl.dispose();
131 maBgFillStyleControl.dispose();
132 maBgGradientControl.dispose();
133 maBgHatchingControl.dispose();
134 maPageColumnControl.dispose();
135 maPageNumFormatControl.dispose();
138 void PageStylesPanel::Initialize()
140 SvxFillTypeBox::Fill(*mxBgFillType);
142 m_aCustomEntry = mxCustomEntry->get_label();
143 mpBindings->Invalidate(SID_ATTR_PAGE_COLUMN);
144 mpBindings->Invalidate(SID_ATTR_PAGE);
145 mpBindings->Invalidate(SID_ATTR_PAGE_FILLSTYLE);
146 Update();
148 mxColumnCount->connect_changed( LINK(this, PageStylesPanel, ModifyColumnCountHdl) );
149 SvxNumOptionsTabPageHelper::GetI18nNumbering(mxNumberSelectLB->get_widget(), ::std::numeric_limits<sal_uInt16>::max());
150 mxNumberSelectLB->connect_changed( LINK(this, PageStylesPanel, ModifyNumberingHdl) );
151 mxLayoutSelectLB->connect_changed( LINK(this, PageStylesPanel, ModifyLayoutHdl) );
152 mxBgFillType->connect_changed( LINK(this, PageStylesPanel, ModifyFillStyleHdl));
153 mxBgColorLB->SetSelectHdl( LINK(this, PageStylesPanel, ModifyFillColorListHdl));
154 mxBgGradientLB->SetSelectHdl( LINK(this, PageStylesPanel, ModifyFillColorListHdl));
155 mxBgHatchingLB->connect_changed( LINK(this, PageStylesPanel, ModifyFillColorHdl));
156 mxBgBitmapLB->connect_changed( LINK(this, PageStylesPanel, ModifyFillColorHdl));
159 void PageStylesPanel::Update()
161 const eFillStyle eXFS = static_cast<eFillStyle>(mxBgFillType->get_active());
162 SfxObjectShell* pSh = SfxObjectShell::Current();
163 if (!pSh)
165 SAL_WARN("sw.ui", "PageStylesPanel::Update(): no SfxObjectShell found");
166 return;
168 switch(eXFS)
170 case NONE:
172 mxBgColorLB->hide();
173 mxBgHatchingLB->hide();
174 mxBgGradientLB->hide();
175 mxBgBitmapLB->hide();
177 break;
178 case SOLID:
180 mxBgBitmapLB->hide();
181 mxBgGradientLB->hide();
182 mxBgHatchingLB->hide();
183 mxBgColorLB->show();
184 const Color aColor = GetColorSetOrDefault();
185 mxBgColorLB->SelectEntry(aColor);
187 break;
188 case GRADIENT:
190 mxBgBitmapLB->hide();
191 mxBgHatchingLB->hide();
192 mxBgColorLB->show();
193 mxBgGradientLB->show();
195 const basegfx::BGradient aBGradient = GetGradientSetOrDefault();
196 const Color aStartColor(aBGradient.GetColorStops().front().getStopColor());
197 mxBgColorLB->SelectEntry(aStartColor);
198 const Color aEndColor(aBGradient.GetColorStops().back().getStopColor());
199 mxBgGradientLB->SelectEntry(aEndColor);
201 break;
203 case HATCH:
205 mxBgColorLB->hide();
206 mxBgGradientLB->hide();
207 mxBgBitmapLB->hide();
208 mxBgHatchingLB->show();
209 mxBgHatchingLB->clear();
210 SvxFillAttrBox::Fill(*mxBgHatchingLB, pSh->GetItem(SID_HATCH_LIST)->GetHatchList());
212 const OUString aHatchName = GetHatchingSetOrDefault();
213 mxBgHatchingLB->set_active_text( aHatchName );
215 break;
217 case BITMAP:
218 case PATTERN:
220 mxBgColorLB->hide();
221 mxBgGradientLB->hide();
222 mxBgHatchingLB->hide();
223 mxBgBitmapLB->show();
224 mxBgBitmapLB->clear();
225 OUString aBitmapName;
227 if( eXFS == BITMAP )
229 SvxFillAttrBox::Fill(*mxBgBitmapLB, pSh->GetItem(SID_BITMAP_LIST)->GetBitmapList());
230 aBitmapName = GetBitmapSetOrDefault();
232 else
234 SvxFillAttrBox::Fill(*mxBgBitmapLB, pSh->GetItem(SID_PATTERN_LIST)->GetPatternList());
235 aBitmapName = GetPatternSetOrDefault();
238 mxBgBitmapLB->set_active_text( aBitmapName );
240 break;
242 default:
243 break;
246 // Need to do a relayouting, otherwise the panel size is not updated after show / hide controls
247 if (m_pPanel)
248 m_pPanel->TriggerDeckLayouting();
251 Color const & PageStylesPanel::GetColorSetOrDefault()
253 if ( !mpBgColorItem )
254 mpBgColorItem.reset( new XFillColorItem( OUString(), Color(0x72, 0x9f, 0xcf) ) );
256 return mpBgColorItem->GetColorValue();
259 basegfx::BGradient const & PageStylesPanel::GetGradientSetOrDefault()
261 if( !mpBgGradientItem )
263 basegfx::BGradient aGradient;
264 OUString aGradientName;
265 if (SfxObjectShell* pSh = SfxObjectShell::Current())
267 const SvxGradientListItem* pGradListItem = pSh->GetItem(SID_GRADIENT_LIST);
268 aGradient = pGradListItem->GetGradientList()->GetGradient(0)->GetGradient();
269 aGradientName = pGradListItem->GetGradientList()->GetGradient(0)->GetName();
271 mpBgGradientItem.reset( new XFillGradientItem( aGradientName, aGradient ) );
274 return mpBgGradientItem->GetGradientValue();
277 OUString const & PageStylesPanel::GetHatchingSetOrDefault()
279 if( !mpBgHatchItem )
281 XHatch aHatch;
282 OUString aHatchName;
283 if (SfxObjectShell* pSh = SfxObjectShell::Current())
285 const SvxHatchListItem * pHatchListItem = pSh->GetItem(SID_HATCH_LIST);
286 aHatch = pHatchListItem->GetHatchList()->GetHatch(0)->GetHatch();
287 aHatchName = pHatchListItem->GetHatchList()->GetHatch(0)->GetName();
289 mpBgHatchItem.reset( new XFillHatchItem( aHatchName, aHatch ) );
292 return mpBgHatchItem->GetName();
295 OUString const & PageStylesPanel::GetBitmapSetOrDefault()
297 if( !mpBgBitmapItem || mpBgBitmapItem->isPattern() )
299 GraphicObject aGraphObj;
300 OUString aBmpName;
301 if (SfxObjectShell* pSh = SfxObjectShell::Current())
303 const SvxBitmapListItem * pBmpListItem = pSh->GetItem(SID_BITMAP_LIST);
304 aGraphObj = pBmpListItem->GetBitmapList()->GetBitmap(0)->GetGraphicObject();
305 aBmpName = pBmpListItem->GetBitmapList()->GetBitmap(0)->GetName();
307 mpBgBitmapItem.reset( new XFillBitmapItem( aBmpName, aGraphObj ) );
310 return mpBgBitmapItem->GetName();
313 OUString const & PageStylesPanel::GetPatternSetOrDefault()
315 if( !mpBgBitmapItem || !mpBgBitmapItem->isPattern() )
317 GraphicObject aGraphObj;
318 OUString aPatternName;
319 if (SfxObjectShell* pSh = SfxObjectShell::Current())
321 const SvxPatternListItem * pPatternListItem = pSh->GetItem(SID_PATTERN_LIST);
322 aGraphObj = pPatternListItem->GetPatternList()->GetBitmap(0)->GetGraphicObject();
323 aPatternName = pPatternListItem->GetPatternList()->GetBitmap(0)->GetName();
325 mpBgBitmapItem.reset( new XFillBitmapItem( aPatternName, aGraphObj ) );
328 return mpBgBitmapItem->GetName();
331 void PageStylesPanel::NotifyItemUpdate(
332 const sal_uInt16 nSid,
333 const SfxItemState eState,
334 const SfxPoolItem* pState)
336 if (!mxColumnCount) //disposed
337 return;
339 switch(nSid)
341 case SID_ATTR_PAGE_COLUMN:
343 if ( eState >= SfxItemState::DEFAULT &&
344 dynamic_cast< const SfxInt16Item *>( pState ) )
346 mpPageColumnItem.reset( static_cast<SfxInt16Item*>(pState->Clone()) );
347 if(mpPageColumnItem->GetValue() <= 5)
349 mxColumnCount->set_active(mpPageColumnItem->GetValue() - 1);
350 int nIndex = mxColumnCount->find_text(m_aCustomEntry);
351 if (nIndex != -1)
352 mxColumnCount->remove(nIndex);
354 else
356 if (mxColumnCount->find_text(m_aCustomEntry) == -1)
357 mxColumnCount->append_text(m_aCustomEntry);
358 mxColumnCount->set_active_text(m_aCustomEntry);
362 break;
364 case SID_ATTR_PAGE:
366 if( eState >= SfxItemState::DEFAULT &&
367 dynamic_cast< const SvxPageItem*>( pState ) )
369 mpPageItem.reset( static_cast<SvxPageItem*>(pState->Clone()) );
370 SvxNumType eNumType = mpPageItem->GetNumType();
371 mxNumberSelectLB->set_active_id(eNumType);
373 SvxPageUsage nUse = mpPageItem->GetPageUsage();
374 mxLayoutSelectLB->set_active( PageUsageToPos_Impl( nUse ) );
377 break;
379 case SID_ATTR_PAGE_COLOR:
381 if(eState >= SfxItemState::DEFAULT)
383 mxBgFillType->set_active( static_cast<sal_Int32>(SOLID) );
384 mpBgColorItem.reset(pState ? static_cast< XFillColorItem* >(pState->Clone()) : nullptr);
385 Update();
388 break;
390 case SID_ATTR_PAGE_HATCH:
392 if(eState >= SfxItemState::DEFAULT)
394 mxBgFillType->set_active( static_cast<sal_Int32>(HATCH) );
395 mpBgHatchItem.reset(pState ? static_cast < XFillHatchItem* >(pState->Clone()) : nullptr);
396 Update();
399 break;
401 case SID_ATTR_PAGE_GRADIENT:
403 if(eState >= SfxItemState::DEFAULT)
405 mxBgFillType->set_active( static_cast<sal_Int32>(GRADIENT) );
406 mpBgGradientItem.reset(pState ? static_cast< XFillGradientItem* >(pState->Clone()) : nullptr);
407 Update();
410 break;
411 case SID_ATTR_PAGE_BITMAP:
413 if(eState >= SfxItemState::DEFAULT)
415 mpBgBitmapItem.reset(pState ? static_cast< XFillBitmapItem* >(pState->Clone()) : nullptr);
416 if (mpBgBitmapItem)
418 if (mpBgBitmapItem->isPattern())
419 mxBgFillType->set_active( static_cast<sal_Int32>(PATTERN) );
420 else
421 mxBgFillType->set_active( static_cast<sal_Int32>(BITMAP) );
423 Update();
426 break;
428 case SID_ATTR_PAGE_FILLSTYLE:
430 const XFillStyleItem* pFillStyleItem = nullptr;
431 if (eState >= SfxItemState::DEFAULT)
432 pFillStyleItem = dynamic_cast< const XFillStyleItem* >(pState);
433 if (pFillStyleItem)
435 css::drawing::FillStyle eXFS = pFillStyleItem->GetValue();
436 switch(eXFS)
438 case drawing::FillStyle_NONE:
439 mxBgFillType->set_active( static_cast<sal_Int32>(NONE) );
440 break;
441 case drawing::FillStyle_SOLID:
442 mxBgFillType->set_active( static_cast<sal_Int32>(SOLID) );
443 break;
444 case drawing::FillStyle_GRADIENT:
445 mxBgFillType->set_active( static_cast<sal_Int32>(GRADIENT) );
446 break;
447 case drawing::FillStyle_HATCH:
448 mxBgFillType->set_active( static_cast<sal_Int32>(HATCH) );
449 break;
450 case drawing::FillStyle_BITMAP:
451 if (mpBgBitmapItem->isPattern())
452 mxBgFillType->set_active( static_cast<sal_Int32>(PATTERN) );
453 else
454 mxBgFillType->set_active( static_cast<sal_Int32>(BITMAP) );
455 break;
456 default:
457 break;
459 Update();
462 break;
464 default:
465 break;
469 IMPL_LINK_NOARG( PageStylesPanel, ModifyColumnCountHdl, weld::ComboBox&, void )
471 sal_uInt16 nColumnType = mxColumnCount->get_active() + 1;
472 mpPageColumnItem->SetValue( nColumnType );
473 mpBindings->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_COLUMN,
474 SfxCallMode::RECORD, { mpPageColumnItem.get() });
477 IMPL_LINK_NOARG( PageStylesPanel, ModifyNumberingHdl, weld::ComboBox&, void )
479 SvxNumType nEntryData = mxNumberSelectLB->get_active_id();
480 mpPageItem->SetNumType(nEntryData);
481 mpBindings->GetDispatcher()->ExecuteList(SID_ATTR_PAGE, SfxCallMode::RECORD, { mpPageItem.get() });
484 IMPL_LINK_NOARG( PageStylesPanel, ModifyLayoutHdl, weld::ComboBox&, void )
486 sal_uInt16 nUse = mxLayoutSelectLB->get_active();
487 mpPageItem->SetPageUsage(PosToPageUsage_Impl(nUse));
488 mpBindings->GetDispatcher()->ExecuteList(SID_ATTR_PAGE, SfxCallMode::RECORD, { mpPageItem.get() });
491 IMPL_LINK_NOARG(PageStylesPanel, ModifyFillStyleHdl, weld::ComboBox&, void)
493 const eFillStyle eXFS = static_cast<eFillStyle>(mxBgFillType->get_active());
494 Update();
496 switch (eXFS)
498 case NONE:
500 const XFillStyleItem aXFillStyleItem(drawing::FillStyle_NONE);
501 GetBindings()->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_FILLSTYLE, SfxCallMode::RECORD, { &aXFillStyleItem });
503 break;
505 case SOLID:
507 XFillColorItem aItem( OUString(), mpBgColorItem->GetColorValue() );
508 GetBindings()->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_COLOR, SfxCallMode::RECORD, { &aItem });
510 break;
512 case GRADIENT:
514 XFillGradientItem aItem( mpBgGradientItem->GetName(), mpBgGradientItem->GetGradientValue() );
515 GetBindings()->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_GRADIENT, SfxCallMode::RECORD, { &aItem });
517 break;
519 case HATCH:
521 XFillHatchItem aItem( mpBgHatchItem->GetName(), mpBgHatchItem->GetHatchValue() );
522 GetBindings()->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_HATCH, SfxCallMode::RECORD, { &aItem });
524 break;
526 case BITMAP:
527 case PATTERN:
529 XFillBitmapItem aItem( mpBgBitmapItem->GetName(), mpBgBitmapItem->GetGraphicObject() );
530 GetBindings()->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_BITMAP, SfxCallMode::RECORD, { &aItem });
532 break;
534 default:
535 break;
539 void PageStylesPanel::ModifyFillColor()
541 const eFillStyle eXFS = static_cast<eFillStyle>(mxBgFillType->get_active());
542 SfxObjectShell* pSh = SfxObjectShell::Current();
543 switch(eXFS)
545 case SOLID:
547 auto aNamedColor = mxBgColorLB->GetSelectedEntry();
548 XFillColorItem aItem(OUString(), aNamedColor.m_aColor);
549 aItem.setComplexColor(aNamedColor.getComplexColor());
550 aItem.setComplexColor(mxBgColorLB->GetSelectedEntry().getComplexColor());
551 GetBindings()->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_COLOR, SfxCallMode::RECORD, { &aItem });
553 break;
554 case GRADIENT:
556 basegfx::BGradient aGradient(
557 basegfx::BColorStops(
558 mxBgColorLB->GetSelectEntryColor().getBColor(),
559 mxBgGradientLB->GetSelectEntryColor().getBColor()));
561 XFillGradientItem aItem(aGradient);
562 GetBindings()->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_GRADIENT, SfxCallMode::RECORD, { &aItem });
564 break;
565 case HATCH:
566 if (pSh)
568 const SvxHatchListItem * pHatchListItem = pSh->GetItem(SID_HATCH_LIST);
569 sal_uInt16 nPos = mxBgHatchingLB->get_active();
570 XHatch aHatch = pHatchListItem->GetHatchList()->GetHatch(nPos)->GetHatch();
571 const OUString aHatchName = pHatchListItem->GetHatchList()->GetHatch(nPos)->GetName();
573 XFillHatchItem aItem(aHatchName, aHatch);
574 GetBindings()->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_HATCH, SfxCallMode::RECORD, { &aItem });
576 break;
577 case BITMAP:
578 case PATTERN:
579 if (pSh)
581 sal_Int16 nPos = mxBgBitmapLB->get_active();
582 GraphicObject aBitmap;
583 OUString aBitmapName;
585 if ( eXFS == BITMAP )
587 SvxBitmapListItem const * pBitmapListItem = pSh->GetItem(SID_BITMAP_LIST);
588 aBitmap = pBitmapListItem->GetBitmapList()->GetBitmap(nPos)->GetGraphicObject();
589 aBitmapName = pBitmapListItem->GetBitmapList()->GetBitmap(nPos)->GetName();
591 else
593 SvxPatternListItem const * pPatternListItem = pSh->GetItem(SID_PATTERN_LIST);
594 aBitmap = pPatternListItem->GetPatternList()->GetBitmap(nPos)->GetGraphicObject();
595 aBitmapName = pPatternListItem->GetPatternList()->GetBitmap(nPos)->GetName();
598 XFillBitmapItem aItem(aBitmapName, aBitmap);
599 GetBindings()->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_BITMAP, SfxCallMode::RECORD, { &aItem });
601 break;
602 default:
603 break;
607 IMPL_LINK_NOARG(PageStylesPanel, ModifyFillColorHdl, weld::ComboBox&, void)
609 ModifyFillColor();
612 IMPL_LINK_NOARG(PageStylesPanel, ModifyFillColorListHdl, ColorListBox&, void)
614 ModifyFillColor();
619 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */