1 /** DGui project file.
2 
3 Copyright: Trogu Antonio Davide 2011-2013
4 
5 License: $(HTTP boost.org/LICENSE_1_0.txt, Boost License 1.0).
6 
7 Authors: Trogu Antonio Davide
8 */
9 module dguihub.trackbar;
10 
11 import dguihub.core.controls.subclassedcontrol;
12 
13 class TrackBar : SubclassedControl {
14    public Event!(Control, EventArgs) valueChanged;
15 
16    private int _minRange = 0;
17    private int _maxRange = 100;
18    private int _value = 0;
19    private int _lastValue = 0;
20 
21    @property public uint minRange() {
22       return this._minRange;
23    }
24 
25    @property public void minRange(uint mr) {
26       this._minRange = mr;
27 
28       if (this.created) {
29          this.sendMessage(TBM_SETRANGE, true, MAKELPARAM(this._minRange, this._maxRange));
30       }
31    }
32 
33    @property public uint maxRange() {
34       return this._maxRange;
35    }
36 
37    @property public void maxRange(uint mr) {
38       this._maxRange = mr;
39 
40       if (this.created) {
41          this.sendMessage(TBM_SETRANGE, true, MAKELPARAM(this._minRange, this._maxRange));
42       }
43    }
44 
45    @property public int value() {
46       if (this.created) {
47          return this.sendMessage(TBM_GETPOS, 0, 0);
48       }
49 
50       return this._value;
51    }
52 
53    @property public void value(int p) {
54       this._value = p;
55 
56       if (this.created) {
57          this.sendMessage(TBM_SETPOS, true, p);
58       }
59    }
60 
61    protected override void createControlParams(ref CreateControlParams ccp) {
62       ccp.superclassName = WC_TRACKBAR;
63       ccp.className = WC_DTRACKBAR;
64       this.setStyle(TBS_AUTOTICKS, true);
65 
66       assert(this._dock is DockStyle.fill, "TrackBar: Invalid Dock Style");
67 
68       if (this._dock is DockStyle.top || this._dock is DockStyle.bottom
69             || (this._dock is DockStyle.none && this._bounds.width >= this._bounds.height)) {
70          this.setStyle(TBS_HORZ, true);
71       } else if (this._dock is DockStyle.left || this._dock is DockStyle.right
72             || (this._dock is DockStyle.none && this._bounds.height < this._bounds.width)) {
73          this.setStyle(TBS_VERT, true);
74       }
75 
76       super.createControlParams(ccp);
77    }
78 
79    protected override void onHandleCreated(EventArgs e) {
80       this.sendMessage(TBM_SETRANGE, true, MAKELPARAM(this._minRange, this._maxRange));
81       this.sendMessage(TBM_SETTIC, 20, 0);
82       this.sendMessage(TBM_SETPOS, true, this._value);
83 
84       super.onHandleCreated(e);
85    }
86 
87    protected override void wndProc(ref Message m) {
88       if (m.msg == WM_MOUSEMOVE && (cast(MouseKeys)m.wParam) is MouseKeys.left
89             || m.msg == WM_KEYDOWN && ((cast(Keys)m.wParam) is Keys.left
90                || (cast(Keys)m.wParam) is Keys.up
91                || (cast(Keys)m.wParam) is Keys.right || (cast(Keys)m.wParam) is Keys.down)) {
92          int val = this.value;
93 
94          if (this._lastValue != val) {
95             this._lastValue = val; //Save last position.
96             this.onValueChanged(EventArgs.empty);
97          }
98       }
99 
100       super.wndProc(m);
101    }
102 
103    private void onValueChanged(EventArgs e) {
104       this.valueChanged(this, e);
105    }
106 }