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