Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve & use the new FlagsEditor for all flags #121

Merged
merged 3 commits into from
May 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions Scintilla.NET/FlagsConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Text;

namespace ScintillaNET;

internal class FlagsConverter : TypeConverter
{
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
return destinationType == typeof(string) || base.CanConvertTo(context, destinationType);
}

public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
}

public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (value is Enum valueEnum && destinationType == typeof(string))
{
Type enumType = valueEnum.GetType();
ulong valueBits = Convert.ToUInt64(valueEnum);
if (valueBits == 0)
{
return Enum.ToObject(enumType, 0).ToString();
}
ulong bits = 0;
StringBuilder sb = new StringBuilder();
void Add(Enum item, ulong itemBits)
{
if (itemBits != 0 && valueEnum.HasFlag(item))
{
bits |= itemBits;
if (sb.Length > 0)
sb.Append(" | ");
sb.Append(item);
}
}
foreach (Enum item in Enum.GetValues(enumType))
{
ulong itemBits = Convert.ToUInt64(item);
if (Helpers.PopCount(itemBits) == 1)
Add(item, itemBits);
}
foreach (Enum item in Enum.GetValues(enumType))
{
ulong itemBits = Convert.ToUInt64(item);
if (Helpers.PopCount(itemBits) > 1 && (bits & itemBits) != itemBits)
Add(item, itemBits);
}
return sb.ToString();
}
return base.ConvertTo(context, culture, value, destinationType);
}

public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string str)
{
Type t = context.PropertyDescriptor.PropertyType;
ulong bits = 0;
var nameList = str.Split('|').Select(x => x.Trim());
foreach (var name in nameList)
{
bits |= Convert.ToUInt64(Enum.Parse(t, name));
}
return Enum.ToObject(t, bits);
}
return base.ConvertFrom(context, culture, value);
}
}
139 changes: 3 additions & 136 deletions Scintilla.NET/FlagsEditor.cs
Original file line number Diff line number Diff line change
@@ -1,159 +1,26 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Design;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.Design;

namespace ScintillaNET;

internal class FlagsConverter<T> : TypeConverter where T : struct, Enum
{
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
return destinationType == typeof(string) || base.CanConvertTo(context, destinationType);
}

public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
}

public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
if (value is Enum e && destinationType == typeof(string))
{
if (Convert.ToUInt64(e) == 0)
{
return Enum.ToObject(e.GetType(), 0).ToString();
}
StringBuilder sb = new StringBuilder();
foreach (Enum item in Enum.GetValues(e.GetType()))
{
if (Convert.ToUInt64(item) != 0 && e.HasFlag(item))
{
if (sb.Length > 0)
sb.Append(" | ");
sb.Append(item);
}
}
return sb.ToString();
}
return base.ConvertTo(context, culture, value, destinationType);
}

public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if (value is string str)
{
Type t = typeof(T);
ulong bits = 0;
var nameList = str.Split('|').Select(x => x.Trim());
foreach (var name in nameList)
{
if (Enum.TryParse(name, out T bit))
bits |= Convert.ToUInt64(bit);
else
throw new InvalidCastException($"Cannot convert \"{str.Replace("\"", "\\\"")}\" to {t}.");
}
return Enum.ToObject(t, bits);
}
return base.ConvertFrom(context, culture, value);
}
}

internal class FlagsEditor : UITypeEditor
{
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) => UITypeEditorEditStyle.DropDown;

public override bool IsDropDownResizable => true;

private int inCheck = 0;

private static ulong CombineEnumList(IEnumerable checkedList)
{
ulong bits = 0;
foreach (var item in checkedList)
{
bits |= Convert.ToUInt64(item);
}
return bits;
}

public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
if (value is Enum e && context.PropertyDescriptor.Attributes.OfType<FlagsAttribute>().Any())
{
IWindowsFormsEditorService svc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
Type enumType = e.GetType();

CheckedListBox checkedListBox = new() {
Dock = DockStyle.Fill,
CheckOnClick = true,
};
checkedListBox.ItemCheck += (object sender, ItemCheckEventArgs e) => {
if (inCheck > 0)
return;
inCheck++;
try
{
ulong bits = CombineEnumList(checkedListBox.CheckedItems);
ulong change = Convert.ToUInt64(checkedListBox.Items[e.Index]);
if (e.NewValue == CheckState.Checked)
bits |= change;
else if (e.NewValue == CheckState.Unchecked)
bits &= ~change;
Enum enumFinal = (Enum)Enum.ToObject(enumType, bits);
for (int i = 0; i < checkedListBox.Items.Count; i++)
{
Enum itemValue = (Enum)checkedListBox.Items[i];
checkedListBox.SetItemChecked(i, enumFinal.HasFlag(itemValue));
}
}
finally
{
inCheck--;
}
};
inCheck++;
try
{
foreach (Enum item in Enum.GetValues(enumType))
{
if (Convert.ToUInt64(item) != 0)
checkedListBox.Items.Add(item, e.HasFlag(item));
}
}
finally
{
inCheck--;
}
Button okBtton = new() {
Dock = DockStyle.Bottom,
AutoSize = true,
AutoSizeMode = AutoSizeMode.GrowAndShrink,
UseVisualStyleBackColor = true,
};
UserControl userControl = new();
userControl.Controls.Add(checkedListBox);
userControl.Controls.Add(okBtton);
okBtton.Text = NativeMethods.GetMessageBoxString(0); // OK
okBtton.Click += (object sender, EventArgs e) => {
ulong bits = 0;
foreach (Enum item in checkedListBox.CheckedItems)
{
bits |= Convert.ToUInt64(item);
}
value = Enum.ToObject(enumType, bits);
svc.CloseDropDown();
};
svc.DropDownControl(userControl);
return value;
var control = new FlagsEditorControl(svc, e);
svc.DropDownControl(control);
return control.Value;
}
else
return base.EditValue(context, provider, value);
Expand Down
126 changes: 126 additions & 0 deletions Scintilla.NET/FlagsEditorControl.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading