nekokun /NekoKun.Core/ObjectEditor/Controls/ArrayEditor.cs

Language C# Lines 229
MD5 Hash d1dc58a23ed009468de369aae6acc697 Estimated Cost $4,959 (why?)
Repository https://bitbucket.org/nekokun/nekokun.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace NekoKun.ObjectEditor
{
    public class ArrayEditor : AbstractObjectEditor, IClipboardHandler, IDeleteHandler
    {
        System.Windows.Forms.SplitContainer split = new System.Windows.Forms.SplitContainer();
        System.Windows.Forms.ListBox list;
        AbstractObjectEditor con;
        System.Windows.Forms.NumericUpDown menuResizeTextbox;
        System.Windows.Forms.ToolStripMenuItem menuResize;

        public ArrayEditor(AbstractObjectEditor Con)
            : base(null)
        {
            con = Con;
            split.Dock = System.Windows.Forms.DockStyle.Fill;
            split.Panel1MinSize = 100;
            split.FixedPanel = System.Windows.Forms.FixedPanel.Panel1;
            split.SplitterDistance = 150;
            list = new ArrayListbox();
            list.Dock = System.Windows.Forms.DockStyle.Fill;
            list.SelectedIndexChanged += new EventHandler(list_SelectedIndexChanged);
            split.Panel1.Controls.Add(list);
            con.Control.Dock = System.Windows.Forms.DockStyle.Fill;
            split.Panel2.Controls.Add(con.Control);

            Con.DirtyChanged += new EventHandler(Con_DirtyChanged);
            menuResizeTextbox = new System.Windows.Forms.NumericUpDown();
            menuResize = new System.Windows.Forms.ToolStripMenuItem();
            menuResize.Text = "?????(&M)";
            menuResizeTextbox.Font = split.Font;
            menuResizeTextbox.UpDownAlign = System.Windows.Forms.LeftRightAlignment.Left;
            menuResizeTextbox.BorderStyle = System.Windows.Forms.BorderStyle.None;
            menuResizeTextbox.Cursor = System.Windows.Forms.Cursors.Default;
            var host = new System.Windows.Forms.ToolStripControlHost(menuResizeTextbox);
            menuResize.DropDownItems.Add(host);
            var dd = (menuResize.DropDown as System.Windows.Forms.ToolStripDropDownMenu);
            dd.ShowImageMargin = false;
            dd.BackColor = System.Drawing.SystemColors.Window;

            this.list.ContextMenuStrip = new EditContextMenuStrip(this);
            this.list.ContextMenuStrip.Items.Add(new System.Windows.Forms.ToolStripSeparator());
            this.list.ContextMenuStrip.Items.Add(menuResize);
            this.list.ContextMenuStrip.Opening += new System.ComponentModel.CancelEventHandler(ContextMenuStrip_Opening);

            menuResizeTextbox.ValueChanged += new EventHandler(menuResizeTextbox_ValueChanged);
        }

        void menuResizeTextbox_ValueChanged(object sender, EventArgs e)
        {
            this.Relength((int)menuResizeTextbox.Value);
        }

        void ContextMenuStrip_Opening(object sender, System.ComponentModel.CancelEventArgs e)
        {
            menuResize.Enabled = (this.CreateDefaultObject != null);
            menuResizeTextbox.Value = this.list.Items.Count;
        }

        void Relength(int newsize)
        {
            if (newsize == this.list.Items.Count)
                return;
            else if (newsize > this.list.Items.Count)
            {
                int count = newsize - this.list.Items.Count;
                for (int i = 0; i < count; i++)
                {
                    this.list.Items.Add(this.CreateDefaultObject());
                }
                MakeDirty();
            }
            else if (newsize < this.list.Items.Count)
            {
                int count = this.list.Items.Count;
                for (int i = newsize; i < count; i++)
                {
                    this.list.Items.RemoveAt(newsize);
                }
                MakeDirty();
            }
        }

        //Still loving my tree, but losing you.

        void Con_DirtyChanged(object sender, EventArgs e)
        {
            MakeDirty();
        }

        int lastIndex = -1;

        void list_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lastIndex >= 0)
            {
                int id = lastIndex;
                lastIndex = -1;
                try
                {
                    list.Items[id] = con.SelectedItem;
                }
                catch { }
            }

            lastIndex = list.SelectedIndex;
            if (list.SelectedItem != null)
                con.SelectedItem = list.SelectedItem;
        }

        public override void Commit()
        {
            object[] ret = new object[this.list.Items.Count];
            this.list.Items.CopyTo(ret, 0);
            if (selectedItem is List<object>)
                selectedItem = new List<object>(ret);
            else
                selectedItem = ret;
        }

        public class ArrayListbox : UI.LynnListbox
        {
            protected override string GetString(int id)
            {
                return String.Format("{0:D3}: {1}", id + 1, this.Items[id].ToString());
            }
        }

        public bool CanCut
        {
            get { return this.CanCopy && this.CanDelete; }
        }

        public bool CanCopy
        {
            get { return this.list.SelectedItem != null && this.ClipboardFormat != null; }
        }

        public bool CanPaste
        {
            get {
                if (this.ClipboardFormat == null) return false;
                return System.Windows.Forms.Clipboard.ContainsData(this.ClipboardFormat);
            }
        }

        public void Cut()
        {
            Copy(); Delete();
            MakeDirty();
        }

        public void Copy()
        {
            System.Windows.Forms.DataObject db = new System.Windows.Forms.DataObject();
            db.SetData(this.ClipboardFormat, this.DumpObject(this.list.SelectedItem));
            System.Windows.Forms.Clipboard.SetDataObject(db, true, 5, 100);
        }

        public void Paste()
        {
            object data = System.Windows.Forms.Clipboard.GetData(this.ClipboardFormat);
            byte[] buffer;
            if (data is System.IO.MemoryStream)
            {
                System.IO.MemoryStream ms = data as System.IO.MemoryStream;
                buffer = new byte[ms.Length];
                ms.Read(buffer, 0, (int)ms.Length);
                ms.Dispose();
            }
            else if (data is byte[])
            {
                buffer = data as byte[];
            }
            else { return; }
            this.lastIndex = -1;
            this.list.Items[this.list.SelectedIndex] = this.LoadObject(buffer);
            con.SelectedItem = this.list.Items[this.list.SelectedIndex];
            MakeDirty();
        }

        public bool CanDelete
        {
            get { return this.list.SelectedItem != null && this.CreateDefaultObject != null; }
        }

        public void Delete()
        {
            this.lastIndex = -1;
            this.list.Items[this.list.SelectedIndex] = this.CreateDefaultObject();
            con.SelectedItem = this.list.Items[this.list.SelectedIndex];
            MakeDirty();
        }

        public CreateDefaultObjectDelegate CreateDefaultObject;
        public DumpObjectDelegate DumpObject;
        public LoadObjectDelegate LoadObject;
        public string ClipboardFormat;

        public delegate object CreateDefaultObjectDelegate();
        public delegate byte[] DumpObjectDelegate(object obj);
        public delegate object LoadObjectDelegate(byte[] obj);

        protected override void InitControl()
        {
            this.list.Items.Clear();
            if (selectedItem is List<object>)
                foreach (var item in (selectedItem as System.Collections.IEnumerable))
                {
                    this.list.Items.Add(item);
                }
            else
                this.list.Items.AddRange(selectedItem as object[]);

            lastIndex = -1;
            this.list.SelectedIndex = 0;
        }

        public override System.Windows.Forms.Control Control
        {
            get { return this.split; }
        }
    }
}
Back to Top