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 CheckListBoxButton button = buttonMaps[(int)row];
173 bool wasChecked = button.checked;
174 button.checked = false;
175 button.buttonState = up;
176 if(wasChecked) NotifyChanged(master, this, row);
181 for(r = row.firstRow; r; r = r.next)
186 void UnsetChildren(DataRow row)
189 CheckListBoxButton button = buttonMaps[(int)row];
192 bool wasChecked = button.checked;
193 button.checked = true;
194 button.buttonState = up;
195 if(!wasChecked) NotifyChanged(master, this, row);
198 for(r = row.firstRow; r; r = r.next)
200 Iterator<DataRow> it { rowChecks };
207 void SetCheck(DataRow row, bool checked)
210 bool wasChecked = false;
212 for(parent = row; parent; parent = parent.parent)
214 if(rowChecks.Find(parent)) { wasChecked = true; break; }
216 if(checked != wasChecked)
218 modifiedDocument = true;
219 // NotifyChanged(master, this, row);
223 // Check if all siblings are checked, if so go up until we reach a row not fully checked
227 for(r = rr.parent.firstRow; r; r = r.next)
229 if(r != rr && !rowChecks.Find(r))
232 if(r || !row.parent) break;
236 // Take out all children from rowChecks, checking them all
241 for(parent = row.parent; parent; parent = parent.parent)
244 CheckListBoxButton button = buttonMaps[(int)parent];
247 button.checked = true;
248 button.buttonState = down;
260 Iterator<DataRow> it { rowChecks };
269 for(r = row.parent.firstRow; r; r = r.next)
278 for(; parent; parent = parent.parent)
280 CheckListBoxButton button = buttonMaps[(int)parent];
283 if(CheckPartialChecks(parent))
285 button.checked = true;
286 button.buttonState = down;
290 button.checked = false;
291 button.buttonState = up;
297 NotifyChanged(master, this, row);
301 bool NotifyKeyDown(CheckListBox listBox, DataRow row, Key key, unichar ch)
305 listBox.ToggleCheck(row);
311 bool OnKeyHit(Key key, unichar ch)
315 return ListBox::OnKeyHit(key, ch);
318 bool NotifyDoubleClick(CheckListBox listBox, int x, int y, Modifiers mods)
320 listBox.OnLeftButtonDown(x, y, mods);
324 bool NotifyReclick(CheckListBox listBox, DataRow row, Modifiers mods)
326 if(row == listBox.currentRow)
327 listBox.ToggleCheck(row);
331 // virtual void Window::NotifyChecked(CheckListBox listBox, DataRow row);