Added a textbox widget that displays its input as stars to be used...

...in the mp login dialogs.
This commit is contained in:
Thomas Baumhauer 2009-01-15 17:38:23 +00:00
parent f1ab17e660
commit 798e0a1001
12 changed files with 234 additions and 15 deletions

View file

@ -143,12 +143,12 @@
border_size = 5
horizontal_alignment = "right"
[text_box]
[password_box]
id = "password"
definition = "default"
label = ""
[/text_box]
[/password_box]
[/column]

View file

@ -123,12 +123,12 @@
border_size = 5
horizontal_alignment = "right"
[text_box]
[password_box]
id = "password"
definition = "default"
label = ""
[/text_box]
[/password_box]
[/column]

View file

@ -237,6 +237,7 @@ wesnoth_sources = Split("""
gui/widgets/menubar.cpp
gui/widgets/minimap.cpp
gui/widgets/panel.cpp
gui/widgets/password_box.cpp
gui/widgets/settings.cpp
gui/widgets/scroll_label.cpp
gui/widgets/scrollbar_container.cpp

View file

@ -22,6 +22,7 @@
#include "gui/widgets/button.hpp"
#include "gui/widgets/listbox.hpp"
#include "gui/widgets/label.hpp"
#include "gui/widgets/password_box.hpp"
#include "gui/widgets/toggle_button.hpp"
namespace gui2 {
@ -218,8 +219,8 @@ void tmp_login::pre_show(CVideo& /*video*/, twindow& window)
username->set_value(preferences::login());
window.keyboard_capture(username);
ttext_box* password =
dynamic_cast<ttext_box*>(window.find_widget("password", false));
tpassword_box* password =
dynamic_cast<tpassword_box*>(window.find_widget("password", false));
VALIDATE(password, missing_widget("password"));
password->set_value(preferences::password());
@ -254,11 +255,11 @@ void tmp_login::post_show(twindow& window)
preferences::set_login(username->get_value());
ttext_box* password =
dynamic_cast<ttext_box*>(window.find_widget("password", false));
tpassword_box* password =
dynamic_cast<tpassword_box*>(window.find_widget("password", false));
assert(password);
preferences::set_password(password->get_value());
preferences::set_password(password->get_real_value());
}
}

View file

@ -17,6 +17,7 @@
#include "gui/dialogs/mp_method_selection.hpp"
#include "gui/widgets/listbox.hpp"
#include "gui/widgets/password_box.hpp"
#include "gui/widgets/window.hpp"
#include "gui/widgets/text_box.hpp"
#include "gui/widgets/toggle_button.hpp"
@ -54,7 +55,7 @@ void tmp_method_selection::pre_show(CVideo& /*video*/, twindow& window)
user_widget->set_maximum_length(mp::max_login_size);
window.keyboard_capture(user_widget);
ttext_box* password_widget = dynamic_cast<ttext_box*>(window.find_widget("password", false));
tpassword_box* password_widget = dynamic_cast<tpassword_box*>(window.find_widget("password", false));
VALIDATE(password_widget, missing_widget("password"));
password_widget->set_value(preferences::password());
@ -77,7 +78,7 @@ void tmp_method_selection::post_show(twindow& window)
ttext_box* user_widget = dynamic_cast<ttext_box*>(window.find_widget("user_name", false));
assert(user_widget);
ttext_box* password_widget = dynamic_cast<ttext_box*>(window.find_widget("password", false));
tpassword_box* password_widget = dynamic_cast<tpassword_box*>(window.find_widget("password", false));
assert(password_widget);
tlistbox* list = dynamic_cast<tlistbox*>(window.find_widget("method_list", false));
@ -93,7 +94,7 @@ void tmp_method_selection::post_show(twindow& window)
user_name_= user_widget->get_value();
preferences::set_login(user_name_);
preferences::set_password(password_widget->get_value());
preferences::set_password(password_widget->get_real_value());
}
}

View file

@ -0,0 +1,80 @@
/* $Id */
/*
Copyright (C) 2009 by Thomas Baumhauer <thomas.baumhauer@NOSPAMgmail.com>
Copyright (C) 2009 by Mark de Wever <koraq@xs4all.nl>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
or at your option any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#define GETTEXT_DOMAIN "wesnoth-lib"
#include "gui/widgets/password_box.hpp"
namespace gui2 {
void tpassword_box::set_value(const std::string& text) {
ttext_box::set_value(text);
real_value_ = get_value();
ttext_box::set_value(std::string(real_value_.size(), '*'));
}
void tpassword_box::insert_char(const Uint16 unicode) {
pre_function();
ttext_box::insert_char(unicode);
post_function();
}
void tpassword_box::delete_char(const bool before_cursor) {
pre_function();
ttext_box::delete_char(before_cursor);
post_function();
}
void tpassword_box::paste_selection(const bool mouse) {
pre_function();
ttext_box::paste_selection(mouse);
post_function();
}
void tpassword_box::pre_function() {
// ttext_box::set_value() will reset the selection,
// we therefore have to remember it
size_t selection_start = get_selection_start();
size_t selection_length = get_selection_length();
// Tell ttext_box the actual input of this box
ttext_box::set_value(real_value_);
// Restore the selection
set_selection_start(selection_start);
set_selection_length(selection_length);
}
void tpassword_box::post_function() {
// See above
size_t selection_start = get_selection_start();
size_t selection_length = get_selection_length();
// Get the input back and make ttext_box forget it
real_value_ = get_value();
ttext_box::set_value(std::string(real_value_.size(), '*'));
// See above
set_selection_start(selection_start);
set_selection_length(selection_length);
// Why do the selection functions not update
// the canvas?
update_canvas();
set_dirty();
}
} //namespace gui2

View file

@ -0,0 +1,79 @@
/* $Id */
/*
Copyright (C) 2009 by Thomas Baumhauer <thomas.baumhauer@NOSPAMgmail.com>
Copyright (C) 2009 by Mark de Wever <koraq@xs4all.nl>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License version 2
or at your option any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#ifndef GUI_WIDGETS_PASSWORD_BOX_HPP_INCLUDED
#define GUI_WIDGETS_PASSWORD_BOX_HPP_INCLUDED
#include "gui/widgets/text_box.hpp"
/**
* A class inherited from ttext_box that displays
* its input as stars
*
* @todo This implementation is quite a hack that
* needs to be rewritten cleanly
*/
namespace gui2 {
class tpassword_box : public ttext_box {
// The hack works like this: we add the member real_value_
// that holds the actual user input.
// Overridden functions now simply
// - call set_value() from ttext_box with real_value_,
// which is done in prefunction()
// - call ttext_box::overridden_function()
// - set real_value_ to get_value() from ttext_box and
// call set_value() from ttext_box with real_value_
// turned into stars, which is done in post_function()
//
// and overridden function should therefore look like this:
//
// overridden_function(some parameter) {
// pre_function();
// ttext_box::overridden_function(some parameter);
// post_function();
// }
public:
tpassword_box() : ttext_box(), real_value_() {}
void set_value(const std::string& text);
std::string get_real_value() const { return real_value_; }
protected:
// Overwritten functions must of course be virtual!
void insert_char(const Uint16 unicode);
void delete_char(const bool before_cursor);
void paste_selection(const bool mouse);
// We do not override copy_selection because we
// actually want it to copy just the stars
private:
void pre_function();
void post_function();
std::string real_value_;
};
} //namespace gui2
#endif

View file

@ -230,6 +230,8 @@ const std::string& tgui_definition::read(const config& cfg)
load_definitions<tslider_definition>("slider", cfg.get_children("slider_definition"));
load_definitions<tspacer_definition>("spacer", cfg.get_children("spacer_definition"));
load_definitions<ttext_box_definition>("text_box", cfg.get_children("text_box_definition"));
// use the same definition for password boxes
load_definitions<ttext_box_definition>("password_box", cfg.get_children("text_box_definition"));
load_definitions<ttoggle_button_definition>("toggle_button", cfg.get_children("toggle_button_definition"));
load_definitions<ttoggle_panel_definition>("toggle_panel", cfg.get_children("toggle_panel_definition"));
load_definitions<ttooltip_definition>("tooltip", cfg.get_children("tooltip_definition"));

View file

@ -140,7 +140,7 @@ protected:
*
* @param unicode The unicode value of the character to insert.
*/
void insert_char(const Uint16 unicode);
virtual void insert_char(const Uint16 unicode);
/**
* Deletes the character.

View file

@ -27,6 +27,7 @@
#include "gui/widgets/listbox.hpp"
#include "gui/widgets/generator.hpp"
#include "gui/widgets/minimap.hpp"
#include "gui/widgets/password_box.hpp"
#include "gui/widgets/scroll_label.hpp"
#include "gui/widgets/slider.hpp"
#include "gui/widgets/spacer.hpp"
@ -191,6 +192,8 @@ tbuilder_widget_ptr create_builder_widget(const config& cfg)
return new tbuilder_spacer(*(cfg.child("spacer")));
} else if(cfg.child("text_box")) {
return new tbuilder_text_box(*(cfg.child("text_box")));
} else if(cfg.child("password_box")) {
return new tbuilder_password_box(*(cfg.child("password_box")));
} else if(cfg.child("toggle_button")) {
return new tbuilder_toggle_button(*(cfg.child("toggle_button")));
} else if(cfg.child("toggle_panel")) {
@ -1168,6 +1171,25 @@ twidget* tbuilder_text_box::build() const
return text_box;
}
twidget* tbuilder_password_box::build() const
{
tpassword_box* password_box = new tpassword_box();
init_control(password_box);
// A textbox doesn't have a label but a text
password_box->set_value(label);
if (!history_.empty()) {
password_box->set_history(history_);
}
DBG_GUI << "Window builder: placed password box '" << id << "' with defintion '"
<< definition << "'.\n";
return password_box;
}
twidget* tbuilder_vertical_scrollbar::build() const
{
tvertical_scrollbar *vertical_scrollbar = new tvertical_scrollbar();

View file

@ -351,6 +351,41 @@ public:
twidget* build () const;
};
// copy & paste from tbuilder_text_box...
// does it make more sense to inherit from it?
struct tbuilder_password_box : public tbuilder_control
{
private:
tbuilder_password_box();
std::string history_;
public:
/*WIKI
* @page = GUIToolkitWML
* @order = 3_widget_password_box
*
* == Password box ==
*
* @start_table = config
* label (tstring = "") The initial text of the text box.
* history (string = "") The name of the history for the text box.
* A history saves the data entered in a
* text box between the games. With the up
* and down arrow it can be accessed. To
* create a new history item just add a new
* unique name for this field and the engine
* will handle the rest.
* @end_table
*
*/
tbuilder_password_box(const config& cfg) :
tbuilder_control(cfg),
history_(cfg["history"])
{}
twidget* build () const;
};
struct tbuilder_toggle_button : public tbuilder_control
{
private:

View file

@ -309,8 +309,6 @@ static server_type open_connection(game_display& disp, const std::string& origin
// Now show a dialog that displays the error and allows to
// enter a new user name and/or password
/** @todo A fancy textbox that displays characters as dots or asterisks would nice. */
gui2::tmp_login dlg((*error)["message"]);
dlg.show(disp.video());