editbox.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 FILE_LICENCE ( GPL2_OR_LATER );
00020
00021 #include <string.h>
00022 #include <assert.h>
00023 #include <gpxe/editbox.h>
00024
00025
00026
00027
00028
00029
00030
00031 #define EDITBOX_MIN_CHARS 3
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
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
00061
00062
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
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
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
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 }