1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2005 VideoLAN
5 * $Id: var_bool.hpp 9934 2005-02-15 13:55:08Z courmisch $
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., 59 Temple Place - Suite 330, Boston, MA 02111, 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( true ), 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 ) );
63 void VarTree::delSelected()
65 Iterator it = begin();
69 if( size() ) it->delSelected();
75 m_children.erase( oldIt );
90 VarTree::Iterator VarTree::operator[]( int n )
94 for( it = begin(), i = 0;
100 VarTree::ConstIterator VarTree::operator[]( int n ) const
104 for( it = begin(), i = 0;
105 i < n && it != end();
110 /* find iterator to next ancestor
111 * ... which means parent++ or grandparent++ or grandgrandparent++ ... */
112 VarTree::Iterator VarTree::uncle()
114 VarTree *p_parent = parent();
115 if( p_parent != NULL )
117 VarTree *p_grandparent = p_parent->parent();
118 while( p_grandparent != NULL )
120 Iterator it = p_grandparent->begin();
121 while( it != p_grandparent->end() && &(*it) != p_parent ) it++;
122 if( it != p_grandparent->end() )
125 if( it != p_grandparent->end() )
130 if( p_grandparent->parent() )
132 p_parent = p_grandparent;
133 p_grandparent = p_parent->parent();
136 p_grandparent = NULL;
140 /* if we didn't return before, it means that we've reached the end */
141 return root()->end();
144 void VarTree::checkParents( VarTree *pParent )
147 Iterator it = begin();
150 it->checkParents( this );
155 int VarTree::visibleItems()
157 int i_count = size();
158 Iterator it = begin();
163 i_count += it->visibleItems();
170 VarTree::Iterator VarTree::getVisibleItem( int n )
172 Iterator it = begin();
176 if( n <= 0 ) return it;
179 int i = n - it->visibleItems();
180 if( i <= 0 ) return it->getVisibleItem( n );
188 VarTree::Iterator VarTree::getNextVisibleItem( Iterator it )
190 if( it->m_expanded && it->size() )
196 VarTree::Iterator it_old = it;
198 // Was 'it' the last brother? If so, look for uncles
199 if( it_old->parent() && it_old->parent()->end() == it )
201 it = it_old->uncle();
207 VarTree::Iterator VarTree::findById( int id )
209 for (Iterator it = begin(); it != end(); ++it )
215 Iterator result = it->findById( id );
216 if( result != it->end() ) return result;