gui/widgets/password_box.cpp

Go to the documentation of this file.
00001 /* $Id: password_box.cpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2009 - 2012 by Thomas Baumhauer <thomas.baumhauer@NOSPAMgmail.com>
00004    Copyright (C) 2009 - 2012 by Mark de Wever <koraq@xs4all.nl>
00005    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00006 
00007    This program is free software; you can redistribute it and/or modify
00008    it under the terms of the GNU General Public License as published by
00009    the Free Software Foundation; either version 2 of the License, or
00010    (at your option) any later version.
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY.
00013 
00014    See the COPYING file for more details.
00015 */
00016 
00017 #define GETTEXT_DOMAIN "wesnoth-lib"
00018 
00019 #include "gui/widgets/password_box.hpp"
00020 
00021 #include "gui/auxiliary/log.hpp"
00022 #include "gui/auxiliary/widget_definition/text_box.hpp"
00023 #include "gui/auxiliary/window_builder/password_box.hpp"
00024 #include "gui/widgets/settings.hpp"
00025 #include "serialization/string_utils.hpp"
00026 
00027 #include <boost/bind.hpp>
00028 
00029 #define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
00030 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
00031 
00032 namespace gui2 {
00033 
00034 REGISTER_WIDGET3(ttext_box_definition, password_box, "text_box_definition")
00035 
00036 namespace {
00037 
00038 size_t get_text_length(const std::string& str)
00039 {
00040     return utils::string_to_wstring(str).size();
00041 }
00042 
00043 } // namespace
00044 
00045 void tpassword_box::set_value(const std::string& text) {
00046     ttext_box::set_value(text);
00047     real_value_ = get_value();
00048     ttext_box::set_value(std::string(get_text_length(real_value_), '*'));
00049 }
00050 
00051 void tpassword_box::insert_char(const Uint16 unicode) {
00052     pre_function();
00053     ttext_box::insert_char(unicode);
00054     post_function();
00055 }
00056 
00057 void tpassword_box::delete_char(const bool before_cursor) {
00058     pre_function();
00059     ttext_box::delete_char(before_cursor);
00060     post_function();
00061 }
00062 
00063 void tpassword_box::handle_key_backspace(SDLMod /*modifier*/, bool& handled) {
00064     pre_function();
00065 
00066     // Copy & paste from ttext_::handle_key_backspace()
00067     DBG_GUI_E << LOG_SCOPE_HEADER << '\n';
00068 
00069     handled = true;
00070     if(get_selection_length() != 0) {
00071         delete_selection();
00072     } else if(get_selection_start()){
00073         delete_char(true);
00074     }
00075 
00076     post_function();
00077 }
00078 
00079 void tpassword_box::handle_key_delete(SDLMod /*modifier*/, bool& handled) {
00080     pre_function();
00081 
00082     // Copy & paste from ttext_::handle_key_delete()
00083     DBG_GUI_E << LOG_SCOPE_HEADER << '\n';
00084 
00085     handled = true;
00086     if(get_selection_length() != 0) {
00087         delete_selection();
00088     } else if (get_selection_start() < get_text_length(text())) {
00089         delete_char(false);
00090     }
00091 
00092     post_function();
00093 }
00094 
00095 void tpassword_box::paste_selection(const bool mouse) {
00096     pre_function();
00097     ttext_box::paste_selection(mouse);
00098     post_function();
00099 }
00100 
00101 void tpassword_box::pre_function() {
00102     // ttext_box::set_value() will reset the selection,
00103     // we therefore have to remember it
00104     size_t selection_start = get_selection_start();
00105     size_t selection_length = get_selection_length();
00106 
00107     // Tell ttext_box the actual input of this box
00108     ttext_box::set_value(real_value_);
00109 
00110     // Restore the selection
00111     set_selection_start(selection_start);
00112     set_selection_length(selection_length);
00113 }
00114 
00115 void tpassword_box::post_function() {
00116     // See above
00117     size_t selection_start = get_selection_start();
00118     size_t selection_length = get_selection_length();
00119 
00120     // Get the input back and make ttext_box forget it
00121     real_value_ = get_value();
00122     ttext_box::set_value(std::string(get_text_length(real_value_), '*'));
00123 
00124     // See above
00125     set_selection_start(selection_start);
00126     set_selection_length(selection_length);
00127 
00128     // Why do the selection functions not update
00129     // the canvas?
00130     update_canvas();
00131     set_dirty();
00132 }
00133 
00134 const std::string& tpassword_box::get_control_type() const
00135 {
00136     static const std::string type = "password_box";
00137     return type;
00138 }
00139 
00140 } //namespace gui2
00141 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:02:57 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs