You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.2KB

  1. #include "widgets.hpp"
  2. namespace rack {
  3. Menu::~Menu() {
  4. setChildMenu(NULL);
  5. }
  6. void Menu::pushChild(Widget *child) {
  7. child->box.pos = Vec(0, box.size.y);
  8. addChild(child);
  9. box.size.y += child->box.size.y;
  10. }
  11. void Menu::setChildMenu(Menu *menu) {
  12. if (childMenu) {
  13. if (childMenu->parent)
  14. childMenu->parent->removeChild(childMenu);
  15. delete childMenu;
  16. childMenu = NULL;
  17. }
  18. if (menu) {
  19. childMenu = menu;
  20. assert(parent);
  21. parent->addChild(childMenu);
  22. }
  23. }
  24. void Menu::step() {
  25. // Try to fit into the parent's box
  26. if (parent)
  27. box = box.nudge(Rect(Vec(0, 0), parent->box.size));
  28. Widget::step();
  29. // Resize the width to the widest child
  30. for (Widget *child : children) {
  31. if (child->box.size.x > box.size.x) {
  32. box.size.x = child->box.size.x;
  33. }
  34. }
  35. // Resize widths of children
  36. for (Widget *child : children) {
  37. child->box.size.x = box.size.x;
  38. }
  39. }
  40. void Menu::draw(NVGcontext *vg) {
  41. bndMenuBackground(vg, 0.0, 0.0, box.size.x, box.size.y, BND_CORNER_NONE);
  42. Widget::draw(vg);
  43. }
  44. void Menu::onScroll(EventScroll &e) {
  45. if (!parent)
  46. return;
  47. if (!parent->box.contains(box))
  48. box.pos = box.pos.plus(e.scrollRel);
  49. e.consumed = true;
  50. }
  51. } // namespace rack