-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnumEditor.cs
More file actions
133 lines (120 loc) · 4.02 KB
/
EnumEditor.cs
File metadata and controls
133 lines (120 loc) · 4.02 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace LSRVersionManager
{
public partial class EnumEditor : CheckedListBoxDisabledItems
{
public EnumEditor()
{
InitializeComponent();
ItemCheck += EnumEditor_ItemCheck;
}
private object _value;
public object Value
{
get
{
return _value;
}
set
{
if (value == null)
throw new ArgumentNullException("Value");
if (!value.GetType().IsEnum)
throw new ArgumentNullException("Value should be enumerable.");
if (value.GetType().GetCustomAttributes(typeof(FlagsAttribute), false).Length == 0)
throw new ArgumentNullException("Value must be marked with [Flags].");
_value = value;
Rebuild();
}
}
public event EventHandler<EnumValueChangeEventArgs> ValueChanged;
protected virtual void OnValueChanged(object value)
{
ValueChanged?.Invoke(this, new EnumValueChangeEventArgs { Value = value });
}
private void Rebuild()
{
Items.Clear();
if (_value != null)
{
foreach (object value in Enum.GetValues(_value.GetType()))
{
if ((int)value == 0) continue;
Items.Add(value, ((int)_value & (int)value) != 0);
}
}
}
void EnumEditor_ItemCheck(object sender, ItemCheckEventArgs e)
{
ulong bits = Convert.ToUInt64(_value);
ulong bit = Convert.ToUInt64(Items[e.Index]);
if (e.NewValue == CheckState.Checked)
bits = bits | bit;
else
bits = bits & ~bit;
_value = Enum.ToObject(_value.GetType(), bits);
OnValueChanged(_value);
}
}
public class EnumValueChangeEventArgs : EventArgs
{
public object Value
{
get;
set;
}
}
public class CheckedListBoxDisabledItems : CheckedListBox
{
private List<string> _disabledItems = new List<string>();
private List<int> _disabledIndexes = new List<int>();
public void Disable(string item)
{
_disabledItems.Add(item);
this.Refresh();
}
public void Disable(int index)
{
_disabledIndexes.Add(index);
this.Refresh();
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
string s = Items[e.Index].ToString();
if (_disabledItems.Contains(s) || _disabledIndexes.Contains(e.Index))
{
System.Windows.Forms.VisualStyles.CheckBoxState state = System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedDisabled;
Size glyphSize = CheckBoxRenderer.GetGlyphSize(e.Graphics, state);
CheckBoxRenderer.DrawCheckBox(
e.Graphics,
new Point(e.Bounds.X + 1, e.Bounds.Y + 1), // add one pixel to align the check gliph properly
new Rectangle(
new Point(e.Bounds.X + glyphSize.Width + 3, e.Bounds.Y), // add three pixels to align text properly
new Size(e.Bounds.Width - glyphSize.Width, e.Bounds.Height)
),
s,
this.Font,
TextFormatFlags.Left, // text is centered by default
false,
state
);
}
else
{
base.OnDrawItem(e);
}
}
public void ClearDisabledItems()
{
_disabledIndexes.Clear();
_disabledItems.Clear();
this.Refresh();
}
}
}