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
19 * You can also choose to distribute this program under the terms of
20 * the Unmodified Binary Distribution Licence (as given in the file
21 * COPYING.UBDL), provided that you have satisfied its requirements.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
28 #include <ipxe/editbox.h>
32 * Editable text box widget
36 #define EDITBOX_MIN_CHARS 3
39 * Initialise text box widget
41 * @v box Editable text box widget
43 * @v len Size of text buffer
44 * @v win Containing window
46 * @v col Starting column
50 void init_editbox ( struct edit_box *box, char *buf, size_t len,
51 WINDOW *win, unsigned int row, unsigned int col,
52 unsigned int width, unsigned int flags ) {
53 memset ( box, 0, sizeof ( *box ) );
54 init_editstring ( &box->string, buf, len );
55 box->string.cursor = strlen ( buf );
56 box->win = ( win ? win : stdscr );
64 * Draw text box widget
66 * @v box Editable text box widget
69 void draw_editbox ( struct edit_box *box ) {
70 size_t width = box->width;
71 char buf[ width + 1 ];
72 signed int cursor_offset, underflow, overflow, first;
75 /* Adjust starting offset so that cursor remains within box */
76 cursor_offset = ( box->string.cursor - box->first );
77 underflow = ( EDITBOX_MIN_CHARS - cursor_offset );
78 overflow = ( cursor_offset - ( width - 1 ) );
80 if ( underflow > 0 ) {
84 } else if ( overflow > 0 ) {
88 cursor_offset = ( box->string.cursor - first );
90 /* Construct underscore-padded string portion */
91 memset ( buf, '_', width );
93 len = ( strlen ( box->string.buf ) - first );
96 if ( box->flags & EDITBOX_STARS ) {
97 memset ( buf, '*', len );
99 memcpy ( buf, ( box->string.buf + first ), len );
102 /* Print box content and move cursor */
105 mvwprintw ( box->win, box->row, box->col, "%s", buf );
106 wmove ( box->win, box->row, ( box->col + cursor_offset ) );