imtk

annotate src/checkbox.c @ 24:f416d8def7ef

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