]> git.saurik.com Git - iphone-api.git/blob - WebCore/CSSPrimitiveValue.h
Add support for new WinterBoard Settings features.
[iphone-api.git] / WebCore / CSSPrimitiveValue.h
1 /*
2 * (C) 1999-2003 Lars Knoll (knoll@kde.org)
3 * Copyright (C) 2004, 2005, 2006, 2008 Apple Inc. All rights reserved.
4 * Copyright (C) 2007 Alexey Proskuryakov <ap@webkit.org>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
15 *
16 * You should have received a copy of the GNU Library General Public License
17 * along with this library; see the file COPYING.LIB. If not, write to
18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22 #ifndef CSSPrimitiveValue_h
23 #define CSSPrimitiveValue_h
24
25 #include "CSSValue.h"
26 #include <wtf/PassRefPtr.h>
27
28 namespace WebCore {
29
30 class Counter;
31 class DashboardRegion;
32 class Pair;
33 class Rect;
34 class RenderStyle;
35 class StringImpl;
36
37 struct Length;
38
39 class CSSPrimitiveValue : public CSSValue {
40 public:
41 enum UnitTypes {
42 CSS_UNKNOWN = 0,
43 CSS_NUMBER = 1,
44 CSS_PERCENTAGE = 2,
45 CSS_EMS = 3,
46 CSS_EXS = 4,
47 CSS_PX = 5,
48 CSS_CM = 6,
49 CSS_MM = 7,
50 CSS_IN = 8,
51 CSS_PT = 9,
52 CSS_PC = 10,
53 CSS_DEG = 11,
54 CSS_RAD = 12,
55 CSS_GRAD = 13,
56 CSS_MS = 14,
57 CSS_S = 15,
58 CSS_HZ = 16,
59 CSS_KHZ = 17,
60 CSS_DIMENSION = 18,
61 CSS_STRING = 19,
62 CSS_URI = 20,
63 CSS_IDENT = 21,
64 CSS_ATTR = 22,
65 CSS_COUNTER = 23,
66 CSS_RECT = 24,
67 CSS_RGBCOLOR = 25,
68 CSS_PAIR = 100, // We envision this being exposed as a means of getting computed style values for pairs (border-spacing/radius, background-position, etc.)
69 CSS_DASHBOARD_REGION = 101, // FIXME: Dashboard region should not be a primitive value.
70 CSS_UNICODE_RANGE = 102,
71
72 // These next types are just used internally to allow us to translate back and forth from CSSPrimitiveValues to CSSParserValues.
73 CSS_PARSER_OPERATOR = 103,
74 CSS_PARSER_INTEGER = 104,
75 CSS_PARSER_VARIABLE_FUNCTION_SYNTAX = 105,
76 CSS_PARSER_HEXCOLOR = 106,
77
78 // This is used internally for unknown identifiers
79 CSS_PARSER_IDENTIFIER = 107,
80
81 // This unit is in CSS 3, but that isn't a finished standard yet
82 CSS_TURN = 108
83 };
84
85 static bool isUnitTypeLength(int type) { return type > CSSPrimitiveValue::CSS_PERCENTAGE && type < CSSPrimitiveValue::CSS_DEG; }
86
87 static PassRefPtr<CSSPrimitiveValue> createIdentifier(int ident);
88 static PassRefPtr<CSSPrimitiveValue> createColor(unsigned rgbValue);
89 static PassRefPtr<CSSPrimitiveValue> create(double value, UnitTypes type);
90 static PassRefPtr<CSSPrimitiveValue> create(const String& value, UnitTypes type);
91
92 template<typename T> static PassRefPtr<CSSPrimitiveValue> create(T value)
93 {
94 return adoptRef(new CSSPrimitiveValue(value));
95 }
96
97 virtual ~CSSPrimitiveValue();
98
99 void cleanup();
100
101 unsigned short primitiveType() const { return m_type; }
102
103 bool isVariable() const { return m_type == CSS_PARSER_VARIABLE_FUNCTION_SYNTAX; }
104
105 /*
106 * computes a length in pixels out of the given CSSValue. Need the RenderStyle to get
107 * the fontinfo in case val is defined in em or ex.
108 *
109 * The metrics have to be a bit different for screen and printer output.
110 * For screen output we assume 1 inch == 72 px, for printer we assume 300 dpi
111 *
112 * this is screen/printer dependent, so we probably need a config option for this,
113 * and some tool to calibrate.
114 */
115 int computeLengthInt(RenderStyle*);
116 int computeLengthInt(RenderStyle*, double multiplier);
117 int computeLengthIntForLength(RenderStyle*);
118 int computeLengthIntForLength(RenderStyle*, double multiplier);
119 short computeLengthShort(RenderStyle*);
120 short computeLengthShort(RenderStyle*, double multiplier);
121 float computeLengthFloat(RenderStyle*, bool computingFontSize = false);
122 float computeLengthFloat(RenderStyle*, double multiplier, bool computingFontSize = false);
123 double computeLengthDouble(RenderStyle*, double multiplier = 1.0, bool computingFontSize = false);
124
125 // use with care!!!
126 void setPrimitiveType(unsigned short type) { m_type = type; }
127
128 double getDoubleValue(unsigned short unitType, ExceptionCode&);
129 double getDoubleValue(unsigned short unitType);
130 double getDoubleValue() const { return m_value.num; }
131
132 void setFloatValue(unsigned short unitType, double floatValue, ExceptionCode&);
133 float getFloatValue(unsigned short unitType, ExceptionCode& ec) { return static_cast<float>(getDoubleValue(unitType, ec)); }
134 float getFloatValue(unsigned short unitType) { return static_cast<float>(getDoubleValue(unitType)); }
135 float getFloatValue() const { return static_cast<float>(m_value.num); }
136
137 int getIntValue(unsigned short unitType, ExceptionCode& ec) { return static_cast<int>(getDoubleValue(unitType, ec)); }
138 int getIntValue(unsigned short unitType) { return static_cast<int>(getDoubleValue(unitType)); }
139 int getIntValue() const { return static_cast<int>(m_value.num); }
140
141 void setStringValue(unsigned short stringType, const String& stringValue, ExceptionCode&);
142 String getStringValue(ExceptionCode&) const;
143 String getStringValue() const;
144
145 Counter* getCounterValue(ExceptionCode&) const;
146 Counter* getCounterValue() const { return m_type != CSS_COUNTER ? 0 : m_value.counter; }
147
148 Rect* getRectValue(ExceptionCode&) const;
149 Rect* getRectValue() const { return m_type != CSS_RECT ? 0 : m_value.rect; }
150
151 unsigned getRGBColorValue(ExceptionCode&) const;
152 unsigned getRGBColorValue() const { return m_type != CSS_RGBCOLOR ? 0 : m_value.rgbcolor; }
153
154 Pair* getPairValue(ExceptionCode&) const;
155 Pair* getPairValue() const { return m_type != CSS_PAIR ? 0 : m_value.pair; }
156
157 DashboardRegion* getDashboardRegionValue() const { return m_type != CSS_DASHBOARD_REGION ? 0 : m_value.region; }
158
159 int getIdent();
160 template<typename T> operator T() const; // Defined in CSSPrimitiveValueMappings.h
161
162 virtual bool parseString(const String&, bool = false);
163 virtual String cssText() const;
164
165 virtual bool isQuirkValue() { return false; }
166
167 virtual CSSParserValue parserValue() const;
168
169 virtual void addSubresourceStyleURLs(ListHashSet<KURL>&, const CSSStyleSheet*);
170
171 protected:
172 // FIXME: int vs. unsigned overloading is too subtle to distinguish the color and identifier cases.
173 CSSPrimitiveValue(int ident);
174 CSSPrimitiveValue(double, UnitTypes);
175 CSSPrimitiveValue(const String&, UnitTypes);
176
177 private:
178 CSSPrimitiveValue();
179 CSSPrimitiveValue(unsigned color); // RGB value
180 CSSPrimitiveValue(const Length&);
181
182 template<typename T> CSSPrimitiveValue(T); // Defined in CSSPrimitiveValueMappings.h
183 template<typename T> CSSPrimitiveValue(T* val) { init(PassRefPtr<T>(val)); }
184 template<typename T> CSSPrimitiveValue(PassRefPtr<T> val) { init(val); }
185
186 static void create(int); // compile-time guard
187 static void create(unsigned); // compile-time guard
188 template<typename T> operator T*(); // compile-time guard
189
190 void init(PassRefPtr<Counter>);
191 void init(PassRefPtr<Rect>);
192 void init(PassRefPtr<Pair>);
193 void init(PassRefPtr<DashboardRegion>); // FIXME: Dashboard region should not be a primitive value.
194
195 virtual bool isPrimitiveValue() const { return true; }
196
197 virtual unsigned short cssValueType() const;
198
199 int m_type;
200 union {
201 int ident;
202 double num;
203 StringImpl* string;
204 Counter* counter;
205 Rect* rect;
206 unsigned rgbcolor;
207 Pair* pair;
208 DashboardRegion* region;
209 } m_value;
210 };
211
212 } // namespace WebCore
213
214 #endif // CSSPrimitiveValue_h