aboutsummaryrefslogtreecommitdiff
path: root/src/hci/mucurses/widgets
diff options
context:
space:
mode:
Diffstat (limited to 'src/hci/mucurses/widgets')
-rw-r--r--src/hci/mucurses/widgets/editbox.c65
-rw-r--r--src/hci/mucurses/widgets/label.c77
2 files changed, 109 insertions, 33 deletions
diff --git a/src/hci/mucurses/widgets/editbox.c b/src/hci/mucurses/widgets/editbox.c
index fddd274..79e7cdd 100644
--- a/src/hci/mucurses/widgets/editbox.c
+++ b/src/hci/mucurses/widgets/editbox.c
@@ -25,6 +25,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#include <string.h>
#include <assert.h>
+#include <ipxe/ansicol.h>
#include <ipxe/editbox.h>
/** @file
@@ -36,38 +37,15 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
#define EDITBOX_MIN_CHARS 3
/**
- * Initialise text box widget
- *
- * @v box Editable text box widget
- * @v buf Dynamically allocated string buffer
- * @v win Containing window
- * @v row Row
- * @v col Starting column
- * @v width Width
- * @v flags Flags
- */
-void init_editbox ( struct edit_box *box, char **buf,
- WINDOW *win, unsigned int row, unsigned int col,
- unsigned int width, unsigned int flags ) {
- memset ( box, 0, sizeof ( *box ) );
- init_editstring ( &box->string, buf );
- box->string.cursor = ( *buf ? strlen ( *buf ) : 0 );
- box->win = ( win ? win : stdscr );
- box->row = row;
- box->col = col;
- box->width = width;
- box->flags = flags;
-}
-
-/**
* Draw text box widget
*
- * @v box Editable text box widget
- *
+ * @v widgets Text widget set
+ * @v widget Text widget
*/
-void draw_editbox ( struct edit_box *box ) {
+static void draw_editbox ( struct widgets *widgets, struct widget *widget ) {
+ struct edit_box *box = container_of ( widget, struct edit_box, widget );
const char *content = *(box->string.buf);
- size_t width = box->width;
+ size_t width = widget->width;
char buf[ width + 1 ];
signed int cursor_offset, underflow, overflow, first;
size_t len;
@@ -93,15 +71,36 @@ void draw_editbox ( struct edit_box *box ) {
len = ( content ? ( strlen ( content ) - first ) : 0 );
if ( len > width )
len = width;
- if ( box->flags & EDITBOX_STARS ) {
+ if ( widget->flags & WIDGET_SECRET ) {
memset ( buf, '*', len );
} else {
memcpy ( buf, ( content + first ), len );
}
/* Print box content and move cursor */
- if ( ! box->win )
- box->win = stdscr;
- mvwprintw ( box->win, box->row, box->col, "%s", buf );
- wmove ( box->win, box->row, ( box->col + cursor_offset ) );
+ color_set ( CPAIR_EDIT, NULL );
+ mvwprintw ( widgets->win, widget->row, widget->col, "%s", buf );
+ wmove ( widgets->win, widget->row, ( widget->col + cursor_offset ) );
+ color_set ( CPAIR_NORMAL, NULL );
}
+
+/**
+ * Edit text box widget
+ *
+ * @v widgets Text widget set
+ * @v widget Text widget
+ * @v key Key pressed by user
+ * @ret key Key returned to application, or zero
+ */
+static int edit_editbox ( struct widgets *widgets __unused,
+ struct widget *widget, int key ) {
+ struct edit_box *box = container_of ( widget, struct edit_box, widget );
+
+ return edit_string ( &box->string, key );
+}
+
+/** Text box widget operations */
+struct widget_operations editbox_operations = {
+ .draw = draw_editbox,
+ .edit = edit_editbox,
+};
diff --git a/src/hci/mucurses/widgets/label.c b/src/hci/mucurses/widgets/label.c
new file mode 100644
index 0000000..29645a6
--- /dev/null
+++ b/src/hci/mucurses/widgets/label.c
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2024 Michael Brown <mbrown@fensystems.co.uk>.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or any later version.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ *
+ * You can also choose to distribute this program under the terms of
+ * the Unmodified Binary Distribution Licence (as given in the file
+ * COPYING.UBDL), provided that you have satisfied its requirements.
+ */
+
+FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
+
+#include <string.h>
+#include <assert.h>
+#include <ipxe/label.h>
+
+/** @file
+ *
+ * Text label widget
+ *
+ */
+
+/**
+ * Draw text label widget
+ *
+ * @v widgets Text widget set
+ * @v widget Text widget
+ */
+static void draw_label ( struct widgets *widgets, struct widget *widget ) {
+ struct label *label = container_of ( widget, struct label, widget );
+ unsigned int width = widget->width;
+ unsigned int col = widget->col;
+ const char *text = label->text;
+
+ /* Centre label if width is non-zero */
+ if ( width )
+ col += ( ( width - strlen ( text ) ) / 2 );
+
+ /* Print label content */
+ attron ( A_BOLD );
+ mvwprintw ( widgets->win, widget->row, col, "%s", text );
+ attroff ( A_BOLD );
+}
+
+/**
+ * Edit text label widget
+ *
+ * @v widgets Text widget set
+ * @v widget Text widget
+ * @v key Key pressed by user
+ * @ret key Key returned to application, or zero
+ */
+static int edit_label ( struct widgets *widgets __unused,
+ struct widget *widget __unused, int key ) {
+
+ /* Cannot be edited */
+ return key;
+}
+
+/** Text label widget operations */
+struct widget_operations label_operations = {
+ .draw = draw_label,
+ .edit = edit_label,
+};