Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / svx / source / sidebar / text / TextCharacterSpacingControl.cxx
blob765a307b9361b5158f9c36c66338897468443594
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 <sfx2/bindings.hxx>
20 #include "TextCharacterSpacingControl.hxx"
21 #include <unotools/viewoptions.hxx>
22 #include <editeng/editids.hrc>
23 #include <editeng/kernitem.hxx>
24 #include <sfx2/app.hxx>
25 #include <sfx2/dispatch.hxx>
26 #include <sfx2/viewfrm.hxx>
27 #include <TextCharacterSpacingPopup.hxx>
28 #include <svl/itempool.hxx>
29 #include <helpids.h>
31 #include <com/sun/star/beans/NamedValue.hpp>
33 #define SPACING_VERY_TIGHT -30
34 #define SPACING_TIGHT -15
35 #define SPACING_NORMAL 0
36 #define SPACING_LOOSE 30
37 #define SPACING_VERY_LOOSE 60
39 namespace svx {
41 TextCharacterSpacingControl::TextCharacterSpacingControl(TextCharacterSpacingPopup* pControl, weld::Widget* pParent)
42 : WeldToolbarPopup(pControl->getFrameInterface(), pParent, "svx/ui/textcharacterspacingcontrol.ui", "TextCharacterSpacingControl")
43 , mnCustomKern(0)
44 , mnLastCus(SPACING_NOCUSTOM)
45 , mxEditKerning(m_xBuilder->weld_metric_spin_button("kerning", FieldUnit::POINT))
46 , mxTight(m_xBuilder->weld_button("tight"))
47 , mxVeryTight(m_xBuilder->weld_button("very_tight"))
48 , mxNormal(m_xBuilder->weld_button("normal"))
49 , mxLoose(m_xBuilder->weld_button("loose"))
50 , mxVeryLoose(m_xBuilder->weld_button("very_loose"))
51 , mxLastCustom(m_xBuilder->weld_button("last_custom"))
52 , mxControl(pControl)
54 mxEditKerning->connect_value_changed(LINK(this, TextCharacterSpacingControl, KerningModifyHdl));
55 mxEditKerning->set_help_id(HID_SPACING_MB_KERN);
57 Link<weld::Button&,void> aLink = LINK(this, TextCharacterSpacingControl, PredefinedValuesHdl);
58 mxNormal->connect_clicked(aLink);
59 mxVeryTight->connect_clicked(aLink);
60 mxTight->connect_clicked(aLink);
61 mxVeryLoose->connect_clicked(aLink);
62 mxLoose->connect_clicked(aLink);
63 mxLastCustom->connect_clicked(aLink);
65 Initialize();
68 void TextCharacterSpacingControl::GrabFocus()
70 tools::Long nKerning = mxEditKerning->get_value(FieldUnit::NONE);
71 switch (nKerning)
73 case SPACING_VERY_TIGHT:
74 mxVeryTight->grab_focus();
75 break;
76 case SPACING_TIGHT:
77 mxTight->grab_focus();
78 break;
79 case SPACING_NORMAL:
80 mxNormal->grab_focus();
81 break;
82 case SPACING_LOOSE:
83 mxLoose->grab_focus();
84 break;
85 case SPACING_VERY_LOOSE:
86 mxVeryLoose->grab_focus();
87 break;
88 default:
89 if (nKerning == mnCustomKern)
90 mxLastCustom->grab_focus();
91 else
92 mxEditKerning->grab_focus();
96 TextCharacterSpacingControl::~TextCharacterSpacingControl()
98 if (mnLastCus == SPACING_CLOSE_BY_CUS_EDIT)
100 SvtViewOptions aWinOpt(EViewType::Window, SIDEBAR_SPACING_GLOBAL_VALUE);
101 css::uno::Sequence<css::beans::NamedValue> aSeq
102 { { "Spacing", css::uno::Any(OUString::number(mnCustomKern)) } };
103 aWinOpt.SetUserData(aSeq);
107 void TextCharacterSpacingControl::Initialize()
109 const SvxKerningItem* pKerningItem(nullptr);
110 SfxViewFrame* pViewFrm = SfxViewFrame::Current();
111 SfxItemState eState = pViewFrm ? pViewFrm->GetBindings().GetDispatcher()->QueryState(SID_ATTR_CHAR_KERNING, pKerningItem) : SfxItemState::UNKNOWN;
113 tools::Long nKerning = 0;
115 if (pKerningItem)
116 nKerning = pKerningItem->GetValue();
118 SvtViewOptions aWinOpt(EViewType::Window, SIDEBAR_SPACING_GLOBAL_VALUE);
119 if(aWinOpt.Exists())
121 css::uno::Sequence<css::beans::NamedValue> aSeq = aWinOpt.GetUserData();
122 OUString aTmp;
123 if(aSeq.hasElements())
124 aSeq[0].Value >>= aTmp;
126 OUString aWinData(aTmp);
127 mnCustomKern = aWinData.toInt32();
128 mnLastCus = SPACING_CLOSE_BY_CUS_EDIT;
130 else
132 mnLastCus = SPACING_NOCUSTOM;
135 if(eState >= SfxItemState::DEFAULT)
137 MapUnit eUnit = GetCoreMetric();
138 MapUnit eOrgUnit = eUnit;
139 tools::Long nBig = mxEditKerning->normalize(nKerning);
140 nKerning = OutputDevice::LogicToLogic(nBig, eOrgUnit, MapUnit::MapPoint);
141 mxEditKerning->set_value(nKerning, FieldUnit::NONE);
143 else if(SfxItemState::DISABLED == eState)
145 mxEditKerning->set_text(OUString());
146 mxEditKerning->set_sensitive(false);
148 else
150 mxEditKerning->set_text(OUString());
151 mxEditKerning->set_sensitive(false);
155 void TextCharacterSpacingControl::ExecuteCharacterSpacing(tools::Long nValue, bool bClose)
157 MapUnit eUnit = GetCoreMetric();
159 tools::Long nSign = (nValue < 0) ? -1 : 1;
160 nValue = nValue * nSign;
162 tools::Long nVal = OutputDevice::LogicToLogic(nValue, MapUnit::MapPoint, eUnit);
163 short nKern = (nValue == 0) ? 0 : static_cast<short>(mxEditKerning->denormalize(nVal));
165 SvxKerningItem aKernItem(nSign * nKern, SID_ATTR_CHAR_KERNING);
167 if (SfxViewFrame* pViewFrm = SfxViewFrame::Current())
169 pViewFrm->GetBindings().GetDispatcher()->ExecuteList(SID_ATTR_CHAR_KERNING,
170 SfxCallMode::RECORD, { &aKernItem });
173 if (bClose)
174 mxControl->EndPopupMode();
177 IMPL_LINK(TextCharacterSpacingControl, PredefinedValuesHdl, weld::Button&, rControl, void)
179 mnLastCus = SPACING_CLOSE_BY_CLICK_ICON;
181 if (&rControl == mxNormal.get())
183 ExecuteCharacterSpacing(SPACING_NORMAL);
185 else if (&rControl == mxVeryTight.get())
187 ExecuteCharacterSpacing(SPACING_VERY_TIGHT);
189 else if (&rControl == mxTight.get())
191 ExecuteCharacterSpacing(SPACING_TIGHT);
193 else if (&rControl == mxVeryLoose.get())
195 ExecuteCharacterSpacing(SPACING_VERY_LOOSE);
197 else if (&rControl == mxLoose.get())
199 ExecuteCharacterSpacing(SPACING_LOOSE);
201 else if (&rControl == mxLastCustom.get())
203 ExecuteCharacterSpacing(mnCustomKern);
207 IMPL_LINK_NOARG(TextCharacterSpacingControl, KerningModifyHdl, weld::MetricSpinButton&, void)
209 mnLastCus = SPACING_CLOSE_BY_CUS_EDIT;
210 mnCustomKern = mxEditKerning->get_value(FieldUnit::NONE);
212 ExecuteCharacterSpacing(mnCustomKern, false);
215 MapUnit TextCharacterSpacingControl::GetCoreMetric()
217 SfxItemPool &rPool = SfxGetpApp()->GetPool();
218 sal_uInt16 nWhich = rPool.GetWhich(SID_ATTR_CHAR_KERNING);
219 return rPool.GetMetric(nWhich);
222 } // end of namespace svx
224 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */