The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

137 lines
4.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. namespace KeyboardFocusHelpers
  19. {
  20. // This will sort a set of components, so that they are ordered in terms of
  21. // left-to-right and then top-to-bottom.
  22. struct ScreenPositionComparator
  23. {
  24. static int compareElements (const Component* const first, const Component* const second)
  25. {
  26. const int explicitOrder1 = getOrder (first);
  27. const int explicitOrder2 = getOrder (second);
  28. if (explicitOrder1 != explicitOrder2)
  29. return explicitOrder1 - explicitOrder2;
  30. const int yDiff = first->getY() - second->getY();
  31. return yDiff == 0 ? first->getX() - second->getX()
  32. : yDiff;
  33. }
  34. static int getOrder (const Component* const c)
  35. {
  36. const int order = c->getExplicitFocusOrder();
  37. return order > 0 ? order : (std::numeric_limits<int>::max() / 2);
  38. }
  39. };
  40. static void findAllFocusableComponents (Component* const parent, Array <Component*>& comps)
  41. {
  42. if (parent->getNumChildComponents() > 0)
  43. {
  44. Array <Component*> localComps;
  45. ScreenPositionComparator comparator;
  46. for (int i = parent->getNumChildComponents(); --i >= 0;)
  47. {
  48. Component* const c = parent->getChildComponent (i);
  49. if (c->isVisible() && c->isEnabled())
  50. localComps.addSorted (comparator, c);
  51. }
  52. for (int i = 0; i < localComps.size(); ++i)
  53. {
  54. Component* const c = localComps.getUnchecked (i);
  55. if (c->getWantsKeyboardFocus())
  56. comps.add (c);
  57. if (! c->isFocusContainer())
  58. findAllFocusableComponents (c, comps);
  59. }
  60. }
  61. }
  62. static Component* findFocusContainer (Component* c)
  63. {
  64. c = c->getParentComponent();
  65. if (c != nullptr)
  66. while (c->getParentComponent() != nullptr && ! c->isFocusContainer())
  67. c = c->getParentComponent();
  68. return c;
  69. }
  70. static Component* getIncrementedComponent (Component* const current, const int delta)
  71. {
  72. Component* focusContainer = findFocusContainer (current);
  73. if (focusContainer != nullptr)
  74. {
  75. Array <Component*> comps;
  76. KeyboardFocusHelpers::findAllFocusableComponents (focusContainer, comps);
  77. if (comps.size() > 0)
  78. {
  79. const int index = comps.indexOf (current);
  80. return comps [(index + comps.size() + delta) % comps.size()];
  81. }
  82. }
  83. return nullptr;
  84. }
  85. }
  86. //==============================================================================
  87. KeyboardFocusTraverser::KeyboardFocusTraverser() {}
  88. KeyboardFocusTraverser::~KeyboardFocusTraverser() {}
  89. Component* KeyboardFocusTraverser::getNextComponent (Component* current)
  90. {
  91. jassert (current != nullptr);
  92. return KeyboardFocusHelpers::getIncrementedComponent (current, 1);
  93. }
  94. Component* KeyboardFocusTraverser::getPreviousComponent (Component* current)
  95. {
  96. jassert (current != nullptr);
  97. return KeyboardFocusHelpers::getIncrementedComponent (current, -1);
  98. }
  99. Component* KeyboardFocusTraverser::getDefaultComponent (Component* parentComponent)
  100. {
  101. Array <Component*> comps;
  102. if (parentComponent != nullptr)
  103. KeyboardFocusHelpers::findAllFocusableComponents (parentComponent, comps);
  104. return comps.getFirst();
  105. }