Beispiel #1
0
        /// <Summary>
        /// Encodes the StylusTip in the ISF stream.
        /// </Summary>
#else
        /// <Summary>
        /// Encodes the StylusTip in the ISF stream.
        /// </Summary>
#endif
        private static void PersistStylusTip(DrawingAttributes da, Stream stream, GuidList guidList, ref uint cbData, ref BinaryWriter bw)
        {
            //
            // persist the StylusTip
            //
            if (da.ContainsPropertyData(KnownIds.StylusTip))
            {
                System.Diagnostics.Debug.Assert(da.StylusTip != StylusTip.Ellipse, "StylusTip was put in the EPC for the default value!");

                //
                // persist PenTip.Rectangle for V1 ISF
                //
                Debug.Assert(bw != null);
                cbData += SerializationHelper.Encode(stream, (uint)guidList.FindTag(KnownIds.PenTip, true));
                cbData += SerializationHelper.Encode(stream, (uint)PenTip.Rectangle);

                using (MemoryStream localStream = new MemoryStream(6)) //reasonable default
                {
                    Int32 stylusTip = Convert.ToInt32(da.StylusTip, System.Globalization.CultureInfo.InvariantCulture);
                    System.Runtime.InteropServices.VarEnum type = SerializationHelper.ConvertToVarEnum(PersistenceTypes.StylusTip, true);
                    ExtendedPropertySerializer.EncodeAttribute(KnownIds.StylusTip, stylusTip, type, localStream);

                    cbData += ExtendedPropertySerializer.EncodeAsISF(KnownIds.StylusTip, localStream.ToArray(), stream, guidList, 0, true);
                }
            }
        }
Beispiel #2
0
        internal static void EncodeToStream(ExtendedProperty attribute, Stream stream)
        {
            VarEnum interopTypeInfo;
            object  data = attribute.Value;

            // NTRAID#T2-23063-2003/12/10-Microsoft: CODEQUALITY: Find better way to discover which properties should be persistable
            if (attribute.Id == KnownIds.DrawingFlags)
            {
                interopTypeInfo = VarEnum.VT_I4;
            }
            else if (attribute.Id == KnownIds.StylusTip)
            {
                interopTypeInfo = VarEnum.VT_I4;
            }
            else
            {
                // Find the type of the object if it's embedded in the stream
                if (UsesEmbeddedTypeInformation(attribute.Id))
                {
                    interopTypeInfo = SerializationHelper.ConvertToVarEnum(attribute.Value.GetType(), true);
                }
                else // Otherwise treat this as byte array
                {
                    interopTypeInfo = (VarEnum.VT_ARRAY | VarEnum.VT_UI1);
                }
            }
            EncodeAttribute(attribute.Id, data, interopTypeInfo, stream);
        }
