1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 #include <sal/config.h>
20 #include <svl/intitem.hxx>
21 #include <svl/eitem.hxx>
22 #include <svx/dlgutil.hxx>
23 #include <svx/rulritem.hxx>
24 #include <svx/svdtrans.hxx>
25 #include <svx/spacinglistbox.hxx>
26 #include <svx/samecontentlistbox.hxx>
27 #include "PageHeaderPanel.hxx"
28 #include <sfx2/dispatch.hxx>
29 #include <sfx2/bindings.hxx>
30 #include <cmdid.h>
31 
32 #include <com/sun/star/lang/IllegalArgumentException.hpp>
33 
34 namespace sw::sidebar{
35 
Create(weld::Widget * pParent,SfxBindings * pBindings)36 std::unique_ptr<PanelLayout> PageHeaderPanel::Create(
37     weld::Widget* pParent,
38     SfxBindings* pBindings)
39 {
40     if( pParent == nullptr )
41         throw ::com::sun::star::lang::IllegalArgumentException("no parent window given to PageHeaderPanel::Create", nullptr, 0);
42     if( pBindings == nullptr )
43         throw ::com::sun::star::lang::IllegalArgumentException("no SfxBindings given to PageHeaderPanel::Create", nullptr, 0);
44 
45     return std::make_unique<PageHeaderPanel>(pParent, pBindings);
46 }
47 
SetMarginsAndSpacingFieldUnit()48 void PageHeaderPanel::SetMarginsAndSpacingFieldUnit()
49 {
50     SpacingListBox::Fill(IsInch(meFUnit) ? SpacingType::SPACING_INCH : SpacingType::SPACING_CM, *mxHeaderSpacingLB);
51     SpacingListBox::Fill(IsInch(meFUnit) ? SpacingType::MARGINS_INCH : SpacingType::MARGINS_CM, *mxHeaderMarginPresetLB);
52 }
53 
PageHeaderPanel(weld::Widget * pParent,SfxBindings * pBindings)54 PageHeaderPanel::PageHeaderPanel(
55     weld::Widget* pParent,
56     SfxBindings* pBindings
57     ) :
58     PanelLayout(pParent, "PageHeaderPanel", "modules/swriter/ui/pageheaderpanel.ui"),
59     mpBindings( pBindings ),
60     maHFToggleController(SID_ATTR_PAGE_HEADER, *pBindings, *this),
61     maMetricController(SID_ATTR_METRIC, *pBindings,*this),
62     maHeaderLRMarginController(SID_ATTR_PAGE_HEADER_LRMARGIN, *pBindings, *this),
63     maHeaderSpacingController(SID_ATTR_PAGE_HEADER_SPACING, *pBindings, *this),
64     maHeaderLayoutController(SID_ATTR_PAGE_HEADER_LAYOUT, *pBindings, *this),
65     meFUnit(GetModuleFieldUnit()),
66     aCustomEntry(),
67     mpHeaderItem( new SfxBoolItem(SID_ATTR_PAGE_HEADER) ),
68     mpHeaderLRMarginItem( new SvxLongLRSpaceItem(0, 0, SID_ATTR_PAGE_HEADER_LRMARGIN)),
69     mpHeaderSpacingItem( new SvxLongULSpaceItem(0, 0, SID_ATTR_PAGE_HEADER_SPACING)),
70     mpHeaderLayoutItem( new SfxInt16Item(SID_ATTR_PAGE_HEADER_LAYOUT)),
71     mxHeaderToggle(m_xBuilder->weld_check_button("headertoggle")),
72     mxHeaderSpacingLB(m_xBuilder->weld_combo_box("spacingpreset")),
73     mxHeaderMarginPresetLB(m_xBuilder->weld_combo_box("headermarginpreset")),
74     mxHeaderLayoutLB(m_xBuilder->weld_combo_box("samecontentLB")),
75     mxCustomEntry(m_xBuilder->weld_label("customlabel"))
76 {
77     Initialize();
78 }
79 
~PageHeaderPanel()80 PageHeaderPanel::~PageHeaderPanel()
81 {
82     mxHeaderToggle.reset();
83     mxHeaderSpacingLB.reset();
84     mxHeaderLayoutLB.reset();
85     mxHeaderMarginPresetLB.reset();
86     mxCustomEntry.reset();
87 }
88 
GetCurrentUnit(SfxItemState eState,const SfxPoolItem * pState)89 FieldUnit PageHeaderPanel::GetCurrentUnit(SfxItemState eState, const SfxPoolItem* pState)
90 {
91     FieldUnit eUnit;
92 
93     if (pState && eState >= SfxItemState::DEFAULT)
94         eUnit = static_cast<FieldUnit>(static_cast<const SfxUInt16Item*>(pState)->GetValue());
95     else
96         eUnit = GetModuleFieldUnit();
97 
98     return eUnit;
99 }
100 
Initialize()101 void PageHeaderPanel::Initialize()
102 {
103     SameContentListBox::Fill(*mxHeaderLayoutLB);
104 
105     SetMarginsAndSpacingFieldUnit();
106 
107     aCustomEntry = mxCustomEntry->get_label();
108     mxHeaderToggle->connect_toggled( LINK(this, PageHeaderPanel, HeaderToggleHdl) );
109     mxHeaderMarginPresetLB->connect_changed( LINK(this, PageHeaderPanel, HeaderLRMarginHdl));
110     mxHeaderSpacingLB->connect_changed( LINK(this, PageHeaderPanel, HeaderSpacingHdl));
111     mxHeaderLayoutLB->connect_changed( LINK(this, PageHeaderPanel, HeaderLayoutHdl));
112 
113     mpBindings->Invalidate(SID_ATTR_METRIC);
114     mpBindings->Invalidate(SID_ATTR_PAGE_HEADER);
115     mpBindings->Invalidate(SID_ATTR_PAGE_HEADER_LRMARGIN);
116     mpBindings->Invalidate(SID_ATTR_PAGE_HEADER_SPACING);
117     mpBindings->Invalidate(SID_ATTR_PAGE_HEADER_LAYOUT);
118 }
119 
UpdateHeaderCheck()120 void PageHeaderPanel::UpdateHeaderCheck()
121 {
122     if (mxHeaderToggle->get_active())
123     {
124         mxHeaderSpacingLB->set_sensitive(true);
125         mxHeaderLayoutLB->set_sensitive(true);
126         mxHeaderMarginPresetLB->set_sensitive(true);
127     }
128     else
129     {
130         mxHeaderSpacingLB->set_sensitive(false);
131         mxHeaderLayoutLB->set_sensitive(false);
132         mxHeaderMarginPresetLB->set_sensitive(false);
133     }
134 }
135 
UpdateMarginControl()136 void PageHeaderPanel::UpdateMarginControl()
137 {
138     sal_uInt16 nLeft = mpHeaderLRMarginItem->GetLeft();
139     sal_uInt16 nRight = mpHeaderLRMarginItem->GetRight();
140     sal_uInt16 nCount = mxHeaderMarginPresetLB->get_count();
141     if(nLeft == nRight)
142     {
143         for (sal_uInt16 i = 0; i < nCount; ++i)
144         {
145             if (mxHeaderMarginPresetLB->get_id(i).toUInt32() == nLeft)
146             {
147                 mxHeaderMarginPresetLB->set_active(i);
148                 int nCustomEntry = mxHeaderMarginPresetLB->find_text(aCustomEntry);
149                 if (nCustomEntry != -1)
150                     mxHeaderMarginPresetLB->remove(nCustomEntry);
151                 return;
152             }
153         }
154     }
155     mxHeaderMarginPresetLB->append_text(aCustomEntry);
156     mxHeaderMarginPresetLB->set_active_text(aCustomEntry);
157 }
158 
UpdateSpacingControl()159 void PageHeaderPanel::UpdateSpacingControl()
160 {
161     sal_uInt16 nBottom = mpHeaderSpacingItem->GetLower();
162     sal_uInt16 nCount = mxHeaderSpacingLB->get_count();
163     for (sal_uInt16 i = 0; i < nCount; ++i)
164     {
165         if (mxHeaderSpacingLB->get_id(i).toUInt32() == nBottom)
166         {
167             mxHeaderSpacingLB->set_active(i);
168             int nCustomEntry = mxHeaderSpacingLB->find_text(aCustomEntry);
169             if (nCustomEntry != -1)
170                 mxHeaderSpacingLB->remove(nCustomEntry);
171             return;
172         }
173     }
174     mxHeaderSpacingLB->append_text(aCustomEntry);
175     mxHeaderSpacingLB->set_active_text(aCustomEntry);
176 }
177 
UpdateLayoutControl()178 void PageHeaderPanel::UpdateLayoutControl()
179 {
180     sal_uInt16 nLayout = mpHeaderLayoutItem->GetValue();
181     mxHeaderLayoutLB->set_active(nLayout);
182 }
183 
NotifyItemUpdate(const sal_uInt16 nSid,const SfxItemState eState,const SfxPoolItem * pState)184 void PageHeaderPanel::NotifyItemUpdate(
185     const sal_uInt16 nSid,
186     const SfxItemState eState,
187     const SfxPoolItem* pState)
188 {
189     if (!mxHeaderToggle) //disposed
190         return;
191 
192     switch(nSid)
193     {
194         case SID_ATTR_PAGE_HEADER:
195         {
196             if(eState >= SfxItemState::DEFAULT &&
197                 dynamic_cast<const SfxBoolItem*>( pState) )
198             {
199                 mpHeaderItem.reset( static_cast<SfxBoolItem*>(pState->Clone()) );
200                 mxHeaderToggle->set_active(mpHeaderItem->GetValue());
201                 UpdateHeaderCheck();
202             }
203         }
204         break;
205         case SID_ATTR_PAGE_HEADER_LRMARGIN:
206         {
207             if(eState >= SfxItemState::DEFAULT &&
208                 dynamic_cast<const SvxLongLRSpaceItem*>( pState) )
209             {
210                 mpHeaderLRMarginItem.reset( static_cast<SvxLongLRSpaceItem*>(pState->Clone()) );
211                 UpdateMarginControl();
212             }
213         }
214         break;
215         case SID_ATTR_PAGE_HEADER_SPACING:
216         {
217             if(eState >= SfxItemState::DEFAULT &&
218                 dynamic_cast<const SvxLongULSpaceItem*>( pState) )
219             {
220                 mpHeaderSpacingItem.reset(static_cast<SvxLongULSpaceItem*>(pState->Clone()) );
221                 UpdateSpacingControl();
222             }
223         }
224         break;
225         case SID_ATTR_PAGE_HEADER_LAYOUT:
226         {
227             if(eState >= SfxItemState::DEFAULT &&
228                 dynamic_cast<const SfxInt16Item*>( pState) )
229             {
230                 mpHeaderLayoutItem.reset(static_cast<SfxInt16Item*>(pState->Clone()) );
231                 UpdateLayoutControl();
232             }
233         }
234         break;
235         case SID_ATTR_METRIC:
236         {
237             FieldUnit eFUnit = GetCurrentUnit(eState, pState);
238             if (meFUnit != eFUnit)
239             {
240                 meFUnit = eFUnit;
241                 SetMarginsAndSpacingFieldUnit();
242                 UpdateSpacingControl();
243                 UpdateMarginControl();
244             }
245         }
246         break;
247         default:
248             break;
249     }
250 }
251 
IMPL_LINK_NOARG(PageHeaderPanel,HeaderToggleHdl,weld::Toggleable &,void)252 IMPL_LINK_NOARG( PageHeaderPanel, HeaderToggleHdl, weld::Toggleable&, void )
253 {
254     bool IsChecked = mxHeaderToggle->get_active();
255     mpHeaderItem->SetValue(IsChecked);
256     GetBindings()->GetDispatcher()->ExecuteList( SID_ATTR_PAGE_HEADER, SfxCallMode::RECORD, { mpHeaderItem.get() } );
257     UpdateHeaderCheck();
258 }
259 
IMPL_LINK_NOARG(PageHeaderPanel,HeaderLRMarginHdl,weld::ComboBox &,void)260 IMPL_LINK_NOARG( PageHeaderPanel, HeaderLRMarginHdl, weld::ComboBox&, void )
261 {
262     sal_uInt16 nVal = mxHeaderMarginPresetLB->get_active_id().toUInt32();
263     mpHeaderLRMarginItem->SetLeft(nVal);
264     mpHeaderLRMarginItem->SetRight(nVal);
265     GetBindings()->GetDispatcher()->ExecuteList( SID_ATTR_PAGE_HEADER_LRMARGIN,
266                                                  SfxCallMode::RECORD, { mpHeaderLRMarginItem.get() } );
267 }
268 
IMPL_LINK_NOARG(PageHeaderPanel,HeaderSpacingHdl,weld::ComboBox &,void)269 IMPL_LINK_NOARG( PageHeaderPanel, HeaderSpacingHdl, weld::ComboBox&, void )
270 {
271     sal_uInt16 nVal = mxHeaderSpacingLB->get_active_id().toUInt32();
272     mpHeaderSpacingItem->SetLower(nVal);
273     GetBindings()->GetDispatcher()->ExecuteList( SID_ATTR_PAGE_HEADER_SPACING,
274                                                  SfxCallMode::RECORD, { mpHeaderSpacingItem.get() } );
275 }
IMPL_LINK_NOARG(PageHeaderPanel,HeaderLayoutHdl,weld::ComboBox &,void)276 IMPL_LINK_NOARG( PageHeaderPanel, HeaderLayoutHdl, weld::ComboBox&, void )
277 {
278     sal_uInt16 nVal = mxHeaderLayoutLB->get_active();
279     mpHeaderLayoutItem->SetValue(nVal);
280     GetBindings()->GetDispatcher()->ExecuteList( SID_ATTR_PAGE_HEADER_LAYOUT,
281                                                  SfxCallMode::RECORD, { mpHeaderLayoutItem.get() } );
282 }
283 
284 
285 }
286 
287 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
288