| 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) return; |
|---|
| 83 | if (box->command) |
|---|
| 84 | free(box->command); |
|---|
| 85 | if (box->output_err) |
|---|
| 86 | free(box->output_err); |
|---|
| 87 | if (box->output_res) |
|---|
| 88 | free(box->output_res); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | void widget_ibox_set_error(struct input_box *box, char *err) |
|---|
| 92 | { |
|---|
| 93 | box->output_err = err; |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | void widget_ibox_set_msg(struct input_box *box, char *msg) |
|---|
| 97 | { |
|---|
| 98 | box->output_res = msg; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | int widget_ibox_handle_key(struct input_box *box, unsigned int c) |
|---|
| 102 | { |
|---|
| 103 | if (c == CACA_KEY_ESCAPE) |
|---|
| 104 | { |
|---|
| 105 | if (box->command) |
|---|
| 106 | { |
|---|
| 107 | free(box->command); |
|---|
| 108 | box->command = NULL; |
|---|
| 109 | } |
|---|
| 110 | return INPUT_BOX_ESC; |
|---|
| 111 | } |
|---|
| 112 | else if (c == CACA_KEY_LEFT) |
|---|
| 113 | { |
|---|
| 114 | if (box->x) |
|---|
| 115 | box->x--; |
|---|
| 116 | } |
|---|
| 117 | else if (c == CACA_KEY_RIGHT) |
|---|
| 118 | { |
|---|
| 119 | if (box->x < box->size - 1) |
|---|
| 120 | box->x++; |
|---|
| 121 | } |
|---|
| 122 | else if (c == CACA_KEY_RETURN) |
|---|
| 123 | { |
|---|
| 124 | return INPUT_BOX_RET; |
|---|
| 125 | } |
|---|
| 126 | else |
|---|
| 127 | { |
|---|
| 128 | if (c >= ' ' && c < 127) |
|---|
| 129 | widget_ibox_add_char(box, c); |
|---|
| 130 | else if (c == 8) |
|---|
| 131 | { |
|---|
| 132 | widget_ibox_del_char(box); |
|---|
| 133 | } |
|---|
| 134 | } |
|---|
| 135 | return INPUT_BOX_NOTHING; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | static void widget_ibox_add_char(struct input_box *box, unsigned int c) |
|---|
| 141 | { |
|---|
| 142 | /* FIXME handle return values */ |
|---|
| 143 | if (!box->command) |
|---|
| 144 | { |
|---|
| 145 | box->size = 1; |
|---|
| 146 | box->x = 0; |
|---|
| 147 | box->command = (char *)malloc(2); |
|---|
| 148 | box->command[0] = 0; |
|---|
| 149 | } |
|---|
| 150 | else |
|---|
| 151 | { |
|---|
| 152 | box->command = (char *)realloc(box->command, box->size + 1); |
|---|
| 153 | } |
|---|
| 154 | memmove(&box->command[box->x + 1], |
|---|
| 155 | &box->command[box->x], (box->size - box->x)); |
|---|
| 156 | |
|---|
| 157 | box->command[box->x] = c; |
|---|
| 158 | box->x++; |
|---|
| 159 | box->size++; |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | static void widget_ibox_del_char(struct input_box *box) |
|---|
| 163 | { |
|---|
| 164 | if (box->x < 1) |
|---|
| 165 | return; |
|---|
| 166 | if (box->size > 1) |
|---|
| 167 | box->size--; |
|---|
| 168 | else |
|---|
| 169 | return; |
|---|
| 170 | |
|---|
| 171 | memcpy(&box->command[box->x - 1], &box->command[box->x], box->size - box->x); |
|---|
| 172 | |
|---|
| 173 | box->command = (char *)realloc(box->command, box->size); |
|---|
| 174 | |
|---|
| 175 | if (box->x) |
|---|
| 176 | box->x--; |
|---|
| 177 | box->command[box->size - 1] = 0; |
|---|
| 178 | } |
|---|