Bump version to 24.04.3.4
[LibreOffice.git] / svx / source / sidebar / text / TextCharacterSpacingControl.cxx
blob28eb699d2d25930044d63316c0b46b898d203c4c
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 <svl/itemset.hxx>
30 #include <helpids.h>
32 #include <com/sun/star/beans/NamedValue.hpp>
34 #define SPACING_VERY_TIGHT -30
35 #define SPACING_TIGHT -15
36 #define SPACING_NORMAL 0
37 #define SPACING_LOOSE 30
38 #define SPACING_VERY_LOOSE 60
40 namespace svx {
42 TextCharacterSpacingControl::TextCharacterSpacingControl(TextCharacterSpacingPopup* pControl, weld::Widget* pParent)
43 : WeldToolbarPopup(pControl->getFrameInterface(), pParent, "svx/ui/textcharacterspacingcontrol.ui", "TextCharacterSpacingControl")
44 , mnCustomKern(0)
45 , mnLastCus(SPACING_NOCUSTOM)
46 , mxEditKerning(m_xBuilder->weld_metric_spin_button("kerning", FieldUnit::POINT))
47 , mxTight(m_xBuilder->weld_button("tight"))
48 , mxVeryTight(m_xBuilder->weld_button("very_tight"))
49 , mxNormal(m_xBuilder->weld_button("normal"))
50 , mxLoose(m_xBuilder->weld_button("loose"))
51 , mxVeryLoose(m_xBuilder->weld_button("very_loose"))
52 , mxLastCustom(m_xBuilder->weld_button("last_custom"))
53 , mxControl(pControl)
55 mxEditKerning->connect_value_changed(LINK(this, TextCharacterSpacingControl, KerningModifyHdl));
56 mxEditKerning->set_help_id(HID_SPACING_MB_KERN);
58 Link<weld::Button&,void> aLink = LINK(this, TextCharacterSpacingControl, PredefinedValuesHdl);
59 mxNormal->connect_clicked(aLink);
60 mxVeryTight->connect_clicked(aLink);
61 mxTight->connect_clicked(aLink);
62 mxVeryLoose->connect_clicked(aLink);
63 mxLoose->connect_clicked(aLink);
64 mxLastCustom->connect_clicked(aLink);
66 Initialize();
69 void TextCharacterSpacingControl::GrabFocus()
71 tools::Long nKerning = mxEditKerning->get_value(FieldUnit::NONE);
72 switch (nKerning)
74 case SPACING_VERY_TIGHT:
75 mxVeryTight->grab_focus();
76 break;
77 case SPACING_TIGHT:
78 mxTight->grab_focus();
79 break;
80 case SPACING_NORMAL:
81 mxNormal->grab_focus();
82 break;
83 case SPACING_LOOSE:
84 mxLoose->grab_focus();
85 break;
86 case SPACING_VERY_LOOSE:
87 mxVeryLoose->grab_focus();
88 break;
89 default:
90 if (nKerning == mnCustomKern)
91 mxLastCustom->grab_focus();
92 else
93 mxEditKerning->grab_focus();
97 TextCharacterSpacingControl::~TextCharacterSpacingControl()
99 if (mnLastCus == SPACING_CLOSE_BY_CUS_EDIT)
101 SvtViewOptions aWinOpt(EViewType::Window, SIDEBAR_SPACING_GLOBAL_VALUE);
102 css::uno::Sequence<css::beans::NamedValue> aSeq
103 { { "Spacing", css::uno::Any(OUString::number(mnCustomKern)) } };
104 aWinOpt.SetUserData(aSeq);
108 void TextCharacterSpacingControl::Initialize()
110 SfxPoolItemHolder aResult;
111 SfxViewFrame* pViewFrm = SfxViewFrame::Current();
112 const SfxItemState eState(pViewFrm ? pViewFrm->GetBindings().GetDispatcher()->QueryState(SID_ATTR_CHAR_KERNING, aResult) : SfxItemState::UNKNOWN);
113 const SvxKerningItem* pKerningItem(static_cast<const SvxKerningItem*>(aResult.getItem()));
114 tools::Long nKerning = 0;
116 if (pKerningItem)
117 nKerning = pKerningItem->GetValue();
119 SvtViewOptions aWinOpt(EViewType::Window, SIDEBAR_SPACING_GLOBAL_VALUE);
120 if(aWinOpt.Exists())
122 css::uno::Sequence<css::beans::NamedValue> aSeq = aWinOpt.GetUserData();
123 OUString aTmp;
124 if(aSeq.hasElements())
125 aSeq[0].Value >>= aTmp;
127 OUString aWinData(aTmp);
128 mnCustomKern = aWinData.toInt32();
129 mnLastCus = SPACING_CLOSE_BY_CUS_EDIT;
131 else
133 mnLastCus = SPACING_NOCUSTOM;
136 if(eState >= SfxItemState::DEFAULT)
138 MapUnit eUnit = GetCoreMetric();
139 MapUnit eOrgUnit = eUnit;
140 tools::Long nBig = mxEditKerning->normalize(nKerning);
141 nKerning = OutputDevice::LogicToLogic(nBig, eOrgUnit, MapUnit::MapPoint);
142 mxEditKerning->set_value(nKerning, FieldUnit::NONE);
144 else if(SfxItemState::DISABLED == eState)
146 mxEditKerning->set_text(OUString());
147 mxEditKerning->set_sensitive(false);
149 else
151 mxEditKerning->set_text(OUString());
152 mxEditKerning->set_sensitive(false);
156 void TextCharacterSpacingControl::ExecuteCharacterSpacing(tools::Long nValue, bool bClose)
158 MapUnit eUnit = GetCoreMetric();
160 tools::Long nSign = (nValue < 0) ? -1 : 1;
161 nValue = nValue * nSign;
163 tools::Long nVal = OutputDevice::LogicToLogic(nValue, MapUnit::MapPoint, eUnit);
164 short nKern = (nValue == 0) ? 0 : static_cast<short>(mxEditKerning->denormalize(nVal));
166 SvxKerningItem aKernItem(nSign * nKern, SID_ATTR_CHAR_KERNING);
168 if (SfxViewFrame* pViewFrm = SfxViewFrame::Current())
170 pViewFrm->GetBindings().GetDispatcher()->ExecuteList(SID_ATTR_CHAR_KERNING,
171 SfxCallMode::RECORD, { &aKernItem });
174 if (bClose)
175 mxControl->EndPopupMode();
178 IMPL_LINK(TextCharacterSpacingControl, PredefinedValuesHdl, weld::Button&, rControl, void)
180 mnLastCus = SPACING_CLOSE_BY_CLICK_ICON;
182 if (&rControl == mxNormal.get())
184 ExecuteCharacterSpacing(SPACING_NORMAL);
186 else if (&rControl == mxVeryTight.get())
188 ExecuteCharacterSpacing(SPACING_VERY_TIGHT);
190 else if (&rControl == mxTight.get())
192 ExecuteCharacterSpacing(SPACING_TIGHT);
194 else if (&rControl == mxVeryLoose.get())
196 ExecuteCharacterSpacing(SPACING_VERY_LOOSE);
198 else if (&rControl == mxLoose.get())
200 ExecuteCharacterSpacing(SPACING_LOOSE);
202 else if (&rControl == mxLastCustom.get())
204 ExecuteCharacterSpacing(mnCustomKern);
208 IMPL_LINK_NOARG(TextCharacterSpacingControl, KerningModifyHdl, weld::MetricSpinButton&, void)
210 mnLastCus = SPACING_CLOSE_BY_CUS_EDIT;
211 mnCustomKern = mxEditKerning->get_value(FieldUnit::NONE);
213 ExecuteCharacterSpacing(mnCustomKern, false);
216 MapUnit TextCharacterSpacingControl::GetCoreMetric()
218 SfxItemPool &rPool = SfxGetpApp()->GetPool();
219 sal_uInt16 nWhich = rPool.GetWhich(SID_ATTR_CHAR_KERNING);
220 return rPool.GetMetric(nWhich);
223 } // end of namespace svx
225 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */