forked from fdorg/flashdevelop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmartForm.cs
More file actions
174 lines (159 loc) · 5.33 KB
/
SmartForm.cs
File metadata and controls
174 lines (159 loc) · 5.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
using System;
using System.IO;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;
using System.Collections.Generic;
using PluginCore.Managers;
using PluginCore.Utilities;
using PluginCore.Helpers;
namespace PluginCore.Controls
{
public class SmartForm : Form
{
private String formGuid;
private String helpLink;
private FormProps formProps;
public event SavePropsHandler SaveProps;
public event ApplyPropsHandler ApplyProps;
public delegate void ApplyPropsHandler(SmartForm form);
public delegate void SavePropsHandler(SmartForm form);
public SmartForm()
{
this.formProps = new FormProps();
this.Load += new EventHandler(this.SmartFormLoad);
this.Shown += new EventHandler(this.SmartFormShown);
this.FormClosed += new FormClosedEventHandler(this.SmartFormClosed);
}
/// <summary>
/// Gets or sets the help link
/// </summary>
public String HelpLink
{
get { return this.helpLink; }
set { this.helpLink = value; }
}
/// <summary>
/// Gets or sets the form guid
/// </summary>
public String FormGuid
{
get { return this.formGuid; }
set { this.formGuid = value.ToUpper(); }
}
/// <summary>
/// Path to the unique setting file
/// </summary>
private String FormPropsFile
{
get { return Path.Combine(this.FormStatesDir, this.formGuid + ".fdb"); }
}
/// <summary>
/// Path to the form state file directory
/// </summary>
private String FormStatesDir
{
get
{
String settingDir = PathHelper.SettingDir;
String formStatesDir = Path.Combine(settingDir, "FormStates");
if (!Directory.Exists(formStatesDir)) Directory.CreateDirectory(formStatesDir);
return formStatesDir;
}
}
/// <summary>
/// Center the dialog to parent if requested
/// </summary>
private void SmartFormShown(Object sender, EventArgs e)
{
if (this.StartPosition == FormStartPosition.CenterParent)
{
this.CenterToParent();
}
}
/// <summary>
/// Load the form state from a setting file and applies it
/// </summary>
private void SmartFormLoad(Object sender, EventArgs e)
{
ScaleHelper.AdjustForHighDPI(this);
if (!String.IsNullOrEmpty(this.formGuid) && File.Exists(this.FormPropsFile))
{
Object obj = ObjectSerializer.Deserialize(this.FormPropsFile, this.formProps);
this.formProps = (FormProps)obj;
if (!this.formProps.WindowSize.IsEmpty)
{
this.Size = this.formProps.WindowSize;
}
}
if (!String.IsNullOrEmpty(this.helpLink))
{
this.HelpButton = true;
this.HelpButtonClicked += new System.ComponentModel.CancelEventHandler(this.SmartFormHelpButtonClick);
}
if (this.ApplyProps != null) this.ApplyProps(this);
}
/// <summary>
/// Saves the current form state to a setting file
/// </summary>
private void SmartFormClosed(Object sender, FormClosedEventArgs e)
{
if (this.SaveProps != null) this.SaveProps(this);
if (!String.IsNullOrEmpty(this.formGuid) && !this.Size.IsEmpty)
{
this.formProps.WindowSize = this.Size;
ObjectSerializer.Serialize(this.FormPropsFile, this.formProps);
}
}
/// <summary>
/// Browse to the specified help link
/// </summary>
private void SmartFormHelpButtonClick(Object sender, CancelEventArgs e)
{
PluginBase.MainForm.CallCommand("Browse", this.helpLink);
}
/// <summary>
/// Get custom property value
/// </summary>
public String GetPropValue(String key)
{
Argument argument;
for (var i = 0; i < this.formProps.ExtraProps.Count; i++)
{
argument = this.formProps.ExtraProps[i];
if (argument.Key == key) return argument.Value;
}
return null;
}
/// <summary>
/// Set custom property value
/// </summary>
public void SetPropValue(String key, String value)
{
Argument argument;
for (var i = 0; i < this.formProps.ExtraProps.Count; i++)
{
argument = this.formProps.ExtraProps[i];
if (argument.Key == key)
{
argument.Value = value;
return;
}
}
argument = new Argument(key, value);
this.formProps.ExtraProps.Add(argument);
}
}
[Serializable]
public class FormProps
{
public Size WindowSize;
public List<Argument> ExtraProps;
public FormProps()
{
this.WindowSize = Size.Empty;
this.ExtraProps = new List<Argument>();
}
}
}