summaryrefslogtreecommitdiff
path: root/Source/ModelViewer/SearchBox.cs
blob: 0f337ba5ea5786aefee3cd55b3e177c320089465 (plain)
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Microsoft.Boogie.ModelViewer
{
  public partial class SearchBox : Form
  {
    public SearchBox(Main a)
    {
      this.axprof = a;
      InitializeComponent();
      this.textBox1.Text = a.SearchText;
    }

    class NodeText
    {
      internal TreeNode n;
      public NodeText(TreeNode n) { this.n = n; }
      public override string ToString()
      {
        return n.Text;
      }
    }

    List<NodeText> nodes = new List<NodeText>();
    Main axprof;

    private void AddNodes(TreeNodeCollection coll)
    {
      foreach (TreeNode n in coll) {
        nodes.Add(new NodeText(n));
        if (n.IsExpanded) {
          AddNodes(n.Nodes);
        }
      }
    }

    public void SetFilter(string s)
    {
      axprof.SearchText = this.textBox1.Text;
      var words0 = s.Split(' ');
      var words = new List<string>();
      foreach (var w in words0) if (w != "") words.Add(w.ToLower());
      var objs = new List<object>();
      foreach (var n in nodes) {
        bool wrong = false;
        string x = n.ToString().ToLower();
        foreach (var w in words) {
          if (!x.Contains(w)) { wrong = true; break; }
        }
        if (!wrong) objs.Add(n);
      }
      listBox1.BeginUpdate();
      listBox1.Items.Clear();
      listBox1.Items.AddRange(objs.ToArray());
      listBox1.EndUpdate();
    }

    public void Populate(TreeNodeCollection coll)
    {
      nodes.Clear();
      AddNodes(coll);
      SetFilter(this.textBox1.Text);
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {
      SetFilter(textBox1.Text);
    }

    private void SearchBox_FormClosing(object sender, FormClosingEventArgs e)
    {

    }

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {
      if (e.KeyChar == (char)13 || e.KeyChar == (char)27)
        e.Handled = true;

    }

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
      if (e.KeyCode == Keys.Down) {
        listBox1.SelectedIndex = 0;
        listBox1.Focus();
        e.Handled = true;
      } else if (e.KeyCode == Keys.Enter) {
        Execute(true);
        e.Handled = true;
      } else if (e.KeyCode == Keys.Escape) {
        this.Hide();
        e.Handled = true;
      }
    }

    private void Execute(bool first)
    {
      if (listBox1.Items.Count == 0) return;

      NodeText n =
        (first ? listBox1.Items[0] : listBox1.SelectedItem) as NodeText;
      if (n != null) {
        axprof.Activate(n.n);
        this.Hide();
      }
    }

    private void listBox1_Click(object sender, EventArgs e)
    {
      Execute(false);
    }

    private void listBox1_KeyDown(object sender, KeyEventArgs e)
    {
      if (e.KeyCode == Keys.Enter) {
        Execute(false);
        e.Handled = true;
      }
    }
  }
}