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., 675 Mass Ave, Cambridge, MA 02139, USA.
00017  */
00018 
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020 
00021 #include <string.h>
00022 #include <assert.h>
00023 #include <gpxe/editbox.h>
00024 
00025 /** @file
00026  *
00027  * Editable text box widget
00028  *
00029  */
00030 
00031 #define EDITBOX_MIN_CHARS 3
00032 
00033 /**
00034  * Initialise text box widget
00035  *
00036  * @v box               Editable text box widget
00037  * @v buf               Text buffer
00038  * @v len               Size of text buffer
00039  * @v win               Containing window
00040  * @v row               Row
00041  * @v col               Starting column
00042  * @v width             Width
00043  * @v flags             Flags
00044  */
00045 void init_editbox ( struct edit_box *box, char *buf, size_t len,
00046                     WINDOW *win, unsigned int row, unsigned int col,
00047                     unsigned int width, unsigned int flags ) {
00048         memset ( box, 0, sizeof ( *box ) );
00049         box->string.buf = buf;
00050         box->string.len = len;
00051         box->string.cursor = strlen ( buf );
00052         box->win = ( win ? win : stdscr );
00053         box->row = row;
00054         box->col = col;
00055         box->width = width;
00056         box->flags = flags;
00057 }
00058 
00059 /**
00060  * Draw text box widget
00061  *
00062  * @v box               Editable text box widget
00063  *
00064  */
00065 void draw_editbox ( struct edit_box *box ) {
00066         size_t width = box->width;
00067         char buf[ width + 1 ];
00068         signed int cursor_offset, underflow, overflow, first;
00069         size_t len;
00070 
00071         /* Adjust starting offset so that cursor remains within box */
00072         cursor_offset = ( box->string.cursor - box->first );
00073         underflow = ( EDITBOX_MIN_CHARS - cursor_offset );
00074         overflow = ( cursor_offset - ( width - 1 ) );
00075         first = box->first;
00076         if ( underflow > 0 ) {
00077                 first -= underflow;
00078                 if ( first < 0 )
00079                         first = 0;
00080         } else if ( overflow > 0 ) {
00081                 first += overflow;
00082         }
00083         box->first = first;
00084         cursor_offset = ( box->string.cursor - first );
00085 
00086         /* Construct underscore-padded string portion */
00087         memset ( buf, '_', width );
00088         buf[width] = '\0';
00089         len = ( strlen ( box->string.buf ) - first );
00090         if ( len > width )
00091                 len = width;
00092         if ( box->flags & EDITBOX_STARS ) {
00093                 memset ( buf, '*', len );
00094         } else {
00095                 memcpy ( buf, ( box->string.buf + first ), len );
00096         }
00097 
00098         /* Print box content and move cursor */
00099         if ( ! box->win )
00100                 box->win = stdscr;
00101         mvwprintw ( box->win, box->row, box->col, "%s", buf );
00102         wmove ( box->win, box->row, ( box->col + cursor_offset ) );
00103 }

Generated on Tue Apr 6 20:01:06 2010 for gPXE by  doxygen 1.5.7.1