The Battle for Wesnoth  1.15.2+dev
password_box.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2018 by Thomas Baumhauer
3  <thomas.baumhauer@NOSPAMgmail.com>
4  Copyright (C) 2009 - 2018 by Mark de Wever <koraq@xs4all.nl>
5  Part of the Battle for Wesnoth Project https://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"
25 
26 #include "desktop/clipboard.hpp"
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(text_box_definition, password_box, "text_box_definition")
38 
39 password_box::password_box(const implementation::builder_password_box& builder)
40  : text_box(builder)
41  , real_value_()
42 {
43 }
44 
45 void password_box::set_value(const std::string& text)
46 {
47  real_value_ = text;
48  std::size_t sz = utf8::size(text);
49  std::string passwd;
50  for(std::size_t i = 0; i < sz; i++) {
51  passwd.append(font::unicode_bullet);
52  }
53  text_box::set_value(passwd);
54 }
55 
57 {
58  int len = get_selection_length();
59  if(len == 0) {
60  return;
61  }
62  unsigned start = get_selection_start();
63  if(len < 0) {
64  len = -len;
65  start -= len;
66  }
67 
68  utf8::erase(real_value_, start, len);
70  set_cursor(start, false);
71 }
72 
73 void password_box::insert_char(const std::string& unicode)
74 {
75  int len = get_selection_length();
76  unsigned sel = get_selection_start();
77  if(len < 0) {
78  len = -len;
79  sel -= len;
80  }
81 
82  std::size_t sz = utf8::size(unicode);
83  if(sz == 1) {
85  } else {
86  std::string passwd;
87  for(std::size_t i = 0; i < sz; i++) {
88  passwd.append(font::unicode_bullet);
89  }
90  text_box::insert_char(passwd);
91  }
92  utf8::insert(real_value_, sel, unicode);
93 }
94 
95 void password_box::paste_selection(const bool mouse)
96 {
97  const std::string& text = desktop::clipboard::copy_from_clipboard(mouse);
98  if(text.empty()) {
99  return;
100  }
101  insert_char(text);
102 }
103 
104 const std::string& password_box::type() \
105 {
106  static const std::string type = "password_box";
107  return type;
108 }
109 
110 const std::string& password_box::get_control_type() const
111 {
112  return type();
113 }
114 
115 // }---------- BUILDER -----------{
116 
117 namespace implementation
118 {
119 
120 /*WIKI
121  * @page = GUIWidgetInstanceWML
122  * @order = 2_password_box
123  *
124  * == Password box ==
125  *
126  * @begin{parent}{name="gui/window/resolution/grid/row/column/"}
127  * @begin{tag}{name="password_box"}{min=0}{max=-1}{super="generic/widget_instance"}
128  * @begin{table}{config}
129  * label & t_string & "" & The initial text of the password box. $
130  * @end{table}
131  * @end{tag}{name="password_box"}
132  * @end{parent}{name="gui/window/resolution/grid/row/column/"}
133  */
134 
135 builder_password_box::builder_password_box(const config& cfg)
136  : builder_styled_widget(cfg), history_(cfg["history"])
137 {
138 }
139 
141 {
142  password_box* widget = new password_box(*this);
143 
144  // A password box doesn't have a label but a text.
145  // It also has no history.
146  widget->set_value(label_string);
147 
148  DBG_GUI_G << "Window builder: placed password box '" << id
149  << "' with definition '" << definition << "'.\n";
150 
151  return widget;
152 }
153 
154 } // namespace implementation
155 
156 // }------------ END --------------
157 
158 } // namespace gui2
Define the common log macros for the gui toolkit.
void insert_char(const std::string &unicode) override
Inserts a character at the cursor.
std::string copy_from_clipboard(const bool)
Copies text from the clipboard.
Definition: clipboard.cpp:40
void paste_selection(const bool mouse) override
Pastes the current selection.
Base class for all widgets.
Definition: widget.hpp:47
Class for a single line text area.
Definition: text_box.hpp:121
std::size_t get_selection_length() const
Generic file dialog.
Definition: field-fwd.hpp:22
std::string definition
Parameters for the styled_widget.
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:86
This file contains the settings handling of the widget library.
virtual void set_value(const std::string &text) override
Inherited from text_box_base.
void delete_selection() override
Deletes the current selection.
virtual void insert_char(const std::string &unicode)
Inserts a character at the cursor.
std::size_t i
Definition: function.cpp:933
std::string & insert(std::string &str, const std::size_t pos, const std::string &insert)
Insert a UTF-8 string at the specified position.
Definition: unicode.cpp:99
const std::string unicode_bullet
Definition: constants.cpp:43
std::string & erase(std::string &str, const std::size_t start, const std::size_t len)
Erases a portion of a UTF-8 string.
Definition: unicode.cpp:104
virtual void set_value(const std::string &text)
The set_value is virtual for the password_box class.
const std::string & text() const
#define REGISTER_WIDGET3(type, id, key)
Registers a widget.
EXIT_STATUS start(const std::string &filename, bool take_screenshot, const std::string &screenshot_filename)
Main interface for launching the editor from the title screen.
Definition: editor_main.cpp:28
virtual const std::string & get_control_type() const override
See styled_widget::get_control_type.
std::size_t get_selection_start() const
void set_cursor(const std::size_t offset, const bool select)
Moves the cursor at the wanted position.
text_history history_
The history text for this widget.
Definition: text_box.hpp:192
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:68
static const std::string & type()
Static type getter that does not rely on the widget being constructed.
#define DBG_GUI_G
Definition: log.hpp:40
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.
std::string real_value_