4 namespace FooEditEngine
9 public sealed class ShowingCompleteBoxEventArgs : EventArgs
14 public string KeyChar;
16 /// 入力した単語と一致したコレクションのインデックス。一致しないなら-1をセットする
18 public int foundIndex;
22 public string inputedWord;
26 public Document textbox;
30 public Point CaretPostion;
34 /// <param name="keyChar"></param>
35 /// <param name="textbox"></param>
36 /// <param name="caret_pos"></param>
37 public ShowingCompleteBoxEventArgs(string keyChar, Document textbox, Point caret_pos)
39 this.inputedWord = null;
40 this.KeyChar = keyChar;
42 this.textbox = textbox;
43 this.CaretPostion = caret_pos;
50 public sealed class SelectItemEventArgs : EventArgs
59 public string inputing_word;
63 public Document textbox;
67 /// <param name="word"></param>
68 /// <param name="inputing_word"></param>
69 /// <param name="textbox"></param>
70 public SelectItemEventArgs(string word, string inputing_word, Document textbox)
73 this.inputing_word = inputing_word;
74 this.textbox = textbox;
81 /// <param name="sender"></param>
82 /// <param name="e"></param>
83 public delegate void SelectItemEventHandler(object sender,SelectItemEventArgs e);
87 /// <param name="sender"></param>
88 /// <param name="e"></param>
89 public delegate void ShowingCompleteBoxEnventHandler(object sender, ShowingCompleteBoxEventArgs e);
94 public class AutoCompleteBoxBase
96 const int InputLength = 2; //補完を開始する文字の長さ
101 protected Document Document
110 /// <param name="document">対象となるDocumentWindow</param>
111 public AutoCompleteBoxBase(Document document)
113 this.SelectItem = (s, e) => {
114 string inputing_word = e.inputing_word;
115 string word = e.word;
118 //キャレットは入力された文字の後ろにあるので、一致する分だけ選択して置き換える
119 int caretIndex = doc.LayoutLines.GetIndexFromTextPoint(e.textbox.CaretPostion);
120 int start = caretIndex - inputing_word.Length;
123 doc.Replace(start, inputing_word.Length, word);
126 this.ShowingCompleteBox = (s, e) => {
127 AutoCompleteBoxBase box = (AutoCompleteBoxBase)s;
130 int caretIndex = doc.LayoutLines.GetIndexFromTextPoint(e.textbox.CaretPostion);
131 int inputingIndex = caretIndex - 1;
132 if (inputingIndex < 0)
135 e.inputedWord = CompleteHelper.GetWord(doc, inputingIndex, box.Operators) + e.KeyChar;
137 if (e.inputedWord == null)
140 for (int i = 0; i < box.Items.Count; i++)
142 CompleteWord item = (CompleteWord)box.Items[i];
143 if (item.word.StartsWith(e.inputedWord))
150 this.Operators = new char[] { ' ', '\t', Document.NewLine };
151 this.Document = document;
154 internal void ParseInput(string input_text)
156 if (this.Operators == null ||
157 input_text == "\r" ||
158 input_text == "\n" ||
159 this.ShowingCompleteBox == null ||
160 (this.IsCloseCompleteBox == false && input_text == "\b"))
163 this.OpenCompleteBox(input_text);
167 /// 補完すべき単語が選択されたときに発生するイベント
169 public SelectItemEventHandler SelectItem;
173 public ShowingCompleteBoxEnventHandler ShowingCompleteBox;
178 public char[] Operators
185 /// オートコンプリートの対象となる単語のリスト
187 public virtual CompleteCollection<ICompleteItem> Items
193 internal Func<TextPoint,Document, Point> GetPostion;
196 /// 自動補完リストが表示されているかどうか
198 protected virtual bool IsCloseCompleteBox
204 /// 自動補完を行うかどうか。行うなら真
215 /// <param name="ev"></param>
216 protected virtual void RequestShowCompleteBox(ShowingCompleteBoxEventArgs ev)
223 protected virtual void RequestCloseCompleteBox()
230 /// <param name="key_char">入力しようとしていた文字列</param>
231 /// <param name="force">補完候補がなくても表示するなら真。そうでないなら偽</param>
232 public void OpenCompleteBox(string key_char, bool force = false)
237 if (this.GetPostion == null)
238 throw new InvalidOperationException("GetPostionがnullです");
239 Point p = this.GetPostion(this.Document.CaretPostion,this.Document);
241 ShowingCompleteBoxEventArgs ev = new ShowingCompleteBoxEventArgs(key_char, this.Document, p);
242 ShowingCompleteBox(this, ev);
244 bool hasCompleteItem = ev.foundIndex != -1 && ev.inputedWord != null && ev.inputedWord != string.Empty && ev.inputedWord.Length >= InputLength;
245 System.Diagnostics.Debug.WriteLine("hasCompleteItem:{0}", hasCompleteItem);
246 if (force || hasCompleteItem)
248 RequestShowCompleteBox(ev);
252 RequestCloseCompleteBox();