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() )
125 return root()->end();
127 return root()->end();
130 /* find iterator to next ancestor
131 * ... which means parent++ or grandparent++ or grandgrandparent++ ... */
132 VarTree::Iterator VarTree::next_uncle()
134 VarTree *p_parent = parent();
135 if( p_parent != NULL )
137 VarTree *p_grandparent = p_parent->parent();
138 while( p_grandparent != NULL )
140 Iterator it = p_grandparent->begin();
141 while( it != p_grandparent->end() && &(*it) != p_parent ) it++;
142 if( it != p_grandparent->end() )
145 if( it != p_grandparent->end() )
150 if( p_grandparent->parent() )
152 p_parent = p_grandparent;
153 p_grandparent = p_parent->parent();
156 p_grandparent = NULL;
160 /* if we didn't return before, it means that we've reached the end */
161 return root()->end();
164 VarTree::Iterator VarTree::prev_uncle()
166 VarTree *p_parent = parent();
167 if( p_parent != NULL )
169 VarTree *p_grandparent = p_parent->parent();
170 while( p_grandparent != NULL )
172 Iterator it = p_grandparent->end();
173 while( it != p_grandparent->begin() && &(*it) != p_parent ) it--;
174 if( it != p_grandparent->begin() )
177 if( it != p_grandparent->begin() )
182 if( p_grandparent->parent() )
184 p_parent = p_grandparent;
185 p_grandparent = p_parent->parent();
188 p_grandparent = NULL;
192 /* if we didn't return before, it means that we've reached the end */
193 return root()->begin();
197 void VarTree::checkParents( VarTree *pParent )
200 Iterator it = begin();
203 it->checkParents( this );
208 int VarTree::visibleItems()
210 int i_count = size();
211 Iterator it = begin();
216 i_count += it->visibleItems();
223 VarTree::Iterator VarTree::getVisibleItem( int n )
225 Iterator it = begin();
234 i = n - it->visibleItems();
235 if( i <= 0 ) return it->getVisibleItem( n );
243 VarTree::Iterator VarTree::getLeaf( int n )
245 Iterator it = begin();
251 i = n - it->countLeafs();
252 if( i <= 0 ) return it->getLeaf( n );
266 VarTree::Iterator VarTree::getNextVisibleItem( Iterator it )
268 if( it->m_expanded && it->size() )
274 VarTree::Iterator it_old = it;
276 // Was 'it' the last brother? If so, look for uncles
277 if( it_old->parent() && it_old->parent()->end() == it )
279 it = it_old->next_uncle();
285 VarTree::Iterator VarTree::getPrevVisibleItem( Iterator it )
287 VarTree::Iterator it_old = it;
288 if( it == root()->begin() || it == ++(root()->begin()) ) return it;
290 /* Was it the first child of its parent ? */
291 if( it->parent() && it == it->parent()->begin() )
293 /* Yes, get previous uncle */
294 it = it_old->prev_uncle();
299 /* We have found an expanded uncle, take its last child */
300 while( it != root()->begin() && it->size() && it->m_expanded )
308 VarTree::Iterator VarTree::getNextItem( Iterator it )
316 VarTree::Iterator it_old = it;
318 // Was 'it' the last brother? If so, look for uncles
319 if( it_old->parent() && it_old->parent()->end() == it )
321 it = it_old->next_uncle();
327 VarTree::Iterator VarTree::getPrevItem( Iterator it )
329 VarTree::Iterator it_old = it;
330 if( it == root()->begin() || it == ++(root()->begin()) ) return it;
332 /* Was it the first child of its parent ? */
333 if( it->parent() && it == it->parent()->begin() )
335 /* Yes, get previous uncle */
336 it = it_old->prev_uncle();
341 /* We have found an expanded uncle, take its last child */
342 while( it != root()->begin() && it->size() )
350 VarTree::Iterator VarTree::getNextLeaf( Iterator it )
354 it = getNextItem( it );
356 while( it != root()->end() && it->size() );
360 VarTree::Iterator VarTree::getPrevLeaf( Iterator it )
364 it = getPrevItem( it );
366 while( it != root()->begin() && it->size() ); /* FIXME ? */
367 if( it == root()->begin() ) it = firstLeaf();
371 VarTree::Iterator VarTree::findById( int id )
373 for (Iterator it = begin(); it != end(); ++it )
379 Iterator result = it->findById( id );
380 if( result != it->end() ) return result;
386 void VarTree::ensureExpanded( VarTree::Iterator it )
388 /// Don't expand ourselves, only our parents
389 VarTree *current = &(*it);
390 current = current->parent();
391 while( current->parent() != NULL )
393 current->m_expanded = true;
394 current = current->parent();
398 int VarTree::countLeafs()
400 if( size() == 0 ) return 1;
403 Iterator it = begin();
406 i_count += it->countLeafs();
412 VarTree::Iterator VarTree::firstLeaf()
414 Iterator b = root()->begin();
415 if( b->size() ) return getNextLeaf( b );