tree-view.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #if defined(Hiro_TreeView)
  2. auto mTreeView::allocate() -> pObject* {
  3. return new pTreeView(*this);
  4. }
  5. auto mTreeView::destruct() -> void {
  6. for(auto& item : state.items) item->destruct();
  7. mWidget::destruct();
  8. }
  9. //
  10. auto mTreeView::activation() const -> Mouse::Click {
  11. return state.activation;
  12. }
  13. auto mTreeView::append(sTreeViewItem item) -> type& {
  14. state.items.append(item);
  15. item->setParent(this, itemCount() - 1);
  16. signal(append, item);
  17. return *this;
  18. }
  19. auto mTreeView::backgroundColor() const -> Color {
  20. return state.backgroundColor;
  21. }
  22. auto mTreeView::collapse(bool recursive) -> type& {
  23. for(auto& item : state.items) item->collapse(recursive);
  24. return *this;
  25. }
  26. auto mTreeView::doActivate() const -> void {
  27. if(state.onActivate) return state.onActivate();
  28. }
  29. auto mTreeView::doChange() const -> void {
  30. if(state.onChange) return state.onChange();
  31. }
  32. auto mTreeView::doContext() const -> void {
  33. if(state.onContext) return state.onContext();
  34. }
  35. auto mTreeView::doToggle(sTreeViewItem item) const -> void {
  36. if(state.onToggle) return state.onToggle(item);
  37. }
  38. auto mTreeView::expand(bool recursive) -> type& {
  39. for(auto& item : state.items) item->expand(recursive);
  40. return *this;
  41. }
  42. auto mTreeView::foregroundColor() const -> Color {
  43. return state.foregroundColor;
  44. }
  45. auto mTreeView::item(const string& path) const -> TreeViewItem {
  46. if(!path) return {};
  47. auto paths = path.split("/");
  48. unsigned position = paths.takeLeft().natural();
  49. if(position >= itemCount()) return {};
  50. if(!paths) return state.items[position];
  51. return state.items[position]->item(paths.merge("/"));
  52. }
  53. auto mTreeView::itemCount() const -> unsigned {
  54. return state.items.size();
  55. }
  56. auto mTreeView::items() const -> vector<TreeViewItem> {
  57. vector<TreeViewItem> items;
  58. for(auto& item : state.items) items.append(item);
  59. return items;
  60. }
  61. auto mTreeView::onActivate(const function<void ()>& callback) -> type& {
  62. state.onActivate = callback;
  63. return *this;
  64. }
  65. auto mTreeView::onChange(const function<void ()>& callback) -> type& {
  66. state.onChange = callback;
  67. return *this;
  68. }
  69. auto mTreeView::onContext(const function<void ()>& callback) -> type& {
  70. state.onContext = callback;
  71. return *this;
  72. }
  73. auto mTreeView::onToggle(const function<void (sTreeViewItem)>& callback) -> type& {
  74. state.onToggle = callback;
  75. return *this;
  76. }
  77. auto mTreeView::remove(sTreeViewItem item) -> type& {
  78. signal(remove, item);
  79. state.items.remove(item->offset());
  80. for(auto n : range(item->offset(), itemCount())) {
  81. state.items[n]->adjustOffset(-1);
  82. }
  83. item->setParent();
  84. return *this;
  85. }
  86. auto mTreeView::reset() -> type& {
  87. state.selectedPath.reset();
  88. while(state.items) remove(state.items.right());
  89. return *this;
  90. }
  91. auto mTreeView::selectNone() -> type& {
  92. if(auto item = selected()) {
  93. //TODO
  94. //item->setSelected(false);
  95. }
  96. return *this;
  97. }
  98. auto mTreeView::selected() const -> TreeViewItem {
  99. return item(state.selectedPath);
  100. }
  101. auto mTreeView::setActivation(Mouse::Click activation) -> type& {
  102. state.activation = activation;
  103. signal(setActivation, activation);
  104. return *this;
  105. }
  106. auto mTreeView::setBackgroundColor(Color color) -> type& {
  107. state.backgroundColor = color;
  108. signal(setBackgroundColor, color);
  109. return *this;
  110. }
  111. auto mTreeView::setForegroundColor(Color color) -> type& {
  112. state.foregroundColor = color;
  113. signal(setForegroundColor, color);
  114. return *this;
  115. }
  116. auto mTreeView::setParent(mObject* object, signed offset) -> type& {
  117. for(auto& item : reverse(state.items)) item->destruct();
  118. mObject::setParent(object, offset);
  119. for(auto& item : state.items) item->setParent(this, item->offset());
  120. return *this;
  121. }
  122. #endif