imtk

view src/textbox.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 <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);
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);
62 }
65 static void draw_textbox(int id, const char *text, int x, int y)
66 {
67 int strsz = imtk_string_size(text);
69 if(imtk_hit_test(x, y, TEXTBOX_SIZE, 20)) {
70 glColor4fv(imtk_get_color(IMTK_FOCUS_COLOR));
71 } else {
72 glColor4fv(imtk_get_color(IMTK_BASE_COLOR));
73 }
75 glBegin(GL_QUADS);
76 glVertex2f(x, y);
77 glVertex2f(x + TEXTBOX_SIZE, y);
78 glVertex2f(x + TEXTBOX_SIZE, y + 20);
79 glVertex2f(x, y + 20);
80 glEnd();
82 glColor4fv(imtk_get_color(IMTK_TEXT_COLOR));
84 if(imtk_has_focus(id)) {
85 glBegin(GL_LINES);
86 glVertex2f(x + strsz + 2, y + 2);
87 glVertex2f(x + strsz + 2, y + 18);
88 glEnd();
89 }
91 imtk_draw_string(x + 2, y + 15, text);
93 imtk_draw_frame(x, y, TEXTBOX_SIZE, 20, FRAME_INSET);
94 }