]> git.sesse.net Git - vlc/blob - modules/gui/skins2/utils/var_tree.hpp
26709e32aae19b84b377fbbefcc8c3e5f5e2a1d1
[vlc] / modules / gui / skins2 / utils / var_tree.hpp
1 /*****************************************************************************
2  * var_tree.hpp
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Antoine Cellerier <dionoea@videolan.org>
8  *          ClĂ©ment Stenac <zorglub@videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 #ifndef VAR_TREE_HPP
26 #define VAR_TREE_HPP
27
28 #include <list>
29
30 #include "variable.hpp"
31 #include "observer.hpp"
32 #include "ustring.hpp"
33 #include "var_percent.hpp"
34
35 /// Description of an update to the tree
36 typedef struct tree_update
37 {
38      int i_type;
39      int i_parent;
40      int i_id;
41      bool b_active_item;
42      bool b_visible;
43 } tree_update;
44
45 /// Tree variable
46 class VarTree: public Variable, public Subject<VarTree, tree_update>
47 {
48 public:
49     VarTree( intf_thread_t *pIntf );
50
51     VarTree( intf_thread_t *pIntf, VarTree *pParent, int id,
52              const UStringPtr &rcString, bool selected, bool playing,
53              bool expanded,bool readonly, void *pData );
54
55     virtual ~VarTree();
56
57     /// Get the variable type
58     virtual const string &getType() const { return m_type; }
59
60     /// Add a pointer on string in the children's list
61     virtual void add( int id, const UStringPtr &rcString, bool selected,
62                       bool playing, bool expanded, bool readonly,
63                       void *pData );
64
65     /// Remove the selected item from the children's list
66     virtual void delSelected();
67
68     /// Remove all elements from the children's list
69     virtual void clear();
70
71     /// \todo Use accessors for these fields ?
72     int m_id;
73     UStringPtr m_cString;
74     bool m_selected;
75     bool m_playing;
76     bool m_expanded;
77     bool m_deleted;
78     void *m_pData;
79
80     inline bool isReadonly() { return m_readonly; };
81
82     /// Get the number of children
83     int size() const { return m_children.size(); }
84
85     /// Iterators
86     typedef list<VarTree>::iterator Iterator;
87     typedef list<VarTree>::const_iterator ConstIterator;
88
89     /// Begining of the children's list
90     Iterator begin() { return m_children.begin(); }
91     ConstIterator begin() const { return m_children.begin(); }
92
93     /// End of children's list
94     Iterator end() { return m_children.end(); }
95     ConstIterator end() const { return m_children.end(); }
96
97     /// Back of children's list
98     VarTree &back() { return m_children.back(); }
99
100     /// Return an iterator on the n'th element of the children's list
101     Iterator operator[]( int n );
102     ConstIterator operator[]( int n ) const;
103
104     /// Parent node
105     VarTree *parent() { return m_pParent; }
106
107     /// Get next sibling
108     Iterator getNextSibling( Iterator );
109
110     Iterator next_uncle();
111     Iterator prev_uncle();
112
113     /// Get first leaf
114     Iterator firstLeaf();
115
116     void removeChild( VarTree::Iterator item )
117     {
118         m_children.erase( item );
119     }
120
121     /// Execute the action associated to this item
122     virtual void action( VarTree *pItem ) { }
123
124     /// Get a reference on the position variable
125     VarPercent &getPositionVar() const
126     { return *((VarPercent*)m_cPosition.get()); }
127
128     /// Get a counted pointer on the position variable
129     const VariablePtr &getPositionVarPtr() const { return m_cPosition; }
130
131     /// Count the number of items that should be displayed if the
132     /// playlist window wasn't limited
133     int visibleItems();
134
135     /// Count the number of leafs in the tree
136     int countLeafs();
137
138     /// Return iterator to the n'th visible item
139     Iterator getVisibleItem( int n );
140
141     /// Return iterator to the n'th leaf
142     Iterator getLeaf( int n );
143
144     /// Given an iterator to a visible item, return the next visible item
145     Iterator getNextVisibleItem( Iterator it );
146
147     /// Given an it to a visible item, return the previous visible item
148     Iterator getPrevVisibleItem( Iterator it );
149
150     /// Given an iterator to an item, return the next item
151     Iterator getNextItem( Iterator it );
152
153     /// Given an iterator to an item, return the previous item
154     Iterator getPrevItem( Iterator it );
155
156     /// Given an iterator to an item, return the next leaf
157     Iterator getNextLeaf( Iterator it );
158
159     /// Given an iterator to an item, return the previous leaf
160     Iterator getPrevLeaf( Iterator it );
161
162     /// Find a children node with the given id
163     Iterator findById( int id );
164
165     /// Ensure an item is expanded
166     void ensureExpanded( VarTree::Iterator );
167
168     /// flag a whole subtree for deletion
169     void cascadeDelete();
170
171     /// Get depth (root depth is 0)
172     int depth()
173     {
174         VarTree *parent = this;
175         int depth = 0;
176         while( ( parent = parent->parent() ) != NULL )
177             depth++;
178         return depth;
179     }
180
181
182 private:
183
184     /// Get root node
185     VarTree *root()
186     {
187         VarTree *parent = this;
188         while( parent->parent() != NULL )
189             parent = parent->parent();
190         return parent;
191     }
192
193     /// List of children
194     list<VarTree> m_children;
195
196     /// Pointer to parent node
197     VarTree *m_pParent;
198
199     bool m_readonly;
200
201     /// Variable type
202     static const string m_type;
203
204     /// Position variable
205     VariablePtr m_cPosition;
206 };
207
208 #endif