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
55 public string inputing_word;
59 public Document textbox;
63 public ICompleteItem item;
67 /// <param name="item"></param>
68 /// <param name="inputing_word"></param>
69 /// <param name="textbox"></param>
70 public SelectItemEventArgs(ICompleteItem item, string inputing_word, Document textbox)
73 this.inputing_word = inputing_word;
74 this.textbox = textbox;
79 /// イベントパラメーターイベントパラメーター
81 public sealed class CollectCompleteItemEventArgs : EventArgs
86 public int InputedRow;
90 public Document textbox;
94 /// <param name="textbox"></param>
95 public CollectCompleteItemEventArgs(Document textbox)
97 this.textbox = textbox;
98 this.InputedRow = textbox.CaretPostion.row - 1;
99 if (this.InputedRow < 0)
107 /// <param name="sender"></param>
108 /// <param name="e"></param>
109 public delegate void SelectItemEventHandler(object sender,SelectItemEventArgs e);
113 /// <param name="sender"></param>
114 /// <param name="e"></param>
115 public delegate void ShowingCompleteBoxEnventHandler(object sender, ShowingCompleteBoxEventArgs e);
120 public class AutoCompleteBoxBase
122 const int InputLength = 2; //補完を開始する文字の長さ
127 protected Document Document
136 /// <param name="document">対象となるDocumentWindow</param>
137 public AutoCompleteBoxBase(Document document)
139 this.SelectItem = (s, e) => {
140 string inputing_word = e.inputing_word;
141 string word = e.item.word;
144 //キャレットは入力された文字の後ろにあるので、一致する分だけ選択して置き換える
145 int caretIndex = doc.LayoutLines.GetIndexFromTextPoint(e.textbox.CaretPostion);
146 int start = caretIndex - inputing_word.Length;
149 doc.Replace(start, inputing_word.Length, word);
152 this.ShowingCompleteBox = (s, e) => {
153 AutoCompleteBoxBase box = (AutoCompleteBoxBase)s;
156 int caretIndex = doc.LayoutLines.GetIndexFromTextPoint(e.textbox.CaretPostion);
157 int inputingIndex = caretIndex - 1;
158 if (inputingIndex < 0)
161 e.inputedWord = CompleteHelper.GetWord(doc, inputingIndex, box.Operators) + e.KeyChar;
163 if (e.inputedWord == null)
166 for (int i = 0; i < box.Items.Count; i++)
168 CompleteWord item = (CompleteWord)box.Items[i];
169 if (item.word.StartsWith(e.inputedWord))
176 this.CollectItems = (s, e) =>
178 AutoCompleteBoxBase box = (AutoCompleteBoxBase)s;
179 CompleteHelper.AddCompleteWords(box.Items, box.Operators, e.textbox.LayoutLines[e.InputedRow]);
181 this.Operators = new char[] { ' ', '\t', Document.NewLine };
182 this.Document = document;
185 internal void ParseInput(string input_text)
187 if (this.Operators == null ||
188 this.ShowingCompleteBox == null ||
189 (this.IsCloseCompleteBox == false && input_text == "\b"))
192 if (input_text == "\r" || input_text == "\n")
194 this.CollectItems(this, new CollectCompleteItemEventArgs(this.Document));
198 this.OpenCompleteBox(input_text);
202 /// 補完候補を追加可能な時に発生するイベント
204 public EventHandler<CollectCompleteItemEventArgs> CollectItems;
206 /// 補完すべき単語が選択されたときに発生するイベント
208 public SelectItemEventHandler SelectItem;
212 public ShowingCompleteBoxEnventHandler ShowingCompleteBox;
217 public char[] Operators
224 /// オートコンプリートの対象となる単語のリスト
226 public virtual CompleteCollection<ICompleteItem> Items
232 internal Func<TextPoint,Document, Point> GetPostion;
235 /// 自動補完リストが表示されているかどうか
237 protected virtual bool IsCloseCompleteBox
243 /// 自動補完を行うかどうか。行うなら真
254 /// <param name="ev"></param>
255 protected virtual void RequestShowCompleteBox(ShowingCompleteBoxEventArgs ev)
262 protected virtual void RequestCloseCompleteBox()
269 /// <param name="key_char">入力しようとしていた文字列</param>
270 /// <param name="force">補完候補がなくても表示するなら真。そうでないなら偽</param>
271 public void OpenCompleteBox(string key_char, bool force = false)
276 if (this.GetPostion == null)
277 throw new InvalidOperationException("GetPostionがnullです");
278 Point p = this.GetPostion(this.Document.CaretPostion,this.Document);
280 ShowingCompleteBoxEventArgs ev = new ShowingCompleteBoxEventArgs(key_char, this.Document, p);
281 ShowingCompleteBox(this, ev);
283 bool hasCompleteItem = ev.foundIndex != -1 && ev.inputedWord != null && ev.inputedWord != string.Empty && ev.inputedWord.Length >= InputLength;
284 System.Diagnostics.Debug.WriteLine("hasCompleteItem:{0}", hasCompleteItem);
285 if (force || hasCompleteItem)
287 RequestShowCompleteBox(ev);
291 RequestCloseCompleteBox();