imtk

diff src/textbox.c @ 6:38609a9f7586

reorganizing ...
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 14 Apr 2011 14:22:42 +0300
parents
children 10604ff95527
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/textbox.c	Thu Apr 14 14:22:42 2011 +0300
     1.3 @@ -0,0 +1,95 @@
     1.4 +#include <string.h>
     1.5 +#include <ctype.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 TEXTBOX_SIZE	100
    1.12 +
    1.13 +static void draw_textbox(int id, const char *text, int x, int y);
    1.14 +
    1.15 +
    1.16 +void imtk_textbox(int id, char *textbuf, size_t buf_sz, int x, int y)
    1.17 +{
    1.18 +	int len, over = 0;
    1.19 +
    1.20 +	assert(id >= 0);
    1.21 +
    1.22 +	if(imtk_hit_test(x, y, TEXTBOX_SIZE, 20)) {
    1.23 +		imtk_set_hot(id);
    1.24 +		over = 1;
    1.25 +	}
    1.26 +
    1.27 +	if(imtk_button_state(IMTK_LEFT_BUTTON)) {
    1.28 +		if(over) {
    1.29 +			imtk_set_active(id);
    1.30 +		}
    1.31 +	} else {
    1.32 +		if(imtk_is_active(id)) {
    1.33 +			imtk_set_active(-1);
    1.34 +			if(imtk_is_hot(id) && over) {
    1.35 +				imtk_set_focus(id);
    1.36 +			}
    1.37 +		}
    1.38 +	}
    1.39 +
    1.40 +	if(imtk_has_focus(id)) {
    1.41 +		int key;
    1.42 +		len = strlen(textbuf);
    1.43 +
    1.44 +		while((key = imtk_get_key()) != -1) {
    1.45 +			if(isprint(key)) {
    1.46 +				if(len < buf_sz) {
    1.47 +					textbuf[len++] = (char)key;
    1.48 +				}
    1.49 +			} else {
    1.50 +				switch(key) {
    1.51 +				case '\b':
    1.52 +					if(len > 0) {
    1.53 +						textbuf[--len] = 0;
    1.54 +					}
    1.55 +					break;
    1.56 +
    1.57 +				default:
    1.58 +					break;
    1.59 +				}
    1.60 +			}
    1.61 +		}
    1.62 +	}
    1.63 +
    1.64 +	draw_textbox(id, textbuf, x, y);
    1.65 +}
    1.66 +
    1.67 +
    1.68 +static void draw_textbox(int id, const char *text, int x, int y)
    1.69 +{
    1.70 +	int strsz = imtk_string_size(text);
    1.71 +
    1.72 +	if(imtk_hit_test(x, y, TEXTBOX_SIZE, 20)) {
    1.73 +		glColor4fv(imtk_get_color(IMTK_FOCUS_COLOR));
    1.74 +	} else {
    1.75 +		glColor4fv(imtk_get_color(IMTK_BASE_COLOR));
    1.76 +	}
    1.77 +
    1.78 +	glBegin(GL_QUADS);
    1.79 +	glVertex2f(x, y);
    1.80 +	glVertex2f(x + TEXTBOX_SIZE, y);
    1.81 +	glVertex2f(x + TEXTBOX_SIZE, y + 20);
    1.82 +	glVertex2f(x, y + 20);
    1.83 +	glEnd();
    1.84 +
    1.85 +	glColor4fv(imtk_get_color(IMTK_TEXT_COLOR));
    1.86 +
    1.87 +	if(imtk_has_focus(id)) {
    1.88 +		glBegin(GL_LINES);
    1.89 +		glVertex2f(x + strsz + 2, y + 2);
    1.90 +		glVertex2f(x + strsz + 2, y + 18);
    1.91 +		glEnd();
    1.92 +	}
    1.93 +
    1.94 +	imtk_draw_string(x + 2, y + 15, text);
    1.95 +
    1.96 +	imtk_draw_frame(x, y, TEXTBOX_SIZE, 20, FRAME_INSET);
    1.97 +}
    1.98 +