rayfract
diff src/imtk/listbox.c @ 10:1496aae2e7d4
- simplified build by including dependences in the source tree
- added make dep tracking
- added mingw cross-build rules
- added readme & licence
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Mon, 31 Jul 2023 18:58:56 +0300 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/imtk/listbox.c Mon Jul 31 18:58:56 2023 +0300 1.3 @@ -0,0 +1,181 @@ 1.4 +#include <string.h> 1.5 +#include <stdarg.h> 1.6 +#include <assert.h> 1.7 +#include "imtk.h" 1.8 +#include "state.h" 1.9 +#include "draw.h" 1.10 + 1.11 +#define ITEM_HEIGHT 18 1.12 +#define PAD 3 1.13 + 1.14 +static int list_radio(int id, const char *list, int sel, int x, int y, void (*draw)()); 1.15 +static void draw_listbox(int id, const char *list, int sel, int x, int y, int width, int nitems, int over); 1.16 +static void draw_radio(int id, const char *list, int sel, int x, int y, int width, int nitems, int over); 1.17 + 1.18 +int imtk_listbox(int id, const char *list, int sel, int x, int y) 1.19 +{ 1.20 + return list_radio(id, list, sel, x, y, draw_listbox); 1.21 +} 1.22 + 1.23 +int imtk_radiogroup(int id, const char *list, int sel, int x, int y) 1.24 +{ 1.25 + return list_radio(id, list, sel, x, y, draw_radio); 1.26 +} 1.27 + 1.28 +static int list_radio(int id, const char *list, int sel, int x, int y, void (*draw)()) 1.29 +{ 1.30 + int i, max_width, nitems, over; 1.31 + const char *ptr; 1.32 + 1.33 + assert(id >= 0); 1.34 + 1.35 + if(x == IMTK_AUTO || y == IMTK_AUTO) { 1.36 + imtk_layout_get_pos(&x, &y); 1.37 + } 1.38 + 1.39 + max_width = 0; 1.40 + over = 0; 1.41 + 1.42 + ptr = list; 1.43 + for(i=0; *ptr; i++) { 1.44 + int strsz = imtk_string_size(ptr) + 2 * PAD; 1.45 + if(strsz > max_width) { 1.46 + max_width = strsz; 1.47 + } 1.48 + ptr += strlen(ptr) + 1; 1.49 + 1.50 + if(imtk_hit_test(x, y + i * ITEM_HEIGHT, max_width, ITEM_HEIGHT)) { 1.51 + imtk_set_hot(id); 1.52 + over = i + 1; 1.53 + } 1.54 + } 1.55 + nitems = i; 1.56 + 1.57 + if(imtk_button_state(IMTK_LEFT_BUTTON)) { 1.58 + if(over) { 1.59 + imtk_set_active(id); 1.60 + } 1.61 + } else { 1.62 + if(imtk_is_active(id)) { 1.63 + imtk_set_active(-1); 1.64 + if(imtk_is_hot(id) && over) { 1.65 + sel = over - 1; 1.66 + } 1.67 + } 1.68 + } 1.69 + 1.70 + draw(id, list, sel, x, y, max_width, nitems, over); 1.71 + imtk_layout_advance(max_width, ITEM_HEIGHT * nitems); 1.72 + return sel; 1.73 +} 1.74 + 1.75 +char *imtk_create_list(const char *first, ...) 1.76 +{ 1.77 + int sz; 1.78 + char *buf, *item; 1.79 + va_list ap; 1.80 + 1.81 + if(!first) { 1.82 + return 0; 1.83 + } 1.84 + 1.85 + sz = strlen(first) + 2; 1.86 + if(!(buf = malloc(sz))) { 1.87 + return 0; 1.88 + } 1.89 + memcpy(buf, first, sz - 2); 1.90 + buf[sz - 1] = buf[sz - 2] = 0; 1.91 + 1.92 + va_start(ap, first); 1.93 + while((item = va_arg(ap, char*))) { 1.94 + int len = strlen(item); 1.95 + char *tmp = realloc(buf, sz + len + 1); 1.96 + if(!tmp) { 1.97 + free(buf); 1.98 + return 0; 1.99 + } 1.100 + buf = tmp; 1.101 + 1.102 + memcpy(buf + sz - 1, item, len); 1.103 + sz += len + 1; 1.104 + buf[sz - 1] = buf[sz - 2] = 0; 1.105 + } 1.106 + va_end(ap); 1.107 + 1.108 + return buf; 1.109 +} 1.110 + 1.111 +void imtk_free_list(char *list) 1.112 +{ 1.113 + free(list); 1.114 +} 1.115 + 1.116 +static void draw_listbox(int id, const char *list, int sel, int x, int y, int width, int nitems, int over) 1.117 +{ 1.118 + int i; 1.119 + const char *item = list; 1.120 + 1.121 + glColor4fv(imtk_get_color(IMTK_TEXT_COLOR)); 1.122 + 1.123 + for(i=0; i<nitems; i++) { 1.124 + int item_y = i * ITEM_HEIGHT + y; 1.125 + unsigned int attr = 0; 1.126 + float tcol[4], bcol[4]; 1.127 + 1.128 + if(over - 1 == i) { 1.129 + attr |= IMTK_FOCUS_BIT; 1.130 + } 1.131 + 1.132 + if(sel == i) { 1.133 + attr |= IMTK_SEL_BIT; 1.134 + memcpy(tcol, imtk_get_color(IMTK_TOP_COLOR | attr), sizeof tcol); 1.135 + memcpy(bcol, imtk_get_color(IMTK_BOTTOM_COLOR | attr), sizeof bcol); 1.136 + } else { 1.137 + memcpy(tcol, imtk_get_color(IMTK_BOTTOM_COLOR | attr), sizeof tcol); 1.138 + memcpy(bcol, imtk_get_color(IMTK_BOTTOM_COLOR | attr), sizeof bcol); 1.139 + } 1.140 + 1.141 + imtk_draw_rect(x, item_y, width, ITEM_HEIGHT, tcol, bcol); 1.142 + 1.143 + glColor4fv(imtk_get_color(IMTK_TEXT_COLOR)); 1.144 + imtk_draw_string(x + 3, item_y + ITEM_HEIGHT - 5, item); 1.145 + item += strlen(item) + 1; 1.146 + } 1.147 + 1.148 + imtk_draw_frame(x, y, width, ITEM_HEIGHT * nitems, FRAME_INSET); 1.149 +} 1.150 + 1.151 +static void draw_radio(int id, const char *list, int sel, int x, int y, int width, int nitems, int over) 1.152 +{ 1.153 + int i; 1.154 + const char *item = list; 1.155 + float rad = ITEM_HEIGHT * 0.5; 1.156 + 1.157 + for(i=0; i<nitems; i++) { 1.158 + int item_y = i * ITEM_HEIGHT + y; 1.159 + unsigned int attr = 0; 1.160 + float tcol[4], bcol[4]; 1.161 + 1.162 + if(over - 1 == i) { 1.163 + attr |= IMTK_FOCUS_BIT; 1.164 + } 1.165 + 1.166 + imtk_draw_disc_frame(x + rad, item_y + rad, rad * 0.9, rad * 0.75, 5, FRAME_INSET); 1.167 + 1.168 + memcpy(tcol, imtk_get_color(IMTK_BOTTOM_COLOR | attr), sizeof tcol); 1.169 + memcpy(bcol, imtk_get_color(IMTK_TOP_COLOR | attr), sizeof bcol); 1.170 + imtk_draw_disc(x + rad, item_y + rad, rad * 0.75, 5, tcol, bcol); 1.171 + 1.172 + if(i == sel) { 1.173 + attr |= IMTK_SEL_BIT; 1.174 + memcpy(tcol, imtk_get_color(IMTK_TOP_COLOR | attr), sizeof tcol); 1.175 + memcpy(bcol, imtk_get_color(IMTK_BOTTOM_COLOR | attr), sizeof bcol); 1.176 + 1.177 + imtk_draw_disc(x + rad, item_y + ITEM_HEIGHT / 2, rad * 0.6, 5, tcol, bcol); 1.178 + } 1.179 + 1.180 + glColor4fv(imtk_get_color(IMTK_TEXT_COLOR)); 1.181 + imtk_draw_string(x + rad * 2.0 + 3, item_y + ITEM_HEIGHT - 5, item); 1.182 + item += strlen(item) + 1; 1.183 + } 1.184 +}