Code quality changes

This commit is contained in:
Robin 2016-09-22 20:40:13 +02:00
parent f07ed83722
commit 610f45d082
189 changed files with 4609 additions and 5203 deletions

View file

@ -26,6 +26,7 @@ using Greenshot.Drawing.Fields;
using GreenshotPlugin.UnmanagedHelpers;
using Greenshot.IniFile;
using Greenshot.Core;
using GreenshotPlugin.Effects;
using GreenshotPlugin.Interfaces.Drawing;
namespace Greenshot.Configuration {
@ -99,9 +100,10 @@ namespace Greenshot.Configuration {
} else {
LastUsedFieldValues.Add(requestedField, fieldValue);
}
Field returnField = new Field(fieldType, requestingType);
returnField.Value = fieldValue;
return returnField;
return new Field(fieldType, requestingType)
{
Value = fieldValue
};
}
public void UpdateLastFieldValue(IField field)

View file

@ -41,8 +41,9 @@ namespace Greenshot.Controls {
CheckedChanged += BindableToolStripButton_CheckedChanged;
}
private void BindableToolStripButton_CheckedChanged(object sender, EventArgs e) {
if(PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Checked"));
private void BindableToolStripButton_CheckedChanged(object sender, EventArgs e)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Checked"));
}
}
}

View file

@ -41,11 +41,9 @@ namespace Greenshot.Controls {
SelectedIndexChanged += BindableToolStripComboBox_SelectedIndexChanged;
}
private void BindableToolStripComboBox_SelectedIndexChanged(object sender, EventArgs e) {
if(PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs("SelectedItem"));
}
private void BindableToolStripComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("SelectedItem"));
}
}
}

View file

@ -48,7 +48,7 @@ namespace Greenshot.Controls {
if(Tag == null || !Tag.Equals(clickedItem.Tag)) {
Tag = clickedItem.Tag;
Image = clickedItem.Image;
if(PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("SelectedTag"));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("SelectedTag"));
}
base.OnDropDownItemClicked(e);
}
@ -63,7 +63,7 @@ namespace Greenshot.Controls {
}
}
Tag = tag;
if(PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("SelectedTag"));
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("SelectedTag"));
}
}
}

View file

@ -72,14 +72,16 @@ namespace Greenshot.Controls {
colorDialog.Color = SelectedColor;
// Using the parent to make sure the dialog doesn't show on another window
colorDialog.ShowDialog(Parent.Parent);
if (colorDialog.DialogResult != DialogResult.Cancel) {
if (!colorDialog.Color.Equals(SelectedColor)) {
if (colorDialog.DialogResult == DialogResult.Cancel)
{
return;
}
if (colorDialog.Color.Equals(SelectedColor))
{
return;
}
SelectedColor = colorDialog.Color;
if(PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs("SelectedColor"));
}
}
}
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("SelectedColor"));
}
}
}

View file

@ -29,18 +29,16 @@ namespace Greenshot.Controls {
/// ToolStripProfessionalRenderer which draws the Check correctly when the icons are larger
/// </summary>
public class ContextMenuToolStripProfessionalRenderer : ToolStripProfessionalRenderer {
private static readonly CoreConfiguration coreConfiguration = IniConfig.GetIniSection<CoreConfiguration>();
private static Image scaledCheckbox;
private static readonly CoreConfiguration CoreConfig = IniConfig.GetIniSection<CoreConfiguration>();
private static Image _scaledCheckbox;
protected override void OnRenderItemCheck(ToolStripItemImageRenderEventArgs e) {
if (scaledCheckbox == null || scaledCheckbox.Size != coreConfiguration.IconSize) {
if (scaledCheckbox != null) {
scaledCheckbox.Dispose();
}
scaledCheckbox = ImageHelper.ResizeImage(e.Image, true, coreConfiguration.IconSize.Width, coreConfiguration.IconSize.Height, null);
if (_scaledCheckbox == null || _scaledCheckbox.Size != CoreConfig.IconSize) {
_scaledCheckbox?.Dispose();
_scaledCheckbox = ImageHelper.ResizeImage(e.Image, true, CoreConfig.IconSize.Width, CoreConfig.IconSize.Height, null);
}
Rectangle old = e.ImageRectangle;
ToolStripItemImageRenderEventArgs clone = new ToolStripItemImageRenderEventArgs(e.Graphics, e.Item, scaledCheckbox, new Rectangle(old.X, 0, old.Width, old.Height));
ToolStripItemImageRenderEventArgs clone = new ToolStripItemImageRenderEventArgs(e.Graphics, e.Item, _scaledCheckbox, new Rectangle(old.X, 0, old.Width, old.Height));
base.OnRenderItemCheck(clone);
}
}

View file

@ -84,9 +84,7 @@ namespace Greenshot.Controls {
if (_cursor != null) {
_cursor.Dispose();
}
if (_movableShowColorForm != null) {
_movableShowColorForm.Dispose();
}
_movableShowColorForm?.Dispose();
}
_movableShowColorForm = null;
_cursor = null;
@ -114,10 +112,7 @@ namespace Greenshot.Controls {
{
//Release Capture should consume MouseUp when canceled with the escape key
User32.ReleaseCapture();
if (PipetteUsed != null)
{
PipetteUsed(this, new PipetteUsedArgs(_movableShowColorForm.color));
}
PipetteUsed?.Invoke(this, new PipetteUsedArgs(_movableShowColorForm.color));
}
base.OnMouseUp(e);
}

View file

@ -70,14 +70,16 @@ namespace Greenshot.Controls {
colorDialog.Color = SelectedColor;
// Using the parent to make sure the dialog doesn't show on another window
colorDialog.ShowDialog(Parent.Parent);
if (colorDialog.DialogResult != DialogResult.Cancel) {
if (!colorDialog.Color.Equals(SelectedColor)) {
if (colorDialog.DialogResult == DialogResult.Cancel)
{
return;
}
if (colorDialog.Color.Equals(SelectedColor))
{
return;
}
SelectedColor = colorDialog.Color;
if (PropertyChanged != null) {
PropertyChanged(this, new PropertyChangedEventArgs("SelectedColor"));
}
}
}
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("SelectedColor"));
}
}
}

View file

