-
Notifications
You must be signed in to change notification settings - Fork 6
/
RadioGroup.mqh
377 lines (374 loc) · 27.5 KB
/
RadioGroup.mqh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
//+------------------------------------------------------------------+
//| RadioGroup.mqh |
//| Copyright 2009-2016, MetaQuotes Software Corp. |
//| http://www.mql5.com |
//+------------------------------------------------------------------+
#include <Controls\WndClient.mqh>
#include "RadioButton.mqh"
#include <Arrays\ArrayString.mqh>
#include <Arrays\ArrayLong.mqh>
//+------------------------------------------------------------------+
//| Class CRadioGroup |
//| Usage: view and edit radio buttons |
//+------------------------------------------------------------------+
class CRadioGroup : public CWndClient
{
private:
//--- dependent controls
CRadioButton m_rows[]; // array of the row objects
//--- set up
int m_offset; // index of first visible row in array of rows
int m_total_view; // number of visible rows
int m_item_height; // height of visible row
//--- data
CArrayString m_strings; // array of rows
CArrayLong m_values; // array of values
int m_current; // index of current row in array of rows
public:
CRadioGroup(void);
~CRadioGroup(void);
//--- create
virtual bool Create(const long chart,const string name,const int subwin,const int x1,const int y1,const int x2,const int y2);
virtual void Destroy(const int reason=0);
//--- chart event handler
virtual bool OnEvent(const int id,const long &lparam,const double &dparam,const string &sparam);
//--- fill
virtual bool AddItem(const string item,const long value=0);
//--- data
long Value(void) const { return(m_values.At(m_current)); }
bool Value(const long value);
bool ValueCheck(long value) const;
//--- state
virtual bool Show(void);
virtual void RedrawButtonStates(void);
//--- methods for working with files
virtual bool Save(const int file_handle);
virtual bool Load(const int file_handle);
protected:
//--- create dependent controls
bool CreateButton(const int index);
//--- handlers of the dependent controls events
virtual bool OnVScrollShow(void);
virtual bool OnVScrollHide(void);
virtual bool OnScrollLineDown(void);
virtual bool OnScrollLineUp(void);
virtual bool OnChangeItem(const int index);
//--- redraw
bool Redraw(void);
bool RowState(const int index,const bool select);
void Select(const int index);
};
//+------------------------------------------------------------------+
//| Common handler of chart events |
//+------------------------------------------------------------------+
EVENT_MAP_BEGIN(CRadioGroup)
ON_INDEXED_EVENT(ON_CHANGE,m_rows,OnChangeItem)
EVENT_MAP_END(CWndClient)
//+------------------------------------------------------------------+
//| Constructor |
//+------------------------------------------------------------------+
CRadioGroup::CRadioGroup(void) : m_offset(0),
m_total_view(0),
m_item_height(CONTROLS_LIST_ITEM_HEIGHT),
m_current(CONTROLS_INVALID_INDEX)
{
}
//+------------------------------------------------------------------+
//| Destructor |
//+------------------------------------------------------------------+
CRadioGroup::~CRadioGroup(void)
{
}
//+------------------------------------------------------------------+
//| Create a control |
//+------------------------------------------------------------------+
bool CRadioGroup::Create(const long chart,const string name,const int subwin,const int x1,const int y1,const int x2,const int y2)
{
//--- determine the number of visible rows
m_total_view=(y2-y1)/m_item_height;
//--- check the number of visible rows
if(m_total_view<1)
return(false);
//--- call method of the parent class
if(!CWndClient::Create(chart,name,subwin,x1,y1,x2,y2))
return(false);
//--- set up
if(!m_background.ColorBackground(CONTROLS_RADIOGROUP_COLOR_BG))
return(false);
if(!m_background.ColorBorder(CONTROLS_RADIOGROUP_COLOR_BORDER))
return(false);
//--- create dependent controls
ArrayResize(m_rows,m_total_view);
for(int i=0;i<m_total_view;i++)
if(!CreateButton(i))
return(false);
//--- succeed
return(true);
}
//+------------------------------------------------------------------+
//| Delete group of controls |
//+------------------------------------------------------------------+
void CRadioGroup::Destroy(const int reason)
{
//--- call of the method of the parent class
CWndClient::Destroy(reason);
//--- clear items
m_strings.Clear();
m_values.Clear();
}
//+------------------------------------------------------------------+
//| Create "row" |
//+------------------------------------------------------------------+
bool CRadioGroup::CreateButton(const int index)
{
//--- calculate coordinates
int x1=CONTROLS_BORDER_WIDTH;
int y1=CONTROLS_BORDER_WIDTH+m_item_height*index;
int x2=Width()-CONTROLS_BORDER_WIDTH;
int y2=y1+m_item_height;
//--- create
if(!m_rows[index].Create(m_chart_id,m_name+"Item"+IntegerToString(index),m_subwin,x1,y1,x2,y2))
return(false);
if(!m_rows[index].Text(""))
return(false);
if(!Add(m_rows[index]))
return(false);
m_rows[index].Hide();
//---
return(true);
}
//+------------------------------------------------------------------+
//| Add item (row) |
//+------------------------------------------------------------------+
bool CRadioGroup::AddItem(const string item,const long value)
{
//--- check value
if(value!=0 && !ValueCheck(value))
return(false);
//--- add
if(!m_strings.Add(item))
return(false);
if(!m_values.Add(value))
return(false);
//--- number of items
int total=m_strings.Total();
//--- exit if number of items does not exceed the size of visible area
if(total<m_total_view+1)
{
if(IS_VISIBLE && total!=0)
m_rows[total-1].Show();
return(Redraw());
}
//--- if number of items exceeded the size of visible area
if(total==m_total_view+1)
{
//--- enable vertical scrollbar
if(!VScrolled(true))
return(false);
//--- and immediately make it invisible (if needed)
if(!IS_VISIBLE)
m_scroll_v.Visible(false);
}
//--- set up the scrollbar
m_scroll_v.MaxPos(m_strings.Total()-m_total_view);
//--- redraw
return(Redraw());
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CRadioGroup::Value(const long value)
{
//--- çيà÷هيèه نîëويî ïًèٌٌٍٍَâîâàٍü â يàلîًه
int total=m_values.Total();
//---
for(int i=0;i<total;i++)
if(m_values.At(i)==value)
Select(i+m_offset);
//---
return(true);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CRadioGroup::ValueCheck(long value) const
{
//--- ïًîâهًےهىîه çيà÷هيèه يه نîëويî نَلëèًîâàٍü َوه ٌَùهٌٍâَùهه
int total=m_values.Total();
//---
for(int i=0;i<total;i++)
if(m_values.At(i)==value)
return(false);
//---
return(true);
}
//+------------------------------------------------------------------+
//| Makes the group visible |
//+------------------------------------------------------------------+
bool CRadioGroup::Show(void)
{
//--- call of the method of the parent class
if(!CWndClient::Show())
return(false);
//--- loop by rows
int total=m_values.Total();
for(int i=total;i<m_total_view;i++)
m_rows[i].Hide();
//--- handled
return(true);
}
//+------------------------------------------------------------------+
//| Recheck button object state |
//+------------------------------------------------------------------+
void CRadioGroup::RedrawButtonStates(void)
{
//--- loop by "rows"
for(int i=0;i<m_total_view;i++)
{
if(i>=ArraySize(m_rows))
break;
//--- select/unselect
m_rows[i].State(m_current==i+m_offset);
}
//---
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CRadioGroup::Save(const int file_handle)
{
//--- check
if(file_handle==INVALID_HANDLE)
return(false);
//---
FileWriteLong(file_handle,Value());
//--- succeed
return(true);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
bool CRadioGroup::Load(const int file_handle)
{
//--- check
if(file_handle==INVALID_HANDLE)
return(false);
//---
if(!FileIsEnding(file_handle))
Value(FileReadLong(file_handle));
//--- succeed
return(true);
}
//+------------------------------------------------------------------+
//| Sett current item |
//+------------------------------------------------------------------+
void CRadioGroup::Select(const int index)
{
//--- disable the "ON" state
if(m_current!=-1)
RowState(m_current-m_offset,false);
//--- enable the "ON" state
if(index!=-1)
RowState(index-m_offset,true);
//--- save value
m_current=index;
}
//+------------------------------------------------------------------+
//| Redraw |
//+------------------------------------------------------------------+
bool CRadioGroup::Redraw(void)
{
//--- loop by "rows"
for(int i=0;i<m_total_view;i++)
{
//--- copy text
if(!m_rows[i].Text(m_strings.At(i+m_offset)))
return(false);
//--- select
if(!RowState(i,(m_current==i+m_offset)))
return(false);
}
//--- succeed
return(true);
}
//+------------------------------------------------------------------+
//| Change state |
//+------------------------------------------------------------------+
bool CRadioGroup::RowState(const int index,const bool select)
{
//--- check index
if(index<0 || index>=ArraySize(m_rows))
return(true);
//--- change state
return(m_rows[index].State(select));
}
//+------------------------------------------------------------------+
//| Handler of the "Show vertical scrollbar" event |
//+------------------------------------------------------------------+
bool CRadioGroup::OnVScrollShow(void)
{
//--- loop by "rows"
for(int i=0;i<m_total_view;i++)
{
//--- resize "rows" according to shown vertical scrollbar
m_rows[i].Width(Width()-(CONTROLS_SCROLL_SIZE+CONTROLS_BORDER_WIDTH));
}
//--- check visibility
if(!IS_VISIBLE)
{
m_scroll_v.Visible(false);
return(true);
}
//---
return(true);
}
//+------------------------------------------------------------------+
//| Handler of the "Hide vertical scrollbar" event |
//+------------------------------------------------------------------+
bool CRadioGroup::OnVScrollHide(void)
{
//--- check visibility
if(!IS_VISIBLE)
return(true);
//--- loop by "rows"
for(int i=0;i<m_total_view;i++)
{
//--- resize "rows" according to hidden vertical scroll bar
m_rows[i].Width(Width()-CONTROLS_BORDER_WIDTH);
}
//---
return(true);
}
//+------------------------------------------------------------------+
//| Handler of the "Scroll up for one row" event |
//+------------------------------------------------------------------+
bool CRadioGroup::OnScrollLineUp(void)
{
//--- get new offset
m_offset=m_scroll_v.CurrPos();
//--- redraw
return(Redraw());
}
//+------------------------------------------------------------------+
//| Handler of the "Scroll down for one row" event |
//+------------------------------------------------------------------+
bool CRadioGroup::OnScrollLineDown(void)
{
//--- get new offset
m_offset=m_scroll_v.CurrPos();
//--- redraw
return(Redraw());
}
//+------------------------------------------------------------------+
//| Handler of changing a "row" state |
//+------------------------------------------------------------------+
bool CRadioGroup::OnChangeItem(const int index)
{
//--- select "row"
Select(index+m_offset);
//--- send notification
EventChartCustom(INTERNAL_EVENT,ON_CHANGE,m_id,0.0,m_name);
//--- handled
return(true);
}
//+------------------------------------------------------------------+