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