iPXE
editbox.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <string.h>
00027 #include <assert.h>
00028 #include <ipxe/editbox.h>
00029 
00030 /** @file
00031  *
00032  * Editable text box widget
00033  *
00034  */
00035 
00036 #define EDITBOX_MIN_CHARS 3
00037 
00038 /**
00039  * Initialise text box widget
00040  *
00041  * @v box               Editable text box widget
00042  * @v buf               Text buffer
00043  * @v len               Size of text buffer
00044  * @v win               Containing window
00045  * @v row               Row
00046  * @v col               Starting column
00047  * @v width             Width
00048  * @v flags             Flags
00049  */
00050 void init_editbox ( struct edit_box *box, char *buf, size_t len,
00051                     WINDOW *win, unsigned int row, unsigned int col,
00052                     unsigned int width, unsigned int flags ) {
00053         memset ( box, 0, sizeof ( *box ) );
00054         init_editstring ( &box->string, buf, len );
00055         box->string.cursor = strlen ( buf );
00056         box->win = ( win ? win : stdscr );
00057         box->row = row;
00058         box->col = col;
00059         box->width = width;
00060         box->flags = flags;
00061 }
00062 
00063 /**
00064  * Draw text box widget
00065  *
00066  * @v box               Editable text box widget
00067  *
00068  */
00069 void draw_editbox ( struct edit_box *box ) {
00070         size_t width = box->width;
00071         char buf[ width + 1 ];
00072         signed int cursor_offset, underflow, overflow, first;
00073         size_t len;
00074 
00075         /* Adjust starting offset so that cursor remains within box */
00076         cursor_offset = ( box->string.cursor - box->first );
00077         underflow = ( EDITBOX_MIN_CHARS - cursor_offset );
00078         overflow = ( cursor_offset - ( width - 1 ) );
00079         first = box->first;
00080         if ( underflow > 0 ) {
00081                 first -= underflow;
00082                 if ( first < 0 )
00083                         first = 0;
00084         } else if ( overflow > 0 ) {
00085                 first += overflow;
00086         }
00087         box->first = first;
00088         cursor_offset = ( box->string.cursor - first );
00089 
00090         /* Construct underscore-padded string portion */
00091         memset ( buf, '_', width );
00092         buf[width] = '\0';
00093         len = ( strlen ( box->string.buf ) - first );
00094         if ( len > width )
00095                 len = width;
00096         if ( box->flags & EDITBOX_STARS ) {
00097                 memset ( buf, '*', len );
00098         } else {
00099                 memcpy ( buf, ( box->string.buf + first ), len );
00100         }
00101 
00102         /* Print box content and move cursor */
00103         if ( ! box->win )
00104                 box->win = stdscr;
00105         mvwprintw ( box->win, box->row, box->col, "%s", buf );
00106         wmove ( box->win, box->row, ( box->col + cursor_offset ) );
00107 }