1 /***************************************************************************
2                   scrollinglist.h  -  Scrollable list widget
3                              -------------------
4     begin                : Thu Aug 28 2003
5     copyright            : (C) 2003 by Gabor Torok
6     email                : cctorok@yahoo.com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  *                                                                         *
11  *   This program is free software; you can redistribute it and/or modify  *
12  *   it under the terms of the GNU General Public License as published by  *
13  *   the Free Software Foundation; either version 2 of the License, or     *
14  *   (at your option) any later version.                                   *
15  *                                                                         *
16  ***************************************************************************/
17 
18 #ifndef SCROLLING_LIST_H
19 #define SCROLLING_LIST_H
20 #pragma once
21 
22 #include "gui.h"
23 #include "widget.h"
24 #include "button.h"
25 #include "window.h"
26 #include "draganddrop.h"
27 
28 /**
29   *@author Gabor Torok
30   */
31 
32 /// A flexible list widget that can also display icons next to entries.
33 class ScrollingList : public Widget {
34 protected:
35 	std::vector<std::string> list;
36 	const Color *colors;
37 	Texture* icons; // pointer to array of icon textures
38 	int value;
39 	int scrollerWidth, scrollerHeight;
40 	int listHeight;
41 	float alpha, alphaInc;
42 	GLint lastTick;
43 	bool inside;
44 	int scrollerY;
45 	bool dragging;
46 	int dragX, dragY;
47 	int *selectedLine;
48 	int selectedLineCount;
49 	DragAndDropHandler *dragAndDropHandler;
50 	bool innerDrag;
51 	int innerDragX, innerDragY;
52 	bool highlightBorders;
53 	Texture highlight;
54 	bool canGetFocusVar;
55 	int lineHeight;
56 	int eventType;
57 	bool allowMultipleSelection;
58 	int tooltipLine;
59 	bool linewrap;
60 	bool iconBorder;
61 
62 public:
63 
64 	enum {
65 		EVENT_DRAG = 0,
66 		EVENT_ACTION
67 	};
68 
69 	bool debug;
70 
71 	ScrollingList( int x, int y, int w, int h, Texture highlight, DragAndDropHandler *dragAndDropHandler = NULL, int lineHeight = 15 );
72 	virtual ~ScrollingList();
73 
setTextLinewrap(bool b)74 	inline void setTextLinewrap( bool b ) {
75 		linewrap = b;
76 	}
setIconBorder(bool b)77 	inline void setIconBorder( bool b ) {
78 		iconBorder = b;
79 	}
80 
setAllowMultipleSelection(bool b)81 	inline void setAllowMultipleSelection( bool b ) {
82 		allowMultipleSelection = b;
83 	}
getAllowMultipleSelection()84 	inline bool getAllowMultipleSelection() {
85 		return allowMultipleSelection;
86 	}
87 	//unused: inline int getLineCount() { return list.size(); }
88 	void setLines( int count, std::string const s[], const Color *colors = NULL, Texture* icons = NULL );
89 	void setLines( const std::vector<std::string>::iterator begin, const std::vector<std::string>::iterator end, const Color *colors = NULL, Texture* icons = NULL );
90 	void setLine( const std::string& toPush );
91 	void setLine( size_t pos, const std::string& toPush );
getLine(int index)92 	inline const std::string& getLine( int index ) {
93 		return list[index];
94 	}
95 	void unselectAllLines();
96 
getSelectedLine()97 	inline int getSelectedLine() {
98 		return ( selectedLine != NULL ? selectedLine[ 0 ] : -1 );
99 	}
100 	void setSelectedLine( size_t n );
isSelected(int line)101 	inline bool isSelected( int line ) {
102 		if ( selectedLine == NULL ) {
103 			return false;
104 		} else {
105 			for ( int i = 0; i < selectedLineCount; i++ ) {
106 				if ( selectedLine[i] == line ) return true;
107 			}
108 		}
109 		return false;
110 	}
getSelectedLineCount()111 	inline int getSelectedLineCount() {
112 		return selectedLineCount;
113 	}
getSelectedLine(int index)114 	inline int getSelectedLine( int index ) {
115 		return selectedLine[ index ];
116 	}
117 
118 	void drawWidget( Widget *parent );
119 
getEventType()120 	inline int getEventType() {
121 		return eventType;
122 	}
123 
124 	/**
125 	Return true, if the event activated this widget. (For example, button push, etc.)
126 	Another way to think about it is that if true, the widget fires an "activated" event
127 	to the outside world.
128 	*/
129 	bool handleEvent( Widget *parent, SDL_Event *event, int x, int y );
130 
131 	void removeEffects( Widget *parent );
132 
133 	// don't play sound when the value changes
hasSound()134 	virtual inline bool hasSound() {
135 		return false;
136 	}
137 
canGetFocus()138 	inline bool canGetFocus() {
139 		return canGetFocusVar;
140 	}
setCanGetFocus(bool b)141 	inline void setCanGetFocus( bool b ) {
142 		this->canGetFocusVar = b;
143 	}
144 
145 	int getLineAtPoint( int x, int y );
146 
147 private:
148 	void selectLine( int x, int y, bool addToSelection = false, bool mouseDown = false );
149 	void drawIcon( int x, int y, Texture icon, Widget *parent );
150 	void moveSelectionUp();
151 	void moveSelectionDown();
152 
153 	/**
154 	* a common function that sets up the internal state after altering the list
155 	*/
156 	void setupHeight();
157 
158 	/**
159 	* Prints s
160 	* if linewrap is true it breaks the line so that no line is longer that getWidth()
161 	* else it just prints it
162 	*/
163 	void printLine( Widget *parent, int x, int y, const std::string& s );
164 };
165 
166 #endif
167 
168