rayfract

view 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 source
1 #include <string.h>
2 #include <assert.h>
3 #include "imtk.h"
4 #include "state.h"
5 #include "draw.h"
8 #define CHECKBOX_SIZE 14
11 static void draw_checkbox(int id, const char *label, int x, int y, int state, int over);
13 int imtk_checkbox(int id, const char *label, int x, int y, int state)
14 {
15 int sz = CHECKBOX_SIZE;
16 int full_size, over = 0;
18 assert(id >= 0);
20 if(x == IMTK_AUTO || y == IMTK_AUTO) {
21 imtk_layout_get_pos(&x, &y);
22 }
24 full_size = sz + imtk_string_size(label) + 5;
25 if(imtk_hit_test(x, y, full_size, sz)) {
26 imtk_set_hot(id);
27 over = 1;
28 }
30 if(imtk_button_state(IMTK_LEFT_BUTTON)) {
31 if(over) {
32 imtk_set_active(id);
33 }
34 } else { /* mouse button up */
35 if(imtk_is_active(id)) {
36 imtk_set_active(-1);
37 if(imtk_is_hot(id) && over) {
38 state = !state;
39 }
40 }
41 }
43 draw_checkbox(id, label, x, y, state, over);
44 imtk_layout_advance(full_size, sz);
45 return state;
46 }
48 static float v[][2] = {
49 {-0.2, 0.63}, /* 0 */
50 {0.121, 0.325}, /* 1 */
51 {0.15, 0.5}, /* 2 */
52 {0.28, 0.125}, /* 3 */
53 {0.38, 0.33}, /* 4 */
54 {0.42, -0.122}, /* 5 */
55 {0.58, 0.25}, /* 6 */
56 {0.72, 0.52}, /* 7 */
57 {0.625, 0.65}, /* 8 */
58 {0.89, 0.78}, /* 9 */
59 {0.875, 0.92}, /* 10 */
60 {1.13, 1.145} /* 11 */
61 };
62 #define TRI(a, b, c) (glVertex2fv(v[a]), glVertex2fv(v[b]), glVertex2fv(v[c]))
64 static void draw_checkbox(int id, const char *label, int x, int y, int state, int over)
65 {
66 static const int sz = CHECKBOX_SIZE;
67 unsigned int attr = 0;
68 float tcol[4], bcol[4];
70 if(over) {
71 attr |= IMTK_FOCUS_BIT;
72 }
73 if(imtk_is_active(id)) {
74 attr |= IMTK_PRESS_BIT;
75 }
76 memcpy(tcol, imtk_get_color(IMTK_BOTTOM_COLOR | attr), sizeof tcol);
77 memcpy(bcol, imtk_get_color(IMTK_TOP_COLOR | attr), sizeof bcol);
79 imtk_draw_rect(x, y, sz, sz, tcol, bcol);
80 imtk_draw_frame(x, y, sz, sz, FRAME_INSET);
82 glColor4fv(imtk_get_color(IMTK_TEXT_COLOR));
83 if(state) {
84 glMatrixMode(GL_MODELVIEW);
85 glPushMatrix();
86 glTranslatef(x, y + sz, 0);
87 glScalef(sz * 1.2, -sz * 1.3, 1);
89 glBegin(GL_TRIANGLES);
90 glColor4fv(imtk_get_color(IMTK_CHECK_COLOR));
91 TRI(0, 1, 2);
92 TRI(1, 3, 2);
93 TRI(3, 4, 2);
94 TRI(3, 5, 4);
95 TRI(4, 5, 6);
96 TRI(4, 6, 7);
97 TRI(4, 7, 8);
98 TRI(8, 7, 9);
99 TRI(8, 9, 10);
100 TRI(10, 9, 11);
101 glEnd();
103 glPopMatrix();
104 }
106 glColor4fv(imtk_get_color(IMTK_TEXT_COLOR));
107 imtk_draw_string(x + sz + 5, y + sz - 2, label);
108 }