Adding debian version 3.70~pre8+dfsg-1.
[syslinux-debian/hramrach.git] / gpxe / src / hci / mucurses / widgets / editbox.c
blob5c066fda7ec22f358c3b71089b0718f11084caf2
1 /*
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 #include <string.h>
20 #include <assert.h>
21 #include <gpxe/editbox.h>
23 /** @file
25 * Editable text box widget
29 #define EDITBOX_MIN_CHARS 3
31 /**
32 * Initialise text box widget
34 * @v box Editable text box widget
35 * @v buf Text buffer
36 * @v len Size of text buffer
37 * @v win Containing window
38 * @v row Row
39 * @v col Starting column
40 * @v width Width
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 );
51 box->row = row;
52 box->col = col;
53 box->width = width;
56 /**
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;
66 size_t len;
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 ) );
72 first = box->first;
73 if ( underflow > 0 ) {
74 first -= underflow;
75 if ( first < 0 )
76 first = 0;
77 } else if ( overflow > 0 ) {
78 first += overflow;
80 box->first = first;
81 cursor_offset = ( box->string.cursor - first );
83 /* Construct underscore-padded string portion */
84 memset ( buf, '_', width );
85 buf[width] = '\0';
86 len = ( strlen ( box->string.buf ) - first );
87 if ( len > width )
88 len = width;
89 memcpy ( buf, ( box->string.buf + first ), len );
91 /* Print box content and move cursor */
92 if ( ! box->win )
93 box->win = stdscr;
94 mvwprintw ( box->win, box->row, box->col, "%s", buf );
95 wmove ( box->win, box->row, ( box->col + cursor_offset ) );