rev |
line source |
nuclear@6
|
1 #include <string.h>
|
nuclear@6
|
2 #include <ctype.h>
|
nuclear@6
|
3 #include <assert.h>
|
nuclear@6
|
4 #include "imtk.h"
|
nuclear@6
|
5 #include "state.h"
|
nuclear@6
|
6 #include "draw.h"
|
nuclear@6
|
7
|
nuclear@6
|
8 #define TEXTBOX_SIZE 100
|
nuclear@6
|
9
|
nuclear@6
|
10 static void draw_textbox(int id, const char *text, int x, int y);
|
nuclear@6
|
11
|
nuclear@6
|
12
|
nuclear@6
|
13 void imtk_textbox(int id, char *textbuf, size_t buf_sz, int x, int y)
|
nuclear@6
|
14 {
|
nuclear@6
|
15 int len, over = 0;
|
nuclear@6
|
16
|
nuclear@6
|
17 assert(id >= 0);
|
nuclear@6
|
18
|
nuclear@6
|
19 if(imtk_hit_test(x, y, TEXTBOX_SIZE, 20)) {
|
nuclear@6
|
20 imtk_set_hot(id);
|
nuclear@6
|
21 over = 1;
|
nuclear@6
|
22 }
|
nuclear@6
|
23
|
nuclear@6
|
24 if(imtk_button_state(IMTK_LEFT_BUTTON)) {
|
nuclear@6
|
25 if(over) {
|
nuclear@6
|
26 imtk_set_active(id);
|
nuclear@6
|
27 }
|
nuclear@6
|
28 } else {
|
nuclear@6
|
29 if(imtk_is_active(id)) {
|
nuclear@6
|
30 imtk_set_active(-1);
|
nuclear@6
|
31 if(imtk_is_hot(id) && over) {
|
nuclear@6
|
32 imtk_set_focus(id);
|
nuclear@6
|
33 }
|
nuclear@6
|
34 }
|
nuclear@6
|
35 }
|
nuclear@6
|
36
|
nuclear@6
|
37 if(imtk_has_focus(id)) {
|
nuclear@6
|
38 int key;
|
nuclear@6
|
39 len = strlen(textbuf);
|
nuclear@6
|
40
|
nuclear@6
|
41 while((key = imtk_get_key()) != -1) {
|
nuclear@6
|
42 if(isprint(key)) {
|
nuclear@6
|
43 if(len < buf_sz) {
|
nuclear@6
|
44 textbuf[len++] = (char)key;
|
nuclear@6
|
45 }
|
nuclear@6
|
46 } else {
|
nuclear@6
|
47 switch(key) {
|
nuclear@6
|
48 case '\b':
|
nuclear@6
|
49 if(len > 0) {
|
nuclear@6
|
50 textbuf[--len] = 0;
|
nuclear@6
|
51 }
|
nuclear@6
|
52 break;
|
nuclear@6
|
53
|
nuclear@6
|
54 default:
|
nuclear@6
|
55 break;
|
nuclear@6
|
56 }
|
nuclear@6
|
57 }
|
nuclear@6
|
58 }
|
nuclear@6
|
59 }
|
nuclear@6
|
60
|
nuclear@6
|
61 draw_textbox(id, textbuf, x, y);
|
nuclear@6
|
62 }
|
nuclear@6
|
63
|
nuclear@6
|
64
|
nuclear@6
|
65 static void draw_textbox(int id, const char *text, int x, int y)
|
nuclear@6
|
66 {
|
nuclear@6
|
67 int strsz = imtk_string_size(text);
|
nuclear@6
|
68
|
nuclear@6
|
69 if(imtk_hit_test(x, y, TEXTBOX_SIZE, 20)) {
|
nuclear@6
|
70 glColor4fv(imtk_get_color(IMTK_FOCUS_COLOR));
|
nuclear@6
|
71 } else {
|
nuclear@6
|
72 glColor4fv(imtk_get_color(IMTK_BASE_COLOR));
|
nuclear@6
|
73 }
|
nuclear@6
|
74
|
nuclear@8
|
75 imtk_draw_rect(x, y, TEXTBOX_SIZE, 20, 0);
|
nuclear@6
|
76
|
nuclear@6
|
77 if(imtk_has_focus(id)) {
|
nuclear@6
|
78 glBegin(GL_LINES);
|
nuclear@8
|
79 glColor4f(0.8, 0.25, 0.18, imtk_get_color(IMTK_TEXT_COLOR)[3]);
|
nuclear@8
|
80 glVertex2f(x + strsz + 3, y + 2);
|
nuclear@8
|
81 glVertex2f(x + strsz + 3, y + 18);
|
nuclear@8
|
82 glVertex2f(x + strsz + 4, y + 2);
|
nuclear@8
|
83 glVertex2f(x + strsz + 4, y + 18);
|
nuclear@6
|
84 glEnd();
|
nuclear@6
|
85 }
|
nuclear@6
|
86
|
nuclear@8
|
87 glColor4fv(imtk_get_color(IMTK_TEXT_COLOR));
|
nuclear@6
|
88 imtk_draw_string(x + 2, y + 15, text);
|
nuclear@6
|
89
|
nuclear@6
|
90 imtk_draw_frame(x, y, TEXTBOX_SIZE, 20, FRAME_INSET);
|
nuclear@6
|
91 }
|
nuclear@6
|
92
|