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