From d309eb8633be1d9f08d1e9cd9930d68d8b169367 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Sun, 8 Sep 2019 17:25:56 +0300 Subject: [PATCH] Make the entry widget cursor blink If the cursor part has different graphics for normal and active states, it will toggle between those every half a second while the widget is focused. --- source/entry.cpp | 38 ++++++++++++++++++++++++++++++++++++-- source/entry.h | 4 ++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/source/entry.cpp b/source/entry.cpp index 8d3c47f..87f88c7 100644 --- a/source/entry.cpp +++ b/source/entry.cpp @@ -23,7 +23,8 @@ Entry::Entry(const string &t): visible_rows(1), text_part(0), slider(0), - got_key_press(false) + got_key_press(false), + cursor_blink(true) { input_type = INPUT_TEXT; set_text(t); @@ -92,7 +93,8 @@ void Entry::rebuild_special(const Part &part) text.build(part, state, geom, first_row, part_cache); else if(part.get_name()=="cursor") { - const Graphic *graphic = part.get_graphic(state); + State cursor_state = (cursor_blink ? ACTIVE : NORMAL); + const Graphic *graphic = part.get_graphic(state|cursor_state); if(!text_part || !graphic || !graphic->get_texture()) return; @@ -170,10 +172,18 @@ bool Entry::character(wchar_t ch) return false; } +void Entry::focus_in() +{ + cursor_blink = true; + Widget::focus_in(); + check_cursor_blink(); +} + void Entry::focus_out() { Widget::focus_out(); got_key_press = false; + check_cursor_blink(); } bool Entry::navigate(Navigation nav) @@ -208,6 +218,12 @@ bool Entry::navigate(Navigation nav) return true; } +void Entry::animate(const Time::TimeDelta &) +{ + cursor_blink = !cursor_blink; + rebuild(); +} + void Entry::on_geometry_change() { if(multiline) @@ -228,6 +244,8 @@ void Entry::on_style_change() if(multiline) check_view_range(); + + check_cursor_blink(); } void Entry::set_edit_position(unsigned ep) @@ -237,6 +255,22 @@ void Entry::set_edit_position(unsigned ep) rebuild(); } +void Entry::check_cursor_blink() +{ + cursor_blink = (state&FOCUS); + if((state&FOCUS) && style) + { + const Part *cursor_part = style->get_part("cursor"); + if(cursor_part && cursor_part->get_graphic(ACTIVE|FOCUS)!=cursor_part->get_graphic(NORMAL|FOCUS)) + { + set_animation_interval(Time::sec/2); + return; + } + } + + stop_animation(); +} + void Entry::check_view_range() { if(!multiline || !text_part) diff --git a/source/entry.h b/source/entry.h index a3b1607..cd80ab0 100644 --- a/source/entry.h +++ b/source/entry.h @@ -45,6 +45,7 @@ private: const Part *text_part; VSlider *slider; bool got_key_press; + bool cursor_blink; public: Entry(const std::string & = std::string()); @@ -73,13 +74,16 @@ public: virtual void touch_press(int, int, unsigned); virtual bool key_press(unsigned, unsigned); virtual bool character(wchar_t); + virtual void focus_in(); virtual void focus_out(); virtual bool navigate(Navigation); + virtual void animate(const Time::TimeDelta &); private: virtual void on_geometry_change(); virtual void on_style_change(); void set_edit_position(unsigned); + void check_cursor_blink(); void check_view_range(); void slider_value_changed(double); }; -- 2.45.2