1 | /* |
---|
2 | * neercs console-based window manager |
---|
3 | * Copyright (c) 2009-2010 Jean-Yves Lamoureux <jylam@lnxscene.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * This program is free software. It comes without any warranty, to |
---|
7 | * the extent permitted by applicable law. You can redistribute it |
---|
8 | * and/or modify it under the terms of the Do What The Fuck You Want |
---|
9 | * To Public License, Version 2, as published by Sam Hocevar. See |
---|
10 | * http://sam.zoy.org/wtfpl/COPYING for more details. |
---|
11 | */ |
---|
12 | |
---|
13 | |
---|
14 | #include "widgets.h" |
---|
15 | |
---|
16 | static void widget_ibox_add_char(struct input_box *box, unsigned int c); |
---|
17 | static void widget_ibox_del_char(struct input_box *box); |
---|
18 | |
---|
19 | struct input_box *widget_ibox_init(caca_canvas_t *cv, int w, int h) |
---|
20 | { |
---|
21 | struct input_box *box = malloc(sizeof(struct input_box)); |
---|
22 | if (!box) |
---|
23 | return NULL; |
---|
24 | box->cv = cv; |
---|
25 | box->w = w; |
---|
26 | box->h = h; |
---|
27 | box->command = NULL; |
---|
28 | box->output_err = NULL; |
---|
29 | box->output_res = NULL; |
---|
30 | |
---|
31 | return box; |
---|
32 | } |
---|
33 | |
---|
34 | |
---|
35 | int widget_ibox_draw(struct input_box *box) |
---|
36 | { |
---|
37 | int x = (caca_get_canvas_width(box->cv) - box->w) / 2; |
---|
38 | int y = (caca_get_canvas_height(box->cv) - box->h) / 2; |
---|
39 | |
---|
40 | caca_set_color_ansi(box->cv, CACA_BLUE, CACA_BLUE); |
---|
41 | caca_fill_box(box->cv, x, y, box->w, box->h, '#'); |
---|
42 | caca_set_color_ansi(box->cv, CACA_DEFAULT, CACA_BLUE); |
---|
43 | caca_draw_cp437_box(box->cv, x, y, box->w, box->h); |
---|
44 | caca_printf(box->cv, x, y, "Mini-command"); |
---|
45 | |
---|
46 | caca_printf(box->cv, x + 2, y + 2, |
---|
47 | "[___________________________________________________________]"); |
---|
48 | |
---|
49 | if (box->command) |
---|
50 | { |
---|
51 | caca_printf(box->cv, x + 3, y + 2, "%s", box->command); |
---|
52 | caca_gotoxy(box->cv, x + 3 + box->x, y + 2); |
---|
53 | } |
---|
54 | else |
---|
55 | { |
---|
56 | caca_gotoxy(box->cv, x + 3, y + 2); |
---|
57 | } |
---|
58 | |
---|
59 | if (box->output_err) |
---|
60 | { |
---|
61 | caca_set_color_ansi(box->cv, CACA_RED, CACA_BLUE); |
---|
62 | caca_printf(box->cv, x + 2, y + 4, box->output_err); |
---|
63 | } |
---|
64 | if (box->output_res) |
---|
65 | { |
---|
66 | caca_set_color_ansi(box->cv, CACA_LIGHTGREEN, CACA_BLUE); |
---|
67 | caca_printf(box->cv, x + 2, y + 4, box->output_res); |
---|
68 | } |
---|
69 | |
---|
70 | return 0; |
---|
71 | } |
---|
72 | |
---|
73 | char *widget_ibox_get_text(struct input_box *box) |
---|
74 | { |
---|
75 | return box->command; |
---|
76 | } |
---|
77 | |
---|
78 | void widget_ibox_destroy(struct input_box *box) |
---|
79 | { |
---|
80 | if(!box) return; |
---|
81 | if (box->command) |
---|
82 | free(box->command); |
---|
83 | if (box->output_err) |
---|
84 | free(box->output_err); |
---|
85 | if (box->output_res) |
---|
86 | free(box->output_res); |
---|
87 | } |
---|
88 | |
---|
89 | void widget_ibox_set_error(struct input_box *box, char *err) |
---|
90 | { |
---|
91 | box->output_err = err; |
---|
92 | } |
---|
93 | |
---|
94 | void widget_ibox_set_msg(struct input_box *box, char *msg) |
---|
95 | { |
---|
96 | box->output_res = msg; |
---|
97 | } |
---|
98 | |
---|
99 | int widget_ibox_handle_key(struct input_box *box, unsigned int c) |
---|
100 | { |
---|
101 | if (c == CACA_KEY_ESCAPE) |
---|
102 | { |
---|
103 | if (box->command) |
---|
104 | { |
---|
105 | free(box->command); |
---|
106 | box->command = NULL; |
---|
107 | } |
---|
108 | return INPUT_BOX_ESC; |
---|
109 | } |
---|
110 | else if (c == CACA_KEY_LEFT) |
---|
111 | { |
---|
112 | if (box->x) |
---|
113 | box->x--; |
---|
114 | } |
---|
115 | else if (c == CACA_KEY_RIGHT) |
---|
116 | { |
---|
117 | if (box->x < box->size - 1) |
---|
118 | box->x++; |
---|
119 | } |
---|
120 | else if (c == CACA_KEY_RETURN) |
---|
121 | { |
---|
122 | return INPUT_BOX_RET; |
---|
123 | } |
---|
124 | else |
---|
125 | { |
---|
126 | if (c >= ' ' && c < 127) |
---|
127 | widget_ibox_add_char(box, c); |
---|
128 | else if (c == 8) |
---|
129 | { |
---|
130 | widget_ibox_del_char(box); |
---|
131 | } |
---|
132 | } |
---|
133 | return INPUT_BOX_NOTHING; |
---|
134 | } |
---|
135 | |
---|
136 | |
---|
137 | |
---|
138 | static void widget_ibox_add_char(struct input_box *box, unsigned int c) |
---|
139 | { |
---|
140 | /* FIXME handle return values */ |
---|
141 | if (!box->command) |
---|
142 | { |
---|
143 | box->size = 1; |
---|
144 | box->x = 0; |
---|
145 | box->command = (char *)malloc(2); |
---|
146 | box->command[0] = 0; |
---|
147 | } |
---|
148 | else |
---|
149 | { |
---|
150 | box->command = (char *)realloc(box->command, box->size + 1); |
---|
151 | } |
---|
152 | memmove(&box->command[box->x + 1], |
---|
153 | &box->command[box->x], (box->size - box->x)); |
---|
154 | |
---|
155 | box->command[box->x] = c; |
---|
156 | box->x++; |
---|
157 | box->size++; |
---|
158 | } |
---|
159 | |
---|
160 | static void widget_ibox_del_char(struct input_box *box) |
---|
161 | { |
---|
162 | if (box->x < 1) |
---|
163 | return; |
---|
164 | if (box->size > 1) |
---|
165 | box->size--; |
---|
166 | else |
---|
167 | return; |
---|
168 | |
---|
169 | memcpy(&box->command[box->x - 1], &box->command[box->x], box->size - box->x); |
---|
170 | |
---|
171 | box->command = (char *)realloc(box->command, box->size); |
---|
172 | |
---|
173 | if (box->x) |
---|
174 | box->x--; |
---|
175 | box->command[box->size - 1] = 0; |
---|
176 | } |
---|