var_tree.hpp 5.06 KB
Newer Older
1 2 3 4
/*****************************************************************************
 * var_tree.hpp
 *****************************************************************************
 * Copyright (C) 2005 VideoLAN
5
 * $Id$
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
 *
 * Authors: Antoine Cellerier <dionoea@videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

#ifndef VAR_TREE_HPP
#define VAR_TREE_HPP

#include <list>

#include "variable.hpp"
#include "observer.hpp"
#include "ustring.hpp"
#include "var_percent.hpp"

zorglub's avatar
zorglub committed
34 35 36 37 38 39
/// Description of an update to the tree
typedef struct tree_update
{
     int i_type;
     int i_parent;
     int i_id;
zorglub's avatar
zorglub committed
40
     bool b_visible;
zorglub's avatar
zorglub committed
41 42
} tree_update;

43
/// Tree variable
zorglub's avatar
zorglub committed
44
class VarTree: public Variable, public Subject<VarTree, tree_update*>
45 46
{
    public:
47 48 49 50 51 52
        VarTree( intf_thread_t *pIntf );

        VarTree( intf_thread_t *pIntf, VarTree *pParent, int id,
                 const UStringPtr &rcString, bool selected, bool playing,
                 bool expanded, void *pData );

53 54 55 56 57 58
        virtual ~VarTree();

        /// Get the variable type
        virtual const string &getType() const { return m_type; }

        /// Add a pointer on string in the children's list
59 60
        virtual void add( int id, const UStringPtr &rcString, bool selected,
                          bool playing, bool expanded, void *pData );
61 62 63 64 65 66 67

        /// Remove the selected item from the children's list
        virtual void delSelected();

        /// Remove all elements from the children's list
        virtual void clear();

68
        /// \todo Use accessors for these fields ?
69
        int m_id;
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
        UStringPtr m_cString;
        bool m_selected;
        bool m_playing;
        bool m_expanded;
        void *m_pData;

        /// Get the number of children
        int size() const { return m_children.size(); }

        /// Iterators
        typedef list<VarTree>::iterator Iterator;
        typedef list<VarTree>::const_iterator ConstIterator;

        /// Begining of the children's list
        Iterator begin() { return m_children.begin(); }
        ConstIterator begin() const { return m_children.begin(); }

        /// End of children's list
        Iterator end() { return m_children.end(); }
        ConstIterator end() const { return m_children.end(); }

        /// Back of children's list
        VarTree &back() { return m_children.back(); }

        /// Return an iterator on the n'th element of the children's list
        Iterator operator[]( int n );
        ConstIterator operator[]( int n ) const;

        /// Parent node
        VarTree *parent() { return m_pParent; }
ipkiss's avatar
ipkiss committed
100
        void VarTree::checkParents( VarTree *pParent );
101 102 103 104

        Iterator uncle();

        /// Get root node
ipkiss's avatar
ipkiss committed
105 106 107 108 109 110 111
        VarTree *root()
        {
            VarTree *parent = this;
            while( parent->parent() != NULL )
                parent = parent->parent();
            return parent;
        }
112 113

        /// Get depth (root depth is 0)
ipkiss's avatar
ipkiss committed
114 115 116 117 118 119 120 121
        int depth()
        {
            VarTree *parent = this;
            int depth = 0;
            while( ( parent = parent->parent() ) != NULL )
                depth++;
            return depth;
        }
122 123 124 125 126 127

        /// Execute the action associated to this item
        virtual void action( VarTree *pItem ) {}

        /// Get a reference on the position variable
        VarPercent &getPositionVar() const
ipkiss's avatar
ipkiss committed
128
        { return *((VarPercent*)m_cPosition.get()); }
129 130 131 132 133 134 135 136

        /// Get a counted pointer on the position variable
        const VariablePtr &getPositionVarPtr() const { return m_cPosition; }

        /// Count the number of items that should be displayed if the playlist window wasn't limited
        int visibleItems();

        /// Return iterator to the n'th visible item
ipkiss's avatar
ipkiss committed
137 138 139 140
        Iterator getVisibleItem( int n );

        /// Given an iterator to a visible item, return the next visible item
        Iterator getNextVisibleItem( Iterator it );
141

zorglub's avatar
zorglub committed
142 143 144
        /// Given an iterator to an item, return the next item
        Iterator getNextItem( Iterator it );

145 146
        /// Find a children node with the given id
        Iterator findById( int id );
147

zorglub's avatar
zorglub committed
148 149 150
        /// Ensure an item is expanded
        void ensureExpanded( VarTree::Iterator );

151
    private:
ipkiss's avatar
ipkiss committed
152
        /// List of children
153 154
        list<VarTree> m_children;

ipkiss's avatar
ipkiss committed
155
        /// Pointer to parent node
156 157
        VarTree *m_pParent;

ipkiss's avatar
ipkiss committed
158
        /// Variable type
159 160 161 162 163 164 165
        static const string m_type;

        /// Position variable
        VariablePtr m_cPosition;
};

#endif