forked from PavelTorgashov/FastColoredTextBox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHotkeysEditorForm.cs
More file actions
179 lines (152 loc) · 5.36 KB
/
Copy pathHotkeysEditorForm.cs
File metadata and controls
179 lines (152 loc) · 5.36 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
175
176
177
178
179
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace FastColoredTextBoxNS
{
public partial class HotkeysEditorForm : Form
{
BindingList<HotkeyWrapper> wrappers = new BindingList<HotkeyWrapper>();
public HotkeysEditorForm(HotkeysMapping hotkeys)
{
InitializeComponent();
BuildWrappers(hotkeys);
dgv.DataSource = wrappers;
}
int CompereKeys(Keys key1, Keys key2)
{
var res = ((int)key1 & 0xff).CompareTo((int)key2 & 0xff);
if (res == 0)
res = key1.CompareTo(key2);
return res;
}
private void BuildWrappers(HotkeysMapping hotkeys)
{
var keys = new List<Keys>(hotkeys.Keys);
keys.Sort(CompereKeys);
wrappers.Clear();
foreach (var k in keys)
wrappers.Add(new HotkeyWrapper(k, hotkeys[k]));
}
/// <summary>
/// Returns edited hotkey map
/// </summary>
/// <returns></returns>
public HotkeysMapping GetHotkeys()
{
var result = new HotkeysMapping();
foreach (var w in wrappers)
result[w.ToKeyData()] = w.Action;
return result;
}
private void btAdd_Click(object sender, EventArgs e)
{
wrappers.Add(new HotkeyWrapper(Keys.None, FCTBAction.None));
}
private void dgv_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
var cell = (dgv[0, e.RowIndex] as DataGridViewComboBoxCell);
if(cell.Items.Count == 0)
foreach(var item in new string[]{"", "Ctrl", "Ctrl + Shift", "Ctrl + Alt", "Shift", "Shift + Alt", "Alt", "Ctrl + Shift + Alt"})
cell.Items.Add(item);
cell = (dgv[1, e.RowIndex] as DataGridViewComboBoxCell);
if (cell.Items.Count == 0)
foreach (var item in Enum.GetValues(typeof(Keys)))
cell.Items.Add(item);
cell = (dgv[2, e.RowIndex] as DataGridViewComboBoxCell);
if (cell.Items.Count == 0)
foreach (var item in Enum.GetValues(typeof(FCTBAction)))
cell.Items.Add(item);
}
private void btResore_Click(object sender, EventArgs e)
{
HotkeysMapping h = new HotkeysMapping();
h.InitDefault();
BuildWrappers(h);
}
private void btRemove_Click(object sender, EventArgs e)
{
for (int i = dgv.RowCount - 1; i >= 0; i--)
if (dgv.Rows[i].Selected) dgv.Rows.RemoveAt(i);
}
private void HotkeysEditorForm_FormClosing(object sender, FormClosingEventArgs e)
{
if(DialogResult == System.Windows.Forms.DialogResult.OK)
{
var actions = GetUnAssignedActions();
if (!string.IsNullOrEmpty(actions))
{
if (MessageBox.Show("Some actions are not assigned!\r\nActions: " + actions + "\r\nPress Yes to save and exit, press No to continue editing", "Some actions is not assigned", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.No)
e.Cancel = true;
}
}
}
private string GetUnAssignedActions()
{
StringBuilder sb = new StringBuilder();
var dic = new Dictionary<FCTBAction, FCTBAction>();
foreach (var w in wrappers)
dic[w.Action] = w.Action;
foreach (var item in Enum.GetValues(typeof(FCTBAction)))
if ((FCTBAction)item != FCTBAction.None)
if(!((FCTBAction)item).ToString().StartsWith("CustomAction"))
{
if(!dic.ContainsKey((FCTBAction)item))
sb.Append(item+", ");
}
return sb.ToString().TrimEnd(' ', ',');
}
}
internal class HotkeyWrapper
{
public HotkeyWrapper(Keys keyData, FCTBAction action)
{
KeyEventArgs a = new KeyEventArgs(keyData);
Ctrl = a.Control;
Shift = a.Shift;
Alt = a.Alt;
Key = a.KeyCode;
Action = action;
}
public Keys ToKeyData()
{
var res = Key;
if (Ctrl) res |= Keys.Control;
if (Alt) res |= Keys.Alt;
if (Shift) res |= Keys.Shift;
return res;
}
bool Ctrl;
bool Shift;
bool Alt;
public string Modifiers
{
get
{
var res = "";
if (Ctrl) res += "Ctrl + ";
if (Shift) res += "Shift + ";
if (Alt) res += "Alt + ";
return res.Trim(' ', '+');
}
set
{
if (value == null)
{
Ctrl = Alt = Shift = false;
}
else
{
Ctrl = value.Contains("Ctrl");
Shift = value.Contains("Shift");
Alt = value.Contains("Alt");
}
}
}
public Keys Key { get; set; }
public FCTBAction Action { get; set; }
}
}