1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea@videolan.org>
8 * Clément Stenac <zorglub@videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 #include "var_tree.hpp"
28 const string VarTree::m_type = "tree";
30 VarTree::VarTree( intf_thread_t *pIntf )
31 : Variable( pIntf ), m_id( 0 ), m_selected( false ), m_playing( false ),
32 m_expanded( false ), m_deleted( false ),
33 m_pData( NULL ), m_pParent( NULL ), m_readonly( false )
35 // Create the position variable
36 m_cPosition = VariablePtr( new VarPercent( pIntf ) );
37 getPositionVar().set( 1.0 );
40 VarTree::VarTree( intf_thread_t *pIntf, VarTree *pParent, int id,
41 const UStringPtr &rcString, bool selected, bool playing,
42 bool expanded, bool readonly,
44 : Variable( pIntf ), m_id( id ), m_cString( rcString ),
45 m_selected( selected ), m_playing( playing ), m_expanded( expanded ),
46 m_deleted( false ), m_pData( pData ), m_pParent( pParent ),
47 m_readonly( readonly )
49 // Create the position variable
50 m_cPosition = VariablePtr( new VarPercent( pIntf ) );
51 getPositionVar().set( 1.0 );
56 /// \todo check that children are deleted
59 void VarTree::add( int id, const UStringPtr &rcString, bool selected,
60 bool playing, bool expanded, bool readonly,
63 m_children.push_back( VarTree( getIntf(), this, id, rcString, selected,
64 playing, expanded, readonly,
68 void VarTree::delSelected()
70 Iterator it = begin();
74 if( size() ) it->delSelected();
80 m_children.erase( oldIt );
94 VarTree::Iterator VarTree::operator[]( int n )
98 for( it = begin(), i = 0;
104 VarTree::ConstIterator VarTree::operator[]( int n ) const
108 for( it = begin(), i = 0;
109 i < n && it != end();
114 VarTree::Iterator VarTree::getNextSibling( VarTree::Iterator current )
116 VarTree *p_parent = current->parent();
117 if( p_parent && current != p_parent->end() )
119 Iterator it = current->parent()->begin();
120 while( it != p_parent->end() && it != current ) it++;
121 if( it != p_parent->end() )
126 return root()->end();
128 return root()->end();
131 /* find iterator to next ancestor
132 * ... which means parent++ or grandparent++ or grandgrandparent++ ... */
133 VarTree::Iterator VarTree::next_uncle()
135 VarTree *p_parent = parent();
136 if( p_parent != NULL )
138 VarTree *p_grandparent = p_parent->parent();
139 while( p_grandparent != NULL )
141 Iterator it = p_grandparent->begin();
142 while( it != p_grandparent->end() && &(*it) != p_parent ) it++;
143 if( it != p_grandparent->end() )
146 if( it != p_grandparent->end() )
151 if( p_grandparent->parent() )
153 p_parent = p_grandparent;
154 p_grandparent = p_parent->parent();
157 p_grandparent = NULL;
161 /* if we didn't return before, it means that we've reached the end */
162 return root()->end();
165 VarTree::Iterator VarTree::prev_uncle()
167 VarTree *p_parent = parent();
168 if( p_parent != NULL )
170 VarTree *p_grandparent = p_parent->parent();
171 while( p_grandparent != NULL )
173 Iterator it = p_grandparent->end();
174 while( it != p_grandparent->begin() && &(*it) != p_parent ) it--;
175 if( it != p_grandparent->begin() )
178 if( it != p_grandparent->begin() )
183 if( p_grandparent->parent() )
185 p_parent = p_grandparent;
186 p_grandparent = p_parent->parent();
189 p_grandparent = NULL;
193 /* if we didn't return before, it means that we've reached the end */
194 return root()->begin();
198 void VarTree::checkParents( VarTree *pParent )
201 Iterator it = begin();
204 it->checkParents( this );
209 int VarTree::visibleItems()
211 int i_count = size();
212 Iterator it = begin();
217 i_count += it->visibleItems();
224 VarTree::Iterator VarTree::getVisibleItem( int n )
226 Iterator it = begin();
235 i = n - it->visibleItems();
236 if( i <= 0 ) return it->getVisibleItem( n );
244 VarTree::Iterator VarTree::getLeaf( int n )
246 Iterator it = begin();
252 i = n - it->countLeafs();
253 if( i <= 0 ) return it->getLeaf( n );
267 VarTree::Iterator VarTree::getNextVisibleItem( Iterator it )
269 if( it->m_expanded && it->size() )
275 VarTree::Iterator it_old = it;
277 // Was 'it' the last brother? If so, look for uncles
278 if( it_old->parent() && it_old->parent()->end() == it )
280 it = it_old->next_uncle();
286 VarTree::Iterator VarTree::getPrevVisibleItem( Iterator it )
288 VarTree::Iterator it_old = it;
289 if( it == root()->begin() || it == ++(root()->begin()) ) return it;
291 /* Was it the first child of its parent ? */
292 if( it->parent() && it == it->parent()->begin() )
294 /* Yes, get previous uncle */
295 it = it_old->prev_uncle();
300 /* We have found an expanded uncle, take its last child */
301 while( it != root()->begin() && it->size() && it->m_expanded )
309 VarTree::Iterator VarTree::getNextItem( Iterator it )
317 VarTree::Iterator it_old = it;
319 // Was 'it' the last brother? If so, look for uncles
320 if( it_old->parent() && it_old->parent()->end() == it )
322 it = it_old->next_uncle();
328 VarTree::Iterator VarTree::getPrevItem( Iterator it )
330 VarTree::Iterator it_old = it;
331 if( it == root()->begin() || it == ++(root()->begin()) ) return it;
333 /* Was it the first child of its parent ? */
334 if( it->parent() && it == it->parent()->begin() )
336 /* Yes, get previous uncle */
337 it = it_old->prev_uncle();
342 /* We have found an expanded uncle, take its last child */
343 while( it != root()->begin() && it->size() )
351 VarTree::Iterator VarTree::getNextLeaf( Iterator it )
355 it = getNextItem( it );
357 while( it != root()->end() && it->size() );
361 VarTree::Iterator VarTree::getPrevLeaf( Iterator it )
365 it = getPrevItem( it );
367 while( it != root()->begin() && it->size() ); /* FIXME ? */
368 if( it == root()->begin() ) it = firstLeaf();
372 VarTree::Iterator VarTree::findById( int id )
374 for (Iterator it = begin(); it != end(); ++it )
380 Iterator result = it->findById( id );
381 if( result != it->end() ) return result;
387 void VarTree::ensureExpanded( VarTree::Iterator it )
389 /// Don't expand ourselves, only our parents
390 VarTree *current = &(*it);
391 current = current->parent();
392 while( current->parent() != NULL )
394 current->m_expanded = true;
395 current = current->parent();
399 int VarTree::countLeafs()
401 if( size() == 0 ) return 1;
404 Iterator it = begin();
407 i_count += it->countLeafs();
413 VarTree::Iterator VarTree::firstLeaf()
415 Iterator b = root()->begin();
416 if( b->size() ) return getNextLeaf( b );