~ubuntu-branches/ubuntu/natty/moon/natty

« back to all changes in this revision

Viewing changes to src/enums.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2009-02-14 12:01:08 UTC
  • Revision ID: james.westby@ubuntu.com-20090214120108-06539vb25vhbd8bn
Tags: upstream-1.0
ImportĀ upstreamĀ versionĀ 1.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * enums.cpp: various enumerated types + enum -> str helpers
 
3
 *
 
4
 * Copyright 2008 Novell, Inc. (http://www.novell.com)
 
5
 *
 
6
 * See the LICENSE file included with the distribution for details.
 
7
 * 
 
8
 */
 
9
 
 
10
#include <config.h>
 
11
#include <stdlib.h>
 
12
#include <glib.h>
 
13
#include "enums.h"
 
14
 
 
15
// XXX enums are contained in these headers and should be moved to
 
16
// enums.h
 
17
#include "brush.h"
 
18
#include "clock.h"
 
19
#include "stylus.h"
 
20
 
 
21
static GHashTable *enum_map = NULL;
 
22
 
 
23
#define MAPPING_FLAG_SL1 0x01
 
24
#define MAPPING_FLAG_SL2 0x02
 
25
 
 
26
#define MAP_ENUM_FULL(n,v,f) { (n), (v), (f) }
 
27
 
 
28
// use this when the name is the same as the quoted value
 
29
#define MAP_NAME(n)          MAP_ENUM_FULL(#n, n, 0)
 
30
 
 
31
// use these when the name is the same as the quoted value, but with the enum type prefixed
 
32
#define MAP_ENUM(t,n)        MAP_ENUM_FULL(#n, t##n, 0)
 
33
#define MAP_ENUM_SL1(t,n)    MAP_ENUM_FULL(#n, t##n, MAPPING_FLAG_SL1)
 
34
#define MAP_ENUM_SL2(t,n)    MAP_ENUM_FULL(#n, t##n, MAPPING_FLAG_SL2)
 
35
 
 
36
#define END_MAPPING          { NULL, 0, 0 }
 
37
 
 
38
typedef struct {
 
39
        const char *name;
 
40
        int value;
 
41
        int flags;
 
42
} enum_map_t;
 
43
 
 
44
static enum_map_t alignment_x_map [] = {
 
45
        MAP_ENUM (AlignmentX, Left),
 
46
        MAP_ENUM (AlignmentX, Center),
 
47
        MAP_ENUM (AlignmentX, Right),
 
48
        END_MAPPING
 
49
};
 
50
 
 
51
static enum_map_t alignment_y_map [] = {
 
52
        MAP_ENUM (AlignmentY, Top),
 
53
        MAP_ENUM (AlignmentY, Center),
 
54
        MAP_ENUM (AlignmentY, Bottom),
 
55
        END_MAPPING
 
56
};
 
57
 
 
58
static enum_map_t brush_mapping_mode_map [] = {
 
59
        MAP_ENUM (BrushMappingMode, Absolute),
 
60
        MAP_ENUM (BrushMappingMode, RelativeToBoundingBox),
 
61
        END_MAPPING
 
62
};
 
63
 
 
64
static enum_map_t color_interpolation_mode_map [] = {
 
65
        MAP_ENUM (ColorInterpolationMode, ScRgbLinearInterpolation),
 
66
        MAP_ENUM (ColorInterpolationMode, SRgbLinearInterpolation),
 
67
        END_MAPPING
 
68
};
 
69
 
 
70
static enum_map_t cursors_map [] = {
 
71
        MAP_ENUM (MouseCursor, Default),
 
72
        MAP_ENUM (MouseCursor, Arrow),
 
73
        MAP_ENUM (MouseCursor, Hand),
 
74
        MAP_ENUM (MouseCursor, Wait),
 
75
        MAP_ENUM (MouseCursor, IBeam),
 
76
        MAP_ENUM (MouseCursor, Stylus),
 
77
        MAP_ENUM (MouseCursor, Eraser),
 
78
        MAP_ENUM (MouseCursor, None),
 
79
        END_MAPPING
 
80
};
 
81
 
 
82
static enum_map_t error_type_map [] = {
 
83
        MAP_NAME (NoError),
 
84
        MAP_NAME (UnknownError),
 
85
        MAP_NAME (InitializeError),
 
86
        MAP_NAME (ParserError),
 
87
        MAP_NAME (ObjectModelError),
 
88
        MAP_NAME (RuntimeError),
 
89
        MAP_NAME (DownloadError),
 
90
        MAP_NAME (MediaError),
 
91
        MAP_NAME (ImageError),
 
92
        END_MAPPING
 
93
};
 
94
 
 
95
static enum_map_t fill_behavior_map [] = {
 
96
        MAP_ENUM (FillBehavior, HoldEnd),
 
97
        MAP_ENUM (FillBehavior, Stop),
 
98
        END_MAPPING
 
99
};
 
100
 
 
101
static enum_map_t fill_rule_map [] = {
 
102
        MAP_ENUM (FillRule, EvenOdd),
 
103
        MAP_ENUM (FillRule, Nonzero),
 
104
        END_MAPPING
 
105
};
 
106
 
 
107
static enum_map_t font_stretches_map [] = {
 
108
        MAP_ENUM (FontStretches, UltraCondensed),
 
109
        MAP_ENUM (FontStretches, ExtraCondensed),
 
110
        MAP_ENUM (FontStretches, Condensed),
 
111
        MAP_ENUM (FontStretches, SemiCondensed),
 
112
        MAP_ENUM (FontStretches, Normal),
 
113
        MAP_ENUM (FontStretches, Medium),
 
114
        MAP_ENUM (FontStretches, SemiExpanded),
 
115
        MAP_ENUM (FontStretches, Expanded),
 
116
        MAP_ENUM (FontStretches, ExtraExpanded),
 
117
        MAP_ENUM (FontStretches, UltraExpanded),
 
118
        END_MAPPING
 
119
};
 
120
 
 
121
static enum_map_t font_styles_map [] = {
 
122
        MAP_ENUM (FontStyles, Normal),
 
123
        MAP_ENUM (FontStyles, Oblique),
 
124
        MAP_ENUM (FontStyles, Italic),
 
125
        END_MAPPING
 
126
};
 
127
 
 
128
static enum_map_t font_weights_map [] = {
 
129
        MAP_ENUM (FontWeights, Thin),
 
130
        MAP_ENUM (FontWeights, ExtraLight),
 
131
        MAP_ENUM_FULL ("UltraLight", FontWeightsExtraLight,0),  // deprecated as of July 2007 
 
132
        MAP_ENUM (FontWeights, Light),
 
133
        MAP_ENUM (FontWeights, Normal),
 
134
        MAP_ENUM_FULL ("Regular", FontWeightsNormal,0),         // deprecated as of July 2007 
 
135
        MAP_ENUM (FontWeights, Medium),
 
136
        MAP_ENUM (FontWeights, SemiBold),
 
137
        MAP_ENUM_FULL ("DemiBold", FontWeightsSemiBold,0),      // deprecated as of July 2007 
 
138
        MAP_ENUM (FontWeights, Bold),
 
139
        MAP_ENUM (FontWeights, ExtraBold),
 
140
        MAP_ENUM_FULL ("UltraBold", FontWeightsExtraBold,0),    // deprecated as of July 2007 
 
141
        MAP_ENUM (FontWeights, Black),
 
142
        MAP_ENUM_FULL ("Heavy", FontWeightsBlack,0),            // deprecated as of July 2007 
 
143
        MAP_ENUM (FontWeights, ExtraBlack),
 
144
        MAP_ENUM_FULL ("UltraBlack", FontWeightsExtraBlack,0),  // deprecated as of July 2007 
 
145
        END_MAPPING
 
146
};
 
147
 
 
148
#if SL_2_0
 
149
static enum_map_t line_stacking_strategy_map [] = {
 
150
        MAP_ENUM_SL2 (LineStackingStrategy, MaxHeight),
 
151
        MAP_ENUM_SL2 (LineStackingStrategy, BlockLineHeight),
 
152
        END_MAPPING
 
153
};
 
154
#endif
 
155
 
 
156
#if SL_2_0
 
157
static enum_map_t horizontal_alignment_map [] = {
 
158
        MAP_ENUM_SL2 (HorizontalAlignment, Left),
 
159
        MAP_ENUM_SL2 (HorizontalAlignment, Center),
 
160
        MAP_ENUM_SL2 (HorizontalAlignment, Right),
 
161
        MAP_ENUM_SL2 (HorizontalAlignment, Stretch),
 
162
        END_MAPPING
 
163
};
 
164
#endif
 
165
 
 
166
static enum_map_t gradient_spread_method_map [] = {
 
167
        MAP_ENUM (GradientSpreadMethod, Pad),
 
168
        MAP_ENUM (GradientSpreadMethod, Reflect),
 
169
        MAP_ENUM (GradientSpreadMethod, Repeat),
 
170
        END_MAPPING
 
171
};
 
172
 
 
173
static enum_map_t pen_line_cap_map [] = {
 
174
        MAP_ENUM (PenLineCap, Flat),
 
175
        MAP_ENUM (PenLineCap, Square),
 
176
        MAP_ENUM (PenLineCap, Round),
 
177
        MAP_ENUM (PenLineCap, Triangle),
 
178
        END_MAPPING
 
179
};
 
180
 
 
181
static enum_map_t pen_line_join_map [] = {
 
182
        MAP_ENUM (PenLineJoin, Miter),
 
183
        MAP_ENUM (PenLineJoin, Bevel),
 
184
        MAP_ENUM (PenLineJoin, Round),
 
185
        END_MAPPING
 
186
};
 
187
 
 
188
static enum_map_t stretch_map [] = {
 
189
        MAP_ENUM (Stretch, None),
 
190
        MAP_ENUM (Stretch, Fill),
 
191
        MAP_ENUM (Stretch, Uniform),
 
192
        MAP_ENUM (Stretch, UniformToFill),
 
193
        END_MAPPING
 
194
};
 
195
 
 
196
static enum_map_t style_simulations_map [] = {
 
197
        MAP_ENUM (StyleSimulations, None),
 
198
        END_MAPPING
 
199
};
 
200
 
 
201
static enum_map_t sweep_direction_map [] = {
 
202
        MAP_ENUM (SweepDirection, Counterclockwise),
 
203
        MAP_ENUM (SweepDirection, Clockwise),
 
204
        END_MAPPING
 
205
};
 
206
 
 
207
static enum_map_t tablet_device_type_map [] = {
 
208
        MAP_ENUM (TabletDeviceType, Mouse),
 
209
        MAP_ENUM (TabletDeviceType, Stylus),
 
210
        MAP_ENUM (TabletDeviceType, Touch),
 
211
        END_MAPPING
 
212
};
 
213
 
 
214
#if SL_2_0
 
215
static enum_map_t text_alignment_map [] = {
 
216
        MAP_ENUM_SL2 (TextAlignment, Center),
 
217
        MAP_ENUM_SL2 (TextAlignment, Left),
 
218
        MAP_ENUM_SL2 (TextAlignment, Right),
 
219
        END_MAPPING
 
220
};
 
221
#endif
 
222
 
 
223
static enum_map_t text_decorations_map [] = {
 
224
        MAP_ENUM (TextDecorations, None),
 
225
        MAP_ENUM (TextDecorations, Underline),
 
226
        END_MAPPING
 
227
};
 
228
 
 
229
static enum_map_t text_wrapping_map [] = {
 
230
        MAP_ENUM (TextWrapping, Wrap),
 
231
        MAP_ENUM (TextWrapping, NoWrap),
 
232
        MAP_ENUM (TextWrapping, WrapWithOverflow),
 
233
        END_MAPPING
 
234
};
 
235
 
 
236
#if SL_2_0
 
237
static enum_map_t scrollbar_visibility_map [] = {
 
238
        MAP_ENUM_SL2 (ScrollBarVisibility, Disabled),
 
239
        MAP_ENUM_SL2 (ScrollBarVisibility, Auto),
 
240
        MAP_ENUM_SL2 (ScrollBarVisibility, Hidden),
 
241
        MAP_ENUM_SL2 (ScrollBarVisibility, Visible),
 
242
        END_MAPPING
 
243
};
 
244
#endif
 
245
 
 
246
#if SL_2_0
 
247
static enum_map_t vertical_alignment_map [] = {
 
248
        MAP_ENUM_SL2 (VerticalAlignment, Top),
 
249
        MAP_ENUM_SL2 (VerticalAlignment, Center),
 
250
        MAP_ENUM_SL2 (VerticalAlignment, Bottom),
 
251
        MAP_ENUM_SL2 (VerticalAlignment, Stretch),
 
252
        END_MAPPING
 
253
};
 
254
#endif
 
255
 
 
256
static enum_map_t visibility_map [] = {
 
257
        MAP_ENUM (Visibility, Visible),
 
258
        MAP_ENUM (Visibility, Collapsed),
 
259
        END_MAPPING
 
260
};
 
261
 
 
262
#if SL_2_0
 
263
static enum_map_t orientation_map [] = {
 
264
        MAP_ENUM_SL2 (Orientation, Vertical),
 
265
        MAP_ENUM_SL2 (Orientation, Horizontal),
 
266
        END_MAPPING
 
267
};
 
268
 
 
269
static enum_map_t cross_domain_access_map [] = {
 
270
        MAP_ENUM_SL2 (CrossDomainAccess, NoAccess), 
 
271
        MAP_ENUM_SL2 (CrossDomainAccess, FullAccess), 
 
272
        MAP_ENUM_SL2 (CrossDomainAccess, ScriptableOnly),
 
273
        END_MAPPING 
 
274
};
 
275
#endif
 
276
 
 
277
static void
 
278
initialize_enums (void)
 
279
{
 
280
        enum_map = g_hash_table_new (g_str_hash, g_str_equal);
 
281
        
 
282
        g_hash_table_insert (enum_map, (char *) "AlignmentX", alignment_x_map);
 
283
        g_hash_table_insert (enum_map, (char *) "AlignmentY", alignment_y_map);
 
284
        g_hash_table_insert (enum_map, (char *) "MappingMode", brush_mapping_mode_map);
 
285
        g_hash_table_insert (enum_map, (char *) "ColorInterpolationMode", color_interpolation_mode_map);
 
286
        g_hash_table_insert (enum_map, (char *) "Cursor", cursors_map);
 
287
        g_hash_table_insert (enum_map, (char *) "ErrorType", error_type_map);
 
288
        g_hash_table_insert (enum_map, (char *) "FillBehavior", fill_behavior_map);
 
289
        g_hash_table_insert (enum_map, (char *) "FillRule", fill_rule_map);
 
290
        g_hash_table_insert (enum_map, (char *) "FontStretch", font_stretches_map);
 
291
        g_hash_table_insert (enum_map, (char *) "FontStyle", font_styles_map);
 
292
        g_hash_table_insert (enum_map, (char *) "FontWeight", font_weights_map);
 
293
        g_hash_table_insert (enum_map, (char *) "SpreadMethod", gradient_spread_method_map);
 
294
        
 
295
        g_hash_table_insert (enum_map, (char *) "StrokeDashCap", pen_line_cap_map);
 
296
        g_hash_table_insert (enum_map, (char *) "StrokeStartLineCap", pen_line_cap_map);
 
297
        g_hash_table_insert (enum_map, (char *) "StrokeEndLineCap", pen_line_cap_map);
 
298
        
 
299
        g_hash_table_insert (enum_map, (char *) "StrokeLineJoin", pen_line_join_map);
 
300
        g_hash_table_insert (enum_map, (char *) "Stretch", stretch_map);
 
301
        g_hash_table_insert (enum_map, (char *) "StyleSimulations", style_simulations_map);
 
302
        g_hash_table_insert (enum_map, (char *) "SweepDirection", sweep_direction_map);
 
303
        g_hash_table_insert (enum_map, (char *) "DeviceType", tablet_device_type_map);
 
304
        g_hash_table_insert (enum_map, (char *) "TextDecorations", text_decorations_map);
 
305
        g_hash_table_insert (enum_map, (char *) "TextWrapping", text_wrapping_map);
 
306
        g_hash_table_insert (enum_map, (char *) "Visibility", visibility_map);
 
307
 
 
308
#if SL_2_0
 
309
        g_hash_table_insert (enum_map, (char *) "ExternalCallersFromCrossDomain", cross_domain_access_map);
 
310
        g_hash_table_insert (enum_map, (char *) "HorizontalScrollBarVisibility", scrollbar_visibility_map);
 
311
        g_hash_table_insert (enum_map, (char *) "VerticalScrollBarVisibility", scrollbar_visibility_map);
 
312
        g_hash_table_insert (enum_map, (char *) "LineStackingStrategy", line_stacking_strategy_map);
 
313
        g_hash_table_insert (enum_map, (char *) "HorizontalAlignment", horizontal_alignment_map);
 
314
        g_hash_table_insert (enum_map, (char *) "VerticalAlignment", vertical_alignment_map);
 
315
        g_hash_table_insert (enum_map, (char *) "TextAlignment", text_alignment_map);
 
316
        g_hash_table_insert (enum_map, (char *) "Orientation", orientation_map);
 
317
#endif
 
318
}
 
319
 
 
320
static int
 
321
enum_from_str (const enum_map_t *emu, const char *str)
 
322
{
 
323
        int i;
 
324
        for (i = 0; emu [i].name; i++) {
 
325
                if (!g_strcasecmp (emu [i].name, str))
 
326
                        return emu [i].value;
 
327
        }
 
328
 
 
329
        return -1;
 
330
}
 
331
 
 
332
static const char*
 
333
str_from_enum (const enum_map_t *emu, int e, bool sl2)
 
334
{
 
335
        int i;
 
336
        for (i = 0; emu [i].name; i++) {
 
337
                if (emu [i].value == e) {
 
338
                        if (emu[i].flags != 0 // 0 means everything
 
339
                            &&
 
340
                            ((sl2 && ((emu[i].flags & MAPPING_FLAG_SL2) == 0)) ||
 
341
                             (!sl2 && ((emu[i].flags & MAPPING_FLAG_SL1) == 0))))
 
342
                                continue;
 
343
 
 
344
                        return emu [i].name;
 
345
                }
 
346
        }
 
347
 
 
348
        return NULL;
 
349
}
 
350
 
 
351
//
 
352
// Converts a string representing an enum (ie. "FillEnd") to int.
 
353
// Returns -1 if no property or enum found.
 
354
//
 
355
int 
 
356
enums_str_to_int (const char *prop_name, const char *str, bool sl2)
 
357
{
 
358
        if (enum_map == NULL)
 
359
                initialize_enums ();
 
360
 
 
361
        enum_map_t *emu = (enum_map_t *) g_hash_table_lookup (enum_map, prop_name);
 
362
        if (! emu)
 
363
                return -1;
 
364
 
 
365
        return enum_from_str (emu, str);
 
366
}
 
367
 
 
368
//
 
369
// Converts an int enum to a string representation.
 
370
// Returns NULL if no match found.
 
371
//
 
372
const char *
 
373
enums_int_to_str (const char *prop_name, int e, bool sl2)
 
374
{
 
375
        if (enum_map == NULL)
 
376
                initialize_enums ();
 
377
 
 
378
        enum_map_t *emu = (enum_map_t *) g_hash_table_lookup (enum_map, prop_name);
 
379
        if (! emu)
 
380
                return NULL;
 
381
 
 
382
        return str_from_enum (emu, e, sl2);
 
383
}
 
384