private void ProcessKeyDownOnCodeEditor(PreviewKeyDownEventArgs e, FunctionBody body)
        {
            if (body == null)
            {
                return;
            }

            if (e.KeyCode == Keys.Enter)
            {
                if (body.Rows.Count == 0)
                {
                    body.RowIndex    = 0;
                    body.ColumnIndex = 0;
                    body.Rows.Add(new FunctionRow());
                }
                else
                {
                    if (body.ColumnIndex > body.Rows[body.RowIndex].Name.Length)
                    {
                        body.ColumnIndex = body.Rows[body.RowIndex].Name.Length;
                    }

                    if (body.ColumnIndex == body.Rows[body.RowIndex].Name.Length)
                    {
                        body.RowIndex++;
                        body.Rows.Insert(body.RowIndex, new FunctionRow());
                        body.ColumnIndex = 0;
                    }
                    else
                    {
                        var row = body.Rows[body.RowIndex];

                        var name = row.Name;

                        row.Name = name.Substring(0, body.ColumnIndex);

                        body.RowIndex++;
                        body.Rows.Insert(body.RowIndex, new FunctionRow()
                        {
                            Name = name.Substring(body.ColumnIndex)
                        });
                        body.ColumnIndex = 0;
                    }
                }

                Refresh();
            }
            else if (e.KeyCode == Keys.Up)
            {
                int prevCol = body.ColumnIndex;
                int prevRow = body.RowIndex;

                body.RowIndex--;
                if (body.RowIndex < 0)
                {
                    body.RowIndex = 0;
                }

                if (e.Shift)
                {
                    if (body.SelectionRowIndex == -1)
                    {
                        body.SelectionRowIndex    = prevRow;
                        body.SelectionColumnIndex = prevCol;
                    }
                }
                else
                {
                    body.SelectionRowIndex    = -1;
                    body.SelectionColumnIndex = -1;
                }

                Refresh();
            }
            else if (e.KeyCode == Keys.Down)
            {
                int prevCol = body.ColumnIndex;
                int prevRow = body.RowIndex;

                body.RowIndex++;
                if (body.RowIndex >= body.Rows.Count)
                {
                    body.RowIndex = body.Rows.Count - 1;
                }


                if (e.Shift)
                {
                    if (body.SelectionRowIndex == -1)
                    {
                        body.SelectionRowIndex    = prevRow;
                        body.SelectionColumnIndex = prevCol;
                    }
                }
                else
                {
                    body.SelectionRowIndex    = -1;
                    body.SelectionColumnIndex = -1;
                }

                Refresh();
            }
            else if (e.KeyCode == Keys.Left)
            {
                if (body.Rows.Count == 0)
                {
                    return;
                }

                if (body.ColumnIndex > body.Rows[body.RowIndex].Name.Length)
                {
                    body.ColumnIndex = body.Rows[body.RowIndex].Name.Length;
                }

                int prevCol = body.ColumnIndex;
                int prevRow = body.RowIndex;

                body.ColumnIndex--;
                if (body.ColumnIndex < 0)
                {
                    body.RowIndex--;
                    if (body.RowIndex < 0)
                    {
                        body.RowIndex    = 0;
                        body.ColumnIndex = 0;
                    }
                    else
                    {
                        body.ColumnIndex = body.Rows[body.RowIndex].Name.Length;
                    }
                }

                if (e.Shift)
                {
                    if (body.SelectionRowIndex == -1)
                    {
                        body.SelectionRowIndex    = prevRow;
                        body.SelectionColumnIndex = prevCol;
                    }
                }
                else
                {
                    body.SelectionRowIndex    = -1;
                    body.SelectionColumnIndex = -1;
                }

                Refresh();
            }
            else if (e.KeyCode == Keys.Right)
            {
                if (body.Rows.Count == 0)
                {
                    return;
                }

                if (body.ColumnIndex > body.Rows[body.RowIndex].Name.Length)
                {
                    body.ColumnIndex = body.Rows[body.RowIndex].Name.Length;
                }

                int prevCol = body.ColumnIndex;
                int prevRow = body.RowIndex;

                body.ColumnIndex++;
                if (body.ColumnIndex > body.Rows[body.RowIndex].Name.Length)
                {
                    body.RowIndex++;
                    if (body.RowIndex >= body.Rows.Count)
                    {
                        body.RowIndex    = body.Rows.Count - 1;
                        body.ColumnIndex = body.Rows[body.RowIndex].Name.Length;
                    }
                    else
                    {
                        body.ColumnIndex = 0;
                    }
                }

                if (e.Shift)
                {
                    if (body.SelectionRowIndex == -1)
                    {
                        body.SelectionRowIndex    = prevRow;
                        body.SelectionColumnIndex = prevCol;
                    }
                }
                else
                {
                    body.SelectionRowIndex    = -1;
                    body.SelectionColumnIndex = -1;
                }

                Refresh();
            }
            else if (e.KeyCode == Keys.End)
            {
                if (body.Rows.Count > 0)
                {
                    body.ColumnIndex = body.Rows[body.RowIndex].Name.Length;
                    Refresh();
                }
            }
            else if (e.KeyCode == Keys.Home)
            {
                body.ColumnIndex = 0;
                Refresh();
            }
            else if (e.KeyCode == Keys.Delete)
            {
                if (body.Rows.Count == 0)
                {
                    return;
                }
                var row = body.Rows[body.RowIndex];
                if (row.Name.Length == 0)
                {
                    // body.RowIndex++;
                    body.Rows.Remove(row);
                    body.ColumnIndex = 0;
                }
                else
                {
                    int index = body.ColumnIndex;
                    if (body.ColumnIndex > row.Name.Length)
                    {
                        index = row.Name.Length;
                    }
                    if (index == row.Name.Length)
                    {
                        if (body.RowIndex == body.Rows.Count - 1)
                        {
                            // last row??
                        }
                        else
                        {
                            var row2 = body.Rows[body.RowIndex + 1];
                            body.Rows.Remove(row2);
                            row.Name += row2.Name;
                        }
                    }
                    else
                    {
                        row.Name = row.Name.Remove(index, 1);
                    }
                }

                Refresh();
            }
        }
        private void ProcessKeyPressOnCodeEditor(KeyPressEventArgs e, FunctionBody body)
        {
            if (body == null)
            {
                return;
            }
            if (e.KeyChar == '\r')
            {
                return;
            }

            if (e.KeyChar == '\b')
            {
                if (body.Rows.Count == 0)
                {
                    return;
                }
                var row = body.Rows[body.RowIndex];
                if (row.Name.Length == 0)
                {
                    body.RowIndex--;
                    body.Rows.Remove(row);
                    if (body.Rows.Count > 0)
                    {
                        body.ColumnIndex = body.Rows.Last().Name.Length;
                    }
                    else
                    {
                        body.ColumnIndex = 0;
                    }
                }
                else
                {
                    int index = body.ColumnIndex;
                    if (body.ColumnIndex > row.Name.Length)
                    {
                        index = row.Name.Length;
                    }
                    if (index - 1 < 0)
                    {
                        if (body.RowIndex > 0)
                        {
                            body.RowIndex--;
                            body.Rows.Remove(row);
                            if (body.Rows.Count > 0)
                            {
                                body.ColumnIndex = body.Rows.Last().Name.Length;
                            }
                            else
                            {
                                body.ColumnIndex = 0;
                            }
                            if (body.RowIndex <= -1)
                            {
                                body.RowIndex = 0;
                            }
                            else
                            {
                                body.Rows[body.RowIndex].Name += row.Name;
                            }
                        }
                    }
                    else
                    {
                        row.Name = row.Name.Remove(index - 1, 1);
                        body.ColumnIndex--;
                    }
                }

                Refresh();
            }
            else
            {
                if (body.Rows.Count == 0)
                {
                    body.RowIndex    = 0;
                    body.ColumnIndex = 0;
                    body.Rows.Add(new FunctionRow());
                }
                var row   = body.Rows[body.RowIndex];
                int index = body.ColumnIndex;
                if (body.ColumnIndex > row.Name.Length)
                {
                    index = row.Name.Length;
                }

                row.Name = row.Name.Insert(index, e.KeyChar.ToString());

                var size = this.CreateGraphics().MeasureString(row.Name, this.Font);

                if (size.Width + 12 > body.Owner.Width)
                {
                    body.Owner.Width = size.Width + 12;
                }

                body.ColumnIndex++;

                Refresh();
            }
        }
Beispiel #3
0
 public Function()
 {
     Body = new FunctionBody(this);
 }