diff --git a/PluginExample/ExampleConfiguration.cs b/PluginExample/ExampleConfiguration.cs new file mode 100644 index 000000000..9f370732a --- /dev/null +++ b/PluginExample/ExampleConfiguration.cs @@ -0,0 +1,58 @@ +/* + * Greenshot - a free and open source screenshot tool + * Copyright (C) 2007-2010 Thomas Braun, Jens Klingen, Robin Krom + * + * For more information see: http://getgreenshot.org/ + * The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/ + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 1 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +using System; +using System.Collections.Generic; +using Greenshot.Core; + +namespace PluginExample { + public enum Location { + TopLeft, TopCenter, TopRight, + CenterLeft, CenterCenter, CenterRight, + BottomLeft, BottomCenter, BottomRight + } + /// + /// Description of FlickrConfiguration. + /// + [IniSection("Example", Description="Greenshot Example Plugin configuration")] + public class ExampleConfiguration : IniSection { + [IniProperty("AddAnnotations", Description="Do we need to add annotations?")] + public bool AddAnnotations; + + [IniProperty("Annotations", Description="The annotations")] + public Dictionary annotations; + + /// + /// Supply values we can't put as defaults + /// + /// The property to return a default for + /// object with the default value for the supplied property + public override object GetDefault(string property) { + switch(property) { + case "Annotations": + Dictionary annotationDefaults = new Dictionary(); + annotationDefaults.Add(Location.TopLeft, "%user%"); + annotationDefaults.Add(Location.BottomRight, "%hostname%"); + return annotationDefaults; + } + return null; + } + } +} diff --git a/PluginExample/PluginExample.cs b/PluginExample/PluginExample.cs index b0d29fc39..746cff50f 100644 --- a/PluginExample/PluginExample.cs +++ b/PluginExample/PluginExample.cs @@ -27,6 +27,7 @@ using System.Text; using System.Windows.Forms; using Greenshot.Capturing; +using Greenshot.Core; using Greenshot.Drawing; using Greenshot.Forms; using Greenshot.Plugin; @@ -40,7 +41,7 @@ namespace PluginExample { private IGreenshotPluginHost host; private ICaptureHost captureHost = null; private PluginAttribute myAttributes; - private bool testAnnotations = false; + private ExampleConfiguration config; public PluginExample() { } @@ -61,6 +62,8 @@ namespace PluginExample { this.host.OnImageEditorOpen += new OnImageEditorOpenHandler(ImageEditorOpened); this.host.OnSurfaceFromCapture += new OnSurfaceFromCaptureHandler(SurfaceFromCapture); this.host.OnImageOutput += new OnImageOutputHandler(ImageOutput); + + this.config = IniConfig.GetIniSection(); } public virtual void Shutdown() { @@ -75,10 +78,10 @@ namespace PluginExample { /// public virtual void Configure() { LOG.Debug("Configure called"); - SettingsForm settingsForm = new SettingsForm(testAnnotations); + SettingsForm settingsForm = new SettingsForm(config.AddAnnotations); DialogResult result = settingsForm.ShowDialog(); if (result == DialogResult.OK) { - testAnnotations = settingsForm.TestAnnotations; + config.AddAnnotations = settingsForm.TestAnnotations; } settingsForm.Dispose(); } @@ -118,10 +121,10 @@ namespace PluginExample { ToolStripMenuItem item = (ToolStripMenuItem)sender; IImageEditor imageEditor = (IImageEditor)item.Tag; - string file = host.GetFilename("png", null); + string file = host.GetFilename(OutputFormat.Png, null); string filePath = Path.Combine(host.ConfigurationPath,file); using (FileStream stream = new FileStream(filePath, FileMode.Create)) { - imageEditor.SaveToStream(stream, "PNG", 100); + imageEditor.SaveToStream(stream, OutputFormat.Png, 100); } LOG.Debug("Saved test file to: " + filePath); } @@ -170,28 +173,87 @@ namespace PluginExample { /// Has the ICapture and ISurface public void SurfaceFromCapture(object sender, SurfaceFromCaptureEventArgs eventArgs) { LOG.Debug("SurfaceFromCapture called"); - if (!testAnnotations) { + if (!config.AddAnnotations) { return; } ISurface surface = eventArgs.Surface; - surface.SelectElement(surface.AddCursorContainer(Cursors.Hand, 100, 100)); - // Do something with the screenshot - string title = eventArgs.Capture.CaptureDetails.Title; - if (title != null) { - LOG.Debug("Added title to surface: " + title); - surface.SelectElement(surface.AddTextContainer(title, HorizontalAlignment.Center, VerticalAlignment.CENTER, - FontFamily.GenericSansSerif, 12f, false, false, false, 2, Color.Red, Color.White)); - surface.SelectElement(surface.AddTextContainer(Environment.UserName, HorizontalAlignment.Right, VerticalAlignment.TOP, - FontFamily.GenericSansSerif, 12f, false, false, false, 2, Color.Red, Color.White)); - surface.SelectElement(surface.AddTextContainer(Environment.MachineName, HorizontalAlignment.Right, VerticalAlignment.BOTTOM, - FontFamily.GenericSansSerif, 12f, false, false, false, 2, Color.Red, Color.White)); - surface.SelectElement(surface.AddTextContainer(eventArgs.Capture.CaptureDetails.DateTime.ToLongDateString(), HorizontalAlignment.Left, VerticalAlignment.BOTTOM, - FontFamily.GenericSansSerif, 12f, false, false, false, 2, Color.Red, Color.White)); + // Example, add a Cursor + //surface.SelectElement(surface.AddCursorContainer(Cursors.Hand, 100, 100)); - } else { - LOG.Debug("No title available, doing nothing."); + DateTime captureTaken = DateTime.Now; + // Use default application name for title + string title = Application.ProductName; + + // Check if we have capture details + if ( eventArgs.Capture.CaptureDetails != null) { + captureTaken = eventArgs.Capture.CaptureDetails.DateTime; + if (eventArgs.Capture.CaptureDetails.Title != null) { + title = eventArgs.Capture.CaptureDetails.Title; + } } + foreach(Location location in config.annotations.Keys) { + string pattern = config.annotations[location]; + pattern = pattern.Replace("%YYYY%",captureTaken.Year.ToString()); + pattern = pattern.Replace("%MM%", zeroPad(captureTaken.Month.ToString(), 2)); + pattern = pattern.Replace("%DD%", zeroPad(captureTaken.Day.ToString(), 2)); + pattern = pattern.Replace("%hh%", zeroPad(captureTaken.Hour.ToString(), 2)); + pattern = pattern.Replace("%mm%", zeroPad(captureTaken.Minute.ToString(), 2)); + pattern = pattern.Replace("%ss%", zeroPad(captureTaken.Second.ToString(), 2)); + pattern = pattern.Replace("%domain%", Environment.UserDomainName); + pattern = pattern.Replace("%user%", Environment.UserName); + pattern = pattern.Replace("%hostname%", Environment.MachineName); + pattern = pattern.Replace("%title%", title); + HorizontalAlignment hAlign = HorizontalAlignment.Center; + VerticalAlignment vAlign = VerticalAlignment.CENTER; + switch(location) { + case Location.TopLeft: + hAlign = HorizontalAlignment.Left; + vAlign = VerticalAlignment.TOP; + break; + case Location.TopCenter: + hAlign = HorizontalAlignment.Center; + vAlign = VerticalAlignment.TOP; + break; + case Location.TopRight: + hAlign = HorizontalAlignment.Right; + vAlign = VerticalAlignment.TOP; + break; + + case Location.CenterLeft: + hAlign = HorizontalAlignment.Left; + vAlign = VerticalAlignment.CENTER; + break; + case Location.CenterCenter: + hAlign = HorizontalAlignment.Center; + vAlign = VerticalAlignment.CENTER; + break; + case Location.CenterRight: + hAlign = HorizontalAlignment.Right; + vAlign = VerticalAlignment.CENTER; + break; + + case Location.BottomLeft: + hAlign = HorizontalAlignment.Left; + vAlign = VerticalAlignment.BOTTOM; + break; + case Location.BottomCenter: + hAlign = HorizontalAlignment.Center; + vAlign = VerticalAlignment.BOTTOM; + break; + case Location.BottomRight: + hAlign = HorizontalAlignment.Right; + vAlign = VerticalAlignment.BOTTOM; + break; + } + surface.SelectElement(surface.AddTextContainer(pattern, hAlign, vAlign, + FontFamily.GenericSansSerif, 12f, false, false, false, 2, Color.Red, Color.White)); + } + } + + private static string zeroPad(string input, int chars) { + while(input.Length < chars) input = "0" + input; + return input; } } } \ No newline at end of file diff --git a/PluginExample/PluginExample.csproj b/PluginExample/PluginExample.csproj index 3656c36bc..7a01ffe1e 100644 --- a/PluginExample/PluginExample.csproj +++ b/PluginExample/PluginExample.csproj @@ -49,6 +49,7 @@ + @@ -56,9 +57,6 @@ SettingsForm.cs - - SettingsForm.cs - mkdir "$(SolutionDir)bin\$(Configuration)\Plugins\$(ProjectName)" diff --git a/PluginExample/SettingsForm.resx b/PluginExample/SettingsForm.resx deleted file mode 100644 index 7080a7d11..000000000 --- a/PluginExample/SettingsForm.resx +++ /dev/null @@ -1,120 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - text/microsoft-resx - - - 2.0 - - - System.Resources.ResXResourceReader, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - - System.Resources.ResXResourceWriter, System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - - \ No newline at end of file