2 * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <gpxe/editbox.h>
25 * Editable text box widget
29 #define EDITBOX_MIN_CHARS 3
32 * Initialise text box widget
34 * @v box Editable text box widget
36 * @v len Size of text buffer
37 * @v win Containing window
39 * @v col Starting column
43 void init_editbox ( struct edit_box *box, char *buf, size_t len,
44 WINDOW *win, unsigned int row, unsigned int col,
45 unsigned int width ) {
46 memset ( box, 0, sizeof ( *box ) );
47 box->string.buf = buf;
48 box->string.len = len;
49 box->string.cursor = strlen ( buf );
50 box->win = ( win ? win : stdscr );
57 * Draw text box widget
59 * @v box Editable text box widget
62 void draw_editbox ( struct edit_box *box ) {
63 size_t width = box->width;
64 char buf[ width + 1 ];
66 signed int cursor_offset, underflow, overflow;
69 /* Adjust starting offset so that cursor remains within box */
70 cursor_offset = ( box->string.cursor - box->first );
71 keep_len = strlen ( box->string.buf );
72 if ( keep_len > EDITBOX_MIN_CHARS )
73 keep_len = EDITBOX_MIN_CHARS;
74 underflow = ( keep_len - cursor_offset );
75 overflow = ( cursor_offset - ( width - 1 ) );
76 if ( underflow > 0 ) {
77 box->first -= underflow;
78 } else if ( overflow > 0 ) {
79 box->first += overflow;
81 cursor_offset = ( box->string.cursor - box->first );
83 /* Construct underscore-padded string portion */
84 memset ( buf, '_', width );
86 len = ( strlen ( box->string.buf ) - box->first );
89 memcpy ( buf, ( box->string.buf + box->first ), len );
91 /* Print box content and move cursor */
94 mvwprintw ( box->win, box->row, box->col, "%s", buf );
95 wmove ( box->win, box->row, ( box->col + cursor_offset ) );
99 * Edit text box widget
101 * @v box Editable text box widget
102 * @v key Key pressed by user
103 * @ret key Key returned to application, or zero
106 int edit_editbox ( struct edit_box *box, int key ) {
108 /* Update the string itself */
109 key = edit_string ( &box->string, key );
111 /* Update the display */
112 draw_editbox ( box );