KiCad PCB EDA Suite
wxdataviewctrl_helpers.cpp
Go to the documentation of this file.
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 
20 #include <wx/dataview.h>
21 #include <wxdataviewctrl_helpers.h>
22 
23 wxDataViewItem GetPrevItem( wxDataViewCtrl const& aView, wxDataViewItem const& aItem )
24 {
25  auto prevItem = GetPrevSibling( aView, aItem );
26 
27  if( !prevItem.IsOk() )
28  {
29  prevItem = aView.GetModel()->GetParent( aItem );
30  }
31  else if( aView.IsExpanded( prevItem ) )
32  {
33  wxDataViewItemArray children;
34  aView.GetModel()->GetChildren( prevItem, children );
35  prevItem = children[children.size() - 1];
36  }
37 
38  return prevItem;
39 }
40 
41 
42 wxDataViewItem GetNextItem( wxDataViewCtrl const& aView, wxDataViewItem const& aItem )
43 {
44  wxDataViewItem nextItem;
45 
46  if( !aItem.IsOk() )
47  {
48  // No selection. Select the first.
49  wxDataViewItemArray children;
50  aView.GetModel()->GetChildren( aItem, children );
51  return children[0];
52  }
53 
54  if( aView.IsExpanded( aItem ) )
55  {
56  wxDataViewItemArray children;
57  aView.GetModel()->GetChildren( aItem, children );
58  nextItem = children[0];
59  }
60  else
61  {
62  // Walk up levels until we find one that has a next sibling.
63  for( wxDataViewItem walk = aItem; walk.IsOk(); walk = aView.GetModel()->GetParent( walk ) )
64  {
65  nextItem = GetNextSibling( aView, walk );
66 
67  if( nextItem.IsOk() )
68  break;
69  }
70  }
71 
72  return nextItem;
73 }
74 
75 
76 wxDataViewItem GetPrevSibling( wxDataViewCtrl const& aView, wxDataViewItem const& aItem )
77 {
78  wxDataViewItemArray siblings;
79  wxDataViewItem invalid;
80  wxDataViewItem parent = aView.GetModel()->GetParent( aItem );
81 
82  aView.GetModel()->GetChildren( parent, siblings );
83 
84  for( size_t i = 0; i < siblings.size(); ++i )
85  {
86  if( siblings[i] == aItem )
87  {
88  if( i == 0 )
89  return invalid;
90  else
91  return siblings[i - 1];
92  }
93  }
94 
95  return invalid;
96 }
97 
98 
99 wxDataViewItem GetNextSibling( wxDataViewCtrl const& aView, wxDataViewItem const& aItem )
100 {
101  wxDataViewItemArray siblings;
102  wxDataViewItem invalid;
103  wxDataViewItem parent = aView.GetModel()->GetParent( aItem );
104 
105  aView.GetModel()->GetChildren( parent, siblings );
106 
107  for( size_t i = 0; i < siblings.size(); ++i )
108  {
109  if( siblings[i] == aItem )
110  {
111  if( i == siblings.size() - 1 )
112  return invalid;
113  else
114  return siblings[i + 1];
115  }
116  }
117 
118  return invalid;
119 }
wxDataViewItem GetPrevSibling(wxDataViewCtrl const &aView, wxDataViewItem const &aItem)
Get the previous sibling of an item.
wxDataViewItem GetPrevItem(wxDataViewCtrl const &aView, wxDataViewItem const &aItem)
Get the previous item in list order.
wxDataViewItem GetNextSibling(wxDataViewCtrl const &aView, wxDataViewItem const &aItem)
Get the next sibling of an item.
wxDataViewItem GetNextItem(wxDataViewCtrl const &aView, wxDataViewItem const &aItem)
Get the next item in list order.
wxDataViewCtrl helper functions.