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 <svl/eitem.hxx>
22 #include <svl/itemset.hxx>
23 #include <svx/dlgutil.hxx>
24 #include <svx/rulritem.hxx>
25 #include <svx/svdtrans.hxx>
26 #include <svx/spacinglistbox.hxx>
27 #include <svx/samecontentlistbox.hxx>
28 #include "PageHeaderPanel.hxx"
29 #include <sfx2/dispatch.hxx>
30 #include <sfx2/bindings.hxx>
33 #include <com/sun/star/lang/IllegalArgumentException.hpp>
35 namespace sw::sidebar
{
37 std::unique_ptr
<PanelLayout
> PageHeaderPanel::Create(
38 weld::Widget
* pParent
,
39 SfxBindings
* pBindings
)
41 if( pParent
== nullptr )
42 throw css::lang::IllegalArgumentException(u
"no parent window given to PageHeaderPanel::Create"_ustr
, nullptr, 0);
43 if( pBindings
== nullptr )
44 throw css::lang::IllegalArgumentException(u
"no SfxBindings given to PageHeaderPanel::Create"_ustr
, nullptr, 0);
46 return std::make_unique
<PageHeaderPanel
>(pParent
, pBindings
);
49 void PageHeaderPanel::SetMarginsAndSpacingFieldUnit()
51 SpacingListBox::Fill(IsInch(meFUnit
) ? SpacingType::SPACING_INCH
: SpacingType::SPACING_CM
, *mxHeaderSpacingLB
);
52 SpacingListBox::Fill(IsInch(meFUnit
) ? SpacingType::MARGINS_INCH
: SpacingType::MARGINS_CM
, *mxHeaderMarginPresetLB
);
55 PageHeaderPanel::PageHeaderPanel(
56 weld::Widget
* pParent
,
57 SfxBindings
* pBindings
59 PanelLayout(pParent
, u
"PageHeaderPanel"_ustr
, u
"modules/swriter/ui/pageheaderpanel.ui"_ustr
),
60 mpBindings( pBindings
),
61 maHFToggleController(SID_ATTR_PAGE_HEADER
, *pBindings
, *this),
62 maMetricController(SID_ATTR_METRIC
, *pBindings
,*this),
63 maHeaderLRMarginController(SID_ATTR_PAGE_HEADER_LRMARGIN
, *pBindings
, *this),
64 maHeaderSpacingController(SID_ATTR_PAGE_HEADER_SPACING
, *pBindings
, *this),
65 maHeaderLayoutController(SID_ATTR_PAGE_HEADER_LAYOUT
, *pBindings
, *this),
66 meFUnit(GetModuleFieldUnit()),
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(u
"headertoggle"_ustr
)),
72 mxHeaderSpacingLB(m_xBuilder
->weld_combo_box(u
"spacingpreset"_ustr
)),
73 mxHeaderMarginPresetLB(m_xBuilder
->weld_combo_box(u
"headermarginpreset"_ustr
)),
74 mxHeaderLayoutLB(m_xBuilder
->weld_combo_box(u
"samecontentLB"_ustr
)),
75 mxCustomEntry(m_xBuilder
->weld_label(u
"customlabel"_ustr
))
80 PageHeaderPanel::~PageHeaderPanel()
82 mxHeaderToggle
.reset();
83 mxHeaderSpacingLB
.reset();
84 mxHeaderLayoutLB
.reset();
85 mxHeaderMarginPresetLB
.reset();
86 mxCustomEntry
.reset();
89 FieldUnit
PageHeaderPanel::GetCurrentUnit(SfxItemState eState
, const SfxPoolItem
* pState
)
93 if (pState
&& eState
>= SfxItemState::DEFAULT
)
94 eUnit
= static_cast<FieldUnit
>(static_cast<const SfxUInt16Item
*>(pState
)->GetValue());
96 eUnit
= GetModuleFieldUnit();
101 void PageHeaderPanel::Initialize()
103 SameContentListBox::Fill(*mxHeaderLayoutLB
);
105 SetMarginsAndSpacingFieldUnit();
107 m_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
));
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
);
120 void PageHeaderPanel::UpdateHeaderCheck()
122 if (mxHeaderToggle
->get_active())
124 mxHeaderSpacingLB
->set_sensitive(true);
125 mxHeaderLayoutLB
->set_sensitive(true);
126 mxHeaderMarginPresetLB
->set_sensitive(true);
130 mxHeaderSpacingLB
->set_sensitive(false);
131 mxHeaderLayoutLB
->set_sensitive(false);
132 mxHeaderMarginPresetLB
->set_sensitive(false);
136 void PageHeaderPanel::UpdateMarginControl()
138 sal_uInt16 nLeft
= mpHeaderLRMarginItem
->GetLeft();
139 sal_uInt16 nRight
= mpHeaderLRMarginItem
->GetRight();
140 sal_uInt16 nCount
= mxHeaderMarginPresetLB
->get_count();
143 for (sal_uInt16 i
= 0; i
< nCount
; ++i
)
145 if (mxHeaderMarginPresetLB
->get_id(i
).toUInt32() == nLeft
)
147 mxHeaderMarginPresetLB
->set_active(i
);
148 int nCustomEntry
= mxHeaderMarginPresetLB
->find_text(m_aCustomEntry
);
149 if (nCustomEntry
!= -1)
150 mxHeaderMarginPresetLB
->remove(nCustomEntry
);
155 mxHeaderMarginPresetLB
->append_text(m_aCustomEntry
);
156 mxHeaderMarginPresetLB
->set_active_text(m_aCustomEntry
);
159 void PageHeaderPanel::UpdateSpacingControl()
161 sal_uInt16 nBottom
= mpHeaderSpacingItem
->GetLower();
162 sal_uInt16 nCount
= mxHeaderSpacingLB
->get_count();
163 for (sal_uInt16 i
= 0; i
< nCount
; ++i
)
165 if (mxHeaderSpacingLB
->get_id(i
).toUInt32() == nBottom
)
167 mxHeaderSpacingLB
->set_active(i
);
168 int nCustomEntry
= mxHeaderSpacingLB
->find_text(m_aCustomEntry
);
169 if (nCustomEntry
!= -1)
170 mxHeaderSpacingLB
->remove(nCustomEntry
);
174 mxHeaderSpacingLB
->append_text(m_aCustomEntry
);
175 mxHeaderSpacingLB
->set_active_text(m_aCustomEntry
);
178 void PageHeaderPanel::UpdateLayoutControl()
180 sal_uInt16 nLayout
= mpHeaderLayoutItem
->GetValue();
181 mxHeaderLayoutLB
->set_active(nLayout
);
184 void PageHeaderPanel::NotifyItemUpdate(
185 const sal_uInt16 nSid
,
186 const SfxItemState eState
,
187 const SfxPoolItem
* pState
)
189 if (!mxHeaderToggle
) //disposed
194 case SID_ATTR_PAGE_HEADER
:
196 if(eState
>= SfxItemState::DEFAULT
&&
197 dynamic_cast<const SfxBoolItem
*>( pState
) )
199 mpHeaderItem
.reset( static_cast<SfxBoolItem
*>(pState
->Clone()) );
200 mxHeaderToggle
->set_active(mpHeaderItem
->GetValue());
205 case SID_ATTR_PAGE_HEADER_LRMARGIN
:
207 if(eState
>= SfxItemState::DEFAULT
&&
208 dynamic_cast<const SvxLongLRSpaceItem
*>( pState
) )
210 mpHeaderLRMarginItem
.reset( static_cast<SvxLongLRSpaceItem
*>(pState
->Clone()) );
211 UpdateMarginControl();
215 case SID_ATTR_PAGE_HEADER_SPACING
:
217 if(eState
>= SfxItemState::DEFAULT
&&
218 dynamic_cast<const SvxLongULSpaceItem
*>( pState
) )
220 mpHeaderSpacingItem
.reset(static_cast<SvxLongULSpaceItem
*>(pState
->Clone()) );
221 UpdateSpacingControl();
225 case SID_ATTR_PAGE_HEADER_LAYOUT
:
227 if(eState
>= SfxItemState::DEFAULT
&&
228 dynamic_cast<const SfxInt16Item
*>( pState
) )
230 mpHeaderLayoutItem
.reset(static_cast<SfxInt16Item
*>(pState
->Clone()) );
231 UpdateLayoutControl();
235 case SID_ATTR_METRIC
:
237 FieldUnit eFUnit
= GetCurrentUnit(eState
, pState
);
238 if (meFUnit
!= eFUnit
)
241 SetMarginsAndSpacingFieldUnit();
242 UpdateSpacingControl();
243 UpdateMarginControl();
252 IMPL_LINK_NOARG( PageHeaderPanel
, HeaderToggleHdl
, weld::Toggleable
&, void )
254 bool IsChecked
= mxHeaderToggle
->get_active();
255 mpHeaderItem
->SetValue(IsChecked
);
256 GetBindings()->GetDispatcher()->ExecuteList( SID_ATTR_PAGE_HEADER
, SfxCallMode::RECORD
, { mpHeaderItem
.get() } );
260 IMPL_LINK_NOARG( PageHeaderPanel
, HeaderLRMarginHdl
, weld::ComboBox
&, void )
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() } );
269 IMPL_LINK_NOARG( PageHeaderPanel
, HeaderSpacingHdl
, weld::ComboBox
&, void )
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() } );
276 IMPL_LINK_NOARG( PageHeaderPanel
, HeaderLayoutHdl
, weld::ComboBox
&, void )
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() } );
287 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */