Audio plugin host https://kx.studio/carla
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.

130 lines
4.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. namespace KeyboardFocusHelpers
  20. {
  21. // This will sort a set of components, so that they are ordered in terms of
  22. // left-to-right and then top-to-bottom.
  23. struct ScreenPositionComparator
  24. {
  25. static int compareElements (const Component* first, const Component* second)
  26. {
  27. auto explicitOrder1 = getOrder (first);
  28. auto explicitOrder2 = getOrder (second);
  29. if (explicitOrder1 != explicitOrder2)
  30. return explicitOrder1 - explicitOrder2;
  31. auto yDiff = first->getY() - second->getY();
  32. return yDiff == 0 ? first->getX() - second->getX()
  33. : yDiff;
  34. }
  35. static int getOrder (const Component* c)
  36. {
  37. auto order = c->getExplicitFocusOrder();
  38. return order > 0 ? order : (std::numeric_limits<int>::max() / 2);
  39. }
  40. };
  41. static void findAllFocusableComponents (Component* parent, Array<Component*>& comps)
  42. {
  43. if (parent->getNumChildComponents() != 0)
  44. {
  45. Array<Component*> localComps;
  46. ScreenPositionComparator comparator;
  47. for (auto* c : parent->getChildren())
  48. if (c->isVisible() && c->isEnabled())
  49. localComps.addSorted (comparator, c);
  50. for (auto* c : localComps)
  51. {
  52. if (c->getWantsKeyboardFocus())
  53. comps.add (c);
  54. if (! c->isFocusContainer())
  55. findAllFocusableComponents (c, comps);
  56. }
  57. }
  58. }
  59. static Component* findFocusContainer (Component* c)
  60. {
  61. c = c->getParentComponent();
  62. if (c != nullptr)
  63. while (c->getParentComponent() != nullptr && ! c->isFocusContainer())
  64. c = c->getParentComponent();
  65. return c;
  66. }
  67. static Component* getIncrementedComponent (Component* current, int delta)
  68. {
  69. if (auto* focusContainer = findFocusContainer (current))
  70. {
  71. Array<Component*> comps;
  72. KeyboardFocusHelpers::findAllFocusableComponents (focusContainer, comps);
  73. if (! comps.isEmpty())
  74. {
  75. auto index = comps.indexOf (current);
  76. return comps [(index + comps.size() + delta) % comps.size()];
  77. }
  78. }
  79. return nullptr;
  80. }
  81. }
  82. //==============================================================================
  83. KeyboardFocusTraverser::KeyboardFocusTraverser() {}
  84. KeyboardFocusTraverser::~KeyboardFocusTraverser() {}
  85. Component* KeyboardFocusTraverser::getNextComponent (Component* current)
  86. {
  87. jassert (current != nullptr);
  88. return KeyboardFocusHelpers::getIncrementedComponent (current, 1);
  89. }
  90. Component* KeyboardFocusTraverser::getPreviousComponent (Component* current)
  91. {
  92. jassert (current != nullptr);
  93. return KeyboardFocusHelpers::getIncrementedComponent (current, -1);
  94. }
  95. Component* KeyboardFocusTraverser::getDefaultComponent (Component* parentComponent)
  96. {
  97. Array<Component*> comps;
  98. if (parentComponent != nullptr)
  99. KeyboardFocusHelpers::findAllFocusableComponents (parentComponent, comps);
  100. return comps.getFirst();
  101. }