1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2005 VideoLAN
7 * Authors: Antoine Cellerier <dionoea@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #include "var_tree.hpp"
27 const string VarTree::m_type = "tree";
29 VarTree::VarTree( intf_thread_t *pIntf )
30 : Variable( pIntf ), m_id( 0 ), m_selected( false ), m_playing( false ),
31 m_expanded( false ), m_pData( NULL ), m_pParent( NULL )
33 // Create the position variable
34 m_cPosition = VariablePtr( new VarPercent( pIntf ) );
35 getPositionVar().set( 1.0 );
38 VarTree::VarTree( intf_thread_t *pIntf, VarTree *pParent, int id,
39 const UStringPtr &rcString, bool selected, bool playing,
40 bool expanded, void *pData )
41 : Variable( pIntf ), m_id( id ), m_cString( rcString ),
42 m_selected( selected ), m_playing( playing ), m_expanded( expanded ),
43 m_pData( pData ), m_pParent( pParent )
45 // Create the position variable
46 m_cPosition = VariablePtr( new VarPercent( pIntf ) );
47 getPositionVar().set( 1.0 );
52 /// \todo check that children are deleted
55 void VarTree::add( int id, const UStringPtr &rcString, bool selected,
56 bool playing, bool expanded, void *pData )
58 m_children.push_back( VarTree( getIntf(), this, id, rcString, selected,
59 playing, expanded, pData ) );
62 void VarTree::delSelected()
64 Iterator it = begin();
68 if( size() ) it->delSelected();
74 m_children.erase( oldIt );
88 VarTree::Iterator VarTree::operator[]( int n )
92 for( it = begin(), i = 0;
98 VarTree::ConstIterator VarTree::operator[]( int n ) const
102 for( it = begin(), i = 0;
103 i < n && it != end();
108 /* find iterator to next ancestor
109 * ... which means parent++ or grandparent++ or grandgrandparent++ ... */
110 VarTree::Iterator VarTree::next_uncle()
112 VarTree *p_parent = parent();
113 if( p_parent != NULL )
115 VarTree *p_grandparent = p_parent->parent();
116 while( p_grandparent != NULL )
118 Iterator it = p_grandparent->begin();
119 while( it != p_grandparent->end() && &(*it) != p_parent ) it++;
120 if( it != p_grandparent->end() )
123 if( it != p_grandparent->end() )
128 if( p_grandparent->parent() )
130 p_parent = p_grandparent;
131 p_grandparent = p_parent->parent();
134 p_grandparent = NULL;
138 /* if we didn't return before, it means that we've reached the end */
139 return root()->end();
142 VarTree::Iterator VarTree::prev_uncle()
144 VarTree *p_parent = parent();
145 if( p_parent != NULL )
147 VarTree *p_grandparent = p_parent->parent();
148 while( p_grandparent != NULL )
150 Iterator it = p_grandparent->end();
151 while( it != p_grandparent->begin() && &(*it) != p_parent ) it--;
152 if( it != p_grandparent->begin() )
155 if( it != p_grandparent->begin() )
160 if( p_grandparent->parent() )
162 p_parent = p_grandparent;
163 p_grandparent = p_parent->parent();
166 p_grandparent = NULL;
170 /* if we didn't return before, it means that we've reached the end */
171 return root()->begin();
175 void VarTree::checkParents( VarTree *pParent )
178 Iterator it = begin();
181 it->checkParents( this );
186 int VarTree::visibleItems()
188 int i_count = size();
189 Iterator it = begin();
194 i_count += it->visibleItems();
201 VarTree::Iterator VarTree::getVisibleItem( int n )
203 Iterator it = begin();
207 if( n <= 0 ) return it;
210 int i = n - it->visibleItems();
211 if( i <= 0 ) return it->getVisibleItem( n );
219 VarTree::Iterator VarTree::getNextVisibleItem( Iterator it )
221 if( it->m_expanded && it->size() )
227 VarTree::Iterator it_old = it;
229 // Was 'it' the last brother? If so, look for uncles
230 if( it_old->parent() && it_old->parent()->end() == it )
232 it = it_old->next_uncle();
238 VarTree::Iterator VarTree::getPrevVisibleItem( Iterator it )
240 VarTree::Iterator it_old = it;
241 if( it == root()->begin() || it == ++(root()->begin()) ) return it;
245 /* Was it the first child of its parent ? */
246 if( it->parent() && it == it->parent()->begin() )
248 /* Yes, get previous uncle */
249 it = it_old->prev_uncle();
254 /* We have found an expanded uncle, take its last child */
255 while( it != root()->begin() && it->size() && it->m_expanded )
263 VarTree::Iterator VarTree::getNextItem( Iterator it )
271 VarTree::Iterator it_old = it;
273 // Was 'it' the last brother? If so, look for uncles
274 if( it_old->parent() && it_old->parent()->end() == it )
276 it = it_old->next_uncle();
283 VarTree::Iterator VarTree::findById( int id )
285 for (Iterator it = begin(); it != end(); ++it )
291 Iterator result = it->findById( id );
292 if( result != it->end() ) return result;
298 void VarTree::ensureExpanded( VarTree::Iterator it )
300 /// Don't expand ourselves, only our parents
301 VarTree *current = &(*it);
302 current = current->parent();
303 while( current->parent() != NULL )
305 current->m_expanded = true;
306 current = current->parent();