Webdar 1.0.0
Web user interface to libdar
html_form_input.hpp
1/*********************************************************************/
2// webdar - a web server and interface program to libdar
3// Copyright (C) 2013-2025 Denis Corbin
4//
5// This file is part of Webdar
6//
7// Webdar 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 3 of the License, or
10// (at your option) any later version.
11//
12// Webdar is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License
18// along with Webdar. If not, see <http://www.gnu.org/licenses/>
19//
20//----
21// to contact the author: dar.linux@free.fr
22/*********************************************************************/
23
24#ifndef HTML_FORM_INPUT_HPP
25#define HTML_FORM_INPUT_HPP
26
27 // C system header files
28#include "my_config.h"
29extern "C"
30{
31
32}
33
34 // C++ system header files
35#include <vector>
36
37 // webdar headers
38#include "body_builder.hpp"
39#include "events.hpp"
40#include "jsoner.hpp"
41#include "html_text.hpp"
42#include "html_label.hpp"
43
45
53
55 public events,
56 public jsoner
57{
58public:
59 static const std::string changed;
60
61 enum input_type { text, password, number, range, check, file };
62
63 html_form_input(const std::string & label,
64 input_type type,
65 const std::string & initial_value,
66 const std::string & size,
67 const std::string & css_class
68 );
69 html_form_input(const html_form_input & ref) = default;
70 html_form_input(html_form_input && ref) noexcept = delete;
71 html_form_input & operator = (const html_form_input & ref) = default;
72 html_form_input & operator = (html_form_input && ref) noexcept = delete;
73 ~html_form_input() = default;
74
75 void set_range(const libdar::infinint & min, const libdar::infinint & max);
76 void set_min_only(const libdar::infinint & min);
77 void set_max_only(const libdar::infinint & max);
78 void change_label(const std::string & label);
79 void change_type(input_type type);
80
81 void set_value(const std::string & val);
82 void set_value_as_bool(bool val);
83 void set_value_as_int(int val);
84 const std::string & get_value() const { return x_init; };
85 bool get_value_as_bool() const { return !x_init.empty(); }; //< for checkbox empty string means unchecked, anything else checked
86 int get_value_as_int() const;
87
89 void set_enabled(bool val);
90
92
94 void box_set_css_class(const std::string & val) { if(box_css != val) my_body_part_has_changed(); box_css = val; };
95
97 const std::string & box_get_css_class() const { return box_css; };
98
100
107 void set_change_event_name(const std::string & name);
108
110 std::string get_event_name() const { return modif_change.empty()? changed: modif_change; };
111
113 virtual void load_json(const json & source) override;
114
116 virtual json save_json() const override;
117
119 virtual void clear_json() override;
120
122 void set_tooltip(const std::string & msg) { hlabel.set_tooltip(msg); };
123
124protected:
126 virtual std::string inherited_get_body_part(const chemin & path,
127 const request & req) override;
128
130 virtual void new_css_library_available() override;
131
132
133private:
134 bool enabled;
135 std::string x_type;
136 std::string x_init;
137 std::string x_size;
138 std::string x_min;
139 std::string x_max;
140 bool value_set;
141 std::string box_css;
142 std::string modif_change;
143
144 void my_act() { act(modif_change.empty() ? changed: modif_change); };
145 void check_min_max_change(const std::string & next_min, const std::string & next_max);
146 std::string generate_input(const std::string & csscl, const std::string & id);
147
148 html_text new_line;
149 html_label hlabel;
150
151 static std::string string_for_type(input_type type);
152
153 static constexpr const unsigned int format_version = 1;
154 static constexpr const char* myclass_id = "html_form_input";
155
156 static constexpr const char* jlabel_init = "value";
157
158 static const std::string css_label;
159 static const std::string css_input;
160 static const std::string css_check;
161 static const std::string css_checktitle;
162 static const std::string css_vertical_space;
163 static const std::string css_display_no_CR;
164};
165
166#endif
class body_builder is the root class of object generating HTML body
Definition: body_builder.hpp:99
void my_body_part_has_changed()
let a class record a change in what inherited_get_body_part() would return if it was called again wit...
Definition: body_builder.cpp:420
unsigned int size() const
access to adopted childs
Definition: body_builder.hpp:395
class chemin definition
Definition: chemin.hpp:51
class css_class gather css attributed under a given label
Definition: css_class.hpp:51
class events
Definition: events.hpp:52
void act(const std::string &name)
trigger a given event
Definition: events.cpp:151
class html_form_input implements HTML input feature
Definition: html_form_input.hpp:57
virtual std::string inherited_get_body_part(const chemin &path, const request &req) override
inherited from body_builder
Definition: html_form_input.cpp:222
void set_change_event_name(const std::string &name)
change the name of the event generated when this control changes
Definition: html_form_input.cpp:361
virtual void new_css_library_available() override
inherited from body_builder
Definition: html_form_input.cpp:305
void set_enabled(bool val)
set whether the HTML control is enable or disabled
Definition: html_form_input.cpp:168
virtual void clear_json() override
inherited from jsoner
Definition: html_form_input.cpp:217
void set_tooltip(const std::string &msg)
set tooltip for the html label of the input form
Definition: html_form_input.hpp:122
std::string get_event_name() const
get the event name used when the component changes
Definition: html_form_input.hpp:110
virtual json save_json() const override
inherited from jsoner
Definition: html_form_input.cpp:206
const std::string & box_get_css_class() const
get the current css class applied to the box only
Definition: html_form_input.hpp:97
virtual void load_json(const json &source) override
inherited from jsoner
Definition: html_form_input.cpp:179
void box_set_css_class(const std::string &val)
apply css to the box (text box if checkbox, but not to the label)
Definition: html_form_input.hpp:94
html_form_input(const std::string &label, input_type type, const std::string &initial_value, const std::string &size, const std::string &css_class)
Definition: html_form_input.cpp:55
class html_label implements label and tooltips (helper string showing when mouse hovering)
Definition: html_label.hpp:47
class html_text manage text and header in html document
Definition: html_text.hpp:52
void set_tooltip(const std::string &helper)
set the helper to the tooltip (helper popup)
Definition: html_tooltip.cpp:45
class jsoner
Definition: jsoner.hpp:73
class holding fields of an HTTP request (method, URI, header, cookies, and so on)
Definition: request.hpp:45
defines the event class
defines jsoner class and class exception_json