From 85df1646ecccc7e484e2efc903166b89e4c55684 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Sat, 1 Jul 2017 20:10:14 +0200 Subject: fixed get_focus error in widget (called the wrong parent) the text widget reacts to mouse down and key events and appends text to the output buffer so we have text input in GUI mode --- src/gui/desktop.c | 12 +++++++++++- src/gui/desktop.h | 1 + src/gui/text_widget.c | 12 ++++++++++-- src/gui/text_widget.h | 2 +- src/gui/widget.c | 2 +- 5 files changed, 24 insertions(+), 5 deletions(-) (limited to 'src/gui') diff --git a/src/gui/desktop.c b/src/gui/desktop.c index ad4aad9..4a3876e 100644 --- a/src/gui/desktop.c +++ b/src/gui/desktop.c @@ -13,7 +13,7 @@ static desktop_vtable_t desktop_vtable = { desktop_on_mouse_down, desktop_on_mouse_up, desktop_on_mouse_move, - composite_widget_on_key_down, + desktop_on_key_down, composite_widget_on_key_up }, composite_widget_add_child @@ -94,3 +94,13 @@ void desktop_on_mouse_move( void *obj, const int old_x, const int old_y, const i desktop->needs_redrawing = true; } + +void desktop_on_key_down( void *obj, char c ) +{ + desktop_t *desktop = obj; + + composite_widget_on_key_down( obj, c ); + + desktop->needs_redrawing = true; +} + diff --git a/src/gui/desktop.h b/src/gui/desktop.h index a9d1946..f644691 100644 --- a/src/gui/desktop.h +++ b/src/gui/desktop.h @@ -25,5 +25,6 @@ void desktop_draw( void *obj, graphics_context_t *context ); void desktop_on_mouse_down( void *obj, const int x, const int y ); void desktop_on_mouse_up( void *obj, const int x, const int y ); void desktop_on_mouse_move( void *obj, const int old_x, const int old_y, const int x, const int y ); +void desktop_on_key_down( void *obj, char c ); #endif // DESKTOP_H diff --git a/src/gui/text_widget.c b/src/gui/text_widget.c index dc12c11..f6ba8eb 100644 --- a/src/gui/text_widget.c +++ b/src/gui/text_widget.c @@ -12,8 +12,8 @@ static text_widget_vtable_t text_widget_vtable = { widget_on_mouse_down, widget_on_mouse_up, widget_on_mouse_move, - widget_on_key_down, - widget_on_key_up + text_widget_on_key_down, + text_widget_on_key_up }, text_widget_set_text }; @@ -25,6 +25,8 @@ void text_widget_init( text_widget_t *widget, widget_t *parent, const int x, con widget_init( &widget->base, parent, x, y, w, h, background_color ); text_widget_set_text( widget, s ); + + widget->base.focusable = true; widget->base.vtable = (widget_vtable_t *)&text_widget_vtable; widget->vtable = &text_widget_vtable; @@ -64,6 +66,12 @@ void text_widget_set_text( void *obj, const char *s ) void text_widget_on_key_down( void *obj, char c ) { + text_widget_t *widget = obj; + + if( strlen( widget->s ) < TEXT_WIDGET_MAX_TEXT_SIZE - 1 ) { + widget->s[strlen( widget->s )] = c; + widget->s[strlen( widget->s )+1] = '\0'; + } } void text_widget_on_key_up( void *obj, char c ) diff --git a/src/gui/text_widget.h b/src/gui/text_widget.h index 4919e26..3b22cf1 100644 --- a/src/gui/text_widget.h +++ b/src/gui/text_widget.h @@ -3,7 +3,7 @@ #include "widget.h" -#define TEXT_WIDGET_MAX_TEXT_SIZE 100 +#define TEXT_WIDGET_MAX_TEXT_SIZE 300 typedef struct { widget_vtable_t base; diff --git a/src/gui/widget.c b/src/gui/widget.c index 1c26680..81ab264 100644 --- a/src/gui/widget.c +++ b/src/gui/widget.c @@ -46,7 +46,7 @@ void widget_get_focus( void *obj, widget_t *widget ) widget_t *o = obj; if( o->parent != NULL ) { - o->parent->vtable->get_focus( widget->parent, widget ); + o->parent->vtable->get_focus( o->parent, widget ); } } -- cgit v1.2.3-54-g00ecf