-
Notifications
You must be signed in to change notification settings - Fork 3
/
SettingForm.cs
222 lines (189 loc) · 7.06 KB
/
SettingForm.cs
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;
namespace WordAddIn1
{
public partial class SettingForm : Form
{
string ControlKey = Ribbon1.ControlKey;
string SettingsFile = Ribbon1.SettingsFile;
//全局常量
const string KeyDefault = Ribbon1.KeyDefault;
const string KeyXMT = Ribbon1.KeyXMT;
const string KeyCode = Ribbon1.KeyCode;
const string KeyCode2 = Ribbon1.KeyCode2;
const string KeyCodeLatex = Ribbon1.KeyCodeLatex;
const string KeyCode4 = Ribbon1.KeyCode4;
const string KeyToolsBox = Ribbon1.KeyToolsBox;
const string KeyRunCode = Ribbon1.KeyRunCode;
const string KeyBrowser = Ribbon1.KeyBrowser;
const string KeyMathList = Ribbon1.KeyMathList;
const string KeyTestGroup = Ribbon1.KeyTestGroup;
const string KeyPdfTools = Ribbon1.KeyPdfTools;
const string KeyAdmin = Ribbon1.KeyAdmin;
string[] keyArray = new string[] { KeyXMT, KeyCode, KeyCode2, KeyCodeLatex, KeyCode4, KeyToolsBox, KeyRunCode, KeyBrowser, KeyMathList, KeyTestGroup, KeyPdfTools };
string[] nameArray = new string[] { "文案", "代码排版", "代码排版3", "代码排版2", "代码排版4", "工具箱", "运行代码", "简单浏览器", "公式编号", "实验性功能", "PDF 工具箱" };
public bool boolKeyAllTrue;
public SettingForm()
{
InitializeComponent();
loadList();
boolKeyAllTrue = false;
textBox1.KeyUp += new KeyEventHandler(textBox1_KeyUp);
JObject js_settings = ImportJSON(SettingsFile);
usePyScripts.Checked = (bool)js_settings["usePyScripts"];
toolTip1.SetToolTip(usePyScripts, "如勾选,则使用 FDscend/scripts 中的 Python 脚本;\r\n否则,使用 FDscend/scripts 中的同名exe文件");
}
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
//MessageBox.Show("回车");
button1_Click(sender, e);
}
}
void loadList()
{
JObject js = ImportJSON(ControlKey);
for (int i = 3; i < js.Count; i++)
{
if (js.Properties().ToList()[i].Name.ToString().StartsWith("key"))
{
//MessageBox.Show(js.Properties().ToList()[i].Value.ToString());
checkedListBox1.Items.Add(GetControlName(js.Properties().ToList()[i].Value.ToString()));
}
}
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
if (js["state_" + GetControlStateCode(checkedListBox1.Items[i].ToString())].ToString() == "1")
{
checkedListBox1.SetItemChecked(i, true);
}
}
}
public static JObject ImportJSON(string jsonfile)
{
StreamReader reader = File.OpenText(jsonfile);
JsonTextReader jsonTextReader = new JsonTextReader(reader);
JObject jsonObject = (JObject)JToken.ReadFrom(jsonTextReader);
reader.Close();
return jsonObject;
}
public static void SetjsonFun(string jsonfile, JObject jsonObject)
{
string output = Newtonsoft.Json.JsonConvert.SerializeObject(jsonObject, Newtonsoft.Json.Formatting.Indented);
File.WriteAllText(jsonfile, output);
}
string GetControlName(string input)
{
string output = "";
if(keyArray.Contains(input))
{
int index_1 = Array.IndexOf(keyArray, input);
output = nameArray[index_1];
}
else
{
MessageBox.Show("加载错误name", "操作码");
}
return output;
}
string GetControlStateCode(string input)
{
string output = "";
if (nameArray.Contains(input))
{
int index_1 = Array.IndexOf(nameArray, input);
output = keyArray[index_1];
}
else
{
MessageBox.Show("加载错误state", "操作码");
}
return output;
}
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
JObject js = ImportJSON(ControlKey);
if (e.NewValue == CheckState.Checked)
{
js["state_" + GetControlStateCode(checkedListBox1.Items[e.Index].ToString())] = "1";
}
else
{
js["state_" + GetControlStateCode(checkedListBox1.Items[e.Index].ToString())] = "0";
}
SetjsonFun(ControlKey, js);
}
private void button1_Click(object sender, EventArgs e)
{
string key = textBox1.Text;
if (key == KeyAdmin)
{
boolKeyAllTrue = true;
this.Close();
}
else if (BoolKey(key) == true)
{
JObject Jtemp = ImportJSON(ControlKey);
Jtemp["key_" + key] = key;
Jtemp["state_" + key] = "1";
SetjsonFun(ControlKey, Jtemp);
checkedListBox1.Items.Clear();
loadList();
textBox1.Text = "";
}
}
private bool BoolKey(string key)
{
switch (key)
{
case KeyDefault:
break;
case KeyXMT:
break;
case KeyCode:
break;
case KeyCode2:
break;
case KeyCodeLatex:
break;
case KeyCode4:
break;
case KeyToolsBox:
break;
case KeyRunCode:
break;
case KeyBrowser:
break;
case KeyMathList:
break;
case KeyTestGroup:
break;
case KeyPdfTools:
break;
default:
return false;
}
return true;
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void usePyScripts_CheckedChanged(object sender, EventArgs e)
{
JObject js = ImportJSON(SettingsFile);
js["usePyScripts"] = usePyScripts.Checked;
SetjsonFun(SettingsFile, js);
}
}
}