The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
password_box.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2016 by Thomas Baumhauer
4  Copyright (C) 2009 - 2016 by Mark de Wever <[email protected]>
5  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
6 
7  This program is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY.
13 
14  See the COPYING file for more details.
15 */
16 
17 #define GETTEXT_DOMAIN "wesnoth-lib"
18 
20 
21 #include "gui/core/log.hpp"
23 #include "gui/widgets/settings.hpp"
26 
27 #include "utils/functional.hpp"
28 
29 #define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
30 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
31 
32 namespace gui2
33 {
34 
35 // ------------ WIDGET -----------{
36 
37 REGISTER_WIDGET3(ttext_box_definition, password_box, "text_box_definition")
38 
39 namespace
40 {
41 
42 size_t get_text_length(const std::string& str)
43 {
44  return utf8::size(str);
45 }
46 
47 } // namespace
48 
50 {
53  ttext_box::set_value(std::string(get_text_length(real_value_), '*'));
54 }
55 
57 {
58  pre_function();
59  ttext_box::insert_char(unicode);
60  post_function();
61 }
62 
63 void tpassword_box::delete_char(const bool before_cursor)
64 {
65  pre_function();
66  ttext_box::delete_char(before_cursor);
67  post_function();
68 }
69 
70 void tpassword_box::handle_key_backspace(SDLMod /*modifier*/, bool& handled)
71 {
72  pre_function();
73 
74  // Copy & paste from ttext_::handle_key_backspace()
75  DBG_GUI_E << LOG_SCOPE_HEADER << '\n';
76 
77  handled = true;
78  if(get_selection_length() != 0) {
80  } else if(get_selection_start()) {
81  delete_char(true);
82  }
83 
84  post_function();
85 }
86 
87 void tpassword_box::handle_key_delete(SDLMod /*modifier*/, bool& handled)
88 {
89  pre_function();
90 
91  // Copy & paste from ttext_::handle_key_delete()
92  DBG_GUI_E << LOG_SCOPE_HEADER << '\n';
93 
94  handled = true;
95  if(get_selection_length() != 0) {
97  } else if(get_selection_start() < get_text_length(text())) {
98  delete_char(false);
99  }
100 
101  post_function();
102 }
103 
104 void tpassword_box::paste_selection(const bool mouse)
105 {
106  pre_function();
108  post_function();
109 }
110 
112 {
113  // ttext_box::set_value() will reset the selection,
114  // we therefore have to remember it
115  size_t selection_start = get_selection_start();
116  size_t selection_length = get_selection_length();
117 
118  // Tell ttext_box the actual input of this box
120 
121  // Restore the selection
122  set_selection_start(selection_start);
123  set_selection_length(selection_length);
124 }
125 
127 {
128  // See above
129  size_t selection_start = get_selection_start();
130  size_t selection_length = get_selection_length();
131 
132  // Get the input back and make ttext_box forget it
134  ttext_box::set_value(std::string(get_text_length(real_value_), '*'));
135 
136  // See above
137  set_selection_start(selection_start);
138  set_selection_length(selection_length);
139 
140  // Why do the selection functions not update
141  // the canvas?
142  update_canvas();
143  set_is_dirty(true);
144 }
145 
147 {
148  static const std::string type = "password_box";
149  return type;
150 }
151 
152 // }---------- BUILDER -----------{
153 
154 namespace implementation
155 {
156 
157 /*WIKI
158  * @page = GUIWidgetInstanceWML
159  * @order = 2_password_box
160  *
161  * == Password box ==
162  *
163  * @begin{parent}{name="gui/window/resolution/grid/row/column/"}
164  * @begin{tag}{name="password_box"}{min=0}{max=-1}{super="generic/widget_instance"}
165  * @begin{table}{config}
166  * label & t_string & "" & The initial text of the password box. $
167  * @end{table}
168  * @end{tag}{name="password_box"}
169  * @end{parent}{name="gui/window/resolution/grid/row/column/"}
170  */
171 
173  : tbuilder_control(cfg), history_(cfg["history"])
174 {
175 }
176 
178 {
179  tpassword_box* widget = new tpassword_box();
180 
181  init_control(widget);
182 
183  // A password box doesn't have a label but a text.
184  // It also has no history.
185  widget->set_value(label);
186 
187  DBG_GUI_G << "Window builder: placed password box '" << id
188  << "' with definition '" << definition << "'.\n";
189 
190  return widget;
191 }
192 
193 } // namespace implementation
194 
195 // }------------ END --------------
196 
197 } // namespace gui2
Define the common log macros for the gui toolkit.
void set_selection_length(const int selection_length)
Definition: text.cpp:192
void handle_key_delete(SDLMod modifier, bool &handled)
Delete key pressed.
#define SDLMod
Definition: compat.hpp:30
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1221
virtual void paste_selection(const bool mouse)
Pastes the current selection.
Definition: text.cpp:168
void set_is_dirty(const bool is_dirty)
Definition: widget.cpp:435
std::string get_value() const
Definition: text.hpp:76
size_t get_selection_start() const
Definition: text.hpp:218
virtual void set_value(const std::string &text)
The set_value is virtual for the tpassword_box class.
Definition: text.cpp:95
virtual void update_canvas() override
See tcontrol::update_canvas.
Definition: text_box.cpp:132
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
void init_control(tcontrol *control) const
Definition: control.cpp:675
std::string definition
Parameters for the control.
Definition: control.hpp:530
void delete_char(const bool before_cursor)
Deletes the character.
virtual void insert_char(const utf8::string &unicode)
Inserts a character at the cursor.
Definition: text.cpp:135
This file contains the settings handling of the widget library.
void handle_key_backspace(SDLMod modifier, bool &handled)
Backspace key pressed.
#define LOG_SCOPE_HEADER
virtual void set_value(const std::string &text)
Inherited from ttext_.
virtual const std::string & get_control_type() const override
See tcontrol::get_control_type.
size_t size(const utf8::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:88
#define DBG_GUI_E
Definition: log.hpp:35
size_t get_selection_length() const
Definition: text.hpp:224
void delete_selection()
Inherited from ttext_.
Definition: text_box.cpp:199
void insert_char(const utf8::string &unicode)
Inserts a character at the cursor.
std::string real_value_
void set_selection_start(const size_t selection_start)
Definition: text.cpp:184
Base class for all widgets.
Definition: widget.hpp:49
#define REGISTER_WIDGET3(type, id, key)
Registers a widget.
void delete_char(const bool before_cursor)
Inherited from ttext_.
Definition: text_box.cpp:188
const std::string & text() const
Definition: text.hpp:81
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
void paste_selection(const bool mouse)
Pastes the current selection.
#define DBG_GUI_G
Definition: log.hpp:41
GLsizei const GLcharARB ** string
Definition: glew.h:4503
Contains the implementation details for lexical_cast and shouldn't be used directly.
std::string string