123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
- namespace Aitex.UI.Charting
- {
- /// <summary>
- /// Interaction logic for LuaEditor.xaml
- /// </summary>
- public partial class LuaEditor : UserControl
- {
- public LuaEditor()
- {
- InitializeComponent();
- }
- public void SetHotWords(List<string> hotWords)
- {
- _hotWords = hotWords;
- }
- List<string> _hotWords = new List<string>();
- public string Text
- {
- get
- {
- return textBox1.Text;
- }
- set
- {
- textBox1.Text = value;
- }
- }
- private void OnKeyUp(object sender, KeyEventArgs e)
- {
- if (e.Key == Key.OemPeriod)
- {
- var rect = textBox1.GetRectFromCharacterIndex(textBox1.SelectionStart);
- listBox1.Margin = new Thickness(rect.Left + 5, rect.Top + 14, 0, 0);
- int index = textBox1.SelectionStart;
- while (index > 0 && index <= textBox1.Text.Length)
- {
- var curChar = textBox1.Text[index - 1];
- if (curChar == ' ' ||
- curChar == '\r' ||
- curChar == '\n' ||
- curChar == '\t')
- {
- break;
- }
- index--;
- }
- string keyWord = textBox1.Text.Substring(index, textBox1.SelectionStart - index);
- Dictionary<string, string> dics = new Dictionary<string, string>();
- foreach (var item in _hotWords)
- {
- if (item.Contains(keyWord))
- {
- var leftStr = item.Replace(keyWord, "");
- var split = leftStr.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries);
- if (split.Length > 1)
- {
- var ss = "+" + split[0];
- if (!dics.ContainsKey(ss))
- {
- dics.Add(ss, split[0]);
- }
- }
- else if (split.Length == 1)
- {
- var ss = " " + split[0];
- if (!dics.ContainsKey(ss))
- {
- dics.Add(ss, split[0]);
- }
- }
- }
- }
- listBox1.Items.Clear();
- if (dics.Keys.Count > 0)
- {
- var keys = dics.Keys.ToList();
- keys.Sort();
- foreach (var key in keys)
- {
- TextBlock tb = new TextBlock() { Text = key, Tag = dics[key], FontSize = 12 };
- listBox1.Items.Add(tb);
- }
- listBox1.Visibility = System.Windows.Visibility.Visible;
- listBox1.Focus();
- }
- else
- {
- listBox1.Visibility = System.Windows.Visibility.Hidden;
- textBox1.Focus();
- }
- }
- else
- {
- listBox1.Visibility = System.Windows.Visibility.Hidden;
- }
- }
- private void listBox1_KeyDown(object sender, KeyEventArgs e)
- {
- if (e.Key == Key.Enter)
- {
- var tb = listBox1.SelectedItem as TextBlock;
- if (tb != null)
- {
- listBox1.Visibility = System.Windows.Visibility.Hidden;
- int selStart = textBox1.SelectionStart;
- string insertStr = (string)tb.Tag;
- textBox1.Text = textBox1.Text.Insert(textBox1.SelectionStart, insertStr);
- textBox1.SelectionStart = selStart + insertStr.Length;
- textBox1.Focus();
- }
- }
- else if (e.Key == Key.Back)
- {
- listBox1.Visibility = System.Windows.Visibility.Hidden;
- int selStart = textBox1.SelectionStart;
- textBox1.Text = textBox1.Text.Remove(selStart - 1, 1);
- textBox1.SelectionStart = selStart - 1;
- textBox1.Focus();
- }
- }
- }
- }
|