From abee16b16efcdfa27513aec1bbc8c9630b1bbef4 Mon Sep 17 00:00:00 2001 From: Mark de Wever Date: Thu, 29 May 2014 10:57:51 +0200 Subject: [PATCH] Convert the GUI2 text API to take strings. This change makes it easier to use the SDL2 text input events. --- src/gui/auxiliary/event/dispatcher.cpp | 6 +++--- src/gui/auxiliary/event/dispatcher.hpp | 6 ++++-- src/gui/auxiliary/event/distributor.cpp | 2 +- src/gui/auxiliary/event/distributor.hpp | 2 +- src/gui/auxiliary/event/handler.cpp | 13 ++++++++----- src/gui/widgets/password_box.cpp | 2 +- src/gui/widgets/password_box.hpp | 2 +- src/gui/widgets/text.cpp | 18 +++++++++--------- src/gui/widgets/text.hpp | 6 +++--- src/gui/widgets/text_box.cpp | 2 +- src/gui/widgets/text_box.hpp | 2 +- 11 files changed, 33 insertions(+), 28 deletions(-) diff --git a/src/gui/auxiliary/event/dispatcher.cpp b/src/gui/auxiliary/event/dispatcher.cpp index 6a2ef8f75a3..156e3dc1aee 100644 --- a/src/gui/auxiliary/event/dispatcher.cpp +++ b/src/gui/auxiliary/event/dispatcher.cpp @@ -212,7 +212,7 @@ class ttrigger_keyboard public: ttrigger_keyboard(const SDLKey key, const SDLMod modifier, - const Uint16 unicode) + const utf8::string& unicode) : key_(key), modifier_(modifier), unicode_(unicode) { } @@ -229,14 +229,14 @@ public: private: SDLKey key_; SDLMod modifier_; - Uint16 unicode_; + utf8::string unicode_; }; bool tdispatcher::fire(const tevent event, twidget& target, const SDLKey key, const SDLMod modifier, - const Uint16 unicode) + const utf8::string& unicode) { assert(find(event, tevent_in_set())); return fire_event( diff --git a/src/gui/auxiliary/event/dispatcher.hpp b/src/gui/auxiliary/event/dispatcher.hpp index 68d55494288..1ac204ad390 100644 --- a/src/gui/auxiliary/event/dispatcher.hpp +++ b/src/gui/auxiliary/event/dispatcher.hpp @@ -18,6 +18,7 @@ #include "gui/auxiliary/event/handler.hpp" #include "hotkey/hotkey_command.hpp" #include "sdl/compat.hpp" +#include "serialization/unicode.hpp" #include #include @@ -70,7 +71,8 @@ typedef boost::function tsignal_keyboard_function; + const utf8::string& unicode)> +tsignal_keyboard_function; /** * Callback function signature. @@ -181,7 +183,7 @@ public: twidget& target, const SDLKey key, const SDLMod modifier, - const Uint16 unicode); + const utf8::string& unicode); /** * Fires an event which takes notification parameters. diff --git a/src/gui/auxiliary/event/distributor.cpp b/src/gui/auxiliary/event/distributor.cpp index f8deae4b187..b89ce17da87 100644 --- a/src/gui/auxiliary/event/distributor.cpp +++ b/src/gui/auxiliary/event/distributor.cpp @@ -692,7 +692,7 @@ void tdistributor::keyboard_remove_from_chain(twidget* widget) void tdistributor::signal_handler_sdl_key_down(const SDLKey key, const SDLMod modifier, - const Uint16 unicode) + const utf8::string& unicode) { /** @todo Test whether recursion protection is needed. */ diff --git a/src/gui/auxiliary/event/distributor.hpp b/src/gui/auxiliary/event/distributor.hpp index 6f4f7fd482a..d10c59c4673 100644 --- a/src/gui/auxiliary/event/distributor.hpp +++ b/src/gui/auxiliary/event/distributor.hpp @@ -308,7 +308,7 @@ private: void signal_handler_sdl_key_down(const SDLKey key, const SDLMod modifier, - const Uint16 unicode); + const utf8::string& unicode); void signal_handler_notify_removal(tdispatcher& widget, const tevent event); }; diff --git a/src/gui/auxiliary/event/handler.cpp b/src/gui/auxiliary/event/handler.cpp index 61102cf10e8..b5999646125 100644 --- a/src/gui/auxiliary/event/handler.cpp +++ b/src/gui/auxiliary/event/handler.cpp @@ -254,8 +254,9 @@ private: * @param modifier The SDL key modifiers used. * @param unicode The unicode value for the key pressed. */ - void - key_down(const SDLKey key, const SDLMod modifier, const Uint16 unicode); + void key_down(const SDLKey key, + const SDLMod modifier, + const utf8::string& unicode); /** * Fires a keyboard event which has no parameters. @@ -714,9 +715,11 @@ void thandler::key_down(const SDL_KeyboardEvent& event) #if SDL_VERSION_ATLEAST(2, 0, 0) key_down(event.keysym.sym, static_cast(event.keysym.mod), - 0); + ""); #else - key_down(event.keysym.sym, event.keysym.mod, event.keysym.unicode); + key_down(event.keysym.sym, + event.keysym.mod, + ::implementation::ucs4char_to_string(event.keysym.unicode)); #endif } } @@ -734,7 +737,7 @@ bool thandler::hotkey_pressed(const hotkey::hotkey_item& key) void thandler::key_down(const SDLKey key, const SDLMod modifier, - const Uint16 unicode) + const utf8::string& unicode) { DBG_GUI_E << "Firing: " << SDL_KEY_DOWN << ".\n"; diff --git a/src/gui/widgets/password_box.cpp b/src/gui/widgets/password_box.cpp index ba6c73dec90..c3b1f460b51 100644 --- a/src/gui/widgets/password_box.cpp +++ b/src/gui/widgets/password_box.cpp @@ -52,7 +52,7 @@ void tpassword_box::set_value(const std::string& text) ttext_box::set_value(std::string(get_text_length(real_value_), '*')); } -void tpassword_box::insert_char(const Uint16 unicode) +void tpassword_box::insert_char(const utf8::string& unicode) { pre_function(); ttext_box::insert_char(unicode); diff --git a/src/gui/widgets/password_box.hpp b/src/gui/widgets/password_box.hpp index 643b919ca96..db44f89b2e4 100644 --- a/src/gui/widgets/password_box.hpp +++ b/src/gui/widgets/password_box.hpp @@ -65,7 +65,7 @@ public: protected: - void insert_char(const Uint16 unicode); + void insert_char(const utf8::string& unicode); void delete_char(const bool before_cursor); void paste_selection(const bool mouse); diff --git a/src/gui/widgets/text.cpp b/src/gui/widgets/text.cpp index ec810757da2..42635d47575 100644 --- a/src/gui/widgets/text.cpp +++ b/src/gui/widgets/text.cpp @@ -127,11 +127,11 @@ void ttext_::set_cursor(const size_t offset, const bool select) } } -void ttext_::insert_char(const Uint16 unicode) +void ttext_::insert_char(const utf8::string& unicode) { delete_selection(); - if(text_.insert_unicode(selection_start_, unicode)) { + if(text_.insert_text(selection_start_, unicode)) { // Update status set_cursor(selection_start_ + 1, false); @@ -277,15 +277,15 @@ void ttext_::handle_key_delete(SDLMod /*modifier*/, bool& handled) void ttext_::handle_key_default(bool& handled, SDLKey /*key*/, SDLMod /*modifier*/, - Uint16 unicode) + const utf8::string& unicode) { DBG_GUI_E << LOG_SCOPE_HEADER << '\n'; - if(unicode >= 32 && unicode != 127) { - handled = true; - insert_char(unicode); - fire(event::NOTIFY_MODIFIED, *this, NULL); - } + // if(unicode >= 32 && unicode != 127) { + handled = true; + insert_char(unicode); + fire(event::NOTIFY_MODIFIED, *this, NULL); + // } } void ttext_::signal_handler_middle_button_click(const event::tevent event, @@ -302,7 +302,7 @@ void ttext_::signal_handler_sdl_key_down(const event::tevent event, bool& handled, const SDLKey key, SDLMod modifier, - const Uint16 unicode) + const utf8::string& unicode) { DBG_GUI_E << LOG_HEADER << ' ' << event << ".\n"; diff --git a/src/gui/widgets/text.hpp b/src/gui/widgets/text.hpp index 668a2b5e51e..28390b1a8b2 100644 --- a/src/gui/widgets/text.hpp +++ b/src/gui/widgets/text.hpp @@ -155,7 +155,7 @@ protected: * * @param unicode The unicode value of the character to insert. */ - virtual void insert_char(const Uint16 unicode); + virtual void insert_char(const utf8::string& unicode); /** * Deletes the character. @@ -423,7 +423,7 @@ protected: virtual void handle_key_default(bool& handled, SDLKey key, SDLMod modifier, - Uint16 unicode); + const utf8::string& unicode); private: /** @@ -446,7 +446,7 @@ private: bool& handled, const SDLKey key, SDLMod modifier, - const Uint16 unicode); + const utf8::string& unicode); void signal_handler_receive_keyboard_focus(const event::tevent event); void signal_handler_lose_keyboard_focus(const event::tevent event); diff --git a/src/gui/widgets/text_box.cpp b/src/gui/widgets/text_box.cpp index 01fbe940598..348a420b5f5 100644 --- a/src/gui/widgets/text_box.cpp +++ b/src/gui/widgets/text_box.cpp @@ -298,7 +298,7 @@ bool ttext_box::history_down() void ttext_box::handle_key_default(bool& handled, SDLKey key, SDLMod modifier, - Uint16 unicode) + const utf8::string& unicode) { if(key == SDLK_TAB && (modifier & KMOD_CTRL)) { if(!(modifier & KMOD_SHIFT)) { diff --git a/src/gui/widgets/text_box.hpp b/src/gui/widgets/text_box.hpp index 5be576b3e1a..c67b32995c2 100644 --- a/src/gui/widgets/text_box.hpp +++ b/src/gui/widgets/text_box.hpp @@ -236,7 +236,7 @@ private: void handle_key_default(bool& handled, SDLKey key, SDLMod modifier, - Uint16 unicode); + const utf8::string& unicode); /** Inherited from ttext_. */ void handle_key_clear_line(SDLMod modifier, bool& handled);