PTConverter.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 / PTConverter.cs / 1 / PTConverter.cs

                            /*++ 

Copyright (C) 2002 Microsoft Corporation
All rights reserved.
 
Module Name:
 
    PTConverter.cs 

Abstract: 

    Definition and implementation of the public PrintTicketConverter class.
    This class is separated from printing LAPI and exposed directly because
    we don't want printing LAPI to have dependency on the legacy binary DEVMODE. 

Author: 
 
    [....] ([....]) 11/22/2002
 
--*/

using System;
using System.IO; 
using System.Runtime.InteropServices;
using System.Globalization; 
 
using System.Printing;
using MS.Internal.Printing.Configuration; 

//[assembly:System.Runtime.InteropServices.ComVisibleAttribute(false)]

namespace System.Printing.Interop 
{
    #region Public Types 
 
    /// 
    /// Types of default DEVMODE to use as base of PrintTicket to DEVMODE conversion. 
    /// 
    [ComVisible(false)]
    public enum BaseDevModeType
    { 
        /// 
        /// User-default DEVMODE as base of conversion. 
        ///  
        UserDefault = 0,
 
        /// 
        /// Printer-default DEVMODE as base of conversion.
        /// 
        PrinterDefault = 1 
    }
 
    #endregion Public Types 

    #region PrintTicketConverter class 

    /// 
    /// PrintTicketConverter class that supports conversions between PrintTicket and DEVMODE.
    ///  
    [ComVisible(false)]
    public sealed class PrintTicketConverter : IDisposable 
    { 
        #region Constructors
 
        /// 
        /// Constructs a new PrintTicketConverter instance for the given device.
        /// 
        /// Name of printer device the PrintTicketConverter instance should be bound to. 
        /// Print Schema version requested by client.
        ///  
        /// The  parameter is null. 
        /// 
        ///  
        /// The  parameter value is not greater than 0
        /// or is greater than the maximum Print Schema version 
        /// PrintTicketConverter can support.
        ///  
        /// 
        /// The PrintTicketConverter instance failed to bind to the printer specified by . 
        ///  
        public PrintTicketConverter(string deviceName, int clientPrintSchemaVersion)
        { 
            // Check input argument
            if (deviceName == null)
            {
                throw new ArgumentNullException("deviceName"); 
            }
 
            // Check if we can support the schema version client has requested 
            if ((clientPrintSchemaVersion > MaxPrintSchemaVersion) ||
                (clientPrintSchemaVersion <= 0)) 
            {
                throw new ArgumentOutOfRangeException("clientPrintSchemaVersion");
            }
 
            // Instantiate the provider object this converter instance will use.
            // PTProvider constructor throws exception if it fails for any reason. 
            _ptProvider = new PTProvider(deviceName, 
                                         MaxPrintSchemaVersion,
                                         clientPrintSchemaVersion); 

            //Create Dispatcher object to insure the PrintTicketConverted is utilized from the same thread
            _accessVerifier = new PrintSystemDispatcherObject();
        } 

        #endregion Constructors 
 
        #region Public Properties
 
        /// 
        /// Gets the maximum Print Schema version PrintTicketConverter can support.
        /// 
        public static int MaxPrintSchemaVersion 
        {
            get 
            { 
                return _maxPrintSchemaVersion;
            } 
        }

        #endregion Public Properties
 
        #region Public Methods
 
        ///  
        /// Converts the given Win32 DEVMODE into PrintTicket.
        ///  
        /// Byte buffer containing the Win32 DEVMODE.
        /// The converted PrintTicket object.
        /// 
        /// The PrintTicketConverter instance has already been disposed. 
        /// 
        ///  
        /// The  parameter is null. 
        /// 
        ///  
        /// The DEVMODE specified by  is not well-formed.
        /// 
        /// 
        /// The PrintTicketConverter instance failed to convert the DEVMODE to a PrintTicket. 
        /// 
        public PrintTicket ConvertDevModeToPrintTicket(byte[] devMode) 
        { 
            return ConvertDevModeToPrintTicket(devMode, PrintTicketScope.JobScope);
        } 

        /// 
        /// Converts the given Win32 DEVMODE into PrintTicket.
        ///  
        /// Byte buffer containing the Win32 DEVMODE.
        /// scope that the result PrintTicket will be limited to 
        /// The converted PrintTicket object. 
        /// 
        /// The PrintTicketConverter instance has already been disposed. 
        /// 
        /// 
        /// The  parameter is null.
        ///  
        /// 
        /// The  parameter is not one of the standard  values. 
        ///  
        /// 
        /// The DEVMODE specified by  is not well-formed. 
        /// 
        /// 
        /// The PrintTicketConverter instance failed to convert the DEVMODE to a PrintTicket.
        ///  
        public PrintTicket ConvertDevModeToPrintTicket(byte[]           devMode,
                                                       PrintTicketScope scope) 
        { 
            if (_disposed)
            { 
                throw new ObjectDisposedException("PrintTicketConverter");
            }

            //Check to insure that the PrintTicketConverter is being called from the same thread that instantiated it 
            VerifyAccess();
 
            return InternalConvertDevModeToPrintTicket(_ptProvider, devMode, scope); 
        }
 
