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::getNextSiblingOrUncle()
116 VarTree *p_parent = parent();
119 Iterator it = p_parent->begin();
120 while( it != p_parent->end() && &(*it) != this ) ++it;
121 if( it != p_parent->end() )
123 Iterator current = it;
125 if( it != p_parent->end() )
128 return current->next_uncle();
132 msg_Err( getIntf(), "should never occur" );
139 /* find iterator to next ancestor
140 * ... which means parent++ or grandparent++ or grandgrandparent++ ... */
141 VarTree::Iterator VarTree::next_uncle()
143 VarTree *p_parent = parent();
144 if( p_parent != NULL )
146 VarTree *p_grandparent = p_parent->parent();
147 while( p_grandparent != NULL )
149 Iterator it = p_grandparent->begin();
150 while( it != p_grandparent->end() && &(*it) != p_parent ) it++;
151 if( it != p_grandparent->end() )
154 if( it != p_grandparent->end() )
159 if( p_grandparent->parent() )
161 p_parent = p_grandparent;
162 p_grandparent = p_parent->parent();
165 p_grandparent = NULL;
169 /* if we didn't return before, it means that we've reached the end */
170 return root()->end();
173 VarTree::Iterator VarTree::prev_uncle()
175 VarTree *p_parent = parent();
176 if( p_parent != NULL )
178 VarTree *p_grandparent = p_parent->parent();
179 while( p_grandparent != NULL )
181 Iterator it = p_grandparent->end();
182 while( it != p_grandparent->begin() && &(*it) != p_parent ) it--;
183 if( it != p_grandparent->begin() )
186 if( it != p_grandparent->begin() )
191 if( p_grandparent->parent() )
193 p_parent = p_grandparent;
194 p_grandparent = p_parent->parent();
197 p_grandparent = NULL;
201 /* if we didn't return before, it means that we've reached the end */
202 return root()->begin();
205 int VarTree::visibleItems()
207 int i_count = size();
208 Iterator it = begin();
213 i_count += it->visibleItems();
220 VarTree::Iterator VarTree::getVisibleItem( int n )
222 Iterator it = begin();
231 i = n - it->visibleItems();
232 if( i <= 0 ) return it->getVisibleItem( n );
240 VarTree::Iterator VarTree::getLeaf( int n )
242 Iterator it = begin();
248 i = n - it->countLeafs();
249 if( i <= 0 ) return it->getLeaf( n );
263 VarTree::Iterator VarTree::getNextVisibleItem( Iterator it )
265 if( it->m_expanded && it->size() )
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();
282 VarTree::Iterator VarTree::getPrevVisibleItem( Iterator it )
284 VarTree::Iterator it_old = it;
285 if( it == root()->begin() || it == ++(root()->begin()) ) return it;
287 /* Was it the first child of its parent ? */
288 if( it->parent() && it == it->parent()->begin() )
290 /* Yes, get previous uncle */
291 it = it_old->prev_uncle();
296 /* We have found an expanded uncle, take its last child */
297 while( it != root()->begin() && it->size() && it->m_expanded )
305 VarTree::Iterator VarTree::getNextItem( Iterator it )
313 VarTree::Iterator it_old = it;
315 // Was 'it' the last brother? If so, look for uncles
316 if( it_old->parent() && it_old->parent()->end() == it )
318 it = it_old->next_uncle();
324 VarTree::Iterator VarTree::getPrevItem( Iterator it )
326 VarTree::Iterator it_old = it;
327 if( it == root()->begin() || it == ++(root()->begin()) ) return it;
329 /* Was it the first child of its parent ? */
330 if( it->parent() && it == it->parent()->begin() )
332 /* Yes, get previous uncle */
333 it = it_old->prev_uncle();
338 /* We have found an expanded uncle, take its last child */
339 while( it != root()->begin() && it->size() )
347 VarTree::Iterator VarTree::getNextLeaf( Iterator it )
351 it = getNextItem( it );
353 while( it != root()->end() && it->size() );
357 VarTree::Iterator VarTree::getPrevLeaf( Iterator it )
361 it = getPrevItem( it );
363 while( it != root()->begin() && it->size() ); /* FIXME ? */
364 if( it == root()->begin() ) it = firstLeaf();
368 VarTree::Iterator VarTree::findById( int id )
370 for (Iterator it = begin(); it != end(); ++it )
376 Iterator result = it->findById( id );
377 if( result != it->end() ) return result;
383 void VarTree::ensureExpanded( VarTree::Iterator it )
385 /// Don't expand ourselves, only our parents
386 VarTree *current = &(*it);
387 current = current->parent();
388 while( current->parent() != NULL )
390 current->m_expanded = true;
391 current = current->parent();
395 int VarTree::countLeafs()
397 if( size() == 0 ) return 1;
400 Iterator it = begin();
403 i_count += it->countLeafs();
409 VarTree::Iterator VarTree::firstLeaf()
411 Iterator b = root()->begin();
412 if( b->size() ) return getNextLeaf( b );
416 void VarTree::cascadeDelete()
419 for( Iterator it = begin(); it != end(); ++it )