From 514a36751f99250defbb7e0c88c38852ae92e22a Mon Sep 17 00:00:00 2001 From: RKrom Date: Wed, 18 Apr 2012 14:08:53 +0000 Subject: [PATCH] Made the printer-destination work like the others, with dynamic selection of the printer git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@1798 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4 --- Greenshot/Destinations/PrinterDestination.cs | 31 ++++++++++++++-- Greenshot/Helpers/PrintHelper.cs | 36 +++++++++++++++++-- .../releases/additional_files/readme.txt | 1 + 3 files changed, 63 insertions(+), 5 deletions(-) diff --git a/Greenshot/Destinations/PrinterDestination.cs b/Greenshot/Destinations/PrinterDestination.cs index 20fc1f019..70d236d03 100644 --- a/Greenshot/Destinations/PrinterDestination.cs +++ b/Greenshot/Destinations/PrinterDestination.cs @@ -39,7 +39,14 @@ namespace Greenshot.Destinations { private static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(PrinterDestination)); private static CoreConfiguration conf = IniConfig.GetIniSection(); public const string DESIGNATION = "Printer"; + public string printerName = null; + public PrinterDestination() { + } + + public PrinterDestination(string printerName) { + this.printerName = printerName; + } public override string Designation { get { return DESIGNATION; @@ -48,7 +55,11 @@ namespace Greenshot.Destinations { public override string Description { get { - return Language.GetString(LangKey.settings_destination_printer); + if (printerName != null) { + return Language.GetString(LangKey.settings_destination_printer) + " - " + printerName; + } else { + return Language.GetString(LangKey.settings_destination_printer); + } } } @@ -70,10 +81,26 @@ namespace Greenshot.Destinations { } } + public override bool isDynamic { + get { + return true; + } + } + + public override IEnumerable DynamicDestinations() { + foreach (string printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters) { + yield return new PrinterDestination(printer); + } + } + public override bool ExportCapture(bool manuallyInitiated, ISurface surface, ICaptureDetails captureDetails) { PrinterSettings printerSettings = null; using (Image image = surface.GetImageForExport()) { - printerSettings = new PrintHelper(image, captureDetails).PrintWithDialog(); + if (!string.IsNullOrEmpty(printerName)) { + printerSettings = new PrintHelper(image, captureDetails).PrintTo(printerName); + } else { + printerSettings = new PrintHelper(image, captureDetails).PrintWithDialog(); + } if (printerSettings != null) { surface.Modified = false; surface.SendMessageEvent(this, SurfaceMessageTyp.Info, Language.GetFormattedString(LangKey.editor_senttoprinter, printerSettings.PrinterName)); diff --git a/Greenshot/Helpers/PrintHelper.cs b/Greenshot/Helpers/PrintHelper.cs index 23ae3fc39..b8072d029 100644 --- a/Greenshot/Helpers/PrintHelper.cs +++ b/Greenshot/Helpers/PrintHelper.cs @@ -94,13 +94,43 @@ namespace Greenshot.Helpers { printOptionsDialog = null; } + /// + /// displays options dialog (if not disabled via settings) and windows + /// print dialog. + /// + /// printer settings if actually printed, or null if print was cancelled or has failed + public PrinterSettings PrintTo(string printerName) { + PrinterSettings returnPrinterSettings = null; + bool cancelled = false; + printOptionsDialog = new PrintOptionsDialog(); + if (conf.OutputPrintPromptOptions) { + DialogResult result = printOptionsDialog.ShowDialog(); + if (result != DialogResult.OK) { + cancelled = true; + } + } + try { + if (!cancelled) { + printDocument.PrinterSettings.PrinterName = printerName; + printDocument.Print(); + returnPrinterSettings = printDocument.PrinterSettings; + } + } catch (Exception e) { + LOG.Error("An error ocurred while trying to print", e); + MessageBox.Show(Language.GetString(LangKey.print_error), Language.GetString(LangKey.error)); + } + image.Dispose(); + image = null; + return returnPrinterSettings; + } + /// /// displays options dialog (if not disabled via settings) and windows /// print dialog. /// /// printer settings if actually printed, or null if print was cancelled or has failed public PrinterSettings PrintWithDialog() { - PrinterSettings ret = null; + PrinterSettings returnPrinterSettings = null; if (printDialog.ShowDialog() == DialogResult.OK) { bool cancelled = false; printOptionsDialog = new PrintOptionsDialog(); @@ -113,7 +143,7 @@ namespace Greenshot.Helpers { try { if (!cancelled) { printDocument.Print(); - ret = printDialog.PrinterSettings; + returnPrinterSettings = printDialog.PrinterSettings; } } catch (Exception e) { LOG.Error("An error ocurred while trying to print", e); @@ -123,7 +153,7 @@ namespace Greenshot.Helpers { } image.Dispose(); image = null; - return ret; + return returnPrinterSettings; } void DrawImageForPrint(object sender, PrintPageEventArgs e) { diff --git a/Greenshot/releases/additional_files/readme.txt b/Greenshot/releases/additional_files/readme.txt index 66463d759..d33ae25be 100644 --- a/Greenshot/releases/additional_files/readme.txt +++ b/Greenshot/releases/additional_files/readme.txt @@ -18,6 +18,7 @@ Features added: * Added rotate clockwise & counter clockwise * Added a preview when using the window capture from the context menu (Windows Vista and later) * Added color reduction as an option and auto detection for image with less than 256 color. When using reduction this results in smaller files. +* Added direct printing to a selected printer Bugs resolved: * Fixed a problem with temp-files being removed before they were used, now using a delay of ~10 hours