imtk

view 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 source
1 #include <assert.h>
2 #include "imtk.h"
3 #include "state.h"
4 #include "draw.h"
6 static void calc_button_size(const char *label, int *wret, int *hret);
7 static void draw_button(int id, const char *label, int x, int y);
9 int imtk_button(int id, const char *label, int x, int y)
10 {
11 int w, h, res = 0;
12 int over = 0;
14 assert(id >= 0);
16 calc_button_size(label, &w, &h);
18 if(imtk_hit_test(x, y, w, h)) {
19 imtk_set_hot(id);
20 over = 1;
21 }
23 if(imtk_button_state(IMTK_LEFT_BUTTON)) {
24 if(over) {
25 imtk_set_active(id);
26 }
27 } else { /* mouse button up */
28 if(imtk_is_active(id)) {
29 imtk_set_active(-1);
30 if(imtk_is_hot(id) && over) {
31 res = 1;
32 }
33 }
34 }
36 draw_button(id, label, x, y);
37 return res;
38 }
40 static void draw_button(int id, const char *label, int x, int y)
41 {
42 int width, height;
44 calc_button_size(label, &width, &height);
46 glBegin(GL_QUADS);
47 if(imtk_hit_test(x, y, width, height)) {
48 glColor4fv(imtk_get_color(IMTK_FOCUS_COLOR));
49 } else {
50 glColor4fv(imtk_get_color(IMTK_BASE_COLOR));
51 }
52 glVertex2f(x, y);
53 glVertex2f(x + width, y);
54 glVertex2f(x + width, y + height);
55 glVertex2f(x, y + height);
56 glEnd();
58 imtk_draw_frame(x, y, width, height, imtk_is_active(id) ? FRAME_INSET : FRAME_OUTSET);
60 glColor4fv(imtk_get_color(IMTK_TEXT_COLOR));
61 imtk_draw_string(x + 20, y + 15, label);
62 }
64 static void calc_button_size(const char *label, int *wret, int *hret)
65 {
66 int strsz = imtk_string_size(label);
67 if(wret) *wret = strsz + 40;
68 if(hret) *hret = 20;
69 }