style_cascade.h
1 /*
2 ** ClanLib SDK
3 ** Copyright (c) 1997-2015 The ClanLib Team
4 **
5 ** This software is provided 'as-is', without any express or implied
6 ** warranty. In no event will the authors be held liable for any damages
7 ** arising from the use of this software.
8 **
9 ** Permission is granted to anyone to use this software for any purpose,
10 ** including commercial applications, and to alter it and redistribute it
11 ** freely, subject to the following restrictions:
12 **
13 ** 1. The origin of this software must not be misrepresented; you must not
14 ** claim that you wrote the original software. If you use this software
15 ** in a product, an acknowledgment in the product documentation would be
16 ** appreciated but is not required.
17 ** 2. Altered source versions must be plainly marked as such, and must not be
18 ** misrepresented as being the original software.
19 ** 3. This notice may not be removed or altered from any source distribution.
20 **
21 ** Note: Some of the libraries ClanLib may link to may have additional
22 ** requirements or restrictions.
23 **
24 ** File Author(s):
25 **
26 ** Magnus Norddahl
27 */
28
29 #pragma once
30
31 #include <memory>
32 #include <string>
33 #include <vector>
34 #include "style_get_value.h"
35
36 namespace clan
37 {
38 class Style;
39 class StyleGetValue;
40 class Canvas;
41 class Font;
42 class ViewGeometry;
43
44 #if defined(MICROSOFT_FINALLY_IMPLEMENTED_CONSTEXPR_TEN_YEARS_AFTER_EVERYONE_ELSE)
45 class PropertyNameConst
47 {
48 public:
49 template<std::size_t Length>
50 constexpr PropertyNameConst(const char(&text)[Length]) : text(text), length(Length - 1) { }
51
52 constexpr char operator[](std::size_t index) const { return index < length ? text[index] : throw std::out_of_range("PropertyNameConst operator[] out of bounds"); }
53 constexpr std::size_t size() const { return length; }
54 constexpr std::size_t hash() const { return hash(2166136261U, 0); }
55
56 private:
57 constexpr std::size_t hash(std::size_t value, std::size_t index) const { return index == length ? value : hash((value ^ (std::size_t)text[index]) * 16777619U, index + 1); }
58
59 const char * const text;
60 const std::size_t length;
61 };
62 #endif
63
65 class StyleCascade
66 {
67 public:
68 StyleCascade() { }
69 StyleCascade(std::vector<Style *> cascade, StyleCascade *parent = nullptr) : cascade(std::move(cascade)), parent(parent) { }
70
72 std::vector<Style *> cascade;
73
75 StyleCascade *parent = nullptr;
76
78 StyleGetValue cascade_value(const char *property_name) const;
79 StyleGetValue cascade_value(const std::string &property_name) const { return cascade_value(property_name.c_str()); }
80
82 StyleGetValue specified_value(const char *property_name) const;
83 StyleGetValue specified_value(const std::string &property_name) const { return specified_value(property_name.c_str()); }
84
88 StyleGetValue computed_value(const char *property_name) const;
89 StyleGetValue computed_value(const std::string &property_name) const { return computed_value(property_name.c_str()); }
90
92 StyleGetValue compute_length(const StyleGetValue &length) const;
93
95 StyleGetValue compute_angle(const StyleGetValue &angle) const;
96
98 StyleGetValue compute_time(const StyleGetValue &time) const;
99
101 StyleGetValue compute_frequency(const StyleGetValue &frequency) const;
102
104 StyleGetValue compute_resolution(const StyleGetValue &resolution) const;
105
107 int array_size(const char *property_name) const;
108 int array_size(const std::string &property_name) const { return array_size(property_name.c_str()); }
109
111 void render_background(Canvas &canvas, const ViewGeometry &geometry) const;
112
114 void render_border(Canvas &canvas, const ViewGeometry &geometry) const;
115
117 Font get_font(Canvas &canvas) const;
118 };
119 }
clan::StyleCascade::compute_length
StyleGetValue compute_length(const StyleGetValue &length) const
Convert length into px (device independent pixel) units.
clan::StyleCascade::get_font
Font get_font(Canvas &canvas) const
Font used by this style cascade.
clan::StyleValueType::time
value is an angle
clan
Definition: clanapp.h:35
clan::StyleCascade::compute_angle
StyleGetValue compute_angle(const StyleGetValue &angle) const
Convert angle into radians.
clan::StyleCascade::compute_resolution
StyleGetValue compute_resolution(const StyleGetValue &resolution) const
Convert resolution to dots per px unit (pixel ratio scale)
clan::Canvas
2D Graphics Canvas
Definition: canvas.h:72
clan::ViewGeometry
Definition: view_geometry.h:38
clan::StyleCascade::StyleCascade
StyleCascade()
Definition: style_cascade.h:68
clan::StyleTokenType::hash
Syntax error.
std
STL namespace.
clan::StyleCascade::compute_time
StyleGetValue compute_time(const StyleGetValue &time) const
Convert time to seconds.
clan::StyleCascade::cascade
std::vector< Style * > cascade
Property sets to be examined.
Definition: style_cascade.h:72
clan::StyleCascade::specified_value
StyleGetValue specified_value(const char *property_name) const
Resolve any inheritance or initial values for the cascade value.
clan::StyleCascade::cascade_value
StyleGetValue cascade_value(const std::string &property_name) const
Definition: style_cascade.h:79
clan::StyleTokenType::string
@ keyword
clan::StyleCascade::compute_frequency
StyleGetValue compute_frequency(const StyleGetValue &frequency) const
Convert frequency to Hz.
clan::StyleCascade::array_size
int array_size(const std::string &property_name) const
Definition: style_cascade.h:108
clan::StyleCascade::array_size
int array_size(const char *property_name) const
Value array size for the property.
clan::StyleCascade::parent
StyleCascade * parent
Parent cascade used for inheritance.
Definition: style_cascade.h:75
clan::StyleValueType::resolution
value is a frequency
clan::StyleValueType::angle
value is a color
clan::StyleCascade::render_border
void render_border(Canvas &canvas, const ViewGeometry &geometry) const
Render styled border.
clan::StyleValueType::length
value is a keyword
clan::StyleGetValue
Style value returned by style classes.
Definition: style_get_value.h:39
clan::StyleCascade::specified_value
StyleGetValue specified_value(const std::string &property_name) const
Definition: style_cascade.h:83
clan::StyleCascade::cascade_value
StyleGetValue cascade_value(const char *property_name) const
Find the first declared value in the cascade for the specified property.
clan::StyleCascade::render_background
void render_background(Canvas &canvas, const ViewGeometry &geometry) const
Render styled background.
clan::Font
Font class.
Definition: font.h:53
clan::StyleCascade
Style value resolver.
Definition: style_cascade.h:65
clan::StyleCascade::StyleCascade
StyleCascade(std::vector< Style * > cascade, StyleCascade *parent=nullptr)
Definition: style_cascade.h:69
clan::PointerEventType::move
Pointer left area.
clan::StyleCascade::computed_value
StyleGetValue computed_value(const std::string &property_name) const
Definition: style_cascade.h:89
clan::StyleValueType::frequency
value is a time
clan::StyleCascade::computed_value
StyleGetValue computed_value(const char *property_name) const