aboutsummaryrefslogtreecommitdiff
path: root/lib/gui/elistbox.cpp
blob: a6fe45f1f2612533ed2fb1b296b800ee8976ffbc (plain)
1
2
3
4
5
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
#include <lib/gui/elistbox.h>
#include <lib/gui/elistboxcontent.h>
#include <lib/actions/action.h>

eListbox::eListbox(eWidget *parent): eWidget(parent)
{
	setContent(new eListboxStringContent());

	ePtr<eActionMap> ptr;
	eActionMap::getInstance(ptr);
	
	ptr->bindAction("ListboxActions", 0, 0, this);
}

eListbox::~eListbox()
{
	ePtr<eActionMap> ptr;
	eActionMap::getInstance(ptr);
	ptr->unbindAction(this, 0);
}

void eListbox::setContent(iListboxContent *content)
{
	m_content = content;
	if (content)
		m_content->setListbox(this);
	entryReset();
}

void eListbox::moveSelection(int dir)
{
		/* refuse to do anything without a valid list. */
	if (!m_content)
		return;
		
		/* we need the old top/sel to see what we have to redraw */
	int oldtop = m_top;
	int oldsel = m_selected;
	
		/* first, move cursor */
	switch (dir)
	{
	case moveUp:
		m_content->cursorMove(-1);
		break;
	case moveDown:
		m_content->cursorMove(1);
			/* ok - we could have reached the end. we just go one back then. */
		if (!m_content->cursorValid())
			m_content->cursorMove(-1);
		break;
	case moveTop:
		m_content->cursorHome();
		m_top = 0; /* align with top, speeds up process */
		break;
	case moveEnd:
			/* move to last existing one ("end" is already invalid) */
		m_content->cursorEnd(); m_content->cursorMove(-1); 
		
		m_top = m_content->cursorGet() - m_items_per_page + 1;
		if (m_top < 0)
			m_top = 0;
		break;
	case justCheck:
		break;
	}
	
		/* note that we could be on an invalid cursor position, but we don't
		   care. this only happens on empty lists, and should have almost no
		   side effects. */
	
		/* now, look wether the current selection is out of screen */
	m_selected = m_content->cursorGet();
	
	if (m_selected < m_top)
	{
		m_top -= m_items_per_page;
		if (m_top < 0)
			m_top = 0;
	} else if (m_selected >= m_top + m_items_per_page)
	{
			/* m_top should be always valid here as it's selected */
		m_top += m_items_per_page;
	}

	if (m_top != oldtop)
		invalidate();
	else if (m_selected != oldsel)
	{
		
			/* redraw the old and newly selected */
		gRegion inv = eRect(0, m_itemheight * (m_selected-m_top), size().width(), m_itemheight);
		inv |= eRect(0, m_itemheight * (oldsel-m_top), size().width(), m_itemheight);
		
		invalidate(inv);
	}
}

int eListbox::event(int event, void *data, void *data2)
{
	switch (event)
	{
	case evtPaint:
	{
		ePtr<eWindowStyle> style;
		
		if (!m_content)
			return eWidget::event(event, data, data2);
		assert(m_content);
		recalcSize(); // move to event
		
		getStyle(style);
		
		if (!m_content)
			return 0;
		
		gPainter &painter = *(gPainter*)data2;
		
		m_content->cursorSave();
		m_content->cursorMove(m_top - m_selected);
		
		for (int y = 0, i = 0; i < m_items_per_page; y += m_itemheight, ++i)
		{
			m_content->paint(painter, *style, ePoint(0, y), m_selected == m_content->cursorGet());
			m_content->cursorMove(+1);
		}
		
		m_content->cursorRestore();
		
		return 0;
	}
	case evtAction:
		moveSelection((int)data2);
		return 1;
	default:
		return eWidget::event(event, data, data2);
	}
}

void eListbox::recalcSize()
{
	m_itemheight = 20;
	m_content->setSize(eSize(size().width(), m_itemheight));
	m_items_per_page = size().height() / m_itemheight;
}

void eListbox::entryAdded(int index)
{
		/* manage our local pointers. when the entry was added before the current position, we have to advance. */
		
		/* we need to check <= - when the new entry has the (old) index of the cursor, the cursor was just moved down. */
	if (index <= m_selected)
		++m_selected;
	if (index <= m_top)
		++m_top;
		
		/* we have to check wether our current cursor is gone out of the screen. */
		/* moveSelection will check for this case */
	moveSelection(justCheck);
	
		/* now, check if the new index is visible. */
	if ((m_top <= index) && (index < (m_top + m_items_per_page)))
	{
			/* todo, calc exact invalidation... */
		invalidate();
	}
}

void eListbox::entryRemoved(int index)
{
	if (index == m_selected)
		m_selected = m_content->cursorGet();

	moveSelection(justCheck);
	
	if ((m_top <= index) && (index < (m_top + m_items_per_page)))
	{
			/* todo, calc exact invalidation... */
		invalidate();
	}
}

void eListbox::entryChanged(int index)
{
	if ((m_top <= index) && (index < (m_top + m_items_per_page)))
	{
		gRegion inv = eRect(0, m_itemheight * (index-m_top), size().width(), m_itemheight);
		invalidate(inv);
	}
}

void eListbox::entryReset()
{
	if (m_content)
		m_content->cursorHome();
	m_top = 0;
	m_selected = 0;
	invalidate();
}