Mini Kabibi Habibi

Current Path : C:/Users/Public/Documents/DXperience 13.1 Demos/Silverlight/CS/BarsDemo/Modules/
Upload File :
Current File : C:/Users/Public/Documents/DXperience 13.1 Demos/Silverlight/CS/BarsDemo/Modules/Commands.xaml.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using DevExpress.Utils;
using DevExpress.Xpf.Editors;

namespace BarsDemo {
    public struct CommandsDemoCommandId : IConvertToInt<CommandsDemoCommandId>, IEquatable<CommandsDemoCommandId> {
        public static readonly CommandsDemoCommandId None = new CommandsDemoCommandId(0);
        public static readonly CommandsDemoCommandId Cut = new CommandsDemoCommandId(1);
        public static readonly CommandsDemoCommandId Copy = new CommandsDemoCommandId(2);
        public static readonly CommandsDemoCommandId Paste = new CommandsDemoCommandId(3);
        public static readonly CommandsDemoCommandId SelectAll = new CommandsDemoCommandId(4);
        public static readonly CommandsDemoCommandId Undo = new CommandsDemoCommandId(5);
        public static readonly CommandsDemoCommandId Redo = new CommandsDemoCommandId(6);

        public static readonly CommandsDemoCommandId MaximizeValue = new CommandsDemoCommandId(7);
        public static readonly CommandsDemoCommandId MinimizeValue = new CommandsDemoCommandId(8);
        public static readonly CommandsDemoCommandId IncreaseSmall = new CommandsDemoCommandId(9);
        public static readonly CommandsDemoCommandId DecreaseSmall = new CommandsDemoCommandId(10);
        public static readonly CommandsDemoCommandId IncreaseLarge = new CommandsDemoCommandId(11);
        public static readonly CommandsDemoCommandId DecreaseLarge = new CommandsDemoCommandId(12);

        public static bool operator ==(CommandsDemoCommandId id1, CommandsDemoCommandId id2) {
            return id1.m_value == id2.m_value;
        }
        public static bool operator !=(CommandsDemoCommandId id1, CommandsDemoCommandId id2) {
            return id1.m_value != id2.m_value;
        }

        public CommandsDemoCommandId(int value) {
            m_value = value;
        }

        public override bool Equals(object obj) {
            return ((obj is CommandsDemoCommandId) && (this.m_value == ((CommandsDemoCommandId)obj).m_value));
        }
        public override int GetHashCode() {
            return m_value.GetHashCode();
        }
        public override string ToString() {
            return m_value.ToString();
        }

        readonly int m_value;

        #region IConvertToInt<CommandsDemoCommandId> Members
        int IConvertToInt<CommandsDemoCommandId>.ToInt() {
            return m_value;
        }
        CommandsDemoCommandId IConvertToInt<CommandsDemoCommandId>.FromInt(int value) {
            return new CommandsDemoCommandId(value);
        }
        #endregion
        #region IEquatable<CommandsDemoCommandId> Members
        public bool Equals(CommandsDemoCommandId other) {
            return this.m_value == other.m_value;
        }
        #endregion
    }
    public class CommandsDemoCommand : ICommand {
        static ICommand _cut;
        static ICommand _copy;
        static ICommand _paste;
        static ICommand _selectAll;
        static ICommand _undo;
        static ICommand _redo;

        static ICommand _maximizeValue;
        static ICommand _minimizeValue;
        static ICommand _increaseSmall;
        static ICommand _decreaseSmall;
        static ICommand _increaseLarge;
        static ICommand _decreaseLarge;

        #region Static
        public static ICommand Cut { get { return _cut; } private set { _cut = value; } }
        public static ICommand Copy { get { return _copy; } private set { _copy = value; } }
        public static ICommand Paste { get { return _paste; } private set { _paste = value; } }
        public static ICommand SelectAll { get { return _selectAll; } private set { _selectAll = value; } }
        public static ICommand Undo { get { return _undo; } private set { _undo = value; } }
        public static ICommand Redo { get { return _redo; } private set { _redo = value; } }

