Ver código fonte

Editable text box widget based on mucurses and edit_string.

tags/v0.9.3
Michael Brown 18 anos atrás
pai
commit
35dcbfe771
2 arquivos alterados com 150 adições e 0 exclusões
  1. 115
    0
      src/hci/mucurses/widgets/editbox.c
  2. 35
    0
      src/include/gpxe/editbox.h

+ 115
- 0
src/hci/mucurses/widgets/editbox.c Ver arquivo

@@ -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
+}

+ 35
- 0
src/include/gpxe/editbox.h Ver arquivo

@@ -0,0 +1,35 @@
1
+#ifndef _GPXE_EDITBOX_H
2
+#define _GPXE_EDITBOX_H
3
+
4
+/** @file
5
+ *
6
+ * Editable text box widget
7
+ *
8
+ */
9
+
10
+#include <curses.h>
11
+#include <gpxe/editstring.h>
12
+
13
+/** An editable text box widget */
14
+struct edit_box {
15
+	/** Editable string */
16
+	struct edit_string string;
17
+	/** Containing window */
18
+	WINDOW *win;
19
+	/** Row */
20
+	unsigned int row;
21
+	/** Starting column */
22
+	unsigned int col;
23
+	/** Width */
24
+	unsigned int width;
25
+	/** First displayed character */
26
+	unsigned int first;
27
+};
28
+
29
+extern void init_editbox ( struct edit_box *box, char *buf, size_t len,
30
+			   WINDOW *win, unsigned int row, unsigned int col,
31
+			   unsigned int width );
32
+extern void draw_editbox ( struct edit_box *box );
33
+extern int edit_editbox ( struct edit_box *box, int key );
34
+
35
+#endif /* _GPXE_EDITBOX_H */

Carregando…
Cancelar
Salvar