DockEditor.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ DotNET / DotNET / 8.0 / untmp / whidbey / REDBITS / ndp / fx / src / Designer / WinForms / System / WinForms / Design / DockEditor.cs / 1 / DockEditor.cs

                            //------------------------------------------------------------------------------ 
// 
//     Copyright (c) Microsoft Corporation.  All rights reserved.
// 
//----------------------------------------------------------------------------- 

/* 
 */ 
namespace System.Windows.Forms.Design {
 
    using System.Runtime.InteropServices;
    using System.ComponentModel;
    using System;
    using System.ComponentModel.Design; 
    using Microsoft.Win32;
    using System.Diagnostics; 
    using System.Drawing; 
    using System.Design;
 
    using System.Drawing.Design;
    using System.Windows.Forms;
    using System.Windows.Forms.ComponentModel;
    using System.Windows.Forms.Design; 
    using System.Diagnostics.CodeAnalysis;
 
    ///  
    /// 
    ///  
    ///    
    ///       Implements the design time editor for specifying the
    ///     property.
    /// 
    ///    
    ///  
    public sealed class DockEditor : UITypeEditor { 
        private DockUI dockUI;
 
        /// 
        /// 
        ///      Edits the given object value using the editor style provided by
        ///      GetEditorStyle.  A service provider is provided so that any 
        ///      required editing services can be obtained.
        ///  
        [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")] // everything in this assembly is full trust. 
        public override object EditValue(ITypeDescriptorContext context,  IServiceProvider  provider, object value) {
 
            object returnValue = value;

            if (provider != null) {
                IWindowsFormsEditorService edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService)); 

                if (edSvc != null) { 
                    if (dockUI == null) { 
                        dockUI = new DockUI(this);
                    } 
                    dockUI.Start(edSvc, value);
                    edSvc.DropDownControl(dockUI);
                    value = dockUI.Value;
                    dockUI.End(); 
                }
            } 
 
            return value;
        } 

