1 /* 2 * Copyright (C) 2006 Michael Brown <mbrown (at) 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., 675 Mass Ave, Cambridge, MA 02139, USA. 17 */ 18 19 FILE_LICENCE ( GPL2_OR_LATER ); 20 21 #include <string.h> 22 #include <assert.h> 23 #include <gpxe/editbox.h> 24 25 /** @file 26 * 27 * Editable text box widget 28 * 29 */ 30 31 #define EDITBOX_MIN_CHARS 3 32 33 /** 34 * Initialise text box widget 35 * 36 * @v box Editable text box widget 37 * @v buf Text buffer 38 * @v len Size of text buffer 39 * @v win Containing window 40 * @v row Row 41 * @v col Starting column 42 * @v width Width 43 * @v flags Flags 44 */ 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 ); 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 } 104