PageMediaSize.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ DotNET / DotNET / 8.0 / untmp / WIN_WINDOWS / lh_tools_devdiv_wpf / Windows / wcp / Print / Reach / PrintConfig / PageMediaSize.cs / 1 / PageMediaSize.cs

                            /*++ 

Copyright (C) 2003 Microsoft Corporation
All rights reserved.
 
Module Name:
 
    PageMediaSize.cs 

Abstract: 

    Definition and implementation of this public feature/parameter related types.

Author: 

    [....] ([....]) 7/1/2003 
 
--*/
 
using System;
using System.IO;
using System.Xml;
using System.Collections; 
using System.Collections.ObjectModel;
using System.Collections.Generic; 
using System.Diagnostics; 
using System.Globalization;
 
using System.Printing;
using MS.Internal.Printing.Configuration;

#pragma warning disable 1634, 1691 // Allows suppression of certain PreSharp messages 

namespace MS.Internal.Printing.Configuration 
{ 
    /// 
    /// Represents a fixed-size page media size option. 
    /// 
    internal class FixedMediaSizeOption : PrintCapabilityOption
    {
        #region Constructors 

        internal FixedMediaSizeOption(PrintCapabilityFeature ownerFeature) : base(ownerFeature) 
        { 
            _value = 0;
            _mediaSizeWidth = _mediaSizeHeight = PrintSchema.UnspecifiedIntValue; 
        }

        #endregion Constructors
 
        #region Public Properties
 
        ///  
        /// Gets the page media size's value.
        ///  
        public PageMediaSizeName Value
        {
            get
            { 
                return _value;
            } 
        } 

        ///  
        /// Gets the fixed page media size's width of the physical media, in 1/96 inch unit.
        /// 
        public double MediaSizeWidth
        { 
            get
            { 
                return UnitConverter.LengthValueFromMicronToDIP(_mediaSizeWidth); 
            }
        } 

        /// 
        /// Gets the fixed page media size's height of the physical media, in 1/96 inch unit.
        ///  
        public double MediaSizeHeight
        { 
            get 
            {
                return UnitConverter.LengthValueFromMicronToDIP(_mediaSizeHeight); 
            }
        }

        #endregion Public Properties 

        #region Public Methods 
 
        /// 
        /// Converts the non-custom page media size to human-readable string. 
        /// 
        /// A string that represents this non-custom page media size.
        public override string ToString()
        { 
            return Value.ToString() + ": " + MediaSizeWidth.ToString(CultureInfo.CurrentCulture) +
                   " x " + MediaSizeHeight.ToString(CultureInfo.CurrentCulture); 
        } 

        #endregion Public Methods 

        #region Internal Fields

        internal PageMediaSizeName _value; 
        // Integer values in micron unit
        internal int _mediaSizeWidth, _mediaSizeHeight; 
 
        #endregion Internal Fields
    } 

    /// 
    /// Represents page media size capability.
    ///  
    internal class PageMediaSizeCapability : PrintCapabilityFeature
    { 
        #region Constructors 

        internal PageMediaSizeCapability(InternalPrintCapabilities ownerPrintCap) : base(ownerPrintCap) 
        {
        }

        #endregion Constructors 

        #region Public Properties 
 
        /// 
        /// Gets the collection object that represents fixed-size page media sizes supported by the device. 
        /// 
        public Collection FixedMediaSizes
        {
            get 
            {
                return _fixedSizes; 
            } 
        }
 
        #endregion Public Properties

        #region Internal Methods
 
        internal static PrintCapabilityFeature NewFeatureCallback(InternalPrintCapabilities printCap)
        { 
            PageMediaSizeCapability cap = new PageMediaSizeCapability(printCap); 
            cap._fixedSizes = new Collection();
 
            return cap;
        }

        internal override sealed bool AddOptionCallback(PrintCapabilityOption baseOption) 
        {
            FixedMediaSizeOption option = baseOption as FixedMediaSizeOption; 
            bool complete = false; 

            // All PageMediaSize options must have an option name 
            if (option._optionName == null)
                return complete;

            int enumValue = PrintSchemaMapper.SchemaNameToEnumValueWithArray( 
                                PrintSchemaTags.Keywords.PageMediaSizeKeys.MediaSizeNames,
                                PrintSchemaTags.Keywords.PageMediaSizeKeys.MediaSizeEnums, 
                                option._optionName); 

            // We only support standard Print Schema options 
            if (enumValue > 0)
            {
                option._value = (PageMediaSizeName)enumValue;
                if ((option._mediaSizeWidth > 0) || 
                    (option._mediaSizeHeight > 0))
                { 
                    this.FixedMediaSizes.Add(option); 
                    complete = true;
                } 
            }

            return complete;
        } 

