Merge branch 'SDL2_GUI2_input'

Enable SDL2 text input in the GUI2 widgets.
This commit is contained in:
Mark de Wever 2014-05-31 22:28:48 +02:00
commit 6deda4ca7f
29 changed files with 151 additions and 124 deletions

View file

@ -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<tset_event_keyboard>(event, tevent_in_set()));
return fire_event<tsignal_keyboard_function>(

View file

@ -18,6 +18,7 @@
#include "gui/auxiliary/event/handler.hpp"
#include "hotkey/hotkey_command.hpp"
#include "sdl/compat.hpp"
#include "serialization/unicode.hpp"
#include <boost/function.hpp>
#include <boost/mpl/int.hpp>
@ -70,7 +71,8 @@ typedef boost::function<void(tdispatcher& dispatcher,
bool& halt,
const SDLKey key,
const SDLMod modifier,
const Uint16 unicode)> 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.

View file

@ -345,7 +345,8 @@ void tmouse_motion::stop_hover_timer()
<< ".\n";
if(!remove_timer(hover_timer_)) {
ERR_GUI_E << LOG_HEADER << "Failed to remove hover timer." << std::endl;
ERR_GUI_E << LOG_HEADER << "Failed to remove hover timer."
<< std::endl;
}
hover_timer_ = 0;
@ -691,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. */

View file

@ -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);
};

View file