        /// 
        /// Converts the given PrintTicket into Win32 DEVMODE.
        /// 
        /// The PrintTicket to be converted. 
        /// Type of default DEVMODE to use as base of conversion.
        /// Byte buffer that contains the converted Win32 DEVMODE. 
        ///  
        /// The PrintTicketConverter instance has already been disposed.
        ///  
        /// 
        /// The  parameter is null.
        /// 
        ///  
        /// The  parameter is not one of the standard  values.
        ///  
        ///  
        /// The PrintTicket specified by  is not well-formed.
        ///  
        /// 
        /// The PrintTicketConverter instance failed to convert the PrintTicket to a DEVMODE.
        /// 
        public byte[] ConvertPrintTicketToDevMode(PrintTicket printTicket, 
                                                  BaseDevModeType baseType)
        { 
            return ConvertPrintTicketToDevMode(printTicket, baseType, PrintTicketScope.JobScope); 
        }
 
        /// 
        /// Converts the given PrintTicket into Win32 DEVMODE.
        /// 
        /// The PrintTicket to be converted. 
        /// Type of default DEVMODE to use as base of conversion.
        /// scope that the input PrintTicket will be limited to 
        /// Byte buffer that contains the converted Win32 DEVMODE. 
        /// 
        /// The PrintTicketConverter instance has already been disposed. 
        /// 
        /// 
        /// The  parameter is null.
        ///  
        /// 
        /// The  parameter is not one of the standard  values. 
        ///  
        /// 
        /// The  parameter is not one of the standard  values. 
        /// 
        /// 
        /// The PrintTicket specified by  is not well-formed.
        ///  
        /// 
        /// The PrintTicketConverter instance failed to convert the PrintTicket to a DEVMODE. 
        ///  
        public byte[] ConvertPrintTicketToDevMode(PrintTicket      printTicket,
                                                  BaseDevModeType  baseType, 
                                                  PrintTicketScope scope)
        {
            if (_disposed)
            { 
                throw new ObjectDisposedException("PrintTicketConverter");
            } 
 
            //Check to insure that the PrintTicketConverter is being called from the same thread that instantiated it
            VerifyAccess(); 

            return InternalConvertPrintTicketToDevMode(_ptProvider, printTicket, baseType, scope);
        }
 
        #endregion Public Methods
 
        #region Internal Methods 

        internal static PrintTicket InternalConvertDevModeToPrintTicket(PTProvider provider, 
                                                                        byte[] devMode,
                                                                        PrintTicketScope scope)
        {
            // validate devMode parameter 
            if (devMode == null)
            { 
                throw new ArgumentNullException("devMode"); 
            }
 
            // validate sope parameter
            if ((scope != PrintTicketScope.PageScope) &&
                (scope != PrintTicketScope.DocumentScope) &&
                (scope != PrintTicketScope.JobScope)) 
            {
                throw new ArgumentOutOfRangeException("scope"); 
            } 

            MemoryStream ptStream = provider.ConvertDevModeToPrintTicket(devMode, scope); 

            return new PrintTicket(ptStream);
        }
 
        internal static byte[] InternalConvertPrintTicketToDevMode(PTProvider provider,
                                                                   PrintTicket printTicket, 
                                                                   BaseDevModeType baseType, 
                                                                   PrintTicketScope scope)
        { 
            // Input PrinTicket can't be null
            if (printTicket == null)
            {
                throw new ArgumentNullException("printTicket"); 
            }
 
            // validate the base type value 
            if ((baseType != BaseDevModeType.UserDefault) &&
                (baseType != BaseDevModeType.PrinterDefault)) 
            {
                throw new ArgumentOutOfRangeException("baseType");
            }
 
            // validate scope value
            if ((scope != PrintTicketScope.PageScope) && 
                (scope != PrintTicketScope.DocumentScope) && 
                (scope != PrintTicketScope.JobScope))
            { 
                throw new ArgumentOutOfRangeException("scope");
            }

            MemoryStream ptStream = printTicket.GetXmlStream(); 

            return provider.ConvertPrintTicketToDevMode(ptStream, baseType, scope); 
        } 

        #endregion Internal Methods 

        #region Private methods

        private void VerifyAccess() 
        {
            _accessVerifier.VerifyThreadLocality(); 
        } 

 
        #endregion Private Methods

        // No need to implement the finalizer since we are using SafeHandle to wrap the unmanaged resource.
 
        #region IDisposable Members
 
        ///  
        /// Dispose this PrintTicketConverter instance.
        ///  
        public void Dispose()
        {
            Dispose(true);
        } 

        ///  
        /// Dispose this instance. 
        /// 
        private void Dispose(bool disposing) 
        {
            if (!this._disposed)
            {
                if (disposing) 
                {
                    _ptProvider.Dispose(); 
                    _ptProvider = null; 
                }
 
                this._disposed = true;
            }
        }
 
        #endregion
 
        #region Private Fields 

        ///  
        /// max Print Schema version the converter class can support
        /// 
        private const int _maxPrintSchemaVersion = 1;
 
        /// 
        /// PrintTicket provider instance the converter instance is using 
        ///  
        private PTProvider _ptProvider;
 
        /// 
        /// boolean of whether or not this converter instance is disposed
        /// 
        private bool _disposed; 

        ///  
        /// Dispatcher object to keep track of thread access 
        /// 
        private PrintSystemDispatcherObject _accessVerifier; 

        #endregion Private Fields
    }
 
    #endregion PrintTicketConverter class
} 

// 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