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 <svx/relfld.hxx>
21 #include <vcl/builderfactory.hxx>
23 SvxRelativeField::SvxRelativeField(
24 vcl::Window
*const pParent
, WinBits
const nBits
, FieldUnit
const eUnit
)
25 : MetricField( pParent
, nBits
)
28 SetDecimalDigits( 2 );
33 extern "C" SAL_DLLPUBLIC_EXPORT
void makeSvxRelativeField(VclPtr
<vcl::Window
> & rRet
, const VclPtr
<vcl::Window
> & pParent
, VclBuilder::stringmap
& rMap
)
35 static_assert(std::is_same_v
<std::remove_pointer_t
<VclBuilder::customMakeWidget
>,
36 decltype(makeSvxRelativeField
)>);
37 OUString
const custom(BuilderUtils::extractCustomProperty(rMap
));
38 FieldUnit
const eUnit(BuilderUtils::detectUnit(custom
));
39 rRet
= VclPtr
<SvxRelativeField
>::Create(pParent
,
40 WB_BORDER
| WB_SPIN
| WB_REPEAT
|
45 RelativeField::RelativeField(std::unique_ptr
<weld::MetricSpinButton
> pControl
)
46 : m_xSpinButton(std::move(pControl
))
49 , bRelativeMode(false)
51 , bNegativeEnabled(false)
54 weld::SpinButton
& rSpinButton
= m_xSpinButton
->get_widget();
55 rSpinButton
.connect_changed(LINK(this, RelativeField
, ModifyHdl
));
58 IMPL_LINK_NOARG(RelativeField
, ModifyHdl
, weld::Entry
&, void)
62 OUString aStr
= m_xSpinButton
->get_text();
63 bool bNewMode
= bRelative
;
67 const sal_Unicode
* pStr
= aStr
.getStr();
71 if( ( ( *pStr
< '0' ) || ( *pStr
> '9' ) ) &&
82 if ( aStr
.indexOf( "%" ) != -1 )
86 if ( bNewMode
!= bRelative
)
87 SetRelative( bNewMode
);
91 void RelativeField::EnableRelativeMode(sal_uInt16 nMin
, sal_uInt16 nMax
)
96 m_xSpinButton
->set_unit(FieldUnit::CM
);
99 void RelativeField::SetRelative( bool bNewRelative
)
101 weld::SpinButton
& rSpinButton
= m_xSpinButton
->get_widget();
103 int nStartPos
, nEndPos
;
104 rSpinButton
.get_selection_bounds(nStartPos
, nEndPos
);
105 OUString aStr
= rSpinButton
.get_text();
110 m_xSpinButton
->set_digits(0);
111 m_xSpinButton
->set_range(nRelMin
, nRelMax
, FieldUnit::NONE
);
112 m_xSpinButton
->set_unit(FieldUnit::PERCENT
);
117 m_xSpinButton
->set_digits(2);
118 m_xSpinButton
->set_range(bNegativeEnabled
? -9999 : 0, 9999, FieldUnit::NONE
);
119 m_xSpinButton
->set_unit(FieldUnit::CM
);
122 rSpinButton
.set_text(aStr
);
123 rSpinButton
.select_region(nStartPos
, nEndPos
);
126 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */