imtk

view src/textbox.c @ 14:df2bc9406561

added gradients
author John Tsiombikas <nuclear@siggraph.org>
date Tue, 19 Apr 2011 03:01:46 +0300
parents 9c7987064bb0
children 1cf212375db3
line source
1 #include <string.h>
2 #include <ctype.h>
3 #include <assert.h>
4 #include "imtk.h"
5 #include "state.h"
6 #include "draw.h"
8 #define TEXTBOX_SIZE 100
10 static void draw_textbox(int id, const char *text, int x, int y, int over);
13 void imtk_textbox(int id, char *textbuf, size_t buf_sz, int x, int y)
14 {
15 int len, over = 0;
17 assert(id >= 0);
19 if(imtk_hit_test(x, y, TEXTBOX_SIZE, 20)) {
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 {
29 if(imtk_is_active(id)) {
30 imtk_set_active(-1);
31 if(imtk_is_hot(id) && over) {
32 imtk_set_focus(id);
33 }
34 }
35 }
37 if(imtk_has_focus(id)) {
38 int key;
39 len = strlen(textbuf);
41 while((key = imtk_get_key()) != -1) {
42 if(isprint(key)) {
43 if(len < buf_sz) {
44 textbuf[len++] = (char)key;
45 }
46 } else {
47 switch(key) {
48 case '\b':
49 if(len > 0) {
50 textbuf[--len] = 0;
51 }
52 break;
54 default:
55 break;
56 }
57 }
58 }
59 }
61 draw_textbox(id, textbuf, x, y, over);
62 }
65 static void draw_textbox(int id, const char *text, int x, int y, int over)
66 {
67 int strsz = imtk_string_size(text);
68 float tcol[4], bcol[4];
69 unsigned int attr = 0;
71 if(over) {
72 attr |= IMTK_FOCUS_BIT;
73 }
74 memcpy(tcol, imtk_get_color(IMTK_BOTTOM_COLOR | attr), sizeof tcol);
75 memcpy(bcol, imtk_get_color(IMTK_TOP_COLOR | attr), sizeof bcol);
77 imtk_draw_rect(x, y, TEXTBOX_SIZE, 20, tcol, bcol);
79 if(imtk_has_focus(id)) {
80 glBegin(GL_LINES);
81 glColor4fv(imtk_get_color(IMTK_CURSOR_COLOR));
82 glVertex2f(x + strsz + 3, y + 2);
83 glVertex2f(x + strsz + 3, y + 18);
84 glVertex2f(x + strsz + 4, y + 2);
85 glVertex2f(x + strsz + 4, y + 18);
86 glEnd();
87 }
89 glColor4fv(imtk_get_color(IMTK_TEXT_COLOR));
90 imtk_draw_string(x + 2, y + 15, text);
92 imtk_draw_frame(x, y, TEXTBOX_SIZE, 20, FRAME_INSET);
93 }