From 218c4988ccd842ce802d4d93f48e8a90b2ba8ee2 Mon Sep 17 00:00:00 2001 From: cxl Date: Mon, 1 May 2017 14:24:12 +0000 Subject: [PATCH] CtrlLib: Edit*Spin::RoundFromMin git-svn-id: svn://ultimatepp.org/upp/trunk@11047 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- uppsrc/Core/Convert.h | 14 ++++++++++++++ uppsrc/CtrlLib/EditCtrl.h | 26 +++++++++++++++++++------- 2 files changed, 33 insertions(+), 7 deletions(-) diff --git a/uppsrc/Core/Convert.h b/uppsrc/Core/Convert.h index 1bc9f07b9..5954876f6 100644 --- a/uppsrc/Core/Convert.h +++ b/uppsrc/Core/Convert.h @@ -68,6 +68,9 @@ public: int GetMin() const { return (int)minval; } int GetMax() const { return (int)maxval; } bool IsNotNull() const { return notnull; } + + static int GetDefaultMin() { return -INT_MAX; } + static int GetDefaultMax() { return INT_MAX; } #ifdef flagSO ConvertInt(int minval = -INT_MAX, int maxval = INT_MAX, bool notnull = false); @@ -88,6 +91,9 @@ struct ConvertInt64 : public ConvertInt { int64 GetMin() const { return minval; } int64 GetMax() const { return maxval; } + static int64 GetDefaultMin() { return -INT64_MAX; } + static int64 GetDefaultMax() { return INT64_MAX; } + #ifdef flagSO ConvertInt64(int64 minval = -INT64_MAX, int64 maxval = INT64_MAX, bool notnull = false); virtual ~ConvertInt64(); @@ -120,6 +126,9 @@ public: double GetMax() const { return maxval; } bool IsNotNull() const { return notnull; } + static double GetDefaultMin() { return DOUBLE_NULL_LIM; } + static double GetDefaultMax() { return -DOUBLE_NULL_LIM; } + ConvertDouble(double minval = DOUBLE_NULL_LIM, double maxval = -DOUBLE_NULL_LIM, bool notnull = false); #ifdef flagSO @@ -151,6 +160,8 @@ public: Date GetMax() const { return maxval; } bool IsNotNull() const { return notnull; } + static Date GetDefaultMin() { return Date::Low(); } + static Date GetDefaultMax() { return Date::High(); } ConvertDate(Date minval = Date::Low(), Date maxval = Date::High(), bool notnull = false); virtual ~ConvertDate(); @@ -190,6 +201,9 @@ public: bool IsTimeAlways() const { return timealways; } bool IsDayEnd() const { return dayend; } + static Time GetDefaultMin() { return ToTime(Date::Low()); } + static Time GetDefaultMax() { return ToTime(Date::High()); } + ConvertTime(Time minval = ToTime(Date::Low()), Time maxval = ToTime(Date::High()), bool notnull = false); virtual ~ConvertTime(); }; diff --git a/uppsrc/CtrlLib/EditCtrl.h b/uppsrc/CtrlLib/EditCtrl.h index 96d57bcee..78e99d6bd 100644 --- a/uppsrc/CtrlLib/EditCtrl.h +++ b/uppsrc/CtrlLib/EditCtrl.h @@ -340,19 +340,23 @@ template IncType WithSpin_DefaultIncValue() { return 1; } template <> inline double WithSpin_DefaultIncValue() { return 0.1; } template -void WithSpin_Add(DataType& value, IncType inc) +void WithSpin_Add(DataType& value, IncType inc, DataType min, bool roundfrommin) { value += inc; } template <> inline -void WithSpin_Add(double& value, double inc) { +void WithSpin_Add(double& value, double inc, double min, bool roundfrommin) { + if(roundfrommin) + value -= min; if(inc < 0) { inc = -inc; value = (ceil(value / inc - inc / 100) - 1) * inc; } else value = (floor(value / inc + inc / 100) + 1) * inc; + if(roundfrommin) + value += min; } template @@ -369,6 +373,7 @@ protected: private: SpinButtons sb; IncType inc; + bool roundfrommin; typedef WithSpin CLASSNAME; public: @@ -381,6 +386,8 @@ public: WithSpin& ShowSpin(bool s = true) { sb.Show(s); return *this; } bool IsSpinVisible() const { return sb.IsVisible(); } + + WithSpin& RoundFromMin(bool b = true) { roundfrommin = b; return *this; } SpinButtons& SpinButtonsObject() { return sb; } const SpinButtons& SpinButtonsObject() const { return sb; } @@ -419,6 +426,7 @@ void WithSpin::Init() Ctrl::AddFrame(sb); sb.inc.WhenRepeat = sb.inc.WhenAction = THISBACK(Inc); sb.dec.WhenRepeat = sb.dec.WhenAction = THISBACK(Dec); + roundfrommin = false; } template @@ -430,7 +438,7 @@ void WithSpin::Inc() } DataType d = Base::GetData(); if(!IsNull(d)) { - WithSpin_Add(d, inc); + WithSpin_Add(d, inc, Base::GetMin(), roundfrommin); if(IsNull(Base::GetMax()) || d <= Base::GetMax()) { Base::SetData(d); Ctrl::Action(); @@ -438,7 +446,9 @@ void WithSpin::Inc() } else { DataType min = Base::GetMin(); - if(!IsNull(min)) + if(IsNull(min) || min <= Base::GetDefaultMin()) + Base::SetData(0); + else Base::SetData(min); } Ctrl::SetFocus(); @@ -453,7 +463,7 @@ void WithSpin::Dec() } DataType d = Base::GetData(); if(!IsNull(d)) { - WithSpin_Add(d, -inc); + WithSpin_Add(d, -inc, Base::GetMin(), roundfrommin); if(IsNull(Base::GetMin()) || d >= Base::GetMin()) { Base::SetData(d); Ctrl::Action(); @@ -461,8 +471,10 @@ void WithSpin::Dec() } else { DataType max = Base::GetMax(); - if(!IsNull(max)) - Base::SetData(Base::maxval); + if(IsNull(max) || max >= Base::GetDefaultMax()) + Base::SetData(0); + else + Base::SetData(max); } Ctrl::SetFocus(); }