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 
20 #include "WrapPropertyPanel.hxx"
21 
22 #include <editeng/editids.hrc>
23 #include <svx/spacinglistbox.hxx>
24 #include <svx/svdtrans.hxx>
25 #include <sfx2/bindings.hxx>
26 #include <sfx2/dispatch.hxx>
27 #include <editeng/lrspitem.hxx>
28 #include <editeng/ulspitem.hxx>
29 #include <hintids.hxx>
30 #include <strings.hrc>
31 #include <uitool.hxx>
32 #include <com/sun/star/lang/IllegalArgumentException.hpp>
33 
34 namespace sw::sidebar {
35 
Create(weld::Widget * pParent,const css::uno::Reference<css::frame::XFrame> & rxFrame,SfxBindings * pBindings)36 std::unique_ptr<PanelLayout> WrapPropertyPanel::Create (
37     weld::Widget* pParent,
38     const css::uno::Reference< css::frame::XFrame >& rxFrame,
39     SfxBindings* pBindings)
40 {
41     if (pParent == nullptr)
42         throw css::lang::IllegalArgumentException("no parent Window given to WrapPropertyPanel::Create", nullptr, 0);
43     if ( ! rxFrame.is())
44         throw css::lang::IllegalArgumentException("no XFrame given to WrapPropertyPanel::Create", nullptr, 1);
45     if (pBindings == nullptr)
46         throw css::lang::IllegalArgumentException("no SfxBindings given to WrapPropertyPanel::Create", nullptr, 2);
47 
48     return std::make_unique<WrapPropertyPanel>(pParent, rxFrame, pBindings);
49 }
50 
WrapPropertyPanel(weld::Widget * pParent,const css::uno::Reference<css::frame::XFrame> & rxFrame,SfxBindings * pBindings)51 WrapPropertyPanel::WrapPropertyPanel(
52     weld::Widget* pParent,
53     const css::uno::Reference< css::frame::XFrame >& rxFrame,
54     SfxBindings* pBindings )
55     : PanelLayout(pParent, "WrapPropertyPanel", "modules/swriter/ui/sidebarwrap.ui")
56     , mpBindings(pBindings)
57     // spacing
58     , nTop(0)
59     , nBottom(0)
60     , nLeft(0)
61     , nRight(0)
62     // resources
63     , aCustomEntry(SwResId(STR_WRAP_PANEL_CUSTOM_STR))
64     // controller items
65     , maSwLRSpacingControl(SID_ATTR_LRSPACE, *pBindings, *this)
66     , maSwULSpacingControl(SID_ATTR_ULSPACE, *pBindings, *this)
67     , mxWrapOptions(m_xBuilder->weld_toolbar("wrapoptions"))
68     , mxWrapOptionsDispatch(new ToolbarUnoDispatcher(*mxWrapOptions, *m_xBuilder, rxFrame))
69     , mxSpacingLB(m_xBuilder->weld_combo_box("spacingLB"))
70 {
71     FieldUnit eMetric = ::GetDfltMetric(false);
72     SpacingListBox::Fill(IsInch(eMetric) ? SpacingType::SPACING_INCH : SpacingType::SPACING_CM, *mxSpacingLB);
73 
74     Initialize();
75 }
76 
~WrapPropertyPanel()77 WrapPropertyPanel::~WrapPropertyPanel()
78 {
79 
80     mxWrapOptionsDispatch.reset();
81     mxWrapOptions.reset();
82 
83     maSwLRSpacingControl.dispose();
84     maSwULSpacingControl.dispose();
85 }
86 
Initialize()87 void WrapPropertyPanel::Initialize()
88 {
89     mxSpacingLB->connect_changed(LINK(this, WrapPropertyPanel, SpacingLBHdl));
90 
91     mpBindings->Update( SID_ATTR_LRSPACE );
92     mpBindings->Update( SID_ATTR_ULSPACE );
93 }
94 
UpdateSpacingLB()95 void WrapPropertyPanel::UpdateSpacingLB()
96 {
97     if( (nLeft == nRight) && (nTop == nBottom) && (nLeft == nTop) )
98     {
99         sal_Int32 nCount = mxSpacingLB->get_count();
100         for (sal_Int32 i = 0; i < nCount; i++)
101         {
102             if (mxSpacingLB->get_id(i).toUInt32() == nLeft)
103             {
104                 mxSpacingLB->set_active(i);
105                 int nCustomEntry = mxSpacingLB->find_text(aCustomEntry);
106                 if (nCustomEntry != -1)
107                     mxSpacingLB->remove(nCustomEntry);
108                 return;
109             }
110         }
111     }
112 
113     if (mxSpacingLB->find_text(aCustomEntry) == -1)
114         mxSpacingLB->append_text(aCustomEntry);
115     mxSpacingLB->set_active_text(aCustomEntry);
116 }
117 
IMPL_LINK(WrapPropertyPanel,SpacingLBHdl,weld::ComboBox &,rBox,void)118 IMPL_LINK(WrapPropertyPanel, SpacingLBHdl, weld::ComboBox&, rBox, void)
119 {
120     sal_uInt16 nVal = rBox.get_active_id().toUInt32();
121 
122     SvxLRSpaceItem aLRItem(nVal, nVal, 0, 0, RES_LR_SPACE);
123     SvxULSpaceItem aULItem(nVal, nVal, RES_UL_SPACE);
124 
125     nTop = nBottom = nLeft = nRight = nVal;
126     mpBindings->GetDispatcher()->ExecuteList(SID_ATTR_LRSPACE,
127             SfxCallMode::RECORD, { &aLRItem });
128     mpBindings->GetDispatcher()->ExecuteList(SID_ATTR_ULSPACE,
129             SfxCallMode::RECORD, { &aULItem });
130 }
131 
NotifyItemUpdate(const sal_uInt16 nSId,const SfxItemState eState,const SfxPoolItem * pState)132 void WrapPropertyPanel::NotifyItemUpdate(
133     const sal_uInt16 nSId,
134     const SfxItemState eState,
135     const SfxPoolItem* pState)
136 {
137     switch(nSId)
138     {
139         case SID_ATTR_LRSPACE:
140         {
141             if(eState >= SfxItemState::DEFAULT)
142             {
143                 const SvxLRSpaceItem* pItem = dynamic_cast< const SvxLRSpaceItem* >(pState);
144                 if(pItem)
145                 {
146                     nLeft = pItem->GetLeft();
147                     nRight = pItem->GetRight();
148 
149                     UpdateSpacingLB();
150                 }
151             }
152         }
153         break;
154         case SID_ATTR_ULSPACE:
155         {
156             if(eState >= SfxItemState::DEFAULT)
157             {
158                 const SvxULSpaceItem* pItem = dynamic_cast< const SvxULSpaceItem* >(pState);
159                 if(pItem)
160                 {
161                     nTop = pItem->GetUpper();
162                     nBottom = pItem->GetLower();
163 
164                     UpdateSpacingLB();
165                 }
166             }
167         }
168         break;
169     }
170 }
171 
172 } // end of namespace ::sw::sidebar
173 
174 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
175