imtk
view src/checkbox.c @ 12:17c9525b2e35
merged -lGLU fix with listbox patch
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 17 Apr 2011 18:20:23 +0300 |
parents | 10604ff95527 |
children | 9c7987064bb0 |
line source
1 #include <assert.h>
2 #include "imtk.h"
3 #include "state.h"
4 #include "draw.h"
7 #define CHECKBOX_SIZE 14
10 static void draw_checkbox(int id, const char *label, int x, int y, int state, int over);
12 int imtk_checkbox(int id, const char *label, int x, int y, int state)
13 {
14 int sz = CHECKBOX_SIZE;
15 int over = 0;
17 assert(id >= 0);
19 if(imtk_hit_test(x, y, sz + imtk_string_size(label) + 5, sz)) {
20 imtk_set_hot(id);
21 over = 1;
22 }
24 if(imtk_button_state(IMTK_LEFT_BUTTON)) {
25 if(over) {
26 imtk_set_active(id);
27 }
28 } else { /* mouse button up */
29 if(imtk_is_active(id)) {
30 imtk_set_active(-1);
31 if(imtk_is_hot(id) && over) {
32 state = !state;
33 }
34 }
35 }
37 draw_checkbox(id, label, x, y, state, over);
38 return state;
39 }
41 static void draw_checkbox(int id, const char *label, int x, int y, int state, int over)
42 {
43 static const int sz = CHECKBOX_SIZE;
45 if(over) {
46 glColor4fv(imtk_get_color(IMTK_FOCUS_COLOR));
47 } else {
48 glColor4fv(imtk_get_color(IMTK_BASE_COLOR));
49 }
51 imtk_draw_rect(x, y, sz, sz, 0);
52 imtk_draw_frame(x, y, sz, sz, FRAME_INSET);
54 glColor4fv(imtk_get_color(IMTK_TEXT_COLOR));
55 if(state) {
56 glPushAttrib(GL_LINE_BIT);
57 glLineWidth(2);
59 glBegin(GL_LINES);
60 glVertex2f(x + 2, y + 2);
61 glVertex2f(x + sz - 2, y + sz - 2);
62 glVertex2f(x + sz - 2, y + 2);
63 glVertex2f(x + 2, y + sz - 2);
64 glEnd();
66 glPopAttrib();
67 }
69 imtk_draw_string(x + sz + 5, y + sz - 2, label);
70 }