X-Git-Url: http://git.mutantstargoat.com/user/nuclear/?p=demo_prior;a=blobdiff_plain;f=src%2Fimtk%2Flistbox.c;fp=src%2Fimtk%2Flistbox.c;h=74dd7aa4d9ac47bdbac97f4f4ab41e757a42ba1a;hp=0000000000000000000000000000000000000000;hb=2f14a35e7d557da12f24056267b911f24774aa18;hpb=5eefe7b94c8d6c6caa2c10e3835ab0831a3c42a1 diff --git a/src/imtk/listbox.c b/src/imtk/listbox.c new file mode 100644 index 0000000..74dd7aa --- /dev/null +++ b/src/imtk/listbox.c @@ -0,0 +1,181 @@ +#include +#include +#include +#include "imtk.h" +#include "state.h" +#include "draw.h" + +#define ITEM_HEIGHT 18 +#define PAD 3 + +static int list_radio(int id, const char *list, int sel, int x, int y, void (*draw)()); +static void draw_listbox(int id, const char *list, int sel, int x, int y, int width, int nitems, int over); +static void draw_radio(int id, const char *list, int sel, int x, int y, int width, int nitems, int over); + +int imtk_listbox(int id, const char *list, int sel, int x, int y) +{ + return list_radio(id, list, sel, x, y, draw_listbox); +} + +int imtk_radiogroup(int id, const char *list, int sel, int x, int y) +{ + return list_radio(id, list, sel, x, y, draw_radio); +} + +static int list_radio(int id, const char *list, int sel, int x, int y, void (*draw)()) +{ + int i, max_width, nitems, over; + const char *ptr; + + assert(id >= 0); + + if(x == IMTK_AUTO || y == IMTK_AUTO) { + imtk_layout_get_pos(&x, &y); + } + + max_width = 0; + over = 0; + + ptr = list; + for(i=0; *ptr; i++) { + int strsz = imtk_string_size(ptr) + 2 * PAD; + if(strsz > max_width) { + max_width = strsz; + } + ptr += strlen(ptr) + 1; + + if(imtk_hit_test(x, y + i * ITEM_HEIGHT, max_width, ITEM_HEIGHT)) { + imtk_set_hot(id); + over = i + 1; + } + } + nitems = i; + + if(imtk_button_state(IMTK_LEFT_BUTTON)) { + if(over) { + imtk_set_active(id); + } + } else { + if(imtk_is_active(id)) { + imtk_set_active(-1); + if(imtk_is_hot(id) && over) { + sel = over - 1; + } + } + } + + draw(id, list, sel, x, y, max_width, nitems, over); + imtk_layout_advance(max_width, ITEM_HEIGHT * nitems); + return sel; +} + +char *imtk_create_list(const char *first, ...) +{ + int sz; + char *buf, *item; + va_list ap; + + if(!first) { + return 0; + } + + sz = strlen(first) + 2; + if(!(buf = malloc(sz))) { + return 0; + } + memcpy(buf, first, sz - 2); + buf[sz - 1] = buf[sz - 2] = 0; + + va_start(ap, first); + while((item = va_arg(ap, char*))) { + int len = strlen(item); + char *tmp = realloc(buf, sz + len + 1); + if(!tmp) { + free(buf); + return 0; + } + buf = tmp; + + memcpy(buf + sz - 1, item, len); + sz += len + 1; + buf[sz - 1] = buf[sz - 2] = 0; + } + va_end(ap); + + return buf; +} + +void imtk_free_list(char *list) +{ + free(list); +} + +static void draw_listbox(int id, const char *list, int sel, int x, int y, int width, int nitems, int over) +{ + int i; + const char *item = list; + + glColor4fv(imtk_get_color(IMTK_TEXT_COLOR)); + + for(i=0; i