Beispiel #3
0
        /// <summary>
        /// Validates the data to be associated with a ExtendedProperty id
        /// </summary>
        /// <param name="id">ExtendedProperty identifier</param>
        /// <param name="value">data</param>
        /// <remarks>Ignores Ids that are not known (e.g. ExtendedProperties)</remarks>
        internal static void Validate(Guid id, object value)
        {
            if (id == Guid.Empty)
            {
                throw new ArgumentException(SR.Get(SRID.InvalidGuid));
            }

            if (id == KnownIds.Color)
            {
                if (!(value is System.Windows.Media.Color))
                {
                    throw new ArgumentException(SR.Get(SRID.InvalidValueType, typeof(System.Windows.Media.Color)), "value");
                }
            }
            // int attributes
            else if (id == KnownIds.CurveFittingError)
            {
                if (!(value.GetType() == typeof(int)))
                {
                    throw new ArgumentException(SR.Get(SRID.InvalidValueType, typeof(int)), "value");
                }
            }
            else if (id == KnownIds.DrawingFlags)
            {
                // ignore validation of flags
                if (value.GetType() != typeof(DrawingFlags))
                {
                    throw new ArgumentException(SR.Get(SRID.InvalidValueType, typeof(DrawingFlags)), "value");
                }
            }
            else if (id == KnownIds.StylusTip)
            {
                Type valueType      = value.GetType();
                bool fStylusTipType = (valueType == typeof(StylusTip));
                bool fIntType       = (valueType == typeof(int));

                if (!fStylusTipType && !fIntType)
                {
                    throw new ArgumentException(SR.Get(SRID.InvalidValueType1, typeof(StylusTip), typeof(int)), "value");
                }
                else if (!StylusTipHelper.IsDefined((StylusTip)value))
                {
                    throw new ArgumentException(SR.Get(SRID.InvalidValueOfType, value, typeof(StylusTip)), "value");
                }
            }
            else if (id == KnownIds.StylusTipTransform)
            {
                //
                // StylusTipTransform gets serialized as a String, but at runtime is a Matrix
                //
                Type t = value.GetType();
                if (t != typeof(String) && t != typeof(Matrix))
                {
                    throw new ArgumentException(SR.Get(SRID.InvalidValueType1, typeof(String), typeof(Matrix)), "value");
                }
                else if (t == typeof(Matrix))
                {
                    Matrix matrix = (Matrix)value;
                    if (!matrix.HasInverse)
                    {
                        throw new ArgumentException(SR.Get(SRID.MatrixNotInvertible), "value");
                    }
                    if (MatrixHelper.ContainsNaN(matrix))
                    {
                        throw new ArgumentException(SR.Get(SRID.InvalidMatrixContainsNaN), "value");
                    }
                    if (MatrixHelper.ContainsInfinity(matrix))
                    {
                        throw new ArgumentException(SR.Get(SRID.InvalidMatrixContainsInfinity), "value");
                    }
                }
            }
            else if (id == KnownIds.IsHighlighter)
            {
                if (value.GetType() != typeof(bool))
                {
                    throw new ArgumentException(SR.Get(SRID.InvalidValueType, typeof(bool)), "value");
                }
            }
            else if (id == KnownIds.StylusHeight || id == KnownIds.StylusWidth)
            {
                if (value.GetType() != typeof(double))
                {
                    throw new ArgumentException(SR.Get(SRID.InvalidValueType, typeof(double)), "value");
                }

                double dVal = (double)value;

                if (id == KnownIds.StylusHeight)
                {
                    if (Double.IsNaN(dVal) || dVal < DrawingAttributes.MinHeight || dVal > DrawingAttributes.MaxHeight)
                    {
                        throw new ArgumentOutOfRangeException("value", SR.Get(SRID.InvalidDrawingAttributesHeight));
                    }
                }
                else
                {
                    if (Double.IsNaN(dVal) || dVal < DrawingAttributes.MinWidth || dVal > DrawingAttributes.MaxWidth)
                    {
                        throw new ArgumentOutOfRangeException("value", SR.Get(SRID.InvalidDrawingAttributesWidth));
                    }
                }
            }
            else if (id == KnownIds.Transparency)
            {
                if (value.GetType() != typeof(byte))
                {
                    throw new ArgumentException(SR.Get(SRID.InvalidValueType, typeof(byte)), "value");
                }

                double dVal = (double)value;
            }
            else
            {
                if (!UsesEmbeddedTypeInformation(id))
                {
                    // if this guid used the legacy internal attribute persistence APIs,
                    //      then it doesn't include embedded type information (it's always a byte array)
                    if (value.GetType() != typeof(byte[]))
                    {
                        throw new ArgumentException(SR.Get(SRID.InvalidValueType, typeof(byte[])), "value");
                    }
                }
                else
                {
                    // if there is any unsupported type, this call will throw.
                    VarEnum varEnum = SerializationHelper.ConvertToVarEnum(value.GetType(), true);

                    switch (varEnum)
                    {
                    case (VarEnum.VT_ARRAY | VarEnum.VT_I1):   //8208
                    case (VarEnum.VT_I1):                      //16
                    case (VarEnum.VT_ARRAY | VarEnum.VT_DATE): //8199
                    case (VarEnum.VT_DATE):                    //7
                    {
                        //we have a char or char[], datetime or datetime[],
                        //we need to write them to a Stream using a BinaryWriter
                        //to see if an exception is thrown so that the exception
                        //happens now, and not at serialization time...
                        using (MemoryStream stream = new MemoryStream(32))    //reasonable default
                        {
                            using (BinaryWriter writer = new BinaryWriter(stream))
                            {
                                try
                                {
                                    switch (varEnum)
                                    {
                                    case (VarEnum.VT_ARRAY | VarEnum.VT_I1):        //8208
                                    {
                                        writer.Write((char[])value);
                                        break;
                                    }

                                    case (VarEnum.VT_I1):        //16
                                    {
                                        writer.Write((char)value);
                                        break;
                                    }

                                    case (VarEnum.VT_ARRAY | VarEnum.VT_DATE):        //8199
                                    {
                                        DateTime[] data = (DateTime[])value;
                                        for (int i = 0; i < data.Length; i++)
                                        {
                                            writer.Write(data[i].ToOADate());
                                        }
                                        break;
                                    }

                                    case (VarEnum.VT_DATE):        //7
                                    {
                                        DateTime data = (DateTime)value;
                                        writer.Write(data.ToOADate());
                                        break;
                                    }

                                    default:
                                    {
                                        Debug.Assert(false, "Missing case statement!");
                                        break;
                                    }
                                    }
                                }
                                catch (ArgumentException ex)
                                {
                                    //catches bad char & char[]
                                    throw new ArgumentException(SR.Get(SRID.InvalidDataInISF), ex);
                                }
                                catch (OverflowException ex)
                                {
                                    //catches bad DateTime
                                    throw new ArgumentException(SR.Get(SRID.InvalidDataInISF), ex);
                                }
                            }
                        }
                        break;
                    }
                        //do nothing in the default case...
                    }
                }
                return;
            }
        }