Rayburn
2012-08-24 d7103db49fd37885f1b33bb9a597c3f55579f44b
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
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
 
namespace DupliDelete
{
    public class CodeEditor : RichTextBox 
    {
        public CodeCompletionListBox CodeCompletionListBox;
 
        protected override void OnKeyPress(KeyPressEventArgs e)
        {
            base.OnKeyPress(e);
 
            int old_pos=0;
            int line = this.GetLineFromCharIndex(this.GetFirstCharIndexOfCurrentLine());
 
            if (this.Lines.Length > 0)
            {
 
                if (this.Lines[line].Contains("if"))
                {
                    this.Select(this.GetFirstCharIndexFromLine(line) + this.Lines[line].IndexOf("if"), 2);
 
                    this.SelectionColor = Color.Blue;
 
                    this.SelectionLength = 0;
 
                    old_pos = this.GetFirstCharIndexFromLine(line) + this.Lines[line].Length;
                    this.Select(old_pos, 0);
 
                    this.SelectionColor = Color.Black;
                }
            }
 
            Point cursor_pos = this.GetPositionFromCharIndex(old_pos);
 
            cursor_pos.Y += 25;
 
            if (GetCurrentWord() == "i")
                CodeCompletionListBox.Show(cursor_pos);
            else
                CodeCompletionListBox.Hide();
        }
 
        private string GetCurrentWord()
        {
            if (Lines.Length > 1)
            {
                int line = this.GetLineFromCharIndex(this.GetFirstCharIndexOfCurrentLine());
                int i = this.GetFirstCharIndexFromLine(line) + this.Lines[line].Length;
                int orig_pos = i;
 
                while (Lines[line][i] != ' ' && i > 0)
                {
                    i--;
                }
 
                return this.Lines[line].Substring(i, orig_pos);
            }
            else
                return "";
        }
 
        public CodeEditor()
        {
            Font = new Font("Courier", 13);
 
            CodeCompletionListBox = new CodeCompletionListBox();
 
            CodeCompletionListBox.Items.Add("if");
            CodeCompletionListBox.Items.Add("while");
            CodeCompletionListBox.Items.Add("else");
            CodeCompletionListBox.Items.Add("than");
 
            CodeCompletionListBox.Visible = false;
 
            Controls.Add(CodeCompletionListBox);
        }
    }
}