        internal override sealed void AddSubFeatureCallback(PrintCapabilityFeature subFeature) 
        { 
            // PageMediaSize has no sub features.
            return; 
        }

        internal override sealed bool FeaturePropCallback(PrintCapabilityFeature feature, XmlPrintCapReader reader)
        { 
            // no feature property to handle
            return false; 
        } 

        internal override sealed PrintCapabilityOption NewOptionCallback(PrintCapabilityFeature baseFeature) 
        {
            FixedMediaSizeOption option = new FixedMediaSizeOption(baseFeature);

            return option; 
        }
 
        internal override sealed void OptionAttrCallback(PrintCapabilityOption baseOption, XmlPrintCapReader reader) 
        {
            // no option attribute to handle 
            return;
        }

        /// XML is not well-formed. 
        internal override sealed bool OptionPropCallback(PrintCapabilityOption baseOption, XmlPrintCapReader reader)
        { 
            FixedMediaSizeOption option = baseOption as FixedMediaSizeOption; 
            bool handled = false;
 
            if (reader.CurrentElementNodeType == PrintSchemaNodeTypes.ScoredProperty)
            {
                handled = true;
 
                string sPropertyName = reader.CurrentElementNameAttrValue;
 
                if ((sPropertyName == PrintSchemaTags.Keywords.PageMediaSizeKeys.MediaSizeWidth) || 
                    (sPropertyName == PrintSchemaTags.Keywords.PageMediaSizeKeys.MediaSizeHeight))
                { 
                    if (reader.MoveToNextSchemaElement(reader.CurrentElementDepth + 1,
                                                       PrintSchemaNodeTypes.ScoredPropertyLevelTypes))
                    {
                        if (reader.CurrentElementNodeType == PrintSchemaNodeTypes.Value) 
                        {
                            // Child property is Value for fixed media size 
                            int     intValue = 0; 
                            bool    convertOK = false;
 
                            try
                            {
                                intValue = XmlConvertHelper.ConvertStringToInt32(reader.CurrentElementTextValue);
                                convertOK = true; 
                            }
                            // We want to catch internal FormatException to skip recoverable XML content syntax error 
                            #pragma warning suppress 56502 
                            #if _DEBUG
                            catch (FormatException e) 
                            #else
                            catch (FormatException)
                            #endif
                            { 
                                #if _DEBUG
                                Trace.WriteLine("-Error- Invalid int value '" + 
                                                reader.CurrentElementTextValue + 
                                                "' at line number " + reader._xmlReader.LineNumber +
                                                ", line position " + reader._xmlReader.LinePosition + 
                                                ": " + e.Message);
                                #endif
                            }
 
                            if (convertOK)
                            { 
                                if (sPropertyName == PrintSchemaTags.Keywords.PageMediaSizeKeys.MediaSizeWidth) 
                                {
                                    option._mediaSizeWidth = intValue; 
                                }
                                else
                                {
                                    option._mediaSizeHeight = intValue; 
                                }
                            } 
                        } 
                    }
                    else 
                    {
                        #if _DEBUG
                        Trace.WriteLine("-Error- Missing required Value or ParameterRef child-element at line number " +
                                         reader._xmlReader.LineNumber + ", line position " + 
                                         reader._xmlReader.LinePosition);
                        #endif 
                    } 
                }
                else 
                {
                    handled = false;

                    #if _DEBUG 
                    Trace.WriteLine("-Warning- skip unknown ScoredProperty '" +
                                    reader.CurrentElementNameAttrValue + "' at line " + 
                                    reader._xmlReader.LineNumber + ", position " + 
                                    reader._xmlReader.LinePosition);
                    #endif 
                }
            }

            return handled; 
        }
 
        #endregion Internal Methods 

        #region Internal Properties 

        internal override sealed bool IsValid
        {
            get 
            {
                return (this.FixedMediaSizes.Count > 0); 
            } 
        }
 
        internal override sealed string FeatureName
        {
            get
            { 
                return PrintSchemaTags.Keywords.PageMediaSizeKeys.Self;
            } 
        } 

        internal override sealed bool HasSubFeature 
        {
            get
            {
                return false; 
            }
        } 
 
        #endregion Internal Properties
 
        #region Internal Fields

        internal Collection _fixedSizes;
 
        #endregion Internal Fields
    } 
 
    /// 
    /// Represents page media size setting. 
    /// 
    internal class PageMediaSizeSetting : PrintTicketFeature
    {
        #region Constructors 

