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 along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
31 #include "variable.hpp"
32 #include "observer.hpp"
33 #include "ustring.hpp"
34 #include "var_percent.hpp"
40 class VarTree: public Variable,
41 public Subject<VarTree, tree_update>,
42 public Observer<VarPercent>
45 VarTree( intf_thread_t *pIntf );
47 VarTree( intf_thread_t *pIntf, VarTree *pParent, int id,
48 const UStringPtr &rcString, bool selected, bool playing,
49 bool expanded, bool readonly );
50 VarTree( const VarTree& );
55 typedef list<VarTree>::iterator Iterator;
56 typedef list<VarTree>::const_iterator ConstIterator;
58 /// Get the variable type
59 virtual const string &getType() const { return m_type; }
61 /// Add a pointer on string in the children's list
62 virtual Iterator add( int id, const UStringPtr &rcString, bool selected,
63 bool playing, bool expanded, bool readonly, int pos = -1 );
65 /// Remove the selected item from the children's list
66 virtual void delSelected();
68 /// Remove all elements from the children's list
71 inline int getId() { return m_id; }
72 inline UString* getString() {return (UString*)m_cString.get(); }
73 inline void setString( UStringPtr val ) { m_cString = val; }
75 inline bool isReadonly() { return m_readonly; };
76 inline bool isSelected() { return m_selected; };
77 inline bool isPlaying() { return m_playing; };
78 inline bool isExpanded() { return m_expanded; };
79 inline bool isFlat() { return m_flat; };
81 inline void setSelected( bool val ) { m_selected = val; }
82 inline void setPlaying( bool val ) { m_playing = val; }
83 inline void setExpanded( bool val ) { m_expanded = val; }
84 inline void setFlat( bool val ) { m_flat = val; }
86 inline void toggleSelected() { m_selected = !m_selected; }
87 inline void toggleExpanded() { setExpanded( !m_expanded ); }
89 /// Get the number of children
90 int size() const { return m_children.size(); }
92 /// iterator over visible items
93 class IteratorVisible : public Iterator
96 IteratorVisible( const VarTree::Iterator& it, VarTree* pRootTree )
97 : VarTree::Iterator( it ), m_pRootTree( pRootTree ) {}
99 IteratorVisible& operator++()
101 Iterator& it = *this;
102 assert( it != end() );
103 it = isFlat() ? m_pRootTree->getNextLeaf( it ) :
104 m_pRootTree->getNextVisibleItem( it );
108 IteratorVisible& operator--()
110 Iterator& it = *this;
111 it = isFlat() ? m_pRootTree->getPrevLeaf( it ) :
112 m_pRootTree->getPrevVisibleItem( it );
116 IteratorVisible getParent()
118 IteratorVisible& it = *this;
119 if( it->parent() && it->parent() != m_pRootTree )
121 return IteratorVisible( it->parent()->getSelf(), m_pRootTree );
127 inline IteratorVisible begin() { return m_pRootTree->begin(); }
128 inline IteratorVisible end() { return m_pRootTree->end(); }
129 inline bool isFlat() { return m_pRootTree->m_flat; }
130 VarTree* m_pRootTree;
133 /// Beginning of the children's list
134 IteratorVisible begin()
136 return IteratorVisible(
137 m_flat ? firstLeaf() : m_children.begin(), this );
140 /// End of children's list
141 IteratorVisible end() { return IteratorVisible( m_children.end(), this ); }
143 /// Back of children's list
144 VarTree &back() { return m_children.back(); }
147 VarTree *parent() { return m_pParent; }
150 Iterator getNextSiblingOrUncle();
151 Iterator getPrevSiblingOrUncle();
156 Iterator it = m_pParent->m_children.begin();
157 for( ; &*it != this && it != m_pParent->m_children.end(); ++it );
158 assert( it != m_pParent->m_children.end() );
167 for( Iterator it = m_pParent->m_children.begin();
168 it != m_pParent->m_children.end(); ++it, i_pos++ )
175 Iterator next_uncle();
176 Iterator prev_uncle();
179 Iterator firstLeaf();
182 void removeChild( Iterator it ) { m_children.erase( it ); }
184 /// Execute the action associated to this item
185 virtual void action( VarTree *pItem ) { VLC_UNUSED(pItem); }
187 /// Get a reference on the position variable
188 VarPercent &getPositionVar() const
189 { return *((VarPercent*)m_cPosition.get()); }
191 /// Get a counted pointer on the position variable
192 const VariablePtr &getPositionVarPtr() const { return m_cPosition; }
194 /// Count the number of items that should be displayed if the
195 /// playlist window wasn't limited
198 /// Count the number of leafs in the tree
201 /// Return iterator to the n'th visible item
202 Iterator getVisibleItem( int n );
204 /// Return iterator to the n'th leaf
205 Iterator getLeaf( int n );
207 /// Given an iterator to a visible item, return the next visible item
208 Iterator getNextVisibleItem( Iterator it );
210 /// Given an it to a visible item, return the previous visible item
211 Iterator getPrevVisibleItem( Iterator it );
213 /// Given an iterator to an item, return the next item
214 Iterator getNextItem( Iterator it );
216 /// Given an iterator to an item, return the previous item
217 Iterator getPrevItem( Iterator it );
219 /// Given an iterator to an item, return the next leaf
220 Iterator getNextLeaf( Iterator it );
222 /// Given an iterator to an item, return the previous leaf
223 Iterator getPrevLeaf( Iterator it );
225 /// Given an iterator to an item, return the parent item
226 Iterator getParent( Iterator it );
228 /// return index of visible item (starting from 0)
229 int getIndex( const Iterator& it );
231 /// Ensure an item is expanded
232 void ensureExpanded( const Iterator& it );
235 Iterator getItemFromSlider();
236 void setSliderFromItem( const Iterator& it );
239 void onUpdate( Subject<VarPercent> &rPercent, void* arg);
241 /// Get depth (root depth is 0)
244 VarTree *parent = this;
246 while( ( parent = parent->parent() ) != NULL )
251 virtual void onUpdateSlider() {}
255 VarTree::IteratorVisible getItem( int index );
260 list<VarTree> m_children;
267 VarTree *parent = this;
268 while( parent->parent() != NULL )
269 parent = parent->parent();
273 /// Pointer to parent node
277 UStringPtr m_cString;
288 static const string m_type;
290 /// Position variable
291 VariablePtr m_cPosition;
294 /// Description of an update to the tree
295 typedef struct tree_update
307 VarTree::IteratorVisible it;
309 tree_update( enum type_t t, VarTree::IteratorVisible item ) :
310 type( t ), it( item ) {}