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 ];
65 signed int cursor_offset
, underflow
, overflow
, first
;
68 /* Adjust starting offset so that cursor remains within box */
69 cursor_offset
= ( box
->string
.cursor
- box
->first
);
70 underflow
= ( EDITBOX_MIN_CHARS
- cursor_offset
);
71 overflow
= ( cursor_offset
- ( width
- 1 ) );
73 if ( underflow
> 0 ) {
77 } else if ( overflow
> 0 ) {
81 cursor_offset
= ( box
->string
.cursor
- first
);
83 /* Construct underscore-padded string portion */
84 memset ( buf
, '_', width
);
86 len
= ( strlen ( box
->string
.buf
) - first
);
89 memcpy ( buf
, ( box
->string
.buf
+ 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
) );