Added Greenshot controls which allow to map directly to the ini and language files. This makes settings forms a lot easier to implement and maintain

git-svn-id: http://svn.code.sf.net/p/greenshot/code/trunk@1766 7dccd23d-a4a3-4e1f-8c07-b4c1b4018ab4
This commit is contained in:
RKrom 2012-04-10 16:03:04 +00:00
commit a4df32c0e8
13 changed files with 787 additions and 258 deletions

View file

@ -0,0 +1,33 @@
/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2012 Thomas Braun, Jens Klingen, Robin Krom
*
* For more information see: http://getgreenshot.org/
* The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace GreenshotPlugin.Controls {
public class GreenshotButton : Button, IGreenshotLanguageBindable {
[Category("Greenshot"), Description("Specifies key of the language file to use when displaying the text.")]
public string LanguageKey {
get;
set;
}
}
}

View file

@ -0,0 +1,53 @@
/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2012 Thomas Braun, Jens Klingen, Robin Krom
*
* For more information see: http://getgreenshot.org/
* The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace GreenshotPlugin.Controls {
/// <summary>
/// Description of GreenshotCheckbox.
/// </summary>
public class GreenshotCheckBox : CheckBox, IGreenshotLanguageBindable, IGreenshotConfigBindable {
[Category("Greenshot"), Description("Specifies key of the language file to use when displaying the text.")]
public string LanguageKey {
get;
set;
}
private string sectionName = "Core";
[Category("Greenshot"), DefaultValue("Core"), Description("Specifies the Ini-Section to map this control with.")]
public string SectionName {
get {
return sectionName;
}
set {
sectionName = value;
}
}
[Category("Greenshot"), Description("Specifies the property name to map the configuration.")]
public string PropertyName {
get;
set;
}
}
}

View file

@ -0,0 +1,140 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using GreenshotPlugin.Core;
using Greenshot.IniFile;
using System.ComponentModel;
namespace GreenshotPlugin.Controls {
public class GreenshotForm : Form , IGreenshotLanguageBindable {
private static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(GreenshotForm));
protected ILanguage language;
[Category("Greenshot"), Description("Specifies key of the language file to use when displaying the text.")]
public string LanguageKey {
get;
set;
}
public GreenshotForm() : base() {
}
protected override void OnLoad(EventArgs e) {
if (!this.DesignMode) {
ApplyLanguage();
FillFields();
}
base.OnLoad(e);
}
/// <summary>
/// check if the form was closed with an OK, if so store the values in the GreenshotControls
/// </summary>
/// <param name="e"></param>
protected override void OnClosed(EventArgs e) {
if (DialogResult == DialogResult.OK) {
LOG.Info("Form was closed with OK: storing field values.");
StoreFields();
}
base.OnClosed(e);
}
/// <summary>
/// Apply all the language settings to the "Greenshot" Controls on this form
/// </summary>
protected void ApplyLanguage() {
if (language == null) {
throw new ArgumentNullException("Language not set!!");
}
// Set title of the form
if (!string.IsNullOrEmpty(LanguageKey)) {
this.Text = language.GetString(LanguageKey);
}
foreach (FieldInfo field in this.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) {
if (!field.FieldType.IsSubclassOf(typeof(Control))) {
continue;
}
Object controlObject = field.GetValue(this);
if (typeof(IGreenshotLanguageBindable).IsAssignableFrom(field.FieldType)) {
IGreenshotLanguageBindable languageBindable = controlObject as IGreenshotLanguageBindable;
if (!string.IsNullOrEmpty(languageBindable.LanguageKey)) {
Control control = controlObject as Control;
control.Text = language.GetString(languageBindable.LanguageKey);
} else {
LOG.WarnFormat("Greenshot control without language key: {0}", field.Name);
}
}
}
}
/// <summary>
/// Fill all GreenshotControls with the values from the configuration
/// </summary>
protected void FillFields() {
foreach (FieldInfo field in this.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) {
if (!field.FieldType.IsSubclassOf(typeof(Control))) {
continue;
}
Object controlObject = field.GetValue(this);
if (typeof(IGreenshotConfigBindable).IsAssignableFrom(field.FieldType)) {
IGreenshotConfigBindable configBindable = controlObject as IGreenshotConfigBindable;
if (!string.IsNullOrEmpty(configBindable.SectionName) && !string.IsNullOrEmpty(configBindable.PropertyName)) {
IniSection section = IniConfig.GetIniSection(configBindable.SectionName);
if (section != null) {
if (typeof(CheckBox).IsAssignableFrom(field.FieldType)) {
CheckBox checkBox = controlObject as CheckBox;
checkBox.Checked = (bool)section.Values[configBindable.PropertyName].Value;
} else if (typeof(TextBox).IsAssignableFrom(field.FieldType)) {
TextBox textBox = controlObject as TextBox;
textBox.Text = (string)section.Values[configBindable.PropertyName].Value;
}
}
}
}
}
}
/// <summary>
/// Store all GreenshotControl values to the configuration
/// </summary>
protected void StoreFields() {
foreach (FieldInfo field in this.GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) {
if (!field.FieldType.IsSubclassOf(typeof(Control))) {
continue;
}
if (!typeof(IGreenshotConfigBindable).IsAssignableFrom(field.FieldType)) {
continue;
}
Object controlObject = field.GetValue(this);
IGreenshotConfigBindable configBindable = controlObject as IGreenshotConfigBindable;
bool iniDirty = false;
if (!string.IsNullOrEmpty(configBindable.SectionName) && !string.IsNullOrEmpty(configBindable.PropertyName)) {
IniSection section = IniConfig.GetIniSection(configBindable.SectionName);
if (section != null) {
if (typeof(CheckBox).IsAssignableFrom(field.FieldType)) {
CheckBox checkBox = controlObject as CheckBox;
section.Values[configBindable.PropertyName].Value = checkBox.Checked;
iniDirty = true;
} else if (typeof(HotkeyControl).IsAssignableFrom(field.FieldType)) {
HotkeyControl hotkeyControl = controlObject as HotkeyControl;
section.Values[configBindable.PropertyName].Value = hotkeyControl.ToString();
iniDirty = true;
} else if (typeof(TextBox).IsAssignableFrom(field.FieldType)) {
TextBox textBox = controlObject as TextBox;
section.Values[configBindable.PropertyName].Value = textBox.Text;
iniDirty = true;
}
}
}
if (iniDirty) {
IniConfig.Save();
}
}
}
}
}