        public static ICommand MaximizeValue { get { return _maximizeValue; } private set { _maximizeValue = value; } }
        public static ICommand MinimizeValue { get { return _minimizeValue; } private set { _minimizeValue = value; } }
        public static ICommand IncreaseSmall { get { return _increaseSmall; } private set { _increaseSmall = value; } }
        public static ICommand DecreaseSmall { get { return _decreaseSmall; } private set { _decreaseSmall = value; } }
        public static ICommand IncreaseLarge { get { return _increaseLarge; } private set { _increaseLarge = value; } }
        public static ICommand DecreaseLarge { get { return _decreaseLarge; } private set { _decreaseLarge = value; } }

        static CommandsDemoCommand() {
            Cut = new CommandsDemoCommand(CommandsDemoCommandId.Cut);
            Copy = new CommandsDemoCommand(CommandsDemoCommandId.Copy);
            Paste = new CommandsDemoCommand(CommandsDemoCommandId.Paste);
            SelectAll = new CommandsDemoCommand(CommandsDemoCommandId.SelectAll);
            Undo = new CommandsDemoCommand(CommandsDemoCommandId.Undo);
            Redo = new CommandsDemoCommand(CommandsDemoCommandId.Redo);

            MaximizeValue = new CommandsDemoCommand(CommandsDemoCommandId.MaximizeValue);
            MinimizeValue = new CommandsDemoCommand(CommandsDemoCommandId.MinimizeValue);
            IncreaseSmall = new CommandsDemoCommand(CommandsDemoCommandId.IncreaseSmall);
            DecreaseSmall = new CommandsDemoCommand(CommandsDemoCommandId.DecreaseSmall);
            IncreaseLarge = new CommandsDemoCommand(CommandsDemoCommandId.IncreaseLarge);
            DecreaseLarge = new CommandsDemoCommand(CommandsDemoCommandId.DecreaseLarge);
        }
        #endregion Static
        public string Text { get { return GetTextByCommandId(); } }
        public CommandsDemoCommandId CommandId { get { return id; } }
        readonly CommandsDemoCommandId id;

        public CommandsDemoCommand() {
        }
        protected CommandsDemoCommand(CommandsDemoCommandId id) {
            this.id = id;
        }

        public virtual bool CanExecute(object parameter) {
            TextEdit textEdit = parameter as TextEdit;
            Slider slider = parameter as Slider;
            if(textEdit != null)
                return CanExecuteTextEdit(textEdit);
            if(slider != null)
                return CanExecuteSlider(slider);
            return true;
        }
        public event EventHandler CanExecuteChanged {
            add { }
            remove { }
        }
        public virtual void Execute(object parameter) {
            TextEdit textEdit = parameter as TextEdit;
            Slider slider = parameter as Slider;
            if(textEdit != null)
                ExecuteTextEdit(textEdit);
            if(slider != null)
                ExecuteSlider(slider);
        }

        protected virtual string GetTextByCommandId() {
            if(id.Equals(CommandsDemoCommandId.Undo))
                return "Undo";
            if(id.Equals(CommandsDemoCommandId.Redo))
                return "Redo";
            if(id.Equals(CommandsDemoCommandId.Cut))
                return "Cut";
            if(id.Equals(CommandsDemoCommandId.Copy))
                return "Copy";
            if(id.Equals(CommandsDemoCommandId.Paste))
                return "Paste";
            if(id.Equals(CommandsDemoCommandId.SelectAll))
                return "SelectAll";

            if(id.Equals(CommandsDemoCommandId.MaximizeValue))
                return "Maximize";
            if(id.Equals(CommandsDemoCommandId.MinimizeValue))
                return "Minimize";
            if(id.Equals(CommandsDemoCommandId.IncreaseSmall))
                return "Increase Small";
            if(id.Equals(CommandsDemoCommandId.DecreaseSmall))
                return "Decrease Small";
            if(id.Equals(CommandsDemoCommandId.IncreaseLarge))
                return "Increase Large";
            if(id.Equals(CommandsDemoCommandId.DecreaseLarge))
                return "Decrease Large";

            return string.Empty;
        }

