1 | /* |
---|
2 | * input libcaca text input test program |
---|
3 | * Copyright (c) 2006 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id: input.c 1048 2006-09-17 12:44:18Z jylam $ |
---|
7 | * |
---|
8 | * This program is free software; you can redistribute it and/or |
---|
9 | * modify it under the terms of the Do What The Fuck You Want To |
---|
10 | * Public License, Version 2, as published by Sam Hocevar. See |
---|
11 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
12 | */ |
---|
13 | |
---|
14 | #include "config.h" |
---|
15 | #include "common.h" |
---|
16 | |
---|
17 | #if !defined(__KERNEL__) |
---|
18 | # if defined(HAVE_INTTYPES_H) |
---|
19 | # include <inttypes.h> |
---|
20 | # endif |
---|
21 | # include <string.h> |
---|
22 | #endif |
---|
23 | |
---|
24 | #include "cucul.h" |
---|
25 | #include "caca.h" |
---|
26 | |
---|
27 | #define BUFFER_SIZE 75 |
---|
28 | #define TEXT_ENTRIES 5 |
---|
29 | |
---|
30 | typedef struct textentry |
---|
31 | { |
---|
32 | uint32_t buffer[BUFFER_SIZE + 1]; |
---|
33 | unsigned int size, cursor; |
---|
34 | } textentry; |
---|
35 | |
---|
36 | int main(int argc, char *argv[]) |
---|
37 | { |
---|
38 | textentry entries[TEXT_ENTRIES]; |
---|
39 | cucul_canvas_t *cv; |
---|
40 | caca_display_t *dp; |
---|
41 | unsigned int i, e = 0, running = 1; |
---|
42 | |
---|
43 | cv = cucul_create_canvas(0, 0); |
---|
44 | dp = caca_create_display(cv); |
---|
45 | |
---|
46 | cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE); |
---|
47 | cucul_putstr(cv, 1, 1, "Text entries - press tab to cycle"); |
---|
48 | |
---|
49 | for(i = 0; i < TEXT_ENTRIES; i++) |
---|
50 | { |
---|
51 | entries[i].buffer[0] = 0; |
---|
52 | entries[i].size = 0; |
---|
53 | entries[i].cursor = 0; |
---|
54 | } |
---|
55 | |
---|
56 | while(running) |
---|
57 | { |
---|
58 | caca_event_t ev; |
---|
59 | |
---|
60 | for(i = 0; i < TEXT_ENTRIES; i++) |
---|
61 | { |
---|
62 | unsigned int j, start, size; |
---|
63 | |
---|
64 | cucul_set_color(cv, CUCUL_COLOR_BLACK, CUCUL_COLOR_LIGHTGRAY); |
---|
65 | cucul_fill_box(cv, 2, 3 * i + 4, 2 + BUFFER_SIZE, 3 * i + 4, " "); |
---|
66 | |
---|
67 | start = 0; |
---|
68 | size = entries[i].size; |
---|
69 | |
---|
70 | for(j = 0; j < size; j++) |
---|
71 | { |
---|
72 | cucul_putchar(cv, 2 + j, 3 * i + 4, |
---|
73 | entries[i].buffer[start + j]); |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | /* Put the cursor on the active textentry */ |
---|
78 | cucul_set_color(cv, CUCUL_COLOR_LIGHTRED, CUCUL_COLOR_LIGHTRED); |
---|
79 | cucul_putchar(cv, 2 + entries[e].cursor, 3 * e + 4, ' '); |
---|
80 | |
---|
81 | caca_refresh_display(dp); |
---|
82 | |
---|
83 | if(caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, -1) == 0) |
---|
84 | continue; |
---|
85 | |
---|
86 | switch(ev.data.key.ch) |
---|
87 | { |
---|
88 | case CACA_KEY_ESCAPE: |
---|
89 | running = 0; |
---|
90 | break; |
---|
91 | case CACA_KEY_TAB: |
---|
92 | case CACA_KEY_RETURN: |
---|
93 | e = (e + 1) % TEXT_ENTRIES; |
---|
94 | break; |
---|
95 | case CACA_KEY_HOME: |
---|
96 | entries[e].cursor = 0; |
---|
97 | break; |
---|
98 | case CACA_KEY_END: |
---|
99 | entries[e].cursor = entries[e].size; |
---|
100 | break; |
---|
101 | case CACA_KEY_LEFT: |
---|
102 | if(entries[e].cursor) |
---|
103 | entries[e].cursor--; |
---|
104 | break; |
---|
105 | case CACA_KEY_RIGHT: |
---|
106 | if(entries[e].cursor < entries[e].size) |
---|
107 | entries[e].cursor++; |
---|
108 | break; |
---|
109 | case CACA_KEY_DELETE: |
---|
110 | if(entries[e].cursor < entries[e].size) |
---|
111 | { |
---|
112 | memmove(entries[e].buffer + entries[e].cursor, |
---|
113 | entries[e].buffer + entries[e].cursor + 1, |
---|
114 | (entries[e].size - entries[e].cursor + 1) * 4); |
---|
115 | entries[e].size--; |
---|
116 | } |
---|
117 | break; |
---|
118 | case CACA_KEY_BACKSPACE: |
---|
119 | if(entries[e].cursor) |
---|
120 | { |
---|
121 | memmove(entries[e].buffer + entries[e].cursor - 1, |
---|
122 | entries[e].buffer + entries[e].cursor, |
---|
123 | (entries[e].size - entries[e].cursor) * 4); |
---|
124 | entries[e].size--; |
---|
125 | entries[e].cursor--; |
---|
126 | } |
---|
127 | break; |
---|
128 | default: |
---|
129 | if(entries[e].size < BUFFER_SIZE) |
---|
130 | { |
---|
131 | memmove(entries[e].buffer + entries[e].cursor + 1, |
---|
132 | entries[e].buffer + entries[e].cursor, |
---|
133 | (entries[e].size - entries[e].cursor) * 4); |
---|
134 | entries[e].buffer[entries[e].cursor] = ev.data.key.utf32; |
---|
135 | entries[e].size++; |
---|
136 | entries[e].cursor++; |
---|
137 | } |
---|
138 | break; |
---|
139 | } |
---|
140 | } |
---|
141 | |
---|
142 | caca_free_display(dp); |
---|
143 | cucul_free_canvas(cv); |
---|
144 | |
---|
145 | return 0; |
---|
146 | } |
---|
147 | |
---|