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.

128 lines
3.4 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2017 KiCad Developers, see AUTHORS.txt for contributors.
  5. *
  6. * This program is free software: you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation, either version 3 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <wx/dataview.h>
  20. #include <wxdataviewctrl_helpers.h>
  21. wxDataViewItem GetPrevItem( wxDataViewCtrl const& aView, wxDataViewItem const& aItem )
  22. {
  23. auto prevItem = GetPrevSibling( aView, aItem );
  24. if( !prevItem.IsOk() )
  25. {
  26. prevItem = aView.GetModel()->GetParent( aItem );
  27. }
  28. else if( aView.IsExpanded( prevItem ) )
  29. {
  30. wxDataViewItemArray children;
  31. aView.GetModel()->GetChildren( prevItem, children );
  32. prevItem = children[children.size() - 1];
  33. }
  34. return prevItem;
  35. }
  36. wxDataViewItem GetNextItem( wxDataViewCtrl const& aView, wxDataViewItem const& aItem )
  37. {
  38. wxDataViewItem nextItem;
  39. wxDataViewItem invalid;
  40. if( !aItem.IsOk() )
  41. {
  42. // No selection. Select the first.
  43. wxDataViewItemArray children;
  44. aView.GetModel()->GetChildren( aItem, children );
  45. if( children.size() )
  46. return children[0];
  47. return invalid;
  48. }
  49. if( aView.IsExpanded( aItem ) )
  50. {
  51. wxDataViewItemArray children;
  52. aView.GetModel()->GetChildren( aItem, children );
  53. if( children.size() )
  54. return children[0];
  55. return invalid;
  56. }
  57. else
  58. {
  59. // Walk up levels until we find one that has a next sibling.
  60. for( wxDataViewItem walk = aItem; walk.IsOk(); walk = aView.GetModel()->GetParent( walk ) )
  61. {
  62. nextItem = GetNextSibling( aView, walk );
  63. if( nextItem.IsOk() )
  64. break;
  65. }
  66. }
  67. return nextItem;
  68. }
  69. wxDataViewItem GetPrevSibling( wxDataViewCtrl const& aView, wxDataViewItem const& aItem )
  70. {
  71. wxDataViewItemArray siblings;
  72. wxDataViewItem invalid;
  73. wxDataViewItem parent = aView.GetModel()->GetParent( aItem );
  74. aView.GetModel()->GetChildren( parent, siblings );
  75. for( size_t i = 0; i < siblings.size(); ++i )
  76. {
  77. if( siblings[i] == aItem )
  78. {
  79. if( i == 0 )
  80. return invalid;
  81. else
  82. return siblings[i - 1];
  83. }
  84. }
  85. return invalid;
  86. }
  87. wxDataViewItem GetNextSibling( wxDataViewCtrl const& aView, wxDataViewItem const& aItem )
  88. {
  89. wxDataViewItemArray siblings;
  90. wxDataViewItem invalid;
  91. wxDataViewItem parent = aView.GetModel()->GetParent( aItem );
  92. aView.GetModel()->GetChildren( parent, siblings );
  93. for( size_t i = 0; i < siblings.size(); ++i )
  94. {
  95. if( siblings[i] == aItem )
  96. {
  97. if( i == siblings.size() - 1 )
  98. return invalid;
  99. else
  100. return siblings[i + 1];
  101. }
  102. }
  103. return invalid;
  104. }