        ///  
        /// Constructs a new page media size setting object. 
        /// 
        internal PageMediaSizeSetting(InternalPrintTicket ownerPrintTicket) : base(ownerPrintTicket) 
        {
            this._featureName = PrintSchemaTags.Keywords.PageMediaSizeKeys.Self;

            this._propertyMaps = new PTPropertyMapEntry[] { 
                new PTPropertyMapEntry(this,
                                       PrintSchemaTags.Framework.OptionNameProperty, 
                                       PTPropValueTypes.EnumStringValue, 
                                       PrintSchemaTags.Keywords.PageMediaSizeKeys.MediaSizeNames,
                                       PrintSchemaTags.Keywords.PageMediaSizeKeys.MediaSizeEnums), 

                new PTPropertyMapEntry(this,
                                       PrintSchemaTags.Keywords.PageMediaSizeKeys.MediaSizeWidth,
                                       PTPropValueTypes.PositiveIntValue), 

                new PTPropertyMapEntry(this, 
                                       PrintSchemaTags.Keywords.PageMediaSizeKeys.MediaSizeHeight, 
                                       PTPropValueTypes.PositiveIntValue),
 
                // property map entries for non-PS custom size
                new PTPropertyMapEntry(this,
                                       PrintSchemaTags.Keywords.PageMediaSizeKeys.CustomMediaSizeWidth,
                                       PTPropValueTypes.IntParamRefValue, 
                                       PrintSchemaTags.Keywords.PageMediaSizeKeys.MediaSizeWidth,
                                       PrintSchemaTags.Keywords.ParameterDefs.PageMediaSizeMediaSizeWidth), 
 
                new PTPropertyMapEntry(this,
                                       PrintSchemaTags.Keywords.PageMediaSizeKeys.CustomMediaSizeHeight, 
                                       PTPropValueTypes.IntParamRefValue,
                                       PrintSchemaTags.Keywords.PageMediaSizeKeys.MediaSizeHeight,
                                       PrintSchemaTags.Keywords.ParameterDefs.PageMediaSizeMediaSizeHeight),
            }; 
        }
 
        #endregion Constructors 

        #region Public Properties 

        /// 
        /// Gets the value of the page media size setting.
        ///  
        /// 
        /// If the setting is not specified yet, the return value will be 0. 
        ///  
        public PageMediaSizeName Value
        { 
            get
            {
                return (PageMediaSizeName)this[PrintSchemaTags.Framework.OptionNameProperty];
            } 
        }
 
        ///  
        /// Gets the the width of the physical media, in 1/96 inch unit.
        ///  
        /// 
        /// If this dimension value is not specified yet, the return value will be
        /// .
        ///  
        public double MediaSizeWidth
        { 
            get 
            {
                int micronValue; 

                micronValue = this[PrintSchemaTags.Keywords.PageMediaSizeKeys.MediaSizeWidth];

                if ((micronValue == PrintSchema.UnspecifiedIntValue) && 
                    (this.Value == PageMediaSizeName.Unknown))
                { 
                    // If we can't get the fixed-size width value, and the media size name is unknown, 
                    // it could be the case of custom media size.
                    string optionName; 
                    bool bIsPrivate;

                    if ((this.FeatureNode != null) &&
                        ((optionName = this.FeatureNode.GetOptionName(out bIsPrivate)) != null) && 
                        (optionName == PrintSchemaTags.Keywords.PageMediaSizeKeys.CustomMediaSize))
                    { 
                        micronValue = this[PrintSchemaTags.Keywords.PageMediaSizeKeys.CustomMediaSizeWidth]; 
                    }
                } 

                return UnitConverter.LengthValueFromMicronToDIP(micronValue);
            }
        } 

        ///  
        /// Gets the height of the physical media, in 1/96 inch unit. 
        /// 
        ///  
        /// If this dimension value is not specified yet, the return value will be
        /// .
        /// 
        public double MediaSizeHeight 
        {
            get 
            { 
                int micronValue;
 
                micronValue = this[PrintSchemaTags.Keywords.PageMediaSizeKeys.MediaSizeHeight];

                if ((micronValue == PrintSchema.UnspecifiedIntValue) &&
                    (this.Value == PageMediaSizeName.Unknown)) 
                {
                    // If we can't get the fixed-size height value, and the media size name is unknown, 
                    // it could be the case of custom media size. 
                    string optionName;
                    bool bIsPrivate; 

                    if ((this.FeatureNode != null) &&
                        ((optionName = this.FeatureNode.GetOptionName(out bIsPrivate)) != null) &&
                        (optionName == PrintSchemaTags.Keywords.PageMediaSizeKeys.CustomMediaSize)) 
                    {
                        micronValue = this[PrintSchemaTags.Keywords.PageMediaSizeKeys.CustomMediaSizeHeight]; 
                    } 
                }
 
                return UnitConverter.LengthValueFromMicronToDIP(micronValue);
            }
        }
 
        #endregion Public Properties
 
        #region Public Methods 

