Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / hci / mucurses / widgets / editbox.c
1 /*
2  * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <string.h>
23 #include <assert.h>
24 #include <ipxe/editbox.h>
25
26 /** @file
27  *
28  * Editable text box widget
29  *
30  */
31
32 #define EDITBOX_MIN_CHARS 3
33
34 /**
35  * Initialise text box widget
36  *
37  * @v box               Editable text box widget
38  * @v buf               Text buffer
39  * @v len               Size of text buffer
40  * @v win               Containing window
41  * @v row               Row
42  * @v col               Starting column
43  * @v width             Width
44  * @v flags             Flags
45  */
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 );
53         box->row = row;
54         box->col = col;
55         box->width = width;
56         box->flags = flags;
57 }
58
59 /**
60  * Draw text box widget
61  *
62  * @v box               Editable text box widget
63  *
64  */
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;
69         size_t len;
70
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 ) );
75         first = box->first;
76         if ( underflow > 0 ) {
77                 first -= underflow;
78                 if ( first < 0 )
79                         first = 0;
80         } else if ( overflow > 0 ) {
81                 first += overflow;
82         }
83         box->first = first;
84         cursor_offset = ( box->string.cursor - first );
85
86         /* Construct underscore-padded string portion */
87         memset ( buf, '_', width );
88         buf[width] = '\0';
89         len = ( strlen ( box->string.buf ) - first );
90         if ( len > width )
91                 len = width;
92         if ( box->flags & EDITBOX_STARS ) {
93                 memset ( buf, '*', len );
94         } else {
95                 memcpy ( buf, ( box->string.buf + first ), len );
96         }
97
98         /* Print box content and move cursor */
99         if ( ! box->win )
100                 box->win = stdscr;
101         mvwprintw ( box->win, box->row, box->col, "%s", buf );
102         wmove ( box->win, box->row, ( box->col + cursor_offset ) );
103 }