LibreOffice Module svx (master) 1
relfld.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <svx/relfld.hxx>
21
22SvxRelativeField::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)
29
30{
31 weld::SpinButton& rSpinButton = m_xSpinButton->get_widget();
32 rSpinButton.connect_changed(LINK(this, SvxRelativeField, ModifyHdl));
33}
34
36{
37 if (!bRelativeMode)
38 return;
39
40 OUString aStr = m_xSpinButton->get_text();
41 bool bNewMode = bRelative;
42
43 if ( bRelative )
44 {
45 const sal_Unicode* pStr = aStr.getStr();
46
47 while ( *pStr )
48 {
49 if( ( ( *pStr < '0' ) || ( *pStr > '9' ) ) &&
50 ( *pStr != '%' ) )
51 {
52 bNewMode = false;
53 break;
54 }
55 pStr++;
56 }
57 }
58 else
59 {
60 if ( aStr.indexOf( "%" ) != -1 )
61 bNewMode = true;
62 }
63
64 if ( bNewMode != bRelative )
65 SetRelative( bNewMode );
66}
67
68void SvxRelativeField::EnableRelativeMode(sal_uInt16 nMin, sal_uInt16 nMax)
69{
70 bRelativeMode = true;
71 nRelMin = nMin;
72 nRelMax = nMax;
73 m_xSpinButton->set_unit(FieldUnit::CM);
74}
75
76void SvxRelativeField::SetRelative( bool bNewRelative )
77{
78 weld::SpinButton& rSpinButton = m_xSpinButton->get_widget();
79
80 int nStartPos, nEndPos;
81 rSpinButton.get_selection_bounds(nStartPos, nEndPos);
82 OUString aStr = rSpinButton.get_text();
83
84 if ( bNewRelative )
85 {
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);
90 }
91 else
92 {
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);
97 }
98
99 rSpinButton.set_text(aStr);
100 rSpinButton.select_region(nStartPos, nEndPos);
101}
102
103/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::unique_ptr< weld::MetricSpinButton > m_xSpinButton
Definition: relfld.hxx:30
bool bRelativeMode
Definition: relfld.hxx:34
void EnableRelativeMode(sal_uInt16 nMin, sal_uInt16 nMax)
Definition: relfld.cxx:68
sal_uInt16 nRelMax
Definition: relfld.hxx:33
sal_uInt16 nRelMin
Definition: relfld.hxx:32
SvxRelativeField(std::unique_ptr< weld::MetricSpinButton > pControl)
Definition: relfld.cxx:22
bool bNegativeEnabled
Definition: relfld.hxx:36
void SetRelative(bool bRelative)
Definition: relfld.cxx:76
virtual void select_region(int nStartPos, int nEndPos)=0
virtual void set_text(const OUString &rText)=0
virtual void connect_changed(const Link< Entry &, void > &rLink)
virtual bool get_selection_bounds(int &rStartPos, int &rEndPos)=0
virtual OUString get_text() const=0
aStr
IMPL_LINK_NOARG(SvxRelativeField, ModifyHdl, weld::Entry &, void)
Definition: relfld.cxx:35
sal_uInt16 sal_Unicode