8 #define TEXTBOX_SIZE 100
9 #define TEXTBOX_HEIGHT 20
11 static void draw_textbox(int id, char *text, int cursor, int x, int y, int over);
14 void imtk_textbox(int id, char *textbuf, size_t buf_sz, int x, int y)
16 int key, len, cursor = 0, over = 0;
20 if(x == IMTK_AUTO || y == IMTK_AUTO) {
21 imtk_layout_get_pos(&x, &y);
24 len = strlen(textbuf);
26 /* HACK! using last element of textbuf for saving cursor position */
27 if((cursor = textbuf[buf_sz - 1]) > len || cursor < 0) {
31 if(imtk_hit_test(x, y, TEXTBOX_SIZE, TEXTBOX_HEIGHT)) {
36 if(imtk_button_state(IMTK_LEFT_BUTTON)) {
41 if(imtk_is_active(id)) {
43 if(imtk_is_hot(id) && over) {
49 if(imtk_has_focus(id)) {
50 while((key = imtk_get_key()) != -1) {
51 if(!(key & 0xff00) && isprint(key)) {
52 if(len < buf_sz - 1) {
54 textbuf[len++] = (char)key;
57 memmove(textbuf + cursor + 1, textbuf + cursor, len - cursor);
59 textbuf[cursor++] = (char)key;
69 textbuf[--cursor] = 0;
71 memmove(textbuf + cursor - 1, textbuf + cursor, len - cursor);
80 memmove(textbuf + cursor, textbuf + cursor + 1, len - cursor);
112 textbuf[buf_sz - 1] = cursor;
113 draw_textbox(id, textbuf, cursor, x, y, over);
114 imtk_layout_advance(TEXTBOX_SIZE, TEXTBOX_HEIGHT);
118 static void draw_textbox(int id, char *text, int cursor, int x, int y, int over)
120 float tcol[4], bcol[4];
121 unsigned int attr = 0;
124 attr |= IMTK_FOCUS_BIT;
126 memcpy(tcol, imtk_get_color(IMTK_BOTTOM_COLOR | attr), sizeof tcol);
127 memcpy(bcol, imtk_get_color(IMTK_TOP_COLOR | attr), sizeof bcol);
129 imtk_draw_rect(x, y, TEXTBOX_SIZE, TEXTBOX_HEIGHT, tcol, bcol);
131 if(imtk_has_focus(id)) {
137 strsz = imtk_string_size(text);
141 glColor4fv(imtk_get_color(IMTK_CURSOR_COLOR));
142 glVertex2f(x + strsz + 2, y + 2);
143 glVertex2f(x + strsz + 4, y + 2);
144 glVertex2f(x + strsz + 4, y + 18);
145 glVertex2f(x + strsz + 2, y + 18);
149 glColor4fv(imtk_get_color(IMTK_TEXT_COLOR));
150 imtk_draw_string(x + 2, y + 15, text);
152 imtk_draw_frame(x, y, TEXTBOX_SIZE, TEXTBOX_HEIGHT, FRAME_INSET);