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 .
20 #include "ParaLineSpacingControl.hxx"
22 #include <editeng/editids.hrc>
23 #include <editeng/kernitem.hxx>
24 #include <editeng/lspcitem.hxx>
25 #include <sfx2/dispatch.hxx>
26 #include <svtools/unitconv.hxx>
28 #define DEFAULT_LINE_SPACING 200
29 #define FIX_DIST_DEF 283
30 #define LINESPACE_1 100
31 #define LINESPACE_15 150
32 #define LINESPACE_2 200
33 #define LINESPACE_115 115
35 // values of the mpLineDist listbox
36 #define LLINESPACE_1 0
37 #define LLINESPACE_15 1
38 #define LLINESPACE_2 2
39 #define LLINESPACE_PROP 3
40 #define LLINESPACE_MIN 4
41 #define LLINESPACE_DURCH 5
42 #define LLINESPACE_FIX 6
44 // special case; should not conflict with the mpLinDist values
45 #define LLINESPACE_115 7
47 #define MIN_FIXED_DISTANCE 28
51 ParaLineSpacingControl::ParaLineSpacingControl(sal_uInt16 nId
)
52 : SfxPopupWindow(nId
, "ParaLineSpacingControl", "svx/ui/paralinespacingcontrol.ui")
54 mpSpacing1Button
= get
<PushButton
>("spacing_1");
55 mpSpacing115Button
= get
<PushButton
>("spacing_115");
56 mpSpacing15Button
= get
<PushButton
>("spacing_15");
57 mpSpacing2Button
= get
<PushButton
>("spacing_2");
59 mpLineDist
= get
<ListBox
>("line_dist");
61 mpLineDistLabel
= get
<FixedText
>("value_label");
62 mpLineDistAtPercentBox
= get
<MetricField
>("percent_box");
63 mpLineDistAtMetricBox
= get
<MetricField
>("metric_box");
65 mpActLineDistFld
= mpLineDistAtPercentBox
.get();
67 meLNSpaceUnit
= SFX_MAPUNIT_100TH_MM
;
69 Link
<> aLink
= LINK(this, ParaLineSpacingControl
, PredefinedValuesHandler
);
70 mpSpacing1Button
->SetClickHdl(aLink
);
71 mpSpacing115Button
->SetClickHdl(aLink
);
72 mpSpacing15Button
->SetClickHdl(aLink
);
73 mpSpacing2Button
->SetClickHdl(aLink
);
75 aLink
= LINK( this, ParaLineSpacingControl
, LineSPDistHdl_Impl
);
76 mpLineDist
->SetSelectHdl(aLink
);
77 SelectEntryPos(LLINESPACE_1
);
79 aLink
= LINK( this, ParaLineSpacingControl
, LineSPDistAtHdl_Impl
);
80 mpLineDistAtPercentBox
->SetModifyHdl( aLink
);
81 mpLineDistAtMetricBox
->SetModifyHdl( aLink
);
83 FieldUnit eUnit
= FUNIT_INCH
;
84 const SfxPoolItem
* pItem
= NULL
;
85 if (SfxViewFrame::Current()->GetBindings().GetDispatcher()->QueryState(SID_ATTR_METRIC
, pItem
) >= SfxItemState::DEFAULT
)
86 eUnit
= static_cast<FieldUnit
>(static_cast<const SfxUInt16Item
*>(pItem
)->GetValue());
88 eUnit
= SfxModule::GetCurrentFieldUnit();
90 SetFieldUnit(*mpLineDistAtMetricBox
, eUnit
);
95 ParaLineSpacingControl::~ParaLineSpacingControl()
100 void ParaLineSpacingControl::dispose()
102 mpActLineDistFld
.clear();
103 mpSpacing1Button
.clear();
104 mpSpacing115Button
.clear();
105 mpSpacing15Button
.clear();
106 mpSpacing2Button
.clear();
108 mpLineDistLabel
.clear();
109 mpLineDistAtPercentBox
.clear();
110 mpLineDistAtMetricBox
.clear();
111 SfxPopupWindow::dispose();
114 void ParaLineSpacingControl::Initialize()
116 const SfxPoolItem
* pItem
;
117 SfxItemState eState
= SfxViewFrame::Current()->GetBindings().GetDispatcher()->QueryState(SID_ATTR_PARA_LINESPACE
, pItem
);
119 const SvxLineSpacingItem
* currSPItem
= static_cast<const SvxLineSpacingItem
*>(pItem
);
121 mpLineDist
->Enable();
123 if( eState
>= SfxItemState::DEFAULT
)
125 SfxMapUnit eUnit
= SFX_MAPUNIT_100TH_MM
;
126 meLNSpaceUnit
= eUnit
;
128 switch( currSPItem
->GetLineSpaceRule() )
130 case SVX_LINE_SPACE_AUTO
:
132 SvxInterLineSpace eInter
= currSPItem
->GetInterLineSpaceRule();
136 case SVX_INTER_LINE_SPACE_OFF
:
137 SelectEntryPos(LLINESPACE_1
);
140 case SVX_INTER_LINE_SPACE_PROP
:
142 if ( LINESPACE_1
== currSPItem
->GetPropLineSpace() )
144 SelectEntryPos(LLINESPACE_1
);
146 else if ( LINESPACE_15
== currSPItem
->GetPropLineSpace() )
148 SelectEntryPos(LLINESPACE_15
);
150 else if ( LINESPACE_2
== currSPItem
->GetPropLineSpace() )
152 SelectEntryPos(LLINESPACE_2
);
156 SelectEntryPos(LLINESPACE_PROP
);
157 mpLineDistAtPercentBox
->SetValue(mpLineDistAtPercentBox
->Normalize(currSPItem
->GetPropLineSpace()));
162 case SVX_INTER_LINE_SPACE_FIX
:
164 SelectEntryPos(LLINESPACE_DURCH
);
165 SetMetricValue(*mpLineDistAtMetricBox
, currSPItem
->GetInterLineSpace(), eUnit
);
173 case SVX_LINE_SPACE_FIX
:
175 SelectEntryPos(LLINESPACE_FIX
);
176 SetMetricValue(*mpLineDistAtMetricBox
, currSPItem
->GetLineHeight(), eUnit
);
180 case SVX_LINE_SPACE_MIN
:
182 SelectEntryPos(LLINESPACE_MIN
);
183 SetMetricValue(*mpLineDistAtMetricBox
, currSPItem
->GetLineHeight(), eUnit
);
190 else if( eState
== SfxItemState::DISABLED
)
192 mpLineDist
->Disable();
193 mpLineDistLabel
->Disable();
194 mpActLineDistFld
->Disable();
195 mpActLineDistFld
->SetText("");
200 mpLineDistLabel
->Disable();
201 mpActLineDistFld
->Disable();
202 mpActLineDistFld
->SetText("");
203 mpLineDist
->SetNoSelection();
206 mpLineDist
->SaveValue();
209 const sal_uInt16 uCount = mpLineDist->GetEntryCount();
210 if( uCount == LLINESPACE_FIX + 1 )
212 switch (currentContext.GetCombinedContext_DI())
214 case CombinedEnumContext(Application_DrawImpress, Context_Table):
215 case CombinedEnumContext(Application_DrawImpress, Context_DrawText):
216 case CombinedEnumContext(Application_DrawImpress, Context_Draw):
217 case CombinedEnumContext(Application_DrawImpress, Context_TextObject):
218 case CombinedEnumContext(Application_DrawImpress, Context_Graphic):
219 case CombinedEnumContext(Application_Calc, Context_DrawText):
220 case CombinedEnumContext(Application_WriterVariants, Context_DrawText):
221 case CombinedEnumContext(Application_WriterVariants, Context_Annotation):
223 mpLineDist->RemoveEntry(LLINESPACE_FIX);
227 else if( uCount == LLINESPACE_FIX)
229 switch (currentContext.GetCombinedContext_DI())
231 case CombinedEnumContext(Application_WriterVariants, Context_Default):
232 case CombinedEnumContext(Application_WriterVariants, Context_Text):
233 case CombinedEnumContext(Application_WriterVariants, Context_Table):
235 mpLineDist->InsertEntry(OUString("Fixed"), LLINESPACE_FIX);
242 void ParaLineSpacingControl::UpdateMetricFields()
244 switch (mpLineDist
->GetSelectEntryPos())
249 if (mpActLineDistFld
== mpLineDistAtPercentBox
)
250 mpLineDistAtMetricBox
->Hide();
252 mpLineDistAtPercentBox
->Hide();
254 mpLineDistLabel
->Disable();
255 mpActLineDistFld
->Show();
256 mpActLineDistFld
->Disable();
257 mpActLineDistFld
->SetText("");
260 case LLINESPACE_DURCH
:
261 mpLineDistAtPercentBox
->Hide();
263 mpActLineDistFld
= mpLineDistAtMetricBox
.get();
264 mpLineDistAtMetricBox
->SetMin(0);
266 if (mpLineDistAtMetricBox
->GetText().isEmpty())
267 mpLineDistAtMetricBox
->SetValue(mpLineDistAtMetricBox
->Normalize(0));
269 mpLineDistLabel
->Enable();
270 mpActLineDistFld
->Show();
271 mpActLineDistFld
->Enable();
275 mpLineDistAtPercentBox
->Hide();
277 mpActLineDistFld
= mpLineDistAtMetricBox
.get();
278 mpLineDistAtMetricBox
->SetMin(0);
280 if (mpLineDistAtMetricBox
->GetText().isEmpty())
281 mpLineDistAtMetricBox
->SetValue(mpLineDistAtMetricBox
->Normalize(0), FUNIT_TWIP
);
283 mpLineDistLabel
->Enable();
284 mpActLineDistFld
->Show();
285 mpActLineDistFld
->Enable();
288 case LLINESPACE_PROP
:
289 mpLineDistAtMetricBox
->Hide();
291 mpActLineDistFld
= mpLineDistAtPercentBox
.get();
293 if (mpLineDistAtPercentBox
->GetText().isEmpty())
294 mpLineDistAtPercentBox
->SetValue(mpLineDistAtPercentBox
->Normalize(100), FUNIT_TWIP
);
296 mpLineDistLabel
->Enable();
297 mpActLineDistFld
->Show();
298 mpActLineDistFld
->Enable();
302 mpLineDistAtPercentBox
->Hide();
304 mpActLineDistFld
= mpLineDistAtMetricBox
.get();
305 sal_Int64 nTemp
= mpLineDistAtMetricBox
->GetValue();
306 mpLineDistAtMetricBox
->SetMin(mpLineDistAtMetricBox
->Normalize(MIN_FIXED_DISTANCE
), FUNIT_TWIP
);
308 if (mpLineDistAtMetricBox
->GetValue() != nTemp
)
309 SetMetricValue(*mpLineDistAtMetricBox
, FIX_DIST_DEF
, SFX_MAPUNIT_TWIP
);
311 mpLineDistLabel
->Enable();
312 mpActLineDistFld
->Show();
313 mpActLineDistFld
->Enable();
318 void ParaLineSpacingControl::SelectEntryPos(sal_Int32 nPos
)
320 mpLineDist
->SelectEntryPos(nPos
);
321 UpdateMetricFields();
324 IMPL_LINK(ParaLineSpacingControl
, LineSPDistHdl_Impl
, ListBox
*, /*pBox*/)
326 UpdateMetricFields();
331 IMPL_LINK_NOARG( ParaLineSpacingControl
, LineSPDistAtHdl_Impl
)
337 void ParaLineSpacingControl::ExecuteLineSpace()
339 mpLineDist
->SaveValue();
341 SvxLineSpacingItem
aSpacing(DEFAULT_LINE_SPACING
, SID_ATTR_PARA_LINESPACE
);
342 sal_uInt16 nPos
= mpLineDist
->GetSelectEntryPos();
349 SetLineSpace(aSpacing
, nPos
);
352 case LLINESPACE_PROP
:
353 SetLineSpace(aSpacing
, nPos
, mpLineDistAtPercentBox
->Denormalize((long)mpLineDistAtPercentBox
->GetValue()));
357 case LLINESPACE_DURCH
:
359 SetLineSpace(aSpacing
, nPos
, GetCoreValue(*mpLineDistAtMetricBox
, meLNSpaceUnit
));
366 SfxViewFrame::Current()->GetBindings().GetDispatcher()->Execute(
367 SID_ATTR_PARA_LINESPACE
, SfxCallMode::RECORD
, &aSpacing
, 0L);
370 void ParaLineSpacingControl::SetLineSpace(SvxLineSpacingItem
& rLineSpace
, int eSpace
, long lValue
)
375 rLineSpace
.GetLineSpaceRule() = SVX_LINE_SPACE_AUTO
;
376 rLineSpace
.GetInterLineSpaceRule() = SVX_INTER_LINE_SPACE_OFF
;
380 rLineSpace
.GetLineSpaceRule() = SVX_LINE_SPACE_AUTO
;
381 rLineSpace
.SetPropLineSpace( LINESPACE_15
);
385 rLineSpace
.GetLineSpaceRule() = SVX_LINE_SPACE_AUTO
;
386 rLineSpace
.SetPropLineSpace( LINESPACE_2
);
389 case LLINESPACE_PROP
:
390 rLineSpace
.GetLineSpaceRule() = SVX_LINE_SPACE_AUTO
;
391 rLineSpace
.SetPropLineSpace( (sal_uInt8
)lValue
);
395 rLineSpace
.SetLineHeight( (sal_uInt16
)lValue
);
396 rLineSpace
.GetInterLineSpaceRule() = SVX_INTER_LINE_SPACE_OFF
;
399 case LLINESPACE_DURCH
:
400 rLineSpace
.GetLineSpaceRule() = SVX_LINE_SPACE_AUTO
;
401 rLineSpace
.SetInterLineSpace( (sal_uInt16
)lValue
);
405 rLineSpace
.SetLineHeight((sal_uInt16
)lValue
);
406 rLineSpace
.GetLineSpaceRule() = SVX_LINE_SPACE_FIX
;
407 rLineSpace
.GetInterLineSpaceRule() = SVX_INTER_LINE_SPACE_OFF
;
412 IMPL_LINK(ParaLineSpacingControl
, PredefinedValuesHandler
, void *, pControl
)
414 if (pControl
== mpSpacing1Button
)
416 ExecuteLineSpacing(LLINESPACE_1
);
418 else if (pControl
== mpSpacing115Button
)
420 ExecuteLineSpacing(LLINESPACE_115
);
422 else if (pControl
== mpSpacing15Button
)
424 ExecuteLineSpacing(LLINESPACE_15
);
426 else if (pControl
== mpSpacing2Button
)
428 ExecuteLineSpacing(LLINESPACE_2
);
434 void ParaLineSpacingControl::ExecuteLineSpacing(sal_uInt16 nEntry
)
436 SvxLineSpacingItem
aSpacing(DEFAULT_LINE_SPACING
, SID_ATTR_PARA_LINESPACE
);
438 // special-case the 1.15 line spacing
439 if (nEntry
== LLINESPACE_115
)
440 SetLineSpace(aSpacing
, LLINESPACE_PROP
, mpLineDistAtPercentBox
->Denormalize(LINESPACE_115
));
442 SetLineSpace(aSpacing
, nEntry
);
444 SfxViewFrame::Current()->GetBindings().GetDispatcher()->Execute(
445 SID_ATTR_PARA_LINESPACE
, SfxCallMode::RECORD
, &aSpacing
, 0L);
447 // close when the user used the buttons
451 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */