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.
19 FILE_LICENCE ( GPL2_OR_LATER
);
23 #include <gpxe/editbox.h>
27 * Editable text box widget
31 #define EDITBOX_MIN_CHARS 3
34 * Initialise text box widget
36 * @v box Editable text box widget
38 * @v len Size of text buffer
39 * @v win Containing window
41 * @v col Starting column
45 void init_editbox ( struct edit_box
*box
, char *buf
, size_t len
,
46 WINDOW
*win
, unsigned int row
, unsigned int col
,
47 unsigned int width
, unsigned int flags
) {
48 memset ( box
, 0, sizeof ( *box
) );
49 box
->string
.buf
= buf
;
50 box
->string
.len
= 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
) );