imtk

diff src/imtk.c @ 4:00a4ea4ee6dc

attempt at slightly more complex widgets
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 05 Mar 2011 09:25:27 +0200
parents 038e5577d527
children 09b6e8a5dc14
line diff
     1.1 --- a/src/imtk.c	Fri Dec 31 18:10:13 2010 +0200
     1.2 +++ b/src/imtk.c	Sat Mar 05 09:25:27 2011 +0200
     1.3 @@ -2,6 +2,7 @@
     1.4  #include <stdlib.h>
     1.5  #include <string.h>
     1.6  #include <ctype.h>
     1.7 +#include <stdarg.h>
     1.8  #include <assert.h>
     1.9  #ifndef __APPLE__
    1.10  #include <GL/glut.h>
    1.11 @@ -52,7 +53,7 @@
    1.12  
    1.13  static int scr_width = 1, scr_height = 1;
    1.14  static int mousex, mousey, prevx, mouse_bnmask;
    1.15 -static int active = -1, hot = -1, input = -1;
    1.16 +static int active = -1, hot = -1, input = -1, prev_active = -1;
    1.17  
    1.18  static struct key_node *key_list, *key_tail;
    1.19  
    1.20 @@ -305,9 +306,84 @@
    1.21  	draw_progress(id, pos, x, y);
    1.22  }
    1.23  
    1.24 +int imtk_listbox(int id, const char *list, int sel, int x, int y)
    1.25 +{
    1.26 +	int i;
    1.27 +	assert(id >= 0);
    1.28 +
    1.29 +	if(!list) {
    1.30 +		return -1;
    1.31 +	}
    1.32 +
    1.33 +	if(id & 1) {
    1.34 +		id++;
    1.35 +	}
    1.36 +
    1.37 +	for(i=0; *list; i++) {
    1.38 +		if(imtk_button(id + i * 2 + 1, list, x, y + i * 20)) {
    1.39 +			sel = i;
    1.40 +		}
    1.41 +		list += strlen(list) + 1;
    1.42 +	}
    1.43 +	return sel;
    1.44 +}
    1.45 +
    1.46 +int imtk_combobox(int id, char *textbuf, size_t buf_sz, const char *list, int sel, int x, int y)
    1.47 +{
    1.48 +	imtk_textbox(id + 1, textbuf, buf_sz, x, y);
    1.49 +	imtk_button(id + 3, "V", x + TEXTBOX_SIZE, y);
    1.50 +
    1.51 +	if(prev_active == id + 3) {
    1.52 +		sel = imtk_listbox(id + 5, list, sel, x, y + 20);
    1.53 +	}
    1.54 +	return sel;
    1.55 +}
    1.56 +
    1.57 +char *imtk_create_list(const char *first, ...)
    1.58 +{
    1.59 +	int sz;
    1.60 +	char *buf, *item;
    1.61 +	va_list ap;
    1.62 +
    1.63 +	if(!first) {
    1.64 +		return 0;
    1.65 +	}
    1.66 +
    1.67 +	sz = strlen(first) + 2;
    1.68 +	if(!(buf = malloc(sz))) {
    1.69 +		return 0;
    1.70 +	}
    1.71 +	memcpy(buf, first, sz - 2);
    1.72 +	buf[sz - 1] = buf[sz - 2] = 0;
    1.73 +
    1.74 +	va_start(ap, first);
    1.75 +	while((item = va_arg(ap, char*))) {
    1.76 +		int len = strlen(item);
    1.77 +		char *tmp = realloc(buf, sz + len + 1);
    1.78 +		if(!tmp) {
    1.79 +			free(buf);
    1.80 +			return 0;
    1.81 +		}
    1.82 +		buf = tmp;
    1.83 +
    1.84 +		memcpy(buf + sz - 1, item, len);
    1.85 +		sz += len + 1;
    1.86 +		buf[sz - 1] = buf[sz - 2] = 0;
    1.87 +	}
    1.88 +	va_end(ap);
    1.89 +
    1.90 +	return buf;
    1.91 +}
    1.92 +
    1.93 +void imtk_free_list(char *list)
    1.94 +{
    1.95 +	free(list);
    1.96 +}
    1.97 +
    1.98  static void set_active(int id)
    1.99  {
   1.100  	if(id == -1 || hot == id) {
   1.101 +		prev_active = active;
   1.102  		active = id;
   1.103  	}
   1.104  }