        /// 
        /// 
        ///      Retrieves the editing style of the Edit method.  If the method 
        ///      is not supported, this will return None.
        ///  
        [SuppressMessage("Microsoft.Security", "CA2123:OverrideLinkDemandsShouldBeIdenticalToBase")] // everything in this assembly is full trust. 
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) {
            return UITypeEditorEditStyle.DropDown; 
        }

        /// 
        ///  
        ///     User Interface for the DockEditor.
        ///  
        ///  
        private class DockUI : Control {
 
            // Even though the selections are mutually exclusive, I'm using
            // CheckBoxes instead of RadioButtons because RadioButtons fire Click
            // events whenever they get focus, which is bad when the user is trying
            // to tab to a specific control using the keyboard. 
            private ContainerPlaceholder container = new ContainerPlaceholder();
            private CheckBox fill = new DockEditorCheckBox(); 
            private CheckBox left = new DockEditorCheckBox(); 
            private CheckBox right = new DockEditorCheckBox();
            private CheckBox top = new DockEditorCheckBox(); 
            private CheckBox bottom = new DockEditorCheckBox();
            private CheckBox none = new DockEditorCheckBox();
            private CheckBox[] upDownOrder;
            private CheckBox[] leftRightOrder; 
            private CheckBox[] tabOrder;
            private DockEditor editor = null; 
            private object value; 
            private IWindowsFormsEditorService edSvc;
 
            public DockUI(DockEditor editor) {
                this.editor = editor;
                upDownOrder = new CheckBox[] { top, fill, bottom, none};
                leftRightOrder = new CheckBox[] { left, fill, right}; 
                tabOrder = new CheckBox[] { top, left, fill, right, bottom, none};
 
                InitializeComponent(); 
            }
 
            public object Value {
                get {
                    return value;
                } 
            }
 
            public void End() { 
                edSvc = null;
                value = null; 
            }

            public virtual DockStyle GetDock(CheckBox btn) {
                if (top == btn) { 
                    return DockStyle.Top;
                } 
                else if (left == btn) { 
                    return DockStyle.Left;
                } 
                else if (bottom == btn) {
                    return DockStyle.Bottom;
                }
                else if (right == btn) { 
                    return DockStyle.Right;
                } 
                else if (fill == btn) { 
                    return DockStyle.Fill;
                } 
                return DockStyle.None;
            }

            [SuppressMessage("Microsoft.Globalization", "CA1303:DoNotPassLiteralsAsLocalizedParameters")] 
            private void InitializeComponent() {
                SetBounds(0, 0, 94, 116); 
 
                this.BackColor = SystemColors.Control;
                this.ForeColor = SystemColors.ControlText; 
                this.AccessibleName = SR.GetString(SR.DockEditorAccName);

                none.Anchor = AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
                none.Location = new Point(2, 94); 
                none.Size = new Size(90, 24);
                none.Text = DockStyle.None.ToString(); 
                none.TabIndex = 0; 
                none.TabStop = true;
                none.Appearance = Appearance.Button; 
                none.Click += new EventHandler(this.OnClick);
                none.KeyDown += new KeyEventHandler(this.OnKeyDown);
                none.AccessibleName = SR.GetString(SR.DockEditorNoneAccName);
 
                container.Anchor = AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right;
                container.Location = new Point(2, 2); 
                container.Size = new Size(90, 90); 

                none.Dock = DockStyle.Bottom; 
                container.Dock = DockStyle.Fill;

                right.Dock = DockStyle.Right;
                right.Size = new Size(20,20); 
                right.TabIndex = 4;
                right.TabStop = true; 
                right.Text = " "; // Needs at least one character so focus rect will show. 
                right.Appearance = Appearance.Button;
                right.Click += new EventHandler(this.OnClick); 
                right.KeyDown += new KeyEventHandler(this.OnKeyDown);
                right.AccessibleName = SR.GetString(SR.DockEditorRightAccName);

                left.Dock = DockStyle.Left; 
                left.Size = new Size(20,20);
                left.TabIndex = 2; 
                left.TabStop = true; 
                left.Text = " ";
                left.Appearance = Appearance.Button; 
                left.Click += new EventHandler(this.OnClick);
                left.KeyDown += new KeyEventHandler(this.OnKeyDown);
                left.AccessibleName = SR.GetString(SR.DockEditorLeftAccName);
 
                top.Dock = DockStyle.Top;
                top.Size = new Size(20,20); 
                top.TabIndex = 1; 
                top.TabStop = true;
                top.Text = " "; 
                top.Appearance = Appearance.Button;
                top.Click += new EventHandler(this.OnClick);
                top.KeyDown += new KeyEventHandler(this.OnKeyDown);
                top.AccessibleName = SR.GetString(SR.DockEditorTopAccName); 

                bottom.Dock = DockStyle.Bottom; 
                bottom.Size = new Size(20,20); 
                bottom.TabIndex = 5;
                bottom.TabStop = true; 
                bottom.Text = " ";
                bottom.Appearance = Appearance.Button;
                bottom.Click += new EventHandler(this.OnClick);
                bottom.KeyDown += new KeyEventHandler(this.OnKeyDown); 
                bottom.AccessibleName = SR.GetString(SR.DockEditorBottomAccName);
 
                fill.Dock = DockStyle.Fill; 
                fill.Size = new Size(20,20);
                fill.TabIndex = 3; 
                fill.TabStop = true;
                fill.Text = " ";
                fill.Appearance = Appearance.Button;
                fill.Click += new EventHandler(this.OnClick); 
                fill.KeyDown += new KeyEventHandler(this.OnKeyDown);
                fill.AccessibleName = SR.GetString(SR.DockEditorFillAccName); 
 
                Controls.Clear();
                Controls.AddRange(new Control[]{ 
                                   container,
                                   none});

                container.Controls.Clear(); 
                container.Controls.AddRange(new Control[]{
                                                fill, 
                                                left, 
                                                right,
                                                top, 
                                                bottom,
                                                });
            }
 
            private void OnClick(object sender, EventArgs eventargs) {
                DockStyle val = GetDock((CheckBox)sender); 
                if (val >= (DockStyle)0) { 
                    value = val;
                } 
                Teardown();
            }

            protected override void OnGotFocus(EventArgs e) { 
                base.OnGotFocus(e);
 
                // Set focus to currently selected Dock style 
                for (int i = 0; i < tabOrder.Length; i++) {
                    if (tabOrder[i].Checked) { 
                        tabOrder[i].Focus();
                        break;
                    }
                } 
            }
 
            private void OnKeyDown(object sender, KeyEventArgs e) { 
                Keys key = e.KeyCode;
                Control target = null; 
                int maxI;

                switch (key) {
                    case Keys.Up: 
                    case Keys.Down:
                        // If we're going up or down from one of the 'sides', act like we're doing 
                        // it from the center 
                        if (sender == left || sender == right)
                            sender = fill; 

                        maxI = upDownOrder.Length - 1;
                        for (int i = 0; i <= maxI; i++) {
                            if (upDownOrder[i] == sender) { 
                                if (key == Keys.Up) {
                                    target = upDownOrder[Math.Max(i-1, 0)]; 
                                } 
                                else {
                                    target = upDownOrder[Math.Min(i+1, maxI)]; 
                                }
                                break;
                            }
                        } 
                        break;
                    case Keys.Left: 
                    case Keys.Right: 
                        maxI = leftRightOrder.Length - 1;
                        for (int i = 0; i <= maxI; i++) { 
                            if (leftRightOrder[i] == sender) {
                                if (key == Keys.Left) {
                                    target = leftRightOrder[Math.Max(i-1, 0)];
                                } 
                                else {
                                    target = leftRightOrder[Math.Min(i+1, maxI)]; 
                                } 
                                break;
                            } 
                        }
                        break;
                    case Keys.Tab:
                        for (int i = 0; i < tabOrder.Length; i++) { 
                            if (tabOrder[i] == sender) {
                                i += ((e.Modifiers & Keys.Shift) == 0 ? 1 : -1); 
                                i = (i < 0 ? i + tabOrder.Length : i % tabOrder.Length); 
                                target = tabOrder[i];
                                break; 
                            }
                        }
                        break;
                    case Keys.Return: 
                        InvokeOnClick(((CheckBox)sender), EventArgs.Empty);  // Will tear down editor
                        return; 
                    default: 
                        return;  // Unhandled keys return here
                } 
                e.Handled = true;

                if (target != null && target != sender) {
                    target.Focus(); 
                }
            } 
 
            public void Start(IWindowsFormsEditorService edSvc, object value) {
                this.edSvc = edSvc; 
                this.value = value;

                if (value is DockStyle) {
                    DockStyle dock = (DockStyle)value; 

                    none.Checked = false; 
                    top.Checked = false; 
                    left.Checked = false;
                    right.Checked = false; 
                    bottom.Checked = false;
                    fill.Checked = false;

                    switch (dock) { 
                        case DockStyle.None:
                            none.Checked = true; 
                            break; 
                        case DockStyle.Top:
                            top.Checked = true; 
                            break;
                        case DockStyle.Left:
                            left.Checked = true;
                            break; 
                        case DockStyle.Right:
                            right.Checked = true; 
                            break; 
                        case DockStyle.Bottom:
                            bottom.Checked = true; 
                            break;
                        case DockStyle.Fill:
                            fill.Checked = true;
                            break; 
                    }
                } 
            } 

            private void Teardown() { 
                edSvc.CloseDropDown();
            }

 

 
            //  
            // 
            //  
            // 
            //
            private class DockEditorCheckBox : CheckBox {
 
                protected override bool ShowFocusCues {
                   get { 
                        return true; 
                   }
                } 


                protected override bool IsInputKey(System.Windows.Forms.Keys keyData) {
                    switch (keyData) { 
                        case Keys.Left:
                        case Keys.Right: 
                        case Keys.Up: 
                        case Keys.Down:
                        case Keys.Return: 
                            return true;
                    }
                    return base.IsInputKey(keyData);
                } 
            }
            ///  
            ///  
            /// 
            private class ContainerPlaceholder : Control { 
                public ContainerPlaceholder() {
                    this.BackColor = SystemColors.Control;
                    this.TabStop = false;
                } 

                protected override void OnPaint(PaintEventArgs e) { 
                    Rectangle rc = this.ClientRectangle; 
                    ControlPaint.DrawButton(e.Graphics, rc, ButtonState.Pushed);
                } 
            }
        }
    }
} 

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.


                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK