1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
37 using namespace ::com::sun::star
;
39 namespace sw::sidebar
{
55 const SvxPageUsage aArr
[] =
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
)
72 static SvxPageUsage
PosToPageUsage_Impl( sal_uInt16 nPos
)
74 if ( nPos
>= SAL_N_ELEMENTS(aArr
) )
75 return SvxPageUsage::NONE
;
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
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"))
117 PageStylesPanel::~PageStylesPanel()
119 mxColumnCount
.reset();
120 mxNumberSelectLB
.reset();
121 mxBgFillType
.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
);
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();
165 SAL_WARN("sw.ui", "PageStylesPanel::Update(): no SfxObjectShell found");
173 mxBgHatchingLB
->hide();
174 mxBgGradientLB
->hide();
175 mxBgBitmapLB
->hide();
180 mxBgBitmapLB
->hide();
181 mxBgGradientLB
->hide();
182 mxBgHatchingLB
->hide();
184 const Color aColor
= GetColorSetOrDefault();
185 mxBgColorLB
->SelectEntry(aColor
);
190 mxBgBitmapLB
->hide();
191 mxBgHatchingLB
->hide();
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
);
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
);
221 mxBgGradientLB
->hide();
222 mxBgHatchingLB
->hide();
223 mxBgBitmapLB
->show();
224 mxBgBitmapLB
->clear();
225 OUString aBitmapName
;
229 SvxFillAttrBox::Fill(*mxBgBitmapLB
, pSh
->GetItem(SID_BITMAP_LIST
)->GetBitmapList());
230 aBitmapName
= GetBitmapSetOrDefault();
234 SvxFillAttrBox::Fill(*mxBgBitmapLB
, pSh
->GetItem(SID_PATTERN_LIST
)->GetPatternList());
235 aBitmapName
= GetPatternSetOrDefault();
238 mxBgBitmapLB
->set_active_text( aBitmapName
);
246 // Need to do a relayouting, otherwise the panel size is not updated after show / hide controls
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()
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
;
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
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
);
352 mxColumnCount
->remove(nIndex
);
356 if (mxColumnCount
->find_text(m_aCustomEntry
) == -1)
357 mxColumnCount
->append_text(m_aCustomEntry
);
358 mxColumnCount
->set_active_text(m_aCustomEntry
);
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
) );
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);
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);
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);
411 case SID_ATTR_PAGE_BITMAP
:
413 if(eState
>= SfxItemState::DEFAULT
)
415 mpBgBitmapItem
.reset(pState
? static_cast< XFillBitmapItem
* >(pState
->Clone()) : nullptr);
418 if (mpBgBitmapItem
->isPattern())
419 mxBgFillType
->set_active( static_cast<sal_Int32
>(PATTERN
) );
421 mxBgFillType
->set_active( static_cast<sal_Int32
>(BITMAP
) );
428 case SID_ATTR_PAGE_FILLSTYLE
:
430 const XFillStyleItem
* pFillStyleItem
= nullptr;
431 if (eState
>= SfxItemState::DEFAULT
)
432 pFillStyleItem
= dynamic_cast< const XFillStyleItem
* >(pState
);
435 css::drawing::FillStyle eXFS
= pFillStyleItem
->GetValue();
438 case drawing::FillStyle_NONE
:
439 mxBgFillType
->set_active( static_cast<sal_Int32
>(NONE
) );
441 case drawing::FillStyle_SOLID
:
442 mxBgFillType
->set_active( static_cast<sal_Int32
>(SOLID
) );
444 case drawing::FillStyle_GRADIENT
:
445 mxBgFillType
->set_active( static_cast<sal_Int32
>(GRADIENT
) );
447 case drawing::FillStyle_HATCH
:
448 mxBgFillType
->set_active( static_cast<sal_Int32
>(HATCH
) );
450 case drawing::FillStyle_BITMAP
:
451 if (mpBgBitmapItem
->isPattern())
452 mxBgFillType
->set_active( static_cast<sal_Int32
>(PATTERN
) );
454 mxBgFillType
->set_active( static_cast<sal_Int32
>(BITMAP
) );
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());
500 const XFillStyleItem
aXFillStyleItem(drawing::FillStyle_NONE
);
501 GetBindings()->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_FILLSTYLE
, SfxCallMode::RECORD
, { &aXFillStyleItem
});
507 XFillColorItem
aItem( OUString(), mpBgColorItem
->GetColorValue() );
508 GetBindings()->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_COLOR
, SfxCallMode::RECORD
, { &aItem
});
514 XFillGradientItem
aItem( mpBgGradientItem
->GetName(), mpBgGradientItem
->GetGradientValue() );
515 GetBindings()->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_GRADIENT
, SfxCallMode::RECORD
, { &aItem
});
521 XFillHatchItem
aItem( mpBgHatchItem
->GetName(), mpBgHatchItem
->GetHatchValue() );
522 GetBindings()->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_HATCH
, SfxCallMode::RECORD
, { &aItem
});
529 XFillBitmapItem
aItem( mpBgBitmapItem
->GetName(), mpBgBitmapItem
->GetGraphicObject() );
530 GetBindings()->GetDispatcher()->ExecuteList(SID_ATTR_PAGE_BITMAP
, SfxCallMode::RECORD
, { &aItem
});
539 void PageStylesPanel::ModifyFillColor()
541 const eFillStyle eXFS
= static_cast<eFillStyle
>(mxBgFillType
->get_active());
542 SfxObjectShell
* pSh
= SfxObjectShell::Current();
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
});
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
});
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
});
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();
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
});
607 IMPL_LINK_NOARG(PageStylesPanel
, ModifyFillColorHdl
, weld::ComboBox
&, void)
612 IMPL_LINK_NOARG(PageStylesPanel
, ModifyFillColorListHdl
, ColorListBox
&, void)
619 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */