3 class CheckListBoxButton : Button
5 isCheckbox = true, inactive = true, size = { 12, 12 };
7 bool CheckListBox::NotifyPushed(Button button, int x, int y, Modifiers mods)
9 currentRow = (DataRow)button.id;
10 ToggleCheck(currentRow);
14 bool CheckListBox::NotifyReleased(Button button, int x, int y, Modifiers mods)
19 bool OnMouseOver(int x, int y, Modifiers mods)
25 bool OnMouseLeave(Modifiers mods)
32 class CheckListBox : ListBox
34 Map<int, CheckListBoxButton> buttonMaps { };
35 AVLTree<DataRow> rowChecks { };
40 fullRowSelect = false, collapseControl = true, treeBranches = true, rootCollapseButton = true,
46 buttonMaps.RemoveAll();
49 bool NotifyCollapse(CheckListBox listBox, DataRow row, bool collapsed)
52 for(r = row.firstRow; r && r != row; )
56 MapIterator<int, Button> it { map = listBox.buttonMaps };
57 if(it.Index((int)r, false))
59 Button checkBox = it.data;
69 listBox.SetupButtons(r, false);
71 if(r.firstRow && !r.collapsed)
74 for(; r != row; r = r.parent)
75 if(r.next) { r = r.next; break; }
77 for(r = row.GetNextRow(); r; r = r.GetNextRow())
79 Button checkBox = listBox.buttonMaps[(int)r];
80 checkBox.position.y = 1 + (r.index + listBox.hasHeader) * listBox.rowHeight;
85 bool CheckPartialChecks(DataRow row)
88 for(r = row.firstRow; r; r = r.next)
90 if(rowChecks.Find(r) || CheckPartialChecks(r))
96 void SetupButtons(DataRow row, bool recurse)
99 CheckListBoxButton button;
100 int indent = checkIndent;
102 for(parent = row.parent; parent; parent = parent.parent) indent += 20;
103 button = buttonMaps[(int)row];
104 if(!button) button = CheckListBoxButton { this };
105 button.position = { 2 + indent, 1+(row.index + hasHeader) * rowHeight };
106 button.id = (int)row;
108 for(parent = row; parent; parent = parent.parent) if(rowChecks.Find(parent)) break;
111 button.checked = true;
112 button.buttonState = up;
116 button.checked = CheckPartialChecks(row);
117 button.buttonState = button.checked ? down : up;
120 buttonMaps[(int)row] = button;
121 if(recurse && !row.collapsed)
124 for(r = row.firstRow; r; r = r.next)
125 SetupButtons(r, recurse);
132 for(row = firstRow; row; row = row.next)
133 SetupButtons(row, true);
138 if(ListBox::OnCreate())
142 buttonMaps.RemoveAll();
144 for(row = firstRow; row; row = row.next)
145 SetupButtons(row, true);
151 void ToggleCheck(DataRow row)
153 CheckListBoxButton checkBox = buttonMaps[(int)row];
156 bool checked = false;
158 for(r = row; r; r = r.parent)
159 if(rowChecks.Find(r))
164 SetCheck(row, !checked);
168 void UncheckBoxes(DataRow row)
170 if(!row.parent || !row.parent.collapsed)
172 CheckListBoxButton button = buttonMaps[(int)row];
175 bool wasChecked = button.checked;
176 button.checked = false;
177 button.buttonState = up;
180 // if(!row.collapsed)
183 for(r = row.firstRow; r; r = r.next)
186 NotifyChanged(master, this, row);
189 void UnsetChildren(DataRow row)
192 CheckListBoxButton button = buttonMaps[(int)row];
195 bool wasChecked = button.checked;
196 button.checked = true;
197 button.buttonState = up;
200 for(r = row.firstRow; r; r = r.next)
202 Iterator<DataRow> it { rowChecks };
207 NotifyChanged(master, this, row);
210 void SetCheck(DataRow row, bool checked)
213 bool wasChecked = false;
215 for(parent = row; parent; parent = parent.parent)
217 if(rowChecks.Find(parent)) { wasChecked = true; break; }
219 if(checked != wasChecked)
221 modifiedDocument = true;
222 // NotifyChanged(master, this, row);
226 // Check if all siblings are checked, if so go up until we reach a row not fully checked
230 for(r = rr.parent.firstRow; r; r = r.next)
232 if(r != rr && !rowChecks.Find(r))
235 if(r || !row.parent) break;
241 // Take out all children from rowChecks, checking them all
244 for(parent = row.parent; parent; parent = parent.parent)
247 CheckListBoxButton button = buttonMaps[(int)parent];
250 button.checked = true;
251 button.buttonState = down;
261 Iterator<DataRow> it { rowChecks };
270 for(r = row.parent.firstRow; r; r = r.next)
280 for(parent = row.parent; parent; parent = parent.parent)
282 CheckListBoxButton button = buttonMaps[(int)parent];
285 if(CheckPartialChecks(parent))
287 button.checked = true;
288 button.buttonState = down;
292 button.checked = false;
293 button.buttonState = up;
299 NotifyChanged(master, this, row);
303 bool NotifyKeyDown(CheckListBox listBox, DataRow row, Key key, unichar ch)
307 listBox.ToggleCheck(row);
313 bool OnKeyHit(Key key, unichar ch)
317 return ListBox::OnKeyHit(key, ch);
320 bool NotifyDoubleClick(CheckListBox listBox, int x, int y, Modifiers mods)
322 listBox.OnLeftButtonDown(x, y, mods);
326 bool NotifyReclick(CheckListBox listBox, DataRow row, Modifiers mods)
328 if(row == listBox.currentRow)
329 listBox.ToggleCheck(row);
334 bool IsChecked(DataRow row)
337 for(parent = row; parent; parent = parent.parent) if(rowChecks.Find(parent)) return true;
341 // virtual void Window::NotifyChecked(CheckListBox listBox, DataRow row);