rayfract

diff src/imtk/checkbox.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/checkbox.c	Mon Jul 31 18:58:56 2023 +0300
     1.3 @@ -0,0 +1,109 @@
     1.4 +#include <string.h>
     1.5 +#include <assert.h>
     1.6 +#include "imtk.h"
     1.7 +#include "state.h"
     1.8 +#include "draw.h"
     1.9 +
    1.10 +
    1.11 +#define CHECKBOX_SIZE	14
    1.12 +
    1.13 +
    1.14 +static void draw_checkbox(int id, const char *label, int x, int y, int state, int over);
    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 full_size, over = 0;
    1.20 +
    1.21 +	assert(id >= 0);
    1.22 +
    1.23 +	if(x == IMTK_AUTO || y == IMTK_AUTO) {
    1.24 +		imtk_layout_get_pos(&x, &y);
    1.25 +	}
    1.26 +
    1.27 +	full_size = sz + imtk_string_size(label) + 5;
    1.28 +	if(imtk_hit_test(x, y, full_size, sz)) {
    1.29 +		imtk_set_hot(id);
    1.30 +		over = 1;
    1.31 +	}
    1.32 +
    1.33 +	if(imtk_button_state(IMTK_LEFT_BUTTON)) {
    1.34 +		if(over) {
    1.35 +			imtk_set_active(id);
    1.36 +		}
    1.37 +	} else { /* mouse button up */
    1.38 +		if(imtk_is_active(id)) {
    1.39 +			imtk_set_active(-1);
    1.40 +			if(imtk_is_hot(id) && over) {
    1.41 +				state = !state;
    1.42 +			}
    1.43 +		}
    1.44 +	}
    1.45 +
    1.46 +	draw_checkbox(id, label, x, y, state, over);
    1.47 +	imtk_layout_advance(full_size, sz);
    1.48 +	return state;
    1.49 +}
    1.50 +
    1.51 +static float v[][2] = {
    1.52 +	{-0.2, 0.63},	/* 0 */
    1.53 +	{0.121, 0.325},	/* 1 */
    1.54 +	{0.15, 0.5},	/* 2 */
    1.55 +	{0.28, 0.125},	/* 3 */
    1.56 +	{0.38, 0.33},	/* 4 */
    1.57 +	{0.42, -0.122},	/* 5 */
    1.58 +	{0.58, 0.25},	/* 6 */
    1.59 +	{0.72, 0.52},	/* 7 */
    1.60 +	{0.625, 0.65},	/* 8 */
    1.61 +	{0.89, 0.78},	/* 9 */
    1.62 +	{0.875, 0.92},	/* 10 */
    1.63 +	{1.13, 1.145}	/* 11 */
    1.64 +};
    1.65 +#define TRI(a, b, c)	(glVertex2fv(v[a]), glVertex2fv(v[b]), glVertex2fv(v[c]))
    1.66 +
    1.67 +static void draw_checkbox(int id, const char *label, int x, int y, int state, int over)
    1.68 +{
    1.69 +	static const int sz = CHECKBOX_SIZE;
    1.70 +	unsigned int attr = 0;
    1.71 +	float tcol[4], bcol[4];
    1.72 +
    1.73 +	if(over) {
    1.74 +		attr |= IMTK_FOCUS_BIT;
    1.75 +	}
    1.76 +	if(imtk_is_active(id)) {
    1.77 +		attr |= IMTK_PRESS_BIT;
    1.78 +	}
    1.79 +	memcpy(tcol, imtk_get_color(IMTK_BOTTOM_COLOR | attr), sizeof tcol);
    1.80 +	memcpy(bcol, imtk_get_color(IMTK_TOP_COLOR | attr), sizeof bcol);
    1.81 +
    1.82 +	imtk_draw_rect(x, y, sz, sz, tcol, bcol);
    1.83 +	imtk_draw_frame(x, y, sz, sz, FRAME_INSET);
    1.84 +
    1.85 +	glColor4fv(imtk_get_color(IMTK_TEXT_COLOR));
    1.86 +	if(state) {
    1.87 +		glMatrixMode(GL_MODELVIEW);
    1.88 +		glPushMatrix();
    1.89 +		glTranslatef(x, y + sz, 0);
    1.90 +		glScalef(sz * 1.2, -sz * 1.3, 1);
    1.91 +
    1.92 +		glBegin(GL_TRIANGLES);
    1.93 +		glColor4fv(imtk_get_color(IMTK_CHECK_COLOR));
    1.94 +		TRI(0, 1, 2);
    1.95 +		TRI(1, 3, 2);
    1.96 +		TRI(3, 4, 2);
    1.97 +		TRI(3, 5, 4);
    1.98 +		TRI(4, 5, 6);
    1.99 +		TRI(4, 6, 7);
   1.100 +		TRI(4, 7, 8);
   1.101 +		TRI(8, 7, 9);
   1.102 +		TRI(8, 9, 10);
   1.103 +		TRI(10, 9, 11);
   1.104 +		glEnd();
   1.105 +
   1.106 +		glPopMatrix();
   1.107 +	}
   1.108 +
   1.109 +	glColor4fv(imtk_get_color(IMTK_TEXT_COLOR));
   1.110 +	imtk_draw_string(x + sz + 5, y + sz - 2, label);
   1.111 +}
   1.112 +