The Battle for Wesnoth  1.19.1+dev
password_box.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2024
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"
23 
24 #include "desktop/clipboard.hpp"
25 
26 #define LOG_SCOPE_HEADER get_control_type() + " [" + id() + "] " + __func__
27 #define LOG_HEADER LOG_SCOPE_HEADER + ':'
28 
29 namespace gui2
30 {
31 
32 // ------------ WIDGET -----------{
33 
34 REGISTER_WIDGET3(text_box_definition, password_box, "text_box_definition")
35 
36 password_box::password_box(const implementation::builder_password_box& builder)
37  : text_box(builder)
38  , real_value_()
39 {
40 }
41 
42 void password_box::set_value(const std::string& text)
43 {
44  real_value_ = text;
45  std::size_t sz = utf8::size(text);
46  std::string passwd;
47  for(std::size_t i = 0; i < sz; i++) {
48  passwd.append(font::unicode_bullet);
49  }
50  text_box::set_value(passwd);
51 }
52 
54 {
55  int len = get_selection_length();
56  if(len == 0) {
57  return;
58  }
59  unsigned start = get_selection_start();
60  if(len < 0) {
61  len = -len;
62  start -= len;
63  }
64 
67  set_cursor(start, false);
68 }
69 
70 void password_box::insert_char(const std::string& unicode)
71 {
72  int len = get_selection_length();
73  unsigned sel = get_selection_start();
74  if(len < 0) {
75  len = -len;
76  sel -= len;
77  }
78 
79  std::size_t sz = utf8::size(unicode);
80  if(sz == 1) {
82  } else {
83  std::string passwd;
84  for(std::size_t i = 0; i < sz; i++) {
85  passwd.append(font::unicode_bullet);
86  }
87  text_box::insert_char(passwd);
88  }
89  utf8::insert(real_value_, sel, unicode);
90 }
91 
92 void password_box::paste_selection(const bool mouse)
93 {
94  const std::string& text = desktop::clipboard::copy_from_clipboard(mouse);
95  if(text.empty()) {
96  return;
97  }
99 }
100 
101 const std::string& password_box::type()
102 {
103  static const std::string type = "password_box";
104  return type;
105 }
106 
107 const std::string& password_box::get_control_type() const
108 {
109  return type();
110 }
111 
112 // }---------- BUILDER -----------{
113 
114 namespace implementation
115 {
116 
118  : builder_styled_widget(cfg), history_(cfg["history"])
119 {
120 }
121 
122 std::unique_ptr<widget> builder_password_box::build() const
123 {
124  auto widget = std::make_unique<password_box>(*this);
125 
126  // A password box doesn't have a label but a text.
127  // It also has no history.
128  widget->set_value(label_string);
129 
130  DBG_GUI_G << "Window builder: placed password box '" << id
131  << "' with definition '" << definition << "'.";
132 
133  return widget;
134 }
135 
136 } // namespace implementation
137 
138 // }------------ END --------------
139 
140 } // namespace gui2
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:159
std::string real_value_
void delete_selection() override
Deletes the current selection.
virtual void set_value(const std::string &text) override
Inherited from text_box_base.
virtual const std::string & get_control_type() const override
See styled_widget::get_control_type.
void paste_selection(const bool mouse) override
Pastes the current selection.
void insert_char(const std::string &unicode) override
Inserts a character at the cursor.
static const std::string & type()
Static type getter that does not rely on the widget being constructed.
virtual void insert_char(const std::string &unicode)
Inserts a character at the cursor.
const std::string & text() const
virtual void set_value(const std::string &text)
The set_value is virtual for the password_box class.
std::size_t get_selection_length() const
virtual void set_cursor(const std::size_t offset, const bool select)
Moves the cursor at the wanted position.
std::size_t get_selection_start() const
A widget that allows the user to input text in single line.
Definition: text_box.hpp:125
Base class for all widgets.
Definition: widget.hpp:53
std::size_t i
Definition: function.cpp:968
Define the common log macros for the gui toolkit.
#define DBG_GUI_G
Definition: log.hpp:41
std::string copy_from_clipboard(const bool)
Copies text from the clipboard.
Definition: clipboard.cpp:37
EXIT_STATUS start(bool clear_id, const std::string &filename, bool take_screenshot, const std::string &screenshot_filename)
Main interface for launching the editor from the title screen.
const std::string unicode_bullet
Definition: constants.cpp:47
Generic file dialog.
Contains the implementation details for lexical_cast and shouldn't be used directly.
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:98
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:103
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:85
#define REGISTER_WIDGET3(type, id, key)
Registers a widget.
virtual std::unique_ptr< widget > build() const override
std::string definition
Parameters for the styled_widget.