diff options
author | Keith Packard <keithp@keithp.com> | 2012-03-01 14:00:33 -0800 |
---|---|---|
committer | Keith Packard <keithp@keithp.com> | 2012-03-01 14:00:33 -0800 |
commit | 4c267cd0ce8323ecfdcfe77d3c67e4b0a6249f1b (patch) | |
tree | bd2d1a89f16fd9070097a7df221876471e48eb67 | |
parent | 8826159c198574c1fb285df45800326fbdee62d0 (diff) |
Add key callback to text widget
Allows application to pre-screen keys headed for the text box
Signed-off-by: Keith Packard <keithp@keithp.com>
-rw-r--r-- | nichrome/nichrome-text.5c | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/nichrome/nichrome-text.5c b/nichrome/nichrome-text.5c index f473034..968610e 100644 --- a/nichrome/nichrome-text.5c +++ b/nichrome/nichrome-text.5c @@ -37,7 +37,7 @@ autoload Nichrome; extend namespace Nichrome { public namespace Text { - public typedef void (&widget_t widget) callback_t; + protected typedef bool (&widget_t widget, string key) callback_t; public typedef widget_t + struct { string text; @@ -47,6 +47,7 @@ extend namespace Nichrome { rgba_color_t color; bool clicked; real click_x; + callback_t callback; } text_t; typedef struct { @@ -191,6 +192,10 @@ extend namespace Nichrome { protected void key(&text_t widget, &key_event_t event) { if (event.type != key_type_t.press) return; + if (!is_uninit(&widget.callback)) + if (widget.callback(&widget, event.key)) + return; + switch (event.key) { case "Left": caret (&widget, widget.caret - 1); @@ -201,6 +206,8 @@ extend namespace Nichrome { case "BackSpace": backspace(&widget, 1); break; + case "Return": + break; default: for (int i = 0; i < String::length(event.text); i++) { @@ -227,6 +234,12 @@ extend namespace Nichrome { public void set_text(&text_t widget, string new_text) { widget.text = new_text; Widget::redraw(&widget); + widget.caret = 0; + } + + public void set_caret(&text_t widget, int caret) { + if (caret < 0) caret = 0; + widget.caret = min (caret, String::length(widget.text)); } public void init (&text_t widget, &nichrome_t nichrome, int num_chars) { |