@ -254,8 +254,16 @@ 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 text input event.
*
* @param unicode The unicode value for the text entered.
*/
void text_input(const std::string& unicode);
/**
* Fires a keyboard event which has no parameters.
@ -331,7 +339,7 @@ void thandler::handle_event(const SDL_Event& event)
event.button.button);
break;
#if SDL_VERSION_ATLEAST(2,0,0)
#if SDL_VERSION_ATLEAST(2, 0, 0)
case SDL_MOUSEWHEEL:
mouse_wheel(get_mouse_position(), event.wheel.x, event.wheel.y);
break;
@ -388,17 +396,22 @@ void thandler::handle_event(const SDL_Event& event)
draw(true);
break;
case SDL_WINDOWEVENT_RESIZED:
video_resize(tpoint(event.window.data1, event.window.data2));
break;
case SDL_WINDOWEVENT_RESIZED:
video_resize(
tpoint(event.window.data1, event.window.data2));
break;
case SDL_WINDOWEVENT_ENTER:
case SDL_WINDOWEVENT_FOCUS_GAINED:
activate();
break;
case SDL_WINDOWEVENT_ENTER:
case SDL_WINDOWEVENT_FOCUS_GAINED:
activate();
break;
}
break;
case SDL_TEXTINPUT:
text_input(event.text.text);
break;
#else
case SDL_VIDEOEXPOSE:
draw(true);
@ -590,7 +603,7 @@ void thandler::mouse_button_up(const tpoint& position, const Uint8 button)
mouse(SDL_RIGHT_BUTTON_UP, position);
break;
#if !SDL_VERSION_ATLEAST(2,0,0)
#if !SDL_VERSION_ATLEAST(2, 0, 0)
case SDL_BUTTON_WHEELLEFT:
mouse(SDL_WHEEL_LEFT, get_mouse_position());
break;
@ -640,18 +653,18 @@ void thandler::mouse_button_down(const tpoint& position, const Uint8 button)
}
}
#if SDL_VERSION_ATLEAST(2,0,0)
#if SDL_VERSION_ATLEAST(2, 0, 0)
void thandler::mouse_wheel(const tpoint& position, int x, int y)
{
if (x > 0) {
if(x > 0) {
mouse(SDL_WHEEL_RIGHT, position);
} else if (x < 0) {
} else if(x < 0) {
mouse(SDL_WHEEL_LEFT, position);
}
if (y < 0) {
if(y < 0) {
mouse(SDL_WHEEL_DOWN, position);
} else if (y > 0){
} else if(y > 0) {
mouse(SDL_WHEEL_UP, position);
}
}
@ -711,16 +724,22 @@ void thandler::key_down(const SDL_KeyboardEvent& event)
}
if(!done) {
#if SDL_VERSION_ATLEAST(2, 0, 0)
key_down(
event.keysym.sym
, static_cast<const SDL_Keymod>(event.keysym.mod)
, static_cast<const Uint16>(event.keysym.scancode));
key_down(event.keysym.sym,
static_cast<const SDL_Keymod>(event.keysym.mod),
"");
#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
}
}
void thandler::text_input(const std::string& unicode)
{
key_down(static_cast<SDLKey>(0), static_cast<SDLMod>(0), unicode);
}
bool thandler::hotkey_pressed(const hotkey::hotkey_item& key)
{
tdispatcher* dispatcher = keyboard_dispatcher();
@ -734,7 +753,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";

View file

@ -80,7 +80,8 @@ public:
bool next()
{
if(at_end()) {
ERR_GUI_I << "Tried to move beyond end of the iteration range." << std::endl;
ERR_GUI_I << "Tried to move beyond end of the iteration range."
<< std::endl;
throw trange_error("Tried to move beyond end of range.");
}
@ -230,7 +231,8 @@ public:
bool next()
{
if(at_end()) {
ERR_GUI_I << "Tried to move beyond end of the iteration range." << std::endl;
ERR_GUI_I << "Tried to move beyond end of the iteration range."
<< std::endl;
throw trange_error("Tried to move beyond end of range.");
}

View file

@ -263,7 +263,9 @@ void tcampaign_selection::post_show(twindow& window)
} else {
choice_ = find_widget<tlistbox>(&window, "campaign_list", false)
.get_selected_row();
deterministic_ = find_widget<ttoggle_button>(&window,"checkbox_deterministic", false).get_value();
deterministic_ = find_widget<ttoggle_button>(&window,
"checkbox_deterministic",
false).get_value();
}
}

View file

@ -76,8 +76,7 @@ REGISTER_DIALOG(core_selection)
void tcore_selection::core_selected(twindow& window)
{
if(new_widgets && false) {
ttree_view& tree
= find_widget<ttree_view>(&window, "core_tree", false);
ttree_view& tree = find_widget<ttree_view>(&window, "core_tree", false);
if(tree.empty()) {
return;
@ -106,13 +105,10 @@ void tcore_selection::pre_show(CVideo& /*video*/, twindow& window)
{
if(new_widgets && false) {
/***** Setup core tree. *****/
ttree_view& tree
= find_widget<ttree_view>(&window, "core_tree", false);
ttree_view& tree = find_widget<ttree_view>(&window, "core_tree", false);
tree.set_selection_change_callback(
boost::bind(&tcore_selection::core_selected,
this,
boost::ref(window)));
tree.set_selection_change_callback(boost::bind(
&tcore_selection::core_selected, this, boost::ref(window)));
window.keyboard_capture(&tree);
@ -125,7 +121,7 @@ void tcore_selection::pre_show(CVideo& /*video*/, twindow& window)
tmulti_page& multi_page
= find_widget<tmulti_page>(&window, "core_details", false);
// unsigned id = 0;
// unsigned id = 0;
FOREACH(const AUTO & core, cores_)
{
@ -216,8 +212,7 @@ void tcore_selection::pre_show(CVideo& /*video*/, twindow& window)
void tcore_selection::post_show(twindow& window)
{
if(new_widgets && false) {
ttree_view& tree
= find_widget<ttree_view>(&window, "core_tree", false);
ttree_view& tree = find_widget<ttree_view>(&window, "core_tree", false);
if(tree.empty()) {
return;

View file

@ -270,22 +270,24 @@ void tgame_load::evaluate_summary_string(std::stringstream& str,
{
const std::string& campaign_type = cfg_summary["campaign_type"];
if (cfg_summary["corrupt"].to_bool()) {
if(cfg_summary["corrupt"].to_bool()) {
str << "\n" << _("#(Invalid)");
} else {
str << "\n";
try {
game_classification::CAMPAIGN_TYPE ct = lexical_cast<game_classification::CAMPAIGN_TYPE> (campaign_type);
try
{
game_classification::CAMPAIGN_TYPE ct
= lexical_cast<game_classification::CAMPAIGN_TYPE>(
campaign_type);
switch (ct) {
case game_classification::SCENARIO:
{
switch(ct) {
case game_classification::SCENARIO: {
const std::string campaign_id = cfg_summary["campaign"];
const config* campaign = NULL;
if(!campaign_id.empty()) {
if(const config& c
= cache_config_.find_child("campaign", "id", campaign_id)) {
if(const config& c = cache_config_.find_child(
"campaign", "id", campaign_id)) {
campaign = &c;
}
@ -314,8 +316,10 @@ void tgame_load::evaluate_summary_string(std::stringstream& str,
case game_classification::TEST:
str << _("Test scenario");
break;
}
} catch (bad_lexical_cast &) {
}
}
catch(bad_lexical_cast&)
{
str << campaign_type;
}

View file

@ -1629,12 +1629,14 @@ bool tlobby_main::do_game_join(int idx, bool observe)
const game_info& game = *lobby_info_.games()[idx];
if(observe) {
if(!game.can_observe()) {
ERR_LB << "Requested observe of a game with observers disabled" << std::endl;
ERR_LB << "Requested observe of a game with observers disabled"
<< std::endl;
return false;
}
} else {
if(!game.can_join()) {
ERR_LB << "Requested join to a game with no vacant slots" << std::endl;
ERR_LB << "Requested join to a game with no vacant slots"
<< std::endl;
return false;
}
}

View file

@ -177,7 +177,8 @@ public:
for(std::vector<team>::const_iterator it = resources::teams->begin();
it != resources::teams->end();
++it) {
if(!it->is_ai() && !it->is_network_ai() && !it->is_idle() && !it->is_empty() && !it->current_player().empty())
if(!it->is_ai() && !it->is_network_ai() && !it->is_idle()
&& !it->is_empty() && !it->current_player().empty())
nicks.insert(it->current_player());
}

View file

@ -29,7 +29,8 @@
#include "gettext.hpp"
namespace gui2 {
namespace gui2
{
/*WIKI
* @page = GUIWindowDefinitionWML
@ -64,8 +65,7 @@ REGISTER_DIALOG(screenshot_notification)
tscreenshot_notification::tscreenshot_notification(const std::string& path,
int filesize)
: path_(path)
, screenshots_dir_path_(get_screenshot_dir())
: path_(path), screenshots_dir_path_(get_screenshot_dir())
{
register_label("filesize",
false,
@ -80,20 +80,17 @@ void tscreenshot_notification::pre_show(CVideo& /*video*/, twindow& window)
path_box.set_active(false);
tbutton& copy_b = find_widget<tbutton>(&window, "copy", false);
connect_signal_mouse_left_click(copy_b,
boost::bind(&copy_to_clipboard,
boost::ref(path_),
false));
connect_signal_mouse_left_click(
copy_b, boost::bind(&copy_to_clipboard, boost::ref(path_), false));
tbutton& open_b = find_widget<tbutton>(&window, "open", false);
connect_signal_mouse_left_click(open_b,
boost::bind(&desktop::open_object,
boost::ref(path_)));
connect_signal_mouse_left_click(
open_b, boost::bind(&desktop::open_object, boost::ref(path_)));
tbutton& bdir_b = find_widget<tbutton>(&window, "browse_dir", false);
connect_signal_mouse_left_click(bdir_b,
boost::bind(&desktop::open_object,
boost::ref(screenshots_dir_path_)));
connect_signal_mouse_left_click(
bdir_b,
boost::bind(&desktop::open_object,
boost::ref(screenshots_dir_path_)));
}
}

View file

@ -17,7 +17,8 @@
#include "gui/dialogs/dialog.hpp"
namespace gui2 {
namespace gui2
{
class tscreenshot_notification : public tdialog
{
@ -50,7 +51,6 @@ private:
/** Inherited from tdialog. */
void pre_show(CVideo& video, twindow& window);
};
}
#endif /* ! GUI_DIALOGS_SCREENSHOT_NOTIFICATION_HPP_INCLUDED */

View file

@ -52,10 +52,8 @@ namespace gui2
REGISTER_DIALOG(theme_list)
ttheme_list::ttheme_list(const std::vector<theme_info>& themes,
int selection)
: index_(selection)
, themes_(themes)
ttheme_list::ttheme_list(const std::vector<theme_info>& themes, int selection)
: index_(selection), themes_(themes)
{
}
@ -98,5 +96,4 @@ void ttheme_list::post_show(twindow& window)
tlistbox& list = find_widget<tlistbox>(&window, "themes", false);
index_ = list.get_selected_row();
}
}

View file

@ -57,7 +57,6 @@ private:
/** Inherited from tdialog. */
void post_show(twindow& window);
};
}
#endif

View file

@ -158,7 +158,8 @@ void show(CVideo& video,
}
catch(twindow_builder_invalid_id&)
{
ERR_CFG << "Default tooltip doesn't exist, no message shown." << std::endl;
ERR_CFG << "Default tooltip doesn't exist, no message shown."
<< std::endl;
}
}
}

View file

@ -245,9 +245,8 @@ void ttitle_screen::post_build(CVideo& video, twindow& window)
hotkey::TITLE_SCREEN__ADDONS,
boost::bind(&hotkey, boost::ref(window), GET_ADDONS));
window.register_hotkey(
hotkey::TITLE_SCREEN__CORES,
boost::bind(&hotkey, boost::ref(window), CORES));
window.register_hotkey(hotkey::TITLE_SCREEN__CORES,
boost::bind(&hotkey, boost::ref(window), CORES));
window.register_hotkey(
hotkey::TITLE_SCREEN__EDITOR,

View file

@ -33,8 +33,7 @@ ttransient_message::ttransient_message(const std::string& title,
const std::string& message,
const bool message_use_markup,
const std::string& image)
: hide_title_(title.empty())
, hide_image_(image.empty())
: hide_title_(title.empty()), hide_image_(image.empty())
{
register_label("title", true, title, title_use_markup);
register_label("message", true, message, message_use_markup);

View file

@ -137,7 +137,8 @@ void tunit_create::pre_show(CVideo& /*video*/, twindow& window)
}
if(type_ids_.empty()) {
ERR_GUI_G << "no unit types found for unit create dialog; not good" << std::endl;
ERR_GUI_G << "no unit types found for unit create dialog; not good"
<< std::endl;
}
}

View file

@ -49,7 +49,7 @@ std::string format_file_list(const std::vector<std::string>& files_original)
const std::string& addons_path = get_addon_campaigns_dir();
std::vector<std::string> files(files_original);
BOOST_FOREACH(std::string& file, files)
BOOST_FOREACH(std::string & file, files)
{
std::string base;
std::string filename = file_name(file);
@ -127,7 +127,6 @@ std::string format_file_list(const std::vector<std::string>& files_original)
return utils::bullet_list(files);
}
}
namespace gui2
@ -203,14 +202,14 @@ twml_error::twml_error(const std::string& summary,
void twml_error::pre_show(CVideo& /*video*/, twindow& window)
{
if(!have_files_) {
if(!have_files_) {
tcontrol& filelist = find_widget<tcontrol>(&window, "files", false);
filelist.set_visible(tcontrol::tvisible::invisible);
}
if(!have_post_summary_) {
tcontrol& post_summary = find_widget<tcontrol>(&window,
"post_summary", false);
tcontrol& post_summary
= find_widget<tcontrol>(&window, "post_summary", false);
post_summary.set_visible(tcontrol::tvisible::invisible);
}

View file

@ -17,7 +17,8 @@
#include "gui/dialogs/dialog.hpp"
namespace gui2 {
namespace gui2
{
/** WML preprocessor/parser error report dialog. */
class twml_error : public tdialog
@ -32,7 +33,7 @@ public:
* @param files List of WML files on which errors were detected.
* @param details Detailed WML preprocessor/parser error report.
*/
twml_error(const std::string& summary,
twml_error(const std::string& summary,
const std::string& post_summary,
const std::vector<std::string>& files,
const std::string& details);
@ -69,6 +70,6 @@ private:
void copy_report_callback();
};
} // end namespace gui2
} // end namespace gui2
#endif

View file

@ -62,7 +62,8 @@ unsigned decode_font_style(const std::string& style)
return TTF_STYLE_NORMAL;
}
ERR_GUI_G << "Unknown style '" << style << "' using 'normal' instead." << std::endl;
ERR_GUI_G << "Unknown style '" << style << "' using 'normal' instead."
<< std::endl;
return TTF_STYLE_NORMAL;
}

View file

@ -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);

View file

@ -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);

View file

@ -208,7 +208,8 @@ private:
* Minimap & @macro = minimap_description $
* Multi_page & @macro = multi_page_description $
* Panel & @macro = panel_description $
* Password_box & A text box masking it's content by asterisks. $
* Password_box & A text box masking it's content by asterisks.
* $
* Repeating_button & @macro = repeating_button_description $
* Scroll_label & @macro = scroll_label_description $
* Slider & @macro = slider_description $
@ -504,11 +505,13 @@ void load_settings()
catch(config::error& e)
{
ERR_GUI_P << e.what() << '\n';
ERR_GUI_P << "Setting: could not read file 'data/gui/default.cfg'." << std::endl;
ERR_GUI_P << "Setting: could not read file 'data/gui/default.cfg'."
<< std::endl;
}
catch(const abstract_validator::error& e)
{
ERR_GUI_P << "Setting: could not read file 'data/gui/schema.cfg'." << std::endl;
ERR_GUI_P << "Setting: could not read file 'data/gui/schema.cfg'."
<< std::endl;
ERR_GUI_P << e.message;
}
// Parse guis

View file

@ -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);
@ -142,20 +142,22 @@ void ttext_::insert_char(const Uint16 unicode)
void ttext_::copy_selection(const bool mouse)
{
if(selection_length_ == 0) return;
unsigned end,start = selection_start_;
if(selection_length_ == 0) {
return;
}
unsigned end, start = selection_start_;
const utf8::string txt = text_.text();
if(selection_length_ > 0) {
end = utf8::index(txt,start+selection_length_);
start = utf8::index(txt,start);
if(selection_length_ > 0) {
end = utf8::index(txt, start + selection_length_);
start = utf8::index(txt, start);
} else {
// inverse selection: selection_start_ is in fact the end
end = utf8::index(txt,start);
start = utf8::index(txt,start+selection_length_);
end = utf8::index(txt, start);
start = utf8::index(txt, start + selection_length_);
}
copy_to_clipboard(txt.substr(start,end-start), mouse);
copy_to_clipboard(txt.substr(start, end - start), mouse);
}
void ttext_::paste_selection(const bool mouse)
@ -275,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,
@ -300,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";

View file

@ -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);

View file

@ -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)) {

View file

@ -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);