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 <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>
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
41 TextCharacterSpacingControl::TextCharacterSpacingControl(TextCharacterSpacingPopup
* pControl
, weld::Widget
* pParent
)
42 : WeldToolbarPopup(pControl
->getFrameInterface(), pParent
, "svx/ui/textcharacterspacingcontrol.ui", "TextCharacterSpacingControl")
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"))
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
);
68 void TextCharacterSpacingControl::GrabFocus()
70 tools::Long nKerning
= mxEditKerning
->get_value(FieldUnit::NONE
);
73 case SPACING_VERY_TIGHT
:
74 mxVeryTight
->grab_focus();
77 mxTight
->grab_focus();
80 mxNormal
->grab_focus();
83 mxLoose
->grab_focus();
85 case SPACING_VERY_LOOSE
:
86 mxVeryLoose
->grab_focus();
89 if (nKerning
== mnCustomKern
)
90 mxLastCustom
->grab_focus();
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;
116 nKerning
= pKerningItem
->GetValue();
118 SvtViewOptions
aWinOpt(EViewType::Window
, SIDEBAR_SPACING_GLOBAL_VALUE
);
121 css::uno::Sequence
<css::beans::NamedValue
> aSeq
= aWinOpt
.GetUserData();
123 if(aSeq
.hasElements())
124 aSeq
[0].Value
>>= aTmp
;
126 OUString
aWinData(aTmp
);
127 mnCustomKern
= aWinData
.toInt32();
128 mnLastCus
= SPACING_CLOSE_BY_CUS_EDIT
;
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);
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
});
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: */