Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
picker.h
Go to the documentation of this file.
1 /*
2  * Vega Strike
3  * Copyright (C) 2003 Mike Byron
4  *
5  * http://vegastrike.sourceforge.net/
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */
21 
22 #ifndef __PICKER_H__
23 #define __PICKER_H__
24 
25 #include "control.h"
26 #include "painttext.h"
27 
28 #include <list>
29 
30 //See cpp file for detailed descriptions of classes, functions, etc.
31 
32 //FORWARD REFERENCES.
33 class PickerCells;
34 class Scroller;
35 
36 //One entry in a Picker.
37 //This is a virtual base class that is the minimum needed to display
38 //cells in a Picker.
40 {
41 public:
42  virtual ~PickerCell() {}
43 //The text to be displayed.
44  virtual std::string text( void ) const = 0;
45 //A unique identifier for the cell.
46  virtual std::string id( void ) const = 0;
47 //The color of the text.
48  virtual GFXColor textColor( void ) const = 0;
49 //List of children.
50  virtual PickerCells * children( void ) const = 0;
51 //Unique identifier for this cell.
52  virtual int tag( void ) const = 0;
53 
54 //Whether to hide or show child cells.
55  virtual bool hideChildren( void ) const
56  {
57  return m_hideChildren;
58  }
59 //Set whether to hide or show child cells.
60  virtual void setHideChildren( bool f )
61  {
62  m_hideChildren = f;
63  }
64 
66 
67 protected:
69 };
70 
71 //A list of Picker cells to show.
72 //This is a virtual base class that is the minimum needed to display
73 //cells in a Picker.
75 {
76 public:
77  virtual ~PickerCells() {}
78 //Number of cells in this list.
79  virtual int count( void ) const = 0;
80 //Get a particular cell.
81  virtual PickerCell * cellAt( int index ) = 0;
82 //Get a particular cell (const).
83  virtual const PickerCell * cellAt( int index ) const = 0;
84 //Find a cell by id. Returns NULL if not found.
85  virtual PickerCell * cellWithId( const std::string &id );
86 
87 //Utility functions:
88 //Saves all open children categories in a list.
89 //Returns true if this list directly contains the selectedCell.
90  bool saveOpenCategories( std::list< std::list< std::string > > &masterList,
91  const std::list< std::string > &parentHier,
92  PickerCell *selectedCell ) const;
93 };
94 
95 //The Picker class supports a list of items that can be
96 //scrolled and selected.
97 //The list can be a tree. When a branch is selected, its children
98 //are displayed. When it is selected again, they are hidden.
99 class Picker : public Control
100 {
101 public:
102 
103  void saveOpenCategories( std::list< std::list< std::string > > &idList ) const;
104  int restoreOpenCategories( const std::list< std::list< std::string > > &idList );
105 
106 //Draw the list.
107  virtual void draw( void );
108 
109 //Get cell collection.
110  virtual PickerCells * cells( void )
111  {
112  return m_cells;
113  }
114  virtual const PickerCells * cells( void ) const
115  {
116  return m_cells;
117  }
118 
119 //Make a cell selected.
120  virtual void selectCell( PickerCell *cell, bool scroll = false );
121 
122 //Return the cell that is currently selected. Can be NULL.
123  virtual PickerCell * selectedCell( void )
124  {
125  return m_selectedCell;
126  }
127 
128 //Return the index of the current selected cell in the list of cells.
129 //This can only be used if the list simple, not a tree.
130 //Returns -1 if no selection, or if the selection is a child.
131  virtual int selectedItem( void );
132 
133 //Make sure the cell is visible in the scroll area. If it is, nothing
134 //happens. If it's not, we move it into the visible section.
135 //If NULL, this routine does nothing.
136  virtual void scrollToCell( const PickerCell *cell, bool middle = false );
137 
138 //This should be called when the lists of cells have been changed
139 //in some way -- added to, "hide children" changed, etc.
140 //It causes this object to figure out from the cell lists which
141 //cells are actually being displayed in the control.
142  virtual void setMustRecalc( void )
143  {
144  m_needRecalcDisplay = true;
145  }
146 
147 //Extra space between cells. This is based on font size.
148 //1.0 = Cells are twice as high as they would normally be.
149 //Cells are vertically centered in the height alloted.
150 //This can be negative, which will remove space, but text
151 //quality may be affected.
152  virtual float extraCellHeight( void )
153  {
154  return m_extraCellHeight;
155  }
156  virtual void setExtraCellHeight( float f )
157  {
158  m_extraCellHeight = f;
159  }
160 
161 //Background color when cell is selected.
162  virtual GFXColor selectionColor( void )
163  {
164  return m_selectionColor;
165  }
166  virtual void setSelectionColor( const GFXColor &c )
167  {
169  }
170 
171 //Text color when mouse is over button.
172  virtual GFXColor selectionTextColor( void )
173  {
174  return m_selectionTextColor;
175  }
176  virtual void setSelectionTextColor( const GFXColor &c )
177  {
179  }
180 
181 //Background color when mouse is over button.
182  virtual GFXColor highlightColor( void )
183  {
184  return m_highlightColor;
185  }
186  virtual void setHighlightColor( const GFXColor &c )
187  {
189  }
190 
191 //Text color when mouse is over button.
192  virtual GFXColor highlightTextColor( void )
193  {
194  return m_highlightTextColor;
195  }
196  virtual void setHighlightTextColor( const GFXColor &c )
197  {
199  }
200 
201 //Text margins.
202  virtual Size textMargins( void )
203  {
204  return m_textMargins;
205  }
206  virtual void setTextMargins( const Size &s )
207  {
208  m_textMargins = s;
209  }
210 
211 //Set the object that takes care of scrolling.
212  virtual void setScroller( Scroller *s );
213 
214 //OVERRIDES
215  virtual bool processMouseDown( const InputEvent &event );
216  virtual bool processMouseUp( const InputEvent &event );
217  virtual bool processMouseMove( const InputEvent &event );
218 
219 //Process a command event.
220  virtual bool processCommand( const EventCommandId &command, Control *control );
221 
222 //CONSTRUCTION
223 public: Picker( void );
224  virtual ~Picker( void );
225 
226 protected:
227 //INTERNAL IMPLEMENTATION
228 //The total vertical space between displayed cells.
229  float totalCellHeight( void )
230  {
231  return m_font.size()+m_extraCellHeight;
232  }
233 
234 //Find the cell that corresponds to a point in the control.
235  PickerCell * cellForMouse( const Point &p );
236 
237 //Reload the list of cells that are being displayed.
238 //This should be called when a change is made in the lists of cells, or
239 //when we scroll, which again changes the cells we display.
240 //It does not need to be called for text or color changes, only when
241 //cells are added or removed, etc.
242  virtual void recalcDisplay( void );
243 
244 //Recursive routine that goes through a cell list and the children
245 //of the cells and puts them on the display list.
246  void addListToDisplay( PickerCells *list, int level );
247 
248 //VARIABLES
249 protected:
250 //Struct to store displayed cells.
251  struct DisplayCell
252  {
254  int level; //How much indent for this cell. 0 = no indent.
255  PaintText paintText; //Object containing drawn text.
256 
257  DisplayCell( PickerCell *c = NULL, int l = 0 ) : cell( c )
258  , level( l )
259  , paintText() {}
260  };
261 
262  PickerCells *m_cells; //The collection containing the list entries.
263  GFXColor m_selectionColor; //Selected cell background color.
264  GFXColor m_selectionTextColor; //Selected cell text color.
265  GFXColor m_highlightColor; //Highlighted cell background color.
266  GFXColor m_highlightTextColor; //Highlighted cell text color.
267  float m_extraCellHeight; //Extra height to be added to each cell. 1.0 for whole cell height.
268  Size m_textMargins; //Inset area where no text appears.
269  PickerCell *m_cellPressed; //Item that mouse-down came on.
270  const PickerCell *m_highlightedCell; //Cell that mouse is currently over.
271  PickerCell *m_selectedCell; //Cell that is currently selected.
273  size_t m_scrollPosition; //Index of first display cell shown.
274  bool m_needRecalcDisplay; //True = Need to recalculate the displayed cells.
275  std::vector< DisplayCell >m_displayCells; //Array of cells currently displayed.
276 };
277 
278 #endif //__PICKER_H__
279