        protected virtual bool CanExecuteSlider(Slider slider) {
            return true;
        }
        protected virtual void ExecuteSlider(Slider slider) {
            if(id.Equals(CommandsDemoCommandId.MaximizeValue)) {
                ExecuteSliderMaximizeValue(slider);
                return;
            }
            if(id.Equals(CommandsDemoCommandId.MinimizeValue)) {
                ExecuteSliderMinimizeValue(slider);
                return;
            }
            if(id.Equals(CommandsDemoCommandId.IncreaseSmall)) {
                ExecuteSliderIncreaseSmall(slider);
                return;
            }
            if(id.Equals(CommandsDemoCommandId.DecreaseSmall)) {
                ExecuteSliderDecreaseSmall(slider);
                return;
            }
            if(id.Equals(CommandsDemoCommandId.IncreaseLarge)) {
                ExecuteSliderIncreaseLarge(slider);
                return;
            }
            if(id.Equals(CommandsDemoCommandId.DecreaseLarge)) {
                ExecuteSliderDecreaseLarge(slider);
                return;
            }
        }
        protected virtual void ExecuteSliderMaximizeValue(Slider slider) {
            slider.Value = slider.Maximum;
        }
        protected virtual void ExecuteSliderMinimizeValue(Slider slider) {
            slider.Value = slider.Minimum;
        }
        protected virtual void ExecuteSliderIncreaseSmall(Slider slider) {
            slider.Value += slider.SmallChange;
        }
        protected virtual void ExecuteSliderDecreaseSmall(Slider slider) {
            slider.Value -= slider.SmallChange;
        }
        protected virtual void ExecuteSliderIncreaseLarge(Slider slider) {
            slider.Value += slider.LargeChange;
        }
        protected virtual void ExecuteSliderDecreaseLarge(Slider slider) {
            slider.Value -= slider.LargeChange;
        }

        protected virtual bool CanExecuteTextEdit(TextEdit textEdit) {
            if(id.Equals(CommandsDemoCommandId.Undo) || id.Equals(CommandsDemoCommandId.Redo))
                return false;
            return true;
        }
        protected virtual void ExecuteTextEdit(TextEdit textEdit) {
            if(id.Equals(CommandsDemoCommandId.Undo) || id.Equals(CommandsDemoCommandId.Redo))
                return;
            if(id.Equals(CommandsDemoCommandId.Cut)) {
                ExecuteTextEditCut(textEdit);
                return;
            }
            if(id.Equals(CommandsDemoCommandId.Copy)) {
                ExecuteTextEditCopy(textEdit);
                return;
            }
            if(id.Equals(CommandsDemoCommandId.Paste)) {
                ExecuteTextEditPaste(textEdit);
                return;
            }
            if(id.Equals(CommandsDemoCommandId.SelectAll)) {
                ExecuteTextEditSelectAll(textEdit);
                return;
            }
        }
        protected virtual void ExecuteTextEditCut(TextEdit textEdit) {
            ExecuteTextEditCopy(textEdit);
            textEdit.SelectedText = string.Empty;
        }
        protected virtual void ExecuteTextEditCopy(TextEdit textEdit) {
            if(textEdit.SelectedText == string.Empty)
                return;
            ClipboardSetText(textEdit.SelectedText);
        }
        protected virtual void ExecuteTextEditPaste(TextEdit textEdit) {
            textEdit.SelectedText = ClipboardGetText();
            textEdit.SelectionStart = textEdit.SelectionStart + textEdit.SelectionLength;
            textEdit.SelectionLength = 0;
        }
        protected virtual void ExecuteTextEditSelectAll(TextEdit textEdit) {
            textEdit.SelectAll();
        }

        void ClipboardSetText(string text) {
            try {
                Clipboard.SetText(text);
            } catch { }
        }
        string ClipboardGetText() {
            try {
                return Clipboard.GetText();
            } catch {
                return string.Empty;
            }
        }
    }
    public partial class Commands : BarsDemoModule {
        public Commands() {
            InitializeComponent();
        }
    }
}