From 97cf5197cf5b882b2c689d8dc2b555f2edf8f418 Mon Sep 17 00:00:00 2001 From: Dave Wilson Date: Tue, 17 Mar 2015 19:50:07 -0700 Subject: Initial publish --- .../ExtensionSample/App.xaml | 7 + .../ExtensionSample/App.xaml.cs | 146 +++++++ .../ExtensionSample/BidiHelper.cs | 154 +++++++ .../ExtensionSample/Fabrikam_Logo.png | Bin 0 -> 2589 bytes .../ExtensionSample/PrintPreferenceWindow.xaml | 147 +++++++ .../ExtensionSample/PrintPreferenceWindow.xaml.cs | 467 +++++++++++++++++++++ .../ExtensionSample/PrintSchemaHelper.cs | 190 +++++++++ .../ExtensionSample/PrinterExtensionSample.csproj | 219 ++++++++++ .../ExtensionSample/Properties/AssemblyInfo.cs | 62 +++ .../Properties/Resources.Designer.cs | 63 +++ .../ExtensionSample/Properties/Resources.resx | 117 ++++++ .../Properties/Settings.Designer.cs | 26 ++ .../ExtensionSample/Properties/Settings.settings | 7 + .../ExtensionSample/Strings.Designer.cs | 82 ++++ .../ExtensionSample/Strings.resx | 129 ++++++ .../ExtensionSample/ValidationModalDialog.xaml | 19 + .../ExtensionSample/ValidationModalDialog.xaml.cs | 108 +++++ .../ExtensionSample/WindowHelper.cs | 83 ++++ .../ExtensionSample/app.config | 9 + .../ExtensionSample/bidi_Ink_mock.xml | 74 ++++ 20 files changed, 2109 insertions(+) create mode 100644 print/v4PrintDriverSamples/PrinterExtensionSample/ExtensionSample/App.xaml create mode 100644 print/v4PrintDriverSamples/PrinterExtensionSample/ExtensionSample/App.xaml.cs create mode 100644 print/v4PrintDriverSamples/PrinterExtensionSample/ExtensionSample/BidiHelper.cs create mode 100644 print/v4PrintDriverSamples/PrinterExtensionSample/ExtensionSample/Fabrikam_Logo.png create mode 100644 print/v4PrintDriverSamples/PrinterExtensionSample/ExtensionSample/PrintPreferenceWindow.xaml create mode 100644 print/v4PrintDriverSamples/PrinterExtensionSample/ExtensionSample/PrintPreferenceWindow.xaml.cs create mode 100644 print/v4PrintDriverSamples/PrinterExtensionSample/ExtensionSample/PrintSchemaHelper.cs create mode 100644 print/v4PrintDriverSamples/PrinterExtensionSample/ExtensionSample/PrinterExtensionSample.csproj create mode 100644 print/v4PrintDriverSamples/PrinterExtensionSample/ExtensionSample/Properties/AssemblyInfo.cs create mode 100644 print/v4PrintDriverSamples/PrinterExtensionSample/ExtensionSample/Properties/Resources.Designer.cs create mode 100644 print/v4PrintDriverSamples/PrinterExtensionSample/ExtensionSample/Properties/Resources.resx create mode 100644 print/v4PrintDriverSamples/PrinterExtensionSample/ExtensionSample/Properties/Settings.Designer.cs create mode 100644 print/v4PrintDriverSamples/PrinterExtensionSample/ExtensionSample/Properties/Settings.settings create mode 100644 print/v4PrintDriverSamples/PrinterExtensionSample/ExtensionSample/Strings.Designer.cs create mode 100644 print/v4PrintDriverSamples/PrinterExtensionSample/ExtensionSample/Strings.resx create mode 100644 print/v4PrintDriverSamples/PrinterExtensionSample/ExtensionSample/ValidationModalDialog.xaml create mode 100644 print/v4PrintDriverSamples/PrinterExtensionSample/ExtensionSample/ValidationModalDialog.xaml.cs create mode 100644 print/v4PrintDriverSamples/PrinterExtensionSample/ExtensionSample/WindowHelper.cs create mode 100644 print/v4PrintDriverSamples/PrinterExtensionSample/ExtensionSample/app.config create mode 100644 print/v4PrintDriverSamples/PrinterExtensionSample/ExtensionSample/bidi_Ink_mock.xml (limited to 'print/v4PrintDriverSamples/PrinterExtensionSample/ExtensionSample') diff --git a/print/v4PrintDriverSamples/PrinterExtensionSample/ExtensionSample/App.xaml b/print/v4PrintDriverSamples/PrinterExtensionSample/ExtensionSample/App.xaml new file mode 100644 index 00000000..f97fc473 --- /dev/null +++ b/print/v4PrintDriverSamples/PrinterExtensionSample/ExtensionSample/App.xaml @@ -0,0 +1,7 @@ + + diff --git a/print/v4PrintDriverSamples/PrinterExtensionSample/ExtensionSample/App.xaml.cs b/print/v4PrintDriverSamples/PrinterExtensionSample/ExtensionSample/App.xaml.cs new file mode 100644 index 00000000..d37585c4 --- /dev/null +++ b/print/v4PrintDriverSamples/PrinterExtensionSample/ExtensionSample/App.xaml.cs @@ -0,0 +1,146 @@ +// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF +// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO +// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A +// PARTICULAR PURPOSE. +// +// Copyright (c) Microsoft Corporation. All rights reserved +// +// +// Abstract: +// +// This file contains the entry point to the application. + +using System; +using System.Windows; +using System.Runtime; + +using Microsoft.Samples.Printing.PrinterExtension.Types; +using Microsoft.Samples.Printing.PrinterExtension.Helpers; + +using System.Windows.Interop; + +namespace Microsoft.Samples.Printing.PrinterExtension +{ + /// + /// Interaction logic for App.xaml. + /// + public partial class App : Application + { + /// + /// This is the event handler invoked on various driver events. + /// + /// + /// + private static void OnDriverEvent(object sender, PrinterExtensionEventArgs eventArgs) + { + // + // Display the print preferences window. + // + + if (eventArgs.ReasonId.Equals(PrinterExtensionReason.PrintPreferences)) + { + PrintPreferenceWindow printPreferenceWindow = new PrintPreferenceWindow(); + printPreferenceWindow.Initialize(eventArgs); + + // + // Set the caller application's window as parent/owner of the newly created printing preferences window. + // + + WindowInteropHelper wih = new WindowInteropHelper(printPreferenceWindow); + wih.Owner = eventArgs.WindowParent; + + // + // Display a modal/non-modal window based on the 'WindowModal' parameter. + // + + if (eventArgs.WindowModal) + { + printPreferenceWindow.ShowDialog(); + } + else + { + printPreferenceWindow.Show(); + + // Flash the window to draw the user's attention. This is required + // because the printer extension may be drawn behind the parent window. + // The return value of FlashWindow can be safely ignored if there is no need + // to know if the window has focus or not. + WindowHelper.FlashWindow(wih.Handle); + } + } + else if (eventArgs.ReasonId.Equals(PrinterExtensionReason.DriverEvent)) + { + // + // Handle driver events here. + // + } + } + + /// + /// Perform initialization tasks for the printer extension in this event handler. + /// + /// + /// + private void Application_Startup(object sender, StartupEventArgs e) + { + // + // It is recommended that exactly one instance of the PrinterExtensionManager be created per instance of + // the printer extension. + // + + if (manager != null) + { + return; + } + manager = new PrinterExtensionManager(); + + // + // Enable events to be received on one printer driver id. + // + // Note: The order of adding the delegate to PrinterExtensionManager.OnDriverEvent + // and invoking PrinterExtensionManager::EnableEvents is important. + // Adding the delegate should be done first. + // + + manager.OnDriverEvent += OnDriverEvent; + + // + // It is recommended that an instance of a printer extension invoke PrinterExtensionManager::EnableEvents + // for exactly one printer driver id. The printer driver id could come in from a command line argument, + // thereby allowing one application binary to dynamically invoke PrinterExtensionManager::EnableEvents against + // the appropriate printer driver id. + // + + manager.EnableEvents(Guid.Parse(PrinterDriverID)); + } + + /// + /// Perform uninitialization tasks for the printer extension in this event handler. + /// + /// + /// + private void Application_Exit(object sender, ExitEventArgs e) + { + manager.OnDriverEvent -= OnDriverEvent; + manager.DisableEvents(); + + manager = null; + } + + /// + /// This is the printer driver id, as defined in the printer driver manifest file. + /// Please replace this GUID with the printer driver id from your manifest file. + /// + /// It is recommended that you invoke PrinterExtensionManager::EnableEvents() on exactly + /// one printer driver id. The id could come in from a command line argument, thereby enabling + /// one application binary to work with multiple printer driver ids. + /// + private const string PrinterDriverID = "{E0691E8D-F7CC-456E-A7B5-D1FC19BA2279}"; + + /// + /// Instance of the PrinterExtensionManager. It is recommended that you have only instance + /// of the PrinterExtensionManager per application instance. + /// + private static PrinterExtensionManager manager = null; + } +} diff --git a/print/v4PrintDriverSamples/PrinterExtensionSample/ExtensionSample/BidiHelper.cs b/print/v4PrintDriverSamples/PrinterExtensionSample/ExtensionSample/BidiHelper.cs new file mode 100644 index 00000000..c975ee7c --- /dev/null +++ b/print/v4PrintDriverSamples/PrinterExtensionSample/ExtensionSample/BidiHelper.cs @@ -0,0 +1,154 @@ +// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF +// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO +// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A +// PARTICULAR PURPOSE. +// +// Copyright (c) Microsoft Corporation. All rights reserved +// +// +// Abstract: +// +// This file contains helper methods that provide a data-binding friendly way to access and parse bidi response data. + +using System; +using System.Collections.Generic; +using System.Windows.Media; +using System.Xml; + +namespace Microsoft.Samples.Printing.PrinterExtension.Helpers +{ + /// + /// Provide a data-binding friendly way to access and parse bidi response data. + /// + public class BidiHelper + { + /// + /// Parse the bidi response. + /// + /// Bidi response XML data. + public BidiHelper(string bidiResponse) + { + BidiResponseParser parser = new BidiResponseParser(bidiResponse); + InkLevelC = parser.GetInkLevel(Colors.Cyan); + InkLevelM = parser.GetInkLevel(Colors.Magenta); + InkLevelY = parser.GetInkLevel(Colors.Yellow); + InkLevelK = parser.GetInkLevel(Colors.Black); + } + + /// + /// Get the Cyan ink level. + /// + public double InkLevelC + { + get; + private set; + } + + /// + /// Get the Magenta ink level. + /// + public double InkLevelM + { + get; + private set; + } + + /// + /// Get the Yellow ink level. + /// + public double InkLevelY + { + get; + private set; + } + + /// + /// Get the Black ink level. + /// + public double InkLevelK + { + get; + private set; + } + } + + /// + /// This class parses bidi response xml data and provides wrapper methods that operate upon the xml. + /// + internal class BidiResponseParser + { + /// + /// Parse the bidi response. + /// + /// Bidi response XML data. + internal BidiResponseParser(string bidiResponse) + { + bidiData = new XmlDocument(); + bidiData.LoadXml(bidiResponse); + + namespaceManager = new XmlNamespaceManager(bidiData.NameTable); + namespaceManager.AddNamespace("bidi", "http://schemas.microsoft.com/windows/2005/03/printing/bidi"); + } + + /// + /// Get the ink level for a given color. + /// + /// Color + /// Ink level percentage + internal double GetInkLevel(Color color) + { + XmlElement root = bidiData.DocumentElement; + XmlNode inkNode = root.SelectSingleNode(CreateInkXPathQuery(color), namespaceManager); + return double.Parse(inkNode.FirstChild.Value) / 100; + } + + /// + /// Create an XPath query that retrieves the ink level from a standard bidi response. + /// + /// + /// + private static string CreateInkXPathQuery(Color color) + { + string colorName = null; + + if (color.Equals(Colors.Black)) + { + colorName = "Black"; + } + else if (color.Equals(Colors.Red)) + { + colorName = "Red"; + } + else if (color.Equals(Colors.Green)) + { + colorName = "Green"; + } + else if (color.Equals(Colors.Blue)) + { + colorName = "Blue"; + } + else if (color.Equals(Colors.Cyan)) + { + colorName = "Cyan"; + } + else if (color.Equals(Colors.Magenta)) + { + colorName = "Magenta"; + } + else if (color.Equals(Colors.Yellow)) + { + colorName = "Yellow"; + } + else + { + throw new ArgumentException("Unsupported color"); + } + + return "/bidi:Get/Query/Schema[@name='\\Printer.Consumables." + colorName + "Ink" + ":Level']/BIDI_INT"; + } + + private XmlDocument bidiData; + private XmlNamespaceManager namespaceManager; + } + +} diff --git a/print/v4PrintDriverSamples/PrinterExtensionSample/ExtensionSample/Fabrikam_Logo.png b/print/v4PrintDriverSamples/PrinterExtensionSample/ExtensionSample/Fabrikam_Logo.png new file mode 100644 index 00000000..196cd5f6 Binary files /dev/null and b/print/v4PrintDriverSamples/PrinterExtensionSample/ExtensionSample/Fabrikam_Logo.png differ diff --git a/print/v4PrintDriverSamples/PrinterExtensionSample/ExtensionSample/PrintPreferenceWindow.xaml b/print/v4PrintDriverSamples/PrinterExtensionSample/ExtensionSample/PrintPreferenceWindow.xaml new file mode 100644 index 00000000..924efc21 --- /dev/null +++ b/print/v4PrintDriverSamples/PrinterExtensionSample/ExtensionSample/PrintPreferenceWindow.xaml @@ -0,0 +1,147 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +