imtk

view src/textbox.c @ 12:17c9525b2e35

merged -lGLU fix with listbox patch
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 17 Apr 2011 18:20:23 +0300
parents 38609a9f7586
children 9c7987064bb0
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 imtk_draw_rect(x, y, TEXTBOX_SIZE, 20, 0);
77 if(imtk_has_focus(id)) {
78 glBegin(GL_LINES);
79 glColor4f(0.8, 0.25, 0.18, imtk_get_color(IMTK_TEXT_COLOR)[3]);
80 glVertex2f(x + strsz + 3, y + 2);
81 glVertex2f(x + strsz + 3, y + 18);
82 glVertex2f(x + strsz + 4, y + 2);
83 glVertex2f(x + strsz + 4, y + 18);
84 glEnd();
85 }
87 glColor4fv(imtk_get_color(IMTK_TEXT_COLOR));
88 imtk_draw_string(x + 2, y + 15, text);
90 imtk_draw_frame(x, y, TEXTBOX_SIZE, 20, FRAME_INSET);
91 }