Click here to Skip to main content
15,891,828 members
Articles / Desktop Programming / Windows Forms

Controls Library: Extended ListView with Column Mapping

Rate me:
Please Sign up or sign in to vote.
4.86/5 (29 votes)
27 Mar 2014CPOL5 min read 82.9K   7.6K   110  
Docking windows container, extended listview, extended property editor.
using dwf.tool;
using Gdk;
using Gtk;
using System;
using System.Collections.Generic;

namespace dwf.gui
{
    public class PEditor : Gtk.HBox, IPEditor
    {
        protected Dictionary<string, object> CacheControls = new Dictionary<string, object> ();
        protected IPCellEditor currentEditor;
        protected bool _changed;
        protected object _value;
        protected Widget _control;
        protected Button _drop;
        protected Button _dropEx;
        protected bool _dropDownVisible = true;
        protected bool _dropDownAutoHide = false;
        protected bool _dropDownExVisible = false;
        protected ToolWindow _dropDown;
        protected EventHandler valueChanged;
        protected EventHandler _dropDownClick;
        protected EventHandler _dropDownExClick;
            
        public PEditor ():base(false, 4)
        {
            _drop = new Button ();
            _drop.Label = "...";
            _drop.WidthRequest = 25;
            _drop.Clicked += DropClicked;
            _dropEx = new Button ();
            _dropEx.Label = "~";
            _dropEx.WidthRequest = 25;
            _dropEx.Clicked += DropExClicked;
        }

        public bool Enabled
        {
            get;
            set;
        }

        private void DropClicked (object sender, EventArgs e)
        {
            if (_dropDownClick != null)
                _dropDownClick (this, e);
            ShowDropDown ();
        }

        private void DropExClicked (object sender, EventArgs e)
        {
            if (_dropDownExClick != null)
                _dropDownExClick (this, e);
        }

        public event EventHandler DropDownClick
        {
            add{ _dropDownClick += value;}
            remove{ _dropDownClick -= value;}
        }

        public event EventHandler DropDownExClick
        {
            add{ _dropDownExClick += value;}
            remove{ _dropDownExClick -= value;}
        }

        protected override bool OnExposeEvent (EventExpose evnt)
        {
            return base.OnExposeEvent (evnt);
            //if (_dropDownVisible)
            //  CtrlService.PaintDropDown (e.Graphics, GetDropDownRectangle (), PCellDisplayState.Default);

        }

        protected override void OnSizeRequested (ref Requisition requisition)
        {
            base.OnSizeRequested (ref requisition);
            requisition.Height = 25;
        }

        public IPCellEditor CurrentEditor
        {
            get { return currentEditor; }
            set { currentEditor = value; }
        }

        public event EventHandler ValueChanged
        {
            add { valueChanged += value; }
            remove { valueChanged -= value; }
        }

        protected virtual void OnValueChanged (EventArgs ea)
        {
            _changed = true;
            if (currentEditor != null)
            {
                if (_control is TextView && !((TextView)_control).Editable)
                {
                    ((TextView)_control).Buffer.Text = currentEditor.FormatValue (_value) as string;
                }
                else if (_control is Entry && !((Entry)_control).IsEditable)
                {
                    ((Entry)_control).Text = currentEditor.FormatValue (_value) as string;
                }
            }
            
            if (DropDown != null && DropDown.Visible && !(DropDown.Target is TextView))
                DropDown.Hide ();
            
            if (valueChanged != null)
            {
                valueChanged (this, ea);
            }
        }
        
        public void RemoveAll ()
        {
            foreach (Widget w in base.AllChildren)  
                this.Remove (w);
        }

        public object EditControl
        {
            get { return _control; }
            set
            {
                if (value == _control)
                    return;
                RemoveAll ();
                _control = value as Widget;
                if (_control == null)
                    return;
                _control.Visible = true;
                this.PackStart (_control, true, true, 0);
                if (_dropDownVisible)
                    this.PackStart (_drop, false, false, 0);
                if (_dropDownExVisible)
                    this.PackStart (_dropEx, false, false, 0);
                _control.GrabFocus ();
            }
        }

        public bool DropDownExVisible
        {
             get { return _dropDownExVisible; }
            set
            {
                _dropDownExVisible = value;
            }
        }

        public bool DropDownAutoHide
        {
             get { return _dropDownAutoHide; }
            set
            {
                _dropDownAutoHide = value;
            }
        }

        public bool DropDownVisible
        {
            get { return this._drop.Visible; }
            set
            {
                if (_drop.Visible == value)
                    return;
                _drop.Visible = value;
            }
        }

        public ToolWindow DropDown
        {
            get { return _dropDown; }
            set
            {
                if (_dropDown == value)
                    return;
                _dropDown = value;              
            }
        }

        public override void Dispose ()
        {
            base.Dispose ();
            foreach (KeyValuePair<string, object> kv in CacheControls)
                if (kv.Value is IDisposable)
                    ((IDisposable)kv.Value).Dispose ();
            CacheControls.Clear ();
        }

        public virtual object Value
        {
             get {  return _value; }
            set
            {
                if (_value == value)
                    return;
                _value = value;
                OnValueChanged (null);              
            }
        }

        public Color BackColor
        {
             get { return base.Style.Background (StateType.Normal); }
            //set { base.Style.Background = value; }
        }

        private void OnButton1Click (object sender, EventArgs e)
        {
            if (_dropDown != null)
            if (!_dropDown.Visible)
                ShowDropDown ();
            //else _dropDown.Close();
        }

        private void OnButton2Click (object sender, EventArgs e)
        {
            Value = null;
        }

        private void ShowDropDown ()
        {
            if (_dropDown != null)
            {
                if (!_dropDown.Visible)
                if (_dropDown.Allocation.Width < this.Allocation.Width)
                    _dropDown.Resize (this.Allocation.Width, 200);  
                _dropDown.Show (this, new Point (0, this.Allocation.Height));
            }
        }

        public object GetCacheControl (string name, Type t)
        {
            object o = null;
            if (!CacheControls.ContainsKey (name))
            {
                o = ReflectionAccessor.CreateObject (t, true);
                CacheControls.Add (name, o);
            }
            else
                o = CacheControls [name];
            return o;
        }

        public bool IsValueChanged
        {
            get { return _changed; }
            set { _changed = value; }
        }

        public IPCell Cell { get; set; }

        public System.Drawing.Point Location { get; set; }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Kazakstan Kazakstan
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions