Bladeren bron

2006-09-27 Jordi Mas i Hernandez <[email protected]>

	* PrintingServices.cs: Add LoadPrinterPaperSources member
	
	* PrintingServicesWin32.cs: Implements LoadPrinterPaperSources and
	reads the default paper source and size.
	
	* PrintingServicesUnix.cs: Add stub LoadPrinterPaperSources member
	
	* PrinterSettings.cs: Implements PaperSourceCollection property and
	makes default paper source and size from the system.


svn path=/trunk/mcs/; revision=65968
Jordi Mas i Hernandez 19 jaren geleden
bovenliggende
commit
466bc86721

+ 12 - 0
mcs/class/System.Drawing/System.Drawing.Printing/ChangeLog

@@ -1,3 +1,15 @@
+2006-09-27 Jordi Mas i Hernandez <[email protected]>
+
+	* PrintingServices.cs: Add LoadPrinterPaperSources member
+	
+	* PrintingServicesWin32.cs: Implements LoadPrinterPaperSources and
+	reads the default paper source and size.
+	
+	* PrintingServicesUnix.cs: Add stub LoadPrinterPaperSources member
+	
+	* PrinterSettings.cs: Implements PaperSourceCollection property and
+	makes default paper source and size from the system.
+
 2006-08-02  Chris Toshok  <[email protected]>
 
 	* PreviewPrintController.cs: scale the Graphics context we pass

+ 33 - 18
mcs/class/System.Drawing/System.Drawing.Printing/PrinterSettings.cs

@@ -58,6 +58,8 @@ namespace System.Drawing.Printing
 		private bool print_tofile;
 		internal PrinterSettings.PrinterResolutionCollection printer_resolutions;
 		internal PrinterSettings.PaperSizeCollection paper_sizes;
+		internal PrinterSettings.PaperSourceCollection paper_sources;
+		private PageSettings default_pagesettings;
 		
 		public PrinterSettings() : this (SysPrn.Service.DefaultPrinter)
 		{			
@@ -75,6 +77,8 @@ namespace System.Drawing.Printing
 		{			
 			printer_resolutions = null;
 			paper_sizes = null;
+			paper_sources = null;
+			default_pagesettings = null;
 			maximum_page = 9999; 	
 			copies = 1;
 		}
@@ -96,8 +100,10 @@ namespace System.Drawing.Printing
 			object ICollection.SyncRoot { get { return this; } }			
 #if NET_2_0
 			[EditorBrowsable(EditorBrowsableState.Never)]
-      			public int Add (PaperSource paperSource) {throw new NotImplementedException (); }
+      			public int Add (PaperSource paperSource) {return _PaperSources.Add (paperSource); }
 			public void CopyTo (PaperSource[] paperSources, int index)  {throw new NotImplementedException (); }
+#else
+			internal int Add (PaperSource paperSource) {return _PaperSources.Add (paperSource); }
 #endif
 			
 			public virtual PaperSource this[int index] {
@@ -118,6 +124,12 @@ namespace System.Drawing.Printing
 			{
 				_PaperSources.CopyTo(array, index);
 			}
+			
+			internal void Clear ()
+			{ 
+				_PaperSources.Clear (); 
+			}
+			
 		}
 
 		public class PaperSizeCollection : ICollection, IEnumerable
@@ -276,23 +288,21 @@ namespace System.Drawing.Printing
 				copies = value;
 			}
 		}
-
-		[MonoTODO("PrinterSettings.DefaultPageSettings")]
+		
 		public PageSettings DefaultPageSettings
 		{
-			get
-			{
-				return new PageSettings(
-					this,
-					SupportsColor,
-					false,
-					// TODO: get default paper size for this printer
-					new PaperSize("A4", 827, 1169),
-					// TODO: get default paper source for this printer
-					new PaperSource("default", PaperSourceKind.FormSource),
-					// TODO: get default resolution for this printer
-					new PrinterResolution(200, 200, PrinterResolutionKind.Medium)
-				);
+			get {
+				if (default_pagesettings == null) {
+					default_pagesettings = new PageSettings (this,
+						SupportsColor,
+						false,	
+						// Real defaults are set by LoadPrinterSettings				
+						new PaperSize("A4", 827, 1169),						
+						new PaperSource("default", PaperSourceKind.FormSource),						
+						new PrinterResolution(200, 200, PrinterResolutionKind.Medium));
+				}
+				
+				return default_pagesettings;
 			}
 		}
 
@@ -380,10 +390,15 @@ namespace System.Drawing.Printing
 			}
 		}
 
-		[MonoTODO("PrinterSettings.PaperSources")]
 		public PrinterSettings.PaperSourceCollection PaperSources
 		{
-			get { throw new NotImplementedException(); }
+			get {
+				if (paper_sources == null) {
+					paper_sources = new PrinterSettings.PaperSourceCollection (new PaperSource [] {});
+					SysPrn.Service.LoadPrinterPaperSources (printer_name, this);
+				}
+				return paper_sources;
+			}
 		}
 #if NET_2_0
 		public

+ 1 - 0
mcs/class/System.Drawing/System.Drawing.Printing/PrintingServices.cs

@@ -50,6 +50,7 @@ namespace System.Drawing.Printing
 		internal abstract void LoadPrinterSettings (string printer, PrinterSettings settings);
 		internal abstract void LoadPrinterResolutions (string printer, PrinterSettings settings);
 		internal abstract void LoadPrinterPaperSizes (string printer, PrinterSettings settings);
+		internal abstract void LoadPrinterPaperSources (string printer, PrinterSettings settings);
 
 		//Used from SWF
 		internal abstract void GetPrintDialogInfo (string printer, ref string port, ref string type, ref string status, ref string comment);

+ 5 - 0
mcs/class/System.Drawing/System.Drawing.Printing/PrintingServicesUnix.cs

@@ -119,6 +119,11 @@ namespace System.Drawing.Printing
 			}
 
 			ppdClose (ppd_handle);
+		}		
+		
+		internal override void LoadPrinterPaperSources (string printer, PrinterSettings settings)
+		{
+			// TODO	
 		}
 
 		internal override bool StartPage (GraphicsPrinter gr)

+ 134 - 1
mcs/class/System.Drawing/System.Drawing.Printing/PrintingServicesWin32.cs

@@ -43,6 +43,9 @@ namespace System.Drawing.Printing
 		internal override void LoadPrinterSettings (string printer, PrinterSettings settings)
 		{
 			int ret;
+			DEVMODE devmode;
+			IntPtr hPrn = IntPtr.Zero;
+			IntPtr ptr_dev = IntPtr.Zero;
 
 			settings.maximum_copies = Win32DeviceCapabilities (printer, null, DCCapabilities.DC_COPIES, IntPtr.Zero, IntPtr.Zero);
 
@@ -55,6 +58,41 @@ namespace System.Drawing.Printing
 			ret = Win32DeviceCapabilities (printer, null, DCCapabilities.DC_ORIENTATION, IntPtr.Zero, IntPtr.Zero);
 			if (ret != -1)
 				settings.landscape_angle = ret;
+
+			try {
+				Win32OpenPrinter (printer, out hPrn, IntPtr.Zero);
+				ret = Win32DocumentProperties (IntPtr.Zero, hPrn, null, IntPtr.Zero, IntPtr.Zero, 2);
+
+				if (ret < 0)
+					return;
+
+				ptr_dev =  Marshal.AllocHGlobal (ret);
+				ret = Win32DocumentProperties (IntPtr.Zero, hPrn, null, ptr_dev, IntPtr.Zero, 2);
+
+				devmode = (DEVMODE) Marshal.PtrToStructure (ptr_dev, typeof(DEVMODE));
+
+	        		foreach (PaperSize paper_size in settings.PaperSizes) {
+					if ((int) paper_size.Kind ==  devmode.dmPaperSize) {
+						settings.DefaultPageSettings.PaperSize = paper_size;
+						break;
+					}
+				}
+
+				foreach (PaperSource paper_source in settings.PaperSources) {
+					if ((int) paper_source.Kind ==  devmode.dmDefaultSource) {
+						settings.DefaultPageSettings.PaperSource = paper_source;
+						break;
+					}
+				}
+	        	}
+	        	finally {
+	        		Win32ClosePrinter (hPrn);
+
+	        		if (ptr_dev != IntPtr.Zero)
+					Marshal.FreeHGlobal (ptr_dev);
+	        	}
+
+
 		}
 
 		internal override void LoadPrinterResolutions (string printer, PrinterSettings settings)
@@ -163,6 +201,54 @@ namespace System.Drawing.Printing
 			return (ret > 0) ? true : false;
 		}
 
