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 widgets Text widget set
43  * @v widget Text widget
44  */
45 static void draw_editbox ( struct widgets *widgets, 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  mvwprintw ( widgets->win, widget->row, widget->col, "%s", buf );
83  wmove ( widgets->win, widget->row, ( widget->col + cursor_offset ) );
85 }
86 
87 /**
88  * Edit text box widget
89  *
90  * @v widgets Text widget set
91  * @v widget Text widget
92  * @v key Key pressed by user
93  * @ret key Key returned to application, or zero
94  */
95 static int edit_editbox ( struct widgets *widgets __unused,
96  struct widget *widget, int key ) {
97  struct edit_box *box = container_of ( widget, struct edit_box, widget );
98 
99  return edit_string ( &box->string, key );
100 }
101 
102 /** Text box widget operations */
104  .draw = draw_editbox,
105  .edit = edit_editbox,
106 };
Editable text box widget.
unsigned int width
Width.
Definition: widget.h:35
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
void(* draw)(struct widgets *widgets, struct widget *widget)
Draw widget.
Definition: widget.h:56
#define CPAIR_EDIT
Editable text.
Definition: ansicol.h:49
unsigned int flags
Flags.
Definition: widget.h:37
Widget contains a secret.
Definition: widget.h:45
#define mvwprintw(win, y, x, fmt,...)
Definition: curses.h:707
A text widget set.
Definition: widget.h:16
int edit_string(struct edit_string *string, int key)
Edit editable string.
Definition: editstring.c:255
#define CPAIR_NORMAL
Normal text.
Definition: ansicol.h:40
struct widget_operations editbox_operations
Text box widget operations.
Definition: editbox.c:103
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 void draw_editbox(struct widgets *widgets, struct widget *widget)
Draw text box widget.
Definition: editbox.c:45
An editable text box widget.
Definition: editbox.h:17
WINDOW * win
Containing window.
Definition: widget.h:20
Text widget operations.
Definition: widget.h:49
size_t strlen(const char *src)
Get length of string.
Definition: string.c:243
unsigned int col
Starting column.
Definition: widget.h:33
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
uint32_t len
Length.
Definition: ena.h:14
unsigned int row
Row.
Definition: widget.h:31
static int edit_editbox(struct widgets *widgets __unused, struct widget *widget, int key)
Edit text box widget.
Definition: editbox.c:95
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
#define color_set(cpno, opts)
Definition: curses.h:240
A text widget.
Definition: widget.h:24
#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 @382 key
Sense key.
Definition: crypto.h:284
ANSI colours.
void * memset(void *dest, int character, size_t len) __nonnull