The Battle for Wesnoth  1.17.0-dev
password_box.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2021
3  by Thomas Baumhauer <thomas.baumhauer@NOSPAMgmail.com>, Mark de Wever <koraq@xs4all.nl>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #define GETTEXT_DOMAIN "wesnoth-lib"
17 
19 
20 #include "gui/core/log.hpp"
22 #include "gui/widgets/settings.hpp"
24 
25 #include "desktop/clipboard.hpp"
26 #include <functional>
27 
28 #define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
29 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
30 
31 namespace gui2
32 {
33 
34 // ------------ WIDGET -----------{
35 
36 REGISTER_WIDGET3(text_box_definition, password_box, "text_box_definition")
37 
38 password_box::password_box(const implementation::builder_password_box& builder)
39  : text_box(builder)
40  , real_value_()
41 {
42 }
43 
44 void password_box::set_value(const std::string& text)
45 {
46  real_value_ = text;
47  std::size_t sz = utf8::size(text);
48  std::string passwd;
49  for(std::size_t i = 0; i < sz; i++) {
50  passwd.append(font::unicode_bullet);
51  }
52  text_box::set_value(passwd);
53 }
54 
56 {
57  int len = get_selection_length();
58  if(len == 0) {
59  return;
60  }
61  unsigned start = get_selection_start();
62  if(len < 0) {
63  len = -len;
64  start -= len;
65  }
66 
67  utf8::erase(real_value_, start, len);
69  set_cursor(start, false);
70 }
71 
72 void password_box::insert_char(const std::string& unicode)
73 {
74  int len = get_selection_length();
75  unsigned sel = get_selection_start();
76  if(len < 0) {
77  len = -len;
78  sel -= len;
79  }
80 
81  std::size_t sz = utf8::size(unicode);
82  if(sz == 1) {
84  } else {
85  std::string passwd;
86  for(std::size_t i = 0; i < sz; i++) {
87  passwd.append(font::unicode_bullet);
88  }
89  text_box::insert_char(passwd);
90  }
91  utf8::insert(real_value_, sel, unicode);
92 }
93 
94 void password_box::paste_selection(const bool mouse)
95 {
96  const std::string& text = desktop::clipboard::copy_from_clipboard(mouse);
97  if(text.empty()) {
98  return;
99  }
100  insert_char(text);
101 }
102 
103 const std::string& password_box::type()
104 {
105  static const std::string type = "password_box";
106  return type;
107 }
108 
109 const std::string& password_box::get_control_type() const
110 {
111  return type();
112 }
113 
114 // }---------- BUILDER -----------{
115 
116 namespace implementation
117 {
118 
119 builder_password_box::builder_password_box(const config& cfg)
120  : builder_styled_widget(cfg), history_(cfg["history"])
121 {
122 }
123 
125 {
126  password_box* widget = new password_box(*this);
127 
128  // A password box doesn't have a label but a text.
129  // It also has no history.
130  widget->set_value(label_string);
131 
132  DBG_GUI_G << "Window builder: placed password box '" << id
133  << "' with definition '" << definition << "'.\n";
134 
135  return widget;
136 }
137 
138 } // namespace implementation
139 
140 // }------------ END --------------
141 
142 } // 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:39
void paste_selection(const bool mouse) override
Pastes the current selection.
Base class for all widgets.
Definition: widget.hpp:49
virtual widget * build() const override
Class for a single line text area.
Definition: text_box.hpp:141
std::size_t get_selection_length() const
Generic file dialog.
Definition: field-fwd.hpp:23
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:87
This file contains the settings handling of the widget library.
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:30
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:967
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:100
const std::string unicode_bullet
Definition: constants.cpp:47
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:105
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.
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:212
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:61
static const std::string & type()
Static type getter that does not rely on the widget being constructed.
#define DBG_GUI_G
Definition: log.hpp:41
Contains the implementation details for lexical_cast and shouldn&#39;t be used directly.
std::string real_value_
Key Type Default Description label t_string"" The initial text of the password box.