Beispiel #1
0
        protected static XElement GetElementWithMatchingAttribute(XElement currentElement, string attribute,
                                                                  string className)
        {
            var elements = from objectTag in currentElement.Elements()
                           let attriNode = objectTag.Attribute(XName.Get(attribute))
                                           where attriNode != null && attriNode.Value == className
                                           select objectTag;

            Debug.Assert(elements.Count() == 1 || elements.Count() == 0);
            if (elements.Count() == 0)
            {
                return(null);
            }
            var element = elements.First();


            if (element.Name == XName.Get("reference"))
            {
                var refAttr = element.Attribute(XName.Get("ref"));
                if (refAttr == null)
                {
                    throw new NotImplementedException("Reference with no Ref attribute");
                }
                element = XamlLegacyXibReader.GetReference(currentElement.Document, refAttr.Value);
            }

            return(element);
        }
Beispiel #2
0
        //static protected void GetNormalTitle(XElement element, XamlDomObject domObject)
        //{
        //    GetAndSetValue(element, domObject, "IBUINormalTitle", domObject.Type.ContentProperty);
        ////}

        //private void GetText(XElement element, XamlDomObject domObject)
        //{
        //    GetAndSetValue(element, domObject, "IBUIText", domObject.Type.ContentProperty);
        //}

        protected static void GetAndSetValue(XElement element, XamlDomObject domObject, string key, XamlMember member)
        {
            var valueElement = GetElementWithMatchingAttribute(element, "key", key);

            if (valueElement != null && !string.IsNullOrEmpty(valueElement.Value))
            {
                var attr      = valueElement.Attribute(XName.Get("type"));
                var textValue = valueElement.Value;
                if (attr != null)
                {
                    if (attr.Value.Equals("base64-UTF8", StringComparison.InvariantCultureIgnoreCase))
                    {
                        var sb       = new StringBuilder();
                        var newlines = textValue.Split('\n');
                        foreach (var line in newlines)
                        {
                            sb.Append(XamlLegacyXibReader.DecodeFromBase64(line));
                        }
                        textValue = sb.ToString();
                    }
                }
                domObject.SetMemberValue(member, textValue);
            }
        }
Beispiel #3
0
        protected static void SetColor(XamlDomObject domObject, XElement color, XamlMember colorMember)
        {
            var colorType = color.Attribute(XName.Get("key")).Value;

            //XamlMember colorMember = GetColorMember(domObject.Type, colorType);

            //Image doesn't have a background but the XIB file allows setting it
            if (colorMember == null && domObject.Type.UnderlyingType == typeof(Image))
            {
                return;
            }

            var    colorSpace = int.Parse(GetElementWithMatchingAttribute(color, "key", "NSColorSpace").Value);
            string value      = null;

            switch (colorSpace)
            {
            case 1:     //NSRGB
            {
                var values =
                    XamlLegacyXibReader.DecodeFromBase64(GetElementWithMatchingAttribute(color, "key", "NSRGB").Value)
                    .Split(' ');

                var color2 = Color.FromRgb((byte)(double.Parse(values[0]) * 255),
                                           (byte)(double.Parse(values[1]) * 255),
                                           (byte)(double.Parse(values[2]) * 255));
                value = color2.ToString();
            }
            break;

            case 2:
            case 3:
            {
                var searchKey          = colorSpace == 2 ? "NSRGB" : "NSWhite";
                var asciiDecodedString =
                    XamlLegacyXibReader.DecodeFromBase64(GetElementWithMatchingAttribute(color, "key", searchKey).Value)
                    .Replace("\0", "");
                var values = asciiDecodedString.Split(' ');

                var doubleValues = colorSpace == 2 ? new double[4] : new double[3];
                if (values.Length >= 1)
                {
                    doubleValues[0] = double.Parse(values[0]);
                }
                if (values.Length >= 2)
                {
                    doubleValues[1] = double.Parse(values[1]);
                }
                if (values.Length >= 3)
                {
                    doubleValues[2] = double.Parse(values[2]);
                }
                if (colorSpace == 2)
                {
                    if (values.Length == 4)
                    {
                        doubleValues[3] = double.Parse(values[3]);
                    }
                    Debug.Assert(values.Length <= 4);
                }
                else
                {
                    Debug.Assert(values.Length <= 3);
                }

                byte[] rgbValues = null;
                if (colorSpace == 2)
                {
                    rgbValues = ColorConverters.CMYKtoRGB(doubleValues[0], doubleValues[1], doubleValues[2],
                                                          doubleValues[3]);
                }
                else if (colorSpace == 3)
                {
                    rgbValues = ColorConverters.LabToRGB(doubleValues[0], doubleValues[1], doubleValues[2]);
                }

                var color2 = Color.FromRgb(rgbValues[0], rgbValues[1], rgbValues[2]);

                value = color2.ToString();
            }
            break;

            default:
                throw new NotImplementedException("Doesn't suppor colorspace: " + colorSpace);
            }
            Debug.Assert(value != null);
            domObject.SetMemberValue(colorMember, value);
        }