diff options
author | Caolán McNamara <caolanm@redhat.com> | 2018-11-14 13:52:06 +0000 |
---|---|---|
committer | Caolán McNamara <caolanm@redhat.com> | 2018-11-15 18:17:35 +0100 |
commit | 4ce5d39689fda418e8f2d7d9c1124190b2cbad7a (patch) | |
tree | 1e261f9e2b305fbdf56e3784c6589efbd683755b /vcl/source/control | |
parent | 9e0a099fb66c303bec0489198ba7cfe770b28684 (diff) |
bind SalInstanceSpinButton to FormattedField
Change-Id: I71190343739fae51b1c17d74ea1c4a548bca0b01
Reviewed-on: https://gerrit.libreoffice.org/63371
Reviewed-by: Caolán McNamara <caolanm@redhat.com>
Tested-by: Caolán McNamara <caolanm@redhat.com>
Diffstat (limited to 'vcl/source/control')
-rw-r--r-- | vcl/source/control/field.cxx | 26 | ||||
-rw-r--r-- | vcl/source/control/fmtfield.cxx | 145 |
2 files changed, 105 insertions, 66 deletions
diff --git a/vcl/source/control/field.cxx b/vcl/source/control/field.cxx index 8cf40c6e7682..519eb8aa5a49 100644 --- a/vcl/source/control/field.cxx +++ b/vcl/source/control/field.cxx @@ -454,8 +454,7 @@ bool FormatterBase::IsEmptyFieldValue() const void NumericFormatter::FormatValue(Selection const * pNewSelection) { mbFormatting = true; - if (!m_aOutputHdl.IsSet() || !m_aOutputHdl.Call(*GetField())) - ImplSetText(CreateFieldText(mnLastValue), pNewSelection); + ImplSetText(CreateFieldText(mnLastValue), pNewSelection); mbFormatting = false; } @@ -478,7 +477,6 @@ void NumericFormatter::ImplInit() mbShowTrailingZeros = true; mbWrapOnLimits = false; mbFormatting = false; - mbDisableRemainderFactor = false; // for fields mnSpinSize = 1; @@ -578,19 +576,6 @@ sal_Int64 NumericFormatter::GetValue() const if (mbFormatting) //don't parse the entry if we're currently formatting what to put in it return mnLastValue; - if (m_aInputHdl.IsSet()) - { - sal_Int64 nResult; - TriState eState = m_aInputHdl.Call(&nResult); - if (eState != TRISTATE_INDET) - { - if (eState == TRISTATE_TRUE) - return ClipAgainstMinMax(nResult); - else - return mnLastValue; - } - } - return GetField() ? GetValueFromString(GetField()->GetText()) : 0; } @@ -642,15 +627,10 @@ void NumericFormatter::Reformat() ImplNumericReformat(); } -void NumericFormatter::DisableRemainderFactor() -{ - mbDisableRemainderFactor = true; -} - void NumericFormatter::FieldUp() { sal_Int64 nValue = GetValue(); - sal_Int64 nRemainder = mbDisableRemainderFactor ? 0 : (nValue % mnSpinSize); + sal_Int64 nRemainder = nValue % mnSpinSize; if (nValue >= 0) nValue = (nRemainder == 0) ? nValue + mnSpinSize : nValue + mnSpinSize - nRemainder; else @@ -664,7 +644,7 @@ void NumericFormatter::FieldUp() void NumericFormatter::FieldDown() { sal_Int64 nValue = GetValue(); - sal_Int64 nRemainder = mbDisableRemainderFactor ? 0 : (nValue % mnSpinSize); + sal_Int64 nRemainder = nValue % mnSpinSize; if (nValue >= 0) nValue = (nRemainder == 0) ? nValue - mnSpinSize : nValue - nRemainder; else diff --git a/vcl/source/control/fmtfield.cxx b/vcl/source/control/fmtfield.cxx index d443f2c1a8e3..280bf06649f5 100644 --- a/vcl/source/control/fmtfield.cxx +++ b/vcl/source/control/fmtfield.cxx @@ -26,6 +26,7 @@ #include <vcl/settings.hxx> #include <svl/zformat.hxx> #include <vcl/fmtfield.hxx> +#include <vcl/weld.hxx> #include <i18nlangtag/languagetag.hxx> #include <com/sun/star/lang/Locale.hpp> #include <com/sun/star/util/SearchOptions.hpp> @@ -304,6 +305,7 @@ FormattedField::FormattedField(vcl::Window* pParent, WinBits nStyle) ,m_bEnableEmptyField(true) ,m_bAutoColor(false) ,m_bEnableNaN(false) + ,m_bDisableRemainderFactor(false) ,m_ValueState(valueDirty) ,m_dCurrentValue(0) ,m_dDefaultValue(0) @@ -859,35 +861,37 @@ void FormattedField::ImplSetValue(double dVal, bool bForce) m_ValueState = valueDouble; m_dCurrentValue = dVal; - OUString sNewText; - if (ImplGetFormatter()->IsTextFormat(m_nFormatKey)) + if (!m_aOutputHdl.IsSet() || !m_aOutputHdl.Call(*this)) { - // first convert the number as string in standard format - OUString sTemp; - ImplGetFormatter()->GetOutputString(dVal, 0, sTemp, &m_pLastOutputColor); - // then encode the string in the corresponding text format - ImplGetFormatter()->GetOutputString(sTemp, m_nFormatKey, sNewText, &m_pLastOutputColor); - } - else - { - if( IsUsingInputStringForFormatting()) + OUString sNewText; + if (ImplGetFormatter()->IsTextFormat(m_nFormatKey)) { - ImplGetFormatter()->GetInputLineString(dVal, m_nFormatKey, sNewText); + // first convert the number as string in standard format + OUString sTemp; + ImplGetFormatter()->GetOutputString(dVal, 0, sTemp, &m_pLastOutputColor); + // then encode the string in the corresponding text format + ImplGetFormatter()->GetOutputString(sTemp, m_nFormatKey, sNewText, &m_pLastOutputColor); } else { - ImplGetFormatter()->GetOutputString(dVal, m_nFormatKey, sNewText, &m_pLastOutputColor); + if( IsUsingInputStringForFormatting()) + { + ImplGetFormatter()->GetInputLineString(dVal, m_nFormatKey, sNewText); + } + else + { + ImplGetFormatter()->GetOutputString(dVal, m_nFormatKey, sNewText, &m_pLastOutputColor); + } } + ImplSetTextImpl(sNewText, nullptr); + DBG_ASSERT(CheckText(sNewText), "FormattedField::ImplSetValue : formatted string doesn't match the criteria !"); } - ImplSetTextImpl(sNewText, nullptr); m_ValueState = valueDouble; - DBG_ASSERT(CheckText(sNewText), "FormattedField::ImplSetValue : formatted string doesn't match the criteria !"); } bool FormattedField::ImplGetValue(double& dNewVal) { - dNewVal = m_dCurrentValue; if (m_ValueState == valueDouble) return true; @@ -897,34 +901,55 @@ bool FormattedField::ImplGetValue(double& dNewVal) if (sText.isEmpty()) return true; - DBG_ASSERT(ImplGetFormatter() != nullptr, "FormattedField::ImplGetValue : can't give you a current value without a formatter !"); + bool bUseExternalFormatterValue = false; + if (m_aInputHdl.IsSet()) + { + sal_Int64 nResult; + auto eState = m_aInputHdl.Call(&nResult); + bUseExternalFormatterValue = eState != TRISTATE_INDET; + if (bUseExternalFormatterValue) + { + if (eState == TRISTATE_TRUE) + { + dNewVal = nResult; + dNewVal /= weld::SpinButton::Power10(GetDecimalDigits()); + } + else + dNewVal = m_dCurrentValue; + } + } + + if (!bUseExternalFormatterValue) + { + DBG_ASSERT(ImplGetFormatter() != nullptr, "FormattedField::ImplGetValue : can't give you a current value without a formatter !"); - sal_uInt32 nFormatKey = m_nFormatKey; // IsNumberFormat changes the FormatKey! + sal_uInt32 nFormatKey = m_nFormatKey; // IsNumberFormat changes the FormatKey! - if (ImplGetFormatter()->IsTextFormat(nFormatKey) && m_bTreatAsNumber) - // for detection of values like "1,1" in fields that are formatted as text - nFormatKey = 0; + if (ImplGetFormatter()->IsTextFormat(nFormatKey) && m_bTreatAsNumber) + // for detection of values like "1,1" in fields that are formatted as text + nFormatKey = 0; - // special treatment for percentage formatting - if (ImplGetFormatter()->GetType(m_nFormatKey) == SvNumFormatType::PERCENT) - { - // the language of our format - LanguageType eLanguage = m_pFormatter->GetEntry(m_nFormatKey)->GetLanguage(); - // the default number format for this language - sal_uLong nStandardNumericFormat = m_pFormatter->GetStandardFormat(SvNumFormatType::NUMBER, eLanguage); - - sal_uInt32 nTempFormat = nStandardNumericFormat; - double dTemp; - if (m_pFormatter->IsNumberFormat(sText, nTempFormat, dTemp) && - SvNumFormatType::NUMBER == m_pFormatter->GetType(nTempFormat)) - // the string is equivalent to a number formatted one (has no % sign) -> append it - sText += "%"; - // (with this, a input of '3' becomes '3%', which then by the formatter is translated - // into 0.03. Without this, the formatter would give us the double 3 for an input '3', - // which equals 300 percent. + // special treatment for percentage formatting + if (ImplGetFormatter()->GetType(m_nFormatKey) == SvNumFormatType::PERCENT) + { + // the language of our format + LanguageType eLanguage = m_pFormatter->GetEntry(m_nFormatKey)->GetLanguage(); + // the default number format for this language + sal_uLong nStandardNumericFormat = m_pFormatter->GetStandardFormat(SvNumFormatType::NUMBER, eLanguage); + + sal_uInt32 nTempFormat = nStandardNumericFormat; + double dTemp; + if (m_pFormatter->IsNumberFormat(sText, nTempFormat, dTemp) && + SvNumFormatType::NUMBER == m_pFormatter->GetType(nTempFormat)) + // the string is equivalent to a number formatted one (has no % sign) -> append it + sText += "%"; + // (with this, a input of '3' becomes '3%', which then by the formatter is translated + // into 0.03. Without this, the formatter would give us the double 3 for an input '3', + // which equals 300 percent. + } + if (!ImplGetFormatter()->IsNumberFormat(sText, nFormatKey, dNewVal)) + return false; } - if (!ImplGetFormatter()->IsNumberFormat(sText, nFormatKey, dNewVal)) - return false; if (m_bHasMin && (dNewVal<m_dMinValue)) dNewVal = m_dMinValue; @@ -953,10 +978,34 @@ double FormattedField::GetValue() return m_dCurrentValue; } +void FormattedField::DisableRemainderFactor() +{ + m_bDisableRemainderFactor = true; +} + +bool FormattedField::set_property(const OString &rKey, const OUString &rValue) +{ + if (rKey == "digits") + SetDecimalDigits(rValue.toInt32()); + else + return SpinField::set_property(rKey, rValue); + return true; +} + void FormattedField::Up() { + auto nScale = weld::SpinButton::Power10(GetDecimalDigits()); + + sal_Int64 nValue = std::round(GetValue() * nScale); + sal_Int64 nSpinSize = std::round(m_dSpinSize * nScale); + sal_Int64 nRemainder = m_bDisableRemainderFactor ? 0 : nValue % nSpinSize; + if (nValue >= 0) + nValue = (nRemainder == 0) ? nValue + nSpinSize : nValue + nSpinSize - nRemainder; + else + nValue = (nRemainder == 0) ? nValue + nSpinSize : nValue - nRemainder; + // setValue handles under- and overflows (min/max) automatically - SetValue(GetValue() + m_dSpinSize); + SetValue(static_cast<double>(nValue) / nScale); SetModifyFlag(); Modify(); @@ -965,7 +1014,18 @@ void FormattedField::Up() void FormattedField::Down() { - SetValue(GetValue() - m_dSpinSize); + auto nScale = weld::SpinButton::Power10(GetDecimalDigits()); + + sal_Int64 nValue = std::round(GetValue() * nScale); + sal_Int64 nSpinSize = std::round(m_dSpinSize * nScale); + sal_Int64 nRemainder = m_bDisableRemainderFactor ? 0 : nValue % nSpinSize; + if (nValue >= 0) + nValue = (nRemainder == 0) ? nValue - nSpinSize : nValue - nRemainder; + else + nValue = (nRemainder == 0) ? nValue - nSpinSize : nValue - nSpinSize - nRemainder; + + // setValue handles under- and overflows (min/max) automatically + SetValue(static_cast<double>(nValue) / nScale); SetModifyFlag(); Modify(); @@ -1001,7 +1061,6 @@ void FormattedField::UseInputStringForFormatting() m_bUseInputStringForFormatting = true; } - DoubleNumericField::DoubleNumericField(vcl::Window* pParent, WinBits nStyle) : FormattedField(pParent, nStyle) { |