WithSpin.cpp

Jan DolinĂ¡r, 10/16/2012 06:34 PM

Download (3.12 KB)

 
1
template <class DataType, class Cv>
2
class WithSpin : public EditMinMax<DataType, Cv>
3
{
4
public:
5
        virtual void MouseWheel(Point p, int zdelta, dword keyflags);
6
        virtual bool Key(dword key, int repcnt);
7

    
8
protected:
9
        void            Inc();
10
        void            Dec();
11
        void            Init();
12

    
13
private:
14
        SpinButtons     sb;
15
        DataType        inc;
16

    
17
        typedef WithSpin CLASSNAME;
18
public:
19

    
20
        WithSpin& SetInc(DataType _inc = 1)    { inc = _inc; return *this; }
21
        DataType          GetInc() const               { return inc; }
22

    
23
        WithSpin& OnSides(bool b = true)       { sb.OnSides(b); return *this; }
24
        bool            IsOnSides() const            { return sb.IsOnSides(); }
25
        
26
        WithSpin& ShowSpin(bool s = true)      { sb.Show(s); return *this; }
27
        bool            IsSpinVisible() const        { return sb.IsVisible(); }
28

    
29
        SpinButtons&       SpinButtonsObject()       { return sb; }
30
        const SpinButtons& SpinButtonsObject() const { return sb; }
31

    
32
        WithSpin();
33
        WithSpin(DataType inc);
34
        WithSpin(DataType min, DataType max, DataType inc);
35
        virtual ~WithSpin();
36
};
37

    
38
template <class DataType, class Cv>
39
WithSpin<DataType, Cv>::WithSpin() : inc(1) { Init(); }
40
template <class DataType, class Cv>
41
WithSpin<DataType, Cv>::WithSpin(DataType inc) : inc(inc) { Init(); }
42
template <class DataType, class Cv>
43
WithSpin<DataType, Cv>::WithSpin(DataType min, DataType max, DataType inc) : EditValue<DataType, Cv>(min, max), inc(inc) { Init(); }
44

    
45
template <class DataType, class Cv>
46
WithSpin<DataType, Cv>::~WithSpin() {}
47

    
48
template <class DataType, class Cv>
49
void WithSpin<DataType, Cv>::Init()
50
{
51
        Ctrl::AddFrame(sb);
52
        sb.inc.WhenRepeat = sb.inc.WhenAction = THISBACK(Inc);
53
        sb.dec.WhenRepeat = sb.dec.WhenAction = THISBACK(Dec);
54
}
55

    
56
template <class DataType, class Cv>
57
void WithSpin<DataType, Cv>::Inc()
58
{
59
        if(Ctrl::IsReadOnly())
60
        {
61
                BeepExclamation();
62
                return;
63
        }
64
        DataType d = EditMinMax<DataType, Cv>::GetData();
65
        if(!IsNull(d))
66
        {
67
                d = (floor(d / inc + 1e-3) + 1) * inc;
68
                if(IsNull(Cv::GetMax()) || d <= Cv::GetMax())
69
                {
70
                        EditMinMax<DataType, Cv>::SetData(d);
71
                        Ctrl::Action();
72
                }
73
        }
74
        else if(Cv::minval != DOUBLE_NULL_LIM)
75
                EditMinMax<DataType, Cv>::SetData(Cv::minval);
76
        Ctrl::SetFocus();
77
}
78

    
79
template <class DataType, class Cv>
80
void WithSpin<DataType, Cv>::Dec()
81
{
82
        if(Ctrl::IsReadOnly())
83
        {
84
                BeepExclamation();
85
                return;
86
        }
87
        DataType d = EditMinMax<DataType, Cv>::GetData();
88
        if(!IsNull(d))
89
        {
90
                d = (ceil(d / inc - 1e-3) - 1) * inc;
91
                if(IsNull(Cv::GetMin()) || d >= Cv::GetMin())
92
                {
93
                        EditMinMax<DataType, Cv>::SetData(d);
94
                        Ctrl::Action();
95
                }
96
        }
97
        else if(Cv::maxval != -DOUBLE_NULL_LIM)
98
                EditMinMax<DataType, Cv>::SetData(Cv::maxval);
99
        Ctrl::SetFocus();
100
}
101

    
102
template <class DataType, class Cv>
103
bool WithSpin<DataType, Cv>::Key(dword key, int repcnt)
104
{
105
        if(key == K_UP)   { Inc(); return true; }
106
        if(key == K_DOWN) { Dec(); return true; }
107
        return EditMinMax<DataType, Cv>::Key(key, repcnt);
108
}
109

    
110
template <class DataType, class Cv>
111
void WithSpin<DataType, Cv>::MouseWheel(Point, int zdelta, dword)
112
{
113
        if(zdelta < 0)
114
                Dec();
115
        else
116
                Inc();
117
}
118

    
119
typedef WithSpin<int, ConvertInt>       EditIntSpin;
120
typedef WithSpin<int64, ConvertInt64>   EditInt64Spin;
121
typedef WithSpin<double, ConvertDouble> EditDoubleSpin;