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., 51 Franklin Street, Fifth Floor, Boston, MA
20 FILE_LICENCE ( GPL2_OR_LATER );
24 #include <ipxe/editbox.h>
28 * Editable text box widget
32 #define EDITBOX_MIN_CHARS 3
35 * Initialise text box widget
37 * @v box Editable text box widget
39 * @v len Size of text buffer
40 * @v win Containing window
42 * @v col Starting column
46 void init_editbox ( struct edit_box *box, char *buf, size_t len,
47 WINDOW *win, unsigned int row, unsigned int col,
48 unsigned int width, unsigned int flags ) {
49 memset ( box, 0, sizeof ( *box ) );
50 init_editstring ( &box->string, buf, len );
51 box->string.cursor = strlen ( buf );
52 box->win = ( win ? win : stdscr );
60 * Draw text box widget
62 * @v box Editable text box widget
65 void draw_editbox ( struct edit_box *box ) {
66 size_t width = box->width;
67 char buf[ width + 1 ];
68 signed int cursor_offset, underflow, overflow, first;
71 /* Adjust starting offset so that cursor remains within box */
72 cursor_offset = ( box->string.cursor - box->first );
73 underflow = ( EDITBOX_MIN_CHARS - cursor_offset );
74 overflow = ( cursor_offset - ( width - 1 ) );
76 if ( underflow > 0 ) {
80 } else if ( overflow > 0 ) {
84 cursor_offset = ( box->string.cursor - first );
86 /* Construct underscore-padded string portion */
87 memset ( buf, '_', width );
89 len = ( strlen ( box->string.buf ) - first );
92 if ( box->flags & EDITBOX_STARS ) {
93 memset ( buf, '*', len );
95 memcpy ( buf, ( box->string.buf + first ), len );
98 /* Print box content and move cursor */
101 mvwprintw ( box->win, box->row, box->col, "%s", buf );
102 wmove ( box->win, box->row, ( box->col + cursor_offset ) );