/src/libreoffice/vcl/source/weld/SpinButton.cxx
Line | Count | Source |
1 | | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ |
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 | | |
10 | | #include <vcl/weld/SpinButton.hxx> |
11 | | |
12 | | namespace weld |
13 | | { |
14 | | double SpinButton::convert_value_to_double(sal_Int64 nValue) const |
15 | 0 | { |
16 | 0 | return static_cast<double>(nValue) / Power10(get_digits()); |
17 | 0 | } |
18 | | |
19 | | sal_Int64 SpinButton::convert_double_to_value(double fDouble) const |
20 | 0 | { |
21 | 0 | return basegfx::fround64(fDouble * Power10(get_digits())); |
22 | 0 | } |
23 | | |
24 | | std::optional<OUString> SpinButton::format_floating_point_value(double fValue) |
25 | 0 | { |
26 | 0 | if (!m_aFormatValueHdl.IsSet()) |
27 | 0 | return {}; |
28 | 0 | const OUString sText = m_aFormatValueHdl.Call(convert_double_to_value(fValue)); |
29 | 0 | return sText; |
30 | 0 | } |
31 | | |
32 | | TriState SpinButton::parse_text(const OUString& rText, double* pResult) |
33 | 0 | { |
34 | 0 | if (!m_aParseTextHdl.IsSet()) |
35 | 0 | return TRISTATE_INDET; |
36 | 0 | std::optional<int> aValue = m_aParseTextHdl.Call(rText); |
37 | 0 | if (!aValue.has_value()) |
38 | 0 | return TRISTATE_FALSE; |
39 | | |
40 | 0 | *pResult = convert_value_to_double(aValue.value()); |
41 | 0 | return TRISTATE_TRUE; |
42 | 0 | } |
43 | | |
44 | | void SpinButton::set_value(sal_Int64 value) |
45 | 0 | { |
46 | 0 | disable_notify_events(); |
47 | 0 | set_floating_point_value(convert_value_to_double(value)); |
48 | 0 | enable_notify_events(); |
49 | 0 | } |
50 | | |
51 | | void SpinButton::set_range(sal_Int64 min, sal_Int64 max) |
52 | 0 | { |
53 | 0 | set_floating_point_range(convert_value_to_double(min), convert_value_to_double(max)); |
54 | 0 | } |
55 | | |
56 | | void SpinButton::get_range(sal_Int64& min, sal_Int64& max) const |
57 | 0 | { |
58 | 0 | double fMin = 0; |
59 | 0 | double fMax = 0; |
60 | 0 | get_floating_point_range(fMin, fMax); |
61 | 0 | min = convert_double_to_value(fMin); |
62 | 0 | max = convert_double_to_value(fMax); |
63 | 0 | } |
64 | | |
65 | | void SpinButton::set_min(sal_Int64 min) |
66 | 0 | { |
67 | 0 | sal_Int64 dummy, max; |
68 | 0 | get_range(dummy, max); |
69 | 0 | set_range(min, max); |
70 | 0 | } |
71 | | |
72 | | void SpinButton::set_max(sal_Int64 max) |
73 | 0 | { |
74 | 0 | sal_Int64 min, dummy; |
75 | 0 | get_range(min, dummy); |
76 | 0 | set_range(min, max); |
77 | 0 | } |
78 | | |
79 | | sal_Int64 SpinButton::get_min() const |
80 | 0 | { |
81 | 0 | sal_Int64 min, dummy; |
82 | 0 | get_range(min, dummy); |
83 | 0 | return min; |
84 | 0 | } |
85 | | |
86 | | sal_Int64 SpinButton::get_max() const |
87 | 0 | { |
88 | 0 | sal_Int64 dummy, max; |
89 | 0 | get_range(dummy, max); |
90 | 0 | return max; |
91 | 0 | } |
92 | | |
93 | | void SpinButton::set_increments(sal_Int64 step, sal_Int64 page) |
94 | 0 | { |
95 | 0 | set_floating_point_increments(convert_value_to_double(step), convert_value_to_double(page)); |
96 | 0 | } |
97 | | |
98 | | void SpinButton::get_increments(sal_Int64& step, sal_Int64& page) const |
99 | 0 | { |
100 | 0 | double fStep = 0; |
101 | 0 | double fPage = 0; |
102 | 0 | get_floating_point_increments(fStep, fPage); |
103 | 0 | step = convert_double_to_value(fStep); |
104 | 0 | page = convert_double_to_value(fPage); |
105 | 0 | } |
106 | | |
107 | | unsigned int SpinButton::Power10(unsigned int n) |
108 | 0 | { |
109 | 0 | unsigned int nValue = 1; |
110 | 0 | for (unsigned int i = 0; i < n; ++i) |
111 | 0 | nValue *= 10; |
112 | 0 | return nValue; |
113 | 0 | } |
114 | | |
115 | | sal_Int64 SpinButton::denormalize(sal_Int64 nValue) const |
116 | 0 | { |
117 | 0 | const int nFactor = Power10(get_digits()); |
118 | |
|
119 | 0 | if ((nValue < (std::numeric_limits<sal_Int64>::min() + nFactor)) |
120 | 0 | || (nValue > (std::numeric_limits<sal_Int64>::max() - nFactor))) |
121 | 0 | { |
122 | 0 | return nValue / nFactor; |
123 | 0 | } |
124 | | |
125 | 0 | const int nHalf = nFactor / 2; |
126 | |
|
127 | 0 | if (nValue < 0) |
128 | 0 | return (nValue - nHalf) / nFactor; |
129 | 0 | return (nValue + nHalf) / nFactor; |
130 | 0 | } |
131 | | } |
132 | | |
133 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ |