From c35a83655707c9aae8f728eb850255ad0f115d11 Mon Sep 17 00:00:00 2001
From: Marvin Borner
Date: Sat, 31 Oct 2020 21:50:43 +0100
Subject: Added 'exec' demo program

---
 libgui/gui.c     | 27 ++++++++++++++++++++-------
 libgui/inc/gui.h |  2 +-
 2 files changed, 21 insertions(+), 8 deletions(-)

(limited to 'libgui')

diff --git a/libgui/gui.c b/libgui/gui.c
index 399192f..675081e 100644
--- a/libgui/gui.c
+++ b/libgui/gui.c
@@ -2,9 +2,11 @@
 // Mostly GFX function wrappers
 // TODO: Reduce code duplication
 
+#include <assert.h>
 #include <def.h>
 #include <gfx.h>
 #include <gui.h>
+#include <input.h>
 #include <list.h>
 #include <mem.h>
 #include <print.h>
@@ -263,34 +265,45 @@ void gui_event_loop(struct element *container)
 			struct gui_event_mouse *event = msg->data;
 			focused = element_at(container, event->x, event->y);
 			if (focused && focused->event.on_click && event->but1)
-				focused->event.on_click(event);
+				focused->event.on_click(event, focused);
 			break;
 		}
 		case GUI_KEYBOARD: {
 			struct gui_event_keyboard *event = msg->data;
 
 			if (focused && focused->type == GUI_TYPE_TEXT_INPUT && event->press &&
-			    event->ch) {
+			    event->ch && event->ch >= ' ') {
 				char *s = ((struct element_text_input *)focused->data)->text;
 				u32 l = strlen(s);
+				if (l >= MAX_INPUT_LENGTH)
+					continue;
 				s[l] = event->ch;
 				s[l + 1] = '\0';
 				gui_sync_text_input(focused);
 				merge_elements(get_root(focused->window_id));
-				gfx_redraw_focused(); //  Only redraw window
+				gfx_redraw_focused();
 			}
 
-			if (focused && focused->event.on_key && event->ch) {
-				focused->event.on_key(event);
+			if (focused && focused->event.on_submit && event->press &&
+			    event->scancode == KEY_ENTER) {
+				focused->event.on_submit(event, focused);
+				// Clear!
+				((struct element_text_input *)focused->data)->text[0] = '\0';
+				gui_sync_text_input(focused);
+				merge_elements(get_root(focused->window_id));
+				gfx_redraw_focused();
 			}
 
+			if (focused && focused->event.on_key && event->press && event->ch)
+				focused->event.on_key(event, focused);
+
 			break;
 		}
 		}
 	}
 }
 
-struct element *gui_init(const char *title, u32 width, u32 height)
+struct element *gui_init(const char *title, u32 width, u32 height, u32 color_bg)
 {
 	if (window_count != 0)
 		return NULL;
@@ -300,7 +313,7 @@ struct element *gui_init(const char *title, u32 width, u32 height)
 	if (!win)
 		return NULL;
 
-	gfx_fill(win->ctx, COLOR_BG);
+	gfx_fill(win->ctx, color_bg);
 
 	struct element *container = malloc(sizeof(*container));
 	container->type = GUI_TYPE_ROOT;
diff --git a/libgui/inc/gui.h b/libgui/inc/gui.h
index 3f74178..8ed8f4e 100644
--- a/libgui/inc/gui.h
+++ b/libgui/inc/gui.h
@@ -86,7 +86,7 @@ struct gui_event_mouse {
 	int but3;
 };
 
-struct element *gui_init(const char *title, u32 width, u32 height);
+struct element *gui_init(const char *title, u32 width, u32 height, u32 color_bg);
 void gui_event_loop(struct element *container);
 struct element *gui_add_button(struct element *container, int x, int y, enum font_type font_type,
 			       char *text, u32 color_bg, u32 color_fg);
-- 
cgit v1.2.3