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.

153 lines
4.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-7 by Raw Material Software ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the
  7. GNU General Public License, as published by the Free Software Foundation;
  8. either version 2 of the License, or (at your option) any later version.
  9. JUCE is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with JUCE; if not, visit www.gnu.org/licenses or write to the
  15. Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  16. Boston, MA 02111-1307 USA
  17. ------------------------------------------------------------------------------
  18. If you'd like to release a closed-source product which uses JUCE, commercial
  19. licenses are also available: visit www.rawmaterialsoftware.com/juce for
  20. more information.
  21. ==============================================================================
  22. */
  23. #include "../../../../juce_core/basics/juce_StandardHeader.h"
  24. BEGIN_JUCE_NAMESPACE
  25. #include "juce_KeyboardFocusTraverser.h"
  26. #include "../juce_Component.h"
  27. //==============================================================================
  28. KeyboardFocusTraverser::KeyboardFocusTraverser()
  29. {
  30. }
  31. KeyboardFocusTraverser::~KeyboardFocusTraverser()
  32. {
  33. }
  34. //==============================================================================
  35. // This will sort a set of components, so that they are ordered in terms of
  36. // left-to-right and then top-to-bottom.
  37. class ScreenPositionComparator
  38. {
  39. public:
  40. ScreenPositionComparator() {}
  41. static int compareElements (const Component* const first, const Component* const second) throw()
  42. {
  43. int explicitOrder1 = first->getExplicitFocusOrder();
  44. if (explicitOrder1 <= 0)
  45. explicitOrder1 = INT_MAX / 2;
  46. int explicitOrder2 = second->getExplicitFocusOrder();
  47. if (explicitOrder2 <= 0)
  48. explicitOrder2 = INT_MAX / 2;
  49. if (explicitOrder1 != explicitOrder2)
  50. return explicitOrder1 - explicitOrder2;
  51. const int diff = first->getY() - second->getY();
  52. return (diff == 0) ? first->getX() - second->getX()
  53. : diff;
  54. }
  55. };
  56. static void findAllFocusableComponents (Component* const parent, Array <Component*>& comps)
  57. {
  58. if (parent->getNumChildComponents() > 0)
  59. {
  60. Array <Component*> localComps;
  61. ScreenPositionComparator comparator;
  62. int i;
  63. for (i = parent->getNumChildComponents(); --i >= 0;)
  64. {
  65. Component* const c = parent->getChildComponent (i);
  66. if (c->isVisible() && c->isEnabled())
  67. localComps.addSorted (comparator, c);
  68. }
  69. for (i = 0; i < localComps.size(); ++i)
  70. {
  71. Component* const c = localComps.getUnchecked (i);
  72. if (c->getWantsKeyboardFocus())
  73. comps.add (c);
  74. if (! c->isFocusContainer())
  75. findAllFocusableComponents (c, comps);
  76. }
  77. }
  78. }
  79. static Component* getIncrementedComponent (Component* const current, const int delta) throw()
  80. {
  81. Component* focusContainer = current->getParentComponent();
  82. if (focusContainer != 0)
  83. {
  84. while (focusContainer->getParentComponent() != 0 && ! focusContainer->isFocusContainer())
  85. focusContainer = focusContainer->getParentComponent();
  86. if (focusContainer != 0)
  87. {
  88. Array <Component*> comps;
  89. findAllFocusableComponents (focusContainer, comps);
  90. if (comps.size() > 0)
  91. {
  92. const int index = comps.indexOf (current);
  93. return comps [(index + comps.size() + delta) % comps.size()];
  94. }
  95. }
  96. }
  97. return 0;
  98. }
  99. Component* KeyboardFocusTraverser::getNextComponent (Component* current)
  100. {
  101. return getIncrementedComponent (current, 1);
  102. }
  103. Component* KeyboardFocusTraverser::getPreviousComponent (Component* current)
  104. {
  105. return getIncrementedComponent (current, -1);
  106. }
  107. Component* KeyboardFocusTraverser::getDefaultComponent (Component* parentComponent)
  108. {
  109. Array <Component*> comps;
  110. if (parentComponent != 0)
  111. findAllFocusableComponents (parentComponent, comps);
  112. return comps.getFirst();
  113. }
  114. END_JUCE_NAMESPACE