Bump version to 6.4.7.2.M8
[LibreOffice.git] / svx / source / dialog / relfld.cxx
blob47642e3ec7165b483c38d518bb0af0da5e967f81
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 .
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)
27 SetUnit(eUnit);
28 SetDecimalDigits( 2 );
29 SetMin( 0 );
30 SetMax( 9999 );
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 |
41 WB_LEFT | WB_GROUP,
42 eUnit);
45 RelativeField::RelativeField(std::unique_ptr<weld::MetricSpinButton> pControl)
46 : m_xSpinButton(std::move(pControl))
47 , nRelMin(0)
48 , nRelMax(0)
49 , bRelativeMode(false)
50 , bRelative(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)
60 if (bRelativeMode)
62 OUString aStr = m_xSpinButton->get_text();
63 bool bNewMode = bRelative;
65 if ( bRelative )
67 const sal_Unicode* pStr = aStr.getStr();
69 while ( *pStr )
71 if( ( ( *pStr < '0' ) || ( *pStr > '9' ) ) &&
72 ( *pStr != '%' ) )
74 bNewMode = false;
75 break;
77 pStr++;
80 else
82 if ( aStr.indexOf( "%" ) != -1 )
83 bNewMode = true;
86 if ( bNewMode != bRelative )
87 SetRelative( bNewMode );
91 void RelativeField::EnableRelativeMode(sal_uInt16 nMin, sal_uInt16 nMax)
93 bRelativeMode = true;
94 nRelMin = nMin;
95 nRelMax = 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();
107 if ( bNewRelative )
109 bRelative = true;
110 m_xSpinButton->set_digits(0);
111 m_xSpinButton->set_range(nRelMin, nRelMax, FieldUnit::NONE);
112 m_xSpinButton->set_unit(FieldUnit::PERCENT);
114 else
116 bRelative = false;
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: */