1 // Written by Christopher E. Miller
2 // See the included license.txt for copyright and license details.
3 
4 
5 ///
6 module dfl.progressbar;
7 
8 private import dfl.base, dfl.control, dfl.drawing, dfl.application,
9 	dfl.event;
10 private import dfl.internal.winapi;
11 
12 
13 private extern(Windows) void _initProgressbar();
14 
15 
16 ///
17 class ProgressBar: ControlSuperClass // docmain
18 {
19 	this()
20 	{
21 		_initProgressbar();
22 		
23 		wexstyle |= WS_EX_CLIENTEDGE;
24 		wclassStyle = progressbarClassStyle;
25 	}
26 	
27 	
28 	///
29 	final @property void maximum(int max) // setter
30 	{
31 		if(max <= 0 /+ || max < _min +/)
32 		{
33 			//bad_max:
34 			//throw new DflException("Unable to set progress bar maximum value");
35 			if(max)
36 				return;
37 		}
38 		
39 		if(created)
40 		{
41 			prevwproc(PBM_SETRANGE, 0, MAKELPARAM(_min, max));
42 		}
43 		
44 		_max = max;
45 		
46 		if(_val > max)
47 			_val = max; // ?
48 	}
49 	
50 	/// ditto
51 	final @property int maximum() // getter
52 	{
53 		return _max;
54 	}
55 	
56 	
57 	///
58 	final @property void minimum(int min) // setter
59 	{
60 		if(min < 0 /+ || min > _max +/)
61 		{
62 			//bad_min:
63 			//throw new DflException("Unable to set progress bar minimum value");
64 			return;
65 		}
66 		
67 		if(created)
68 		{
69 			prevwproc(PBM_SETRANGE, 0, MAKELPARAM(min, _max));
70 		}
71 		
72 		_min = min;
73 		
74 		if(_val < min)
75 			_val = min; // ?
76 	}
77 	
78 	/// ditto
79 	final @property int minimum() // getter
80 	{
81 		return _min;
82 	}
83 	
84 	
85 	///
86 	final @property void step(int stepby) // setter
87 	{
88 		if(stepby <= 0 /+ || stepby > _max +/)
89 		{
90 			//bad_max:
91 			//throw new DflException("Unable to set progress bar step value");
92 			if(stepby)
93 				return;
94 		}
95 		
96 		if(created)
97 		{
98 			prevwproc(PBM_SETSTEP, stepby, 0);
99 		}
100 		
101 		_step = stepby;
102 	}
103 	
104 	/// ditto
105 	final @property int step() // getter
106 	{
107 		return _step;
108 	}
109 	
110 	
111 	///
112 	final @property void value(int setval) // setter
113 	{
114 		if(setval < _min || setval > _max)
115 		{
116 			//throw new DflException("Progress bar value out of minimum/maximum range");
117 			//return;
118 			if(setval > _max)
119 				setval = _max;
120 			else
121 				setval = _min;
122 		}
123 		
124 		if(created)
125 		{
126 			prevwproc(PBM_SETPOS, setval, 0);
127 		}
128 		
129 		_val = setval;
130 	}
131 	
132 	/// ditto
133 	final @property int value() // getter
134 	{
135 		return _val;
136 	}
137 	
138 	
139 	///
140 	final void increment(int incby)
141 	{
142 		int newpos = _val + incby;
143 		if(newpos < _min)
144 			newpos = _min;
145 		if(newpos > _max)
146 			newpos = _max;
147 		
148 		if(created)
149 		{
150 			prevwproc(PBM_SETPOS, newpos, 0);
151 		}
152 		
153 		_val = newpos;
154 	}
155 	
156 	
157 	///
158 	final void performStep()
159 	{
160 		increment(_step);
161 	}
162 	
163 	
164 	protected override void onHandleCreated(EventArgs ea)
165 	{
166 		super.onHandleCreated(ea);
167 		
168 		if(_min != MIN_INIT || _max != MAX_INIT)
169 		{
170 			prevwproc(PBM_SETRANGE, 0, MAKELPARAM(_min, _max));
171 		}
172 		
173 		if(_step != STEP_INIT)
174 		{
175 			prevwproc(PBM_SETSTEP, _step, 0);
176 		}
177 		
178 		if(_val != VAL_INIT)
179 		{
180 			prevwproc(PBM_SETPOS, _val, 0);
181 		}
182 	}
183 	
184 	
185 	protected override @property Size defaultSize() // getter
186 	{
187 		return Size(100, 23);
188 	}
189 	
190 	
191 	static @property Color defaultForeColor() // getter
192 	{
193 		return SystemColors.highlight;
194 	}
195 	
196 	
197 	protected override void createParams(ref CreateParams cp)
198 	{
199 		super.createParams(cp);
200 		
201 		cp.className = PROGRESSBAR_CLASSNAME;
202 	}
203 	
204 	
205 	protected override void prevWndProc(ref Message msg)
206 	{
207 		//msg.result = CallWindowProcA(progressbarPrevWndProc, msg.hWnd, msg.msg, msg.wParam, msg.lParam);
208 		msg.result = dfl.internal.utf.callWindowProc(progressbarPrevWndProc, msg.hWnd, msg.msg, msg.wParam, msg.lParam);
209 	}
210 	
211 	
212 	private:
213 	
214 	enum MIN_INIT = 0;
215 	enum MAX_INIT = 100;
216 	enum STEP_INIT = 10;
217 	enum VAL_INIT = 0;
218 	
219 	int _min = MIN_INIT, _max = MAX_INIT, _step = STEP_INIT, _val = VAL_INIT;
220 	
221 	
222 	package:
223 	final:
224 	LRESULT prevwproc(UINT msg, WPARAM wparam, LPARAM lparam)
225 	{
226 		//return CallWindowProcA(progressbarPrevWndProc, hwnd, msg, wparam, lparam);
227 		return dfl.internal.utf.callWindowProc(progressbarPrevWndProc, hwnd, msg, wparam, lparam);
228 	}
229 }
230