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_deleted( false ), m_readonly( false),
32 m_pData( NULL ), m_pParent( NULL )
34 // Create the position variable
35 m_cPosition = VariablePtr( new VarPercent( pIntf ) );
36 getPositionVar().set( 1.0 );
39 VarTree::VarTree( intf_thread_t *pIntf, VarTree *pParent, int id,
40 const UStringPtr &rcString, bool selected, bool playing,
41 bool expanded, bool readonly,
43 : Variable( pIntf ), m_id( id ), m_cString( rcString ),
44 m_selected( selected ), m_playing( playing ), m_expanded( expanded ),
45 m_deleted( false ), m_readonly( readonly ),
46 m_pData( pData ), m_pParent( pParent )
48 // Create the position variable
49 m_cPosition = VariablePtr( new VarPercent( pIntf ) );
50 getPositionVar().set( 1.0 );
55 /// \todo check that children are deleted
58 void VarTree::add( int id, const UStringPtr &rcString, bool selected,
59 bool playing, bool expanded, bool readonly,
62 m_children.push_back( VarTree( getIntf(), this, id, rcString, selected,
63 playing, expanded, readonly,
67 void VarTree::delSelected()
69 Iterator it = begin();
73 if( size() ) it->delSelected();
79 m_children.erase( oldIt );
93 VarTree::Iterator VarTree::operator[]( int n )
97 for( it = begin(), i = 0;
103 VarTree::ConstIterator VarTree::operator[]( int n ) const
107 for( it = begin(), i = 0;
108 i < n && it != end();
113 VarTree::Iterator VarTree::getNextSibling( VarTree::Iterator current )
115 VarTree *p_parent = current->parent();
116 if( p_parent && current != p_parent->end() )
118 Iterator it = current->parent()->begin();
119 while( it != p_parent->end() && it != current ) it++;
120 if( it != p_parent->end() )
124 return root()->end();
126 return root()->end();
129 /* find iterator to next ancestor
130 * ... which means parent++ or grandparent++ or grandgrandparent++ ... */
131 VarTree::Iterator VarTree::next_uncle()
133 VarTree *p_parent = parent();
134 if( p_parent != NULL )
136 VarTree *p_grandparent = p_parent->parent();
137 while( p_grandparent != NULL )
139 Iterator it = p_grandparent->begin();
140 while( it != p_grandparent->end() && &(*it) != p_parent ) it++;
141 if( it != p_grandparent->end() )
144 if( it != p_grandparent->end() )
149 if( p_grandparent->parent() )
151 p_parent = p_grandparent;
152 p_grandparent = p_parent->parent();
155 p_grandparent = NULL;
159 /* if we didn't return before, it means that we've reached the end */
160 return root()->end();
163 VarTree::Iterator VarTree::prev_uncle()
165 VarTree *p_parent = parent();
166 if( p_parent != NULL )
168 VarTree *p_grandparent = p_parent->parent();
169 while( p_grandparent != NULL )
171 Iterator it = p_grandparent->end();
172 while( it != p_grandparent->begin() && &(*it) != p_parent ) it--;
173 if( it != p_grandparent->begin() )
176 if( it != p_grandparent->begin() )
181 if( p_grandparent->parent() )
183 p_parent = p_grandparent;
184 p_grandparent = p_parent->parent();
187 p_grandparent = NULL;
191 /* if we didn't return before, it means that we've reached the end */
192 return root()->begin();
196 void VarTree::checkParents( VarTree *pParent )
199 Iterator it = begin();
202 it->checkParents( this );
207 int VarTree::visibleItems()
209 int i_count = size();
210 Iterator it = begin();
215 i_count += it->visibleItems();
222 VarTree::Iterator VarTree::getVisibleItem( int n )
224 Iterator it = begin();
228 if( n <= 0 ) return it;
231 int i = n - it->visibleItems();
232 if( i <= 0 ) return it->getVisibleItem( n );
240 VarTree::Iterator VarTree::getNextVisibleItem( Iterator it )
242 if( it->m_expanded && it->size() )
248 VarTree::Iterator it_old = it;
250 // Was 'it' the last brother? If so, look for uncles
251 if( it_old->parent() && it_old->parent()->end() == it )
253 it = it_old->next_uncle();
259 VarTree::Iterator VarTree::getPrevVisibleItem( Iterator it )
261 VarTree::Iterator it_old = it;
262 if( it == root()->begin() || it == ++(root()->begin()) ) return it;
266 /* Was it the first child of its parent ? */
267 if( it->parent() && it == it->parent()->begin() )
269 /* Yes, get previous uncle */
270 it = it_old->prev_uncle();
275 /* We have found an expanded uncle, take its last child */
276 while( it != root()->begin() && it->size() && it->m_expanded )
284 VarTree::Iterator VarTree::getNextItem( Iterator it )
292 VarTree::Iterator it_old = it;
294 // Was 'it' the last brother? If so, look for uncles
295 if( it_old->parent() && it_old->parent()->end() == it )
297 it = it_old->next_uncle();
304 VarTree::Iterator VarTree::findById( int id )
306 for (Iterator it = begin(); it != end(); ++it )
312 Iterator result = it->findById( id );
313 if( result != it->end() ) return result;
319 void VarTree::ensureExpanded( VarTree::Iterator it )
321 /// Don't expand ourselves, only our parents
322 VarTree *current = &(*it);
323 current = current->parent();
324 while( current->parent() != NULL )
326 current->m_expanded = true;
327 current = current->parent();