|
|
1.1 ! root 1: /* ! 2: * Copyright (C) 2006 Michael Brown <[email protected]>. ! 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 <ipxe/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: init_editstring ( &box->string, buf, len ); ! 50: box->string.cursor = strlen ( buf ); ! 51: box->win = ( win ? win : stdscr ); ! 52: box->row = row; ! 53: box->col = col; ! 54: box->width = width; ! 55: box->flags = flags; ! 56: } ! 57: ! 58: /** ! 59: * Draw text box widget ! 60: * ! 61: * @v box Editable text box widget ! 62: * ! 63: */ ! 64: void draw_editbox ( struct edit_box *box ) { ! 65: size_t width = box->width; ! 66: char buf[ width + 1 ]; ! 67: signed int cursor_offset, underflow, overflow, first; ! 68: size_t len; ! 69: ! 70: /* Adjust starting offset so that cursor remains within box */ ! 71: cursor_offset = ( box->string.cursor - box->first ); ! 72: underflow = ( EDITBOX_MIN_CHARS - cursor_offset ); ! 73: overflow = ( cursor_offset - ( width - 1 ) ); ! 74: first = box->first; ! 75: if ( underflow > 0 ) { ! 76: first -= underflow; ! 77: if ( first < 0 ) ! 78: first = 0; ! 79: } else if ( overflow > 0 ) { ! 80: first += overflow; ! 81: } ! 82: box->first = first; ! 83: cursor_offset = ( box->string.cursor - first ); ! 84: ! 85: /* Construct underscore-padded string portion */ ! 86: memset ( buf, '_', width ); ! 87: buf[width] = '\0'; ! 88: len = ( strlen ( box->string.buf ) - first ); ! 89: if ( len > width ) ! 90: len = width; ! 91: if ( box->flags & EDITBOX_STARS ) { ! 92: memset ( buf, '*', len ); ! 93: } else { ! 94: memcpy ( buf, ( box->string.buf + first ), len ); ! 95: } ! 96: ! 97: /* Print box content and move cursor */ ! 98: if ( ! box->win ) ! 99: box->win = stdscr; ! 100: mvwprintw ( box->win, box->row, box->col, "%s", buf ); ! 101: wmove ( box->win, box->row, ( box->col + cursor_offset ) ); ! 102: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.