View file

@ -0,0 +1,33 @@
/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2012 Thomas Braun, Jens Klingen, Robin Krom
*
* For more information see: http://getgreenshot.org/
* The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Windows.Forms;
using System.ComponentModel;
namespace GreenshotPlugin.Controls {
public class GreenshotGroupBox : GroupBox , IGreenshotLanguageBindable {
[Category("Greenshot"), Description("Specifies key of the language file to use when displaying the text.")]
public string LanguageKey {
get;
set;
}
}
}

View file

@ -0,0 +1,33 @@
/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2012 Thomas Braun, Jens Klingen, Robin Krom
*
* For more information see: http://getgreenshot.org/
* The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Windows.Forms;
using System.ComponentModel;
namespace GreenshotPlugin.Controls {
public class GreenshotLabel : Label, IGreenshotLanguageBindable {
[Category("Greenshot"), Description("Specifies key of the language file to use when displaying the text.")]
public string LanguageKey {
get;
set;
}
}
}

View file

@ -0,0 +1,33 @@
/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2012 Thomas Braun, Jens Klingen, Robin Krom
*
* For more information see: http://getgreenshot.org/
* The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.Windows.Forms;
using System.ComponentModel;
namespace GreenshotPlugin.Controls {
public class GreenshotTabPage : TabPage, IGreenshotLanguageBindable {
[Category("Greenshot"), Description("Specifies key of the language file to use when displaying the text.")]
public string LanguageKey {
get;
set;
}
}
}

View file

@ -0,0 +1,44 @@
/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2012 Thomas Braun, Jens Klingen, Robin Krom
*
* For more information see: http://getgreenshot.org/
* The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace GreenshotPlugin.Controls {
public class GreenshotTextBox : TextBox, IGreenshotConfigBindable {
private string sectionName = "Core";
[Category("Greenshot"), DefaultValue("Core"), Description("Specifies the Ini-Section to map this control with.")]
public string SectionName {
get {
return sectionName;
}
set {
sectionName = value;
}
}
[Category("Greenshot"), Description("Specifies the property name to map the configuration.")]
public string PropertyName {
get;
set;
}
}
}

View file

@ -33,7 +33,7 @@ namespace GreenshotPlugin.Controls {
/// See: http://www.codeproject.com/KB/buttons/hotkeycontrol.aspx
/// But is modified to fit in Greenshot, and have localized support
/// </summary>
public class HotkeyControl : TextBox {
public class HotkeyControl : GreenshotTextBox {
private static log4net.ILog LOG = log4net.LogManager.GetLogger(typeof(HotkeyControl));
// Holds the list of hotkeys

View file

@ -0,0 +1,41 @@
/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2012 Thomas Braun, Jens Klingen, Robin Krom
*
* For more information see: http://getgreenshot.org/
* The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
namespace GreenshotPlugin.Controls {
public interface IGreenshotConfigBindable {
/// <summary>
/// The class where the property-value is stored
/// </summary>
string SectionName {
get;
set;
}
/// <summary>
/// Path to the property value which will be mapped with this control
/// </summary>
string PropertyName {
get;
set;
}
}
}

View file

@ -0,0 +1,37 @@
/*
* Greenshot - a free and open source screenshot tool
* Copyright (C) 2007-2012 Thomas Braun, Jens Klingen, Robin Krom
*
* For more information see: http://getgreenshot.org/
* The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
using System;
namespace GreenshotPlugin.Controls {
/// <summary>
/// This interface describes the designer fields that need to be implemented for Greenshot controls
/// </summary>
public interface IGreenshotLanguageBindable {
/// <summary>
/// Language key to use to fill the Text value with
/// </summary>
string LanguageKey {
get;
set;
}
}
}