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