Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sw / source / uibase / sidebar / PageHeaderPanel.cxx
blobd799247adbc107fa7bb6fce9e727096d850c0a76
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 <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>
32 #include <com/sun/star/lang/IllegalArgumentException.hpp>
34 namespace sw::sidebar{
36 std::unique_ptr<PanelLayout> PageHeaderPanel::Create(
37 weld::Widget* pParent,
38 SfxBindings* pBindings)
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);
45 return std::make_unique<PageHeaderPanel>(pParent, pBindings);
48 void PageHeaderPanel::SetMarginsAndSpacingFieldUnit()
50 SpacingListBox::Fill(IsInch(meFUnit) ? SpacingType::SPACING_INCH : SpacingType::SPACING_CM, *mxHeaderSpacingLB);
51 SpacingListBox::Fill(IsInch(meFUnit) ? SpacingType::MARGINS_INCH : SpacingType::MARGINS_CM, *mxHeaderMarginPresetLB);
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 mpHeaderItem( new SfxBoolItem(SID_ATTR_PAGE_HEADER) ),
67 mpHeaderLRMarginItem( new SvxLongLRSpaceItem(0, 0, SID_ATTR_PAGE_HEADER_LRMARGIN)),
68 mpHeaderSpacingItem( new SvxLongULSpaceItem(0, 0, SID_ATTR_PAGE_HEADER_SPACING)),
69 mpHeaderLayoutItem( new SfxInt16Item(SID_ATTR_PAGE_HEADER_LAYOUT)),
70 mxHeaderToggle(m_xBuilder->weld_check_button("headertoggle")),
71 mxHeaderSpacingLB(m_xBuilder->weld_combo_box("spacingpreset")),
72 mxHeaderMarginPresetLB(m_xBuilder->weld_combo_box("headermarginpreset")),
73 mxHeaderLayoutLB(m_xBuilder->weld_combo_box("samecontentLB")),
74 mxCustomEntry(m_xBuilder->weld_label("customlabel"))
76 Initialize();
79 PageHeaderPanel::~PageHeaderPanel()
81 mxHeaderToggle.reset();
82 mxHeaderSpacingLB.reset();
83 mxHeaderLayoutLB.reset();
84 mxHeaderMarginPresetLB.reset();
85 mxCustomEntry.reset();
88 FieldUnit PageHeaderPanel::GetCurrentUnit(SfxItemState eState, const SfxPoolItem* pState)
90 FieldUnit eUnit;
92 if (pState && eState >= SfxItemState::DEFAULT)
93 eUnit = static_cast<FieldUnit>(static_cast<const SfxUInt16Item*>(pState)->GetValue());
94 else
95 eUnit = GetModuleFieldUnit();
97 return eUnit;
100 void PageHeaderPanel::Initialize()
102 SameContentListBox::Fill(*mxHeaderLayoutLB);
104 SetMarginsAndSpacingFieldUnit();
106 m_aCustomEntry = mxCustomEntry->get_label();
107 mxHeaderToggle->connect_toggled( LINK(this, PageHeaderPanel, HeaderToggleHdl) );
108 mxHeaderMarginPresetLB->connect_changed( LINK(this, PageHeaderPanel, HeaderLRMarginHdl));
109 mxHeaderSpacingLB->connect_changed( LINK(this, PageHeaderPanel, HeaderSpacingHdl));
110 mxHeaderLayoutLB->connect_changed( LINK(this, PageHeaderPanel, HeaderLayoutHdl));
112 mpBindings->Invalidate(SID_ATTR_METRIC);
113 mpBindings->Invalidate(SID_ATTR_PAGE_HEADER);
114 mpBindings->Invalidate(SID_ATTR_PAGE_HEADER_LRMARGIN);
115 mpBindings->Invalidate(SID_ATTR_PAGE_HEADER_SPACING);
116 mpBindings->Invalidate(SID_ATTR_PAGE_HEADER_LAYOUT);
119 void PageHeaderPanel::UpdateHeaderCheck()
121 if (mxHeaderToggle->get_active())
123 mxHeaderSpacingLB->set_sensitive(true);
124 mxHeaderLayoutLB->set_sensitive(true);
125 mxHeaderMarginPresetLB->set_sensitive(true);
127 else
129 mxHeaderSpacingLB->set_sensitive(false);
130 mxHeaderLayoutLB->set_sensitive(false);
131 mxHeaderMarginPresetLB->set_sensitive(false);
135 void PageHeaderPanel::UpdateMarginControl()
137 sal_uInt16 nLeft = mpHeaderLRMarginItem->GetLeft();
138 sal_uInt16 nRight = mpHeaderLRMarginItem->GetRight();
139 sal_uInt16 nCount = mxHeaderMarginPresetLB->get_count();
140 if(nLeft == nRight)
142 for (sal_uInt16 i = 0; i < nCount; ++i)
144 if (mxHeaderMarginPresetLB->get_id(i).toUInt32() == nLeft)
146 mxHeaderMarginPresetLB->set_active(i);
147 int nCustomEntry = mxHeaderMarginPresetLB->find_text(m_aCustomEntry);
148 if (nCustomEntry != -1)
149 mxHeaderMarginPresetLB->remove(nCustomEntry);
150 return;
154 mxHeaderMarginPresetLB->append_text(m_aCustomEntry);
155 mxHeaderMarginPresetLB->set_active_text(m_aCustomEntry);
158 void PageHeaderPanel::UpdateSpacingControl()
160 sal_uInt16 nBottom = mpHeaderSpacingItem->GetLower();
161 sal_uInt16 nCount = mxHeaderSpacingLB->get_count();
162 for (sal_uInt16 i = 0; i < nCount; ++i)
164 if (mxHeaderSpacingLB->get_id(i).toUInt32() == nBottom)
166 mxHeaderSpacingLB->set_active(i);
167 int nCustomEntry = mxHeaderSpacingLB->find_text(m_aCustomEntry);
168 if (nCustomEntry != -1)
169 mxHeaderSpacingLB->remove(nCustomEntry);
170 return;
173 mxHeaderSpacingLB->append_text(m_aCustomEntry);
174 mxHeaderSpacingLB->set_active_text(m_aCustomEntry);
177 void PageHeaderPanel::UpdateLayoutControl()
179 sal_uInt16 nLayout = mpHeaderLayoutItem->GetValue();
180 mxHeaderLayoutLB->set_active(nLayout);
183 void PageHeaderPanel::NotifyItemUpdate(
184 const sal_uInt16 nSid,
185 const SfxItemState eState,
186 const SfxPoolItem* pState)
188 if (!mxHeaderToggle) //disposed
189 return;
191 switch(nSid)
193 case SID_ATTR_PAGE_HEADER:
195 if(eState >= SfxItemState::DEFAULT &&
196 dynamic_cast<const SfxBoolItem*>( pState) )
198 mpHeaderItem.reset( static_cast<SfxBoolItem*>(pState->Clone()) );
199 mxHeaderToggle->set_active(mpHeaderItem->GetValue());
200 UpdateHeaderCheck();
203 break;
204 case SID_ATTR_PAGE_HEADER_LRMARGIN:
206 if(eState >= SfxItemState::DEFAULT &&
207 dynamic_cast<const SvxLongLRSpaceItem*>( pState) )
209 mpHeaderLRMarginItem.reset( static_cast<SvxLongLRSpaceItem*>(pState->Clone()) );
210 UpdateMarginControl();
213 break;
214 case SID_ATTR_PAGE_HEADER_SPACING:
216 if(eState >= SfxItemState::DEFAULT &&
217 dynamic_cast<const SvxLongULSpaceItem*>( pState) )
219 mpHeaderSpacingItem.reset(static_cast<SvxLongULSpaceItem*>(pState->Clone()) );
220 UpdateSpacingControl();
223 break;
224 case SID_ATTR_PAGE_HEADER_LAYOUT:
226 if(eState >= SfxItemState::DEFAULT &&
227 dynamic_cast<const SfxInt16Item*>( pState) )
229 mpHeaderLayoutItem.reset(static_cast<SfxInt16Item*>(pState->Clone()) );
230 UpdateLayoutControl();
233 break;
234 case SID_ATTR_METRIC:
236 FieldUnit eFUnit = GetCurrentUnit(eState, pState);
237 if (meFUnit != eFUnit)
239 meFUnit = eFUnit;
240 SetMarginsAndSpacingFieldUnit();
241 UpdateSpacingControl();
242 UpdateMarginControl();
245 break;
246 default:
247 break;
251 IMPL_LINK_NOARG( PageHeaderPanel, HeaderToggleHdl, weld::Toggleable&, void )
253 bool IsChecked = mxHeaderToggle->get_active();
254 mpHeaderItem->SetValue(IsChecked);
255 GetBindings()->GetDispatcher()->ExecuteList( SID_ATTR_PAGE_HEADER, SfxCallMode::RECORD, { mpHeaderItem.get() } );
256 UpdateHeaderCheck();
259 IMPL_LINK_NOARG( PageHeaderPanel, HeaderLRMarginHdl, weld::ComboBox&, void )
261 sal_uInt16 nVal = mxHeaderMarginPresetLB->get_active_id().toUInt32();
262 mpHeaderLRMarginItem->SetLeft(nVal);
263 mpHeaderLRMarginItem->SetRight(nVal);
264 GetBindings()->GetDispatcher()->ExecuteList( SID_ATTR_PAGE_HEADER_LRMARGIN,
265 SfxCallMode::RECORD, { mpHeaderLRMarginItem.get() } );
268 IMPL_LINK_NOARG( PageHeaderPanel, HeaderSpacingHdl, weld::ComboBox&, void )
270 sal_uInt16 nVal = mxHeaderSpacingLB->get_active_id().toUInt32();
271 mpHeaderSpacingItem->SetLower(nVal);
272 GetBindings()->GetDispatcher()->ExecuteList( SID_ATTR_PAGE_HEADER_SPACING,
273 SfxCallMode::RECORD, { mpHeaderSpacingItem.get() } );
275 IMPL_LINK_NOARG( PageHeaderPanel, HeaderLayoutHdl, weld::ComboBox&, void )
277 sal_uInt16 nVal = mxHeaderLayoutLB->get_active();
278 mpHeaderLayoutItem->SetValue(nVal);
279 GetBindings()->GetDispatcher()->ExecuteList( SID_ATTR_PAGE_HEADER_LAYOUT,
280 SfxCallMode::RECORD, { mpHeaderLayoutItem.get() } );
286 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */