-
Notifications
You must be signed in to change notification settings - Fork 1
/
FlagEditor.cs
291 lines (248 loc) · 6.74 KB
/
FlagEditor.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
* Creato da SharpDevelop.
* Utente: michele
* Data: 29/01/2009
* Ora: 8.41
*
* To change this template use Tools | Options | Coding | Edit Standard Headers.
*/
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms;
using System.Windows.Forms.Design;
using System.Drawing;
using System.ComponentModel.Design.Serialization;
using System.Reflection;
using wx;
namespace mkdb
{
public class wxFlagsItem
{
private string _name;
private uint _val;
private bool _state;
public wxFlagsItem(string name, uint val, bool state)
{
_name = name;
_val = val;
_state = state;
}
public string Name
{
get { return _name; }
}
public uint Value
{
get { return _val; }
}
public bool Checked
{
get { return (_state == true); }
set { _state = value; }
}
public override string ToString()
{
return _name;
}
}
public class wxFlags : ArrayList
{
private uint _long_flag;
public wxFlags() : base()
{
_long_flag = 0;
}
public int AddItem(string name, uint val, bool state)
{
if (state == true) _long_flag |= val;
return this.Add(new wxFlagsItem(name, val, state));
}
public uint ToLong
{
get { return _long_flag; }
set { _long_flag = value; }
}
public void Refresh()
{
_long_flag = 0;
foreach(wxFlagsItem item in this)
{
if (item.Checked)
{
_long_flag |= item.Value;
}
}
}
public override string ToString()
{
string str = "";
foreach (wxFlagsItem item in this)
{
if (item.Checked)
{
str += item.Name + "; ";
}
}
return str;
}
}
public class wxFlagsEditor : UITypeEditor
{
private IWindowsFormsEditorService edSvc = null;
private CheckedListBox clb;
/// <summary>
/// Overrides the method used to provide basic behaviour for selecting editor.
/// Shows our custom control for editing the value.
/// </summary>
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
if (context == null || context.Instance == null || provider == null)
return value;
edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
if (edSvc != null)
{
// Create a CheckedListBox and populate it with all the enum values
clb = new CheckedListBox();
clb.BorderStyle = BorderStyle.FixedSingle;
clb.CheckOnClick = true;
clb.MouseDown += new MouseEventHandler(this.OnMouseDown);
wxFlags ht = (wxFlags)value;
foreach (wxFlagsItem d in ht)
{
clb.Items.Add(new wxFlagsItem(d.Name, d.Value, d.Checked), d.Checked);
}
// Show our CheckedListbox as a DropDownControl.
// This methods returns only when the dropdowncontrol is closed
edSvc.DropDownControl(clb);
wxFlags retflags = new wxFlags();
int i = 0;
retflags.ToLong = 0;
foreach(wxFlagsItem item in clb.Items)
{
bool b;
if (clb.GetItemChecked(i))
{
b = true;
retflags.ToLong |= item.Value;
} else
{
b = false;
// retflags.ToLong = retflags.ToLong & (~item.Value);
}
retflags.AddItem(item.Name, item.Value, b);
i++;
}
return retflags;
}
return value;
}
/// <summary>
/// Shows a dropdown icon in the property editor
/// </summary>
/// <param name="context">The context of the editing control</param>
/// <returns>Returns <c>UITypeEditorEditStyle.DropDown</c></returns>
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.DropDown;
}
private bool handleLostfocus = false;
/// <summary>
/// When got the focus, handle the lost focus event.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnMouseDown(object sender, MouseEventArgs e)
{
if (!handleLostfocus && clb.ClientRectangle.Contains(clb.PointToClient(new Point(e.X, e.Y))))
{
clb.LostFocus += new EventHandler(this.ValueChanged);
handleLostfocus = true;
}
}
private void ValueChanged(object sender, EventArgs e)
{
if (edSvc != null)
{
edSvc.CloseDropDown();
}
}
}
public class wxFlagsTypeConverter : TypeConverter
{
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
if (sourceType == typeof(string))
return true;
else
return base.CanConvertFrom(context, sourceType);
}
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
if ((destinationType == typeof(string)) |
(destinationType == typeof(InstanceDescriptor)))
return true;
else
return base.CanConvertTo(context, destinationType);
}
public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
{
// no value so we return a new Longitude instance
if (value == null)
return new wxFlags();
// convert from a string
if (value is string)
{
// get strongly typed value
string StringValue = value as string;
// empty string so we return a new Longitude instance
if (StringValue.Length <= 0)
return new wxFlags();
// create a new Longitude instance with these values and return it
return new wxFlags();
} else
return base.ConvertFrom(context, culture, value);
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
// check that the value we got passed on is of type Longitude
if (value != null)
if (!(value is wxFlags))
throw new Exception("wrong");
// convert to a string
if (destinationType == typeof(string))
{
// no value so we return an empty string
if (value == null)
return string.Empty;
wxFlags flags = (wxFlags)value;
// convert to a string and return
return flags.ToString();
}
// convert to a instance descriptor
if (destinationType == typeof(InstanceDescriptor))
{
// no value so we return no instance descriptor
if (value == null)
return null;
// strongly typed
wxFlags flags = (wxFlags)value;
// used to descripe the constructor
MemberInfo Member = null;
object[] Arguments = null;
// get the constructor for the type
Member = typeof(wxFlags).GetConstructor(new Type[] {});
// create the arguments to pass along
Arguments = new object[] {};
// return the instance descriptor
if (Member != null)
return new InstanceDescriptor(Member, Arguments);
else
return null;
}
// call the base converter
return base.ConvertTo(context, culture, value, destinationType);
}
}
}