Webdar 1.0.0
Web user interface to libdar
css_class.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 CSS_CLASS_HPP
25#define CSS_CLASS_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 <string>
36#include <map>
37
38
39 // webdar headers
40#include "css.hpp"
41
43
49
51{
52public:
53
56 {
57 active,
58 checked,
59 disabled,
60 enabled,
61 focus,
62 hover,
63 link,
64 in_range,
65 invalid,
66 out_of_range,
67 read_only,
68 read_write,
69 root,
70 target,
71 valid,
72 visited
73 };
74
75 enum pseudo_element_type
76 {
77 first_line,
78 first_letter,
79 before,
80 after,
81 marker,
82 selection
83 };
84
85 css_class(const std::string & name = "");
86 css_class(const std::string & name, const css & ref);
87 css_class(const css_class & ref) = default;
88 css_class(css_class && ref) noexcept = default;
89 css_class & operator = (const css_class & ref) = default;
90 css_class & operator = (css_class && ref) noexcept = default;
91 virtual ~css_class() = default;
92
94 void change_name(const std::string & newname) { class_name = newname; };
95
97 const std::string & get_name() const { return class_name; };
98
100 void set_value(const css & ref) { class_value = ref; };
101
103 const css & get_value() const { if(class_name.empty()) throw WEBDAR_BUG; return class_value; };
104
106 void clear_value() { class_value.clear(); };
107
109
124 const css & ref,
125 const std::string & descendant = "");
126
129 const std::string & descendant = "");
130
132
134 void set_pseudo_element(pseudo_element_type pe,
135 const css & val,
136 const std::string & descendant = "");
137
139 void clear_pseudo_element(pseudo_element_type pe,
140 const std::string & descendant = "");
141
143 void clear() { class_value.clear(); selectors.clear(); };
144
146 std::string get_definition() const;
147
148private:
149 template<class T> struct sujet
150 {
151 T type;
152 std::string subcomp;
153
154 sujet(T t): type(t) { subcomp.clear(); };
155 sujet(T t, const std::string & sub): type(t), subcomp(sub) {};
156 sujet(const sujet & ref) = default;
157 sujet(sujet && ref) = default;
158 sujet & operator = (const sujet & ref) = default;
159 sujet & operator = (sujet && ref) = default;
160 ~sujet() = default;
161
162 // to be used a index of std::map
163 bool operator < (const sujet & ref) const;
164 };
165
166 std::string class_name;
167 std::string descendant;
168 css class_value;
169 std::map<sujet<selector_type>, css> selectors;
170 std::map<sujet<pseudo_element_type>, css> pseudo_elements;
171
172 static std::string get_selector_name(selector_type sel);
173 static std::string get_pseudo_element_name(pseudo_element_type pe);
174};
175
176template<class T> bool css_class::sujet<T>::operator < (const sujet & ref) const
177{
178 if(type < ref.type)
179 return true;
180 else if(type == ref.type)
181 return subcomp < ref.subcomp;
182 else
183 return false;
184}
185
186
187#endif
class css_class gather css attributed under a given label
Definition: css_class.hpp:51
void clear()
clear all css definition, including those provided with selector, only the css name is kept
Definition: css_class.hpp:143
const std::string & get_name() const
get the css_class name
Definition: css_class.hpp:97
void set_selector(selector_type sel, const css &ref, const std::string &descendant="")
defines the value for a given css_selector on that class (or pseudo-class in CSS parlance)
Definition: css_class.cpp:58
void set_value(const css &ref)
defines or overwirte the css_class value from a css object
Definition: css_class.hpp:100
selector_type
selector_type is ordered for :hover being defined after :link and :visited
Definition: css_class.hpp:56
std::string get_definition() const
returns the css class definition
Definition: css_class.cpp:107
void set_pseudo_element(pseudo_element_type pe, const css &val, const std::string &descendant="")
defines the value of a given pseudo-element
Definition: css_class.cpp:83
void change_name(const std::string &newname)
change css_class name
Definition: css_class.hpp:94
void clear_pseudo_element(pseudo_element_type pe, const std::string &descendant="")
remove definition for the given pseudo-element
Definition: css_class.cpp:96
void clear_selector(selector_type sel, const std::string &descendant="")
remove definition for the given selector type
Definition: css_class.cpp:71
void clear_value()
clear css_class value and all selector values
Definition: css_class.hpp:106
const css & get_value() const
obtain the current value
Definition: css_class.hpp:103
class managing Cascading Style Sheets attributes
Definition: css.hpp:48
void clear()
set css attributes to their default
Definition: css.cpp:116