imtk

view src/button.c @ 8:10604ff95527

imtk_draw_rect
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 14 Apr 2011 23:21:56 +0300
parents 38609a9f7586
children df2bc9406561
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 if(imtk_hit_test(x, y, width, height)) {
47 glColor4fv(imtk_get_color(IMTK_FOCUS_COLOR));
48 } else {
49 glColor4fv(imtk_get_color(IMTK_BASE_COLOR));
50 }
52 imtk_draw_rect(x, y, width, height, 0);
53 imtk_draw_frame(x, y, width, height, imtk_is_active(id) ? FRAME_INSET : FRAME_OUTSET);
55 glColor4fv(imtk_get_color(IMTK_TEXT_COLOR));
56 imtk_draw_string(x + 20, y + 15, label);
57 }
59 static void calc_button_size(const char *label, int *wret, int *hret)
60 {
61 int strsz = imtk_string_size(label);
62 if(wret) *wret = strsz + 40;
63 if(hret) *hret = 20;
64 }