Exemplo n.º 1
0
 /// <summary>
 /// Sets flag bit(s)
 /// </summary>
 /// <param name="flag">The flag bit(s) to set</param>
 /// <param name="setting">True to set, false to clear</param>
 void SetFlag(OperationFlag flag, bool setting)
 {
     if (setting)
     {
         m_Flag |= flag;
     }
     else
     {
         m_Flag &= (~flag);
     }
 }
Exemplo n.º 2
0
 /// <summary>
 /// Sets flag bit(s)
 /// </summary>
 /// <param name="flag">The flag bit(s) to set</param>
 /// <param name="setting">True to set, false to clear</param>
 void SetFlag(OperationFlag flag, bool setting)
 {
     if (setting)
         m_Flag |= flag;
     else
         m_Flag &= (~flag);
 }
Exemplo n.º 3
0
 /// <summary>
 /// Is a flag bit set?
 /// </summary>
 /// <param name="flag">The flag(s) to check for (may be a combination of more
 /// than one flag)</param>
 /// <returns>True if any of the supplied flag bits are set</returns>
 bool IsFlagSet(OperationFlag flag)
 {
     return((m_Flag & flag) != 0);
 }
Exemplo n.º 4
0
 /// <summary>
 /// Is a flag bit set?
 /// </summary>
 /// <param name="flag">The flag(s) to check for (may be a combination of more
 /// than one flag)</param>
 /// <returns>True if any of the supplied flag bits are set</returns>
 bool IsFlagSet(OperationFlag flag)
 {
     return ((m_Flag & flag)!=0);
 }
Exemplo n.º 5
0
 public static void ClearOperationModeFlag(OperationFlag flagValue)
 {
     _operationLevelFlag &= ~((int)flagValue);
 }
Exemplo n.º 6
0
 public static bool IsOperationModeFlagSet(OperationFlag flagValue)
 {
     return((_operationLevelFlag & (int)flagValue) > 0);
 }
Exemplo n.º 7
0
 public static void SetOperationModeFlag(OperationFlag flagValue)
 {
     _operationLevelFlag = _operationLevelFlag | (int)flagValue;
     ValidateFlag();
 }
Exemplo n.º 8
0
        /// <summary>
        /// Edits the specified object's value using the editor style indicated by the GetEditStyle method.
        /// </summary>
        /// <param name="context">An ITypeDescriptorContext that can be used to gain additional context information.</param>
        /// <param name="provider">An IServiceProvider that this editor can use to obtain services.</param>
        /// <param name="value">The object to edit.</param>
        /// <returns>The new value of the object. If the value of the object has not changed, this should return the same object it was passed.</returns>
        public override object EditValue(
            ITypeDescriptorContext context,
            IServiceProvider provider,
            object value)
        {
            Type type = value.GetType();

            if (type.FullName != typeof(TEnum).FullName)
            {
                throw new ArgumentException(string.Format(" {0}\nActual: {1}", typeof(TEnum), type));
            }

            if (!type.IsEnum ||
                !type.CustomAttributes.Any(customAttr =>
                                           customAttr.AttributeType == typeof(FlagsAttribute)))
            {
                throw new InvalidEnumArgumentException();
            }

            var   winFormsSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
            TEnum enumValue   = (TEnum)(value ?? default(TEnum));

            if (winFormsSvc != null)
            {
                TEnum fullSelection = EnumExtansions.Combine(EnumExtansions.GetValues <TEnum>());

                using (var control = new UserControl()
                {
                    BackColor = SystemColors.Control
                })
                {
                    var listbox = new CheckedListBox()
                    {
                        Dock         = DockStyle.Fill,
                        CheckOnClick = true,
                    };

                    var listValues = EnumExtansions.GetValues <TEnum>().Select(val =>
                                                                               new
                    {
                        Object    = val,
                        IsChecked = EnumExtansions.HasFlag(enumValue, val),
                    });

                    listbox.Items.Add("Select all",
                                      fullSelection.Equals(value));

                    foreach (var item in listValues)
                    {
                        listbox.Items.Add(item.Object, item.IsChecked);
                    }

                    OperationFlag isChecking = false;

                    listbox.ItemCheck += (s, e) =>
                    {
                        if (!isChecking)
                        {
                            using (isChecking.Flip())
                            {
                                if (e.Index == 0)
                                {
                                    for (int i = 1; i < listbox.Items.Count; i++)
                                    {
                                        listbox.SetItemChecked(i, (e.NewValue == CheckState.Checked) ^
                                                               ((TEnum)listbox.Items[i]).Equals(default(TEnum)));
                                    }

                                    value = e.NewValue == CheckState.Checked
                                                                                        ? fullSelection
                                                                                        : default(TEnum);
                                }
                                else
                                {
                                    var updatedValues = listbox.Items.Cast <object>()
                                                        .Select((Item, Index) => new { Item, Index })
                                                        .Where(item => (item.Index == e.Index && e.NewValue == CheckState.Checked) ||
                                                               (listbox.GetItemChecked(item.Index) && (item.Index != e.Index)))
                                                        .Select(item => item.Item)
                                                        .OfType <TEnum>();

                                    value = EnumExtansions.Combine(updatedValues);
                                    listbox.SetItemChecked(0, fullSelection.Equals(value));
                                }
                            }
                        }
                    };

                    var okButton = new Button()
                    {
                        Text      = "Ok",
                        Dock      = DockStyle.Bottom,
                        BackColor = SystemColors.ButtonFace,
                    };

                    okButton.Click += delegate
                    {
                        winFormsSvc.CloseDropDown();
                    };

                    control.Controls.Add(listbox);
                    control.Controls.Add(okButton);

                    winFormsSvc.DropDownControl(control);
                }
            }

            return(value);
        }