File: combo-button.cpp

package info (click to toggle)
ares 147%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 35,244 kB
  • sloc: cpp: 334,263; ansic: 98,696; sh: 123; makefile: 31
file content (75 lines) | stat: -rw-r--r-- 1,899 bytes parent folder | download | duplicates (2)
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
#if defined(Hiro_ComboButton)

auto mComboButton::allocate() -> pObject* {
  return new pComboButton(*this);
}

auto mComboButton::destruct() -> void {
  for(auto& item : state.items) item->destruct();
  mWidget::destruct();
}

//

auto mComboButton::append(sComboButtonItem item) -> type& {
  if(state.items.empty()) item->state.selected = true;
  state.items.push_back(item);
  item->setParent(this, itemCount() - 1);
  signal(append, item);
  return *this;
}

auto mComboButton::doChange() const -> void {
  if(state.onChange) return state.onChange();
}

auto mComboButton::item(u32 position) const -> ComboButtonItem {
  if(position < itemCount()) return state.items[position];
  return {};
}

auto mComboButton::itemCount() const -> u32 {
  return state.items.size();
}

auto mComboButton::items() const -> std::vector<ComboButtonItem> {
  std::vector<ComboButtonItem> items;
  for(auto& item : state.items) items.push_back(item);
  return items;
}

auto mComboButton::onChange(const std::function<void ()>& callback) -> type& {
  state.onChange = callback;
  return *this;
}

auto mComboButton::remove(sComboButtonItem item) -> type& {
  signal(remove, item);
  state.items.erase(state.items.begin() + item->offset());
  for(u32 n : range(item->offset(), itemCount())) {
    state.items[n]->adjustOffset(-1);
  }
  item->setParent();
  return *this;
}

auto mComboButton::reset() -> type& {
  while(!state.items.empty()) remove(state.items.back());
  return *this;
}

auto mComboButton::selected() const -> ComboButtonItem {
  for(auto& item : state.items) {
    if(item->selected()) return item;
  }
  return {};
}

auto mComboButton::setParent(mObject* parent, s32 offset) -> type& {
  for(auto& item : state.items | std::views::reverse) item->destruct();
  mObject::setParent(parent, offset);
  for(auto& item : state.items) item->setParent(this, item->offset());
  return *this;
}

#endif