2009-12-03 Jeffrey Stedfast <fejj@novell.com>
[moon.git] / src / enums.cpp
blob7b08a18327214df3cb73c8e79231ea6b17dd852e
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * enums.cpp: various enumerated types + enum -> str helpers
5 * Copyright 2008 Novell, Inc. (http://www.novell.com)
7 * See the LICENSE file included with the distribution for details.
8 *
9 */
11 #include <config.h>
13 #include <glib.h>
15 #include "enums.h"
17 // XXX enums are contained in these headers and should be moved to
18 // enums.h
19 #include "brush.h"
20 #include "clock.h"
21 #include "stylus.h"
23 static GHashTable *enum_map = NULL;
25 #define MAP_ENUM_FULL(n,v) { (n), (v) }
27 // use this when the name is the same as the quoted value
28 #define MAP_NAME(n) MAP_ENUM_FULL(#n, n)
30 // use these when the name is the same as the quoted value, but with the enum type prefixed
31 #define MAP_ENUM(t,n) MAP_ENUM_FULL(#n, t##n)
33 #define END_MAPPING { NULL, 0 }
35 typedef struct {
36 const char *name;
37 int value;
38 } enum_map_t;
40 static enum_map_t alignment_x_map [] = {
41 MAP_ENUM (AlignmentX, Left),
42 MAP_ENUM (AlignmentX, Center),
43 MAP_ENUM (AlignmentX, Right),
44 END_MAPPING
47 static enum_map_t alignment_y_map [] = {
48 MAP_ENUM (AlignmentY, Top),
49 MAP_ENUM (AlignmentY, Center),
50 MAP_ENUM (AlignmentY, Bottom),
51 END_MAPPING
54 static enum_map_t binding_mode_map [] = {
55 MAP_ENUM (BindingMode, OneWay),
56 MAP_ENUM (BindingMode, OneTime),
57 MAP_ENUM (BindingMode, TwoWay),
58 END_MAPPING
61 static enum_map_t brush_mapping_mode_map [] = {
62 MAP_ENUM (BrushMappingMode, Absolute),
63 MAP_ENUM (BrushMappingMode, RelativeToBoundingBox),
64 END_MAPPING
67 static enum_map_t color_interpolation_mode_map [] = {
68 MAP_ENUM (ColorInterpolationMode, ScRgbLinearInterpolation),
69 MAP_ENUM (ColorInterpolationMode, SRgbLinearInterpolation),
70 END_MAPPING
73 static enum_map_t cursors_map [] = {
74 MAP_ENUM (MouseCursor, Default),
75 MAP_ENUM (MouseCursor, Arrow),
76 MAP_ENUM (MouseCursor, Hand),
77 MAP_ENUM (MouseCursor, Wait),
78 MAP_ENUM (MouseCursor, IBeam),
79 MAP_ENUM (MouseCursor, Stylus),
80 MAP_ENUM (MouseCursor, Eraser),
81 MAP_ENUM (MouseCursor, SizeNS),
82 MAP_ENUM (MouseCursor, SizeWE),
83 MAP_ENUM (MouseCursor, None),
84 END_MAPPING
87 static enum_map_t error_type_map [] = {
88 MAP_NAME (NoError),
89 MAP_NAME (UnknownError),
90 MAP_NAME (InitializeError),
91 MAP_NAME (ParserError),
92 MAP_NAME (ObjectModelError),
93 MAP_NAME (RuntimeError),
94 MAP_NAME (DownloadError),
95 MAP_NAME (MediaError),
96 MAP_NAME (ImageError),
97 END_MAPPING
100 static enum_map_t fill_behavior_map [] = {
101 MAP_ENUM (FillBehavior, HoldEnd),
102 MAP_ENUM (FillBehavior, Stop),
103 END_MAPPING
106 static enum_map_t fill_rule_map [] = {
107 MAP_ENUM (FillRule, EvenOdd),
108 MAP_ENUM (FillRule, Nonzero),
109 END_MAPPING
112 static enum_map_t font_stretches_map [] = {
113 MAP_ENUM (FontStretches, UltraCondensed),
114 MAP_ENUM (FontStretches, ExtraCondensed),
115 MAP_ENUM (FontStretches, Condensed),
116 MAP_ENUM (FontStretches, SemiCondensed),
117 MAP_ENUM (FontStretches, Normal),
118 MAP_ENUM (FontStretches, Medium),
119 MAP_ENUM (FontStretches, SemiExpanded),
120 MAP_ENUM (FontStretches, Expanded),
121 MAP_ENUM (FontStretches, ExtraExpanded),
122 MAP_ENUM (FontStretches, UltraExpanded),
123 END_MAPPING
126 static enum_map_t font_styles_map [] = {
127 MAP_ENUM (FontStyles, Normal),
128 MAP_ENUM (FontStyles, Oblique),
129 MAP_ENUM (FontStyles, Italic),
130 END_MAPPING
133 static enum_map_t font_weights_map [] = {
134 MAP_ENUM (FontWeights, Thin),
135 MAP_ENUM (FontWeights, ExtraLight),
136 MAP_ENUM_FULL ("UltraLight", FontWeightsExtraLight), // deprecated as of July 2007
137 MAP_ENUM (FontWeights, Light),
138 MAP_ENUM (FontWeights, Normal),
139 MAP_ENUM_FULL ("Regular", FontWeightsNormal), // deprecated as of July 2007
140 MAP_ENUM (FontWeights, Medium),
141 MAP_ENUM (FontWeights, SemiBold),
142 MAP_ENUM_FULL ("DemiBold", FontWeightsSemiBold), // deprecated as of July 2007
143 MAP_ENUM (FontWeights, Bold),
144 MAP_ENUM (FontWeights, ExtraBold),
145 MAP_ENUM_FULL ("UltraBold", FontWeightsExtraBold), // deprecated as of July 2007
146 MAP_ENUM (FontWeights, Black),
147 MAP_ENUM_FULL ("Heavy", FontWeightsBlack), // deprecated as of July 2007
148 MAP_ENUM (FontWeights, ExtraBlack),
149 MAP_ENUM_FULL ("UltraBlack", FontWeightsExtraBlack), // deprecated as of July 2007
150 END_MAPPING
153 static enum_map_t line_stacking_strategy_map [] = {
154 MAP_ENUM (LineStackingStrategy, MaxHeight),
155 MAP_ENUM (LineStackingStrategy, BlockLineHeight),
156 END_MAPPING
159 static enum_map_t horizontal_alignment_map [] = {
160 MAP_ENUM (HorizontalAlignment, Left),
161 MAP_ENUM (HorizontalAlignment, Center),
162 MAP_ENUM (HorizontalAlignment, Right),
163 MAP_ENUM (HorizontalAlignment, Stretch),
164 END_MAPPING
167 static enum_map_t gradient_spread_method_map [] = {
168 MAP_ENUM (GradientSpreadMethod, Pad),
169 MAP_ENUM (GradientSpreadMethod, Reflect),
170 MAP_ENUM (GradientSpreadMethod, Repeat),
171 END_MAPPING
174 static enum_map_t keyboard_navigation_mode_map [] = {
175 MAP_ENUM (KeyboardNavigationMode, Local),
176 MAP_ENUM (KeyboardNavigationMode, Cycle),
177 MAP_ENUM (KeyboardNavigationMode, Once),
178 END_MAPPING
181 static enum_map_t media_element_state_map [] = {
182 MAP_ENUM (MediaState, Closed),
183 MAP_ENUM (MediaState, Opening),
184 MAP_ENUM (MediaState, Buffering),
185 MAP_ENUM (MediaState, Playing),
186 MAP_ENUM (MediaState, Paused),
187 MAP_ENUM (MediaState, Stopped),
188 MAP_ENUM (MediaState, Individualizing),
189 MAP_ENUM (MediaState, AcquiringLicense),
190 END_MAPPING
193 static enum_map_t pen_line_cap_map [] = {
194 MAP_ENUM (PenLineCap, Flat),
195 MAP_ENUM (PenLineCap, Square),
196 MAP_ENUM (PenLineCap, Round),
197 MAP_ENUM (PenLineCap, Triangle),
198 END_MAPPING
201 static enum_map_t pen_line_join_map [] = {
202 MAP_ENUM (PenLineJoin, Miter),
203 MAP_ENUM (PenLineJoin, Bevel),
204 MAP_ENUM (PenLineJoin, Round),
205 END_MAPPING
208 static enum_map_t stretch_map [] = {
209 MAP_ENUM (Stretch, None),
210 MAP_ENUM (Stretch, Fill),
211 MAP_ENUM (Stretch, Uniform),
212 MAP_ENUM (Stretch, UniformToFill),
213 END_MAPPING
216 static enum_map_t style_simulations_map [] = {
217 MAP_ENUM (StyleSimulations, None),
218 MAP_ENUM_FULL ("BoldSimulation", StyleSimulationsBold),
219 MAP_ENUM_FULL ("ItalicSimulation", StyleSimulationsItalic),
220 MAP_ENUM_FULL ("BoldItalicSimulation", StyleSimulationsBoldItalic),
221 END_MAPPING
224 static enum_map_t sweep_direction_map [] = {
225 MAP_ENUM (SweepDirection, Counterclockwise),
226 MAP_ENUM (SweepDirection, Clockwise),
227 END_MAPPING
230 static enum_map_t tablet_device_type_map [] = {
231 MAP_ENUM (TabletDeviceType, Mouse),
232 MAP_ENUM (TabletDeviceType, Stylus),
233 MAP_ENUM (TabletDeviceType, Touch),
234 END_MAPPING
237 static enum_map_t text_alignment_map [] = {
238 MAP_ENUM (TextAlignment, Center),
239 MAP_ENUM (TextAlignment, Left),
240 MAP_ENUM (TextAlignment, Right),
241 END_MAPPING
244 static enum_map_t text_decorations_map [] = {
245 MAP_ENUM (TextDecorations, None),
246 MAP_ENUM (TextDecorations, Underline),
247 END_MAPPING
250 static enum_map_t text_wrapping_map [] = {
251 MAP_ENUM (TextWrapping, Wrap),
252 MAP_ENUM (TextWrapping, NoWrap),
253 MAP_ENUM (TextWrapping, WrapWithOverflow),
254 END_MAPPING
257 static enum_map_t scrollbar_visibility_map [] = {
258 MAP_ENUM (ScrollBarVisibility, Disabled),
259 MAP_ENUM (ScrollBarVisibility, Auto),
260 MAP_ENUM (ScrollBarVisibility, Hidden),
261 MAP_ENUM (ScrollBarVisibility, Visible),
262 END_MAPPING
265 static enum_map_t vertical_alignment_map [] = {
266 MAP_ENUM (VerticalAlignment, Top),
267 MAP_ENUM (VerticalAlignment, Center),
268 MAP_ENUM (VerticalAlignment, Bottom),
269 MAP_ENUM (VerticalAlignment, Stretch),
270 END_MAPPING
273 static enum_map_t visibility_map [] = {
274 MAP_ENUM (Visibility, Visible),
275 MAP_ENUM (Visibility, Collapsed),
276 END_MAPPING
279 static enum_map_t orientation_map [] = {
280 MAP_ENUM (Orientation, Vertical),
281 MAP_ENUM (Orientation, Horizontal),
282 END_MAPPING
285 static enum_map_t cross_domain_access_map [] = {
286 MAP_ENUM (CrossDomainAccess, NoAccess),
287 MAP_ENUM (CrossDomainAccess, ScriptableOnly),
288 END_MAPPING
291 static enum_map_t grid_unit_type_map [] = {
292 MAP_ENUM (GridUnitType, Auto),
293 MAP_ENUM (GridUnitType, Pixel),
294 MAP_ENUM (GridUnitType, Star),
295 END_MAPPING
298 static enum_map_t easing_mode_map [] = {
299 MAP_ENUM_FULL ("EaseIn", EasingModeIn),
300 MAP_ENUM_FULL ("EaseOut", EasingModeOut),
301 MAP_ENUM_FULL ("EaseInOut", EasingModeInOut),
302 END_MAPPING
305 static enum_map_t log_source_map [] = {
306 MAP_ENUM_FULL ("RequestLog", LogSourceRequestLog),
307 MAP_ENUM_FULL ("Stop", LogSourceStop),
308 MAP_ENUM_FULL ("Seek", LogSourceSeek),
309 MAP_ENUM_FULL ("Pause", LogSourcePause),
310 MAP_ENUM_FULL ("SourceChanged", LogSourceSourceChanged),
311 MAP_ENUM_FULL ("EndOfStream", LogSourceEndOfStream),
312 MAP_ENUM_FULL ("MediaElementShutdown", LogSourceMediaElementShutdown),
313 MAP_ENUM_FULL ("RuntimeShutdown", LogSourceRuntimeShutdown),
314 END_MAPPING
317 static enum_map_t text_hinting_mode_map [] = {
318 MAP_ENUM (TextHintingMode, Fixed),
319 MAP_ENUM (TextHintingMode, Animated),
322 static void
323 initialize_enums (void)
325 enum_map = g_hash_table_new (g_str_hash, g_str_equal);
327 g_hash_table_insert (enum_map, (char *) "AlignmentX", alignment_x_map);
328 g_hash_table_insert (enum_map, (char *) "AlignmentY", alignment_y_map);
329 g_hash_table_insert (enum_map, (char *) "MappingMode", brush_mapping_mode_map);
330 g_hash_table_insert (enum_map, (char *) "ColorInterpolationMode", color_interpolation_mode_map);
331 g_hash_table_insert (enum_map, (char *) "Cursor", cursors_map);
332 g_hash_table_insert (enum_map, (char *) "ErrorType", error_type_map);
333 g_hash_table_insert (enum_map, (char *) "FillBehavior", fill_behavior_map);
334 g_hash_table_insert (enum_map, (char *) "FillRule", fill_rule_map);
335 g_hash_table_insert (enum_map, (char *) "FontStretch", font_stretches_map);
336 g_hash_table_insert (enum_map, (char *) "FontStyle", font_styles_map);
337 g_hash_table_insert (enum_map, (char *) "FontWeight", font_weights_map);
338 g_hash_table_insert (enum_map, (char *) "SpreadMethod", gradient_spread_method_map);
340 g_hash_table_insert (enum_map, (char *) "StrokeDashCap", pen_line_cap_map);
341 g_hash_table_insert (enum_map, (char *) "StrokeStartLineCap", pen_line_cap_map);
342 g_hash_table_insert (enum_map, (char *) "StrokeEndLineCap", pen_line_cap_map);
344 g_hash_table_insert (enum_map, (char *) "StrokeLineJoin", pen_line_join_map);
345 g_hash_table_insert (enum_map, (char *) "Stretch", stretch_map);
346 g_hash_table_insert (enum_map, (char *) "StyleSimulations", style_simulations_map);
347 g_hash_table_insert (enum_map, (char *) "SweepDirection", sweep_direction_map);
348 g_hash_table_insert (enum_map, (char *) "DeviceType", tablet_device_type_map);
349 g_hash_table_insert (enum_map, (char *) "TextDecorations", text_decorations_map);
350 g_hash_table_insert (enum_map, (char *) "TextWrapping", text_wrapping_map);
351 g_hash_table_insert (enum_map, (char *) "Visibility", visibility_map);
353 g_hash_table_insert (enum_map, (char *) "BindingMode", binding_mode_map);
354 g_hash_table_insert (enum_map, (char *) "ExternalCallersFromCrossDomain", cross_domain_access_map);
355 g_hash_table_insert (enum_map, (char *) "HorizontalScrollBarVisibility", scrollbar_visibility_map);
356 g_hash_table_insert (enum_map, (char *) "VerticalScrollBarVisibility", scrollbar_visibility_map);
357 g_hash_table_insert (enum_map, (char *) "LineStackingStrategy", line_stacking_strategy_map);
358 g_hash_table_insert (enum_map, (char *) "HorizontalAlignment", horizontal_alignment_map);
359 g_hash_table_insert (enum_map, (char *) "HorizontalContentAlignment", horizontal_alignment_map);
360 g_hash_table_insert (enum_map, (char *) "VerticalAlignment", vertical_alignment_map);
361 g_hash_table_insert (enum_map, (char *) "VerticalContentAlignment", vertical_alignment_map);
362 g_hash_table_insert (enum_map, (char *) "TextAlignment", text_alignment_map);
363 g_hash_table_insert (enum_map, (char *) "Orientation", orientation_map);
365 g_hash_table_insert (enum_map, (char *) "TabNavigation", keyboard_navigation_mode_map);
367 g_hash_table_insert (enum_map, (char *) "MediaState", media_element_state_map);
368 g_hash_table_insert (enum_map, (char *) "GridUnitType", grid_unit_type_map);
370 g_hash_table_insert (enum_map, (char *) "EasingMode", easing_mode_map);
372 g_hash_table_insert (enum_map, (char *) "LogSource", log_source_map);
374 g_hash_table_insert (enum_map, (char *) "TextHintingMode", text_hinting_mode_map);
377 static int
378 enum_from_str (const enum_map_t *emu, const char *str)
380 int i;
381 for (i = 0; emu [i].name; i++) {
382 if (!g_ascii_strcasecmp (emu [i].name, str))
383 return emu [i].value;
386 return -1;
389 static const char*
390 str_from_enum (const enum_map_t *emu, int e)
392 int i;
393 for (i = 0; emu [i].name; i++) {
394 if (emu [i].value == e)
395 return emu [i].name;
398 return NULL;
402 // Converts a string representing an enum (ie. "FillEnd") to int.
403 // Returns -1 if no property or enum found.
405 int
406 enums_str_to_int (const char *prop_name, const char *str)
408 if (enum_map == NULL)
409 initialize_enums ();
411 enum_map_t *emu = (enum_map_t *) g_hash_table_lookup (enum_map, prop_name);
412 if (! emu)
413 return -1;
415 return enum_from_str (emu, str);
419 // Converts an int enum to a string representation.
420 // Returns NULL if no match found.
422 const char *
423 enums_int_to_str (const char *prop_name, int e)
425 if (enum_map == NULL)
426 initialize_enums ();
428 enum_map_t *emu = (enum_map_t *) g_hash_table_lookup (enum_map, prop_name);
429 if (! emu)
430 return NULL;
432 return str_from_enum (emu, e);
435 bool
436 enums_is_enum_name (const char *enum_name)
438 if (enum_map == NULL)
439 initialize_enums ();
441 return g_hash_table_lookup (enum_map, enum_name) != NULL;