        ///  
        /// Sets page media size setting to a standard fixed media size name.
        /// 
        /// the standard fixed media size name
        ///  
        /// "value" is not one of the standard .
        ///  
        ///  
        /// This function will also automatically set page media size setting to
        /// fixed width and height physical media dimensions defined by Print Schema for 
        /// the standard fixed media size name.
        /// 
        public void SetFixedMediaSize(PageMediaSizeName value)
        { 
            InternalSetFixedMediaSize(value, true, 0, 0, false);
 
            // automatically set the dimension for the standard media sizes 
            for (int i = 0; i < PrintSchema.StdMediaSizeTable.Length; i++)
            { 
                if (PrintSchema.StdMediaSizeTable[i].SizeValue == value)
                {
                    // Our internal table stores size width/height values in microns, so no need to
                    // do unit conversion. 
                    if (PrintSchema.StdMediaSizeTable[i].SizeW != -1)
                    { 
                        this[PrintSchemaTags.Keywords.PageMediaSizeKeys.MediaSizeWidth] = 
                            PrintSchema.StdMediaSizeTable[i].SizeW;
                    } 

                    if (PrintSchema.StdMediaSizeTable[i].SizeH != -1)
                    {
                        this[PrintSchemaTags.Keywords.PageMediaSizeKeys.MediaSizeHeight] = 
                            PrintSchema.StdMediaSizeTable[i].SizeH;
                    } 
 
                    break;
                } 
            }
        }

        ///  
        /// Sets page media size setting to fixed width and height physical media dimensions.
        ///  
        /// physical media dimension in the width direction, in 1/96 inch unit 
        /// physical media dimension in the height direction, in 1/96 inch unit
        ///  
        /// Either "mediaSizeWidth" or "mediaSizeHeight" is not a positive dimension value.
        /// 
        public void SetFixedMediaSize(double mediaSizeWidth, double mediaSizeHeight)
        { 
            InternalSetFixedMediaSize(0, false, mediaSizeWidth, mediaSizeHeight, true);
        } 
 
        /// 
        /// Sets page media size setting to a standard fixed media size name and 
        /// to fixed width and height physical media dimensions.
        /// 
        /// the standard fixed media size name
        /// physical media dimension in the width direction, in 1/96 inch unit 
        /// physical media dimension in the height direction, in 1/96 inch unit
        ///  
        /// "value" is not one of the standard . 
        /// 
        ///  
        /// Either "mediaSizeWidth" or "mediaSizeHeight" is not a positive dimension value.
        /// 
        public void SetFixedMediaSize(PageMediaSizeName value,
                                      double mediaSizeWidth, 
                                      double mediaSizeHeight)
        { 
            InternalSetFixedMediaSize(value, true, mediaSizeWidth, mediaSizeHeight, true); 
        }
 
        /// 
        /// Converts the page media size setting to human-readable string.
        /// 
        /// A string that represents this page media size setting. 
        public override string ToString()
        { 
            return Value.ToString() + 
                   ": MediaSizeWidth=" + MediaSizeWidth.ToString(CultureInfo.CurrentCulture) +
                   ", MediaSizeHeight=" + MediaSizeHeight.ToString(CultureInfo.CurrentCulture); 
        }

        #endregion Public Methods
 
        #region Private Methods
 
        ///  
        /// "value" is not one of the standard .
        ///  
        /// 
        /// Either "mediaSizeWidth" or "mediaSizeHeight" is not a positive dimension value.
        /// 
        private void InternalSetFixedMediaSize(PageMediaSizeName value, 
                                               bool bSetValue,
                                               double mediaSizeWidth, 
                                               double mediaSizeHeight, 
                                               bool bSetWH)
        { 
            if (bSetValue)
            {
                if (value < PrintSchema.PageMediaSizeNameEnumMin ||
                    value > PrintSchema.PageMediaSizeNameEnumMax) 
                {
                    throw new ArgumentOutOfRangeException("value"); 
                } 
            }
 
            if (bSetWH)
            {
                if ( (mediaSizeWidth <= 0) || (mediaSizeHeight <= 0) )
                { 
                    throw new ArgumentOutOfRangeException((mediaSizeWidth <= 0) ? "mediaSizeWidth" : "mediaSizeHeight",
                                  PTUtility.GetTextFromResource("ArgumentException.PositiveValue")); 
                } 
            }
 
            // Remove all XML content related to PageMediaSize feature
            ClearSetting();

            if (bSetValue) 
            {
                this[PrintSchemaTags.Framework.OptionNameProperty] = (int)value; 
            } 

            if (bSetWH) 
            {
                this[PrintSchemaTags.Keywords.PageMediaSizeKeys.MediaSizeWidth] =
                    UnitConverter.LengthValueFromDIPToMicron(mediaSizeWidth);
                this[PrintSchemaTags.Keywords.PageMediaSizeKeys.MediaSizeHeight] = 
                    UnitConverter.LengthValueFromDIPToMicron(mediaSizeHeight);
            } 
        } 

        #endregion Private Methods 
    }
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK