Version 7.1.7.1, tag libreoffice-7.1.7.1
[LibreOffice.git] / svx / source / dialog / relfld.cxx
blob3929e4fcf7df9bf5670290a62a7373cf1fa2894b
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>
22 SvxRelativeField::SvxRelativeField(std::unique_ptr<weld::MetricSpinButton> pControl)
23 : m_xSpinButton(std::move(pControl))
24 , nRelMin(0)
25 , nRelMax(0)
26 , bRelativeMode(false)
27 , bRelative(false)
28 , bNegativeEnabled(false)
31 weld::SpinButton& rSpinButton = m_xSpinButton->get_widget();
32 rSpinButton.connect_changed(LINK(this, SvxRelativeField, ModifyHdl));
35 IMPL_LINK_NOARG(SvxRelativeField, ModifyHdl, weld::Entry&, void)
37 if (!bRelativeMode)
38 return;
40 OUString aStr = m_xSpinButton->get_text();
41 bool bNewMode = bRelative;
43 if ( bRelative )
45 const sal_Unicode* pStr = aStr.getStr();
47 while ( *pStr )
49 if( ( ( *pStr < '0' ) || ( *pStr > '9' ) ) &&
50 ( *pStr != '%' ) )
52 bNewMode = false;
53 break;
55 pStr++;
58 else
60 if ( aStr.indexOf( "%" ) != -1 )
61 bNewMode = true;
64 if ( bNewMode != bRelative )
65 SetRelative( bNewMode );
68 void SvxRelativeField::EnableRelativeMode(sal_uInt16 nMin, sal_uInt16 nMax)
70 bRelativeMode = true;
71 nRelMin = nMin;
72 nRelMax = nMax;
73 m_xSpinButton->set_unit(FieldUnit::CM);
76 void SvxRelativeField::SetRelative( bool bNewRelative )
78 weld::SpinButton& rSpinButton = m_xSpinButton->get_widget();
80 int nStartPos, nEndPos;
81 rSpinButton.get_selection_bounds(nStartPos, nEndPos);
82 OUString aStr = rSpinButton.get_text();
84 if ( bNewRelative )
86 bRelative = true;
87 m_xSpinButton->set_digits(0);
88 m_xSpinButton->set_range(nRelMin, nRelMax, FieldUnit::NONE);
89 m_xSpinButton->set_unit(FieldUnit::PERCENT);
91 else
93 bRelative = false;
94 m_xSpinButton->set_digits(2);
95 m_xSpinButton->set_range(bNegativeEnabled ? -9999 : 0, 9999, FieldUnit::NONE);
96 m_xSpinButton->set_unit(FieldUnit::CM);
99 rSpinButton.set_text(aStr);
100 rSpinButton.select_region(nStartPos, nEndPos);
103 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */