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.

119 lines
3.2 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. if( !aItem.IsOk() )
  40. {
  41. // No selection. Select the first.
  42. wxDataViewItemArray children;
  43. aView.GetModel()->GetChildren( aItem, children );
  44. return children[0];
  45. }
  46. if( aView.IsExpanded( aItem ) )
  47. {
  48. wxDataViewItemArray children;
  49. aView.GetModel()->GetChildren( aItem, children );
  50. nextItem = children[0];
  51. }
  52. else
  53. {
  54. // Walk up levels until we find one that has a next sibling.
  55. for( wxDataViewItem walk = aItem; walk.IsOk(); walk = aView.GetModel()->GetParent( walk ) )
  56. {
  57. nextItem = GetNextSibling( aView, walk );
  58. if( nextItem.IsOk() )
  59. break;
  60. }
  61. }
  62. return nextItem;
  63. }
  64. wxDataViewItem GetPrevSibling( wxDataViewCtrl const& aView, wxDataViewItem const& aItem )
  65. {
  66. wxDataViewItemArray siblings;
  67. wxDataViewItem invalid;
  68. wxDataViewItem parent = aView.GetModel()->GetParent( aItem );
  69. aView.GetModel()->GetChildren( parent, siblings );
  70. for( size_t i = 0; i < siblings.size(); ++i )
  71. {
  72. if( siblings[i] == aItem )
  73. {
  74. if( i == 0 )
  75. return invalid;
  76. else
  77. return siblings[i - 1];
  78. }
  79. }
  80. return invalid;
  81. }
  82. wxDataViewItem GetNextSibling( wxDataViewCtrl const& aView, wxDataViewItem const& aItem )
  83. {
  84. wxDataViewItemArray siblings;
  85. wxDataViewItem invalid;
  86. wxDataViewItem parent = aView.GetModel()->GetParent( aItem );
  87. aView.GetModel()->GetChildren( parent, siblings );
  88. for( size_t i = 0; i < siblings.size(); ++i )
  89. {
  90. if( siblings[i] == aItem )
  91. {
  92. if( i == siblings.size() - 1 )
  93. return invalid;
  94. else
  95. return siblings[i + 1];
  96. }
  97. }
  98. return invalid;
  99. }