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/ansicol.h>
29 #include <ipxe/editbox.h>
30 
31 /** @file
32  *
33  * Editable text box widget
34  *
35  */
36 
37 #define EDITBOX_MIN_CHARS 3
38 
39 /**
40  * Draw text box widget
41  *
42  * @v widget Text widget
43  */
44 static void draw_editbox ( struct widget *widget ) {
45  struct edit_box *box = container_of ( widget, struct edit_box, widget );
46  const char *content = *(box->string.buf);
47  size_t width = widget->width;
48  char buf[ width + 1 ];
49  signed int cursor_offset, underflow, overflow, first;
50  size_t len;
51 
52  /* Adjust starting offset so that cursor remains within box */
53  cursor_offset = ( box->string.cursor - box->first );
54  underflow = ( EDITBOX_MIN_CHARS - cursor_offset );
55  overflow = ( cursor_offset - ( width - 1 ) );
56  first = box->first;
57  if ( underflow > 0 ) {
58  first -= underflow;
59  if ( first < 0 )
60  first = 0;
61  } else if ( overflow > 0 ) {
62  first += overflow;
63  }
64  box->first = first;
65  cursor_offset = ( box->string.cursor - first );
66 
67  /* Construct underscore-padded string portion */
68  memset ( buf, '_', width );
69  buf[width] = '\0';
70  len = ( content ? ( strlen ( content ) - first ) : 0 );
71  if ( len > width )
72  len = width;
73  if ( widget->flags & WIDGET_SECRET ) {
74  memset ( buf, '*', len );
75  } else {
76  memcpy ( buf, ( content + first ), len );
77  }
78 
79  /* Print box content and move cursor */
81  mvprintw ( widget->row, widget->col, "%s", buf );
82  move ( widget->row, ( widget->col + cursor_offset ) );
84 }
85 
86 /**
87  * Edit text box widget
88  *
89  * @v widget Text widget
90  * @v key Key pressed by user
91  * @ret key Key returned to application, or zero
92  */
93 static int edit_editbox ( struct widget *widget, int key ) {
94  struct edit_box *box = container_of ( widget, struct edit_box, widget );
95 
96  return edit_string ( &box->string, key );
97 }
98 
99 /** Text box widget operations */
101  .draw = draw_editbox,
102  .edit = edit_editbox,
103 };
Editable text box widget.
unsigned int width
Width.
Definition: widget.h:24
int box(WINDOW *win, chtype verch, chtype horch)
Draw borders from single-byte characters and renditions around a window.
Definition: edging.c:22
#define CPAIR_EDIT
Editable text.
Definition: ansicol.h:49
unsigned int flags
Flags.
Definition: widget.h:26
void(* draw)(struct widget *widget)
Draw widget.
Definition: widget.h:44
Widget contains a secret.
Definition: widget.h:34
int edit_string(struct edit_string *string, int key)
Edit editable string.
Definition: editstring.c:255
#define mvprintw(y, x, fmt,...)
Definition: curses.h:648
static void draw_editbox(struct widget *widget)
Draw text box widget.
Definition: editbox.c:44
#define CPAIR_NORMAL
Normal text.
Definition: ansicol.h:40
struct widget_operations editbox_operations
Text box widget operations.
Definition: editbox.c:100
void * memcpy(void *dest, const void *src, size_t len) __nonnull
Assertions.
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
static int edit_editbox(struct widget *widget, int key)
Edit text box widget.
Definition: editbox.c:93
An editable text box widget.
Definition: editbox.h:17
Text widget operations.
Definition: widget.h:38
size_t strlen(const char *src)
Get length of string.
Definition: string.c:243
unsigned int col
Starting column.
Definition: widget.h:22
uint32_t len
Length.
Definition: ena.h:14
unsigned int row
Row.
Definition: widget.h:20
static int move(int y, int x)
Definition: curses.h:593
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
#define color_set(cpno, opts)
Definition: curses.h:240
A text widget.
Definition: widget.h:15
#define EDITBOX_MIN_CHARS
Definition: editbox.c:37
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
String functions.
uint32_t first
Length to skip in first segment.
Definition: pccrc.h:23
union @383 key
Sense key.
Definition: crypto.h:284
ANSI colours.
void * memset(void *dest, int character, size_t len) __nonnull