Cogs.Core
css_length.h
1#ifndef LH_CSS_LENGTH_H
2#define LH_CSS_LENGTH_H
3
4#include "types.h"
5
6namespace litehtml
7{
9 {
10 union
11 {
12 float m_value;
13 int m_predef;
14 };
15 css_units m_units;
16 bool m_is_predefined;
17 public:
18 css_length();
19 css_length(const css_length& val);
20
21 css_length& operator=(const css_length& val);
22 css_length& operator=(float val);
23 bool is_predefined() const;
24 void predef(int val);
25 int predef() const;
26 void set_value(float val, css_units units);
27 float val() const;
28 css_units units() const;
29 int calc_percent(int width) const;
30 void fromString(const tstring& str, const tstring& predefs = _t(""), int defValue = 0);
31 };
32
33 // css_length inlines
34
35 inline css_length::css_length()
36 {
37 m_value = 0;
38 m_predef = 0;
39 m_units = css_units_none;
40 m_is_predefined = false;
41 }
42
43 inline css_length::css_length(const css_length& val)
44 {
45 if(val.is_predefined())
46 {
47 m_predef = val.m_predef;
48 } else
49 {
50 m_value = val.m_value;
51 }
52 m_units = val.m_units;
53 m_is_predefined = val.m_is_predefined;
54 }
55
56 inline css_length& css_length::operator=(const css_length& val)
57 {
58 if(val.is_predefined())
59 {
60 m_predef = val.m_predef;
61 } else
62 {
63 m_value = val.m_value;
64 }
65 m_units = val.m_units;
66 m_is_predefined = val.m_is_predefined;
67 return *this;
68 }
69
70 inline css_length& css_length::operator=(float val)
71 {
72 m_value = val;
73 m_units = css_units_px;
74 m_is_predefined = false;
75 return *this;
76 }
77
78 inline bool css_length::is_predefined() const
79 {
80 return m_is_predefined;
81 }
82
83 inline void css_length::predef(int val)
84 {
85 m_predef = val;
86 m_is_predefined = true;
87 }
88
89 inline int css_length::predef() const
90 {
91 if(m_is_predefined)
92 {
93 return m_predef;
94 }
95 return 0;
96 }
97
98 inline void css_length::set_value(float val, css_units units)
99 {
100 m_value = val;
101 m_is_predefined = false;
102 m_units = units;
103 }
104
105 inline float css_length::val() const
106 {
107 if(!m_is_predefined)
108 {
109 return m_value;
110 }
111 return 0;
112 }
113
114 inline css_units css_length::units() const
115 {
116 return m_units;
117 }
118
119 inline int css_length::calc_percent(int width) const
120 {
121 if(!is_predefined())
122 {
123 if(units() == css_units_percentage)
124 {
125 return (int) ((double) width * (double) m_value / 100.0);
126 } else
127 {
128 return (int) val();
129 }
130 }
131 return 0;
132 }
133}
134
135#endif // LH_CSS_LENGTH_H