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.

138 lines
4.7KB

  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. int i;
  47. for (i = parent->getNumChildComponents(); --i >= 0;)
  48. {
  49. Component* const c = parent->getChildComponent (i);
  50. if (c->isVisible() && c->isEnabled())
  51. localComps.addSorted (comparator, c);
  52. }
  53. for (i = 0; i < localComps.size(); ++i)
  54. {
  55. Component* const c = localComps.getUnchecked (i);
  56. if (c->getWantsKeyboardFocus())
  57. comps.add (c);
  58. if (! c->isFocusContainer())
  59. findAllFocusableComponents (c, comps);
  60. }
  61. }
  62. }
  63. static Component* findFocusContainer (Component* c)
  64. {
  65. c = c->getParentComponent();
  66. if (c != nullptr)
  67. while (c->getParentComponent() != nullptr && ! c->isFocusContainer())
  68. c = c->getParentComponent();
  69. return c;
  70. }
  71. static Component* getIncrementedComponent (Component* const current, const int delta)
  72. {
  73. Component* focusContainer = findFocusContainer (current);
  74. if (focusContainer != nullptr)
  75. {
  76. Array <Component*> comps;
  77. KeyboardFocusHelpers::findAllFocusableComponents (focusContainer, comps);
  78. if (comps.size() > 0)
  79. {
  80. const int index = comps.indexOf (current);
  81. return comps [(index + comps.size() + delta) % comps.size()];
  82. }
  83. }
  84. return nullptr;
  85. }
  86. }
  87. //==============================================================================
  88. KeyboardFocusTraverser::KeyboardFocusTraverser() {}
  89. KeyboardFocusTraverser::~KeyboardFocusTraverser() {}
  90. Component* KeyboardFocusTraverser::getNextComponent (Component* current)
  91. {
  92. jassert (current != nullptr);
  93. return KeyboardFocusHelpers::getIncrementedComponent (current, 1);
  94. }
  95. Component* KeyboardFocusTraverser::getPreviousComponent (Component* current)
  96. {
  97. jassert (current != nullptr);
  98. return KeyboardFocusHelpers::getIncrementedComponent (current, -1);
  99. }
  100. Component* KeyboardFocusTraverser::getDefaultComponent (Component* parentComponent)
  101. {
  102. Array <Component*> comps;
  103. if (parentComponent != nullptr)
  104. KeyboardFocusHelpers::findAllFocusableComponents (parentComponent, comps);
  105. return comps.getFirst();
  106. }