}
darr_clear(wl->entries);
wl->dirty = 1;
+
+ tui_call_callback(w, TUI_ONMODIFY);
}
void tui_add_list_item(struct tui_widget *w, const char *text)
str = strdup_nf(text);
darr_push(wl->entries, &str);
wl->dirty = 1;
+
+ tui_call_callback(w, TUI_ONMODIFY);
}
int tui_num_list_items(struct tui_widget *w)
return darr_size(wl->entries);
}
+#define VISLINES(wl) ((wl)->height - 2)
+
int tui_list_select(struct tui_widget *w, int idx)
{
- int offs, nelem;
+ int offs, nelem, numvis;
struct tui_list *wl = (struct tui_list*)w;
assert(wl->type == TUI_LIST);
return 0; /* no change */
}
+ numvis = VISLINES(wl);
+
if(idx < 0) {
wl->sel = -1;
return 0;
}
wl->sel = idx;
- if(idx < wl->view_offs || idx >= wl->view_offs + wl->height) {
- offs = idx - wl->height / 2;
- if(offs + wl->height >= nelem) {
- offs = nelem - wl->height;
+ if(idx < wl->view_offs || idx >= wl->view_offs + numvis) {
+ offs = idx - numvis / 2;
+ if(offs + numvis >= nelem) {
+ offs = nelem - numvis;
}
if(offs < 0) {
offs = 0;
}
wl->dirty = 1;
+ tui_call_callback(w, TUI_ONMODIFY);
return 0;
}
int tui_list_sel_next(struct tui_widget *w)
{
- int nelem;
+ int nelem, numvis;
struct tui_list *wl = (struct tui_list*)w;
assert(wl->type == TUI_LIST);
nelem = darr_size(wl->entries);
+ numvis = VISLINES(wl);
+
if(wl->sel + 1 >= nelem) {
return -1;
}
- if(++wl->sel - wl->view_offs >= wl->height) {
- wl->view_offs = wl->sel - wl->height;
+ if(++wl->sel - wl->view_offs >= numvis) {
+ wl->view_offs = wl->sel - numvis + 1;
}
wl->dirty = 1;
+ tui_call_callback(w, TUI_ONMODIFY);
return 0;
}
wl->view_offs = wl->sel;
}
wl->dirty = 1;
+ tui_call_callback(w, TUI_ONMODIFY);
return 0;
}
wl->sel = 0;
wl->view_offs = 0;
wl->dirty = 1;
+ tui_call_callback(w, TUI_ONMODIFY);
return 0;
}
int tui_list_sel_end(struct tui_widget *w)
{
- int nelem;
+ int nelem, numvis;
struct tui_list *wl = (struct tui_list*)w;
assert(wl->type == TUI_LIST);
nelem = darr_size(wl->entries);
+ numvis = VISLINES(wl);
wl->sel = nelem - 1;
- wl->view_offs = nelem - wl->height;
+ wl->view_offs = nelem - numvis;
if(wl->view_offs < 0) wl->view_offs = 0;
wl->dirty = 1;
+ tui_call_callback(w, TUI_ONMODIFY);
return 0;
}
void tui_sort_list(struct tui_widget *w, int (*cmpfunc)(const void*, const void*))
{
+ int nelem;
struct tui_list *wl = (struct tui_list*)w;
assert(wl->type == TUI_LIST);
if(!cmpfunc) {
cmpfunc = (int (*)(const void*, const void*))strcmp;
}
- qsort(wl->entries, darr_size(wl->entries), sizeof *wl->entries, cmpfunc);
+
+ nelem = darr_size(wl->entries);
+ qsort(wl->entries, nelem, sizeof *wl->entries, cmpfunc);
+
+ wl->dirty = 1;
+ tui_call_callback(w, TUI_ONMODIFY);
}
static void draw_list(struct tui_widget *w, void *cls)
x++;
for(i=0; i<num; i++) {
idx = i + wl->view_offs;
- if(idx == wl->sel) {
+ if(w->focus && idx == wl->sel) {
tg_bgcolor(TGFX_CYAN);
tg_fgcolor(TGFX_BLUE);