+		internal override void LoadPrinterPaperSources (string printer, PrinterSettings settings)
+		{
+			int items, ret;
+			IntPtr ptr_names, buff_names = IntPtr.Zero;
+			IntPtr ptr_bins, buff_bins = IntPtr.Zero;
+			PaperSourceKind kind;
+			string name;
+
+			settings.PaperSources.Clear ();
+			items = Win32DeviceCapabilities (printer, null, DCCapabilities.DC_BINNAMES, IntPtr.Zero, IntPtr.Zero);
+
+			if (items == -1)
+				return;
+
+			try {
+				ptr_names = buff_names = Marshal.AllocHGlobal (items * 2 * 24);
+				ptr_bins = buff_bins = Marshal.AllocHGlobal (items * 2);
+
+				ret = Win32DeviceCapabilities (printer, null, DCCapabilities.DC_BINNAMES, buff_names, IntPtr.Zero);
+
+				if (ret == -1) {
+					// the finally clause will free the unmanaged memory before returning
+					return;
+				}
+
+				ret = Win32DeviceCapabilities (printer, null, DCCapabilities.DC_BINS, buff_bins, IntPtr.Zero);
+
+				for (int i = 0; i < ret; i++) {
+					name  = Marshal.PtrToStringUni (ptr_names);
+					kind = (PaperSourceKind) Marshal.ReadInt16 (ptr_bins);
+					settings.PaperSources.Add (new PaperSource (name, kind));
+
+					ptr_names = new IntPtr (ptr_names.ToInt64 () + 24 * 2);
+					ptr_bins = new IntPtr (ptr_bins.ToInt64 () + 2);
+				}
+
+			}
+			finally {
+				if (buff_names != IntPtr.Zero)
+					Marshal.FreeHGlobal (buff_names);
+
+				if (buff_bins != IntPtr.Zero)
+					Marshal.FreeHGlobal (buff_bins);
+
+			}
+
+		}
+
 		internal override bool StartPage (GraphicsPrinter gr)
 		{
 			int ret = Win32StartPage (gr.Hdc);
@@ -241,7 +327,7 @@ namespace System.Drawing.Printing
 			IntPtr ptr;
 
 			Win32OpenPrinter (printer, out hPrn, IntPtr.Zero);
-			
+
 			if (hPrn == IntPtr.Zero)
 				return;
 
@@ -319,6 +405,10 @@ namespace System.Drawing.Printing
       		[DllImport("winspool.drv", EntryPoint="GetDefaultPrinter", CharSet=CharSet.Unicode, SetLastError=true)]
       		private static extern int Win32GetDefaultPrinter (StringBuilder buffer, ref int bufferSize);
 
+		[DllImport("winspool.drv", EntryPoint="DocumentProperties", CharSet=CharSet.Unicode, SetLastError=true)]
+		private static extern int Win32DocumentProperties (IntPtr hwnd, IntPtr hPrinter, string pDeviceName,
+			IntPtr pDevModeOutput, IntPtr pDevModeInput, int fMode);
+
     		[DllImport("gdi32.dll", EntryPoint="CreateDC")]
 		static extern IntPtr Win32CreateDC (string lpszDriver, string lpszDevice,
    			string lpszOutput, IntPtr lpInitData);
@@ -377,6 +467,49 @@ namespace System.Drawing.Printing
   			public int	fwType;
   		}
 
+		[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
+		internal struct DEVMODE
+		{
+			[MarshalAs(UnmanagedType.ByValTStr,SizeConst=32)]
+			public string	dmDeviceName;
+			public short	dmSpecVersion;
+			public short	dmDriverVersion;
+			public short	dmSize;
+			public short	dmDriverExtra;
+			public int	dmFields;
+
+			public short	dmOrientation;
+			public short	dmPaperSize;
+			public short	dmPaperLength;
+			public short	dmPaperWidth;
+
+			public short	dmScale;
+			public short	dmCopies;
+			public short	dmDefaultSource;
+			public short	dmPrintQuality;
+			public short	dmColor;
+			public short	dmDuplex;
+			public short	dmYResolution;
+			public short	dmTTOption;
+			public short	dmCollate;
+			[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
+			public string	dmFormName;
+			public short	dmLogPixels;
+			public short	dmBitsPerPel;
+			public int	dmPelsWidth;
+			public int	dmPelsHeight;
+			public int	dmDisplayFlags;
+			public int	dmDisplayFrequency;
+			public int	dmICMMethod;
+			public int	dmICMIntent;
+			public int	dmMediaType;
+			public int	dmDitherType;
+			public int	dmReserved1;
+			public int	dmReserved2;
+			public int	dmPanningWidth;
+			public int	dmPanningHeight;
+		}
+
   		// Enums
   		internal enum DCCapabilities : short
 		{