@ -34,10 +34,7 @@ namespace Greenshot.Controls {
{
}
public NumericUpDown NumericUpDown
{
get {return Control as NumericUpDown;}
}
public NumericUpDown NumericUpDown => Control as NumericUpDown;
public decimal Value
{
@ -74,8 +71,9 @@ namespace Greenshot.Controls {
NumericUpDown.ValueChanged -= _valueChanged;
}
private void _valueChanged(object sender, EventArgs e) {
if(PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Value"));
private void _valueChanged(object sender, EventArgs e)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Value"));
}
}
}

View file

@ -66,7 +66,7 @@ namespace Greenshot.Destinations {
}
}
public override bool isDynamic {
public override bool IsDynamic {
get {
return true;
}

View file

@ -32,76 +32,56 @@ namespace Greenshot.Destinations {
/// Description of EmailDestination.
/// </summary>
public class EmailDestination : AbstractDestination {
private static readonly Image mailIcon = GreenshotResources.getImage("Email.Image");
private static bool isActiveFlag;
private static string mapiClient;
private static readonly Image MailIcon = GreenshotResources.getImage("Email.Image");
private static bool _isActiveFlag;
private static string _mapiClient;
public const string DESIGNATION = "EMail";
static EmailDestination() {
// Logic to decide what email implementation we use
if (EmailConfigHelper.HasMAPI()) {
isActiveFlag = true;
mapiClient = EmailConfigHelper.GetMapiClient();
if (!string.IsNullOrEmpty(mapiClient)) {
if (EmailConfigHelper.HasMapi()) {
_isActiveFlag = true;
_mapiClient = EmailConfigHelper.GetMapiClient();
if (!string.IsNullOrEmpty(_mapiClient)) {
// Active as we have a mapi client, can be disabled later
isActiveFlag = true;
_isActiveFlag = true;
}
}
}
public EmailDestination() {
}
public override string Designation {
get {
return DESIGNATION;
}
}
public override string Designation => DESIGNATION;
public override string Description {
get {
// Make sure there is some kind of "mail" name
if (mapiClient == null) {
mapiClient = Language.GetString(LangKey.editor_email);
if (_mapiClient == null) {
_mapiClient = Language.GetString(LangKey.editor_email);
}
return mapiClient;
return _mapiClient;
}
}
public override int Priority {
get {
return 3;
}
}
public override int Priority => 3;
public override bool isActive {
public override bool IsActive {
get {
if (isActiveFlag) {
if (_isActiveFlag) {
// Disable if the office plugin is installed and the client is outlook
// TODO: Change this! It always creates an exception, as the plugin has not been loaded the type is not there :(
Type outlookdestination = Type.GetType("GreenshotOfficePlugin.OutlookDestination,GreenshotOfficePlugin");
if (outlookdestination != null) {
if (mapiClient.ToLower().Contains("microsoft outlook")) {
isActiveFlag = false;
if (_mapiClient.ToLower().Contains("microsoft outlook")) {
_isActiveFlag = false;
}
}
}
return base.isActive && isActiveFlag;
return base.IsActive && _isActiveFlag;
}
}
public override Keys EditorShortcutKeys {
get {
return Keys.Control | Keys.E;
}
}
public override Keys EditorShortcutKeys => Keys.Control | Keys.E;
public override Image DisplayIcon {
get {
return mailIcon;
}
}
public override Image DisplayIcon => MailIcon;
public override ExportInformation ExportCapture(bool manuallyInitiated, ISurface surface, ICaptureDetails captureDetails) {
ExportInformation exportInformation = new ExportInformation(Designation, Description);

View file

@ -35,39 +35,19 @@ namespace Greenshot.Destinations {
/// Description of FileSaveAsDestination.
/// </summary>
public class FileDestination : AbstractDestination {
private static readonly ILog LOG = LogManager.GetLogger(typeof(FileDestination));
private static readonly CoreConfiguration conf = IniConfig.GetIniSection<CoreConfiguration>();
private static readonly ILog Log = LogManager.GetLogger(typeof(FileDestination));
private static readonly CoreConfiguration CoreConfig = IniConfig.GetIniSection<CoreConfiguration>();
public const string DESIGNATION = "FileNoDialog";
public override string Designation {
get {
return DESIGNATION;
}
}
public override string Designation => DESIGNATION;
public override string Description {
get {
return Language.GetString(LangKey.quicksettings_destination_file);
}
}
public override string Description => Language.GetString(LangKey.quicksettings_destination_file);
public override int Priority {
get {
return 0;
}
}
public override int Priority => 0;
public override Keys EditorShortcutKeys {
get {
return Keys.Control | Keys.S;
}
}
public override Keys EditorShortcutKeys => Keys.Control | Keys.S;
public override Image DisplayIcon {
get {
return GreenshotResources.getImage("Save.Image");
}
}
public override Image DisplayIcon => GreenshotResources.getImage("Save.Image");
public override ExportInformation ExportCapture(bool manuallyInitiated, ISurface surface, ICaptureDetails captureDetails) {
ExportInformation exportInformation = new ExportInformation(Designation, Description);
@ -77,18 +57,18 @@ namespace Greenshot.Destinations {
// Get output settings from the configuration
SurfaceOutputSettings outputSettings = new SurfaceOutputSettings();
if (captureDetails != null && captureDetails.Filename != null) {
if (captureDetails?.Filename != null) {
// As we save a pre-selected file, allow to overwrite.
overwrite = true;
LOG.InfoFormat("Using previous filename");
Log.InfoFormat("Using previous filename");
fullPath = captureDetails.Filename;
outputSettings.Format = ImageOutput.FormatForFilename(fullPath);
} else {
fullPath = CreateNewFilename(captureDetails);
// As we generate a file, the configuration tells us if we allow to overwrite
overwrite = conf.OutputFileAllowOverwrite;
overwrite = CoreConfig.OutputFileAllowOverwrite;
}
if (conf.OutputFilePromptQuality) {
if (CoreConfig.OutputFilePromptQuality) {
QualityDialog qualityDialog = new QualityDialog(outputSettings);
qualityDialog.ShowDialog();
}
@ -96,16 +76,16 @@ namespace Greenshot.Destinations {
// Catching any exception to prevent that the user can't write in the directory.
// This is done for e.g. bugs #2974608, #2963943, #2816163, #2795317, #2789218, #3004642
try {
ImageOutput.Save(surface, fullPath, overwrite, outputSettings, conf.OutputFileCopyPathToClipboard);
ImageOutput.Save(surface, fullPath, overwrite, outputSettings, CoreConfig.OutputFileCopyPathToClipboard);
outputMade = true;
} catch (ArgumentException ex1) {
// Our generated filename exists, display 'save-as'
LOG.InfoFormat("Not overwriting: {0}", ex1.Message);
Log.InfoFormat("Not overwriting: {0}", ex1.Message);
// when we don't allow to overwrite present a new SaveWithDialog
fullPath = ImageOutput.SaveWithDialog(surface, captureDetails);
outputMade = fullPath != null;
} catch (Exception ex2) {
LOG.Error("Error saving screenshot!", ex2);
Log.Error("Error saving screenshot!", ex2);
// Show the problem
MessageBox.Show(Language.GetString(LangKey.error_save), Language.GetString(LangKey.error));
// when save failed we present a SaveWithDialog
@ -120,7 +100,7 @@ namespace Greenshot.Destinations {
{
captureDetails.Filename = fullPath;
}
conf.OutputFileAsFullpath = fullPath;
CoreConfig.OutputFileAsFullpath = fullPath;
}
ProcessExport(exportInformation, surface);
@ -129,18 +109,18 @@ namespace Greenshot.Destinations {
private static string CreateNewFilename(ICaptureDetails captureDetails) {
string fullPath;
LOG.InfoFormat("Creating new filename");
string pattern = conf.OutputFileFilenamePattern;
Log.InfoFormat("Creating new filename");
string pattern = CoreConfig.OutputFileFilenamePattern;
if (string.IsNullOrEmpty(pattern)) {
pattern = "greenshot ${capturetime}";
}
string filename = FilenameHelper.GetFilenameFromPattern(pattern, conf.OutputFileFormat, captureDetails);
string filepath = FilenameHelper.FillVariables(conf.OutputFilePath, false);
string filename = FilenameHelper.GetFilenameFromPattern(pattern, CoreConfig.OutputFileFormat, captureDetails);
string filepath = FilenameHelper.FillVariables(CoreConfig.OutputFilePath, false);
try {
fullPath = Path.Combine(filepath, filename);
} catch (ArgumentException) {
// configured filename or path not valid, show error message...
LOG.InfoFormat("Generated path or filename not valid: {0}, {1}", filepath, filename);
Log.InfoFormat("Generated path or filename not valid: {0}, {1}", filepath, filename);
MessageBox.Show(Language.GetString(LangKey.error_save_invalid_chars), Language.GetString(LangKey.error));
// ... lets get the pattern fixed....

View file

@ -24,35 +24,19 @@ using Greenshot.Configuration;
using GreenshotPlugin.Core;
using Greenshot.Plugin;
using Greenshot.Helpers;
using Greenshot.IniFile;
using log4net;
namespace Greenshot.Destinations {
/// <summary>
/// The PickerDestination shows a context menu with all possible destinations, so the user can "pick" one
/// </summary>
public class PickerDestination : AbstractDestination {
private static ILog LOG = LogManager.GetLogger(typeof(PickerDestination));
private static CoreConfiguration conf = IniConfig.GetIniSection<CoreConfiguration>();
public const string DESIGNATION = "Picker";
public override string Designation {
get {
return DESIGNATION;
}
}
public override string Designation => DESIGNATION;
public override string Description {
get {
return Language.GetString(LangKey.settings_destination_picker);
}
}
public override string Description => Language.GetString(LangKey.settings_destination_picker);
public override int Priority {
get {
return 1;
}
}
public override int Priority => 1;
/// <summary>
@ -68,7 +52,7 @@ namespace Greenshot.Destinations {
if ("Picker".Equals(destination.Designation)) {
continue;
}
if (!destination.isActive) {
if (!destination.IsActive) {
continue;
}
destinations.Add(destination);

View file

@ -76,7 +76,7 @@ namespace Greenshot.Destinations {
}
}
public override bool isDynamic {
public override bool IsDynamic {
get {
return true;
}

View file

@ -45,13 +45,7 @@ namespace Greenshot.Drawing.Adorners
/// <summary>
/// Returns the cursor for when the mouse is over the adorner
/// </summary>
public override Cursor Cursor
{
get
{
return Cursors.SizeAll;
}
}
public override Cursor Cursor => Cursors.SizeAll;
/// <summary>
/// Handle the mouse down
@ -147,7 +141,6 @@ namespace Greenshot.Drawing.Adorners
public override void Paint(PaintEventArgs paintEventArgs)
{
Graphics targetGraphics = paintEventArgs.Graphics;
Rectangle clipRectangle = paintEventArgs.ClipRectangle;
var bounds = Bounds;
GraphicsState state = targetGraphics.Save();

View file

@ -168,7 +168,6 @@ namespace Greenshot.Drawing.Adorners
public override void Paint(PaintEventArgs paintEventArgs)
{
Graphics targetGraphics = paintEventArgs.Graphics;
Rectangle clipRectangle = paintEventArgs.ClipRectangle;
var bounds = Bounds;
GraphicsState state = targetGraphics.Save();

View file

@ -95,7 +95,6 @@ namespace Greenshot.Drawing.Adorners
public override void Paint(PaintEventArgs paintEventArgs)
{
Graphics targetGraphics = paintEventArgs.Graphics;
Rectangle clipRectangle = paintEventArgs.ClipRectangle;
var bounds = Bounds;
targetGraphics.FillRectangle(Brushes.Green, bounds.X, bounds.Y, bounds.Width, bounds.Height);

View file

@ -517,7 +517,7 @@ namespace Greenshot.Drawing
/// <param name="e"></param>
public void HandleFieldChanged(object sender, FieldChangedEventArgs e) {
LOG.DebugFormat("Field {0} changed", e.Field.FieldType);
if (e.Field.FieldType == FieldType.SHADOW) {
if (Equals(e.Field.FieldType, FieldType.SHADOW)) {
accountForShadowChange = true;
}
}

View file

@ -20,6 +20,7 @@
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Runtime.Serialization;
@ -37,52 +38,55 @@ namespace Greenshot.Drawing.Fields
public abstract class AbstractFieldHolder : IFieldHolder
{
private static readonly ILog LOG = LogManager.GetLogger(typeof(AbstractFieldHolder));
private static EditorConfiguration editorConfiguration = IniConfig.GetIniSection<EditorConfiguration>();
private static readonly EditorConfiguration EditorConfig = IniConfig.GetIniSection<EditorConfiguration>();
private readonly IDictionary<IField, PropertyChangedEventHandler> _handlers = new Dictionary<IField, PropertyChangedEventHandler>();
/// <summary>
/// called when a field's value has changed
/// </summary>
[NonSerialized]
private FieldChangedEventHandler fieldChanged;
private FieldChangedEventHandler _fieldChanged;
public event FieldChangedEventHandler FieldChanged
{
add { fieldChanged += value; }
remove { fieldChanged -= value; }
add { _fieldChanged += value; }
remove { _fieldChanged -= value; }
}
// we keep two Collections of our fields, dictionary for quick access, list for serialization
// this allows us to use default serialization
[NonSerialized]
private IDictionary<IFieldType, IField> fieldsByType = new Dictionary<IFieldType, IField>();
private IList<IField> fields = new List<IField>();
public AbstractFieldHolder() { }
private IDictionary<IFieldType, IField> _fieldsByType = new Dictionary<IFieldType, IField>();
private readonly IList<IField> fields = new List<IField>();
[OnDeserialized]
private void OnDeserialized(StreamingContext context)
{
fieldsByType = new Dictionary<IFieldType, IField>();
_fieldsByType = new Dictionary<IFieldType, IField>();
// listen to changing properties
foreach (Field field in fields)
foreach (var field in fields)
{
field.PropertyChanged += delegate {
if (fieldChanged != null)
{
fieldChanged(this, new FieldChangedEventArgs(field));
}
_fieldChanged?.Invoke(this, new FieldChangedEventArgs(field));
};
fieldsByType[field.FieldType] = field;
_fieldsByType[field.FieldType] = field;
}
}
public void AddField(Type requestingType, IFieldType fieldType, object fieldValue)
{
AddField(editorConfiguration.CreateField(requestingType, fieldType, fieldValue));
AddField(EditorConfig.CreateField(requestingType, fieldType, fieldValue));
}
public virtual void AddField(IField field)
{
if (fieldsByType != null && fieldsByType.ContainsKey(field.FieldType))
fields.Add(field);
if (_fieldsByType == null)
{
return;
}
if (_fieldsByType.ContainsKey(field.FieldType))
{
if (LOG.IsDebugEnabled)
{
@ -90,21 +94,21 @@ namespace Greenshot.Drawing.Fields
}
}
fields.Add(field);
fieldsByType[field.FieldType] = field;
field.PropertyChanged += delegate { if (fieldChanged != null) fieldChanged(this, new FieldChangedEventArgs(field)); };
_fieldsByType[field.FieldType] = field;
_handlers[field] = (sender, args) =>
{
_fieldChanged?.Invoke(this, new FieldChangedEventArgs(field));
};
field.PropertyChanged += _handlers[field];
}
public void RemoveField(IField field)
{
fields.Remove(field);
fieldsByType.Remove(field.FieldType);
field.PropertyChanged -= delegate {
if (fieldChanged != null)
{
fieldChanged(this, new FieldChangedEventArgs(field));
}
};
_fieldsByType.Remove(field.FieldType);
field.PropertyChanged -= _handlers[field];
_handlers.Remove(field);
}
public IList<IField> GetFields()
@ -117,7 +121,7 @@ namespace Greenshot.Drawing.Fields
{
try
{
return fieldsByType[fieldType];
return _fieldsByType[fieldType];
}
catch (KeyNotFoundException e)
{
@ -169,19 +173,19 @@ namespace Greenshot.Drawing.Fields
public bool HasField(IFieldType fieldType)
{
return fieldsByType.ContainsKey(fieldType);
return _fieldsByType.ContainsKey(fieldType);
}
public bool HasFieldValue(IFieldType fieldType)
{
return HasField(fieldType) && fieldsByType[fieldType].HasValue;
return HasField(fieldType) && _fieldsByType[fieldType].HasValue;
}
public void SetFieldValue(IFieldType fieldType, object value)
{
try
{
fieldsByType[fieldType].Value = value;
_fieldsByType[fieldType].Value = value;
}
catch (KeyNotFoundException e)
{
@ -191,10 +195,7 @@ namespace Greenshot.Drawing.Fields
protected void OnFieldChanged(object sender, FieldChangedEventArgs e)
{
if (fieldChanged != null)
{
fieldChanged(sender, e);
}
_fieldChanged?.Invoke(sender, e);
}
}
}

View file

@ -34,7 +34,7 @@ namespace Greenshot.Drawing.Fields
[Serializable()]
public abstract class AbstractFieldHolderWithChildren : AbstractFieldHolder
{
private FieldChangedEventHandler fieldChangedEventHandler;
private readonly FieldChangedEventHandler _fieldChangedEventHandler;
[NonSerialized]
private EventHandler childrenChanged;
@ -48,7 +48,7 @@ namespace Greenshot.Drawing.Fields
public AbstractFieldHolderWithChildren()
{
fieldChangedEventHandler = OnFieldChanged;
_fieldChangedEventHandler = OnFieldChanged;
}
[OnDeserialized()]
@ -57,28 +57,28 @@ namespace Greenshot.Drawing.Fields
// listen to changing properties
foreach (IFieldHolder fieldHolder in Children)
{
fieldHolder.FieldChanged += fieldChangedEventHandler;
fieldHolder.FieldChanged += _fieldChangedEventHandler;
}
if (childrenChanged != null) childrenChanged(this, EventArgs.Empty);
childrenChanged?.Invoke(this, EventArgs.Empty);
}
public void AddChild(IFieldHolder fieldHolder)
{
Children.Add(fieldHolder);
fieldHolder.FieldChanged += fieldChangedEventHandler;
if (childrenChanged != null) childrenChanged(this, EventArgs.Empty);
fieldHolder.FieldChanged += _fieldChangedEventHandler;
childrenChanged?.Invoke(this, EventArgs.Empty);
}
public void RemoveChild(IFieldHolder fieldHolder)
{
Children.Remove(fieldHolder);
fieldHolder.FieldChanged -= fieldChangedEventHandler;
if (childrenChanged != null) childrenChanged(this, EventArgs.Empty);
fieldHolder.FieldChanged -= _fieldChangedEventHandler;
childrenChanged?.Invoke(this, EventArgs.Empty);
}
public new IList<IField> GetFields()
{
List<IField> ret = new List<IField>();
var ret = new List<IField>();
ret.AddRange(base.GetFields());
foreach (IFieldHolder fh in Children)
{

View file

@ -129,7 +129,7 @@ namespace Greenshot.Drawing.Fields.Binding {
targetPropertyInfo.SetValue(targetObject, bValue, null);
}
} catch (Exception e) {
throw new MemberAccessException("Could not set property '"+targetProperty+"' to '"+bValue+"' ["+(bValue!=null?bValue.GetType().Name:"")+"] on "+targetObject+". Probably other type than expected, IBindingCoverter to the rescue.", e);
throw new MemberAccessException("Could not set property '"+targetProperty+"' to '"+bValue+"' ["+(bValue?.GetType().Name ?? "")+"] on "+targetObject+". Probably other type than expected, IBindingCoverter to the rescue.", e);
}
}

View file

@ -47,10 +47,7 @@ namespace Greenshot.Drawing.Fields
if (!Equals(_myValue, value))
{
_myValue = value;
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs("Value"));
}
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Value"));
}
}
}
@ -93,10 +90,7 @@ namespace Greenshot.Drawing.Fields
/// <summary>
/// Returns true if this field holds a value other than null.
/// </summary>
public bool HasValue
{
get { return Value != null; }
}
public bool HasValue => Value != null;
/// <summary>
/// Creates a flat clone of this Field. The fields value itself is not cloned.
@ -104,9 +98,7 @@ namespace Greenshot.Drawing.Fields
/// <returns></returns>
public Field Clone()
{
Field ret = new Field(FieldType, Scope);
ret.Value = Value;
return ret;
return new Field(FieldType, Scope) {Value = Value};
}
public override int GetHashCode()
@ -123,7 +115,7 @@ namespace Greenshot.Drawing.Fields
public override bool Equals(object obj)
{
Field other = obj as Field;
var other = obj as Field;
if (other == null)
{
return false;

View file

@ -24,7 +24,6 @@ using Greenshot.IniFile;
using Greenshot.Plugin;
using Greenshot.Plugin.Drawing;
using GreenshotPlugin.Interfaces.Drawing;
using log4net;
using System.Collections.Generic;
using System.ComponentModel;
@ -41,26 +40,25 @@ namespace Greenshot.Drawing.Fields
/// Properties that do not apply for ALL selected elements are null (or 0 respectively)
/// If the property values of the selected elements differ, the value of the last bound element wins.
/// </summary>
public class FieldAggregator : AbstractFieldHolder
public sealed class FieldAggregator : AbstractFieldHolder
{
private IDrawableContainerList boundContainers;
private bool internalUpdateRunning = false;
private readonly IDrawableContainerList _boundContainers;
private bool _internalUpdateRunning;
private enum Status { IDLE, BINDING, UPDATING };
private static readonly ILog LOG = LogManager.GetLogger(typeof(FieldAggregator));
private static EditorConfiguration editorConfiguration = IniConfig.GetIniSection<EditorConfiguration>();
private static readonly EditorConfiguration EditorConfig = IniConfig.GetIniSection<EditorConfiguration>();
public FieldAggregator(ISurface parent)
{
foreach (FieldType fieldType in FieldType.Values)
foreach (var fieldType in FieldType.Values)
{
Field field = new Field(fieldType, GetType());
var field = new Field(fieldType, GetType());
AddField(field);
}
boundContainers = new DrawableContainerList();
boundContainers.Parent = parent;
_boundContainers = new DrawableContainerList
{
Parent = parent
};
}
public override void AddField(IField field)
@ -71,7 +69,7 @@ namespace Greenshot.Drawing.Fields
public void BindElements(IDrawableContainerList dcs)
{
foreach (DrawableContainer dc in dcs)
foreach (var dc in dcs)
{
BindElement(dc);
}
@ -80,15 +78,16 @@ namespace Greenshot.Drawing.Fields
public void BindElement(IDrawableContainer dc)
{
DrawableContainer container = dc as DrawableContainer;
if (container != null && !boundContainers.Contains(container))
if (container == null || _boundContainers.Contains(container))
{
boundContainers.Add(container);
return;
}
_boundContainers.Add(container);
container.ChildrenChanged += delegate {
UpdateFromBoundElements();
};
UpdateFromBoundElements();
}
}
public void BindAndUpdateElement(IDrawableContainer dc)
{
@ -103,8 +102,8 @@ namespace Greenshot.Drawing.Fields
{
return;
}
internalUpdateRunning = true;
foreach (Field field in GetFields())
_internalUpdateRunning = true;
foreach (var field in GetFields())
{
if (container.HasField(field.FieldType) && field.HasValue)
{
@ -112,14 +111,14 @@ namespace Greenshot.Drawing.Fields
container.SetFieldValue(field.FieldType, field.Value);
}
}
internalUpdateRunning = false;
_internalUpdateRunning = false;
}
public void UnbindElement(IDrawableContainer dc)
{
if (boundContainers.Contains(dc))
if (_boundContainers.Contains(dc))
{
boundContainers.Remove(dc);
_boundContainers.Remove(dc);
UpdateFromBoundElements();
}
}
@ -127,7 +126,7 @@ namespace Greenshot.Drawing.Fields
public void Clear()
{
ClearFields();
boundContainers.Clear();
_boundContainers.Clear();
UpdateFromBoundElements();
}
@ -136,12 +135,12 @@ namespace Greenshot.Drawing.Fields
/// </summary>
private void ClearFields()
{
internalUpdateRunning = true;
foreach (Field field in GetFields())
_internalUpdateRunning = true;
foreach (var field in GetFields())
{
field.Value = null;
}
internalUpdateRunning = false;
_internalUpdateRunning = false;
}
/// <summary>
@ -152,27 +151,27 @@ namespace Greenshot.Drawing.Fields
private void UpdateFromBoundElements()
{
ClearFields();
internalUpdateRunning = true;
_internalUpdateRunning = true;
foreach (var field in FindCommonFields())
{
SetFieldValue(field.FieldType, field.Value);
}
internalUpdateRunning = false;
_internalUpdateRunning = false;
}
private IList<IField> FindCommonFields()
{
IList<IField> returnFields = null;
if (boundContainers.Count > 0)
if (_boundContainers.Count > 0)
{
// take all fields from the least selected container...
DrawableContainer leastSelectedContainer = boundContainers[boundContainers.Count - 1] as DrawableContainer;
DrawableContainer leastSelectedContainer = _boundContainers[_boundContainers.Count - 1] as DrawableContainer;
if (leastSelectedContainer != null)
{
returnFields = leastSelectedContainer.GetFields();
for (int i = 0; i < boundContainers.Count - 1; i++)
for (int i = 0; i < _boundContainers.Count - 1; i++)
{
DrawableContainer dc = boundContainers[i] as DrawableContainer;
DrawableContainer dc = _boundContainers[i] as DrawableContainer;
if (dc != null)
{
IList<IField> fieldsToRemove = new List<IField>();
@ -184,7 +183,7 @@ namespace Greenshot.Drawing.Fields
fieldsToRemove.Add(field);
}
}
foreach (IField field in fieldsToRemove)
foreach (var field in fieldsToRemove)
{
returnFields.Remove(field);
}
@ -192,31 +191,30 @@ namespace Greenshot.Drawing.Fields
}
}
}
if (returnFields == null)
{
returnFields = new List<IField>();
}
return returnFields;
return returnFields ?? new List<IField>();
}
public void OwnPropertyChanged(object sender, PropertyChangedEventArgs ea)
{
IField field = (IField)sender;
if (!internalUpdateRunning && field.Value != null)
if (_internalUpdateRunning || field.Value == null)
{
foreach (DrawableContainer drawableContainer in boundContainers)
return;
}
foreach (var drawableContainer1 in _boundContainers)
{
if (drawableContainer.HasField(field.FieldType))
var drawableContainer = (DrawableContainer) drawableContainer1;
if (!drawableContainer.HasField(field.FieldType))
{
continue;
}
IField drawableContainerField = drawableContainer.GetField(field.FieldType);
// Notify before change, so we can e.g. invalidate the area
drawableContainer.BeforeFieldChange(drawableContainerField, field.Value);
drawableContainerField.Value = field.Value;
// update last used from DC field, so that scope is honored
editorConfiguration.UpdateLastFieldValue(drawableContainerField);
}
}
EditorConfig.UpdateLastFieldValue(drawableContainerField);
}
}

View file

@ -72,10 +72,9 @@ namespace Greenshot.Drawing.Filters {
public abstract void Apply(Graphics graphics, Bitmap applyBitmap, Rectangle rect, RenderMode renderMode);
protected void OnPropertyChanged(string propertyName) {
if (propertyChanged != null) {
propertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
protected void OnPropertyChanged(string propertyName)
{
propertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}

View file

@ -34,7 +34,7 @@ namespace Greenshot.Drawing {
/// </summary>
[Serializable]
public class FreehandContainer : DrawableContainer {
private static readonly float [] POINT_OFFSET = new float[]{0.5f, 0.25f, 0.75f};
private static readonly float [] PointOffset = {0.5f, 0.25f, 0.75f};
[NonSerialized]
private GraphicsPath freehandPath = new GraphicsPath();
@ -77,10 +77,9 @@ namespace Greenshot.Drawing {
/// <param name="disposing">When disposing==true all non-managed resources should be freed too!</param>
protected override void Dispose(bool disposing) {
base.Dispose(disposing);
if (disposing) {
if (freehandPath != null) {
freehandPath.Dispose();
}
if (disposing)
{
freehandPath?.Dispose();
}
freehandPath = null;
}
@ -133,9 +132,7 @@ namespace Greenshot.Drawing {
private void RecalculatePath() {
isRecalculated = true;
// Dispose the previous path, if we have one
if (freehandPath != null) {
freehandPath.Dispose();
}
freehandPath?.Dispose();
freehandPath = new GraphicsPath();
// Here we can put some cleanup... like losing all the uninteresting points.
@ -143,7 +140,7 @@ namespace Greenshot.Drawing {
int index = 0;
while ((capturePoints.Count - 1) % 3 != 0) {
// duplicate points, first at 50% than 25% than 75%
capturePoints.Insert((int)(capturePoints.Count*POINT_OFFSET[index]), capturePoints[(int)(capturePoints.Count*POINT_OFFSET[index++])]);
capturePoints.Insert((int)(capturePoints.Count*PointOffset[index]), capturePoints[(int)(capturePoints.Count*PointOffset[index++])]);
}
freehandPath.AddBeziers(capturePoints.ToArray());
} else if (capturePoints.Count == 2) {
@ -228,9 +225,9 @@ namespace Greenshot.Drawing {
/// <returns></returns>
public override bool Equals(object obj) {
bool ret = false;
if(obj != null && GetType().Equals(obj.GetType())) {
if(obj != null && GetType() == obj.GetType()) {
FreehandContainer other = obj as FreehandContainer;
if(freehandPath.Equals(other.freehandPath)) {
if(other != null && freehandPath.Equals(other.freehandPath)) {
ret = true;
}
}

View file

@ -57,7 +57,7 @@ namespace Greenshot.Drawing {
if (!sender.Equals(this)) {
return;
}
if (e.Field.FieldType == FieldType.PREPARED_FILTER_HIGHLIGHT) {
if (Equals(e.Field.FieldType, FieldType.PREPARED_FILTER_HIGHLIGHT)) {
ConfigurePreparedFilters();
}
}
@ -72,12 +72,16 @@ namespace Greenshot.Drawing {
Add(new HighlightFilter(this));
break;
case PreparedFilter.AREA_HIGHLIGHT:
AbstractFilter bf = new BrightnessFilter(this);
bf.Invert = true;
Add(bf);
bf = new BlurFilter(this);
bf.Invert = true;
Add(bf);
var brightnessFilter = new BrightnessFilter(this)
{
Invert = true
};
Add(brightnessFilter);
var blurFilter = new BlurFilter(this)
{
Invert = true
};
Add(blurFilter);
break;
case PreparedFilter.GRAYSCALE:
AbstractFilter f = new GrayscaleFilter(this);

View file

@ -32,7 +32,7 @@ namespace Greenshot.Drawing {
/// </summary>
[Serializable]
public class IconContainer : DrawableContainer, IIconContainer {
private static readonly ILog LOG = LogManager.GetLogger(typeof(IconContainer));
private static readonly ILog Log = LogManager.GetLogger(typeof(IconContainer));
protected Icon icon;
@ -57,9 +57,7 @@ namespace Greenshot.Drawing {
public Icon Icon {
set {
if (icon != null) {
icon.Dispose();
}
icon?.Dispose();
icon = (Icon)value.Clone();
Width = value.Width;
Height = value.Height;
@ -72,10 +70,9 @@ namespace Greenshot.Drawing {
* When disposing==true all non-managed resources should be freed too!
*/
protected override void Dispose(bool disposing) {
if (disposing) {
if (icon != null) {
icon.Dispose();
}
if (disposing)
{
icon?.Dispose();
}
icon = null;
base.Dispose(disposing);
@ -85,7 +82,7 @@ namespace Greenshot.Drawing {
if (File.Exists(filename)) {
using (Icon fileIcon = new Icon(filename)) {
Icon = fileIcon;
LOG.Debug("Loaded file: " + filename + " with resolution: " + Height + "," + Width);
Log.Debug("Loaded file: " + filename + " with resolution: " + Height + "," + Width);
}
}
}
@ -100,16 +97,8 @@ namespace Greenshot.Drawing {
}
}
public override bool HasDefaultSize {
get {
return true;
}
}
public override bool HasDefaultSize => true;
public override Size DefaultSize {
get {
return icon.Size;
}
}
public override Size DefaultSize => icon.Size;
}
}

View file

@ -28,6 +28,7 @@ using System.Drawing.Drawing2D;
using Greenshot.Core;
using log4net;
using System.Runtime.Serialization;
using GreenshotPlugin.Effects;
using GreenshotPlugin.Interfaces.Drawing;
namespace Greenshot.Drawing {

View file

@ -52,7 +52,7 @@ namespace Greenshot.Drawing {
protected void ObfuscateContainer_OnFieldChanged(object sender, FieldChangedEventArgs e) {
if(sender.Equals(this)) {
if(e.Field.FieldType == FieldType.PREPARED_FILTER_OBFUSCATE) {
if(Equals(e.Field.FieldType, FieldType.PREPARED_FILTER_OBFUSCATE)) {
ConfigurePreparedFilters();
}
}

View file

@ -19,6 +19,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
@ -27,6 +28,7 @@ namespace Greenshot.Drawing {
/// TODO: currently this is only used in the capture form, we might move this code directly to there!
/// </summary>
public abstract class RoundedRectangle {
[Flags]
public enum RectangleCorners {
None = 0, TopLeft = 1, TopRight = 2,
BottomLeft = 4, BottomRight = 8,

View file

@ -84,9 +84,11 @@ namespace Greenshot.Drawing {
protected override void OnDeserialized(StreamingContext context)
{
Init();
_stringFormat = new StringFormat();
_stringFormat.Alignment = StringAlignment.Center;
_stringFormat.LineAlignment = StringAlignment.Center;
_stringFormat = new StringFormat
{
Alignment = StringAlignment.Center,
LineAlignment = StringAlignment.Center
};
}
/// <summary>
@ -98,20 +100,14 @@ namespace Greenshot.Drawing {
{
return;
}
if (Parent != null) {
((Surface)Parent).RemoveStepLabel(this);
}
((Surface) Parent)?.RemoveStepLabel(this);
base.SwitchParent(newParent);
if (newParent != null) {
((Surface)Parent)?.AddStepLabel(this);
}
}
public override Size DefaultSize {
get {
return new Size(30, 30);
}
}
public override Size DefaultSize => new Size(30, 30);
public override bool InitContent() {
_defaultEditMode = EditStatus.IDLE;
@ -148,15 +144,14 @@ namespace Greenshot.Drawing {
if (!disposing) {
return;
}
if (Parent != null)
((Surface) Parent)?.RemoveStepLabel(this);
if (_stringFormat == null)
{
((Surface)Parent).RemoveStepLabel(this);
return;
}
if (_stringFormat != null) {
_stringFormat.Dispose();
_stringFormat = null;
}
}
public override bool HandleMouseMove(int x, int y) {
Invalidate();

View file

@ -40,6 +40,7 @@ using System.Drawing.Imaging;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;
using GreenshotPlugin.Effects;
namespace Greenshot.Drawing
{
@ -48,9 +49,9 @@ namespace Greenshot.Drawing
/// </summary>
public sealed class Surface : Control, ISurface
{
private static ILog LOG = LogManager.GetLogger(typeof(Surface));
public static int Count = 0;
private static CoreConfiguration conf = IniConfig.GetIniSection<CoreConfiguration>();
private static readonly ILog LOG = LogManager.GetLogger(typeof(Surface));
public static int Count;
private static readonly CoreConfiguration conf = IniConfig.GetIniSection<CoreConfiguration>();
// Property to identify the Surface ID
private Guid _uniqueId = Guid.NewGuid();
@ -276,11 +277,6 @@ namespace Greenshot.Drawing
/// </summary>
private IDrawableContainer _cursorContainer;
/// <summary>
/// the capture details, needed with serialization
/// </summary>
private ICaptureDetails _captureDetails;
/// <summary>
/// the modified flag specifies if the surface has had modifications after the last export.
/// Initial state is modified, as "it's not saved"
@ -325,31 +321,19 @@ namespace Greenshot.Drawing
/// <summary>
/// The cursor container has it's own accessor so we can find and remove this (when needed)
/// </summary>
public IDrawableContainer CursorContainer
{
get
{
return _cursorContainer;
}
}
public IDrawableContainer CursorContainer => _cursorContainer;
/// <summary>
/// A simple getter to ask if this surface has a cursor
/// </summary>
public bool HasCursor
{
get
{
return _cursorContainer != null;
}
}
public bool HasCursor => _cursorContainer != null;
/// <summary>
/// A simple helper method to remove the cursor from the surface
/// </summary>
public void RemoveCursor()
{
RemoveElement(_cursorContainer, true);
RemoveElement(_cursorContainer);
_cursorContainer = null;
}
@ -409,8 +393,10 @@ namespace Greenshot.Drawing
_drawingMode = value;
if (_drawingModeChanged != null)
{
SurfaceDrawingModeEventArgs eventArgs = new SurfaceDrawingModeEventArgs();
eventArgs.DrawingMode = _drawingMode;
SurfaceDrawingModeEventArgs eventArgs = new SurfaceDrawingModeEventArgs
{
DrawingMode = _drawingMode
};
_drawingModeChanged.Invoke(this, eventArgs);
}
DeselectAllElements();
@ -445,22 +431,12 @@ namespace Greenshot.Drawing
/// <summary>
/// Property for accessing the capture details
/// </summary>
public ICaptureDetails CaptureDetails
{
get
{
return _captureDetails;
}
set
{
_captureDetails = value;
}
}
public ICaptureDetails CaptureDetails { get; set; }
/// <summary>
/// Base Surface constructor
/// </summary>
public Surface() : base()
public Surface()
{
_fieldAggregator = new FieldAggregator(this);
Count++;
@ -537,7 +513,7 @@ namespace Greenshot.Drawing
// Make sure the image is NOT disposed, we took the reference directly into ourselves
((Capture)capture).NullImage();
_captureDetails = capture.CaptureDetails;
CaptureDetails = capture.CaptureDetails;
}
protected override void Dispose(bool disposing)
@ -615,46 +591,22 @@ namespace Greenshot.Drawing
/// <summary>
/// Returns if the surface can do a undo
/// </summary>
public bool CanUndo
{
get
{
return _undoStack.Count > 0;
}
}
public bool CanUndo => _undoStack.Count > 0;
/// <summary>
/// Returns if the surface can do a redo
/// </summary>
public bool CanRedo
{
get
{
return _redoStack.Count > 0;
}
}
public bool CanRedo => _redoStack.Count > 0;
/// <summary>
/// Get the language key for the undo action
/// </summary>
public LangKey UndoActionLanguageKey
{
get
{
return LangKey.none;
}
}
public LangKey UndoActionLanguageKey => LangKey.none;
/// <summary>
/// Get the language key for redo action
/// </summary>
public LangKey RedoActionLanguageKey
{
get
{
return LangKey.none;
}
}
public LangKey RedoActionLanguageKey => LangKey.none;
/// <summary>
/// Make an action undo-able
@ -722,9 +674,7 @@ namespace Greenshot.Drawing
IDrawableContainerList loadedElements = (IDrawableContainerList)binaryRead.Deserialize(streamRead);
loadedElements.Parent = this;
// Make sure the steplabels are sorted accoring to their number
_stepLabels.Sort(delegate (StepLabelContainer p1, StepLabelContainer p2) {
return p1.Number.CompareTo(p2.Number);
});
_stepLabels.Sort((p1, p2) => p1.Number.CompareTo(p2.Number));
DeselectAllElements();
AddElements(loadedElements);
SelectElements(loadedElements);
@ -799,10 +749,12 @@ namespace Greenshot.Drawing
#region Plugin interface implementations
public IImageContainer AddImageContainer(Image image, int x, int y)
{
ImageContainer bitmapContainer = new ImageContainer(this);
bitmapContainer.Image = image;
bitmapContainer.Left = x;
bitmapContainer.Top = y;
ImageContainer bitmapContainer = new ImageContainer(this)
{
Image = image,
Left = x,
Top = y
};
AddElement(bitmapContainer);
return bitmapContainer;
}
@ -818,10 +770,12 @@ namespace Greenshot.Drawing
}
public IIconContainer AddIconContainer(Icon icon, int x, int y)
{
IconContainer iconContainer = new IconContainer(this);
iconContainer.Icon = icon;
iconContainer.Left = x;
iconContainer.Top = y;
IconContainer iconContainer = new IconContainer(this)
{
Icon = icon,
Left = x,
Top = y
};
AddElement(iconContainer);
return iconContainer;
}
@ -836,10 +790,12 @@ namespace Greenshot.Drawing
}
public ICursorContainer AddCursorContainer(Cursor cursor, int x, int y)
{
CursorContainer cursorContainer = new CursorContainer(this);
cursorContainer.Cursor = cursor;
cursorContainer.Left = x;
cursorContainer.Top = y;
CursorContainer cursorContainer = new CursorContainer(this)
{
Cursor = cursor,
Left = x,
Top = y
};
AddElement(cursorContainer);
return cursorContainer;
}
@ -855,8 +811,7 @@ namespace Greenshot.Drawing
public ITextContainer AddTextContainer(string text, HorizontalAlignment horizontalAlignment, VerticalAlignment verticalAlignment, FontFamily family, float size, bool italic, bool bold, bool shadow, int borderSize, Color color, Color fillColor)
{
TextContainer textContainer = new TextContainer(this);
textContainer.Text = text;
TextContainer textContainer = new TextContainer(this) {Text = text};
textContainer.SetFieldValue(FieldType.FONT_FAMILY, family.Name);
textContainer.SetFieldValue(FieldType.FONT_BOLD, bold);
textContainer.SetFieldValue(FieldType.FONT_ITALIC, italic);
@ -1069,10 +1024,12 @@ namespace Greenshot.Drawing
{
if (_surfaceMessage != null)
{
SurfaceMessageEventArgs eventArgs = new SurfaceMessageEventArgs();
eventArgs.Message = message;
eventArgs.MessageType = messageType;
eventArgs.Surface = this;
var eventArgs = new SurfaceMessageEventArgs
{
Message = message,
MessageType = messageType,
Surface = this
};
_surfaceMessage(source, eventArgs);
}
}
@ -1133,10 +1090,7 @@ namespace Greenshot.Drawing
{
_elements.Transform(matrix);
}
if (_surfaceSizeChanged != null)
{
_surfaceSizeChanged(this, null);
}
_surfaceSizeChanged?.Invoke(this, null);
Invalidate();
}
/// <summary>
@ -1196,8 +1150,7 @@ namespace Greenshot.Drawing
IDrawableContainer rightClickedContainer = _elements.ClickableElementAt(_mouseStart.X, _mouseStart.Y);
if (rightClickedContainer != null)
{
selectedList = new DrawableContainerList(ID);
selectedList.Add(rightClickedContainer);
selectedList = new DrawableContainerList(ID) {rightClickedContainer};
}
}
if (selectedList != null && selectedList.Count > 0)
@ -1230,8 +1183,11 @@ namespace Greenshot.Drawing
_drawingElement = _undrawnElement;
// if a new element has been drawn, set location and register it
if (_drawingElement != null)
{
if (_undrawnElement != null)
{
_drawingElement.Status = _undrawnElement.DefaultEditMode;
}
if (!_drawingElement.HandleMouseDown(_mouseStart.X, _mouseStart.Y))
{
_drawingElement.Left = _mouseStart.X;
@ -1362,14 +1318,7 @@ namespace Greenshot.Drawing
Point currentMouse = e.Location;
if (DrawingMode != DrawingModes.None)
{
Cursor = Cursors.Cross;
}
else
{
Cursor = Cursors.Default;
}
Cursor = DrawingMode != DrawingModes.None ? Cursors.Cross : Cursors.Default;
if (_mouseDown)
{
@ -1597,8 +1546,7 @@ namespace Greenshot.Drawing
Invalidate();
if (_movingElementChanged != null)
{
SurfaceElementEventArgs eventArgs = new SurfaceElementEventArgs();
eventArgs.Elements = cloned;
SurfaceElementEventArgs eventArgs = new SurfaceElementEventArgs {Elements = cloned};
_movingElementChanged(this, eventArgs);
}
}
@ -1663,13 +1611,7 @@ namespace Greenshot.Drawing
/// Returns if this surface has selected elements
/// </summary>
/// <returns></returns>
public bool HasSelectedElements
{
get
{
return (selectedElements != null && selectedElements.Count > 0);
}
}
public bool HasSelectedElements => (selectedElements != null && selectedElements.Count > 0);
/// <summary>
/// Remove all the selected elements
@ -1764,29 +1706,14 @@ namespace Greenshot.Drawing
if (dcs != null)
{
// Make element(s) only move 10,10 if the surface is the same
Point moveOffset;
bool isSameSurface = (dcs.ParentID == _uniqueId);
dcs.Parent = this;
if (isSameSurface)
{
moveOffset = new Point(10, 10);
}
else
{
moveOffset = Point.Empty;
}
var moveOffset = isSameSurface ? new Point(10, 10) : Point.Empty;
// Here a fix for bug #1475, first calculate the bounds of the complete IDrawableContainerList
Rectangle drawableContainerListBounds = Rectangle.Empty;
foreach (IDrawableContainer element in dcs)
foreach (var element in dcs)
{
if (drawableContainerListBounds == Rectangle.Empty)
{
drawableContainerListBounds = element.DrawingBounds;
}
else
{
drawableContainerListBounds = Rectangle.Union(drawableContainerListBounds, element.DrawingBounds);
}
drawableContainerListBounds = drawableContainerListBounds == Rectangle.Empty ? element.DrawingBounds : Rectangle.Union(drawableContainerListBounds, element.DrawingBounds);
}
// And find a location inside the target surface to paste to
bool containersCanFit = drawableContainerListBounds.Width < Bounds.Width && drawableContainerListBounds.Height < Bounds.Height;
@ -1797,15 +1724,7 @@ namespace Greenshot.Drawing
if (!Bounds.Contains(containersLocation))
{
// Easy fix for same surface
if (isSameSurface)
{
moveOffset = new Point(-10, -10);
}
else
{
// For different surface, which is most likely smaller, we move to "10,10"
moveOffset = new Point(-drawableContainerListBounds.Location.X + 10, -drawableContainerListBounds.Location.Y + 10);
}
moveOffset = isSameSurface ? new Point(-10, -10) : new Point(-drawableContainerListBounds.Location.X + 10, -drawableContainerListBounds.Location.Y + 10);
}
}
else
@ -1932,8 +1851,10 @@ namespace Greenshot.Drawing
}
if (_movingElementChanged != null)
{
SurfaceElementEventArgs eventArgs = new SurfaceElementEventArgs();
eventArgs.Elements = selectedElements;
SurfaceElementEventArgs eventArgs = new SurfaceElementEventArgs
{
Elements = selectedElements
};
_movingElementChanged(this, eventArgs);
}
Invalidate();
@ -1962,8 +1883,10 @@ namespace Greenshot.Drawing
FieldAggregator.BindElement(container);
if (generateEvents && _movingElementChanged != null)
{
SurfaceElementEventArgs eventArgs = new SurfaceElementEventArgs();
eventArgs.Elements = selectedElements;
SurfaceElementEventArgs eventArgs = new SurfaceElementEventArgs
{
Elements = selectedElements
};
_movingElementChanged(this, eventArgs);
}
if (invalidate)
@ -1988,14 +1911,14 @@ namespace Greenshot.Drawing
public void SelectElements(IDrawableContainerList elements)
{
SuspendLayout();
foreach (DrawableContainer element in elements)
foreach (var drawableContainer in elements)
{
var element = (DrawableContainer) drawableContainer;
SelectElement(element, false, false);
}
if (_movingElementChanged != null)
{
SurfaceElementEventArgs eventArgs = new SurfaceElementEventArgs();
eventArgs.Elements = selectedElements;
SurfaceElementEventArgs eventArgs = new SurfaceElementEventArgs {Elements = selectedElements};
_movingElementChanged(this, eventArgs);
}
ResumeLayout();
@ -2070,13 +1993,7 @@ namespace Greenshot.Drawing
/// <summary>
/// Property for accessing the elements on the surface
/// </summary>
public IDrawableContainerList Elements
{
get
{
return _elements;
}
}
public IDrawableContainerList Elements => _elements;
/// <summary>
/// pulls selected elements up one level in hierarchy

View file

@ -26,6 +26,7 @@ using Greenshot.Plugin.Drawing;
using GreenshotPlugin.Interfaces.Drawing;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
@ -321,47 +322,47 @@ namespace Greenshot.Drawing
UpdateFormat();
}
private Font CreateFont(string fontFamily, bool fontBold, bool fontItalic, float fontSize)
private Font CreateFont(string fontFamilyName, bool fontBold, bool fontItalic, float fontSize)
{
FontStyle fs = FontStyle.Regular;
FontStyle fontStyle = FontStyle.Regular;
bool hasStyle = false;
using (FontFamily fam = new FontFamily(fontFamily))
using (var fontFamily = new FontFamily(fontFamilyName))
{
bool boldAvailable = fam.IsStyleAvailable(FontStyle.Bold);
bool boldAvailable = fontFamily.IsStyleAvailable(FontStyle.Bold);
if (fontBold && boldAvailable)
{
fs |= FontStyle.Bold;
fontStyle |= FontStyle.Bold;
hasStyle = true;
}
bool italicAvailable = fam.IsStyleAvailable(FontStyle.Italic);
bool italicAvailable = fontFamily.IsStyleAvailable(FontStyle.Italic);
if (fontItalic && italicAvailable)
{
fs |= FontStyle.Italic;
fontStyle |= FontStyle.Italic;
hasStyle = true;
}
if (!hasStyle)
{
bool regularAvailable = fam.IsStyleAvailable(FontStyle.Regular);
bool regularAvailable = fontFamily.IsStyleAvailable(FontStyle.Regular);
if (regularAvailable)
{
fs = FontStyle.Regular;
fontStyle = FontStyle.Regular;
}
else
{
if (boldAvailable)
{
fs = FontStyle.Bold;
fontStyle = FontStyle.Bold;
}
else if (italicAvailable)
{
fs = FontStyle.Italic;
fontStyle = FontStyle.Italic;
}
}
}
return new Font(fam, fontSize, fs, GraphicsUnit.Pixel);
return new Font(fontFamily, fontSize, fontStyle, GraphicsUnit.Pixel);
}
}
@ -400,7 +401,7 @@ namespace Greenshot.Drawing
catch (Exception)
{
// When this happens... the PC is broken
ex.Data.Add("fontFamily", fontFamily);
ex.Data.Add("fontFamilyName", fontFamily);
ex.Data.Add("fontBold", fontBold);
ex.Data.Add("fontItalic", fontItalic);
ex.Data.Add("fontSize", fontSize);
@ -534,6 +535,14 @@ namespace Greenshot.Drawing
/// <param name="font"></param>
public static void DrawText(Graphics graphics, Rectangle drawingRectange, int lineThickness, Color fontColor, bool drawShadow, StringFormat stringFormat, string text, Font font)
{
#if DEBUG
Debug.Assert(font != null);
#else
if (font == null)
{
return;
}
#endif
int textOffset = lineThickness > 0 ? (int)Math.Ceiling(lineThickness / 2d) : 0;
// draw shadow before anything else
if (drawShadow)

View file

@ -152,7 +152,7 @@ namespace Greenshot {
Version v = Assembly.GetExecutingAssembly().GetName().Version;
// Format is like this: AssemblyVersion("Major.Minor.Build.Revision")]
lblTitle.Text = "Greenshot " + v.Major + "." + v.Minor + "." + v.Build + " Build " + v.Revision + (IniConfig.IsPortable ? " Portable" : "") + (" (" + OSInfo.Bits) + " bit)";
lblTitle.Text = "Greenshot " + v.Major + "." + v.Minor + "." + v.Build + " Build " + v.Revision + (IniConfig.IsPortable ? " Portable" : "") + (" (" + OsInfo.Bits) + " bit)";
//Random rand = new Random();

View file

@ -43,7 +43,7 @@ namespace Greenshot.Forms {
public sealed partial class CaptureForm : AnimatingForm {
private enum FixMode {None, Initiated, Horizontal, Vertical};
private static readonly ILog LOG = LogManager.GetLogger(typeof(CaptureForm));
private static readonly ILog Log = LogManager.GetLogger(typeof(CaptureForm));
private static readonly CoreConfiguration Conf = IniConfig.GetIniSection<CoreConfiguration>();
private static readonly Brush GreenOverlayBrush = new SolidBrush(Color.FromArgb(50, Color.MediumSeaGreen));
private static readonly Pen OverlayPen = new Pen(Color.FromArgb(50, Color.Black));
@ -79,29 +79,17 @@ namespace Greenshot.Forms {
/// <summary>
/// Property to access the selected capture rectangle
/// </summary>
public Rectangle CaptureRectangle {
get {
return _captureRect;
}
}
public Rectangle CaptureRectangle => _captureRect;
/// <summary>
/// Property to access the used capture mode
/// </summary>
public CaptureMode UsedCaptureMode {
get {
return _captureMode;
}
}
public CaptureMode UsedCaptureMode => _captureMode;
/// <summary>
/// Get the selected window
/// </summary>
public WindowDetails SelectedCaptureWindow {
get {
return _selectedCaptureWindow;
}
}
public WindowDetails SelectedCaptureWindow => _selectedCaptureWindow;
/// <summary>
/// This should prevent childs to draw backgrounds
@ -117,11 +105,11 @@ namespace Greenshot.Forms {
private void ClosedHandler(object sender, EventArgs e) {
_currentForm = null;
LOG.Debug("Remove CaptureForm from currentForm");
Log.Debug("Remove CaptureForm from currentForm");
}
private void ClosingHandler(object sender, EventArgs e) {
LOG.Debug("Closing captureform");
Log.Debug("Closing captureform");
WindowDetails.UnregisterIgnoreHandle(Handle);
}
@ -132,7 +120,7 @@ namespace Greenshot.Forms {
/// <param name="windows"></param>
public CaptureForm(ICapture capture, List<WindowDetails> windows) {
if (_currentForm != null) {
LOG.Warn("Found currentForm, Closing already opened CaptureForm");
Log.Warn("Found currentForm, Closing already opened CaptureForm");
_currentForm.Close();
_currentForm = null;
Application.DoEvents();
@ -190,8 +178,10 @@ namespace Greenshot.Forms {
// Initialize the zoom with a invalid position
_zoomAnimator = new RectangleAnimator(Rectangle.Empty, new Rectangle(int.MaxValue, int.MaxValue, 0, 0), FramesForMillis(1000), EasingType.Quintic, EasingMode.EaseOut);
VerifyZoomAnimation(_cursorPos, false);
} else if (_zoomAnimator != null) {
_zoomAnimator.ChangeDestination(new Rectangle(Point.Empty, Size.Empty), FramesForMillis(1000));
}
else
{
_zoomAnimator?.ChangeDestination(new Rectangle(Point.Empty, Size.Empty), FramesForMillis(1000));
}
}
@ -441,18 +431,16 @@ namespace Greenshot.Forms {
Point cursorPosition = Cursor.Position;
_selectedCaptureWindow = null;
lock (_windows) {
foreach (WindowDetails window in _windows) {
if (window.Contains(cursorPosition)) {
foreach (var window in _windows) {
if (!window.Contains(cursorPosition))
{
continue;
}
// Only go over the children if we are in window mode
if (CaptureMode.Window == _captureMode) {
_selectedCaptureWindow = window.FindChildUnderPoint(cursorPosition);
} else {
_selectedCaptureWindow = window;
}
_selectedCaptureWindow = CaptureMode.Window == _captureMode ? window.FindChildUnderPoint(cursorPosition) : window;
break;
}
}
}
if (_selectedCaptureWindow != null && !_selectedCaptureWindow.Equals(lastWindow)) {
_capture.CaptureDetails.Title = _selectedCaptureWindow.Text;
@ -619,8 +607,10 @@ namespace Greenshot.Forms {
if (_isZoomerTransparent) {
//create a color matrix object to change the opacy
ColorMatrix opacyMatrix = new ColorMatrix();
opacyMatrix.Matrix33 = Conf.ZoomerOpacity;
ColorMatrix opacyMatrix = new ColorMatrix
{
Matrix33 = Conf.ZoomerOpacity
};
attributes = new ImageAttributes();
attributes.SetColorMatrix(opacyMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
} else {
@ -695,9 +685,7 @@ namespace Greenshot.Forms {
// Horizontal middle + 1 to right
graphics.DrawRectangle(pen, destinationRectangle.X + halfWidthEnd + 2 * padding, drawAtHeight, halfWidthEnd - 2 * padding - 1, pixelThickness);
}
if (attributes != null) {
attributes.Dispose();
}
attributes?.Dispose();
}
/// <summary>
@ -718,14 +706,7 @@ namespace Greenshot.Forms {
if (_mouseDown || _captureMode == CaptureMode.Window || IsAnimating(_windowAnimator)) {
_captureRect.Intersect(new Rectangle(Point.Empty, _capture.ScreenBounds.Size)); // crop what is outside the screen
Rectangle fixedRect;
//if (captureMode == CaptureMode.Window) {
if (IsAnimating(_windowAnimator)) {
// Use the animator
fixedRect = _windowAnimator.Current;
} else {
fixedRect = _captureRect;
}
var fixedRect = IsAnimating(_windowAnimator) ? _windowAnimator.Current : _captureRect;
// TODO: enable when the screen capture code works reliable
//if (capture.CaptureDetails.CaptureMode == CaptureMode.Video) {
@ -827,7 +808,7 @@ namespace Greenshot.Forms {
if (_showDebugInfo && _selectedCaptureWindow != null)
{
string title = string.Format("#{0:X} - {1}", _selectedCaptureWindow.Handle.ToInt64(), _selectedCaptureWindow.Text.Length > 0 ? _selectedCaptureWindow.Text : _selectedCaptureWindow.Process.ProcessName);
string title = $"#{_selectedCaptureWindow.Handle.ToInt64():X} - {(_selectedCaptureWindow.Text.Length > 0 ? _selectedCaptureWindow.Text : _selectedCaptureWindow.Process.ProcessName)}";
PointF debugLocation = new PointF(fixedRect.X, fixedRect.Y);
graphics.DrawString(title, sizeFont, Brushes.DarkOrange, debugLocation);
}

View file

@ -144,7 +144,7 @@ namespace Greenshot {
this.textBoxRed.TabIndex = 2;
this.textBoxRed.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
this.textBoxRed.Click += new System.EventHandler(this.TextBoxGotFocus);
this.textBoxRed.TextChanged += new System.EventHandler(this.TextBoxRGBTextChanged);
this.textBoxRed.TextChanged += new System.EventHandler(this.TextBoxRgbTextChanged);
this.textBoxRed.GotFocus += new System.EventHandler(this.TextBoxGotFocus);
this.textBoxRed.KeyDown += new System.Windows.Forms.KeyEventHandler(this.TextBoxKeyDown);
//
@ -156,7 +156,7 @@ namespace Greenshot {
this.textBoxGreen.TabIndex = 3;
this.textBoxGreen.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
this.textBoxGreen.Click += new System.EventHandler(this.TextBoxGotFocus);
this.textBoxGreen.TextChanged += new System.EventHandler(this.TextBoxRGBTextChanged);
this.textBoxGreen.TextChanged += new System.EventHandler(this.TextBoxRgbTextChanged);
this.textBoxGreen.GotFocus += new System.EventHandler(this.TextBoxGotFocus);
this.textBoxGreen.KeyDown += new System.Windows.Forms.KeyEventHandler(this.TextBoxKeyDown);
//
@ -168,7 +168,7 @@ namespace Greenshot {
this.textBoxBlue.TabIndex = 4;
this.textBoxBlue.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
this.textBoxBlue.Click += new System.EventHandler(this.TextBoxGotFocus);
this.textBoxBlue.TextChanged += new System.EventHandler(this.TextBoxRGBTextChanged);
this.textBoxBlue.TextChanged += new System.EventHandler(this.TextBoxRgbTextChanged);
this.textBoxBlue.GotFocus += new System.EventHandler(this.TextBoxGotFocus);
this.textBoxBlue.KeyDown += new System.Windows.Forms.KeyEventHandler(this.TextBoxKeyDown);
//
@ -189,7 +189,7 @@ namespace Greenshot {
this.textBoxAlpha.TabIndex = 5;
this.textBoxAlpha.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
this.textBoxAlpha.Click += new System.EventHandler(this.TextBoxGotFocus);
this.textBoxAlpha.TextChanged += new System.EventHandler(this.TextBoxRGBTextChanged);
this.textBoxAlpha.TextChanged += new System.EventHandler(this.TextBoxRgbTextChanged);
this.textBoxAlpha.GotFocus += new System.EventHandler(this.TextBoxGotFocus);
this.textBoxAlpha.KeyDown += new System.Windows.Forms.KeyEventHandler(this.TextBoxKeyDown);
//

View file

@ -34,8 +34,8 @@ namespace Greenshot {
/// Description of ColorDialog.
/// </summary>
public partial class ColorDialog : BaseForm {
private static ColorDialog uniqueInstance;
private static readonly EditorConfiguration editorConfiguration = IniConfig.GetIniSection<EditorConfiguration>();
private static ColorDialog _uniqueInstance;
private static readonly EditorConfiguration EditorConfig = IniConfig.GetIniSection<EditorConfiguration>();
private ColorDialog() {
SuspendLayout();
@ -47,11 +47,9 @@ namespace Greenshot {
UpdateRecentColorsButtonRow();
}
public static ColorDialog GetInstance() {
if (uniqueInstance == null) {
uniqueInstance = new ColorDialog();
}
return uniqueInstance;
public static ColorDialog GetInstance()
{
return _uniqueInstance ?? (_uniqueInstance = new ColorDialog());
}
private readonly List<Button> _colorButtons = new List<Button>();
@ -108,13 +106,15 @@ namespace Greenshot {
}
private Button CreateColorButton(Color color, int x, int y, int w, int h) {
Button b = new Button();
b.BackColor = color;
Button b = new Button
{
BackColor = color,
FlatStyle = FlatStyle.Flat,
Location = new Point(x, y),
Size = new Size(w, h),
TabStop = false
};
b.FlatAppearance.BorderSize = 0;
b.FlatStyle = FlatStyle.Flat;
b.Location = new Point(x, y);
b.Size = new Size(w, h);
b.TabStop = false;
b.Click += ColorButtonClick;
_toolTip.SetToolTip(b, ColorTranslator.ToHtml(color) + " | R:" + color.R + ", G:" + color.G + ", B:" + color.B);
return b;
@ -133,8 +133,8 @@ namespace Greenshot {
#region update user interface
private void UpdateRecentColorsButtonRow() {
for (int i = 0; i < editorConfiguration.RecentColors.Count && i < 12; i++) {
_recentColorButtons[i].BackColor = editorConfiguration.RecentColors[i];
for (int i = 0; i < EditorConfig.RecentColors.Count && i < 12; i++) {
_recentColorButtons[i].BackColor = EditorConfig.RecentColors[i];
_recentColorButtons[i].Enabled = true;
}
}
@ -155,10 +155,10 @@ namespace Greenshot {
}
private void AddToRecentColors(Color c) {
editorConfiguration.RecentColors.Remove(c);
editorConfiguration.RecentColors.Insert(0, c);
if (editorConfiguration.RecentColors.Count > 12) {
editorConfiguration.RecentColors.RemoveRange(12, editorConfiguration.RecentColors.Count - 12);
EditorConfig.RecentColors.Remove(c);
EditorConfig.RecentColors.Insert(0, c);
if (EditorConfig.RecentColors.Count > 12) {
EditorConfig.RecentColors.RemoveRange(12, EditorConfig.RecentColors.Count - 12);
}
UpdateRecentColorsButtonRow();
}
@ -172,14 +172,14 @@ namespace Greenshot {
}
TextBox textBox = (TextBox)sender;
string text = textBox.Text.Replace("#", "");
int i = 0;
int i;
Color c;
if (Int32.TryParse(text, NumberStyles.AllowHexSpecifier, Thread.CurrentThread.CurrentCulture, out i)) {
if (int.TryParse(text, NumberStyles.AllowHexSpecifier, Thread.CurrentThread.CurrentCulture, out i)) {
c = Color.FromArgb(i);
} else {
KnownColor knownColor;
try {
knownColor = (KnownColor)Enum.Parse(typeof(KnownColor), text, true);
try
{
var knownColor = (KnownColor)Enum.Parse(typeof(KnownColor), text, true);
c = Color.FromKnownColor(knownColor);
} catch (Exception) {
return;
@ -189,7 +189,7 @@ namespace Greenshot {
PreviewColor(opaqueColor, textBox);
}
private void TextBoxRGBTextChanged(object sender, EventArgs e) {
private void TextBoxRgbTextChanged(object sender, EventArgs e) {
if (_updateInProgress) {
return;
}
@ -228,10 +228,16 @@ namespace Greenshot {
#region helper functions
private int GetColorPartIntFromString(string s) {
int ret = 0;
Int32.TryParse(s, out ret);
if (ret < 0) ret = 0;
else if (ret > 255) ret = 255;
int ret;
int.TryParse(s, out ret);
if (ret < 0)
{
ret = 0;
}
else if (ret > 255)
{
ret = 255;
}
return ret;
}

View file

@ -21,14 +21,14 @@
using System;
using System.Drawing;
using System.Windows.Forms;
using Greenshot.Core;
using GreenshotPlugin.Effects;
namespace Greenshot.Forms {
public partial class DropShadowSettingsForm : BaseForm {
private readonly DropShadowEffect effect;
private readonly DropShadowEffect _effect;
public DropShadowSettingsForm(DropShadowEffect effect) {
this.effect = effect;
_effect = effect;
InitializeComponent();
ShowSettings();
}
@ -37,16 +37,16 @@ namespace Greenshot.Forms {
/// Apply the settings from the effect to the view
/// </summary>
private void ShowSettings() {
trackBar1.Value = (int)(effect.Darkness * 40);
offsetX.Value = effect.ShadowOffset.X;
offsetY.Value = effect.ShadowOffset.Y;
thickness.Value = effect.ShadowSize;
trackBar1.Value = (int)(_effect.Darkness * 40);
offsetX.Value = _effect.ShadowOffset.X;
offsetY.Value = _effect.ShadowOffset.Y;
thickness.Value = _effect.ShadowSize;
}
private void ButtonOK_Click(object sender, EventArgs e) {
effect.Darkness = (float)trackBar1.Value / (float)40;
effect.ShadowOffset = new Point((int)offsetX.Value, (int)offsetY.Value);
effect.ShadowSize = (int)thickness.Value;
_effect.Darkness = trackBar1.Value / (float)40;
_effect.ShadowOffset = new Point((int)offsetX.Value, (int)offsetY.Value);
_effect.ShadowSize = (int)thickness.Value;
DialogResult = DialogResult.OK;
}
}

View file

@ -19,8 +19,15 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using Greenshot.Configuration;
using Greenshot.Core;
using Greenshot.Destinations;
using Greenshot.Drawing;
using Greenshot.Drawing.Fields;
@ -33,26 +40,19 @@ using Greenshot.Plugin;
using Greenshot.Plugin.Drawing;
using GreenshotPlugin.Controls;
using GreenshotPlugin.Core;
using GreenshotPlugin.Effects;
using GreenshotPlugin.Interfaces.Drawing;
using GreenshotPlugin.UnmanagedHelpers;
using log4net;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Threading;
using System.Windows.Forms;
namespace Greenshot {
/// <summary>
/// Description of ImageEditorForm.
/// </summary>
public partial class ImageEditorForm : BaseForm, IImageEditor {
private static readonly ILog LOG = LogManager.GetLogger(typeof(ImageEditorForm));
private static readonly ILog Log = LogManager.GetLogger(typeof(ImageEditorForm));
private static readonly EditorConfiguration EditorConfiguration = IniConfig.GetIniSection<EditorConfiguration>();
private static readonly List<string> IgnoreDestinations = new List<string>() { PickerDestination.DESIGNATION, EditorDestination.DESIGNATION };
private static readonly List<string> IgnoreDestinations = new List<string> { PickerDestination.DESIGNATION, EditorDestination.DESIGNATION };
private static readonly List<IImageEditor> EditorList = new List<IImageEditor>();
private Surface _surface;
@ -69,18 +69,14 @@ namespace Greenshot {
/// <summary>
/// An Implementation for the IImageEditor, this way Plugins have access to the HWND handles wich can be used with Win32 API calls.
/// </summary>
public IWin32Window WindowHandle {
get { return this; }
}
public IWin32Window WindowHandle => this;
public static List<IImageEditor> Editors {
get {
try {
EditorList.Sort(delegate(IImageEditor e1, IImageEditor e2) {
return string.Compare(e1.Surface.CaptureDetails.Title, e2.Surface.CaptureDetails.Title, StringComparison.Ordinal);
});
EditorList.Sort((e1, e2) => string.Compare(e1.Surface.CaptureDetails.Title, e2.Surface.CaptureDetails.Title, StringComparison.Ordinal));
} catch(Exception ex) {
LOG.Warn("Sorting of editors failed.", ex);
Log.Warn("Sorting of editors failed.", ex);
}
return EditorList;
}
@ -104,7 +100,7 @@ namespace Greenshot {
};
// Make sure the editor is placed on the same location as the last editor was on close
// But only if this still exists, else it will be reset (BUG-1812
// But only if this still exists, else it will be reset (BUG-1812)
WindowPlacement editorWindowPlacement = EditorConfiguration.GetEditorPlacement();
Rectangle screenbounds = WindowCapture.GetScreenBounds();
if (!screenbounds.Contains(editorWindowPlacement.NormalPosition))
@ -132,12 +128,14 @@ namespace Greenshot {
/// Remove the current surface
/// </summary>
private void RemoveSurface() {
if (_surface != null) {
if (_surface == null)
{
return;
}
panel1.Controls.Remove(_surface);
_surface.Dispose();
_surface = null;
}
}
/// <summary>
/// Change the surface
@ -174,7 +172,7 @@ namespace Greenshot {
BindFieldControls();
RefreshEditorControls();
// Fix title
if (_surface != null && _surface.CaptureDetails != null && _surface.CaptureDetails.Title != null) {
if (_surface?.CaptureDetails?.Title != null) {
Text = _surface.CaptureDetails.Title + " - " + Language.GetString(LangKey.editor_title);
}
}
@ -249,7 +247,7 @@ namespace Greenshot {
if (destination.Priority <= 2) {
continue;
}
if (!destination.isActive) {
if (!destination.IsActive) {
continue;
}
if (destination.DisplayIcon == null) {
@ -258,15 +256,15 @@ namespace Greenshot {
try {
AddDestinationButton(destination);
} catch (Exception addingException) {
LOG.WarnFormat("Problem adding destination {0}", destination.Designation);
LOG.Warn("Exception: ", addingException);
Log.WarnFormat("Problem adding destination {0}", destination.Designation);
Log.Warn("Exception: ", addingException);
}
}
});
}
private void AddDestinationButton(IDestination toolstripDestination) {
if (toolstripDestination.isDynamic) {
if (toolstripDestination.IsDynamic) {
ToolStripSplitButton destinationButton = new ToolStripSplitButton
{
DisplayStyle = ToolStripItemDisplayStyle.Image,
@ -302,9 +300,11 @@ namespace Greenshot {
subDestinations.Sort();
foreach(IDestination subDestination in subDestinations) {
IDestination closureFixedDestination = subDestination;
ToolStripMenuItem destinationMenuItem = new ToolStripMenuItem(closureFixedDestination.Description);
destinationMenuItem.Tag = closureFixedDestination;
destinationMenuItem.Image = closureFixedDestination.DisplayIcon;
ToolStripMenuItem destinationMenuItem = new ToolStripMenuItem(closureFixedDestination.Description)
{
Tag = closureFixedDestination,
Image = closureFixedDestination.DisplayIcon
};
destinationMenuItem.Click += delegate {
closureFixedDestination.ExportCapture(true, _surface, _surface.CaptureDetails);
};
@ -351,7 +351,7 @@ namespace Greenshot {
if (IgnoreDestinations.Contains(destination.Designation)) {
continue;
}
if (!destination.isActive) {
if (!destination.IsActive) {
continue;
}
@ -375,20 +375,20 @@ namespace Greenshot {
/// <param name="eventArgs"></param>
private void SurfaceMessageReceived(object sender, SurfaceMessageEventArgs eventArgs) {
if (InvokeRequired) {
Invoke(new SurfaceMessageReceivedThreadSafeDelegate(SurfaceMessageReceived), new object[] { sender, eventArgs });
Invoke(new SurfaceMessageReceivedThreadSafeDelegate(SurfaceMessageReceived), sender, eventArgs);
} else {
string dateTime = DateTime.Now.ToLongTimeString();
// TODO: Fix that we only open files, like in the tooltip
switch (eventArgs.MessageType) {
case SurfaceMessageTyp.FileSaved:
// Put the event message on the status label and attach the context menu
updateStatusLabel(dateTime + " - " + eventArgs.Message, fileSavedStatusContextMenu);
UpdateStatusLabel(dateTime + " - " + eventArgs.Message, fileSavedStatusContextMenu);
// Change title
Text = eventArgs.Surface.LastSaveFullPath + " - " + Language.GetString(LangKey.editor_title);
break;
default:
// Put the event message on the status label
updateStatusLabel(dateTime + " - " + eventArgs.Message);
UpdateStatusLabel(dateTime + " - " + eventArgs.Message);
break;
}
}
@ -434,7 +434,7 @@ namespace Greenshot {
if (fullpath == null) {
return;
}
updateStatusLabel(Language.GetFormattedString(LangKey.editor_imagesaved, fullpath), fileSavedStatusContextMenu);
UpdateStatusLabel(Language.GetFormattedString(LangKey.editor_imagesaved, fullpath), fileSavedStatusContextMenu);
Text = Path.GetFileName(fullpath) + " - " + Language.GetString(LangKey.editor_title);
}
@ -489,9 +489,7 @@ namespace Greenshot {
return _surface.GetImageForExport();
}
public ICaptureDetails CaptureDetails {
get { return _surface.CaptureDetails; }
}
public ICaptureDetails CaptureDetails => _surface.CaptureDetails;
public ToolStripMenuItem GetPluginMenuItem() {
return pluginToolStripMenuItem;
@ -902,7 +900,7 @@ namespace Greenshot {
if (IgnoreDestinations.Contains(destination.Designation)) {
continue;
}
if (!destination.isActive) {
if (!destination.IsActive) {
continue;
}
@ -981,23 +979,18 @@ namespace Greenshot {
#endregion
#region status label handling
private void updateStatusLabel(string text, ContextMenuStrip contextMenu) {
private void UpdateStatusLabel(string text, ContextMenuStrip contextMenu = null) {
statusLabel.Text = text;
statusStrip1.ContextMenuStrip = contextMenu;
}
private void updateStatusLabel(string text) {
updateStatusLabel(text, null);
}
private void ClearStatusLabel() {
updateStatusLabel(null, null);
UpdateStatusLabel(null);
}
private void StatusLabelClicked(object sender, MouseEventArgs e) {
ToolStrip ss = (StatusStrip)((ToolStripStatusLabel)sender).Owner;
if(ss.ContextMenuStrip != null) {
ss.ContextMenuStrip.Show(ss, e.X, e.Y);
}
ss.ContextMenuStrip?.Show(ss, e.X, e.Y);
}
private void CopyPathMenuItemClick(object sender, EventArgs e) {
@ -1005,14 +998,19 @@ namespace Greenshot {
}
private void OpenDirectoryMenuItemClick(object sender, EventArgs e) {
ProcessStartInfo psi = new ProcessStartInfo("explorer")
var path = Path.GetDirectoryName(_surface.LastSaveFullPath);
if (path == null)
{
Arguments = Path.GetDirectoryName(_surface.LastSaveFullPath),
return;
}
var processStartInfo = new ProcessStartInfo("explorer")
{
Arguments = path,
UseShellExecute = false
};
using (Process p = new Process()) {
p.StartInfo = psi;
p.Start();
using (var process = new Process()) {
process.StartInfo = processStartInfo;
process.Start();
}
}
#endregion
@ -1084,7 +1082,7 @@ namespace Greenshot {
int stepLabels = _surface.CountStepLabels(null);
Image icon;
if (stepLabels <= 20) {
icon = (Image)resources.GetObject(string.Format("btnStepLabel{0:00}.Image", stepLabels));
icon = (Image)resources.GetObject($"btnStepLabel{stepLabels:00}.Image");
} else {
icon = (Image)resources.GetObject("btnStepLabel20+.Image");
}
@ -1156,24 +1154,42 @@ namespace Greenshot {
fontItalicButton.Checked = _originalItalicCheckState;
}
FontFamily fam = fontFamilyComboBox.FontFamily;
var fontFamily = fontFamilyComboBox.FontFamily;
bool boldAvailable = fam.IsStyleAvailable(FontStyle.Bold);
if(!boldAvailable) {
bool boldAvailable = fontFamily.IsStyleAvailable(FontStyle.Bold);
if (fontBoldButton != null)
{
if (!boldAvailable)
{
_originalBoldCheckState = fontBoldButton.Checked;
fontBoldButton.Checked = false;
}
fontBoldButton.Enabled = boldAvailable;
}
bool italicAvailable = fam.IsStyleAvailable(FontStyle.Italic);
if(!italicAvailable) fontItalicButton.Checked = false;
bool italicAvailable = fontFamily.IsStyleAvailable(FontStyle.Italic);
if (fontItalicButton != null)
{
if (!italicAvailable)
{
fontItalicButton.Checked = false;
}
fontItalicButton.Enabled = italicAvailable;
}
bool regularAvailable = fam.IsStyleAvailable(FontStyle.Regular);
if(!regularAvailable) {
bool regularAvailable = fontFamily.IsStyleAvailable(FontStyle.Regular);
if (regularAvailable)
{
return;
}
if (boldAvailable) {
if (fontBoldButton != null)
{
fontBoldButton.Checked = true;
}
} else if(italicAvailable) {
if (fontItalicButton != null)
{
fontItalicButton.Checked = true;
}
}
@ -1182,7 +1198,7 @@ namespace Greenshot {
private void FieldAggregatorFieldChanged(object sender, FieldChangedEventArgs e) {
// in addition to selection, deselection of elements, we need to
// refresh toolbar if prepared filter mode is changed
if(e.Field.FieldType == FieldType.PREPARED_FILTER_HIGHLIGHT) {
if(Equals(e.Field.FieldType, FieldType.PREPARED_FILTER_HIGHLIGHT)) {
RefreshFieldControls();
}
}
@ -1204,9 +1220,11 @@ namespace Greenshot {
}
private void SaveElementsToolStripMenuItemClick(object sender, EventArgs e) {
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "Greenshot templates (*.gst)|*.gst";
saveFileDialog.FileName = FilenameHelper.GetFilenameWithoutExtensionFromPattern(coreConfiguration.OutputFileFilenamePattern, _surface.CaptureDetails);
SaveFileDialog saveFileDialog = new SaveFileDialog
{
Filter = "Greenshot templates (*.gst)|*.gst",
FileName = FilenameHelper.GetFilenameWithoutExtensionFromPattern(coreConfiguration.OutputFileFilenamePattern, _surface.CaptureDetails)
};
DialogResult dialogResult = saveFileDialog.ShowDialog();
if(dialogResult.Equals(DialogResult.OK)) {
using (Stream streamWrite = File.OpenWrite(saveFileDialog.FileName)) {
@ -1216,8 +1234,10 @@ namespace Greenshot {
}
private void LoadElementsToolStripMenuItemClick(object sender, EventArgs e) {
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Greenshot templates (*.gst)|*.gst";
OpenFileDialog openFileDialog = new OpenFileDialog
{
Filter = "Greenshot templates (*.gst)|*.gst"
};
if (openFileDialog.ShowDialog() == DialogResult.OK) {
using (Stream streamRead = File.OpenRead(openFileDialog.FileName)) {
_surface.LoadElementsFromStream(streamRead);
@ -1228,24 +1248,28 @@ namespace Greenshot {
private void DestinationToolStripMenuItemClick(object sender, EventArgs e) {
IDestination clickedDestination = null;
if (sender is Control) {
Control clickedControl = sender as Control;
var control = sender as Control;
if (control != null) {
Control clickedControl = control;
if (clickedControl.ContextMenuStrip != null) {
clickedControl.ContextMenuStrip.Show(Cursor.Position);
return;
}
clickedDestination = (IDestination)clickedControl.Tag;
} else if (sender is ToolStripMenuItem) {
ToolStripMenuItem clickedMenuItem = sender as ToolStripMenuItem;
}
else
{
var item = sender as ToolStripMenuItem;
if (item != null) {
ToolStripMenuItem clickedMenuItem = item;
clickedDestination = (IDestination)clickedMenuItem.Tag;
}
if (clickedDestination != null) {
ExportInformation exportInformation = clickedDestination.ExportCapture(true, _surface, _surface.CaptureDetails);
}
ExportInformation exportInformation = clickedDestination?.ExportCapture(true, _surface, _surface.CaptureDetails);
if (exportInformation != null && exportInformation.ExportMade) {
_surface.Modified = false;
}
}
}
protected void FilterPresetDropDownItemClicked(object sender, ToolStripItemClickedEventArgs e) {
RefreshFieldControls();
@ -1284,7 +1308,7 @@ namespace Greenshot {
windowToCapture = CaptureHelper.SelectCaptureWindow(windowToCapture);
if (windowToCapture != null) {
capture = CaptureHelper.CaptureWindow(windowToCapture, capture, coreConfiguration.WindowCaptureMode);
if (capture != null && capture.CaptureDetails != null && capture.Image != null) {
if (capture?.CaptureDetails != null && capture.Image != null) {
((Bitmap)capture.Image).SetResolution(capture.CaptureDetails.DpiX, capture.CaptureDetails.DpiY);
_surface.AddImageContainer((Bitmap)capture.Image, 100, 100);
}
@ -1292,11 +1316,9 @@ namespace Greenshot {
WindowDetails.ToForeground(Handle);
}
if (capture!= null) {
capture.Dispose();
}
capture?.Dispose();
} catch (Exception exception) {
LOG.Error(exception);
Log.Error(exception);
}
}
@ -1440,17 +1462,13 @@ namespace Greenshot {
}
private void ImageEditorFormResize(object sender, EventArgs e) {
if (Surface == null || Surface.Image == null || panel1 == null) {
if (Surface?.Image == null || panel1 == null) {
return;
}
Size imageSize = Surface.Image.Size;
Size currentClientSize = panel1.ClientSize;
var canvas = Surface as Control;
if (canvas == null)
{
return;
}
Panel panel = (Panel)canvas.Parent;
Panel panel = (Panel) canvas?.Parent;
if (panel == null) {
return;
}

View file

@ -162,7 +162,7 @@ namespace Greenshot {
//
this.contextmenu_captureiefromlist.Name = "contextmenu_captureiefromlist";
this.contextmenu_captureiefromlist.Size = new System.Drawing.Size(170, 22);
this.contextmenu_captureiefromlist.DropDownOpening += new System.EventHandler(this.CaptureIEMenuDropDownOpening);
this.contextmenu_captureiefromlist.DropDownOpening += new System.EventHandler(this.CaptureIeMenuDropDownOpening);
//
// toolStripOtherSourcesSeparator
//

View file

@ -56,15 +56,14 @@ namespace Greenshot {
private static CoreConfiguration _conf;
public static string LogFileLocation;
public static void Start(string[] args) {
bool isAlreadyRunning = false;
List<string> filesToOpen = new List<string>();
public static void Start(string[] arguments) {
var filesToOpen = new List<string>();
// Set the Thread name, is better than "1"
Thread.CurrentThread.Name = Application.ProductName;
// Init Log4NET
LogFileLocation = LogHelper.InitializeLog4NET();
LogFileLocation = LogHelper.InitializeLog4Net();
// Get logger
LOG = LogManager.GetLogger(typeof(MainForm));
@ -84,18 +83,19 @@ namespace Greenshot {
// check whether there's an local instance running already
_applicationMutex = ResourceMutex.Create("F48E86D3-E34C-4DB7-8F8F-9A0EA55F0D08", "Greenshot", false);
isAlreadyRunning = !_applicationMutex.IsLocked;
var isAlreadyRunning = !_applicationMutex.IsLocked;
if (args.Length > 0 && LOG.IsDebugEnabled) {
if (arguments.Length > 0 && LOG.IsDebugEnabled) {
StringBuilder argumentString = new StringBuilder();
for(int argumentNr = 0; argumentNr < args.Length; argumentNr++) {
argumentString.Append("[").Append(args[argumentNr]).Append("] ");
foreach (string argument in arguments)
{
argumentString.Append("[").Append(argument).Append("] ");
}
LOG.Debug("Greenshot arguments: " + argumentString);
}
for(int argumentNr = 0; argumentNr < args.Length; argumentNr++) {
string argument = args[argumentNr];
for(int argumentNr = 0; argumentNr < arguments.Length; argumentNr++) {
string argument = arguments[argumentNr];
// Help
if (argument.ToLower().Equals("/help") || argument.ToLower().Equals("/h") || argument.ToLower().Equals("/?")) {
// Try to attach to the console
@ -104,7 +104,7 @@ namespace Greenshot {
if (!attachedToConsole) {
Kernel32.AllocConsole();
}
StringBuilder helpOutput = new StringBuilder();
var helpOutput = new StringBuilder();
helpOutput.AppendLine();
helpOutput.AppendLine("Greenshot commandline options:");
helpOutput.AppendLine();
@ -173,14 +173,14 @@ namespace Greenshot {
// Language
if (argument.ToLower().Equals("/language")) {
_conf.Language = args[++argumentNr];
_conf.Language = arguments[++argumentNr];
IniConfig.Save();
continue;
}
// Setting the INI-directory
if (argument.ToLower().Equals("/inidirectory")) {
IniConfig.IniDirectory = args[++argumentNr];
IniConfig.IniDirectory = arguments[++argumentNr];
continue;
}
@ -296,11 +296,7 @@ namespace Greenshot {
}
private static MainForm _instance;
public static MainForm Instance {
get {
return _instance;
}
}
public static MainForm Instance => _instance;
private readonly CopyData _copyData;
@ -313,11 +309,7 @@ namespace Greenshot {
// Timer for the double click test
private readonly Timer _doubleClickTimer = new Timer();
public NotifyIcon NotifyIcon {
get {
return notifyIcon;
}
}
public NotifyIcon NotifyIcon => notifyIcon;
public MainForm(CopyDataTransport dataTransport) {
_instance = this;
@ -341,12 +333,12 @@ namespace Greenshot {
contextmenu_settings.Visible = !_conf.DisableSettings;
// Make sure all hotkeys pass this window!
HotkeyControl.RegisterHotkeyHWND(Handle);
HotkeyControl.RegisterHotkeyHwnd(Handle);
RegisterHotkeys();
new ToolTip();
UpdateUI();
UpdateUi();
// This forces the registration of all destinations inside Greenshot itself.
DestinationHelper.GetAllDestinations();
@ -452,7 +444,7 @@ namespace Greenshot {
IniConfig.Reload();
Invoke((MethodInvoker) delegate {
// Even update language when needed
UpdateUI();
UpdateUi();
// Update the hotkey
// Make sure the current hotkeys are disabled
HotkeyControl.UnregisterHotkeys();
@ -480,9 +472,10 @@ namespace Greenshot {
}
}
public ContextMenuStrip MainMenu {
get {return contextMenu;}
}
/// <summary>
/// Main context menu
/// </summary>
public ContextMenuStrip MainMenu => contextMenu;
protected override void WndProc(ref Message m) {
if (HotkeyControl.HandleMessages(ref m)) {
@ -659,7 +652,7 @@ namespace Greenshot {
}
#endregion
public void UpdateUI() {
public void UpdateUi() {
// As the form is never loaded, call ApplyLanguage ourselves
ApplyLanguage();
@ -693,8 +686,10 @@ namespace Greenshot {
}
private void CaptureFile() {
var openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Image files (*.greenshot, *.png, *.jpg, *.gif, *.bmp, *.ico, *.tiff, *.wmf)|*.greenshot; *.png; *.jpg; *.jpeg; *.gif; *.bmp; *.ico; *.tiff; *.tif; *.wmf";
var openFileDialog = new OpenFileDialog
{
Filter = "Image files (*.greenshot, *.png, *.jpg, *.gif, *.bmp, *.ico, *.tiff, *.wmf)|*.greenshot; *.png; *.jpg; *.jpeg; *.gif; *.bmp; *.ico; *.tiff; *.tif; *.wmf"
};
if (openFileDialog.ShowDialog() == DialogResult.OK) {
if (File.Exists(openFileDialog.FileName)) {
CaptureHelper.CaptureFile(openFileDialog.FileName);
@ -712,7 +707,7 @@ namespace Greenshot {
private void CaptureIE() {
if (_conf.IECapture) {
CaptureHelper.CaptureIE(true, null);
CaptureHelper.CaptureIe(true, null);
}
}
@ -734,7 +729,7 @@ namespace Greenshot {
// IE context menu code
try {
if (_conf.IECapture && IECaptureHelper.IsIERunning()) {
if (_conf.IECapture && IeCaptureHelper.IsIeRunning()) {
contextmenu_captureie.Enabled = true;
contextmenu_captureiefromlist.Enabled = true;
} else {
@ -773,12 +768,12 @@ namespace Greenshot {
/// <summary>
/// Build a selectable list of IE tabs when we enter the menu item
/// </summary>
private void CaptureIEMenuDropDownOpening(object sender, EventArgs e) {
private void CaptureIeMenuDropDownOpening(object sender, EventArgs e) {
if (!_conf.IECapture) {
return;
}
try {
List<KeyValuePair<WindowDetails, string>> tabs = IECaptureHelper.GetBrowserTabs();
List<KeyValuePair<WindowDetails, string>> tabs = IeCaptureHelper.GetBrowserTabs();
contextmenu_captureiefromlist.DropDownItems.Clear();
if (tabs.Count > 0) {
contextmenu_captureie.Enabled = true;
@ -793,12 +788,12 @@ namespace Greenshot {
if (title.Length > _conf.MaxMenuItemLength) {
title = title.Substring(0, Math.Min(title.Length, _conf.MaxMenuItemLength));
}
ToolStripItem captureIETabItem = contextmenu_captureiefromlist.DropDownItems.Add(title);
var captureIeTabItem = contextmenu_captureiefromlist.DropDownItems.Add(title);
int index = counter.ContainsKey(tabData.Key) ? counter[tabData.Key] : 0;
captureIETabItem.Image = tabData.Key.DisplayIcon;
captureIETabItem.Tag = new KeyValuePair<WindowDetails, int>(tabData.Key, index++);
captureIETabItem.Click += Contextmenu_captureiefromlist_Click;
contextmenu_captureiefromlist.DropDownItems.Add(captureIETabItem);
captureIeTabItem.Image = tabData.Key.DisplayIcon;
captureIeTabItem.Tag = new KeyValuePair<WindowDetails, int>(tabData.Key, index++);
captureIeTabItem.Click += Contextmenu_captureiefromlist_Click;
contextmenu_captureiefromlist.DropDownItems.Add(captureIeTabItem);
if (counter.ContainsKey(tabData.Key)) {
counter[tabData.Key] = index;
} else {
@ -823,10 +818,9 @@ namespace Greenshot {
ToolStripMenuItem captureScreenMenuItem = (ToolStripMenuItem)sender;
captureScreenMenuItem.DropDownItems.Clear();
if (Screen.AllScreens.Length > 1) {
ToolStripMenuItem captureScreenItem;
Rectangle allScreensBounds = WindowCapture.GetScreenBounds();
captureScreenItem = new ToolStripMenuItem(Language.GetString(LangKey.contextmenu_capturefullscreen_all));
var captureScreenItem = new ToolStripMenuItem(Language.GetString(LangKey.contextmenu_capturefullscreen_all));
captureScreenItem.Click += delegate {
BeginInvoke((MethodInvoker)delegate {
CaptureHelper.CaptureFullscreen(false, ScreenCaptureMode.FullScreen);
@ -893,18 +887,19 @@ namespace Greenshot {
}
}
private void HideThumbnailOnLeave(object sender, EventArgs e) {
if (_thumbnailForm != null) {
_thumbnailForm.Hide();
}
private void HideThumbnailOnLeave(object sender, EventArgs e)
{
_thumbnailForm?.Hide();
}
private void CleanupThumbnail() {
if (_thumbnailForm != null) {
if (_thumbnailForm == null)
{
return;
}
_thumbnailForm.Close();
_thumbnailForm = null;
}
}
public void AddCaptureWindowMenuItems(ToolStripMenuItem menuItem, EventHandler eventHandler) {
menuItem.DropDownItems.Clear();
@ -938,15 +933,11 @@ namespace Greenshot {
}
private void CaptureClipboardToolStripMenuItemClick(object sender, EventArgs e) {
BeginInvoke((MethodInvoker)delegate {
CaptureHelper.CaptureClipboard();
});
BeginInvoke((MethodInvoker)CaptureHelper.CaptureClipboard);
}
private void OpenFileToolStripMenuItemClick(object sender, EventArgs e) {
BeginInvoke((MethodInvoker)delegate {
CaptureFile();
});
BeginInvoke((MethodInvoker)CaptureFile);
}
private void CaptureFullScreenToolStripMenuItemClick(object sender, EventArgs e) {
@ -996,12 +987,12 @@ namespace Greenshot {
ieWindowToCapture.Restore();
}
try {
IECaptureHelper.ActivateIETab(ieWindowToCapture, tabData.Value);
IeCaptureHelper.ActivateIeTab(ieWindowToCapture, tabData.Value);
} catch (Exception exception) {
LOG.Error(exception);
}
try {
CaptureHelper.CaptureIE(false, ieWindowToCapture);
CaptureHelper.CaptureIe(false, ieWindowToCapture);
} catch (Exception exception) {
LOG.Error(exception);
}
@ -1025,9 +1016,7 @@ namespace Greenshot {
/// <param name="sender"></param>
/// <param name="e"></param>
private void Contextmenu_settingsClick(object sender, EventArgs e) {
BeginInvoke((MethodInvoker)delegate {
ShowSetting();
});
BeginInvoke((MethodInvoker)ShowSetting);
}
/// <summary>
@ -1110,10 +1099,12 @@ namespace Greenshot {
// Only add if the value is not fixed
if (!_conf.Values["CaptureMousepointer"].IsFixed) {
// For the capture mousecursor option
ToolStripMenuSelectListItem captureMouseItem = new ToolStripMenuSelectListItem();
captureMouseItem.Text = Language.GetString("settings_capture_mousepointer");
captureMouseItem.Checked = _conf.CaptureMousepointer;
captureMouseItem.CheckOnClick = true;
ToolStripMenuSelectListItem captureMouseItem = new ToolStripMenuSelectListItem
{
Text = Language.GetString("settings_capture_mousepointer"),
Checked = _conf.CaptureMousepointer,
CheckOnClick = true
};
captureMouseItem.CheckStateChanged += CheckStateChangedHandler;
contextmenu_quicksettings.DropDownItems.Add(captureMouseItem);
@ -1121,10 +1112,12 @@ namespace Greenshot {
ToolStripMenuSelectList selectList;
if (!_conf.Values["Destinations"].IsFixed) {
// screenshot destination
selectList = new ToolStripMenuSelectList("destinations", true);
selectList.Text = Language.GetString(LangKey.settings_destination);
selectList = new ToolStripMenuSelectList("destinations", true)
{
Text = Language.GetString(LangKey.settings_destination)
};
// Working with IDestination:
foreach (IDestination destination in DestinationHelper.GetAllDestinations()) {
foreach (var destination in DestinationHelper.GetAllDestinations()) {
selectList.AddItem(destination.Description, destination, _conf.OutputDestinations.Contains(destination.Designation));
}
selectList.CheckedChanged += QuickSettingDestinationChanged;
@ -1133,19 +1126,23 @@ namespace Greenshot {
if (!_conf.Values["WindowCaptureMode"].IsFixed) {
// Capture Modes
selectList = new ToolStripMenuSelectList("capturemodes", false);
selectList.Text = Language.GetString(LangKey.settings_window_capture_mode);
selectList = new ToolStripMenuSelectList("capturemodes", false)
{
Text = Language.GetString(LangKey.settings_window_capture_mode)
};
string enumTypeName = typeof(WindowCaptureMode).Name;
foreach (WindowCaptureMode captureMode in Enum.GetValues(typeof(WindowCaptureMode))) {
selectList.AddItem(Language.GetString(enumTypeName + "." + captureMode.ToString()), captureMode, _conf.WindowCaptureMode == captureMode);
selectList.AddItem(Language.GetString(enumTypeName + "." + captureMode), captureMode, _conf.WindowCaptureMode == captureMode);
}
selectList.CheckedChanged += QuickSettingCaptureModeChanged;
contextmenu_quicksettings.DropDownItems.Add(selectList);
}
// print options
selectList = new ToolStripMenuSelectList("printoptions",true);
selectList.Text = Language.GetString(LangKey.settings_printoptions);
selectList = new ToolStripMenuSelectList("printoptions", true)
{
Text = Language.GetString(LangKey.settings_printoptions)
};
IniValue iniValue;
foreach(string propertyName in _conf.Values.Keys) {
@ -1162,8 +1159,10 @@ namespace Greenshot {
}
// effects
selectList = new ToolStripMenuSelectList("effects",true);
selectList.Text = Language.GetString(LangKey.settings_visualization);
selectList = new ToolStripMenuSelectList("effects", true)
{
Text = Language.GetString(LangKey.settings_visualization)
};
iniValue = _conf.Values["PlayCameraSound"];
if (!iniValue.IsFixed) {
@ -1383,7 +1382,7 @@ namespace Greenshot {
// Close all open forms (except this), use a separate List to make sure we don't get a "InvalidOperationException: Collection was modified"
List<Form> formsToClose = new List<Form>();
foreach(Form form in Application.OpenForms) {
if (form.Handle != Handle && !form.GetType().Equals(typeof(ImageEditorForm))) {
if (form.Handle != Handle && form.GetType() != typeof(ImageEditorForm)) {
formsToClose.Add(form);
}
}
@ -1459,9 +1458,11 @@ namespace Greenshot {
if (UpdateHelper.IsUpdateCheckNeeded()) {
LOG.Debug("BackgroundWorkerTimerTick checking for update");
// Start update check in the background
Thread backgroundTask = new Thread (UpdateHelper.CheckAndAskForUpdate);
backgroundTask.Name = "Update check";
backgroundTask.IsBackground = true;
var backgroundTask = new Thread(UpdateHelper.CheckAndAskForUpdate)
{
Name = "Update check",
IsBackground = true
};
backgroundTask.Start();
}
}

View file

@ -86,7 +86,7 @@ namespace Greenshot.Forms {
/// <param name="screenCoordinates">Point with the coordinates</param>
/// <returns>Color at the specified screenCoordinates</returns>
private static Color GetPixelColor(Point screenCoordinates) {
using (SafeWindowDCHandle screenDC = SafeWindowDCHandle.FromDesktop()) {
using (SafeWindowDcHandle screenDC = SafeWindowDcHandle.FromDesktop()) {
try {
uint pixel = GDI32.GetPixel(screenDC, screenCoordinates.X, screenCoordinates.Y);
Color color = Color.FromArgb(255, (int)(pixel & 0xFF), (int)(pixel & 0xFF00) >> 8, (int)(pixel & 0xFF0000) >> 16);

View file

@ -21,137 +21,130 @@
using System;
using System.Drawing;
using System.Windows.Forms;
using Greenshot.Core;
using GreenshotPlugin.Core;
using GreenshotPlugin.Effects;
namespace Greenshot.Forms {
/// <summary>
/// A form to set the resize settings
/// </summary>
public partial class ResizeSettingsForm : BaseForm {
private readonly ResizeEffect effect;
private readonly string value_pixel;
private readonly string value_percent;
private double newWidth, newHeight;
private readonly ResizeEffect _effect;
private readonly string _valuePercent;
private double _newWidth, _newHeight;
public ResizeSettingsForm(ResizeEffect effect) {
this.effect = effect;
_effect = effect;
InitializeComponent();
value_pixel = Language.GetString("editor_resize_pixel");
value_percent = Language.GetString("editor_resize_percent");
combobox_width.Items.Add(value_pixel);
combobox_width.Items.Add(value_percent);
combobox_width.SelectedItem = value_pixel;
combobox_height.Items.Add(value_pixel);
combobox_height.Items.Add(value_percent);
combobox_height.SelectedItem = value_pixel;
var valuePixel = Language.GetString("editor_resize_pixel");
_valuePercent = Language.GetString("editor_resize_percent");
combobox_width.Items.Add(valuePixel);
combobox_width.Items.Add(_valuePercent);
combobox_width.SelectedItem = valuePixel;
combobox_height.Items.Add(valuePixel);
combobox_height.Items.Add(_valuePercent);
combobox_height.SelectedItem = valuePixel;
textbox_width.Text = effect.Width.ToString();
textbox_height.Text = effect.Height.ToString();
newWidth = effect.Width;
newHeight = effect.Height;
combobox_width.SelectedIndexChanged += new EventHandler(combobox_SelectedIndexChanged);
combobox_height.SelectedIndexChanged += new EventHandler(combobox_SelectedIndexChanged);
_newWidth = effect.Width;
_newHeight = effect.Height;
combobox_width.SelectedIndexChanged += combobox_SelectedIndexChanged;
combobox_height.SelectedIndexChanged += combobox_SelectedIndexChanged;
checkbox_aspectratio.Checked = effect.MaintainAspectRatio;
}
private void buttonOK_Click(object sender, EventArgs e) {
if (newWidth != effect.Width || newHeight != effect.Height) {
effect.Width = (int)newWidth;
effect.Height = (int)newHeight;
effect.MaintainAspectRatio = checkbox_aspectratio.Checked;
if (_newWidth != _effect.Width || _newHeight != _effect.Height) {
_effect.Width = (int)_newWidth;
_effect.Height = (int)_newHeight;
_effect.MaintainAspectRatio = checkbox_aspectratio.Checked;
DialogResult = DialogResult.OK;
}
}
private bool validate(object sender) {
private static bool Validate(object sender) {
TextBox textbox = sender as TextBox;
if (textbox != null) {
double numberEntered;
if (!double.TryParse(textbox.Text, out numberEntered)) {
textbox.BackColor = Color.Red;
return false;
} else {
textbox.BackColor = Color.White;
}
textbox.BackColor = Color.White;
}
return true;
}
private void displayWidth() {
private void DisplayWidth() {
double displayValue;
if (value_percent.Equals(combobox_width.SelectedItem)) {
displayValue = (double)newWidth / (double)effect.Width * 100d;
if (_valuePercent.Equals(combobox_width.SelectedItem)) {
displayValue = _newWidth / _effect.Width * 100d;
} else {
displayValue = newWidth;
displayValue = _newWidth;
}
textbox_width.Text = ((int)displayValue).ToString();
}
private void displayHeight() {
private void DisplayHeight() {
double displayValue;
if (value_percent.Equals(combobox_height.SelectedItem)) {
displayValue = (double)newHeight / (double)effect.Height * 100d;
if (_valuePercent.Equals(combobox_height.SelectedItem)) {
displayValue = _newHeight / _effect.Height * 100d;
} else {
displayValue = newHeight;
displayValue = _newHeight;
}
textbox_height.Text = ((int)displayValue).ToString();
}
private void textbox_KeyUp(object sender, KeyEventArgs e) {
if (!validate(sender)) {
if (!Validate(sender)) {
return;
}
TextBox textbox = sender as TextBox;
if (textbox.Text.Length == 0) {
if (string.IsNullOrEmpty(textbox?.Text)) {
return;
}
bool isWidth = textbox == textbox_width;
if (!checkbox_aspectratio.Checked) {
if (isWidth) {
newWidth = double.Parse(textbox_width.Text);
_newWidth = double.Parse(textbox_width.Text);
} else {
newHeight = double.Parse(textbox_height.Text);
_newHeight = double.Parse(textbox_height.Text);
}
return;
}
bool isPercent = false;
if (isWidth) {
isPercent = value_percent.Equals(combobox_width.SelectedItem);
} else {
isPercent = value_percent.Equals(combobox_height.SelectedItem);
}
var isPercent = _valuePercent.Equals(isWidth ? combobox_width.SelectedItem : combobox_height.SelectedItem);
double percent;
if (isWidth) {
if (isPercent) {
percent = double.Parse(textbox_width.Text);
newWidth = (double)effect.Width / 100d * percent;
_newWidth = _effect.Width / 100d * percent;
} else {
newWidth = double.Parse(textbox_width.Text);
percent = (double)double.Parse(textbox_width.Text) / (double)effect.Width * 100d;
_newWidth = double.Parse(textbox_width.Text);
percent = double.Parse(textbox_width.Text) / _effect.Width * 100d;
}
if (checkbox_aspectratio.Checked) {
newHeight = (double)effect.Height / 100d * percent;
displayHeight();
_newHeight = _effect.Height / 100d * percent;
DisplayHeight();
}
} else {
if (isPercent) {
percent = double.Parse(textbox_height.Text);
newHeight = (double)effect.Height / 100d * percent;
_newHeight = _effect.Height / 100d * percent;
} else {
newHeight = double.Parse(textbox_height.Text);
percent = (double)double.Parse(textbox_height.Text) / (double)effect.Height * 100d;
_newHeight = double.Parse(textbox_height.Text);
percent = double.Parse(textbox_height.Text) / _effect.Height * 100d;
}
if (checkbox_aspectratio.Checked) {
newWidth = (double)effect.Width / 100d * percent;
displayWidth();
_newWidth = _effect.Width / 100d * percent;
DisplayWidth();
}
}
}
private void textbox_Validating(object sender, System.ComponentModel.CancelEventArgs e) {
validate(sender);
Validate(sender);
}
/// <summary>
@ -160,11 +153,11 @@ namespace Greenshot.Forms {
/// <param name="sender"></param>
/// <param name="e"></param>
private void combobox_SelectedIndexChanged(object sender, EventArgs e) {
if (validate(textbox_width)) {
displayWidth();
if (Validate(textbox_width)) {
DisplayWidth();
}
if (validate(textbox_height)) {
displayHeight();
if (Validate(textbox_height)) {
DisplayHeight();
}
}
}

View file

@ -43,8 +43,7 @@ namespace Greenshot {
/// Description of SettingsForm.
/// </summary>
public partial class SettingsForm : BaseForm {
private static readonly ILog LOG = LogManager.GetLogger(typeof(SettingsForm));
private static EditorConfiguration editorConfiguration = IniConfig.GetIniSection<EditorConfiguration>();
private static readonly ILog Log = LogManager.GetLogger(typeof(SettingsForm));
private readonly ToolTip _toolTip = new ToolTip();
private bool _inHotkey;
@ -59,11 +58,7 @@ namespace Greenshot {
base.OnLoad(e);
// Fix for Vista/XP differences
if (Environment.OSVersion.Version.Major >= 6) {
trackBarJpegQuality.BackColor = SystemColors.Window;
} else {
trackBarJpegQuality.BackColor = SystemColors.Control;
}
trackBarJpegQuality.BackColor = Environment.OSVersion.Version.Major >= 6 ? SystemColors.Window : SystemColors.Control;
// This makes it possible to still capture the settings screen
fullscreen_hotkeyControl.Enter += EnterHotkeyControl;
@ -78,7 +73,7 @@ namespace Greenshot {
lastregion_hotkeyControl.Leave += LeaveHotkeyControl;
DisplayPluginTab();
UpdateUI();
UpdateUi();
ExpertSettingsEnableState(false);
DisplaySettings();
CheckSettings();
@ -116,9 +111,9 @@ namespace Greenshot {
/// <param name="comboBox">ComboBox to populate</param>
/// <param name="availableValues"></param>
/// <param name="selectedValue"></param>
private void PopulateComboBox<ET>(ComboBox comboBox, ET[] availableValues, ET selectedValue) where ET : struct {
private void PopulateComboBox<TEnum>(ComboBox comboBox, TEnum[] availableValues, TEnum selectedValue) where TEnum : struct {
comboBox.Items.Clear();
foreach(ET enumValue in availableValues) {
foreach(TEnum enumValue in availableValues) {
comboBox.Items.Add(Language.Translate(enumValue));
}
comboBox.SelectedItem = Language.Translate(selectedValue);
@ -130,13 +125,13 @@ namespace Greenshot {
/// </summary>
/// <param name="comboBox">Combobox to get the value from</param>
/// <returns>The generics value of the combobox</returns>
private ET GetSelected<ET>(ComboBox comboBox) {
string enumTypeName = typeof(ET).Name;
private TEnum GetSelected<TEnum>(ComboBox comboBox) {
string enumTypeName = typeof(TEnum).Name;
string selectedValue = comboBox.SelectedItem as string;
ET[] availableValues = (ET[])Enum.GetValues(typeof(ET));
ET returnValue = availableValues[0];
foreach(ET enumValue in availableValues) {
string translation = Language.GetString(enumTypeName + "." + enumValue.ToString());
TEnum[] availableValues = (TEnum[])Enum.GetValues(typeof(TEnum));
TEnum returnValue = availableValues[0];
foreach(TEnum enumValue in availableValues) {
string translation = Language.GetString(enumTypeName + "." + enumValue);
if (translation.Equals(selectedValue)) {
returnValue = enumValue;
break;
@ -196,7 +191,7 @@ namespace Greenshot {
/// <summary>
/// Update the UI to reflect the language and other text settings
/// </summary>
private void UpdateUI() {
private void UpdateUi() {
if (coreConfiguration.HideExpertSettings) {
tabcontrol.Controls.Remove(tab_expert);
}
@ -230,13 +225,12 @@ namespace Greenshot {
}
private bool CheckFilenamePattern() {
bool settingsOk = true;
string filename = FilenameHelper.GetFilenameFromPattern(textbox_screenshotname.Text, coreConfiguration.OutputFileFormat, null);
// we allow dynamically created subfolders, need to check for them, too
string[] pathParts = filename.Split(Path.DirectorySeparatorChar);
string filenamePart = pathParts[pathParts.Length-1];
settingsOk = FilenameHelper.IsFilenameValid(filenamePart);
var settingsOk = FilenameHelper.IsFilenameValid(filenamePart);
for (int i = 0; settingsOk && i<pathParts.Length-1; i++) {
settingsOk = FilenameHelper.IsDirectoryNameValid(pathParts[i]);
@ -248,10 +242,7 @@ namespace Greenshot {
}
private bool CheckStorageLocationPath() {
bool settingsOk = true;
if(!Directory.Exists(FilenameHelper.FillVariables(textbox_storagelocation.Text, false))) {
settingsOk = false;
}
bool settingsOk = Directory.Exists(FilenameHelper.FillVariables(textbox_storagelocation.Text, false));
DisplayTextBoxValidity(textbox_storagelocation, settingsOk);
return settingsOk;
}
@ -259,11 +250,7 @@ namespace Greenshot {
private void DisplayTextBoxValidity(GreenshotTextBox textbox, bool valid) {
if (valid) {
// "Added" feature #3547158
if (Environment.OSVersion.Version.Major >= 6) {
textbox.BackColor = SystemColors.Window;
} else {
textbox.BackColor = SystemColors.Control;
}
textbox.BackColor = Environment.OSVersion.Version.Major >= 6 ? SystemColors.Window : SystemColors.Control;
} else {
textbox.BackColor = Color.Red;
}
@ -464,7 +451,7 @@ namespace Greenshot {
}
}
} catch (Exception e) {
LOG.Warn("Problem checking registry, ignoring for now: ", e);
Log.Warn("Problem checking registry, ignoring for now: ", e);
}
}
@ -480,7 +467,7 @@ namespace Greenshot {
MainForm.RegisterHotkeys();
// Make sure the current language & settings are reflected in the Main-context menu
MainForm.Instance.UpdateUI();
MainForm.Instance.UpdateUi();
DialogResult = DialogResult.OK;
} else {
tabcontrol.SelectTab(tab_output);
@ -511,7 +498,7 @@ namespace Greenshot {
}
private void Listview_pluginsSelectedIndexChanged(object sender, EventArgs e) {
button_pluginconfigure.Enabled = PluginHelper.Instance.isSelectedItemConfigurable(listview_plugins);
button_pluginconfigure.Enabled = PluginHelper.Instance.IsSelectedItemConfigurable(listview_plugins);
}
private void Button_pluginconfigureClick(object sender, EventArgs e) {
@ -523,11 +510,11 @@ namespace Greenshot {
//EmailFormat selectedEmailFormat = GetSelected<EmailFormat>(combobox_emailformat);
WindowCaptureMode selectedWindowCaptureMode = GetSelected<WindowCaptureMode>(combobox_window_capture_mode);
if (combobox_language.SelectedItem != null) {
LOG.Debug("Setting language to: " + (string)combobox_language.SelectedValue);
Log.Debug("Setting language to: " + (string)combobox_language.SelectedValue);
Language.CurrentLanguage = (string)combobox_language.SelectedValue;
}
// Reflect language changes to the settings form
UpdateUI();
UpdateUi();
// Reflect Language changes form
ApplyLanguage();
@ -652,7 +639,7 @@ namespace Greenshot {
return 1;
}
if (firstDestination != null && firstDestination.Priority == secondDestination.Priority) {
return firstDestination.Description.CompareTo(secondDestination.Description);
return string.Compare(firstDestination.Description, secondDestination.Description, StringComparison.Ordinal);
}
if (firstDestination != null) {
return firstDestination.Priority - secondDestination.Priority;

View file

@ -31,11 +31,11 @@ namespace Greenshot.Forms {
/// <summary>
/// the ToolStripMenuSelectList makes it possible to have a single or multi-check menu
/// </summary>
public class ToolStripMenuSelectList : ToolStripMenuItem {
private static readonly CoreConfiguration coreConfiguration = IniConfig.GetIniSection<CoreConfiguration>();
private readonly bool multiCheckAllowed;
private bool updateInProgress;
private static Image defaultImage;
public sealed class ToolStripMenuSelectList : ToolStripMenuItem {
private static readonly CoreConfiguration CoreConfig = IniConfig.GetIniSection<CoreConfiguration>();
private readonly bool _multiCheckAllowed;
private bool _updateInProgress;
private static Image _defaultImage;
/// <summary>
/// Occurs when one of the list's child element's Checked state changes.
@ -49,14 +49,12 @@ namespace Greenshot.Forms {
public ToolStripMenuSelectList(object identifier, bool allowMultiCheck) {
Identifier = identifier;
CheckOnClick = false;
multiCheckAllowed = allowMultiCheck;
if (defaultImage == null || defaultImage.Size != coreConfiguration.IconSize) {
if (defaultImage != null) {
defaultImage.Dispose();
_multiCheckAllowed = allowMultiCheck;
if (_defaultImage == null || _defaultImage.Size != CoreConfig.IconSize) {
_defaultImage?.Dispose();
_defaultImage = ImageHelper.CreateEmpty(CoreConfig.IconSize.Width, CoreConfig.IconSize.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb, Color.Transparent, 96f, 96f);
}
defaultImage = ImageHelper.CreateEmpty(coreConfiguration.IconSize.Width, coreConfiguration.IconSize.Height, System.Drawing.Imaging.PixelFormat.Format32bppArgb, Color.Transparent, 96f, 96f);
}
Image = defaultImage;
Image = _defaultImage;
}
public ToolStripMenuSelectList() : this(null,false) {}
public ToolStripMenuSelectList(object identifier) : this(identifier,false) {}
@ -70,7 +68,7 @@ namespace Greenshot.Forms {
IEnumerator items = DropDownItems.GetEnumerator();
while (items.MoveNext()) {
ToolStripMenuSelectListItem tsmi = (ToolStripMenuSelectListItem)items.Current;
if (tsmi.Checked) {
if (tsmi != null && tsmi.Checked) {
return tsmi;
}
}
@ -80,9 +78,9 @@ namespace Greenshot.Forms {
IEnumerator items = DropDownItems.GetEnumerator();
while (items.MoveNext()) {
ToolStripMenuSelectListItem tsmi = (ToolStripMenuSelectListItem)items.Current;
if (!multiCheckAllowed && !tsmi.Equals(value)) {
if (tsmi != null && !_multiCheckAllowed && !tsmi.Equals(value)) {
tsmi.Checked = false;
} else if (tsmi.Equals(value)) {
} else if (tsmi != null && tsmi.Equals(value)) {
tsmi.Checked = true;
}
}
@ -98,30 +96,31 @@ namespace Greenshot.Forms {
IEnumerator items = DropDownItems.GetEnumerator();
while(items.MoveNext()) {
ToolStripMenuSelectListItem tsmi = (ToolStripMenuSelectListItem)items.Current;
if (tsmi.Checked) {
if (tsmi != null && tsmi.Checked) {
sel.Add(tsmi);
}
}
return sel.ToArray();
}
set {
if (!multiCheckAllowed) {
if (!_multiCheckAllowed) {
throw new ArgumentException("Writing to checkedItems is only allowed in multi-check mode. Either set allowMultiCheck to true or use set SelectedItem instead of SelectedItems.");
}
IEnumerator items = DropDownItems.GetEnumerator();
IEnumerator sel = value.GetEnumerator();
while (items.MoveNext()) {
ToolStripMenuSelectListItem tsmi = (ToolStripMenuSelectListItem)items.Current;
while (sel.MoveNext()) {
if (tsmi.Equals(sel.Current)) {
tsmi.Checked = true;
} else {
tsmi.Checked = false;
var toolStripMenuSelectListItem = (ToolStripMenuSelectListItem)items.Current;
if (toolStripMenuSelectListItem == null)
{
continue;
}
if (!multiCheckAllowed && !tsmi.Equals(sel.Current)) {
tsmi.Checked = false;
} else if (tsmi.Equals(value)) {
tsmi.Checked = true;
while (sel.MoveNext())
{
toolStripMenuSelectListItem.Checked = toolStripMenuSelectListItem.Equals(sel.Current);
if (!_multiCheckAllowed && !toolStripMenuSelectListItem.Equals(sel.Current)) {
toolStripMenuSelectListItem.Checked = false;
} else if (toolStripMenuSelectListItem.Equals(value)) {
toolStripMenuSelectListItem.Checked = true;
}
}
}
@ -129,19 +128,17 @@ namespace Greenshot.Forms {
}
private void ItemCheckStateChanged(object sender, EventArgs e) {
if (updateInProgress) {
if (_updateInProgress) {
return;
}
ToolStripMenuSelectListItem tsmi = (ToolStripMenuSelectListItem)sender;
updateInProgress = true;
if (tsmi.Checked && !multiCheckAllowed) {
var toolStripMenuSelectListItem = (ToolStripMenuSelectListItem)sender;
_updateInProgress = true;
if (toolStripMenuSelectListItem.Checked && !_multiCheckAllowed) {
UncheckAll();
tsmi.Checked = true;
}
updateInProgress = false;
if (CheckedChanged != null) {
CheckedChanged(this, new ItemCheckedChangedEventArgs(tsmi));
toolStripMenuSelectListItem.Checked = true;
}
_updateInProgress = false;
CheckedChanged?.Invoke(this, new ItemCheckedChangedEventArgs(toolStripMenuSelectListItem));
}
/// <summary>
@ -151,26 +148,28 @@ namespace Greenshot.Forms {
/// <param name="image">the icon to be displayed</param>
/// <param name="data">the data to be returned when an item is queried</param>
/// <param name="isChecked">whether the item is initially checked</param>
public void AddItem(string label, Image image, Object data, bool isChecked) {
ToolStripMenuSelectListItem newItem = new ToolStripMenuSelectListItem();
newItem.Text = label;
public void AddItem(string label, Image image, object data, bool isChecked) {
var toolStripMenuSelectListItem = new ToolStripMenuSelectListItem
{
Text = label
};
if (image == null) {
image = defaultImage;
image = _defaultImage;
}
newItem.DisplayStyle = ToolStripItemDisplayStyle.Text;
newItem.Image = image;
newItem.CheckOnClick = true;
newItem.CheckStateChanged += ItemCheckStateChanged;
newItem.Data = data;
toolStripMenuSelectListItem.DisplayStyle = ToolStripItemDisplayStyle.Text;
toolStripMenuSelectListItem.Image = image;
toolStripMenuSelectListItem.CheckOnClick = true;
toolStripMenuSelectListItem.CheckStateChanged += ItemCheckStateChanged;
toolStripMenuSelectListItem.Data = data;
if (isChecked) {
if (!multiCheckAllowed) {
updateInProgress = true;
if (!_multiCheckAllowed) {
_updateInProgress = true;
UncheckAll();
updateInProgress = false;
_updateInProgress = false;
}
newItem.Checked = isChecked;
toolStripMenuSelectListItem.Checked = true;
}
DropDownItems.Add(newItem);
DropDownItems.Add(toolStripMenuSelectListItem);
}
/// <summary>
@ -187,7 +186,7 @@ namespace Greenshot.Forms {
/// </summary>
/// <param name="label">the label to be displayed</param>
/// <param name="data">the data to be returned when an item is queried</param>
public void AddItem(string label, Object data) {
public void AddItem(string label, object data) {
AddItem(label, null, data, false);
}
@ -216,7 +215,7 @@ namespace Greenshot.Forms {
/// <param name="label">the label to be displayed</param>
/// <param name="data">the data to be returned when an item is queried</param>
/// <param name="isChecked">whether the item is initially checked</param>
public void AddItem(string label, Object data, bool isChecked) {
public void AddItem(string label, object data, bool isChecked) {
AddItem(label, null, data, isChecked);
}
@ -234,8 +233,13 @@ namespace Greenshot.Forms {
/// </summary>
public void UncheckAll() {
IEnumerator items = DropDownItems.GetEnumerator();
while (items.MoveNext()) {
((ToolStripMenuSelectListItem)items.Current).Checked = false;
while (items.MoveNext())
{
var toolStripMenuSelectListItem = (ToolStripMenuSelectListItem)items.Current;
if (toolStripMenuSelectListItem != null)
{
toolStripMenuSelectListItem.Checked = false;
}
}
}
}

View file

@ -21,41 +21,41 @@
using System;
using System.Drawing;
using System.Windows.Forms;
using Greenshot.Core;
using GreenshotPlugin.Effects;
namespace Greenshot.Forms {
public partial class TornEdgeSettingsForm : BaseForm {
private readonly TornEdgeEffect effect;
private readonly TornEdgeEffect _effect;
public TornEdgeSettingsForm(TornEdgeEffect effect) {
this.effect = effect;
_effect = effect;
InitializeComponent();
ShowSettings();
}
private void ShowSettings() {
shadowCheckbox.Checked = effect.GenerateShadow;
shadowCheckbox.Checked = _effect.GenerateShadow;
// Fix to prevent BUG-1753
shadowDarkness.Value = Math.Max(shadowDarkness.Minimum, Math.Min(shadowDarkness.Maximum, (int)(effect.Darkness * shadowDarkness.Maximum)));
offsetX.Value = effect.ShadowOffset.X;
offsetY.Value = effect.ShadowOffset.Y;
toothsize.Value = effect.ToothHeight;
verticaltoothrange.Value = effect.VerticalToothRange;
horizontaltoothrange.Value = effect.HorizontalToothRange;
top.Checked = effect.Edges[0];
right.Checked = effect.Edges[1];
bottom.Checked = effect.Edges[2];
left.Checked = effect.Edges[3];
shadowDarkness.Value = Math.Max(shadowDarkness.Minimum, Math.Min(shadowDarkness.Maximum, (int)(_effect.Darkness * shadowDarkness.Maximum)));
offsetX.Value = _effect.ShadowOffset.X;
offsetY.Value = _effect.ShadowOffset.Y;
toothsize.Value = _effect.ToothHeight;
verticaltoothrange.Value = _effect.VerticalToothRange;
horizontaltoothrange.Value = _effect.HorizontalToothRange;
top.Checked = _effect.Edges[0];
right.Checked = _effect.Edges[1];
bottom.Checked = _effect.Edges[2];
left.Checked = _effect.Edges[3];
}
private void ButtonOK_Click(object sender, EventArgs e) {
effect.Darkness = (float)shadowDarkness.Value / (float)40;
effect.ShadowOffset = new Point((int)offsetX.Value, (int)offsetY.Value);
effect.ShadowSize = (int)thickness.Value;
effect.ToothHeight = (int)toothsize.Value;
effect.VerticalToothRange = (int)verticaltoothrange.Value;
effect.HorizontalToothRange = (int)horizontaltoothrange.Value;
effect.Edges = new bool[] { top.Checked, right.Checked, bottom.Checked, left.Checked };
effect.GenerateShadow = shadowCheckbox.Checked;
_effect.Darkness = shadowDarkness.Value / (float)40;
_effect.ShadowOffset = new Point((int)offsetX.Value, (int)offsetY.Value);
_effect.ShadowSize = (int)thickness.Value;
_effect.ToothHeight = (int)toothsize.Value;
_effect.VerticalToothRange = (int)verticaltoothrange.Value;
_effect.HorizontalToothRange = (int)horizontaltoothrange.Value;
_effect.Edges = new[] { top.Checked, right.Checked, bottom.Checked, left.Checked };
_effect.GenerateShadow = shadowCheckbox.Checked;
DialogResult = DialogResult.OK;
}

View file

@ -29,21 +29,14 @@ namespace Greenshot.Help
/// <summary>
/// Description of HelpFileLoader.
/// </summary>
public sealed class HelpFileLoader
public static class HelpFileLoader
{
private static readonly ILog Log = LogManager.GetLogger(typeof(HelpFileLoader));
private static readonly ILog LOG = LogManager.GetLogger(typeof(HelpFileLoader));
private const string EXT_HELP_URL = @"http://getgreenshot.org/help/";
private HelpFileLoader() {
}
private const string ExtHelpUrl = @"http://getgreenshot.org/help/";
public static void LoadHelp() {
string uri = FindOnlineHelpUrl(Language.CurrentLanguage);
if(uri == null) {
uri = Language.HelpFilePath;
}
string uri = FindOnlineHelpUrl(Language.CurrentLanguage) ?? Language.HelpFilePath;
Process.Start(uri);
}
@ -51,7 +44,7 @@ namespace Greenshot.Help
private static string FindOnlineHelpUrl(string currentIETF) {
string ret = null;
string extHelpUrlForCurrrentIETF = EXT_HELP_URL;
string extHelpUrlForCurrrentIETF = ExtHelpUrl;
if(!currentIETF.Equals("en-US")) {
extHelpUrlForCurrrentIETF += currentIETF.ToLower() + "/";
@ -60,16 +53,16 @@ namespace Greenshot.Help
HttpStatusCode? httpStatusCode = GetHttpStatus(extHelpUrlForCurrrentIETF);
if(httpStatusCode == HttpStatusCode.OK) {
ret = extHelpUrlForCurrrentIETF;
} else if(httpStatusCode != null && !extHelpUrlForCurrrentIETF.Equals(EXT_HELP_URL)) {
LOG.DebugFormat("Localized online help not found at {0}, will try {1} as fallback", extHelpUrlForCurrrentIETF, EXT_HELP_URL);
httpStatusCode = GetHttpStatus(EXT_HELP_URL);
} else if(httpStatusCode != null && !extHelpUrlForCurrrentIETF.Equals(ExtHelpUrl)) {
Log.DebugFormat("Localized online help not found at {0}, will try {1} as fallback", extHelpUrlForCurrrentIETF, ExtHelpUrl);
httpStatusCode = GetHttpStatus(ExtHelpUrl);
if(httpStatusCode == HttpStatusCode.OK) {
ret = EXT_HELP_URL;
ret = ExtHelpUrl;
} else {
LOG.WarnFormat("{0} returned status {1}", EXT_HELP_URL, httpStatusCode);
Log.WarnFormat("{0} returned status {1}", ExtHelpUrl, httpStatusCode);
}
} else if(httpStatusCode == null){
LOG.Info("Internet connection does not seem to be available, will load help from file system.");
Log.Info("Internet connection does not seem to be available, will load help from file system.");
}
return ret;
@ -88,11 +81,7 @@ namespace Greenshot.Help
return res.StatusCode;
}
} catch (WebException e) {
if (e.Response != null)
{
return ((HttpWebResponse)e.Response).StatusCode;
}
return null;
return ((HttpWebResponse) e.Response)?.StatusCode;
}
}
}

View file

@ -41,8 +41,8 @@ namespace Greenshot.Helpers {
/// CaptureHelper contains all the capture logic
/// </summary>
public class CaptureHelper : IDisposable {
private static readonly ILog LOG = LogManager.GetLogger(typeof(CaptureHelper));
private static readonly CoreConfiguration conf = IniConfig.GetIniSection<CoreConfiguration>();
private static readonly ILog Log = LogManager.GetLogger(typeof(CaptureHelper));
private static readonly CoreConfiguration CoreConfig = IniConfig.GetIniSection<CoreConfiguration>();
// TODO: when we get the screen capture code working correctly, this needs to be enabled
//private static ScreenCaptureHelper screenCapture = null;
private List<WindowDetails> _windows = new List<WindowDetails>();
@ -78,7 +78,7 @@ namespace Greenshot.Helpers {
_selectedCaptureWindow = null;
_capture = null;
// Empty working set after capturing
if (conf.MinimizeWorkingSetSize) {
if (CoreConfig.MinimizeWorkingSetSize) {
PsAPI.EmptyWorkingSet();
}
}
@ -114,7 +114,7 @@ namespace Greenshot.Helpers {
}
}
public static void CaptureIE(bool captureMouse, WindowDetails windowToCapture) {
public static void CaptureIe(bool captureMouse, WindowDetails windowToCapture) {
using (CaptureHelper captureHelper = new CaptureHelper(CaptureMode.IE, captureMouse)) {
captureHelper.SelectedCaptureWindow = windowToCapture;
captureHelper.MakeCapture();
@ -192,7 +192,7 @@ namespace Greenshot.Helpers {
}
private void DoCaptureFeedback() {
if(conf.PlayCameraSound) {
if(CoreConfig.PlayCameraSound) {
SoundHelper.Play();
}
}
@ -224,11 +224,11 @@ namespace Greenshot.Helpers {
// This fixes a problem when a balloon is still visible and a capture needs to be taken
// forcefully removes the balloon!
if (!conf.HideTrayicon) {
if (!CoreConfig.HideTrayicon) {
MainForm.Instance.NotifyIcon.Visible = false;
MainForm.Instance.NotifyIcon.Visible = true;
}
LOG.Debug(String.Format("Capturing with mode {0} and using Cursor {1}", _captureMode, _captureMouseCursor));
Log.Debug($"Capturing with mode {_captureMode} and using Cursor {_captureMouseCursor}");
_capture.CaptureDetails.CaptureMode = _captureMode;
// Get the windows details in a seperate thread, only for those captures that have a Feedback
@ -251,27 +251,24 @@ namespace Greenshot.Helpers {
}
// Delay for the Context menu
if (conf.CaptureDelay > 0) {
Thread.Sleep(conf.CaptureDelay);
if (CoreConfig.CaptureDelay > 0) {
Thread.Sleep(CoreConfig.CaptureDelay);
} else {
conf.CaptureDelay = 0;
CoreConfig.CaptureDelay = 0;
}
// Capture Mousecursor if we are not loading from file or clipboard, only show when needed
if (_captureMode != CaptureMode.File && _captureMode != CaptureMode.Clipboard) {
if (_captureMode != CaptureMode.File && _captureMode != CaptureMode.Clipboard)
{
_capture = WindowCapture.CaptureCursor(_capture);
if (_captureMouseCursor) {
_capture.CursorVisible = conf.CaptureMousepointer;
} else {
_capture.CursorVisible = false;
}
_capture.CursorVisible = _captureMouseCursor && CoreConfig.CaptureMousepointer;
}
switch(_captureMode) {
case CaptureMode.Window:
_capture = WindowCapture.CaptureScreen(_capture);
_capture.CaptureDetails.AddMetaData("source", "Screen");
SetDPI();
SetDpi();
CaptureWithFeedback();
break;
case CaptureMode.ActiveWindow:
@ -285,13 +282,13 @@ namespace Greenshot.Helpers {
_capture.CaptureDetails.AddMetaData("source", "Screen");
_capture.CaptureDetails.Title = "Screen";
}
SetDPI();
SetDpi();
HandleCapture();
break;
case CaptureMode.IE:
if (IECaptureHelper.CaptureIE(_capture, SelectedCaptureWindow) != null) {
if (IeCaptureHelper.CaptureIe(_capture, SelectedCaptureWindow) != null) {
_capture.CaptureDetails.AddMetaData("source", "Internet Explorer");
SetDPI();
SetDpi();
HandleCapture();
}
break;
@ -310,8 +307,8 @@ namespace Greenshot.Helpers {
}
break;
case ScreenCaptureMode.Fixed:
if (conf.ScreenToCapture > 0 && conf.ScreenToCapture <= Screen.AllScreens.Length) {
_capture = WindowCapture.CaptureRectangle(_capture, Screen.AllScreens[conf.ScreenToCapture].Bounds);
if (CoreConfig.ScreenToCapture > 0 && CoreConfig.ScreenToCapture <= Screen.AllScreens.Length) {
_capture = WindowCapture.CaptureRectangle(_capture, Screen.AllScreens[CoreConfig.ScreenToCapture].Bounds);
captureTaken = true;
}
break;
@ -322,7 +319,7 @@ namespace Greenshot.Helpers {
if (!captureTaken) {
_capture = WindowCapture.CaptureScreen(_capture);
}
SetDPI();
SetDpi();
HandleCapture();
break;
case CaptureMode.Clipboard:
@ -363,13 +360,13 @@ namespace Greenshot.Helpers {
break;
}
} catch (Exception e) {
LOG.Error(e.Message, e);
Log.Error(e.Message, e);
MessageBox.Show(Language.GetFormattedString(LangKey.error_openfile, filename));
}
try {
fileImage = ImageHelper.LoadImage(filename);
} catch (Exception e) {
LOG.Error(e.Message, e);
Log.Error(e.Message, e);
MessageBox.Show(Language.GetFormattedString(LangKey.error_openfile, filename));
}
}
@ -395,8 +392,8 @@ namespace Greenshot.Helpers {
}
break;
case CaptureMode.LastRegion:
if (!conf.LastCapturedRegion.IsEmpty) {
_capture = WindowCapture.CaptureRectangle(_capture, conf.LastCapturedRegion);
if (!CoreConfig.LastCapturedRegion.IsEmpty) {
_capture = WindowCapture.CaptureRectangle(_capture, CoreConfig.LastCapturedRegion);
// TODO: Reactive / check if the elements code is activated
//if (windowDetailsThread != null) {
// windowDetailsThread.Join();
@ -404,7 +401,7 @@ namespace Greenshot.Helpers {
// Set capture title, fixing bug #3569703
foreach (WindowDetails window in WindowDetails.GetVisibleWindows()) {
Point estimatedLocation = new Point(conf.LastCapturedRegion.X + conf.LastCapturedRegion.Width / 2, conf.LastCapturedRegion.Y + conf.LastCapturedRegion.Height / 2);
Point estimatedLocation = new Point(CoreConfig.LastCapturedRegion.X + CoreConfig.LastCapturedRegion.Width / 2, CoreConfig.LastCapturedRegion.Y + CoreConfig.LastCapturedRegion.Height / 2);
if (window.Contains(estimatedLocation)) {
_selectedCaptureWindow = window;
_capture.CaptureDetails.Title = _selectedCaptureWindow.Text;
@ -416,7 +413,7 @@ namespace Greenshot.Helpers {
//capture.MoveElements(capture.ScreenBounds.Location.X - capture.Location.X, capture.ScreenBounds.Location.Y - capture.Location.Y);
_capture.CaptureDetails.AddMetaData("source", "screen");
SetDPI();
SetDpi();
HandleCapture();
}
break;
@ -425,25 +422,23 @@ namespace Greenshot.Helpers {
if (Rectangle.Empty.Equals(_captureRect)) {
_capture = WindowCapture.CaptureScreen(_capture);
_capture.CaptureDetails.AddMetaData("source", "screen");
SetDPI();
SetDpi();
CaptureWithFeedback();
} else {
_capture = WindowCapture.CaptureRectangle(_capture, _captureRect);
_capture.CaptureDetails.AddMetaData("source", "screen");
SetDPI();
SetDpi();
HandleCapture();
}
break;
default:
LOG.Warn("Unknown capture mode: " + _captureMode);
Log.Warn("Unknown capture mode: " + _captureMode);
break;
}
// Wait for thread, otherwise we can't dipose the CaptureHelper
if (retrieveWindowDetailsThread != null) {
retrieveWindowDetailsThread.Join();
}
retrieveWindowDetailsThread?.Join();
if (_capture != null) {
LOG.Debug("Disposing capture");
Log.Debug("Disposing capture");
_capture.Dispose();
}
}
@ -471,7 +466,7 @@ namespace Greenshot.Helpers {
}
private void RetrieveWindowDetails() {
LOG.Debug("start RetrieveWindowDetails");
Log.Debug("start RetrieveWindowDetails");
// Start Enumeration of "active" windows
foreach (var window in WindowDetails.GetVisibleWindows()) {
// Make sure the details are retrieved once
@ -479,7 +474,7 @@ namespace Greenshot.Helpers {
// Force children retrieval, sometimes windows close on losing focus and this is solved by caching
int goLevelDeep = 3;
if (conf.WindowCaptureAllChildLocations) {
if (CoreConfig.WindowCaptureAllChildLocations) {
goLevelDeep = 20;
}
window.GetChildren(goLevelDeep);
@ -487,11 +482,11 @@ namespace Greenshot.Helpers {
_windows.Add(window);
}
}
LOG.Debug("end RetrieveWindowDetails");
Log.Debug("end RetrieveWindowDetails");
}
private void AddConfiguredDestination() {
foreach(string destinationDesignation in conf.OutputDestinations) {
foreach(string destinationDesignation in CoreConfig.OutputDestinations) {
IDestination destination = DestinationHelper.GetDestination(destinationDesignation);
if (destination != null) {
_capture.CaptureDetails.AddDestination(destination);
@ -507,7 +502,7 @@ namespace Greenshot.Helpers {
private void OpenCaptureOnClick(object sender, EventArgs e) {
SurfaceMessageEventArgs eventArgs = MainForm.Instance.NotifyIcon.Tag as SurfaceMessageEventArgs;
if (eventArgs == null) {
LOG.Warn("OpenCaptureOnClick called without SurfaceMessageEventArgs");
Log.Warn("OpenCaptureOnClick called without SurfaceMessageEventArgs");
RemoveEventHandler(sender, e);
return;
}
@ -515,14 +510,19 @@ namespace Greenshot.Helpers {
if (surface != null && eventArgs.MessageType == SurfaceMessageTyp.FileSaved) {
if (!string.IsNullOrEmpty(surface.LastSaveFullPath)) {
string errorMessage = null;
var path = Path.GetDirectoryName(surface.LastSaveFullPath);
try {
ProcessStartInfo psi = new ProcessStartInfo("explorer.exe");
psi.Arguments = Path.GetDirectoryName(surface.LastSaveFullPath);
psi.UseShellExecute = false;
using (Process p = new Process()) {
p.StartInfo = psi;
p.Start();
if (path != null)
{
var processStartInfo = new ProcessStartInfo("explorer.exe")
{
Arguments = path,
UseShellExecute = false
};
using (var process = new Process()) {
process.StartInfo = processStartInfo;
process.Start();
}
}
} catch (Exception ex) {
errorMessage = ex.Message;
@ -531,28 +531,41 @@ namespace Greenshot.Helpers {
if (errorMessage != null) {
try {
string windowsPath = Environment.GetEnvironmentVariable("SYSTEMROOT");
if (windowsPath != null)
{
string explorerPath = Path.Combine(windowsPath, "explorer.exe");
if (File.Exists(explorerPath)) {
ProcessStartInfo psi = new ProcessStartInfo(explorerPath);
psi.Arguments = Path.GetDirectoryName(surface.LastSaveFullPath);
psi.UseShellExecute = false;
using (Process p = new Process()) {
p.StartInfo = psi;
p.Start();
if (File.Exists(explorerPath))
{
var lastSaveDirectory = Path.GetDirectoryName(surface.LastSaveFullPath);
if (lastSaveDirectory != null)
{
var processStartInfo = new ProcessStartInfo(explorerPath)
{
Arguments = lastSaveDirectory,
UseShellExecute = false
};
using (var process = new Process()) {
process.StartInfo = processStartInfo;
process.Start();
}
}
errorMessage = null;
}
} catch {
}
}
catch
{
// ignored
}
}
if (errorMessage != null) {
MessageBox.Show(string.Format("{0}\r\nexplorer.exe {1}", errorMessage, surface.LastSaveFullPath), "explorer.exe", MessageBoxButtons.OK, MessageBoxIcon.Error);
MessageBox.Show($"{errorMessage}\r\nexplorer.exe {surface.LastSaveFullPath}", "explorer.exe", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
} else if (surface != null && !string.IsNullOrEmpty(surface.UploadUrl)) {
} else if (!string.IsNullOrEmpty(surface?.UploadUrl)) {
Process.Start(surface.UploadUrl);
}
LOG.DebugFormat("Deregistering the BalloonTipClicked");
Log.DebugFormat("Deregistering the BalloonTipClicked");
RemoveEventHandler(sender, e);
}
@ -568,7 +581,11 @@ namespace Greenshot.Helpers {
/// <param name="sender"></param>
/// <param name="eventArgs"></param>
private void SurfaceMessageReceived(object sender, SurfaceMessageEventArgs eventArgs) {
if (eventArgs == null || string.IsNullOrEmpty(eventArgs.Message)) {
if (string.IsNullOrEmpty(eventArgs?.Message)) {
return;
}
if (MainForm.Instance == null)
{
return;
}
switch (eventArgs.MessageType) {
@ -602,13 +619,13 @@ namespace Greenshot.Helpers {
outputMade = true;
} else {
// Make sure the resolution is set correctly!
if (_capture.CaptureDetails != null && _capture.Image != null) {
((Bitmap)_capture.Image).SetResolution(_capture.CaptureDetails.DpiX, _capture.CaptureDetails.DpiY);
if (_capture.CaptureDetails != null) {
((Bitmap) _capture.Image)?.SetResolution(_capture.CaptureDetails.DpiX, _capture.CaptureDetails.DpiY);
}
DoCaptureFeedback();
}
LOG.Debug("A capture of: " + _capture.CaptureDetails.Title);
Log.Debug("A capture of: " + _capture.CaptureDetails.Title);
// check if someone has passed a destination
if (_capture.CaptureDetails.CaptureDestinations == null || _capture.CaptureDetails.CaptureDestinations.Count == 0) {
@ -616,18 +633,20 @@ namespace Greenshot.Helpers {
}
// Create Surface with capture, this way elements can be added automatically (like the mouse cursor)
Surface surface = new Surface(_capture);
surface.Modified = !outputMade;
Surface surface = new Surface(_capture)
{
Modified = !outputMade
};
// Register notify events if this is wanted
if (conf.ShowTrayNotification && !conf.HideTrayicon) {
if (CoreConfig.ShowTrayNotification && !CoreConfig.HideTrayicon) {
surface.SurfaceMessage += SurfaceMessageReceived;
}
// Let the processors do their job
foreach(IProcessor processor in ProcessorHelper.GetAllProcessors()) {
if (processor.isActive) {
LOG.InfoFormat("Calling processor {0}", processor.Description);
Log.InfoFormat("Calling processor {0}", processor.Description);
processor.ProcessCapture(surface, _capture.CaptureDetails);
}
}
@ -659,7 +678,7 @@ namespace Greenshot.Helpers {
if (PickerDestination.DESIGNATION.Equals(destination.Designation)) {
continue;
}
LOG.InfoFormat("Calling destination {0}", destination.Description);
Log.InfoFormat("Calling destination {0}", destination.Description);
ExportInformation exportInformation = destination.ExportCapture(false, surface, captureDetails);
if (EditorDestination.DESIGNATION.Equals(destination.Designation) && exportInformation.ExportMade) {
@ -674,21 +693,21 @@ namespace Greenshot.Helpers {
private bool CaptureActiveWindow() {
bool presupplied = false;
LOG.Debug("CaptureActiveWindow");
Log.Debug("CaptureActiveWindow");
if (_selectedCaptureWindow != null) {
LOG.Debug("Using supplied window");
Log.Debug("Using supplied window");
presupplied = true;
} else {
_selectedCaptureWindow = WindowDetails.GetActiveWindow();
if (_selectedCaptureWindow != null) {
if (LOG.IsDebugEnabled)
if (Log.IsDebugEnabled)
{
LOG.DebugFormat("Capturing window: {0} with {1}", _selectedCaptureWindow.Text, _selectedCaptureWindow.WindowRectangle);
Log.DebugFormat("Capturing window: {0} with {1}", _selectedCaptureWindow.Text, _selectedCaptureWindow.WindowRectangle);
}
}
}
if (_selectedCaptureWindow == null || (!presupplied && _selectedCaptureWindow.Iconic)) {
LOG.Warn("No window to capture!");
Log.Warn("No window to capture!");
// Nothing to capture, code up in the stack will capture the full screen
return false;
}
@ -699,13 +718,13 @@ namespace Greenshot.Helpers {
}
_selectedCaptureWindow = SelectCaptureWindow(_selectedCaptureWindow);
if (_selectedCaptureWindow == null) {
LOG.Warn("No window to capture, after SelectCaptureWindow!");
Log.Warn("No window to capture, after SelectCaptureWindow!");
// Nothing to capture, code up in the stack will capture the full screen
return false;
}
// Fix for Bug #3430560
conf.LastCapturedRegion = _selectedCaptureWindow.WindowRectangle;
bool returnValue = CaptureWindow(_selectedCaptureWindow, _capture, conf.WindowCaptureMode) != null;
CoreConfig.LastCapturedRegion = _selectedCaptureWindow.WindowRectangle;
bool returnValue = CaptureWindow(_selectedCaptureWindow, _capture, CoreConfig.WindowCaptureMode) != null;
return returnValue;
}
@ -718,7 +737,7 @@ namespace Greenshot.Helpers {
public static WindowDetails SelectCaptureWindow(WindowDetails windowToCapture) {
Rectangle windowRectangle = windowToCapture.WindowRectangle;
if (windowRectangle.Width == 0 || windowRectangle.Height == 0) {
LOG.WarnFormat("Window {0} has nothing to capture, using workaround to find other window of same process.", windowToCapture.Text);
Log.WarnFormat("Window {0} has nothing to capture, using workaround to find other window of same process.", windowToCapture.Text);
// Trying workaround, the size 0 arrises with e.g. Toad.exe, has a different Window when minimized
WindowDetails linkedWindow = WindowDetails.GetLinkedWindow(windowToCapture);
if (linkedWindow != null) {
@ -735,18 +754,18 @@ namespace Greenshot.Helpers {
/// </summary>
/// <param name="process">Proces to check for the presentation framework</param>
/// <returns>true if the process uses WPF</returns>
private static bool isWPF(Process process) {
private static bool IsWpf(Process process) {
if (process != null) {
try {
foreach (ProcessModule module in process.Modules) {
if (module.ModuleName.StartsWith("PresentationFramework")) {
LOG.InfoFormat("Found that Process {0} uses {1}, assuming it's using WPF", process.ProcessName, module.FileName);
Log.InfoFormat("Found that Process {0} uses {1}, assuming it's using WPF", process.ProcessName, module.FileName);
return true;
}
}
} catch (Exception) {
// Access denied on the modules
LOG.WarnFormat("No access on the modules from process {0}, assuming WPF is used.", process.ProcessName);
Log.WarnFormat("No access on the modules from process {0}, assuming WPF is used.", process.ProcessName);
return true;
}
}
@ -776,14 +795,14 @@ namespace Greenshot.Helpers {
// 2) Is Windows >= Vista & DWM enabled: use DWM
// 3) Otherwise use GDI (Screen might be also okay but might lose content)
if (isAutoMode) {
if (conf.IECapture && IECaptureHelper.IsIEWindow(windowToCapture)) {
if (CoreConfig.IECapture && IeCaptureHelper.IsIeWindow(windowToCapture)) {
try {
ICapture ieCapture = IECaptureHelper.CaptureIE(captureForWindow, windowToCapture);
ICapture ieCapture = IeCaptureHelper.CaptureIe(captureForWindow, windowToCapture);
if (ieCapture != null) {
return ieCapture;
}
} catch (Exception ex) {
LOG.WarnFormat("Problem capturing IE, skipping to normal capture. Exception message was: {0}", ex.Message);
Log.WarnFormat("Problem capturing IE, skipping to normal capture. Exception message was: {0}", ex.Message);
}
}
@ -792,9 +811,9 @@ namespace Greenshot.Helpers {
// Change to GDI, if allowed
if (!windowToCapture.IsMetroApp && WindowCapture.IsGdiAllowed(process)) {
if (!dwmEnabled && isWPF(process)) {
if (!dwmEnabled && IsWpf(process)) {
// do not use GDI, as DWM is not enabled and the application uses PresentationFramework.dll -> isWPF
LOG.InfoFormat("Not using GDI for windows of process {0}, as the process uses WPF", process.ProcessName);
Log.InfoFormat("Not using GDI for windows of process {0}, as the process uses WPF", process.ProcessName);
} else {
windowCaptureMode = WindowCaptureMode.GDI;
}
@ -820,7 +839,7 @@ namespace Greenshot.Helpers {
windowCaptureMode = WindowCaptureMode.Screen;
}
LOG.InfoFormat("Capturing window with mode {0}", windowCaptureMode);
Log.InfoFormat("Capturing window with mode {0}", windowCaptureMode);
bool captureTaken = false;
windowRectangle.Intersect(captureForWindow.ScreenBounds);
// Try to capture
@ -852,7 +871,7 @@ namespace Greenshot.Helpers {
// "easy compare", both have the same size
// If GDI has more black, use the screen capture.
if (blackPercentageGdi > blackPercentageScreen) {
LOG.Debug("Using screen capture, as GDI had additional black.");
Log.Debug("Using screen capture, as GDI had additional black.");
// changeing the image will automatically dispose the previous
tmpCapture.Image = screenCapture.Image;
// Make sure it's not disposed, else the picture is gone!
@ -861,7 +880,7 @@ namespace Greenshot.Helpers {
} else if (screenPixels < gdiPixels) {
// Screen capture is cropped, window is outside of screen
if (blackPercentageGdi > 50 && blackPercentageGdi > blackPercentageScreen) {
LOG.Debug("Using screen capture, as GDI had additional black.");
Log.Debug("Using screen capture, as GDI had additional black.");
// changeing the image will automatically dispose the previous
tmpCapture.Image = screenCapture.Image;
// Make sure it's not disposed, else the picture is gone!
@ -869,7 +888,7 @@ namespace Greenshot.Helpers {
}
} else {
// Use the GDI capture by doing nothing
LOG.Debug("This should not happen, how can there be more screen as GDI pixels?");
Log.Debug("This should not happen, how can there be more screen as GDI pixels?");
}
}
}
@ -910,7 +929,7 @@ namespace Greenshot.Helpers {
captureForWindow = WindowCapture.CaptureRectangleFromDesktopScreen(captureForWindow, windowRectangle);
captureTaken = true;
} catch (Exception e) {
LOG.Error("Problem capturing", e);
Log.Error("Problem capturing", e);
return null;
}
break;
@ -918,16 +937,15 @@ namespace Greenshot.Helpers {
}
}
if (captureForWindow != null) {
if (windowToCapture != null) {
if (captureForWindow != null)
{
captureForWindow.CaptureDetails.Title = windowToCapture.Text;
}
}
return captureForWindow;
}
private void SetDPI() {
private void SetDpi() {
// Workaround for proble with DPI retrieval, the FromHwnd activates the window...
WindowDetails previouslyActiveWindow = WindowDetails.GetActiveWindow();
// Workaround for changed DPI settings in Windows 7
@ -935,12 +953,10 @@ namespace Greenshot.Helpers {
_capture.CaptureDetails.DpiX = graphics.DpiX;
_capture.CaptureDetails.DpiY = graphics.DpiY;
}
if (previouslyActiveWindow != null) {
// Set previouslyActiveWindow as foreground window
previouslyActiveWindow.ToForeground();
}
if (_capture.CaptureDetails != null && _capture.Image != null) {
((Bitmap)_capture.Image).SetResolution(_capture.CaptureDetails.DpiX, _capture.CaptureDetails.DpiY);
previouslyActiveWindow?.ToForeground();
if (_capture.CaptureDetails != null) {
((Bitmap) _capture.Image)?.SetResolution(_capture.CaptureDetails.DpiX, _capture.CaptureDetails.DpiY);
}
}
@ -958,7 +974,7 @@ namespace Greenshot.Helpers {
using (CaptureForm captureForm = new CaptureForm(_capture, _windows)) {
// Make sure the form is hidden after showing, even if an exception occurs, so all errors will be shown
DialogResult result = DialogResult.Cancel;
DialogResult result;
try {
result = captureForm.ShowDialog(MainForm.Instance);
} finally {
@ -980,7 +996,7 @@ namespace Greenshot.Helpers {
// Important here is that the location needs to be offsetted back to screen coordinates!
Rectangle tmpRectangle = _captureRect;
tmpRectangle.Offset(_capture.ScreenBounds.Location.X, _capture.ScreenBounds.Location.Y);
conf.LastCapturedRegion = tmpRectangle;
CoreConfig.LastCapturedRegion = tmpRectangle;
HandleCapture();
}
}

View file

@ -39,9 +39,8 @@ namespace Greenshot.Helpers {
[Serializable()]
public class CopyDataTransport {
private readonly List<KeyValuePair<CommandEnum, string>> _commands;
public List<KeyValuePair<CommandEnum, string>> Commands {
get {return _commands;}
}
public List<KeyValuePair<CommandEnum, string>> Commands => _commands;
public CopyDataTransport() {
_commands = new List<KeyValuePair<CommandEnum, string>>();
}
@ -100,9 +99,9 @@ namespace Greenshot.Helpers {
/// </summary>
/// <param name="m">The Windows Message information.</param>
protected override void WndProc (ref Message m ) {
if (m.Msg == WM_COPYDATA) {
COPYDATASTRUCT cds = new COPYDATASTRUCT();
cds = (COPYDATASTRUCT) Marshal.PtrToStructure(m.LParam, typeof(COPYDATASTRUCT));
if (m.Msg == WM_COPYDATA)
{
var cds = (COPYDATASTRUCT) Marshal.PtrToStructure(m.LParam, typeof(COPYDATASTRUCT));
if (cds.cbData > 0) {
byte[] data = new byte[cds.cbData];
Marshal.Copy(cds.lpData, data, 0, cds.cbData);
@ -120,9 +119,7 @@ namespace Greenshot.Helpers {
// WM_DESTROY fires before OnHandleChanged and is
// a better place to ensure that we've cleared
// everything up.
if (_channels != null) {
_channels.OnHandleChange();
}
_channels?.OnHandleChange();
base.OnHandleChange();
}
base.WndProc(ref m);
@ -134,10 +131,7 @@ namespace Greenshot.Helpers {
/// <param name="e">The data which has been received.</param>
protected void OnCopyDataReceived(CopyDataReceivedEventArgs e)
{
if (CopyDataReceived != null)
{
CopyDataReceived(this, e);
}
CopyDataReceived?.Invoke(this, e);
}
/// <summary>
@ -149,20 +143,14 @@ namespace Greenshot.Helpers {
/// </summary>
protected override void OnHandleChange () {
// need to clear up everything we had set.
if (_channels != null) {
_channels.OnHandleChange();
}
_channels?.OnHandleChange();
base.OnHandleChange();
}
/// <summary>
/// Gets the collection of channels.
/// </summary>
public CopyDataChannels Channels {
get {
return _channels;
}
}
public CopyDataChannels Channels => _channels;
public void Dispose() {
Dispose(true);
@ -204,7 +192,7 @@ namespace Greenshot.Helpers {
/// <summary>
/// Gets the channel name that this data was sent on.
/// </summary>
public string ChannelName { get; } = "";
public string ChannelName { get; }
/// <summary>
/// Gets the data object which was sent.
@ -274,11 +262,8 @@ namespace Greenshot.Helpers {
/// <summary>
/// Returns the CopyDataChannel for the specified channelName
/// </summary>
public CopyDataChannel this[string channelName] {
get {
return (CopyDataChannel) Dictionary[channelName];
}
}
public CopyDataChannel this[string channelName] => (CopyDataChannel) Dictionary[channelName];
/// <summary>
/// Adds a new channel on which this application can send and
/// receive messages.
@ -320,7 +305,7 @@ namespace Greenshot.Helpers {
/// <param name="key">The channelName</param>
/// <param name="data">The CopyDataChannel object which has
/// just been removed</param>
protected override void OnRemoveComplete ( Object key , Object data ) {
protected override void OnRemoveComplete ( object key , object data ) {
( (CopyDataChannel) data).Dispose();
OnRemove(key, data);
}

View file

@ -31,9 +31,9 @@ namespace Greenshot.Helpers {
/// Description of DestinationHelper.
/// </summary>
public static class DestinationHelper {
private static readonly ILog LOG = LogManager.GetLogger(typeof(DestinationHelper));
private static readonly ILog Log = LogManager.GetLogger(typeof(DestinationHelper));
private static readonly Dictionary<string, IDestination> RegisteredDestinations = new Dictionary<string, IDestination>();
private static readonly CoreConfiguration coreConfig = IniConfig.GetIniSection<CoreConfiguration>();
private static readonly CoreConfiguration CoreConfig = IniConfig.GetIniSection<CoreConfiguration>();
/// Initialize the destinations
static DestinationHelper() {
@ -47,15 +47,15 @@ namespace Greenshot.Helpers {
try {
destination = (IDestination)Activator.CreateInstance(destinationType);
} catch (Exception e) {
LOG.ErrorFormat("Can't create instance of {0}", destinationType);
LOG.Error(e);
Log.ErrorFormat("Can't create instance of {0}", destinationType);
Log.Error(e);
continue;
}
if (destination.isActive) {
LOG.DebugFormat("Found destination {0} with designation {1}", destinationType.Name, destination.Designation);
if (destination.IsActive) {
Log.DebugFormat("Found destination {0} with designation {1}", destinationType.Name, destination.Designation);
RegisterDestination(destination);
} else {
LOG.DebugFormat("Ignoring destination {0} with designation {1}", destinationType.Name, destination.Designation);
Log.DebugFormat("Ignoring destination {0} with designation {1}", destinationType.Name, destination.Designation);
}
}
}
@ -66,7 +66,7 @@ namespace Greenshot.Helpers {
/// </summary>
/// <param name="destination"></param>
public static void RegisterDestination(IDestination destination) {
if (coreConfig.ExcludeDestinations == null || !coreConfig.ExcludeDestinations.Contains(destination.Designation)) {
if (CoreConfig.ExcludeDestinations == null || !CoreConfig.ExcludeDestinations.Contains(destination.Designation)) {
// don't test the key, an exception should happen wenn it's not unique
RegisteredDestinations.Add(destination.Designation, destination);
}
@ -82,13 +82,13 @@ namespace Greenshot.Helpers {
IGreenshotPlugin plugin = PluginHelper.Instance.Plugins[pluginAttribute];
try {
foreach (IDestination destination in plugin.Destinations()) {
if (coreConfig.ExcludeDestinations == null || !coreConfig.ExcludeDestinations.Contains(destination.Designation)) {
if (CoreConfig.ExcludeDestinations == null || !CoreConfig.ExcludeDestinations.Contains(destination.Designation)) {
destinations.Add(destination);
}
}
} catch (Exception ex) {
LOG.ErrorFormat("Couldn't get destinations from the plugin {0}", pluginAttribute.Name);
LOG.Error(ex);
Log.ErrorFormat("Couldn't get destinations from the plugin {0}", pluginAttribute.Name);
Log.Error(ex);
}
}
destinations.Sort();
@ -136,7 +136,7 @@ namespace Greenshot.Helpers {
/// <param name="captureDetails"></param>
public static ExportInformation ExportCapture(bool manuallyInitiated, string designation, ISurface surface, ICaptureDetails captureDetails) {
IDestination destination = GetDestination(designation);
if (destination != null && destination.isActive) {
if (destination != null && destination.IsActive) {
return destination.ExportCapture(manuallyInitiated, surface, captureDetails);
}
return null;

View file

@ -62,7 +62,7 @@ namespace Greenshot.Helpers
if (IniConfig.IsPortable) {
environment.Append(" Portable");
}
environment.Append(" (" + OSInfo.Bits + " bit)");
environment.Append(" (" + OsInfo.Bits + " bit)");
if (newline)
{
@ -98,7 +98,7 @@ namespace Greenshot.Helpers
{
environment.Append(", ");
}
environment.Append(string.Format("OS: {0} {1} {2} (x{3}) {4}", OSInfo.Name, OSInfo.Edition, OSInfo.ServicePack, OSInfo.Bits, OSInfo.VersionString));
environment.Append(string.Format("OS: {0} {1} {2} (x{3}) {4}", OsInfo.Name, OsInfo.Edition, OsInfo.ServicePack, OsInfo.Bits, OsInfo.VersionString));
if (newline)
{
environment.AppendLine();
@ -152,9 +152,9 @@ namespace Greenshot.Helpers
StringBuilder report = new StringBuilder();
report.AppendLine("Exception: " + ex.GetType().ToString());
report.AppendLine("Exception: " + ex.GetType());
report.AppendLine("Message: " + ex.Message);
if (ex.Data != null && ex.Data.Count > 0)
if (ex.Data.Count > 0)
{
report.AppendLine();
report.AppendLine("Additional Information:");
@ -207,7 +207,7 @@ namespace Greenshot.Helpers
/// Provides detailed information about the host operating system.
/// Code is available at: http://www.csharp411.com/determine-windows-version-and-edition-with-c/
/// </summary>
public static class OSInfo
public static class OsInfo
{
#region BITS
/// <summary>
@ -261,16 +261,7 @@ namespace Greenshot.Helpers
}
else if (productType == VER_NT_SERVER)
{
if ((suiteMask & VER_SUITE_ENTERPRISE) != 0)
{
// Windows NT 4.0 Server Enterprise
edition = "Enterprise Server";
}
else
{
// Windows NT 4.0 Server
edition = "Standard Server";
}
edition = (suiteMask & VER_SUITE_ENTERPRISE) != 0 ? "Enterprise Server" : "Standard Server";
}
}
#endregion VERSION 4
@ -470,7 +461,7 @@ namespace Greenshot.Helpers
#endregion EDITION
#region NAME
private static string s_Name;
private static string _name;
/// <summary>
/// Gets the name of the operating system running on this computer.
/// </summary>
@ -478,16 +469,18 @@ namespace Greenshot.Helpers
{
get
{
if (s_Name != null)
if (_name != null)
{
return s_Name; //***** RETURN *****//
return _name; //***** RETURN *****//
}
string name = "unknown";
OperatingSystem osVersion = Environment.OSVersion;
OSVERSIONINFOEX osVersionInfo = new OSVERSIONINFOEX();
osVersionInfo.dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX));
OSVERSIONINFOEX osVersionInfo = new OSVERSIONINFOEX
{
dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX))
};
if (GetVersionEx(ref osVersionInfo))
{
@ -514,14 +507,7 @@ namespace Greenshot.Helpers
}
break;
case 10:
if (csdVersion == "A")
{
name = "Windows 98 Second Edition";
}
else
{
name = "Windows 98";
}
name = csdVersion == "A" ? "Windows 98 Second Edition" : "Windows 98";
break;
case 90:
name = "Windows Me";
@ -629,7 +615,7 @@ namespace Greenshot.Helpers
}
}
s_Name = name;
_name = name;
return name;
}
}
@ -735,9 +721,11 @@ namespace Greenshot.Helpers
get
{
string servicePack = string.Empty;
OSVERSIONINFOEX osVersionInfo = new OSVERSIONINFOEX();
OSVERSIONINFOEX osVersionInfo = new OSVERSIONINFOEX
{
dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX))
};
osVersionInfo.dwOSVersionInfoSize = Marshal.SizeOf(typeof(OSVERSIONINFOEX));
if (GetVersionEx(ref osVersionInfo))
{
@ -754,13 +742,8 @@ namespace Greenshot.Helpers
/// <summary>
/// Gets the build version number of the operating system running on this computer.
/// </summary>
public static int BuildVersion
{
get
{
return Environment.OSVersion.Version.Build;
}
}
public static int BuildVersion => Environment.OSVersion.Version.Build;
#endregion BUILD
#region FULL

View file

@ -34,8 +34,6 @@ namespace Greenshot.Helpers {
/// <param name="y2">The point on the y-axis of the second point</param>
/// <returns></returns>
public static int Distance2D(int x1, int y1, int x2, int y2) {
//Our end result
int result = 0;
//Take x2-x1, then square it
double part1 = Math.Pow(x2 - x1, 2);
//Take y2-y1, then square it
@ -43,9 +41,7 @@ namespace Greenshot.Helpers {
//Add both of the parts together
double underRadical = part1 + part2;
//Get the square root of the parts
result = (int)Math.Sqrt(underRadical);
//Return our result
return result;
return (int)Math.Sqrt(underRadical);
}
/// <summary>

View file

@ -22,6 +22,7 @@ using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Runtime.InteropServices;
using Greenshot.Configuration;
using Greenshot.Helpers.IEInterop;
@ -41,18 +42,18 @@ namespace Greenshot.Helpers {
/// On top I modified it to use the already available code in Greenshot.
/// Many thanks to all the people who contributed here!
/// </summary>
public static class IECaptureHelper {
private static readonly ILog LOG = LogManager.GetLogger(typeof(IECaptureHelper));
private static readonly CoreConfiguration configuration = IniConfig.GetIniSection<CoreConfiguration>();
public static class IeCaptureHelper {
private static readonly ILog Log = LogManager.GetLogger(typeof(IeCaptureHelper));
private static readonly CoreConfiguration CoreConfig = IniConfig.GetIniSection<CoreConfiguration>();
// Helper method to activate a certain IE Tab
public static void ActivateIETab(WindowDetails ieWindowDetails, int tabIndex) {
WindowDetails directUIWindowDetails = IEHelper.GetDirectUI(ieWindowDetails);
if(directUIWindowDetails != null) {
public static void ActivateIeTab(WindowDetails ieWindowDetails, int tabIndex) {
WindowDetails directUiWindowDetails = IEHelper.GetDirectUI(ieWindowDetails);
if(directUiWindowDetails != null) {
// Bring window to the front
ieWindowDetails.Restore();
// Get accessible
Accessible ieAccessible = new Accessible(directUIWindowDetails.Handle);
Accessible ieAccessible = new Accessible(directUiWindowDetails.Handle);
// Activate Tab
ieAccessible.ActivateIETab(tabIndex);
}
@ -64,13 +65,13 @@ namespace Greenshot.Helpers {
/// <param name="someWindow">WindowDetails to check</param>
/// <param name="minimumPercentage">min percentage</param>
/// <returns></returns>
public static bool IsMostlyIEWindow(WindowDetails someWindow, int minimumPercentage) {
public static bool IsMostlyIeWindow(WindowDetails someWindow, int minimumPercentage) {
WindowDetails ieWindow = someWindow.GetChild("Internet Explorer_Server");
if (ieWindow != null) {
Rectangle wholeClient = someWindow.ClientRectangle;
Rectangle partClient = ieWindow.ClientRectangle;
int percentage = (int)(100*(float)(partClient.Width * partClient.Height) / (float)(wholeClient.Width * wholeClient.Height));
LOG.InfoFormat("Window {0}, ie part {1}, percentage {2}", wholeClient, partClient, percentage);
int percentage = (int)(100*(float)(partClient.Width * partClient.Height) / (wholeClient.Width * wholeClient.Height));
Log.InfoFormat("Window {0}, ie part {1}, percentage {2}", wholeClient, partClient, percentage);
if (percentage > minimumPercentage) {
return true;
}
@ -83,11 +84,11 @@ namespace Greenshot.Helpers {
/// </summary>
/// <param name="someWindow"></param>
/// <returns></returns>
public static bool IsIEWindow(WindowDetails someWindow) {
public static bool IsIeWindow(WindowDetails someWindow) {
if ("IEFrame".Equals(someWindow.ClassName)) {
return true;
}
if (configuration.WindowClassesToCheckForIE != null && configuration.WindowClassesToCheckForIE.Contains(someWindow.ClassName)) {
if (CoreConfig.WindowClassesToCheckForIE != null && CoreConfig.WindowClassesToCheckForIE.Contains(someWindow.ClassName)) {
return someWindow.GetChild("Internet Explorer_Server") != null;
}
return false;
@ -97,16 +98,16 @@ namespace Greenshot.Helpers {
/// Get Windows displaying an IE
/// </summary>
/// <returns>IEnumerable WindowDetails</returns>
public static IEnumerable<WindowDetails> GetIEWindows() {
foreach (WindowDetails possibleIEWindow in WindowDetails.GetAllWindows()) {
if (possibleIEWindow.Text.Length == 0) {
public static IEnumerable<WindowDetails> GetIeWindows() {
foreach (var possibleIeWindow in WindowDetails.GetAllWindows()) {
if (possibleIeWindow.Text.Length == 0) {
continue;
}
if (possibleIEWindow.ClientRectangle.IsEmpty) {
if (possibleIeWindow.ClientRectangle.IsEmpty) {
continue;
}
if (IsIEWindow(possibleIEWindow)) {
yield return possibleIEWindow;
if (IsIeWindow(possibleIeWindow)) {
yield return possibleIeWindow;
}
}
}
@ -115,11 +116,9 @@ namespace Greenshot.Helpers {
/// Simple check if IE is running
/// </summary>
/// <returns>bool</returns>
public static bool IsIERunning() {
foreach (WindowDetails ieWindow in GetIEWindows()) {
return true;
}
return false;
public static bool IsIeRunning()
{
return GetIeWindows().Any();
}
/// <summary>
@ -127,23 +126,26 @@ namespace Greenshot.Helpers {
/// </summary>
/// <returns>List with KeyValuePair of WindowDetails and string</returns>
public static List<KeyValuePair<WindowDetails, string>> GetBrowserTabs() {
List<IntPtr> ieHandleList = new List<IntPtr>();
Dictionary<WindowDetails, List<string>> browserWindows = new Dictionary<WindowDetails, List<string>>();
var ieHandleList = new List<IntPtr>();
var browserWindows = new Dictionary<WindowDetails, List<string>>();
// Find the IE windows
foreach (WindowDetails ieWindow in GetIEWindows()) {
foreach (var ieWindow in GetIeWindows()) {
try {
if (!ieHandleList.Contains(ieWindow.Handle)) {
if (ieHandleList.Contains(ieWindow.Handle))
{
continue;
}
if ("IEFrame".Equals(ieWindow.ClassName)) {
WindowDetails directUIWD = IEHelper.GetDirectUI(ieWindow);
if (directUIWD != null) {
Accessible accessible = new Accessible(directUIWD.Handle);
var directUiwd = IEHelper.GetDirectUI(ieWindow);
if (directUiwd != null) {
var accessible = new Accessible(directUiwd.Handle);
browserWindows.Add(ieWindow, accessible.IETabCaptions);
}
} else if (configuration.WindowClassesToCheckForIE != null && configuration.WindowClassesToCheckForIE.Contains(ieWindow.ClassName)) {
List<string> singleWindowText = new List<string>();
} else if (CoreConfig.WindowClassesToCheckForIE != null && CoreConfig.WindowClassesToCheckForIE.Contains(ieWindow.ClassName)) {
var singleWindowText = new List<string>();
try {
IHTMLDocument2 document2 = GetHtmlDocument(ieWindow);
var document2 = GetHtmlDocument(ieWindow);
string title = document2.title;
Marshal.ReleaseComObject(document2);
if (string.IsNullOrEmpty(title)) {
@ -157,14 +159,13 @@ namespace Greenshot.Helpers {
browserWindows.Add(ieWindow, singleWindowText);
}
ieHandleList.Add(ieWindow.Handle);
}
} catch (Exception) {
LOG.Warn("Can't get Info from " + ieWindow.ClassName);
Log.Warn("Can't get Info from " + ieWindow.ClassName);
}
}
List<KeyValuePair<WindowDetails, string>> returnList = new List<KeyValuePair<WindowDetails, string>>();
foreach(WindowDetails windowDetails in browserWindows.Keys) {
var returnList = new List<KeyValuePair<WindowDetails, string>>();
foreach(var windowDetails in browserWindows.Keys) {
foreach(string tab in browserWindows[windowDetails]) {
returnList.Add(new KeyValuePair<WindowDetails, string>(windowDetails, tab));
}
@ -178,35 +179,30 @@ namespace Greenshot.Helpers {
/// <param name="mainWindow"></param>
/// <returns></returns>
private static IHTMLDocument2 GetHtmlDocument(WindowDetails mainWindow) {
WindowDetails ieServer;
if ("Internet Explorer_Server".Equals(mainWindow.ClassName)) {
ieServer = mainWindow;
} else {
ieServer = mainWindow.GetChild("Internet Explorer_Server");
}
var ieServer = "Internet Explorer_Server".Equals(mainWindow.ClassName) ? mainWindow : mainWindow.GetChild("Internet Explorer_Server");
if (ieServer == null) {
LOG.WarnFormat("No Internet Explorer_Server for {0}", mainWindow.Text);
Log.WarnFormat("No Internet Explorer_Server for {0}", mainWindow.Text);
return null;
}
IHTMLDocument2 document2 = null;
uint windowMessage = User32.RegisterWindowMessage("WM_HTML_GETOBJECT");
if (windowMessage == 0) {
LOG.WarnFormat("Couldn't register WM_HTML_GETOBJECT");
Log.WarnFormat("Couldn't register WM_HTML_GETOBJECT");
return null;
}
LOG.DebugFormat("Trying WM_HTML_GETOBJECT on {0}", ieServer.ClassName);
Log.DebugFormat("Trying WM_HTML_GETOBJECT on {0}", ieServer.ClassName);
UIntPtr response;
User32.SendMessageTimeout(ieServer.Handle, windowMessage, IntPtr.Zero, IntPtr.Zero, SendMessageTimeoutFlags.SMTO_NORMAL, 5000, out response);
IHTMLDocument2 document2;
if (response != UIntPtr.Zero) {
document2 = (IHTMLDocument2)Accessible.ObjectFromLresult(response, typeof(IHTMLDocument).GUID, IntPtr.Zero);
if (document2 == null) {
LOG.Error("No IHTMLDocument2 found");
Log.Error("No IHTMLDocument2 found");
return null;
}
} else {
LOG.Error("No answer on WM_HTML_GETOBJECT.");
Log.Error("No answer on WM_HTML_GETOBJECT.");
return null;
}
return document2;
@ -227,23 +223,23 @@ namespace Greenshot.Helpers {
IHTMLDocument2 alternativeReturnDocument2 = null;
// Find the IE windows
foreach (WindowDetails ieWindow in GetIEWindows()) {
LOG.DebugFormat("Processing {0} - {1}", ieWindow.ClassName, ieWindow.Text);
foreach (WindowDetails ieWindow in GetIeWindows()) {
Log.DebugFormat("Processing {0} - {1}", ieWindow.ClassName, ieWindow.Text);
Accessible ieAccessible = null;
WindowDetails directUIWD = IEHelper.GetDirectUI(ieWindow);
if (directUIWD != null) {
ieAccessible = new Accessible(directUIWD.Handle);
WindowDetails directUiwd = IEHelper.GetDirectUI(ieWindow);
if (directUiwd != null) {
ieAccessible = new Accessible(directUiwd.Handle);
}
if (ieAccessible == null) {
if (browserWindow != null) {
LOG.InfoFormat("Active Window is {0}", browserWindow.Text);
Log.InfoFormat("Active Window is {0}", browserWindow.Text);
}
if (!ieWindow.Equals(browserWindow)) {
LOG.WarnFormat("No ieAccessible for {0}", ieWindow.Text);
Log.WarnFormat("No ieAccessible for {0}", ieWindow.Text);
continue;
}
LOG.DebugFormat("No ieAccessible, but the active window is an IE window: {0}, ", ieWindow.Text);
Log.DebugFormat("No ieAccessible, but the active window is an IE window: {0}, ", ieWindow.Text);
}
try {
@ -256,9 +252,7 @@ namespace Greenshot.Helpers {
// Get the content window handle for the shellWindow.Document
IOleWindow oleWindow = (IOleWindow)document2;
IntPtr contentWindowHandle = IntPtr.Zero;
if (oleWindow != null) {
oleWindow.GetWindow(out contentWindowHandle);
}
oleWindow?.GetWindow(out contentWindowHandle);
if (contentWindowHandle != IntPtr.Zero) {
// Get the HTMLDocument to check the hasFocus
@ -266,7 +260,7 @@ namespace Greenshot.Helpers {
IHTMLDocument4 document4 = (IHTMLDocument4)document2;
if (document4.hasFocus()) {
LOG.DebugFormat("Matched focused document: {0}", document2.title);
Log.DebugFormat("Matched focused document: {0}", document2.title);
// Look no further, we got what we wanted!
returnDocument2 = document2;
returnWindow = new WindowDetails(contentWindowHandle);
@ -279,7 +273,7 @@ namespace Greenshot.Helpers {
break;
}
if (ieAccessible != null && returnWindow == null && document2.title.Equals(ieAccessible.IEActiveTabCaption) ) {
LOG.DebugFormat("Title: {0}", document2.title);
Log.DebugFormat("Title: {0}", document2.title);
returnDocument2 = document2;
returnWindow = new WindowDetails(contentWindowHandle);
} else {
@ -292,8 +286,8 @@ namespace Greenshot.Helpers {
}
}
} catch (Exception e) {
LOG.ErrorFormat("Major problem: Problem retrieving Document from {0}", ieWindow.Text);
LOG.Error(e);
Log.ErrorFormat("Major problem: Problem retrieving Document from {0}", ieWindow.Text);
Log.Error(e);
}
}
@ -307,8 +301,8 @@ namespace Greenshot.Helpers {
try {
returnDocumentContainer = new DocumentContainer(returnDocument2, returnWindow);
} catch (Exception e) {
LOG.Error("Major problem: Problem retrieving Document.");
LOG.Error(e);
Log.Error("Major problem: Problem retrieving Document.");
Log.Error(e);
}
}
@ -320,8 +314,8 @@ namespace Greenshot.Helpers {
try {
returnDocumentContainer = new DocumentContainer(alternativeReturnDocument2, alternativeReturnWindow);
} catch (Exception e) {
LOG.Error("Major problem: Problem retrieving Document.");
LOG.Error(e);
Log.Error("Major problem: Problem retrieving Document.");
Log.Error(e);
}
}
return returnDocumentContainer;
@ -332,8 +326,8 @@ namespace Greenshot.Helpers {
/// </summary>
/// <param name="capture">ICapture where the capture needs to be stored</param>
/// <returns>ICapture with the content (if any)</returns>
public static ICapture CaptureIE(ICapture capture) {
return CaptureIE(capture, WindowDetails.GetActiveWindow());
public static ICapture CaptureIe(ICapture capture) {
return CaptureIe(capture, WindowDetails.GetActiveWindow());
}
/// <summary>
/// Here the logic for capturing the IE Content is located
@ -341,7 +335,7 @@ namespace Greenshot.Helpers {
/// <param name="capture">ICapture where the capture needs to be stored</param>
/// <param name="windowToCapture">window to use</param>
/// <returns>ICapture with the content (if any)</returns>
public static ICapture CaptureIE(ICapture capture, WindowDetails windowToCapture) {
public static ICapture CaptureIe(ICapture capture, WindowDetails windowToCapture) {
if (windowToCapture == null) {
windowToCapture = WindowDetails.GetActiveWindow();
}
@ -355,15 +349,15 @@ namespace Greenshot.Helpers {
// Nothing found
if (documentContainer == null) {
LOG.Debug("Nothing to capture found");
Log.Debug("Nothing to capture found");
return null;
}
try {
LOG.DebugFormat("Window class {0}", documentContainer.ContentWindow.ClassName);
LOG.DebugFormat("Window location {0}", documentContainer.ContentWindow.Location);
Log.DebugFormat("Window class {0}", documentContainer.ContentWindow.ClassName);
Log.DebugFormat("Window location {0}", documentContainer.ContentWindow.Location);
} catch (Exception ex) {
LOG.Warn("Error while logging information.", ex);
Log.Warn("Error while logging information.", ex);
}
// bitmap to return
@ -372,12 +366,12 @@ namespace Greenshot.Helpers {
Size pageSize = PrepareCapture(documentContainer, capture);
returnBitmap = CapturePage(documentContainer, pageSize);
} catch (Exception captureException) {
LOG.Error("Exception found, ignoring and returning nothing! Error was: ", captureException);
Log.Error("Exception found, ignoring and returning nothing! Error was: ", captureException);
}
// TODO: Enable when the elements are usable again.
// Capture the element on the page
//try {
// if (configuration.IEFieldCapture && capture.CaptureDetails.HasDestination("Editor")) {
// if (CoreConfig.IEFieldCapture && capture.CaptureDetails.HasDestination("Editor")) {
// // clear the current elements, as they are for the window itself
// capture.Elements.Clear();
// CaptureElement documentCaptureElement = documentContainer.CreateCaptureElements(pageSize);
@ -414,13 +408,9 @@ namespace Greenshot.Helpers {
// The URL is available unter "document2.url" and can be used to enhance the meta-data etc.
capture.CaptureDetails.AddMetaData("url", documentContainer.Url);
// Store the title of the page
if (documentContainer.Name != null) {
capture.CaptureDetails.Title = documentContainer.Name;
} else {
capture.CaptureDetails.Title = windowToCapture.Text;
}
capture.CaptureDetails.Title = documentContainer.Name ?? windowToCapture.Text;
} catch (Exception ex) {
LOG.Warn("Problems getting some attributes...", ex);
Log.Warn("Problems getting some attributes...", ex);
}
// Store the URL of the page
@ -446,7 +436,7 @@ namespace Greenshot.Helpers {
}
capture.CaptureDetails.AddMetaData("URL_PORT", uri.Port.ToString());
} catch(Exception e) {
LOG.Warn("Exception when trying to use url in metadata "+documentContainer.Url,e);
Log.Warn("Exception when trying to use url in metadata "+documentContainer.Url,e);
}
}
try {
@ -454,7 +444,7 @@ namespace Greenshot.Helpers {
capture.MoveMouseLocation(-documentContainer.ViewportRectangle.X, -documentContainer.ViewportRectangle.Y);
// Used to be: capture.MoveMouseLocation(-(capture.Location.X + documentContainer.CaptureOffset.X), -(capture.Location.Y + documentContainer.CaptureOffset.Y));
} catch (Exception ex) {
LOG.Warn("Error while correcting the mouse offset.", ex);
Log.Warn("Error while correcting the mouse offset.", ex);
}
} finally {
// Always close the background form
@ -480,7 +470,7 @@ namespace Greenshot.Helpers {
movedFrame = false;
foreach(DocumentContainer currentFrame in documentContainer.Frames) {
foreach(DocumentContainer otherFrame in documentContainer.Frames) {
if (otherFrame.ID == currentFrame.ID) {
if (otherFrame.Id == currentFrame.Id) {
continue;
}
// check if we need to move
@ -494,16 +484,16 @@ namespace Greenshot.Helpers {
if ((horizalResize || horizalMove) && leftOf) {
// Current frame resized horizontally, so move other horizontally
LOG.DebugFormat("Moving Frame {0} horizontally to the right of {1}", otherFrame.Name, currentFrame.Name);
Log.DebugFormat("Moving Frame {0} horizontally to the right of {1}", otherFrame.Name, currentFrame.Name);
otherFrame.DestinationLeft = currentFrame.DestinationRight;
movedFrame = true;
} else if ((verticalResize || verticalMove) && belowOf){
// Current frame resized vertically, so move other vertically
LOG.DebugFormat("Moving Frame {0} vertically to the bottom of {1}", otherFrame.Name, currentFrame.Name);
Log.DebugFormat("Moving Frame {0} vertically to the bottom of {1}", otherFrame.Name, currentFrame.Name);
otherFrame.DestinationTop = currentFrame.DestinationBottom;
movedFrame = true;
} else {
LOG.DebugFormat("Frame {0} intersects with {1}", otherFrame.Name, currentFrame.Name);
Log.DebugFormat("Frame {0} intersects with {1}", otherFrame.Name, currentFrame.Name);
}
}
}
@ -539,11 +529,11 @@ namespace Greenshot.Helpers {
// Limit the size as the editor currently can't work with sizes > short.MaxValue
if (pageWidth > short.MaxValue) {
LOG.WarnFormat("Capture has a width of {0} which bigger than the maximum supported {1}, cutting width to the maxium.", pageWidth, short.MaxValue);
Log.WarnFormat("Capture has a width of {0} which bigger than the maximum supported {1}, cutting width to the maxium.", pageWidth, short.MaxValue);
pageWidth = Math.Min(pageWidth, short.MaxValue);
}
if (pageHeight > short.MaxValue) {
LOG.WarnFormat("Capture has a height of {0} which bigger than the maximum supported {1}, cutting height to the maxium", pageHeight, short.MaxValue);
Log.WarnFormat("Capture has a height of {0} which bigger than the maximum supported {1}, cutting height to the maxium", pageHeight, short.MaxValue);
pageHeight = Math.Min(pageHeight, short.MaxValue);
}
return new Size(pageWidth, pageHeight);
@ -563,7 +553,7 @@ namespace Greenshot.Helpers {
using (Graphics graphicsTarget = Graphics.FromImage(returnBitmap)) {
// Clear the target with the backgroundcolor
Color clearColor = documentContainer.BackgroundColor;
LOG.DebugFormat("Clear color: {0}", clearColor);
Log.DebugFormat("Clear color: {0}", clearColor);
graphicsTarget.Clear(clearColor);
// Get the base document & draw it
@ -597,7 +587,7 @@ namespace Greenshot.Helpers {
int pageWidth = documentContainer.ScrollWidth;
int pageHeight = documentContainer.ScrollHeight;
if (pageWidth * pageHeight == 0) {
LOG.WarnFormat("Empty page for DocumentContainer {0}: {1}", documentContainer.Name, documentContainer.Url);
Log.WarnFormat("Empty page for DocumentContainer {0}: {1}", documentContainer.Name, documentContainer.Url);
return;
}
@ -605,7 +595,7 @@ namespace Greenshot.Helpers {
int viewportWidth = documentContainer.ClientWidth;
int viewportHeight = documentContainer.ClientHeight;
if (viewportWidth * viewportHeight == 0) {
LOG.WarnFormat("Empty viewport for DocumentContainer {0}: {1}", documentContainer.Name, documentContainer.Url);
Log.WarnFormat("Empty viewport for DocumentContainer {0}: {1}", documentContainer.Name, documentContainer.Url);
return;
}
@ -613,7 +603,7 @@ namespace Greenshot.Helpers {
int startLeft = documentContainer.ScrollLeft;
int startTop = documentContainer.ScrollTop;
LOG.DebugFormat("Capturing {4} with total size {0},{1} displayed with size {2},{3}", pageWidth, pageHeight, viewportWidth, viewportHeight, documentContainer.Name);
Log.DebugFormat("Capturing {4} with total size {0},{1} displayed with size {2},{3}", pageWidth, pageHeight, viewportWidth, viewportHeight, documentContainer.Name);
// Variable used for looping horizontally
int horizontalPage = 0;
@ -640,34 +630,34 @@ namespace Greenshot.Helpers {
Rectangle clientRectangle = new Rectangle(documentContainer.SourceLocation, viewPortSize);
Image fragment = contentWindowDetails.PrintWindow();
if (fragment != null) {
LOG.DebugFormat("Captured fragment size: {0}x{1}", fragment.Width, fragment.Height);
Log.DebugFormat("Captured fragment size: {0}x{1}", fragment.Width, fragment.Height);
try {
// cut all junk, due to IE "border" we need to remove some parts
Rectangle viewportRect = documentContainer.ViewportRectangle;
if (!viewportRect.IsEmpty) {
LOG.DebugFormat("Cropping to viewport: {0}", viewportRect);
Log.DebugFormat("Cropping to viewport: {0}", viewportRect);
ImageHelper.Crop(ref fragment, ref viewportRect);
}
LOG.DebugFormat("Cropping to clientRectangle: {0}", clientRectangle);
Log.DebugFormat("Cropping to clientRectangle: {0}", clientRectangle);
// Crop to clientRectangle
if (ImageHelper.Crop(ref fragment, ref clientRectangle)) {
Point targetLocation = new Point(documentContainer.DestinationLocation.X, documentContainer.DestinationLocation.Y);
LOG.DebugFormat("Fragment targetLocation is {0}", targetLocation);
Log.DebugFormat("Fragment targetLocation is {0}", targetLocation);
targetLocation.Offset(targetOffset);
LOG.DebugFormat("After offsetting the fragment targetLocation is {0}", targetLocation);
LOG.DebugFormat("Drawing fragment of size {0} to {1}", fragment.Size, targetLocation);
Log.DebugFormat("After offsetting the fragment targetLocation is {0}", targetLocation);
Log.DebugFormat("Drawing fragment of size {0} to {1}", fragment.Size, targetLocation);
graphicsTarget.DrawImage(fragment, targetLocation);
graphicsTarget.Flush();
} else {
// somehow we are capturing nothing!?
LOG.WarnFormat("Crop of {0} failed?", documentContainer.Name);
Log.WarnFormat("Crop of {0} failed?", documentContainer.Name);
break;
}
} finally {
fragment.Dispose();
}
} else {
LOG.WarnFormat("Capture of {0} failed!", documentContainer.Name);
Log.WarnFormat("Capture of {0} failed!", documentContainer.Name);
}
verticalPage++;
}

View file

@ -36,15 +36,12 @@ namespace Greenshot.Helpers.IEInterop {
private static readonly Guid IID_IWebBrowserApp = new Guid("0002DF05-0000-0000-C000-000000000046");
private static readonly Guid IID_IWebBrowser2 = new Guid("D30C1661-CDAF-11D0-8A3E-00C04FC9E26E");
private static int _counter;
private readonly int _id = _counter++;
private IHTMLDocument2 _document2;
private IHTMLDocument3 _document3;
private Point _sourceLocation;
private Point _destinationLocation;
private Point _startLocation = Point.Empty;
private Rectangle _viewportRectangle = Rectangle.Empty;
private string _name;
private string _url;
private bool _isDtd;
private DocumentContainer _parent;
private WindowDetails _contentWindow;
@ -58,7 +55,7 @@ namespace Greenshot.Helpers.IEInterop {
IHTMLDocument2 document2 = GetDocumentFromWindow(frameWindow);
try {
LOG.DebugFormat("frameWindow.name {0}", frameWindow.name);
_name = frameWindow.name;
Name = frameWindow.name;
} catch {
// Ignore
}
@ -91,7 +88,7 @@ namespace Greenshot.Helpers.IEInterop {
public DocumentContainer(IHTMLDocument2 document2, WindowDetails contentWindow) {
Init(document2, contentWindow);
LOG.DebugFormat("Creating DocumentContainer for Document {0} found in window with rectangle {1}", _name, SourceRectangle);
LOG.DebugFormat("Creating DocumentContainer for Document {0} found in window with rectangle {1}", Name, SourceRectangle);
}
/// <summary>
@ -119,7 +116,7 @@ namespace Greenshot.Helpers.IEInterop {
//compatibility mode affects how height is computed
_isDtd = false;
try {
if (_document3 != null && (_document3.documentElement != null) && !document5.compatMode.Equals("BackCompat")) {
if (_document3?.documentElement != null && !document5.compatMode.Equals("BackCompat")) {
_isDtd = true;
}
} catch (Exception ex) {
@ -176,15 +173,15 @@ namespace Greenshot.Helpers.IEInterop {
try {
LOG.DebugFormat("Calculated location {0} for {1}", _startLocation, document2.title);
if (_name == null) {
_name = document2.title;
if (Name == null) {
Name = document2.title;
}
} catch (Exception e) {
LOG.Warn("Problem while trying to get document title!", e);
}
try {
_url = document2.url;
Url = document2.url;
} catch (Exception e) {
LOG.Warn("Problem while trying to get document url!", e);
}
@ -202,7 +199,7 @@ namespace Greenshot.Helpers.IEInterop {
DocumentContainer frameData = new DocumentContainer(frameWindow, contentWindow, this);
// check if frame is hidden
if (!frameData.IsHidden) {
LOG.DebugFormat("Creating DocumentContainer for Frame {0} found in window with rectangle {1}", frameData._name, frameData.SourceRectangle);
LOG.DebugFormat("Creating DocumentContainer for Frame {0} found in window with rectangle {1}", frameData.Name, frameData.SourceRectangle);
_frames.Add(frameData);
} else {
LOG.DebugFormat("Skipping frame {0}", frameData.Name);
@ -341,17 +338,9 @@ namespace Greenshot.Helpers.IEInterop {
}
}
public Rectangle ViewportRectangle {
get {
return _viewportRectangle;
}
}
public Rectangle ViewportRectangle => _viewportRectangle;
public WindowDetails ContentWindow {
get {
return _contentWindow;
}
}
public WindowDetails ContentWindow => _contentWindow;
public DocumentContainer Parent {
get {
@ -391,12 +380,7 @@ namespace Greenshot.Helpers.IEInterop {
/// <param name="attribute">Attribute to set</param>
/// <param name="value">Value to set</param>
public void SetAttribute(string attribute, string value) {
IHTMLElement element = null;
if (!_isDtd) {
element = _document2.body;
} else {
element = _document3.documentElement;
}
var element = !_isDtd ? _document2.body : _document3.documentElement;
element.setAttribute(attribute, value, 1);
// Release IHTMLElement com object
releaseCom(element);
@ -408,12 +392,7 @@ namespace Greenshot.Helpers.IEInterop {
/// <param name="attribute">Attribute to get</param>
/// <returns>object with the attribute value</returns>
public object GetAttribute(string attribute) {
IHTMLElement element;
if (!_isDtd) {
element = _document2.body;
} else {
element = _document3.documentElement;
}
var element = !_isDtd ? _document2.body : _document3.documentElement;
var retVal = element.getAttribute(attribute, 1);
// Release IHTMLElement com object
releaseCom(element);
@ -428,53 +407,21 @@ namespace Greenshot.Helpers.IEInterop {
return retVal;
}
public int ID {
get {
return _id;
}
}
public int Id { get; } = _counter++;
public string Name {
get {
return _name;
}
}
public string Name { get; private set; }
public string Url {
get {
return _url;
}
}
public string Url { get; private set; }
public bool IsHidden {
get {
return ClientWidth == 0 || ClientHeight == 0;
}
}
public bool IsHidden => ClientWidth == 0 || ClientHeight == 0;
public int ClientWidth {
get {
return ScaleX(GetAttributeAsInt("clientWidth"));
}
}
public int ClientWidth => ScaleX(GetAttributeAsInt("clientWidth"));
public int ClientHeight {
get {
return ScaleY(GetAttributeAsInt("clientHeight"));
}
}
public int ClientHeight => ScaleY(GetAttributeAsInt("clientHeight"));
public int ScrollWidth {
get {
return ScaleX(GetAttributeAsInt("scrollWidth"));
}
}
public int ScrollWidth => ScaleX(GetAttributeAsInt("scrollWidth"));
public int ScrollHeight {
get {
return ScaleY(GetAttributeAsInt("scrollHeight"));
}
}
public int ScrollHeight => ScaleY(GetAttributeAsInt("scrollHeight"));
public Point SourceLocation {
get {
@ -485,41 +432,17 @@ namespace Greenshot.Helpers.IEInterop {
}
}
public Size SourceSize {
get {
return new Size(ClientWidth, ClientHeight);
}
}
public Size SourceSize => new Size(ClientWidth, ClientHeight);
public Rectangle SourceRectangle {
get {
return new Rectangle(SourceLocation, SourceSize);
}
}
public Rectangle SourceRectangle => new Rectangle(SourceLocation, SourceSize);
public int SourceLeft {
get {
return _sourceLocation.X;
}
}
public int SourceLeft => _sourceLocation.X;
public int SourceTop {
get {
return _sourceLocation.Y;
}
}
public int SourceTop => _sourceLocation.Y;
public int SourceRight {
get {
return _sourceLocation.X + ClientWidth;
}
}
public int SourceRight => _sourceLocation.X + ClientWidth;
public int SourceBottom {
get {
return _sourceLocation.Y + ClientHeight;
}
}
public int SourceBottom => _sourceLocation.Y + ClientHeight;
public Point DestinationLocation {
get {
@ -531,17 +454,9 @@ namespace Greenshot.Helpers.IEInterop {
}
public Size DestinationSize {
get {
return new Size(ScrollWidth, ScrollHeight);
}
}
public Size DestinationSize => new Size(ScrollWidth, ScrollHeight);
public Rectangle DestinationRectangle {
get {
return new Rectangle(DestinationLocation, DestinationSize);
}
}
public Rectangle DestinationRectangle => new Rectangle(DestinationLocation, DestinationSize);
public int DestinationLeft {
get {
@ -561,17 +476,9 @@ namespace Greenshot.Helpers.IEInterop {
}
}
public int DestinationRight {
get {
return _destinationLocation.X + ScrollWidth;
}
}
public int DestinationRight => _destinationLocation.X + ScrollWidth;
public int DestinationBottom {
get {
return _destinationLocation.Y + ScrollHeight;
}
}
public int DestinationBottom => _destinationLocation.Y + ScrollHeight;
public int ScrollLeft {
get{
@ -591,10 +498,6 @@ namespace Greenshot.Helpers.IEInterop {
}
}
public IList<DocumentContainer> Frames {
get {
return _frames;
}
}
public IList<DocumentContainer> Frames => _frames;
}
}

View file

@ -43,8 +43,8 @@ namespace Greenshot.Helpers {
/// Represents an email message to be sent through MAPI.
/// </summary>
public class MapiMailMessage : IDisposable {
private static readonly ILog LOG = LogManager.GetLogger(typeof(MapiMailMessage));
private static readonly CoreConfiguration conf = IniConfig.GetIniSection<CoreConfiguration>();
private static readonly ILog Log = LogManager.GetLogger(typeof(MapiMailMessage));
private static readonly CoreConfiguration CoreConfig = IniConfig.GetIniSection<CoreConfiguration>();
/// <summary>
/// Helper Method for creating an Email with Attachment
@ -54,14 +54,14 @@ namespace Greenshot.Helpers {
public static void SendImage(string fullPath, string title) {
using (MapiMailMessage message = new MapiMailMessage(title, null)) {
message.Files.Add(fullPath);
if (!string.IsNullOrEmpty(conf.MailApiTo)) {
message._recipientCollection.Add(new Recipient(conf.MailApiTo, RecipientType.To));
if (!string.IsNullOrEmpty(CoreConfig.MailApiTo)) {
message.Recipients.Add(new Recipient(CoreConfig.MailApiTo, RecipientType.To));
}
if (!string.IsNullOrEmpty(conf.MailApiCC)) {
message._recipientCollection.Add(new Recipient(conf.MailApiCC, RecipientType.CC));
if (!string.IsNullOrEmpty(CoreConfig.MailApiCC)) {
message.Recipients.Add(new Recipient(CoreConfig.MailApiCC, RecipientType.CC));
}
if (!string.IsNullOrEmpty(conf.MailApiBCC)) {
message._recipientCollection.Add(new Recipient(conf.MailApiBCC, RecipientType.BCC));
if (!string.IsNullOrEmpty(CoreConfig.MailApiBCC)) {
message.Recipients.Add(new Recipient(CoreConfig.MailApiBCC, RecipientType.BCC));
}
message.ShowDialog();
}
@ -80,10 +80,10 @@ namespace Greenshot.Helpers {
// Store the list of currently active windows, so we can make sure we show the email window later!
var windowsBefore = WindowDetails.GetVisibleWindows();
//bool isEmailSend = false;
//if (EmailConfigHelper.HasOutlook() && (conf.OutputEMailFormat == EmailFormat.OUTLOOK_HTML || conf.OutputEMailFormat == EmailFormat.OUTLOOK_TXT)) {
//if (EmailConfigHelper.HasOutlook() && (CoreConfig.OutputEMailFormat == EmailFormat.OUTLOOK_HTML || CoreConfig.OutputEMailFormat == EmailFormat.OUTLOOK_TXT)) {
// isEmailSend = OutlookExporter.ExportToOutlook(tmpFile, captureDetails);
//}
if (/*!isEmailSend &&*/ EmailConfigHelper.HasMAPI()) {
if (/*!isEmailSend &&*/ EmailConfigHelper.HasMapi()) {
// Fallback to MAPI
// Send the email
SendImage(tmpFile, captureDetails.Title);
@ -132,9 +132,6 @@ namespace Greenshot.Helpers {
#region Member Variables
private string _subject;
private string _body;
private RecipientCollection _recipientCollection;
private readonly ManualResetEvent _manualResetEvent;
#endregion Member Variables
@ -146,7 +143,7 @@ namespace Greenshot.Helpers {
/// </summary>
public MapiMailMessage() {
Files = new List<string>();
_recipientCollection = new RecipientCollection();
Recipients = new RecipientCollection();
_manualResetEvent = new ManualResetEvent(false);
}
@ -154,15 +151,15 @@ namespace Greenshot.Helpers {
/// Creates a new mail message with the specified subject.
/// </summary>
public MapiMailMessage(string subject) : this() {
_subject = subject;
Subject = subject;
}
/// <summary>
/// Creates a new mail message with the specified subject and body.
/// </summary>
public MapiMailMessage(string subject, string body) : this() {
_subject = subject;
_body = body;
Subject = subject;
Body = body;
}
#endregion Constructors
@ -172,35 +169,17 @@ namespace Greenshot.Helpers {
/// <summary>
/// Gets or sets the subject of this mail message.
/// </summary>
public string Subject {
get {
return _subject;
}
set {
_subject = value;
}
}
public string Subject { get; set; }
/// <summary>
/// Gets or sets the body of this mail message.
/// </summary>
public string Body {
get {
return _body;
}
set {
_body = value;
}
}
public string Body { get; set; }
/// <summary>
/// Gets the recipient list for this mail message.
/// </summary>
public RecipientCollection Recipients {
get {
return _recipientCollection;
}
}
public RecipientCollection Recipients { get; private set; }
/// <summary>
/// Gets the file list for this mail message.
@ -216,11 +195,13 @@ namespace Greenshot.Helpers {
/// </summary>
public void ShowDialog() {
// Create the mail message in an STA thread
Thread t = new Thread(_ShowMail);
t.IsBackground = true;
t.Name = "Create MAPI mail";
t.SetApartmentState(ApartmentState.STA);
t.Start();
var thread = new Thread(_ShowMail)
{
IsBackground = true,
Name = "Create MAPI mail"
};
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
// only return when the new thread has built it's interop representation
_manualResetEvent.WaitOne();
@ -240,10 +221,7 @@ namespace Greenshot.Helpers {
if (!disposing) {
return;
}
if (_manualResetEvent != null) {
_manualResetEvent.Close();
}
_manualResetEvent?.Close();
}
/// <summary>
@ -252,12 +230,12 @@ namespace Greenshot.Helpers {
private void _ShowMail() {
var message = new MapiHelperInterop.MapiMessage();
using (var interopRecipients = _recipientCollection.GetInteropRepresentation()) {
message.Subject = _subject;
message.NoteText = _body;
using (var interopRecipients = Recipients.GetInteropRepresentation()) {
message.Subject = Subject;
message.NoteText = Body;
message.Recipients = interopRecipients.Handle;
message.RecipientCount = _recipientCollection.Count;
message.RecipientCount = Recipients.Count;
// Check if we need to add attachments
if (Files.Count > 0) {
@ -284,14 +262,14 @@ namespace Greenshot.Helpers {
return;
}
string errorText = GetMapiError(errorCode);
LOG.Error("Error sending MAPI Email. Error: " + errorText + " (code = " + errorCode + ").");
Log.Error("Error sending MAPI Email. Error: " + errorText + " (code = " + errorCode + ").");
MessageBox.Show(errorText, "Mail (MAPI) destination", MessageBoxButtons.OK, MessageBoxIcon.Error);
// Recover from bad settings, show again
if (errorCode != MAPI_CODES.INVALID_RECIPS)
{
return;
}
_recipientCollection = new RecipientCollection();
Recipients = new RecipientCollection();
_ShowMail();
}
}
@ -340,8 +318,8 @@ namespace Greenshot.Helpers {
position = -1
};
IntPtr runptr = ptra;
for (int i = 0; i < Files.Count; i++) {
string path = Files[i];
foreach (string path in Files)
{
mfd.name = Path.GetFileName(path);
mfd.path = path;
Marshal.StructureToPtr(mfd, runptr, false);
@ -664,11 +642,7 @@ namespace Greenshot.Helpers {
/// <summary>
/// Returns the recipient stored in this collection at the specified index.
/// </summary>
public Recipient this[int index] {
get {
return (Recipient)List[index];
}
}
public Recipient this[int index] => (Recipient)List[index];
internal InteropRecipientCollection GetInteropRepresentation() {
return new InteropRecipientCollection(this);
@ -680,7 +654,6 @@ namespace Greenshot.Helpers {
internal struct InteropRecipientCollection : IDisposable {
#region Member Variables
private IntPtr _handle;
private int _count;
#endregion Member Variables
@ -695,16 +668,16 @@ namespace Greenshot.Helpers {
_count = outer.Count;
if (_count == 0) {
_handle = IntPtr.Zero;
Handle = IntPtr.Zero;
return;
}
// allocate enough memory to hold all recipients
int size = Marshal.SizeOf(typeof(MapiMailMessage.MapiHelperInterop.MapiRecipDesc));
_handle = Marshal.AllocHGlobal(_count * size);
Handle = Marshal.AllocHGlobal(_count * size);
// place all interop recipients into the memory just allocated
IntPtr ptr = _handle;
IntPtr ptr = Handle;
foreach (Recipient native in outer) {
MapiMailMessage.MapiHelperInterop.MapiRecipDesc interop = native.GetInteropRepresentation();
@ -718,11 +691,7 @@ namespace Greenshot.Helpers {
#region Public Properties
public IntPtr Handle {
get {
return _handle;
}
}
public IntPtr Handle { get; private set; }
#endregion Public Properties
@ -732,21 +701,21 @@ namespace Greenshot.Helpers {
/// Disposes of resources.
/// </summary>
public void Dispose() {
if (_handle != IntPtr.Zero) {
if (Handle != IntPtr.Zero) {
Type type = typeof(MapiMailMessage.MapiHelperInterop.MapiRecipDesc);
int size = Marshal.SizeOf(type);
// destroy all the structures in the memory area
IntPtr ptr = _handle;
IntPtr ptr = Handle;
for (int i = 0; i < _count; i++) {
Marshal.DestroyStructure(ptr, type);
ptr = new IntPtr(ptr.ToInt64() + size);
}
// free the memory
Marshal.FreeHGlobal(_handle);
Marshal.FreeHGlobal(Handle);
_handle = IntPtr.Zero;
Handle = IntPtr.Zero;
_count = 0;
}
}

View file

@ -35,42 +35,31 @@ namespace Greenshot.Helpers {
/// </summary>
[Serializable]
public class PluginHelper : IGreenshotHost {
private static readonly ILog LOG = LogManager.GetLogger(typeof(PluginHelper));
private static readonly CoreConfiguration conf = IniConfig.GetIniSection<CoreConfiguration>();
private static readonly ILog Log = LogManager.GetLogger(typeof(PluginHelper));
private static readonly CoreConfiguration CoreConfig = IniConfig.GetIniSection<CoreConfiguration>();
private static readonly string pluginPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),Application.ProductName);
private static readonly string applicationPath = Path.GetDirectoryName(Application.ExecutablePath);
private static readonly string pafPath = Path.Combine(Application.StartupPath, @"App\Greenshot");
private static readonly string PluginPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),Application.ProductName);
private static readonly string ApplicationPath = Path.GetDirectoryName(Application.ExecutablePath);
private static readonly string PafPath = Path.Combine(Application.StartupPath, @"App\Greenshot");
private static readonly IDictionary<PluginAttribute, IGreenshotPlugin> plugins = new SortedDictionary<PluginAttribute, IGreenshotPlugin>();
private static readonly PluginHelper instance = new PluginHelper();
public static PluginHelper Instance {
get {
return instance;
}
}
public static PluginHelper Instance => instance;
private PluginHelper() {
PluginUtils.Host = this;
}
public Form GreenshotForm {
get {
return MainForm.Instance;
}
}
public Form GreenshotForm => MainForm.Instance;
public NotifyIcon NotifyIcon {
get {
return MainForm.Instance.NotifyIcon;
}
}
public NotifyIcon NotifyIcon => MainForm.Instance.NotifyIcon;
public bool HasPlugins() {
return plugins != null && plugins.Count > 0;
}
public void Shutdown() {
foreach(IGreenshotPlugin plugin in plugins.Values) {
foreach(var plugin in plugins.Values) {
plugin.Shutdown();
plugin.Dispose();
}
@ -79,35 +68,40 @@ namespace Greenshot.Helpers {
// Add plugins to the Listview
public void FillListview(ListView listview) {
foreach(PluginAttribute pluginAttribute in plugins.Keys) {
ListViewItem item = new ListViewItem(pluginAttribute.Name);
foreach(var pluginAttribute in plugins.Keys) {
var item = new ListViewItem(pluginAttribute.Name)
{
Tag = pluginAttribute
};
item.SubItems.Add(pluginAttribute.Version);
item.SubItems.Add(pluginAttribute.CreatedBy);
item.SubItems.Add(pluginAttribute.DllFile);
item.Tag = pluginAttribute;
listview.Items.Add(item);
}
}
public bool isSelectedItemConfigurable(ListView listview) {
if (listview.SelectedItems.Count > 0) {
PluginAttribute pluginAttribute = (PluginAttribute)listview.SelectedItems[0].Tag;
if (pluginAttribute != null) {
return pluginAttribute.Configurable;
}
}
public bool IsSelectedItemConfigurable(ListView listview) {
if (listview.SelectedItems.Count <= 0)
{
return false;
}
var pluginAttribute = (PluginAttribute)listview.SelectedItems[0].Tag;
return pluginAttribute != null && pluginAttribute.Configurable;
}
public void ConfigureSelectedItem(ListView listview) {
if (listview.SelectedItems.Count > 0) {
PluginAttribute pluginAttribute = (PluginAttribute)listview.SelectedItems[0].Tag;
if (pluginAttribute != null) {
IGreenshotPlugin plugin = plugins[pluginAttribute];
if (listview.SelectedItems.Count <= 0)
{
return;
}
var pluginAttribute = (PluginAttribute)listview.SelectedItems[0].Tag;
if (pluginAttribute == null)
{
return;
}
var plugin = plugins[pluginAttribute];
plugin.Configure();
}
}
}
#region Implementation of IGreenshotPluginHost
@ -115,6 +109,8 @@ namespace Greenshot.Helpers {
/// Create a Thumbnail
/// </summary>
/// <param name="image">Image of which we need a Thumbnail</param>
/// <param name="width">Thumbnail width</param>
/// <param name="height">Thumbnail height</param>
/// <returns>Image with Thumbnail</returns>
public Image GetThumbnail(Image image, int width, int height) {
return image.GetThumbnailImage(width, height, ThumbnailCallback, IntPtr.Zero);
@ -128,15 +124,9 @@ namespace Greenshot.Helpers {
return true;
}
public ContextMenuStrip MainMenu {
get {
return MainForm.Instance.MainMenu;
}
}
public ContextMenuStrip MainMenu => MainForm.Instance.MainMenu;
public IDictionary<PluginAttribute, IGreenshotPlugin> Plugins {
get {return plugins;}
}
public IDictionary<PluginAttribute, IGreenshotPlugin> Plugins => plugins;
public IDestination GetDestination(string designation) {
return DestinationHelper.GetDestination(designation);
@ -161,7 +151,7 @@ namespace Greenshot.Helpers {
/// <summary>
/// Use the supplied image, and handle it as if it's captured.
/// </summary>
/// <param name="imageToImport">Image to handle</param>
/// <param name="captureToImport">Image to handle</param>
public void ImportCapture(ICapture captureToImport) {
MainForm.Instance.BeginInvoke((MethodInvoker)delegate {
CaptureHelper.ImportCapture(captureToImport);
@ -173,10 +163,14 @@ namespace Greenshot.Helpers {
/// </summary>
/// <returns></returns>
public ICapture GetCapture(Image imageToCapture) {
Capture capture = new Capture(imageToCapture);
capture.CaptureDetails = new CaptureDetails();
capture.CaptureDetails.CaptureMode = CaptureMode.Import;
capture.CaptureDetails.Title = "Imported";
var capture = new Capture(imageToCapture)
{
CaptureDetails = new CaptureDetails
{
CaptureMode = CaptureMode.Import,
Title = "Imported"
}
};
return capture;
}
#endregion
@ -220,9 +214,8 @@ namespace Greenshot.Helpers {
pluginFiles.Add(pluginFile);
}
} catch (UnauthorizedAccessException) {
return;
} catch (Exception ex) {
LOG.Error("Error loading plugin: ", ex);
Log.Error("Error loading plugin: ", ex);
}
}
}
@ -234,10 +227,10 @@ namespace Greenshot.Helpers {
List<string> pluginFiles = new List<string>();
if (IniConfig.IsPortable) {
findPluginsOnPath(pluginFiles, pafPath);
findPluginsOnPath(pluginFiles, PafPath);
} else {
findPluginsOnPath(pluginFiles, pluginPath);
findPluginsOnPath(pluginFiles, applicationPath);
findPluginsOnPath(pluginFiles, PluginPath);
findPluginsOnPath(pluginFiles, ApplicationPath);
}
Dictionary<string, PluginAttribute> tmpAttributes = new Dictionary<string, PluginAttribute>();
@ -277,36 +270,36 @@ namespace Greenshot.Helpers {
}
if (checkPluginAttribute != null) {
LOG.WarnFormat("Duplicate plugin {0} found", pluginAttribute.Name);
Log.WarnFormat("Duplicate plugin {0} found", pluginAttribute.Name);
if (isNewer(pluginAttribute.Version, checkPluginAttribute.Version)) {
// Found is newer
tmpAttributes[pluginAttribute.Name] = pluginAttribute;
tmpAssemblies[pluginAttribute.Name] = assembly;
LOG.InfoFormat("Loading the newer plugin {0} with version {1} from {2}", pluginAttribute.Name, pluginAttribute.Version, pluginAttribute.DllFile);
Log.InfoFormat("Loading the newer plugin {0} with version {1} from {2}", pluginAttribute.Name, pluginAttribute.Version, pluginAttribute.DllFile);
} else {
LOG.InfoFormat("Skipping (as the duplicate is newer or same version) the plugin {0} with version {1} from {2}", pluginAttribute.Name, pluginAttribute.Version, pluginAttribute.DllFile);
Log.InfoFormat("Skipping (as the duplicate is newer or same version) the plugin {0} with version {1} from {2}", pluginAttribute.Name, pluginAttribute.Version, pluginAttribute.DllFile);
}
continue;
}
if (conf.ExcludePlugins != null && conf.ExcludePlugins.Contains(pluginAttribute.Name)) {
LOG.WarnFormat("Exclude list: {0}", conf.ExcludePlugins.ToArray());
LOG.WarnFormat("Skipping the excluded plugin {0} with version {1} from {2}", pluginAttribute.Name, pluginAttribute.Version, pluginAttribute.DllFile);
if (CoreConfig.ExcludePlugins != null && CoreConfig.ExcludePlugins.Contains(pluginAttribute.Name)) {
Log.WarnFormat("Exclude list: {0}", CoreConfig.ExcludePlugins.ToArray());
Log.WarnFormat("Skipping the excluded plugin {0} with version {1} from {2}", pluginAttribute.Name, pluginAttribute.Version, pluginAttribute.DllFile);
continue;
}
if (conf.IncludePlugins != null && conf.IncludePlugins.Count > 0 && !conf.IncludePlugins.Contains(pluginAttribute.Name)) {
if (CoreConfig.IncludePlugins != null && CoreConfig.IncludePlugins.Count > 0 && !CoreConfig.IncludePlugins.Contains(pluginAttribute.Name)) {
// Whitelist is set
LOG.WarnFormat("Include list: {0}", conf.IncludePlugins.ToArray());
LOG.WarnFormat("Skipping the not included plugin {0} with version {1} from {2}", pluginAttribute.Name, pluginAttribute.Version, pluginAttribute.DllFile);
Log.WarnFormat("Include list: {0}", CoreConfig.IncludePlugins.ToArray());
Log.WarnFormat("Skipping the not included plugin {0} with version {1} from {2}", pluginAttribute.Name, pluginAttribute.Version, pluginAttribute.DllFile);
continue;
}
LOG.InfoFormat("Loading the plugin {0} with version {1} from {2}", pluginAttribute.Name, pluginAttribute.Version, pluginAttribute.DllFile);
Log.InfoFormat("Loading the plugin {0} with version {1} from {2}", pluginAttribute.Name, pluginAttribute.Version, pluginAttribute.DllFile);
tmpAttributes[pluginAttribute.Name] = pluginAttribute;
tmpAssemblies[pluginAttribute.Name] = assembly;
} else {
LOG.ErrorFormat("Can't find the needed Plugin Attribute ({0}) in the assembly of the file \"{1}\", skipping this file.", typeof(PluginAttribute), pluginFile);
Log.ErrorFormat("Can't find the needed Plugin Attribute ({0}) in the assembly of the file \"{1}\", skipping this file.", typeof(PluginAttribute), pluginFile);
}
} catch (Exception e) {
LOG.Warn("Can't load file: " + pluginFile, e);
Log.Warn("Can't load file: " + pluginFile, e);
}
}
foreach(string pluginName in tmpAttributes.Keys) {
@ -315,7 +308,7 @@ namespace Greenshot.Helpers {
Assembly assembly = tmpAssemblies[pluginName];
Type entryType = assembly.GetType(pluginAttribute.EntryType);
if (entryType == null) {
LOG.ErrorFormat("Can't find the in the PluginAttribute referenced type {0} in \"{1}\"", pluginAttribute.EntryType, pluginAttribute.DllFile);
Log.ErrorFormat("Can't find the in the PluginAttribute referenced type {0} in \"{1}\"", pluginAttribute.EntryType, pluginAttribute.DllFile);
continue;
}
try {
@ -324,16 +317,16 @@ namespace Greenshot.Helpers {
if (plugin.Initialize(this, pluginAttribute)) {
plugins.Add(pluginAttribute, plugin);
} else {
LOG.InfoFormat("Plugin {0} not initialized!", pluginAttribute.Name);
Log.InfoFormat("Plugin {0} not initialized!", pluginAttribute.Name);
}
} else {
LOG.ErrorFormat("Can't create an instance of the in the PluginAttribute referenced type {0} from \"{1}\"", pluginAttribute.EntryType, pluginAttribute.DllFile);
Log.ErrorFormat("Can't create an instance of the in the PluginAttribute referenced type {0} from \"{1}\"", pluginAttribute.EntryType, pluginAttribute.DllFile);
}
} catch(Exception e) {
LOG.Error("Can't load Plugin: " + pluginAttribute.Name, e);
Log.Error("Can't load Plugin: " + pluginAttribute.Name, e);
}
} catch(Exception e) {
LOG.Error("Can't load Plugin: " + pluginName, e);
Log.Error("Can't load Plugin: " + pluginName, e);
}
}
}

View file

@ -28,7 +28,7 @@ using Greenshot.Forms;
using Greenshot.Plugin;
using GreenshotPlugin.Core;
using Greenshot.IniFile;
using Greenshot.Core;
using GreenshotPlugin.Effects;
using log4net;
namespace Greenshot.Helpers {
@ -36,21 +36,21 @@ namespace Greenshot.Helpers {
/// Description of PrintHelper.
/// </summary>
public class PrintHelper : IDisposable {
private static readonly ILog LOG = LogManager.GetLogger(typeof(PrintHelper));
private static readonly CoreConfiguration conf = IniConfig.GetIniSection<CoreConfiguration>();
private static readonly ILog Log = LogManager.GetLogger(typeof(PrintHelper));
private static readonly CoreConfiguration CoreConfig = IniConfig.GetIniSection<CoreConfiguration>();
private ISurface surface;
private readonly ICaptureDetails captureDetails;
private PrintDocument printDocument = new PrintDocument();
private PrintDialog printDialog = new PrintDialog();
private ISurface _surface;
private readonly ICaptureDetails _captureDetails;
private PrintDocument _printDocument = new PrintDocument();
private PrintDialog _printDialog = new PrintDialog();
public PrintHelper(ISurface surface, ICaptureDetails captureDetails) {
this.surface = surface;
this.captureDetails = captureDetails;
printDialog.UseEXDialog = true;
printDocument.DocumentName = FilenameHelper.GetFilenameWithoutExtensionFromPattern(conf.OutputFileFilenamePattern, captureDetails);
printDocument.PrintPage += DrawImageForPrint;
printDialog.Document = printDocument;
_surface = surface;
_captureDetails = captureDetails;
_printDialog.UseEXDialog = true;
_printDocument.DocumentName = FilenameHelper.GetFilenameWithoutExtensionFromPattern(CoreConfig.OutputFileFilenamePattern, captureDetails);
_printDocument.PrintPage += DrawImageForPrint;
_printDialog.Document = _printDocument;
}
/**
@ -75,16 +75,12 @@ namespace Greenshot.Helpers {
*/
protected virtual void Dispose(bool disposing) {
if (disposing) {
if (printDocument != null) {
printDocument.Dispose();
_printDocument?.Dispose();
_printDialog?.Dispose();
}
if (printDialog != null) {
printDialog.Dispose();
}
}
surface = null;
printDocument = null;
printDialog = null;
_surface = null;
_printDocument = null;
_printDialog = null;
}
/// <summary>
@ -97,15 +93,15 @@ namespace Greenshot.Helpers {
DialogResult? printOptionsResult = ShowPrintOptionsDialog();
try {
if (printOptionsResult == null || printOptionsResult == DialogResult.OK) {
printDocument.PrinterSettings.PrinterName = printerName;
_printDocument.PrinterSettings.PrinterName = printerName;
if (!IsColorPrint()) {
printDocument.DefaultPageSettings.Color = false;
_printDocument.DefaultPageSettings.Color = false;
}
printDocument.Print();
returnPrinterSettings = printDocument.PrinterSettings;
_printDocument.Print();
returnPrinterSettings = _printDocument.PrinterSettings;
}
} catch (Exception e) {
LOG.Error("An error ocurred while trying to print", e);
Log.Error("An error ocurred while trying to print", e);
MessageBox.Show(Language.GetString(LangKey.print_error), Language.GetString(LangKey.error));
}
return returnPrinterSettings;
@ -118,18 +114,18 @@ namespace Greenshot.Helpers {
/// <returns>printer settings if actually printed, or null if print was cancelled or has failed</returns>
public PrinterSettings PrintWithDialog() {
PrinterSettings returnPrinterSettings = null;
if (printDialog.ShowDialog() == DialogResult.OK) {
if (_printDialog.ShowDialog() == DialogResult.OK) {
DialogResult? printOptionsResult = ShowPrintOptionsDialog();
try {
if (printOptionsResult == null || printOptionsResult == DialogResult.OK) {
if (!IsColorPrint()) {
printDocument.DefaultPageSettings.Color = false;
_printDocument.DefaultPageSettings.Color = false;
}
printDocument.Print();
returnPrinterSettings = printDialog.PrinterSettings;
_printDocument.Print();
returnPrinterSettings = _printDialog.PrinterSettings;
}
} catch (Exception e) {
LOG.Error("An error ocurred while trying to print", e);
Log.Error("An error ocurred while trying to print", e);
MessageBox.Show(Language.GetString(LangKey.print_error), Language.GetString(LangKey.error));
}
@ -138,7 +134,7 @@ namespace Greenshot.Helpers {
}
private bool IsColorPrint() {
return !conf.OutputPrintGrayscale && !conf.OutputPrintMonochrome;
return !CoreConfig.OutputPrintGrayscale && !CoreConfig.OutputPrintMonochrome;
}
/// <summary>
@ -147,7 +143,7 @@ namespace Greenshot.Helpers {
/// <returns>result of the print dialog, or null if the dialog has not been displayed by config</returns>
private DialogResult? ShowPrintOptionsDialog() {
DialogResult? ret = null;
if (conf.OutputPrintPromptOptions) {
if (CoreConfig.OutputPrintPromptOptions) {
using (PrintOptionsDialog printOptionsDialog = new PrintOptionsDialog()) {
ret = printOptionsDialog.ShowDialog();
}
@ -164,16 +160,16 @@ namespace Greenshot.Helpers {
ApplyEffects(printOutputSettings);
Image image;
bool disposeImage = ImageOutput.CreateImageFromSurface(surface, printOutputSettings, out image);
bool disposeImage = ImageOutput.CreateImageFromSurface(_surface, printOutputSettings, out image);
try {
ContentAlignment alignment = conf.OutputPrintCenter ? ContentAlignment.MiddleCenter : ContentAlignment.TopLeft;
ContentAlignment alignment = CoreConfig.OutputPrintCenter ? ContentAlignment.MiddleCenter : ContentAlignment.TopLeft;
// prepare timestamp
float footerStringWidth = 0;
float footerStringHeight = 0;
string footerString = null; //DateTime.Now.ToLongDateString() + " " + DateTime.Now.ToLongTimeString();
if (conf.OutputPrintFooter) {
footerString = FilenameHelper.FillPattern(conf.OutputPrintFooterPattern, captureDetails, false);
if (CoreConfig.OutputPrintFooter) {
footerString = FilenameHelper.FillPattern(CoreConfig.OutputPrintFooterPattern, _captureDetails, false);
using (Font f = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular)) {
footerStringWidth = e.Graphics.MeasureString(footerString, f).Width;
footerStringHeight = e.Graphics.MeasureString(footerString, f).Height;
@ -194,7 +190,7 @@ namespace Greenshot.Helpers {
GraphicsUnit gu = GraphicsUnit.Pixel;
RectangleF imageRect = image.GetBounds(ref gu);
// rotate the image if it fits the page better
if (conf.OutputPrintAllowRotate) {
if (CoreConfig.OutputPrintAllowRotate) {
if ((pageRect.Width > pageRect.Height && imageRect.Width < imageRect.Height) || (pageRect.Width < pageRect.Height && imageRect.Width > imageRect.Height)) {
image.RotateFlip(RotateFlipType.Rotate270FlipNone);
imageRect = image.GetBounds(ref gu);
@ -206,16 +202,16 @@ namespace Greenshot.Helpers {
RectangleF printRect = new RectangleF(0, 0, imageRect.Width, imageRect.Height);
// scale the image to fit the page better
if (conf.OutputPrintAllowEnlarge || conf.OutputPrintAllowShrink) {
if (CoreConfig.OutputPrintAllowEnlarge || CoreConfig.OutputPrintAllowShrink) {
SizeF resizedRect = ScaleHelper.GetScaledSize(imageRect.Size, pageRect.Size, false);
if ((conf.OutputPrintAllowShrink && resizedRect.Width < printRect.Width) || conf.OutputPrintAllowEnlarge && resizedRect.Width > printRect.Width) {
if ((CoreConfig.OutputPrintAllowShrink && resizedRect.Width < printRect.Width) || CoreConfig.OutputPrintAllowEnlarge && resizedRect.Width > printRect.Width) {
printRect.Size = resizedRect;
}
}
// align the image
printRect = ScaleHelper.GetAlignedRectangle(printRect, new RectangleF(0, 0, pageRect.Width, pageRect.Height), alignment);
if (conf.OutputPrintFooter) {
if (CoreConfig.OutputPrintFooter) {
//printRect = new RectangleF(0, 0, printRect.Width, printRect.Height - (dateStringHeight * 2));
using (Font f = new Font(FontFamily.GenericSansSerif, 10, FontStyle.Regular)) {
e.Graphics.DrawString(footerString, f, Brushes.Black, pageRect.Width / 2 - footerStringWidth / 2, pageRect.Height);
@ -224,9 +220,8 @@ namespace Greenshot.Helpers {
e.Graphics.DrawImage(image, printRect, imageRect, GraphicsUnit.Pixel);
} finally {
if (disposeImage && image != null) {
image.Dispose();
image = null;
if (disposeImage) {
image?.Dispose();
}
}
}
@ -234,14 +229,14 @@ namespace Greenshot.Helpers {
private void ApplyEffects(SurfaceOutputSettings printOutputSettings) {
// TODO:
// add effects here
if (conf.OutputPrintMonochrome) {
byte threshold = conf.OutputPrintMonochromeThreshold;
if (CoreConfig.OutputPrintMonochrome) {
byte threshold = CoreConfig.OutputPrintMonochromeThreshold;
printOutputSettings.Effects.Add(new MonochromeEffect(threshold));
printOutputSettings.ReduceColors = true;
}
// the invert effect should probably be the last
if (conf.OutputPrintInverted) {
if (CoreConfig.OutputPrintInverted) {
printOutputSettings.Effects.Add(new InvertEffect());
}
}

View file

@ -36,8 +36,8 @@ namespace Greenshot.Helpers {
/// See: http://www.codeproject.com/KB/audio-video/soundplayerbug.aspx?msg=2487569
/// </summary>
public static class SoundHelper {
private static readonly ILog LOG = LogManager.GetLogger(typeof(SoundHelper));
private static readonly CoreConfiguration conf = IniConfig.GetIniSection<CoreConfiguration>();
private static readonly ILog Log = LogManager.GetLogger(typeof(SoundHelper));
private static readonly CoreConfiguration CoreConfig = IniConfig.GetIniSection<CoreConfiguration>();
private static GCHandle? _gcHandle;
private static byte[] _soundBuffer;
@ -47,19 +47,19 @@ namespace Greenshot.Helpers {
ResourceManager resources = new ResourceManager("Greenshot.Sounds", Assembly.GetExecutingAssembly());
_soundBuffer = (byte[])resources.GetObject("camera");
if (conf.NotificationSound != null && conf.NotificationSound.EndsWith(".wav")) {
if (CoreConfig.NotificationSound != null && CoreConfig.NotificationSound.EndsWith(".wav")) {
try {
if (File.Exists(conf.NotificationSound)) {
_soundBuffer = File.ReadAllBytes(conf.NotificationSound);
if (File.Exists(CoreConfig.NotificationSound)) {
_soundBuffer = File.ReadAllBytes(CoreConfig.NotificationSound);
}
} catch (Exception ex) {
LOG.WarnFormat("couldn't load {0}: {1}", conf.NotificationSound, ex.Message);
Log.WarnFormat("couldn't load {0}: {1}", CoreConfig.NotificationSound, ex.Message);
}
}
// Pin sound so it can't be moved by the Garbage Collector, this was the cause for the bad sound
_gcHandle = GCHandle.Alloc(_soundBuffer, GCHandleType.Pinned);
} catch (Exception e) {
LOG.Error("Error initializing.", e);
Log.Error("Error initializing.", e);
}
}
}
@ -69,9 +69,9 @@ namespace Greenshot.Helpers {
//Thread playSoundThread = new Thread(delegate() {
SoundFlags flags = SoundFlags.SND_ASYNC | SoundFlags.SND_MEMORY | SoundFlags.SND_NOWAIT | SoundFlags.SND_NOSTOP;
try {
WinMM.PlaySound(_gcHandle.Value.AddrOfPinnedObject(), (UIntPtr)0, (uint)flags);
if (_gcHandle != null) WinMM.PlaySound(_gcHandle.Value.AddrOfPinnedObject(), (UIntPtr)0, (uint)flags);
} catch (Exception e) {
LOG.Error("Error in play.", e);
Log.Error("Error in play.", e);
}
//});
//playSoundThread.Name = "Play camera sound";
@ -83,12 +83,12 @@ namespace Greenshot.Helpers {
public static void Deinitialize() {
try {
if (_gcHandle != null) {
WinMM.PlaySound((byte[])null, (UIntPtr)0, (uint)0);
WinMM.PlaySound(null, (UIntPtr)0, 0);
_gcHandle.Value.Free();
_gcHandle = null;
}
} catch (Exception e) {
LOG.Error("Error in deinitialize.", e);
Log.Error("Error in deinitialize.", e);
}
}
}

View file

@ -29,12 +29,12 @@ namespace Greenshot.Helpers {
/// A helper class for the startup registry
/// </summary>
public static class StartupHelper {
private static readonly ILog LOG = LogManager.GetLogger(typeof(StartupHelper));
private static readonly ILog Log = LogManager.GetLogger(typeof(StartupHelper));
private const string RUNKEY6432 = @"Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Run";
private const string RUNKEY = @"Software\Microsoft\Windows\CurrentVersion\Run";
private const string RunKey6432 = @"Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Run";
private const string RunKey = @"Software\Microsoft\Windows\CurrentVersion\Run";
private const string APPLICATIONNAME = "Greenshot";
private const string ApplicationName = "Greenshot";
private static string GetExecutablePath() {
return "\"" + Application.ExecutablePath + "\"";
@ -46,7 +46,8 @@ namespace Greenshot.Helpers {
/// <returns>true if Greenshot can write key</returns>
public static bool CanWriteRunAll() {
try {
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(RUNKEY, true)) {
using (Registry.LocalMachine.OpenSubKey(RunKey, true))
{
}
} catch {
return false;
@ -60,7 +61,8 @@ namespace Greenshot.Helpers {
/// <returns>true if Greenshot can write key</returns>
public static bool CanWriteRunUser() {
try {
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(RUNKEY, true)) {
using (Registry.CurrentUser.OpenSubKey(RunKey, true))
{
}
} catch {
return false;
@ -72,26 +74,29 @@ namespace Greenshot.Helpers {
/// Return the RUN key value of the local machine
/// </summary>
/// <returns>the RUN key value of the local machine</returns>
public static Object GetRunAllValue() {
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(RUNKEY, false)) {
if (key != null) {
object runValue = key.GetValue(APPLICATIONNAME);
if (runValue != null) {
public static object GetRunAllValue()
{
using (var key = Registry.LocalMachine.OpenSubKey(RunKey, false))
{
object runValue = key?.GetValue(ApplicationName);
if (runValue != null)
{
return runValue;
}
}
}
// for 64-bit systems we need to check the 32-bit keys too
if (IntPtr.Size == 8) {
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(RUNKEY6432, false)) {
if (key != null) {
object runValue = key.GetValue(APPLICATIONNAME);
if (runValue != null) {
if (IntPtr.Size != 8)
{
return null;
}
using (var key = Registry.LocalMachine.OpenSubKey(RunKey6432, false))
{
object runValue = key?.GetValue(ApplicationName);
if (runValue != null)
{
return runValue;
}
}
}
}
return null;
}
@ -99,26 +104,24 @@ namespace Greenshot.Helpers {
/// Return the RUN key value of the current user
/// </summary>
/// <returns>the RUN key value of the current user</returns>
public static Object GetRunUserValue() {
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(RUNKEY, false)) {
if (key != null) {
object runValue = key.GetValue(APPLICATIONNAME);
public static object GetRunUserValue() {
using (var key = Registry.CurrentUser.OpenSubKey(RunKey, false)) {
object runValue = key?.GetValue(ApplicationName);
if (runValue != null) {
return runValue;
}
}
}
// for 64-bit systems we need to check the 32-bit keys too
if (IntPtr.Size == 8) {
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(RUNKEY6432, false)) {
if (key != null) {
object runValue = key.GetValue(APPLICATIONNAME);
if (IntPtr.Size != 8)
{
return null;
}
using (var key = Registry.CurrentUser.OpenSubKey(RunKey6432, false)) {
object runValue = key?.GetValue(ApplicationName);
if (runValue != null) {
return runValue;
}
}
}
}
return null;
}
@ -130,7 +133,7 @@ namespace Greenshot.Helpers {
try {
return GetRunAllValue() != null;
} catch (Exception e) {
LOG.Error("Error retrieving RunAllValue", e);
Log.Error("Error retrieving RunAllValue", e);
}
return false;
}
@ -140,11 +143,11 @@ namespace Greenshot.Helpers {
/// </summary>
/// <returns>true if there is a run key</returns>
public static bool HasRunUser() {
Object runValue = null;
object runValue = null;
try {
runValue = GetRunUserValue();
} catch (Exception e) {
LOG.Error("Error retrieving RunUserValue", e);
Log.Error("Error retrieving RunUserValue", e);
}
return runValue != null;
}
@ -153,24 +156,29 @@ namespace Greenshot.Helpers {
/// Delete the RUN key for the localmachine ("ALL")
/// </summary>
public static void DeleteRunAll() {
if (HasRunAll()) {
if (!HasRunAll())
{
return;
}
try {
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(RUNKEY, true)) {
key.DeleteValue(APPLICATIONNAME);
using (var key = Registry.LocalMachine.OpenSubKey(RunKey, true)) {
key?.DeleteValue(ApplicationName);
}
} catch (Exception e) {
LOG.Error("Error in deleteRunAll.", e);
Log.Error("Error in deleteRunAll.", e);
}
try {
try
{
// for 64-bit systems we need to delete the 32-bit keys too
if (IntPtr.Size == 8) {
using (RegistryKey key = Registry.LocalMachine.OpenSubKey(RUNKEY6432, false)) {
key.DeleteValue(APPLICATIONNAME);
if (IntPtr.Size != 8)
{
return;
}
using (var key = Registry.LocalMachine.OpenSubKey(RunKey6432, false)) {
key?.DeleteValue(ApplicationName);
}
} catch (Exception e) {
LOG.Error("Error in deleteRunAll.", e);
}
Log.Error("Error in deleteRunAll.", e);
}
}
@ -178,24 +186,29 @@ namespace Greenshot.Helpers {
/// Delete the RUN key for the current user
/// </summary>
public static void DeleteRunUser() {
if (HasRunUser()) {
if (!HasRunUser())
{
return;
}
try {
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(RUNKEY, true)) {
key.DeleteValue(APPLICATIONNAME);
using (var key = Registry.CurrentUser.OpenSubKey(RunKey, true)) {
key?.DeleteValue(ApplicationName);
}
} catch (Exception e) {
LOG.Error("Error in deleteRunUser.", e);
Log.Error("Error in deleteRunUser.", e);
}
try {
try
{
// for 64-bit systems we need to delete the 32-bit keys too
if (IntPtr.Size == 8) {
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(RUNKEY6432, false)) {
key.DeleteValue(APPLICATIONNAME);
if (IntPtr.Size != 8)
{
return;
}
using (var key = Registry.CurrentUser.OpenSubKey(RunKey6432, false)) {
key?.DeleteValue(ApplicationName);
}
} catch (Exception e) {
LOG.Error("Error in deleteRunUser.", e);
}
Log.Error("Error in deleteRunUser.", e);
}
}
@ -204,11 +217,12 @@ namespace Greenshot.Helpers {
/// </summary>
public static void SetRunUser() {
try {
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(RUNKEY, true)) {
key.SetValue(APPLICATIONNAME, GetExecutablePath());
using (var key = Registry.CurrentUser.OpenSubKey(RunKey, true))
{
key?.SetValue(ApplicationName, GetExecutablePath());
}
} catch (Exception e) {
LOG.Error("Error in setRunUser.", e);
Log.Error("Error in setRunUser.", e);
}
}
@ -221,19 +235,22 @@ namespace Greenshot.Helpers {
string lnkName = Path.GetFileNameWithoutExtension(Application.ExecutablePath) + ".lnk";
string startupPath = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
if (Directory.Exists(startupPath)) {
LOG.DebugFormat("Startup path: {0}", startupPath);
Log.DebugFormat("Startup path: {0}", startupPath);
if (File.Exists(Path.Combine(startupPath, lnkName))) {
return true;
}
}
string startupAll = Environment.GetEnvironmentVariable("ALLUSERSPROFILE") + @"\Microsoft\Windows\Start Menu\Programs\Startup";
if (Directory.Exists(startupAll)) {
LOG.DebugFormat("Startup all path: {0}", startupAll);
Log.DebugFormat("Startup all path: {0}", startupAll);
if (File.Exists(Path.Combine(startupAll, lnkName))) {
return true;
}
}
} catch {
}
catch
{
// ignored
}
return false;
}

View file

@ -34,37 +34,39 @@ namespace Greenshot.Experimental {
/// Description of RssFeedHelper.
/// </summary>
public static class UpdateHelper {
private static readonly ILog LOG = LogManager.GetLogger(typeof(UpdateHelper));
private static readonly CoreConfiguration conf = IniConfig.GetIniSection<CoreConfiguration>();
private const string STABLE_DOWNLOAD_LINK = "http://getgreenshot.org/downloads/";
private const string VERSION_HISTORY_LINK = "http://getgreenshot.org/version-history/";
private static readonly ILog Log = LogManager.GetLogger(typeof(UpdateHelper));
private static readonly CoreConfiguration CoreConfig = IniConfig.GetIniSection<CoreConfiguration>();
private const string StableDownloadLink = "http://getgreenshot.org/downloads/";
private const string VersionHistoryLink = "http://getgreenshot.org/version-history/";
private static readonly object LockObject = new object();
private static RssFile _latestGreenshot;
private static string _downloadLink = STABLE_DOWNLOAD_LINK;
private static string _downloadLink = StableDownloadLink;
/// <summary>
/// Is an update check needed?
/// </summary>
/// <returns>bool true if yes</returns>
public static bool IsUpdateCheckNeeded() {
lock (LockObject) {
if (conf.UpdateCheckInterval == 0) {
lock (LockObject)
{
if (CoreConfig.UpdateCheckInterval == 0)
{
return false;
}
if (conf.LastUpdateCheck != null) {
DateTime checkTime = conf.LastUpdateCheck;
checkTime = checkTime.AddDays(conf.UpdateCheckInterval);
if (DateTime.Now.CompareTo(checkTime) < 0) {
LOG.DebugFormat("No need to check RSS feed for updates, feed check will be after {0}", checkTime);
DateTime checkTime = CoreConfig.LastUpdateCheck;
checkTime = checkTime.AddDays(CoreConfig.UpdateCheckInterval);
if (DateTime.Now.CompareTo(checkTime) < 0)
{
Log.DebugFormat("No need to check RSS feed for updates, feed check will be after {0}", checkTime);
return false;
}
LOG.DebugFormat("Update check is due, last check was {0} check needs to be made after {1} (which is one {2} later)", conf.LastUpdateCheck, checkTime, conf.UpdateCheckInterval);
if (!RssHelper.IsRSSModifiedAfter(conf.LastUpdateCheck)) {
LOG.DebugFormat("RSS feed has not been updated since after {0}", conf.LastUpdateCheck);
Log.DebugFormat("Update check is due, last check was {0} check needs to be made after {1} (which is one {2} later)", CoreConfig.LastUpdateCheck, checkTime, CoreConfig.UpdateCheckInterval);
if (!RssHelper.IsRssModifiedAfter(CoreConfig.LastUpdateCheck))
{
Log.DebugFormat("RSS feed has not been updated since after {0}", CoreConfig.LastUpdateCheck);
return false;
}
}
}
return true;
}
@ -79,15 +81,15 @@ namespace Greenshot.Experimental {
try {
_latestGreenshot = null;
ProcessRSSInfo(currentVersion);
ProcessRssInfo(currentVersion);
if (_latestGreenshot != null) {
MainForm.Instance.NotifyIcon.BalloonTipClicked += HandleBalloonTipClick;
MainForm.Instance.NotifyIcon.BalloonTipClosed += CleanupBalloonTipClick;
MainForm.Instance.NotifyIcon.ShowBalloonTip(10000, "Greenshot", Language.GetFormattedString(LangKey.update_found, "'" + _latestGreenshot.File + "'"), ToolTipIcon.Info);
}
conf.LastUpdateCheck = DateTime.Now;
CoreConfig.LastUpdateCheck = DateTime.Now;
} catch (Exception e) {
LOG.Error("An error occured while checking for updates, the error will be ignored: ", e);
Log.Error("An error occured while checking for updates, the error will be ignored: ", e);
}
}
}
@ -112,9 +114,9 @@ namespace Greenshot.Experimental {
}
}
private static void ProcessRSSInfo(Version currentVersion) {
private static void ProcessRssInfo(Version currentVersion) {
// Reset latest Greenshot
IList<RssFile> rssFiles = RssHelper.readRSS();
IList<RssFile> rssFiles = RssHelper.ReadRss();
if (rssFiles == null) {
return;
@ -124,29 +126,29 @@ namespace Greenshot.Experimental {
foreach(RssFile rssFile in rssFiles) {
if (rssFile.File.StartsWith("Greenshot")) {
// check for exe
if (!rssFile.isExe) {
if (!rssFile.IsExe) {
continue;
}
// do we have a version?
if (rssFile.Version == null) {
LOG.DebugFormat("Skipping unversioned exe {0} which is published at {1} : {2}", rssFile.File, rssFile.Pubdate.ToLocalTime(), rssFile.Link);
Log.DebugFormat("Skipping unversioned exe {0} which is published at {1} : {2}", rssFile.File, rssFile.Pubdate.ToLocalTime(), rssFile.Link);
continue;
}
// if the file is unstable, we will skip it when:
// the current version is a release or release candidate AND check unstable is turned off.
if (rssFile.isUnstable) {
if (rssFile.IsUnstable) {
// Skip if we shouldn't check unstables
if ((conf.BuildState == BuildStates.RELEASE) && !conf.CheckForUnstable) {
if ((CoreConfig.BuildState == BuildStates.RELEASE) && !CoreConfig.CheckForUnstable) {
continue;
}
}
// if the file is a release candidate, we will skip it when:
// the current version is a release AND check unstable is turned off.
if (rssFile.isReleaseCandidate) {
if (conf.BuildState == BuildStates.RELEASE && !conf.CheckForUnstable) {
if (rssFile.IsReleaseCandidate) {
if (CoreConfig.BuildState == BuildStates.RELEASE && !CoreConfig.CheckForUnstable) {
continue;
}
}
@ -154,19 +156,19 @@ namespace Greenshot.Experimental {
// Compare versions
int versionCompare = rssFile.Version.CompareTo(currentVersion);
if (versionCompare > 0) {
LOG.DebugFormat("Found newer Greenshot '{0}' with version {1} published at {2} : {3}", rssFile.File, rssFile.Version, rssFile.Pubdate.ToLocalTime(), rssFile.Link);
Log.DebugFormat("Found newer Greenshot '{0}' with version {1} published at {2} : {3}", rssFile.File, rssFile.Version, rssFile.Pubdate.ToLocalTime(), rssFile.Link);
if (_latestGreenshot == null || rssFile.Version.CompareTo(_latestGreenshot.Version) > 0) {
_latestGreenshot = rssFile;
if (rssFile.isReleaseCandidate || rssFile.isUnstable) {
_downloadLink = VERSION_HISTORY_LINK;
if (rssFile.IsReleaseCandidate || rssFile.IsUnstable) {
_downloadLink = VersionHistoryLink;
} else {
_downloadLink = STABLE_DOWNLOAD_LINK;
_downloadLink = StableDownloadLink;
}
}
} else if (versionCompare < 0) {
LOG.DebugFormat("Skipping older greenshot with version {0}", rssFile.Version);
Log.DebugFormat("Skipping older greenshot with version {0}", rssFile.Version);
} else if (versionCompare == 0) {
LOG.DebugFormat("Found current version as exe {0} with version {1} published at {2} : {3}", rssFile.File, rssFile.Version, rssFile.Pubdate.ToLocalTime(), rssFile.Link);
Log.DebugFormat("Found current version as exe {0} with version {1} published at {2} : {3}", rssFile.File, rssFile.Version, rssFile.Pubdate.ToLocalTime(), rssFile.Link);
}
}
}

View file

@ -24,13 +24,9 @@ using System.Windows.Forms;
namespace Greenshot.Helpers {
public class WindowWrapper : IWin32Window {
public WindowWrapper(IntPtr handle) {
_hwnd = handle;
Handle = handle;
}
public IntPtr Handle {
get { return _hwnd; }
}
private readonly IntPtr _hwnd;
public IntPtr Handle { get; }
}
}

View file

@ -46,10 +46,7 @@ namespace Greenshot.Memento
{
if (disposing)
{
if (_containerList != null)
{
_containerList.Dispose();
}
_containerList?.Dispose();
}
_containerList = null;
_surface = null;
@ -62,9 +59,6 @@ namespace Greenshot.Memento
public IMemento Restore()
{
// Store the selected state, as it's overwritten by the RemoveElement
bool selected = _containerList.Selected;
var oldState = new DeleteElementsMemento(_surface, _containerList);
_surface.RemoveElements(_containerList, false);

View file

@ -30,8 +30,8 @@ namespace Greenshot.Memento
public class ChangeFieldHolderMemento : IMemento
{
private IDrawableContainer _drawableContainer;
private IField _fieldToBeChanged;
private object _oldValue;
private readonly IField _fieldToBeChanged;
private readonly object _oldValue;
public ChangeFieldHolderMemento(IDrawableContainer drawableContainer, IField fieldToBeChanged)
{
@ -49,10 +49,7 @@ namespace Greenshot.Memento
{
if (disposing)
{
if (_drawableContainer != null)
{
_drawableContainer.Dispose();
}
_drawableContainer?.Dispose();
}
_drawableContainer = null;
}

View file

@ -46,10 +46,7 @@ namespace Greenshot.Memento
{
if (disposing)
{
if (_containerList != null)
{
_containerList.Dispose();
}
_containerList?.Dispose();
}
_containerList = null;
_surface = null;

View file

@ -31,30 +31,32 @@ namespace Greenshot.Memento
/// </summary>
public class DrawableContainerBoundsChangeMemento : IMemento
{
private List<Point> points = new List<Point>();
private List<Size> sizes = new List<Size>();
private IDrawableContainerList listOfdrawableContainer;
private readonly List<Point> _points = new List<Point>();
private readonly List<Size> _sizes = new List<Size>();
private IDrawableContainerList _listOfdrawableContainer;
private void StoreBounds()
{
foreach (IDrawableContainer drawableContainer in listOfdrawableContainer)
foreach (IDrawableContainer drawableContainer in _listOfdrawableContainer)
{
points.Add(drawableContainer.Location);
sizes.Add(drawableContainer.Size);
_points.Add(drawableContainer.Location);
_sizes.Add(drawableContainer.Size);
}
}
public DrawableContainerBoundsChangeMemento(IDrawableContainerList listOfdrawableContainer)
{
this.listOfdrawableContainer = listOfdrawableContainer;
_listOfdrawableContainer = listOfdrawableContainer;
StoreBounds();
}
public DrawableContainerBoundsChangeMemento(IDrawableContainer drawableContainer)
{
listOfdrawableContainer = new DrawableContainerList();
listOfdrawableContainer.Add(drawableContainer);
listOfdrawableContainer.Parent = drawableContainer.Parent;
_listOfdrawableContainer = new DrawableContainerList
{
drawableContainer
};
_listOfdrawableContainer.Parent = drawableContainer.Parent;
StoreBounds();
}
@ -67,12 +69,9 @@ namespace Greenshot.Memento
{
if (disposing)
{
if (listOfdrawableContainer != null)
{
listOfdrawableContainer.Dispose();
_listOfdrawableContainer?.Dispose();
}
}
listOfdrawableContainer = null;
_listOfdrawableContainer = null;
}
public bool Merge(IMemento otherMemento)
@ -80,7 +79,7 @@ namespace Greenshot.Memento
var other = otherMemento as DrawableContainerBoundsChangeMemento;
if (other != null)
{
if (ObjectExtensions.CompareLists<IDrawableContainer>(listOfdrawableContainer, other.listOfdrawableContainer))
if (ObjectExtensions.CompareLists(_listOfdrawableContainer, other._listOfdrawableContainer))
{
// Lists are equal, as we have the state already we can ignore the new memento
return true;
@ -91,16 +90,16 @@ namespace Greenshot.Memento
public IMemento Restore()
{
var oldState = new DrawableContainerBoundsChangeMemento(listOfdrawableContainer);
for (int index = 0; index < listOfdrawableContainer.Count; index++)
var oldState = new DrawableContainerBoundsChangeMemento(_listOfdrawableContainer);
for (int index = 0; index < _listOfdrawableContainer.Count; index++)
{
IDrawableContainer drawableContainer = listOfdrawableContainer[index];
IDrawableContainer drawableContainer = _listOfdrawableContainer[index];
// Before
drawableContainer.Invalidate();
drawableContainer.Left = points[index].X;
drawableContainer.Top = points[index].Y;
drawableContainer.Width = sizes[index].Width;
drawableContainer.Height = sizes[index].Height;
drawableContainer.Left = _points[index].X;
drawableContainer.Top = _points[index].Y;
drawableContainer.Width = _sizes[index].Width;
drawableContainer.Height = _sizes[index].Height;
// After
drawableContainer.Invalidate();
drawableContainer.Parent.Modified = true;

View file

@ -68,7 +68,7 @@ namespace GreenshotBoxPlugin {
/// </summary>
/// <returns>bool true if OK was pressed, false if cancel</returns>
public bool ShowConfigDialog() {
DialogResult result = new SettingsForm(this).ShowDialog();
DialogResult result = new SettingsForm().ShowDialog();
if (result == DialogResult.OK) {
return true;
}

View file

@ -33,7 +33,7 @@ namespace GreenshotBoxPlugin {
/// Description of ImgurUtils.
/// </summary>
public static class BoxUtils {
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(BoxUtils));
private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(typeof(BoxUtils));
private static readonly BoxConfiguration Config = IniConfig.GetIniSection<BoxConfiguration>();
private const string UploadFileUri = "https://upload.box.com/api/2.0/files/content";
private const string FilesUri = "https://www.box.com/api/2.0/files/{0}";
@ -66,21 +66,23 @@ namespace GreenshotBoxPlugin {
public static string UploadToBox(SurfaceContainer image, string title, string filename) {
// Fill the OAuth2Settings
OAuth2Settings settings = new OAuth2Settings();
var settings = new OAuth2Settings
{
AuthUrlPattern = "https://app.box.com/api/oauth2/authorize?client_id={ClientId}&response_type=code&state={State}&redirect_uri={RedirectUrl}",
TokenUrl = "https://api.box.com/oauth2/token",
CloudServiceName = "Box",
ClientId = BoxCredentials.ClientId,
ClientSecret = BoxCredentials.ClientSecret,
RedirectUrl = "https://www.box.com/home/",
BrowserSize = new Size(1060, 600),
AuthorizeMode = OAuth2AuthorizeMode.EmbeddedBrowser,
RefreshToken = Config.RefreshToken,
AccessToken = Config.AccessToken,
AccessTokenExpires = Config.AccessTokenExpires
};
settings.AuthUrlPattern = "https://app.box.com/api/oauth2/authorize?client_id={ClientId}&response_type=code&state={State}&redirect_uri={RedirectUrl}";
settings.TokenUrl = "https://api.box.com/oauth2/token";
settings.CloudServiceName = "Box";
settings.ClientId = BoxCredentials.ClientId;
settings.ClientSecret = BoxCredentials.ClientSecret;
settings.RedirectUrl = "https://www.box.com/home/";
settings.BrowserSize = new Size(1060, 600);
settings.AuthorizeMode = OAuth2AuthorizeMode.EmbeddedBrowser;
// Copy the settings from the config, which is kept in memory and on the disk
settings.RefreshToken = Config.RefreshToken;
settings.AccessToken = Config.AccessToken;
settings.AccessTokenExpires = Config.AccessTokenExpires;
try {
var webRequest = OAuth2Helper.CreateOAuth2WebRequest(HTTPMethod.POST, UploadFileUri, settings);
@ -92,17 +94,17 @@ namespace GreenshotBoxPlugin {
var response = NetworkHelper.GetResponseAsString(webRequest);
LOG.DebugFormat("Box response: {0}", response);
Log.DebugFormat("Box response: {0}", response);
var upload = JsonSerializer.Deserialize<Upload>(response);
if (upload == null || upload.Entries == null || upload.Entries.Count == 0) return null;
if (upload?.Entries == null || upload.Entries.Count == 0) return null;
if (Config.UseSharedLink) {
string filesResponse = HttpPut(string.Format(FilesUri, upload.Entries[0].Id), "{\"shared_link\": {\"access\": \"open\"}}", settings);
var file = JsonSerializer.Deserialize<FileEntry>(filesResponse);
return file.SharedLink.Url;
}
return string.Format("http://www.box.com/files/0/f/0/1/f_{0}", upload.Entries[0].Id);
return $"http://www.box.com/files/0/f/0/1/f_{upload.Entries[0].Id}";
} finally {
// Copy the settings back to the config, so they are stored.
Config.RefreshToken = settings.RefreshToken;

View file

@ -26,7 +26,7 @@ namespace GreenshotBoxPlugin {
/// Description of PasswordRequestForm.
/// </summary>
public partial class SettingsForm : BoxForm {
public SettingsForm(BoxConfiguration config) {
public SettingsForm() {
//
// The InitializeComponent() call is required for Windows Forms designer support.
//

View file

@ -222,19 +222,17 @@ namespace Confluence {
}
}
public bool IsLoggedIn {
get {
return _loggedIn;
}
}
public bool IsLoggedIn => _loggedIn;
public void AddAttachment(long pageId, string mime, string comment, string filename, IBinaryContainer image) {
CheckCredentials();
RemoteAttachment attachment = new RemoteAttachment();
// Comment is ignored, see: http://jira.atlassian.com/browse/CONF-9395
attachment.comment = comment;
attachment.fileName = filename;
attachment.contentType = mime;
var attachment = new RemoteAttachment
{
comment = comment,
fileName = filename,
contentType = mime
};
_confluence.addAttachment(_credentials, pageId, attachment, image.ToByteArray());
}
@ -284,7 +282,6 @@ namespace Confluence {
public IEnumerable<Page> GetPageChildren(Page parentPage) {
CheckCredentials();
List<Page> returnPages = new List<Page>();
RemotePageSummary[] pages = _confluence.getChildren(_credentials, parentPage.Id);
foreach(RemotePageSummary page in pages) {
yield return new Page(page);

View file

@ -84,15 +84,15 @@ namespace GreenshotConfluencePlugin {
}
}
public override bool isDynamic {
public override bool IsDynamic {
get {
return true;
}
}
public override bool isActive {
public override bool IsActive {
get {
return base.isActive && !string.IsNullOrEmpty(ConfluenceConfig.Url);
return base.IsActive && !string.IsNullOrEmpty(ConfluenceConfig.Url);
}
}
@ -127,7 +127,7 @@ namespace GreenshotConfluencePlugin {
string filename = FilenameHelper.GetFilenameWithoutExtensionFromPattern(CoreConfig.OutputFileFilenamePattern, captureDetails);
if (selectedPage == null) {
ConfluenceUpload confluenceUpload = new ConfluenceUpload(filename);
Nullable<bool> dialogResult = confluenceUpload.ShowDialog();
bool? dialogResult = confluenceUpload.ShowDialog();
if (dialogResult.HasValue && dialogResult.Value) {
selectedPage = confluenceUpload.SelectedPage;
if (confluenceUpload.IsOpenPageSelected) {

View file

@ -124,7 +124,7 @@ namespace GreenshotConfluencePlugin {
ConfluenceConfiguration clonedConfig = _config.Clone();
ConfluenceConfigurationForm configForm = new ConfluenceConfigurationForm(clonedConfig);
string url = _config.Url;
Nullable<bool> dialogResult = configForm.ShowDialog();
bool? dialogResult = configForm.ShowDialog();
if (dialogResult.HasValue && dialogResult.Value) {
// copy the new object to the old...
clonedConfig.CloneTo(_config);

View file

@ -95,7 +95,6 @@ namespace GreenshotConfluencePlugin {
pages.Add(page);
}
continue;
} catch (Exception ex) {
// Preventing security problems
LOG.DebugFormat("Couldn't get page details for space {0} / title {1}", space, title);
@ -133,7 +132,7 @@ namespace GreenshotConfluencePlugin {
if (pattern.ProgrammaticName != "ValuePatternIdentifiers.Pattern") {
continue;
}
string url = (docElement.GetCurrentPattern(pattern) as ValuePattern).Current.Value.ToString();
string url = (docElement.GetCurrentPattern(pattern) as ValuePattern).Current.Value;
if (!string.IsNullOrEmpty(url)) {
urls.Add(url);
break;

View file

@ -30,11 +30,9 @@ using GreenshotPlugin.Core;
namespace GreenshotConfluencePlugin {
public class EnumDisplayer : IValueConverter {
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(EnumDisplayer));
private Type type;
private IDictionary displayValues;
private IDictionary reverseValues;
private Type _type;
private IDictionary _displayValues;
private IDictionary _reverseValues;
public EnumDisplayer() {
}
@ -44,22 +42,30 @@ namespace GreenshotConfluencePlugin {
}
public Type Type {
get { return type; }
get { return _type; }
set {
if (!value.IsEnum) {
throw new ArgumentException("parameter is not an Enumerated type", "value");
throw new ArgumentException("parameter is not an Enumerated type", nameof(value));
}
type = value;
_type = value;
}
}
public ReadOnlyCollection<string> DisplayNames {
get {
reverseValues = (IDictionary) Activator.CreateInstance(typeof(Dictionary<,>).GetGenericTypeDefinition().MakeGenericType(typeof(string),type));
var genericTypeDefinition = typeof(Dictionary<,>).GetGenericTypeDefinition();
if (genericTypeDefinition != null)
{
_reverseValues = (IDictionary) Activator.CreateInstance(genericTypeDefinition.MakeGenericType(typeof(string),_type));
}
displayValues = (IDictionary)Activator.CreateInstance(typeof(Dictionary<,>).GetGenericTypeDefinition().MakeGenericType(type, typeof(string)));
var typeDefinition = typeof(Dictionary<,>).GetGenericTypeDefinition();
if (typeDefinition != null)
{
_displayValues = (IDictionary)Activator.CreateInstance(typeDefinition.MakeGenericType(_type, typeof(string)));
}
var fields = type.GetFields(BindingFlags.Public | BindingFlags.Static);
var fields = _type.GetFields(BindingFlags.Public | BindingFlags.Static);
foreach (var field in fields) {
DisplayKeyAttribute[] a = (DisplayKeyAttribute[])field.GetCustomAttributes(typeof(DisplayKeyAttribute), false);
@ -67,25 +73,19 @@ namespace GreenshotConfluencePlugin {
object enumValue = field.GetValue(null);
string displayString;
if (displayKey != null && Language.hasKey(displayKey)) {
if (displayKey != null && Language.HasKey(displayKey)) {
displayString = Language.GetString(displayKey);
}
if (displayKey != null) {
displayString = displayKey;
} else {
displayString = enumValue.ToString();
}
displayString = displayKey ?? enumValue.ToString();
if (displayString != null) {
displayValues.Add(enumValue, displayString);
reverseValues.Add(displayString, enumValue);
_displayValues.Add(enumValue, displayString);
_reverseValues.Add(displayString, enumValue);
}
}
return new List<string>((IEnumerable<string>)displayValues.Values).AsReadOnly();
return new List<string>((IEnumerable<string>)_displayValues.Values).AsReadOnly();
}
}
private string GetDisplayKeyValue(DisplayKeyAttribute[] a) {
private static string GetDisplayKeyValue(DisplayKeyAttribute[] a) {
if (a == null || a.Length == 0) {
return null;
}
@ -94,11 +94,11 @@ namespace GreenshotConfluencePlugin {
}
object IValueConverter.Convert(object value, Type targetType, object parameter, CultureInfo culture) {
return displayValues[value];
return _displayValues[value];
}
object IValueConverter.ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) {
return reverseValues[value];
return _reverseValues[value];
}
}
}

View file

@ -26,16 +26,11 @@ namespace GreenshotConfluencePlugin {
/// Interaction logic for ConfluenceConfigurationForm.xaml
/// </summary>
public partial class ConfluenceConfigurationForm : Window {
private readonly ConfluenceConfiguration config;
public ConfluenceConfiguration Config {
get {
return config;
}
}
public ConfluenceConfiguration Config { get; }
public ConfluenceConfigurationForm(ConfluenceConfiguration config) {
DataContext = config;
this.config = config;
Config = config;
InitializeComponent();
}

View file

@ -26,12 +26,12 @@ namespace GreenshotConfluencePlugin {
/// <summary>
/// Interaction logic for ConfluencePagePicker.xaml
/// </summary>
public partial class ConfluencePagePicker : System.Windows.Controls.Page {
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(ConfluencePagePicker));
private readonly ConfluenceUpload confluenceUpload = null;
public partial class ConfluencePagePicker
{
private readonly ConfluenceUpload _confluenceUpload;
public ConfluencePagePicker(ConfluenceUpload confluenceUpload, List<Page> pagesToPick) {
this.confluenceUpload = confluenceUpload;
_confluenceUpload = confluenceUpload;
DataContext = pagesToPick;
InitializeComponent();
}
@ -42,11 +42,11 @@ namespace GreenshotConfluencePlugin {
private void SelectionChanged() {
if (PageListView.HasItems && PageListView.SelectedItems.Count > 0) {
confluenceUpload.SelectedPage = (Page)PageListView.SelectedItem;
_confluenceUpload.SelectedPage = (Page)PageListView.SelectedItem;
// Make sure the uploader knows we selected an already opened page
confluenceUpload.IsOpenPageSelected = true;
_confluenceUpload.IsOpenPageSelected = true;
} else {
confluenceUpload.SelectedPage = null;
_confluenceUpload.SelectedPage = null;
}
}

View file

@ -26,7 +26,7 @@ namespace TranslationByMarkupExtension
manager.LanguageChanged += OnLanguageChanged;
}
protected override void StopListening(Object source)
protected override void StopListening(object source)
{
var manager = (TranslationManager)source;
manager.LanguageChanged -= OnLanguageChanged;

View file

@ -11,14 +11,6 @@ namespace TranslationByMarkupExtension {
#region Construction
/// <summary>
/// Initializes a new instance of the <see cref="ResxTranslationProvider"/> class.
/// </summary>
/// <param name="baseName">Name of the base.</param>
/// <param name="assembly">The assembly.</param>
public LanguageXMLTranslationProvider() {
}
#endregion
#region ITranslationProvider Members
@ -27,7 +19,7 @@ namespace TranslationByMarkupExtension {
/// See <see cref="ITranslationProvider.Translate" />
/// </summary>
public object Translate(string key) {
if (Language.hasKey("confluence", key)) {
if (Language.HasKey("confluence", key)) {
return Language.GetString("confluence", key);
}
return key;

View file

@ -27,11 +27,7 @@ namespace TranslationByMarkupExtension {
LanguageChangedEventManager.RemoveListener(TranslationManager.Instance, this);
}
public object Value {
get {
return TranslationManager.Instance.Translate(_key);
}
}
public object Value => TranslationManager.Instance.Translate(_key);
#region IWeakEventListener Members
@ -47,10 +43,7 @@ namespace TranslationByMarkupExtension {
private void OnLanguageChanged(object sender, EventArgs e)
{
if( PropertyChanged != null )
{
PropertyChanged( this, new PropertyChangedEventArgs("Value"));
}
PropertyChanged?.Invoke( this, new PropertyChangedEventArgs("Value"));
}
#endregion

View file

@ -25,31 +25,21 @@ namespace TranslationByMarkupExtension {
}
}*/
public static TranslationManager Instance {
get {
if (_translationManager == null) {
_translationManager = new TranslationManager();
}
return _translationManager;
}
}
public static TranslationManager Instance => _translationManager ?? (_translationManager = new TranslationManager());
public ITranslationProvider TranslationProvider { get; set; }
private void OnLanguageChanged() {
if (LanguageChanged != null) {
LanguageChanged(this, EventArgs.Empty);
}
private void OnLanguageChanged()
{
LanguageChanged?.Invoke(this, EventArgs.Empty);
}
public object Translate(string key) {
if( TranslationProvider != null) {
object translatedValue = TranslationProvider.Translate(key);
object translatedValue = TranslationProvider?.Translate(key);
if( translatedValue != null) {
return translatedValue;
}
}
return string.Format("!{0}!", key);
return $"!{key}!";
}
}
}

View file

@ -23,27 +23,19 @@ using System.Drawing;
using Greenshot.IniFile;
using Greenshot.Plugin;
using GreenshotPlugin.Core;
namespace GreenshotDropboxPlugin {
internal class DropboxDestination : AbstractDestination {
private static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(DropboxDestination));
private static readonly DropboxPluginConfiguration config = IniConfig.GetIniSection<DropboxPluginConfiguration>();
private static readonly DropboxPluginConfiguration DropboxConfig = IniConfig.GetIniSection<DropboxPluginConfiguration>();
private readonly DropboxPlugin plugin = null;
private readonly DropboxPlugin _plugin;
public DropboxDestination(DropboxPlugin plugin) {
this.plugin = plugin;
_plugin = plugin;
}
public override string Designation {
get {
return "Dropbox";
}
}
public override string Designation => "Dropbox";
public override string Description {
get {
return Language.GetString("dropbox", LangKey.upload_menu_item);
}
}
public override string Description => Language.GetString("dropbox", LangKey.upload_menu_item);
public override Image DisplayIcon {
get {
@ -55,11 +47,11 @@ namespace GreenshotDropboxPlugin {
public override ExportInformation ExportCapture(bool manually, ISurface surface, ICaptureDetails captureDetails) {
ExportInformation exportInformation = new ExportInformation(Designation, Description);
string uploadUrl;
bool uploaded = plugin.Upload(captureDetails, surface, out uploadUrl);
bool uploaded = _plugin.Upload(captureDetails, surface, out uploadUrl);
if (uploaded) {
exportInformation.Uri = uploadUrl;
exportInformation.ExportMade = true;
if (config.AfterUploadLinkToClipBoard) {
if (DropboxConfig.AfterUploadLinkToClipBoard) {
ClipboardHelper.SetClipboardData(uploadUrl);
}
}

View file

@ -55,9 +55,6 @@ namespace GreenshotDropboxPlugin {
}
}
public DropboxPlugin() {
}
public IEnumerable<IDestination> Destinations() {
yield return new DropboxDestination(this);
}
@ -123,7 +120,8 @@ namespace GreenshotDropboxPlugin {
try {
string dropboxUrl = null;
new PleaseWaitForm().ShowAndWait(Attributes.Name, Language.GetString("dropbox", LangKey.communication_wait),
delegate() {
delegate
{
string filename = Path.GetFileName(FilenameHelper.GetFilename(_config.UploadFormat, captureDetails));
dropboxUrl = DropboxUtils.UploadToDropbox(surfaceToUpload, outputSettings, filename);
}

View file

@ -30,36 +30,38 @@ namespace GreenshotDropboxPlugin {
/// Description of DropboxUtils.
/// </summary>
public class DropboxUtils {
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(DropboxUtils));
private static readonly DropboxPluginConfiguration config = IniConfig.GetIniSection<DropboxPluginConfiguration>();
private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(typeof(DropboxUtils));
private static readonly DropboxPluginConfiguration DropboxConfig = IniConfig.GetIniSection<DropboxPluginConfiguration>();
private DropboxUtils() {
}
public static string UploadToDropbox(ISurface surfaceToUpload, SurfaceOutputSettings outputSettings, string filename) {
OAuthSession oAuth = new OAuthSession(DropBoxCredentials.CONSUMER_KEY, DropBoxCredentials.CONSUMER_SECRET);
oAuth.BrowserSize = new Size(1080, 650);
oAuth.CheckVerifier = false;
oAuth.AccessTokenUrl = "https://api.dropbox.com/1/oauth/access_token";
oAuth.AuthorizeUrl = "https://api.dropbox.com/1/oauth/authorize";
oAuth.RequestTokenUrl = "https://api.dropbox.com/1/oauth/request_token";
oAuth.LoginTitle = "Dropbox authorization";
oAuth.Token = config.DropboxToken;
oAuth.TokenSecret = config.DropboxTokenSecret;
var oAuth = new OAuthSession(DropBoxCredentials.CONSUMER_KEY, DropBoxCredentials.CONSUMER_SECRET)
{
BrowserSize = new Size(1080, 650),
CheckVerifier = false,
AccessTokenUrl = "https://api.dropbox.com/1/oauth/access_token",
AuthorizeUrl = "https://api.dropbox.com/1/oauth/authorize",
RequestTokenUrl = "https://api.dropbox.com/1/oauth/request_token",
LoginTitle = "Dropbox authorization",
Token = DropboxConfig.DropboxToken,
TokenSecret = DropboxConfig.DropboxTokenSecret
};
try {
SurfaceContainer imageToUpload = new SurfaceContainer(surfaceToUpload, outputSettings, filename);
string uploadResponse = oAuth.MakeOAuthRequest(HTTPMethod.POST, "https://api-content.dropbox.com/1/files_put/sandbox/" + OAuthSession.UrlEncode3986(filename), null, null, imageToUpload);
LOG.DebugFormat("Upload response: {0}", uploadResponse);
Log.DebugFormat("Upload response: {0}", uploadResponse);
} catch (Exception ex) {
LOG.Error("Upload error: ", ex);
Log.Error("Upload error: ", ex);
throw;
} finally {
if (!string.IsNullOrEmpty(oAuth.Token)) {
config.DropboxToken = oAuth.Token;
DropboxConfig.DropboxToken = oAuth.Token;
}
if (!string.IsNullOrEmpty(oAuth.TokenSecret)) {
config.DropboxTokenSecret = oAuth.TokenSecret;
DropboxConfig.DropboxTokenSecret = oAuth.TokenSecret;
}
}
@ -67,14 +69,14 @@ namespace GreenshotDropboxPlugin {
try {
string responseString = oAuth.MakeOAuthRequest(HTTPMethod.GET, "https://api.dropbox.com/1/shares/sandbox/" + OAuthSession.UrlEncode3986(filename), null, null, null);
if (responseString != null) {
LOG.DebugFormat("Parsing output: {0}", responseString);
Log.DebugFormat("Parsing output: {0}", responseString);
IDictionary<string, object> returnValues = JSONHelper.JsonDecode(responseString);
if (returnValues.ContainsKey("url")) {
return returnValues["url"] as string;
}
}
} catch (Exception ex) {
LOG.Error("Can't parse response.", ex);
Log.Error("Can't parse response.", ex);
}
return null;
}

View file

@ -20,16 +20,12 @@
*/
using GreenshotDropboxPlugin.Forms;
using Greenshot.IniFile;
namespace GreenshotDropboxPlugin {
/// <summary>
/// Description of PasswordRequestForm.
/// </summary>
public partial class SettingsForm : DropboxForm {
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(SettingsForm));
private static DropboxPluginConfiguration config = IniConfig.GetIniSection<DropboxPluginConfiguration>();
public SettingsForm() {
//
// The InitializeComponent() call is required for Windows Forms designer support.

View file

@ -31,8 +31,8 @@ namespace ExternalCommand {
/// Description of SettingsFormDetail.
/// </summary>
public partial class SettingsFormDetail : ExternalCommandForm {
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(SettingsFormDetail));
private static readonly ExternalCommandConfiguration config = IniConfig.GetIniSection<ExternalCommandConfiguration>();
private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(typeof(SettingsFormDetail));
private static readonly ExternalCommandConfiguration ExternalCommandConfig = IniConfig.GetIniSection<ExternalCommandConfiguration>();
private readonly string _commando;
private readonly int _commandIndex;
@ -45,9 +45,9 @@ namespace ExternalCommand {
if(commando != null) {
textBox_name.Text = commando;
textBox_commandline.Text = config.Commandline[commando];
textBox_arguments.Text = config.Argument[commando];
_commandIndex = config.Commands.FindIndex(delegate(string s) { return s == commando; });
textBox_commandline.Text = ExternalCommandConfig.Commandline[commando];
textBox_arguments.Text = ExternalCommandConfig.Argument[commando];
_commandIndex = ExternalCommandConfig.Commands.FindIndex(s => s == commando);
} else {
textBox_arguments.Text = "\"{0}\"";
}
@ -59,15 +59,15 @@ namespace ExternalCommand {
string commandLine = textBox_commandline.Text;
string arguments = textBox_arguments.Text;
if(_commando != null) {
config.Commands[_commandIndex] = commandName;
config.Commandline.Remove(_commando);
config.Commandline.Add(commandName, commandLine);
config.Argument.Remove(_commando);
config.Argument.Add(commandName, arguments);
ExternalCommandConfig.Commands[_commandIndex] = commandName;
ExternalCommandConfig.Commandline.Remove(_commando);
ExternalCommandConfig.Commandline.Add(commandName, commandLine);
ExternalCommandConfig.Argument.Remove(_commando);
ExternalCommandConfig.Argument.Add(commandName, arguments);
} else {
config.Commands.Add(commandName);
config.Commandline.Add(commandName, commandLine);
config.Argument.Add(commandName, arguments);
ExternalCommandConfig.Commands.Add(commandName);
ExternalCommandConfig.Commandline.Add(commandName, commandLine);
ExternalCommandConfig.Argument.Add(commandName, arguments);
}
}
@ -86,8 +86,8 @@ namespace ExternalCommand {
}
catch (Exception ex)
{
LOG.WarnFormat("Can't get the initial path via {0}", textBox_commandline.Text);
LOG.Warn("Exception: ", ex);
Log.WarnFormat("Can't get the initial path via {0}", textBox_commandline.Text);
Log.Warn("Exception: ", ex);
}
if(initialPath != null && Directory.Exists(initialPath)) {
openFileDialog.InitialDirectory = initialPath;
@ -95,7 +95,7 @@ namespace ExternalCommand {
initialPath = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
openFileDialog.InitialDirectory = initialPath;
}
LOG.DebugFormat("Starting OpenFileDialog at {0}", initialPath);
Log.DebugFormat("Starting OpenFileDialog at {0}", initialPath);
if(openFileDialog.ShowDialog() == DialogResult.OK) {
textBox_commandline.Text = openFileDialog.FileName;
}
@ -112,7 +112,7 @@ namespace ExternalCommand {
buttonOk.Enabled = false;
}
// Check if commandname is unique
if(_commando == null && !string.IsNullOrEmpty(textBox_name.Text) && config.Commands.Contains(textBox_name.Text)) {
if(_commando == null && !string.IsNullOrEmpty(textBox_name.Text) && ExternalCommandConfig.Commands.Contains(textBox_name.Text)) {
buttonOk.Enabled = false;
textBox_name.BackColor = Color.Red;
}

View file

@ -70,7 +70,7 @@ namespace GreenshotFlickrPlugin {
/// </summary>
/// <returns>bool true if OK was pressed, false if cancel</returns>
public bool ShowConfigDialog() {
DialogResult result = new SettingsForm(this).ShowDialog();
DialogResult result = new SettingsForm().ShowDialog();
if (result == DialogResult.OK) {
return true;
}

View file

@ -22,27 +22,17 @@ using System.ComponentModel;
using System.Drawing;
using Greenshot.Plugin;
using GreenshotPlugin.Core;
using log4net;
namespace GreenshotFlickrPlugin {
public class FlickrDestination : AbstractDestination {
private static ILog LOG = LogManager.GetLogger(typeof(FlickrDestination));
private readonly FlickrPlugin plugin;
private readonly FlickrPlugin _plugin;
public FlickrDestination(FlickrPlugin plugin) {
this.plugin = plugin;
_plugin = plugin;
}
public override string Designation {
get {
return "Flickr";
}
}
public override string Designation => "Flickr";
public override string Description {
get {
return Language.GetString("flickr", LangKey.upload_menu_item);
}
}
public override string Description => Language.GetString("flickr", LangKey.upload_menu_item);
public override Image DisplayIcon {
get {
@ -53,11 +43,11 @@ namespace GreenshotFlickrPlugin {
public override ExportInformation ExportCapture(bool manuallyInitiated, ISurface surface, ICaptureDetails captureDetails) {
ExportInformation exportInformation = new ExportInformation(Designation, Description);
string uploadURL;
bool uploaded = plugin.Upload(captureDetails, surface, out uploadURL);
string uploadUrl;
bool uploaded = _plugin.Upload(captureDetails, surface, out uploadUrl);
if (uploaded) {
exportInformation.ExportMade = true;
exportInformation.Uri = uploadURL;
exportInformation.Uri = uploadUrl;
}
ProcessExport(exportInformation, surface);
return exportInformation;

View file

@ -37,7 +37,7 @@ namespace GreenshotFlickrPlugin
/// This is the Flickr base code
/// </summary>
public class FlickrPlugin : IGreenshotPlugin {
private static readonly ILog LOG = LogManager.GetLogger(typeof(FlickrPlugin));
private static readonly ILog Log = LogManager.GetLogger(typeof(FlickrPlugin));
private static FlickrConfiguration _config;
public static PluginAttribute Attributes;
private IGreenshotHost _host;
@ -83,10 +83,12 @@ namespace GreenshotFlickrPlugin
_config = IniConfig.GetIniSection<FlickrConfiguration>();
_resources = new ComponentResourceManager(typeof(FlickrPlugin));
_itemPlugInConfig = new ToolStripMenuItem();
_itemPlugInConfig.Text = Language.GetString("flickr", LangKey.Configure);
_itemPlugInConfig.Tag = _host;
_itemPlugInConfig.Image = (Image)_resources.GetObject("flickr");
_itemPlugInConfig = new ToolStripMenuItem
{
Text = Language.GetString("flickr", LangKey.Configure),
Tag = _host,
Image = (Image) _resources.GetObject("flickr")
};
_itemPlugInConfig.Click += ConfigMenuClick;
PluginUtils.AddToContextMenu(_host, _itemPlugInConfig);
@ -101,7 +103,7 @@ namespace GreenshotFlickrPlugin
}
public virtual void Shutdown() {
LOG.Debug("Flickr Plugin shutdown.");
Log.Debug("Flickr Plugin shutdown.");
}
/// <summary>
@ -137,7 +139,7 @@ namespace GreenshotFlickrPlugin
}
return true;
} catch (Exception e) {
LOG.Error("Error uploading.", e);
Log.Error("Error uploading.", e);
MessageBox.Show(Language.GetString("flickr", LangKey.upload_failure) + " " + e.Message);
}
return false;

View file

@ -57,15 +57,17 @@ namespace GreenshotFlickrPlugin {
/// <param name="filename"></param>
/// <returns>url to image</returns>
public static string UploadToFlickr(ISurface surfaceToUpload, SurfaceOutputSettings outputSettings, string title, string filename) {
OAuthSession oAuth = new OAuthSession(FlickrCredentials.ConsumerKey, FlickrCredentials.ConsumerSecret);
oAuth.BrowserSize = new Size(520, 800);
oAuth.CheckVerifier = false;
oAuth.AccessTokenUrl = FLICKR_ACCESS_TOKEN_URL;
oAuth.AuthorizeUrl = FLICKR_AUTHORIZE_URL;
oAuth.RequestTokenUrl = FLICKR_REQUEST_TOKEN_URL;
oAuth.LoginTitle = "Flickr authorization";
oAuth.Token = config.FlickrToken;
oAuth.TokenSecret = config.FlickrTokenSecret;
var oAuth = new OAuthSession(FlickrCredentials.ConsumerKey, FlickrCredentials.ConsumerSecret)
{
BrowserSize = new Size(520, 800),
CheckVerifier = false,
AccessTokenUrl = FLICKR_ACCESS_TOKEN_URL,
AuthorizeUrl = FLICKR_AUTHORIZE_URL,
RequestTokenUrl = FLICKR_REQUEST_TOKEN_URL,
LoginTitle = "Flickr authorization",
Token = config.FlickrToken,
TokenSecret = config.FlickrTokenSecret
};
if (string.IsNullOrEmpty(oAuth.Token)) {
if (!oAuth.Authorize()) {
return null;
@ -85,7 +87,7 @@ namespace GreenshotFlickrPlugin {
signedParameters.Add("is_public", config.IsPublic ? "1" : "0");
signedParameters.Add("is_friend", config.IsFriend ? "1" : "0");
signedParameters.Add("is_family", config.IsFamily ? "1" : "0");
signedParameters.Add("safety_level", string.Format("{0}", (int)config.SafetyLevel));
signedParameters.Add("safety_level", $"{(int) config.SafetyLevel}");
signedParameters.Add("hidden", config.HiddenFromSearch ? "1" : "2");
IDictionary<string, object> otherParameters = new Dictionary<string, object>();
otherParameters.Add("photo", new SurfaceContainer(surfaceToUpload, outputSettings, filename));
@ -125,8 +127,7 @@ namespace GreenshotFlickrPlugin {
XmlNodeList nodes = doc.GetElementsByTagName("photo");
if (nodes.Count > 0) {
var item = nodes.Item(0);
if (item != null) {
if (item.Attributes != null) {
if (item?.Attributes != null) {
string farmId = item.Attributes["farm"].Value;
string serverId = item.Attributes["server"].Value;
string photoId = item.Attributes["id"].Value;
@ -134,8 +135,6 @@ namespace GreenshotFlickrPlugin {
return string.Format(FLICKR_FARM_URL, farmId, serverId, photoId, secret);
}
}
}
}
} catch (Exception ex) {
LOG.Error("Error parsing Flickr Response.", ex);

View file

@ -26,9 +26,7 @@ namespace GreenshotFlickrPlugin {
/// Description of PasswordRequestForm.
/// </summary>
public partial class SettingsForm : FlickrForm {
private string flickrFrob = string.Empty;
public SettingsForm(FlickrConfiguration config) {
public SettingsForm() {
//
// The InitializeComponent() call is required for Windows Forms designer support.
//

View file

@ -26,7 +26,5 @@ namespace GreenshotImgurPlugin {
/// This class is needed for design-time resolving of the language files
/// </summary>
public class ImgurForm : GreenshotForm {
public ImgurForm() : base() {
}
}
}

View file

@ -81,8 +81,10 @@ namespace GreenshotImgurPlugin {
listview_imgur_uploads.Columns.Add(column);
}
foreach (ImgurInfo imgurInfo in Config.runtimeImgurHistory.Values) {
ListViewItem item = new ListViewItem(imgurInfo.Hash);
item.Tag = imgurInfo;
var item = new ListViewItem(imgurInfo.Hash)
{
Tag = imgurInfo
};
item.SubItems.Add(imgurInfo.Title);
item.SubItems.Add(imgurInfo.DeleteHash);
item.SubItems.Add(imgurInfo.Timestamp.ToString("yyyy-MM-dd HH:mm:ss", DateTimeFormatInfo.InvariantInfo));
@ -101,7 +103,7 @@ namespace GreenshotImgurPlugin {
private void Listview_imgur_uploadsSelectedIndexChanged(object sender, EventArgs e) {
pictureBox1.Image = pictureBox1.ErrorImage;
if (listview_imgur_uploads.SelectedItems != null && listview_imgur_uploads.SelectedItems.Count > 0) {
if (listview_imgur_uploads.SelectedItems.Count > 0) {
deleteButton.Enabled = true;
openButton.Enabled = true;
clipboardButton.Enabled = true;
@ -118,16 +120,19 @@ namespace GreenshotImgurPlugin {
}
private void DeleteButtonClick(object sender, EventArgs e) {
if (listview_imgur_uploads.SelectedItems != null && listview_imgur_uploads.SelectedItems.Count > 0) {
if (listview_imgur_uploads.SelectedItems.Count > 0) {
for (int i = 0; i < listview_imgur_uploads.SelectedItems.Count; i++) {
ImgurInfo imgurInfo = (ImgurInfo)listview_imgur_uploads.SelectedItems[i].Tag;
DialogResult result = MessageBox.Show(Language.GetFormattedString("imgur", LangKey.delete_question, imgurInfo.Title), Language.GetFormattedString("imgur", LangKey.delete_title, imgurInfo.Hash), MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes) {
if (result != DialogResult.Yes)
{
continue;
}
// Should fix Bug #3378699
pictureBox1.Image = pictureBox1.ErrorImage;
try {
new PleaseWaitForm().ShowAndWait(ImgurPlugin.Attributes.Name, Language.GetString("imgur", LangKey.communication_wait),
delegate() {
delegate {
ImgurUtils.DeleteImgurImage(imgurInfo);
}
);
@ -138,20 +143,16 @@ namespace GreenshotImgurPlugin {
imgurInfo.Dispose();
}
}
}
Redraw();
}
private void ClipboardButtonClick(object sender, EventArgs e) {
StringBuilder links = new StringBuilder();
if (listview_imgur_uploads.SelectedItems != null && listview_imgur_uploads.SelectedItems.Count > 0) {
for (int i = 0; i < listview_imgur_uploads.SelectedItems.Count; i++) {
if (listview_imgur_uploads.SelectedItems.Count > 0) {
for (int i = 0; i < listview_imgur_uploads.SelectedItems.Count; i++)
{
ImgurInfo imgurInfo = (ImgurInfo)listview_imgur_uploads.SelectedItems[i].Tag;
if (Config.UsePageLink) {
links.AppendLine(imgurInfo.Page);
} else {
links.AppendLine(imgurInfo.Original);
}
links.AppendLine(Config.UsePageLink ? imgurInfo.Page : imgurInfo.Original);
}
}
ClipboardHelper.SetClipboardData(links.ToString());
@ -173,7 +174,7 @@ namespace GreenshotImgurPlugin {
}
private void OpenButtonClick(object sender, EventArgs e) {
if (listview_imgur_uploads.SelectedItems != null && listview_imgur_uploads.SelectedItems.Count > 0) {
if (listview_imgur_uploads.SelectedItems.Count > 0) {
for (int i = 0; i < listview_imgur_uploads.SelectedItems.Count; i++) {
ImgurInfo imgurInfo = (ImgurInfo)listview_imgur_uploads.SelectedItems[i].Tag;
System.Diagnostics.Process.Start(imgurInfo.Page);
@ -185,11 +186,7 @@ namespace GreenshotImgurPlugin {
// Determine if clicked column is already the column that is being sorted.
if (e.Column == _columnSorter.SortColumn) {
// Reverse the current sort direction for this column.
if (_columnSorter.Order == SortOrder.Ascending) {
_columnSorter.Order = SortOrder.Descending;
} else {
_columnSorter.Order = SortOrder.Ascending;
}
_columnSorter.Order = _columnSorter.Order == SortOrder.Ascending ? SortOrder.Descending : SortOrder.Ascending;
} else {
// Set the column number that is to be sorted; default to ascending.
_columnSorter.SortColumn = e.Column;

View file

@ -25,7 +25,8 @@ namespace GreenshotImgurPlugin {
/// Description of PasswordRequestForm.
/// </summary>
public partial class SettingsForm : ImgurForm {
public SettingsForm(ImgurConfiguration config) : base() {
public SettingsForm(ImgurConfiguration config)
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
@ -35,11 +36,7 @@ namespace GreenshotImgurPlugin {
ImgurUtils.LoadHistory();
if (config.runtimeImgurHistory.Count > 0) {
historyButton.Enabled = true;
} else {
historyButton.Enabled = false;
}
historyButton.Enabled = config.runtimeImgurHistory.Count > 0;
}
private void ButtonHistoryClick(object sender, EventArgs e) {

View file

@ -98,7 +98,7 @@ namespace GreenshotImgurPlugin {
SettingsForm settingsForm = null;
new PleaseWaitForm().ShowAndWait(ImgurPlugin.Attributes.Name, Language.GetString("imgur", LangKey.communication_wait),
delegate() {
delegate {
settingsForm = new SettingsForm(this);
}
);

View file

@ -20,7 +20,6 @@
*/
using System.ComponentModel;
using System.Drawing;
using Greenshot.IniFile;
using Greenshot.Plugin;
using GreenshotPlugin.Core;
@ -29,25 +28,15 @@ namespace GreenshotImgurPlugin {
/// Description of ImgurDestination.
/// </summary>
public class ImgurDestination : AbstractDestination {
private static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(ImgurDestination));
private static ImgurConfiguration config = IniConfig.GetIniSection<ImgurConfiguration>();
private readonly ImgurPlugin plugin = null;
private readonly ImgurPlugin _plugin;
public ImgurDestination(ImgurPlugin plugin) {
this.plugin = plugin;
_plugin = plugin;
}
public override string Designation {
get {
return "Imgur";
}
}
public override string Designation => "Imgur";
public override string Description {
get {
return Language.GetString("imgur", LangKey.upload_menu_item);
}
}
public override string Description => Language.GetString("imgur", LangKey.upload_menu_item);
public override Image DisplayIcon {
get {
@ -59,7 +48,7 @@ namespace GreenshotImgurPlugin {
public override ExportInformation ExportCapture(bool manuallyInitiated, ISurface surface, ICaptureDetails captureDetails) {
ExportInformation exportInformation = new ExportInformation(Designation, Description);
string uploadUrl;
exportInformation.ExportMade = plugin.Upload(captureDetails, surface, out uploadUrl);
exportInformation.ExportMade = _plugin.Upload(captureDetails, surface, out uploadUrl);
exportInformation.Uri = uploadUrl;
ProcessExport(exportInformation, surface);
return exportInformation;

View file

@ -29,7 +29,7 @@ namespace GreenshotImgurPlugin
/// </summary>
public class ImgurInfo : IDisposable
{
private static readonly log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(ImgurInfo));
private static readonly log4net.ILog Log = log4net.LogManager.GetLogger(typeof(ImgurInfo));
public string Hash
{
@ -37,13 +37,13 @@ namespace GreenshotImgurPlugin
set;
}
private string deleteHash;
private string _deleteHash;
public string DeleteHash
{
get { return deleteHash; }
get { return _deleteHash; }
set
{
deleteHash = value;
_deleteHash = value;
DeletePage = "https://imgur.com/delete/" + value;
}
}
@ -96,22 +96,15 @@ namespace GreenshotImgurPlugin
set;
}
private Image image;
private Image _image;
public Image Image
{
get { return image; }
get { return _image; }
set
{
if (image != null)
{
image.Dispose();
_image?.Dispose();
_image = value;
}
image = value;
}
}
public ImgurInfo()
{
}
/// <summary>
@ -133,16 +126,13 @@ namespace GreenshotImgurPlugin
{
if (disposing)
{
if (image != null)
{
image.Dispose();
_image?.Dispose();
}
}
image = null;
_image = null;
}
public static ImgurInfo ParseResponse(string response)
{
LOG.Debug(response);
Log.Debug(response);
// This is actually a hack for BUG-1695
// The problem is the (C) sign, we send it HTML encoded "&reg;" to Imgur and get it HTML encoded in the XML back
// Added all the encodings I found quickly, I guess these are not all... but it should fix the issue for now.
@ -161,34 +151,34 @@ namespace GreenshotImgurPlugin
XmlNodeList nodes = doc.GetElementsByTagName("id");
if (nodes.Count > 0)
{
imgurInfo.Hash = nodes.Item(0).InnerText;
imgurInfo.Hash = nodes.Item(0)?.InnerText;
}
nodes = doc.GetElementsByTagName("hash");
if (nodes.Count > 0)
{
imgurInfo.Hash = nodes.Item(0).InnerText;
imgurInfo.Hash = nodes.Item(0)?.InnerText;
}
nodes = doc.GetElementsByTagName("deletehash");
if (nodes.Count > 0)
{
imgurInfo.DeleteHash = nodes.Item(0).InnerText;
imgurInfo.DeleteHash = nodes.Item(0)?.InnerText;
}
nodes = doc.GetElementsByTagName("type");
if (nodes.Count > 0)
{
imgurInfo.ImageType = nodes.Item(0).InnerText;
imgurInfo.ImageType = nodes.Item(0)?.InnerText;
}
nodes = doc.GetElementsByTagName("title");
if (nodes.Count > 0)
{
imgurInfo.Title = nodes.Item(0).InnerText;
imgurInfo.Title = nodes.Item(0)?.InnerText;
}
nodes = doc.GetElementsByTagName("datetime");
if (nodes.Count > 0)
{
// Version 3 has seconds since Epoch
double secondsSince;
if (double.TryParse(nodes.Item(0).InnerText, out secondsSince))
if (double.TryParse(nodes.Item(0)?.InnerText, out secondsSince))
{
var epoch = new DateTimeOffset(1970, 1, 1, 0, 0, 0, TimeSpan.Zero);
imgurInfo.Timestamp = epoch.AddSeconds(secondsSince).DateTime;
@ -197,37 +187,30 @@ namespace GreenshotImgurPlugin
nodes = doc.GetElementsByTagName("original");
if (nodes.Count > 0)
{
imgurInfo.Original = nodes.Item(0).InnerText.Replace("http:", "https:");
imgurInfo.Original = nodes.Item(0)?.InnerText.Replace("http:", "https:");
}
// Version 3 API only has Link
nodes = doc.GetElementsByTagName("link");
if (nodes.Count > 0)
{
imgurInfo.Original = nodes.Item(0).InnerText.Replace("http:", "https:");
imgurInfo.Original = nodes.Item(0)?.InnerText.Replace("http:", "https:");
}
nodes = doc.GetElementsByTagName("imgur_page");
if (nodes.Count > 0)
{
imgurInfo.Page = nodes.Item(0).InnerText.Replace("http:", "https:");
imgurInfo.Page = nodes.Item(0)?.InnerText.Replace("http:", "https:");
}
else
{
// Version 3 doesn't have a page link in the response
imgurInfo.Page = string.Format("https://imgur.com/{0}", imgurInfo.Hash);
imgurInfo.Page = $"https://imgur.com/{imgurInfo.Hash}";
}
nodes = doc.GetElementsByTagName("small_square");
if (nodes.Count > 0)
{
imgurInfo.SmallSquare = nodes.Item(0).InnerText;
}
else
{
imgurInfo.SmallSquare = string.Format("http://i.imgur.com/{0}s.png",imgurInfo.Hash);
}
imgurInfo.SmallSquare = nodes.Count > 0 ? nodes.Item(0)?.InnerText : $"http://i.imgur.com/{imgurInfo.Hash}s.png";
}
catch (Exception e)
{
LOG.ErrorFormat("Could not parse Imgur response due to error {0}, response was: {1}", e.Message, response);
Log.ErrorFormat("Could not parse Imgur response due to error {0}, response was: {1}", e.Message, response);
}
return imgurInfo;
}

View file

@ -61,9 +61,6 @@ namespace GreenshotImgurPlugin {
}
}
public ImgurPlugin() {
}
public IEnumerable<IDestination> Destinations() {
yield return new ImgurDestination(this);
}

View file

@ -65,9 +65,9 @@ namespace GreenshotJiraPlugin {
}
}
public override bool isActive => base.isActive && !string.IsNullOrEmpty(Config.Url);
public override bool IsActive => base.IsActive && !string.IsNullOrEmpty(Config.Url);
public override bool isDynamic => true;
public override bool IsDynamic => true;
public override Image DisplayIcon {
get

View file

@ -23,13 +23,10 @@ using System.Windows.Forms;
using Greenshot.IniFile;
using Greenshot.Plugin;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Threading.Tasks;
using Dapplo.Log.Facade;
using GreenshotJiraPlugin.Forms;
using GreenshotPlugin.Core;
using Svg;
namespace GreenshotJiraPlugin {
/// <summary>

View file

@ -316,13 +316,9 @@ namespace Greenshot.Interop {
/// </returns>
public override IMessage Invoke(IMessage myMessage) {
IMethodCallMessage callMessage = myMessage as IMethodCallMessage;
if (null == callMessage) {
//LOG.DebugFormat("Message type not implemented: {0}", myMessage.GetType().ToString());
return null;
}
MethodInfo method = callMessage.MethodBase as MethodInfo;
if (null == method) {
MethodInfo method = callMessage?.MethodBase as MethodInfo;
if (method == null) {
//LOG.DebugFormat("Unrecognized Invoke call: {0}", callMessage.MethodBase.ToString());
return null;
}
@ -336,18 +332,8 @@ namespace Greenshot.Interop {
BindingFlags flags = BindingFlags.InvokeMethod;
int argCount = callMessage.ArgCount;
object invokeObject;
Type invokeType;
Type byValType;
object[] args;
object arg;
COMWrapper[] originalArgs;
COMWrapper wrapper;
ParameterModifier[] argModifiers = null;
ParameterInfo[] parameters = null;
ParameterInfo parameter;
if ("Dispose" == methodName && 0 == argCount && typeof(void) == returnType) {
Dispose();
@ -365,9 +351,11 @@ namespace Greenshot.Interop {
return new ReturnMessage(new ArgumentNullException(nameof(handler)), callMessage);
}
} else {
invokeObject = _comObject;
invokeType = _comType;
var invokeObject = _comObject;
var invokeType = _comType;
ParameterInfo parameter;
object[] args;
if (methodName.StartsWith("get_")) {
// Property Get
methodName = methodName.Substring(4);
@ -401,6 +389,9 @@ namespace Greenshot.Interop {
}
// Un-wrap wrapped COM objects before passing to the method
COMWrapper[] originalArgs;
COMWrapper wrapper;
Type byValType;
if (null == args || 0 == args.Length) {
originalArgs = null;
} else {
@ -412,7 +403,7 @@ namespace Greenshot.Interop {
originalArgs[i] = wrapper;
args[i] = wrapper._comObject;
}
} else if (0 != outArgsCount && argModifiers[0][i]) {
} else if (argModifiers != null && (0 != outArgsCount && argModifiers[0][i])) {
byValType = GetByValType(parameters[i].ParameterType);
if (byValType.IsInterface) {
// If we're passing a COM object by reference, and
@ -453,13 +444,15 @@ namespace Greenshot.Interop {
// Handle out args
if (0 != outArgsCount) {
if (args != null && parameters != null)
{
outArgs = new object[args.Length];
for (int i = 0; i < parameters.Length; i++) {
if (!argModifiers[0][i]) {
if (argModifiers != null && !argModifiers[0][i]) {
continue;
}
arg = args[i];
var arg = args[i];
if (null == arg) {
continue;
}
@ -476,7 +469,10 @@ namespace Greenshot.Interop {
arg = Enum.Parse(byValType, arg.ToString());
} else if (byValType.IsInterface) {
if (Marshal.IsComObject(arg)) {
if (originalArgs != null)
{
wrapper = originalArgs[i];
}
if (null != wrapper && wrapper._comObject != arg) {
wrapper.Dispose();
wrapper = null;
@ -492,6 +488,7 @@ namespace Greenshot.Interop {
}
}
}
}
return new ReturnMessage(returnValue, outArgs, outArgsCount, callMessage.LogicalCallContext, callMessage);
}

View file

@ -24,10 +24,8 @@ namespace Greenshot.Interop {
/// <summary>
/// An attribute to specifiy the ProgID of the COM class to create. (As suggested by Kristen Wegner)
/// </summary>
[AttributeUsage(AttributeTargets.Interface, Inherited = false, AllowMultiple = false)]
[AttributeUsage(AttributeTargets.Interface)]
public sealed class ComProgIdAttribute : Attribute {
private readonly string _value;
/// <summary>
/// Extracts the attribute from the specified type.
/// </summary>
@ -42,24 +40,24 @@ namespace Greenshot.Interop {
/// </exception>
public static ComProgIdAttribute GetAttribute(Type interfaceType) {
if (null == interfaceType) {
throw new ArgumentNullException("interfaceType");
throw new ArgumentNullException(nameof(interfaceType));
}
Type attributeType = typeof(ComProgIdAttribute);
object[] attributes = interfaceType.GetCustomAttributes(attributeType, false);
if (null == attributes || 0 == attributes.Length) {
if (0 == attributes.Length) {
Type[] interfaces = interfaceType.GetInterfaces();
for (int i = 0; i < interfaces.Length; i++) {
interfaceType = interfaces[i];
attributes = interfaceType.GetCustomAttributes(attributeType, false);
if (null != attributes && 0 != attributes.Length) {
if (0 != attributes.Length) {
break;
}
}
}
if (null == attributes || 0 == attributes.Length) {
if (0 == attributes.Length) {
return null;
}
return (ComProgIdAttribute)attributes[0];
@ -68,16 +66,12 @@ namespace Greenshot.Interop {
/// <summary>Constructor</summary>
/// <param name="value">The COM ProgID.</param>
public ComProgIdAttribute(string value) {
_value = value;
Value = value;
}
/// <summary>
/// Returns the COM ProgID
/// </summary>
public string Value {
get {
return _value;
}
}
public string Value { get; }
}
}

Some files were not shown because too many files have changed in this diff Show more