|
@@ -0,0 +1,115 @@
|
|
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., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
17
|
+ */
|
|
18
|
+
|
|
19
|
+#include <string.h>
|
|
20
|
+#include <assert.h>
|
|
21
|
+#include <gpxe/editbox.h>
|
|
22
|
+
|
|
23
|
+/** @file
|
|
24
|
+ *
|
|
25
|
+ * Editable text box widget
|
|
26
|
+ *
|
|
27
|
+ */
|
|
28
|
+
|
|
29
|
+#define EDITBOX_MIN_CHARS 3
|
|
30
|
+
|
|
31
|
+/**
|
|
32
|
+ * Initialise text box widget
|
|
33
|
+ *
|
|
34
|
+ * @v box Editable text box widget
|
|
35
|
+ * @v buf Text buffer
|
|
36
|
+ * @v len Size of text buffer
|
|
37
|
+ * @v win Containing window
|
|
38
|
+ * @v row Row
|
|
39
|
+ * @v col Starting column
|
|
40
|
+ * @v width Width
|
|
41
|
+ *
|
|
42
|
+ */
|
|
43
|
+void init_editbox ( struct edit_box *box, char *buf, size_t len,
|
|
44
|
+ WINDOW *win, unsigned int row, unsigned int col,
|
|
45
|
+ unsigned int width ) {
|
|
46
|
+ memset ( box, 0, sizeof ( *box ) );
|
|
47
|
+ box->string.buf = buf;
|
|
48
|
+ box->string.len = len;
|
|
49
|
+ box->string.cursor = strlen ( buf );
|
|
50
|
+ box->win = ( win ? win : stdscr );
|
|
51
|
+ box->row = row;
|
|
52
|
+ box->col = col;
|
|
53
|
+ box->width = width;
|
|
54
|
+}
|
|
55
|
+
|
|
56
|
+/**
|
|
57
|
+ * Draw text box widget
|
|
58
|
+ *
|
|
59
|
+ * @v box Editable text box widget
|
|
60
|
+ *
|
|
61
|
+ */
|
|
62
|
+void draw_editbox ( struct edit_box *box ) {
|
|
63
|
+ size_t width = box->width;
|
|
64
|
+ char buf[ width + 1 ];
|
|
65
|
+ size_t keep_len;
|
|
66
|
+ signed int cursor_offset, underflow, overflow;
|
|
67
|
+ size_t len;
|
|
68
|
+
|
|
69
|
+ /* Adjust starting offset so that cursor remains within box */
|
|
70
|
+ cursor_offset = ( box->string.cursor - box->first );
|
|
71
|
+ keep_len = strlen ( box->string.buf );
|
|
72
|
+ if ( keep_len > EDITBOX_MIN_CHARS )
|
|
73
|
+ keep_len = EDITBOX_MIN_CHARS;
|
|
74
|
+ underflow = ( keep_len - cursor_offset );
|
|
75
|
+ overflow = ( cursor_offset - ( width - 1 ) );
|
|
76
|
+ if ( underflow > 0 ) {
|
|
77
|
+ box->first -= underflow;
|
|
78
|
+ } else if ( overflow > 0 ) {
|
|
79
|
+ box->first += overflow;
|
|
80
|
+ }
|
|
81
|
+ cursor_offset = ( box->string.cursor - box->first );
|
|
82
|
+
|
|
83
|
+ /* Construct underscore-padded string portion */
|
|
84
|
+ memset ( buf, '_', width );
|
|
85
|
+ buf[width] = '\0';
|
|
86
|
+ len = ( strlen ( box->string.buf ) - box->first );
|
|
87
|
+ if ( len > width )
|
|
88
|
+ len = width;
|
|
89
|
+ memcpy ( buf, ( box->string.buf + box->first ), len );
|
|
90
|
+
|
|
91
|
+ /* Print box content and move cursor */
|
|
92
|
+ if ( ! box->win )
|
|
93
|
+ box->win = stdscr;
|
|
94
|
+ mvwprintw ( box->win, box->row, box->col, "%s", buf );
|
|
95
|
+ wmove ( box->win, box->row, ( box->col + cursor_offset ) );
|
|
96
|
+}
|
|
97
|
+
|
|
98
|
+/**
|
|
99
|
+ * Edit text box widget
|
|
100
|
+ *
|
|
101
|
+ * @v box Editable text box widget
|
|
102
|
+ * @v key Key pressed by user
|
|
103
|
+ * @ret key Key returned to application, or zero
|
|
104
|
+ *
|
|
105
|
+ */
|
|
106
|
+int edit_editbox ( struct edit_box *box, int key ) {
|
|
107
|
+
|
|
108
|
+ /* Update the string itself */
|
|
109
|
+ key = edit_string ( &box->string, key );
|
|
110
|
+
|
|
111
|
+ /* Update the display */
|
|
112
|
+ draw_editbox ( box );
|
|
113
|
+
|
|
114
|
+ return key;
|
|
115
|
+}
|