imtk

diff src/button.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/button.c	Thu Apr 14 14:22:42 2011 +0300
     1.3 @@ -0,0 +1,69 @@
     1.4 +#include <assert.h>
     1.5 +#include "imtk.h"
     1.6 +#include "state.h"
     1.7 +#include "draw.h"
     1.8 +
     1.9 +static void calc_button_size(const char *label, int *wret, int *hret);
    1.10 +static void draw_button(int id, const char *label, int x, int y);
    1.11 +
    1.12 +int imtk_button(int id, const char *label, int x, int y)
    1.13 +{
    1.14 +	int w, h, res = 0;
    1.15 +	int over = 0;
    1.16 +
    1.17 +	assert(id >= 0);
    1.18 +
    1.19 +	calc_button_size(label, &w, &h);
    1.20 +
    1.21 +	if(imtk_hit_test(x, y, w, h)) {
    1.22 +		imtk_set_hot(id);
    1.23 +		over = 1;
    1.24 +	}
    1.25 +
    1.26 +	if(imtk_button_state(IMTK_LEFT_BUTTON)) {
    1.27 +		if(over) {
    1.28 +			imtk_set_active(id);
    1.29 +		}
    1.30 +	} else { /* mouse button up */
    1.31 +		if(imtk_is_active(id)) {
    1.32 +			imtk_set_active(-1);
    1.33 +			if(imtk_is_hot(id) && over) {
    1.34 +				res = 1;
    1.35 +			}
    1.36 +		}
    1.37 +	}
    1.38 +
    1.39 +	draw_button(id, label, x, y);
    1.40 +	return res;
    1.41 +}
    1.42 +
    1.43 +static void draw_button(int id, const char *label, int x, int y)
    1.44 +{
    1.45 +	int width, height;
    1.46 +
    1.47 +	calc_button_size(label, &width, &height);
    1.48 +
    1.49 +	glBegin(GL_QUADS);
    1.50 +	if(imtk_hit_test(x, y, width, height)) {
    1.51 +		glColor4fv(imtk_get_color(IMTK_FOCUS_COLOR));
    1.52 +	} else {
    1.53 +		glColor4fv(imtk_get_color(IMTK_BASE_COLOR));
    1.54 +	}
    1.55 +	glVertex2f(x, y);
    1.56 +	glVertex2f(x + width, y);
    1.57 +	glVertex2f(x + width, y + height);
    1.58 +	glVertex2f(x, y + height);
    1.59 +	glEnd();
    1.60 +
    1.61 +	imtk_draw_frame(x, y, width, height, imtk_is_active(id) ? FRAME_INSET : FRAME_OUTSET);
    1.62 +
    1.63 +	glColor4fv(imtk_get_color(IMTK_TEXT_COLOR));
    1.64 +	imtk_draw_string(x + 20, y + 15, label);
    1.65 +}
    1.66 +
    1.67 +static void calc_button_size(const char *label, int *wret, int *hret)
    1.68 +{
    1.69 +	int strsz = imtk_string_size(label);
    1.70 +	if(wret) *wret = strsz + 40;
    1.71 +	if(hret) *hret = 20;
    1.72 +}