mirror of
https://github.com/greenshot/greenshot
synced 2025-08-20 05:23:24 -07:00
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:
parent
0d0c0a5f45
commit
a4df32c0e8
13 changed files with 787 additions and 258 deletions
140
GreenshotPlugin/Controls/GreenshotForm.cs
Normal file
140
GreenshotPlugin/Controls/GreenshotForm.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue