1 /*****************************************************************************
2  * ctrl_tree.hpp
3  *****************************************************************************
4  * Copyright (C) 2003 the VideoLAN team
5  * $Id: 15284a429e402d312164e47f888a3a9485cf5009 $
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 CTRL_TREE_HPP
26 #define CTRL_TREE_HPP
27 
28 #include "ctrl_generic.hpp"
29 #include "../utils/observer.hpp"
30 #include "../utils/var_tree.hpp"
31 
32 class OSGraphics;
33 class GenericFont;
34 class GenericBitmap;
35 
36 /// Class for control tree
37 class CtrlTree: public CtrlGeneric, public Observer<VarTree, tree_update>
38 {
39 public:
40     typedef VarTree::IteratorVisible Iterator;
41 
42     CtrlTree( intf_thread_t *pIntf,
43               VarTree &rTree,
44               const GenericFont &rFont,
45               const GenericBitmap *pBgBitmap,
46               const GenericBitmap *pItemBitmap,
47               const GenericBitmap *pOpenBitmap,
48               const GenericBitmap *pClosedBitmap,
49               uint32_t fgColor,
50               uint32_t playColor,
51               uint32_t bgColor1,
52               uint32_t bgColor2,
53               uint32_t selColor,
54               const UString &rHelp,
55               VarBool *pVisible,
56               VarBool *pFlat );
57     virtual ~CtrlTree();
58 
59     /// Handle an event on the control
60     virtual void handleEvent( EvtGeneric &rEvent );
61 
62     /// Check whether coordinates are inside the control
63     virtual bool mouseOver( int x, int y ) const;
64 
65     /// Draw the control on the given graphics
66     virtual void draw( OSGraphics &rImage, int xDest, int yDest, int w, int h );
67 
68     /// Called when the layout is resized
69     virtual void onResize();
70 
71     /// Return true if the control can gain the focus
isFocusable() const72     virtual bool isFocusable() const { return true; }
73 
74     /// Return true if the control can be scrollable
isScrollable() const75     virtual bool isScrollable() const { return true; }
76 
77     /// Get the type of control (custom RTTI)
getType() const78     virtual std::string getType() const { return "tree"; }
79 
80     /// Make sure an item is visible
81     /// \param item an iterator to a tree item
82     /// \return true if it changed the position
83     bool ensureVisible( const Iterator& it );
84 
85 private:
86     /// Tree associated to the control
87     VarTree &m_rTree;
88     /// Font
89     const GenericFont &m_rFont;
90     /// Background bitmap
91     const GenericBitmap *m_pBgBitmap;
92     /// Item (leaf) bitmap
93     // (TODO : add different bitmaps for different item types
94     //         like in the wx playlist)
95     const GenericBitmap *m_pItemBitmap;
96     /// Open (expanded) node bitmap
97     const GenericBitmap *m_pOpenBitmap;
98     /// Closed node bitmap
99     const GenericBitmap *m_pClosedBitmap;
100     /// scaled bitmap
101     GenericBitmap *m_pScaledBitmap;
102     /// Image of the control
103     OSGraphics *m_pImage;
104 
105     /// Color of normal test
106     uint32_t m_fgColor;
107     /// Color of the playing item
108     uint32_t m_playColor;
109     /// Background colors, used when no background bitmap is given
110     uint32_t m_bgColor1, m_bgColor2;
111     /// Background of selected items
112     uint32_t m_selColor;
113 
114     /// First item in the visible area
115     Iterator m_firstPos;
116     /// Pointer on the last clicked item in the tree
117     Iterator m_lastClicked;
118     ///
119     Iterator m_itOver;
120 
121     /// Do we want to "flaten" the tree ?
122     bool m_flat;
123     /// Number of visible lines
124     float m_capacity;
125     /// flag for item deletion
126     bool m_bRefreshOnDelete;
127 
128     /// Method called when the tree variable is modified
129     virtual void onUpdate( Subject<VarTree, tree_update> &rTree,
130                            tree_update *);
131 
132     /// Called when the position is set
133     virtual void onPositionChange();
134 
135     /// Compute the number of lines that can be displayed
136     float maxItems();
137 
138     /// Compute the item's height (depends on fonts and images used)
139     int itemHeight();
140 
141     /// Compute the width of an item's bitmap
142     int itemImageWidth();
143 
144     /// Draw the image of the control
145     void makeImage();
146 
147     /// Return the n'th displayed item (starting at position 0)
148     Iterator findItemAtPos( int n );
149 
150     /// return the nearest item
151     Iterator getNearestItem( const Iterator& it );
152 
153     /// return whether the item is visible or not
154     bool isItemVisible( const Iterator& it );
155 
156     void setSliderFromFirst();
157     Iterator getFirstFromSlider();
158     void setScrollStep();
159 };
160 
161 #endif
162