imtk

diff src/checkbox.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/checkbox.c	Thu Apr 14 14:22:42 2011 +0300
     1.3 @@ -0,0 +1,78 @@
     1.4 +#include <assert.h>
     1.5 +#include "imtk.h"
     1.6 +#include "state.h"
     1.7 +#include "draw.h"
     1.8 +
     1.9 +
    1.10 +#define CHECKBOX_SIZE	14
    1.11 +
    1.12 +
    1.13 +static void draw_checkbox(int id, const char *label, int x, int y, int state);
    1.14 +
    1.15 +
    1.16 +int imtk_checkbox(int id, const char *label, int x, int y, int state)
    1.17 +{
    1.18 +	int sz = CHECKBOX_SIZE;
    1.19 +	int over = 0;
    1.20 +
    1.21 +	assert(id >= 0);
    1.22 +
    1.23 +	if(imtk_hit_test(x, y, sz, sz)) {
    1.24 +		imtk_set_hot(id);
    1.25 +		over = 1;
    1.26 +	}
    1.27 +
    1.28 +	if(imtk_button_state(IMTK_LEFT_BUTTON)) {
    1.29 +		if(over) {
    1.30 +			imtk_set_active(id);
    1.31 +		}
    1.32 +	} else { /* mouse button up */
    1.33 +		if(imtk_is_active(id)) {
    1.34 +			imtk_set_active(-1);
    1.35 +			if(imtk_is_hot(id) && over) {
    1.36 +				state = !state;
    1.37 +			}
    1.38 +		}
    1.39 +	}
    1.40 +
    1.41 +	draw_checkbox(id, label, x, y, state);
    1.42 +	return state;
    1.43 +}
    1.44 +
    1.45 +static void draw_checkbox(int id, const char *label, int x, int y, int state)
    1.46 +{
    1.47 +	static const int sz = CHECKBOX_SIZE;
    1.48 +
    1.49 +	if(imtk_hit_test(x, y, sz, sz)) {
    1.50 +		glColor4fv(imtk_get_color(IMTK_FOCUS_COLOR));
    1.51 +	} else {
    1.52 +		glColor4fv(imtk_get_color(IMTK_BASE_COLOR));
    1.53 +	}
    1.54 +
    1.55 +	glBegin(GL_QUADS);
    1.56 +	glVertex2f(x, y);
    1.57 +	glVertex2f(x + sz, y);
    1.58 +	glVertex2f(x + sz, y + sz);
    1.59 +	glVertex2f(x, y + sz);
    1.60 +	glEnd();
    1.61 +
    1.62 +	imtk_draw_frame(x, y, sz, sz, FRAME_INSET);
    1.63 +
    1.64 +	glColor4fv(imtk_get_color(IMTK_TEXT_COLOR));
    1.65 +	if(state) {
    1.66 +		glPushAttrib(GL_LINE_BIT);
    1.67 +		glLineWidth(2);
    1.68 +
    1.69 +		glBegin(GL_LINES);
    1.70 +		glVertex2f(x + 2, y + 2);
    1.71 +		glVertex2f(x + sz - 2, y + sz - 2);
    1.72 +		glVertex2f(x + sz - 2, y + 2);
    1.73 +		glVertex2f(x + 2, y + sz - 2);
    1.74 +		glEnd();
    1.75 +
    1.76 +		glPopAttrib();
    1.77 +	}
    1.78 +
    1.79 +	imtk_draw_string(x + sz + 5, y + sz - 2, label);
    1.80 +}
    1.81 +