iPXE
editbox.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006 Michael Brown <mbrown@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., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <string.h>
27 #include <assert.h>
28 #include <ipxe/editbox.h>
29 
30 /** @file
31  *
32  * Editable text box widget
33  *
34  */
35 
36 #define EDITBOX_MIN_CHARS 3
37 
38 /**
39  * Initialise text box widget
40  *
41  * @v box Editable text box widget
42  * @v buf Text buffer
43  * @v len Size of text buffer
44  * @v win Containing window
45  * @v row Row
46  * @v col Starting column
47  * @v width Width
48  * @v flags Flags
49  */
50 void init_editbox ( struct edit_box *box, char *buf, size_t len,
51  WINDOW *win, unsigned int row, unsigned int col,
52  unsigned int width, unsigned int flags ) {
53  memset ( box, 0, sizeof ( *box ) );
54  init_editstring ( &box->string, buf, len );
55  box->string.cursor = strlen ( buf );
56  box->win = ( win ? win : stdscr );
57  box->row = row;
58  box->col = col;
59  box->width = width;
60  box->flags = flags;
61 }
62 
63 /**
64  * Draw text box widget
65  *
66  * @v box Editable text box widget
67  *
68  */
69 void draw_editbox ( struct edit_box *box ) {
70  size_t width = box->width;
71  char buf[ width + 1 ];
72  signed int cursor_offset, underflow, overflow, first;
73  size_t len;
74 
75  /* Adjust starting offset so that cursor remains within box */
76  cursor_offset = ( box->string.cursor - box->first );
77  underflow = ( EDITBOX_MIN_CHARS - cursor_offset );
78  overflow = ( cursor_offset - ( width - 1 ) );
79  first = box->first;
80  if ( underflow > 0 ) {
81  first -= underflow;
82  if ( first < 0 )
83  first = 0;
84  } else if ( overflow > 0 ) {
85  first += overflow;
86  }
87  box->first = first;
88  cursor_offset = ( box->string.cursor - first );
89 
90  /* Construct underscore-padded string portion */
91  memset ( buf, '_', width );
92  buf[width] = '\0';
93  len = ( strlen ( box->string.buf ) - first );
94  if ( len > width )
95  len = width;
96  if ( box->flags & EDITBOX_STARS ) {
97  memset ( buf, '*', len );
98  } else {
99  memcpy ( buf, ( box->string.buf + first ), len );
100  }
101 
102  /* Print box content and move cursor */
103  if ( ! box->win )
104  box->win = stdscr;
105  mvwprintw ( box->win, box->row, box->col, "%s", buf );
106  wmove ( box->win, box->row, ( box->col + cursor_offset ) );
107 }
Editable text box widget.
int wmove(WINDOW *win, int y, int x) __nonnull
Move a window's cursor to the specified position.
Definition: mucurses.c:135
int box(WINDOW *win, chtype verch, chtype horch)
Draw borders from single-byte characters and renditions around a window.
Definition: edging.c:22
Curses Window struct.
Definition: curses.h:89
#define mvwprintw(win, y, x, fmt,...)
Definition: curses.h:707
void * memcpy(void *dest, const void *src, size_t len) __nonnull
Assertions.
static void init_editstring(struct edit_string *string, char *buf, size_t len)
Initialise editable string.
Definition: editstring.h:38
An editable text box widget.
Definition: editbox.h:16
#define stdscr
Definition: curses.h:110
void init_editbox(struct edit_box *box, char *buf, size_t len, WINDOW *win, unsigned int row, unsigned int col, unsigned int width, unsigned int flags)
Initialise text box widget.
Definition: editbox.c:50
size_t strlen(const char *src)
Get length of string.
Definition: string.c:243
void draw_editbox(struct edit_box *box)
Draw text box widget.
Definition: editbox.c:69
Show stars instead of contents (for password widgets)
Definition: editbox.h:36
uint32_t len
Length.
Definition: ena.h:14
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
#define EDITBOX_MIN_CHARS
Definition: editbox.c:36
String functions.
uint32_t first
Length to skip in first segment.
Definition: pccrc.h:23
void * memset(void *dest, int character, size_t len) __nonnull
uint8_t flags
Flags.
Definition: ena.h:18