forked from oligau/xwax-1.5-osc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
listbox.c
192 lines (151 loc) · 4.06 KB
/
listbox.c
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
/*
* Copyright (C) 2014 Mark Hills <mark@xwax.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2, as published by the Free Software Foundation.
*
* 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 version 2 for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*/
#include <assert.h>
#include "listbox.h"
void listbox_init(struct listbox *s)
{
s->lines = 0;
s->entries = 0;
s->offset = 0;
s->selected = -1;
}
/*
* Set the number of lines displayed on screen
*
* Zero is valid, to mean that the display is hidden. In all other
* cases, the current selection is moved to within range.
*/
void listbox_set_lines(struct listbox *s, unsigned int lines)
{
s->lines = lines;
if (s->selected >= s->offset + s->lines)
s->selected = s->offset + s->lines - 1;
if (s->offset + s->lines > s->entries) {
s->offset = s->entries - s->lines;
if (s->offset < 0)
s->offset = 0;
}
}
/*
* Set the number of entries in the list which backs the scrolling
* display. Bring the current selection within the bounds given.
*/
void listbox_set_entries(struct listbox *s, unsigned int entries)
{
s->entries = entries;
if (s->selected >= s->entries)
s->selected = s->entries - 1;
if (s->offset + s->lines > s->entries) {
s->offset = s->entries - s->lines;
if (s->offset < 0)
s->offset = 0;
}
/* If we went previously had zero entries, reset the selection */
if (s->selected < 0)
s->selected = 0;
}
/*
* Scroll the selection up by n lines. Move the window offset if
* needed
*/
void listbox_up(struct listbox *s, unsigned int n)
{
s->selected -= n;
if (s->selected < 0)
s->selected = 0;
/* Move the viewing offset up, if necessary */
if (s->selected < s->offset) {
s->offset = s->selected + s->lines / 2 - s->lines + 1;
if (s->offset < 0)
s->offset = 0;
}
}
void listbox_down(struct listbox *s, unsigned int n)
{
s->selected += n;
if (s->selected >= s->entries)
s->selected = s->entries - 1;
/* Move the viewing offset down, if necessary */
if (s->selected >= s->offset + s->lines) {
s->offset = s->selected - s->lines / 2;
if (s->offset + s->lines > s->entries)
s->offset = s->entries - s->lines;
}
}
/*
* Scroll to the first entry on the list
*/
void listbox_first(struct listbox *s)
{
s->selected = 0;
s->offset = 0;
}
/*
* Scroll to the final entry on the list
*/
void listbox_last(struct listbox *s)
{
s->selected = s->entries - 1;
s->offset = s->selected - s->lines + 1;
if (s->offset < 0)
s->offset = 0;
}
/*
* Scroll to an entry by index
*/
void listbox_to(struct listbox *s, unsigned int n)
{
int p;
assert(s->selected != -1);
assert(n < s->entries);
/* Retain the on-screen position of the current selection */
p = s->selected - s->offset;
s->selected = n;
s->offset = s->selected - p;
if (s->offset < 0)
s->offset = 0;
}
/*
* Return the index of the current selected list entry, or -1 if
* no current selection
*/
int listbox_current(const struct listbox *s)
{
if (s->entries == 0)
return -1;
else
return s->selected;
}
/*
* Translate the given row on-screen (starting with row zero)
* into a position in the list
*
* Return: -1 if the row is out of range, otherwise entry number
*/
int listbox_map(const struct listbox *s, int row)
{
int e;
assert(row >= 0);
if (row >= s->lines)
return -1;
e = s->offset + row;
if (e >= s->entries)
return -1;
return e;
}