7 class CheckListBoxButton : Button
9 isCheckbox = true, inactive = true, size = { 12, 12 };
11 bool CheckListBox::NotifyPushed(Button button, int x, int y, Modifiers mods)
13 currentRow = (DataRow)button.id;
14 ToggleCheck(currentRow);
18 bool CheckListBox::NotifyReleased(Button button, int x, int y, Modifiers mods)
23 bool OnMouseOver(int x, int y, Modifiers mods)
29 bool OnMouseLeave(Modifiers mods)
36 class CheckListBox : ListBox
38 Map<uintptr, CheckListBoxButton> buttonMaps { };
39 AVLTree<DataRow> rowChecks { };
40 AVLTree<DataRow> rowDisabled { };
45 fullRowSelect = false, collapseControl = true, treeBranches = true, rootCollapseButton = true,
51 buttonMaps.RemoveAll();
54 bool NotifyCollapse(CheckListBox listBox, DataRow row, bool collapsed)
57 for(r = row.firstRow; r && r != row; )
61 MapIterator<uintptr, Button> it { map = listBox.buttonMaps };
62 if(it.Index((uintptr)r, false))
64 Button checkBox = it.data;
74 listBox.SetupButtons(r, false);
76 if(r.firstRow && !r.collapsed)
79 for(; r != row; r = r.parent)
80 if(r.next) { r = r.next; break; }
82 for(r = row.GetNextRow(); r; r = r.GetNextRow())
84 Button checkBox = listBox.buttonMaps[(uintptr)r];
86 checkBox.position.y = 1 + (r.index + listBox.hasHeader) * listBox.rowHeight;
91 bool CheckPartialChecks(DataRow row)
94 for(r = row.firstRow; r; r = r.next)
96 if(rowChecks.Find(r) || CheckPartialChecks(r))
102 void SetupButtons(DataRow row, bool recurse)
105 CheckListBoxButton button;
106 int indent = checkIndent;
108 for(parent = row.parent; parent; parent = parent.parent) indent += 20;
109 button = buttonMaps[(uintptr)row];
110 if(!button) button = CheckListBoxButton { this };
111 button.position = { 2 + indent, 1+(row.index + hasHeader) * rowHeight };
112 button.id = (uintptr)row;
114 for(parent = row; parent; parent = parent.parent) if(rowChecks.Find(parent)) break;
117 button.checked = true;
118 button.buttonState = up;
122 button.checked = CheckPartialChecks(row);
123 button.buttonState = button.checked ? down : up;
125 if(rowDisabled.Find(row))
126 button.disabled = true;
128 buttonMaps[(uintptr)row] = button;
129 if(recurse && !row.collapsed)
132 for(r = row.firstRow; r; r = r.next)
133 SetupButtons(r, recurse);
140 for(row = firstRow; row; row = row.next)
141 SetupButtons(row, true);
146 if(ListBox::OnCreate())
150 buttonMaps.RemoveAll();
152 for(row = firstRow; row; row = row.next)
153 SetupButtons(row, true);
159 void ToggleCheck(DataRow row)
161 CheckListBoxButton checkBox = buttonMaps[(uintptr)row];
162 if(checkBox && !checkBox.disabled)
164 bool checked = false;
166 for(r = row; r; r = r.parent)
167 if(rowChecks.Find(r))
172 SetCheck(row, !checked);
176 void UncheckBoxes(DataRow row)
178 if(!row.parent || !row.parent.collapsed)
180 CheckListBoxButton button = buttonMaps[(uintptr)row];
183 bool wasChecked = button.checked;
184 button.checked = false;
185 button.buttonState = up;
188 // if(!row.collapsed)
191 for(r = row.firstRow; r; r = r.next)
194 NotifyChecked(master, this, row);
197 void UnsetChildren(DataRow row)
200 CheckListBoxButton button = buttonMaps[(uintptr)row];
203 button.checked = true;
204 button.buttonState = up;
207 for(r = row.firstRow; r; r = r.next)
209 Iterator<DataRow> it { rowChecks };
214 NotifyChecked(master, this, r);
218 void SetCheck(DataRow row, bool checked)
221 bool wasChecked = false;
223 for(parent = row; parent; parent = parent.parent)
225 if(rowChecks.Find(parent)) { wasChecked = true; break; }
227 if(checked != wasChecked)
229 modifiedDocument = true;
230 // NotifyChecked(master, this, row);
234 // Check if all siblings are checked, if so go up until we reach a row not fully checked
238 for(r = rr.parent.firstRow; r; r = r.next)
240 if(r != rr && !rowChecks.Find(r))
243 if(r || !rr.parent) break;
249 // Take out all children from rowChecks, checking them all
252 NotifyChecked(master, this, rr);
254 for(parent = rr.parent; parent; parent = parent.parent)
256 CheckListBoxButton button = buttonMaps[(uintptr)parent];
259 button.checked = true;
260 button.buttonState = down;
262 NotifyChecked(master, this, parent);
273 Iterator<DataRow> it { rowChecks };
282 for(r = rr.parent.firstRow; r; r = r.next)
293 for(; parent; parent = parent.parent)
295 CheckListBoxButton button = buttonMaps[(uintptr)parent];
298 if(CheckPartialChecks(parent))
300 button.checked = true;
301 button.buttonState = down;
305 button.checked = false;
306 button.buttonState = up;
309 NotifyChecked(master, this, parent);
316 bool NotifyKeyDown(CheckListBox listBox, DataRow row, Key key, unichar ch)
320 listBox.ToggleCheck(row);
326 bool OnKeyHit(Key key, unichar ch)
330 return ListBox::OnKeyHit(key, ch);
333 bool NotifyDoubleClick(CheckListBox listBox, int x, int y, Modifiers mods)
335 listBox.OnLeftButtonDown(x, y, mods);
339 bool NotifyReclick(CheckListBox listBox, DataRow row, Modifiers mods)
341 if(row == listBox.currentRow)
342 listBox.ToggleCheck(row);
347 bool IsChecked(DataRow row)
349 CheckListBoxButton button = buttonMaps[(uintptr)row];
351 for(parent = row; parent; parent = parent.parent) if(rowChecks.Find(parent)) return true;
352 // For partially checked because of children:
353 if(button && button.checked)
358 virtual void Window::NotifyChecked(CheckListBox listBox, DataRow row);
360 void SetDisabled(DataRow row, bool disabled)
362 CheckListBoxButton checkBox = buttonMaps[(uintptr)row];
364 checkBox.disabled = disabled;
366 if(rowDisabled.Find(row))
369 rowDisabled.TakeOut(row);
374 rowDisabled.Add(row);