EnumConverters.cs 3.38 KB
Newer Older
1
2
using Svg.DataTypes;
using System;
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
using System.ComponentModel;
using System.Globalization;

namespace Svg
{
	
	//just overrrides canconvert and returns true
	public class BaseConverter : TypeConverter
    {
     	
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(string))
            {
                return true;
            }

            return base.CanConvertFrom(context, sourceType);
        }

        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                return true;
            }

            return base.CanConvertTo(context, destinationType);
        }
    }
	
	public sealed class SvgBoolConverter : BaseConverter
	{
		public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
		{
			if (value == null)
			{
				return true;
			}
			
			if (!(value is string))
			{
				throw new ArgumentOutOfRangeException("value must be a string.");
			}
C Moore's avatar
C Moore committed
47
48
49
50
51
52
53
54

			// Note: currently only used by SvgVisualElement.Visible but if
			// conversion is used elsewhere these checks below will need to change
			string visibility = (string)value;
			if ((visibility == "hidden") || (visibility == "collapse"))
				return false;
			else
				return true;
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
        }
		
		public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
            	return ((bool)value) ? "visible" : "hidden";
            }

            return base.ConvertTo(context, culture, value, destinationType);
        }	
	}
	
	//converts enums to lower case strings
	public class EnumBaseConverter<T> : BaseConverter
    {
		public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
		{
			if (value == null)
			{
				return Activator.CreateInstance(typeof(T));
			}
			
			if (!(value is string))
			{
				throw new ArgumentOutOfRangeException("value must be a string.");
			}

			return (T)Enum.Parse(typeof(T), (string)value, true);
        }
		
		public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
            	return ((T)value).ToString().ToLower();
            }

            return base.ConvertTo(context, culture, value, destinationType);
        }	
	}
	
	//implementation for fill-rule
    public sealed class SvgFillRuleConverter : EnumBaseConverter<SvgFillRule>
    {
    }
    
    //implementaton for clip rule
    public sealed class SvgClipRuleConverter : EnumBaseConverter<SvgClipRule>
    {
    }
    
    //implementaton for clip rule
    public sealed class SvgTextAnchorConverter : EnumBaseConverter<SvgTextAnchor>
    {
    }
    
    public sealed class SvgStrokeLineCapConverter : EnumBaseConverter<SvgStrokeLineCap>
    {
    }

    public sealed class SvgStrokeLineJoinConverter : EnumBaseConverter<SvgStrokeLineJoin>
    {
    }
119
120
121
122

	public sealed class SvgMarkerUnitsConverter : EnumBaseConverter<SvgMarkerUnits>
	{
	}
123
}