1/*
2 * Copyright 2002-2006, project beam (http://sourceforge.net/projects/beam).
3 * All rights reserved. Distributed under the terms of the MIT License.
4 *
5 * Authors:
6 *		Oliver Tappe <beam@hirschkaefer.de>
7 */
8
9#include "AutoCompleter.h"
10
11#include <Looper.h>
12#include <Message.h>
13
14#include "AutoCompleterDefaultImpl.h"
15
16
17// #pragma mark - DefaultPatternSelector
18
19
20class DefaultPatternSelector : public BAutoCompleter::PatternSelector {
21public:
22	virtual void SelectPatternBounds(const BString& text, int32 caretPos,
23		int32* start, int32* length);
24};
25
26
27void
28DefaultPatternSelector::SelectPatternBounds(const BString& text,
29	int32 caretPos, int32* start, int32* length)
30{
31	if (!start || !length)
32		return;
33	*start = 0;
34	*length = text.Length();
35}
36
37
38// #pragma mark - CompletionStyle
39
40
41BAutoCompleter::CompletionStyle::CompletionStyle(EditView* editView,
42		ChoiceModel* choiceModel, ChoiceView* choiceView,
43		PatternSelector* patternSelector)
44	:
45	fEditView(editView),
46	fPatternSelector(patternSelector ? patternSelector
47		: new DefaultPatternSelector()),
48	fChoiceModel(choiceModel),
49	fChoiceView(choiceView)
50{
51}
52
53
54BAutoCompleter::CompletionStyle::~CompletionStyle()
55{
56	delete fEditView;
57	delete fChoiceModel;
58	delete fChoiceView;
59	delete fPatternSelector;
60}
61
62
63void
64BAutoCompleter::CompletionStyle::SetEditView(EditView* view)
65{
66	delete fEditView;
67	fEditView = view;
68}
69
70
71void
72BAutoCompleter::CompletionStyle::SetPatternSelector(
73	PatternSelector* selector)
74{
75	delete fPatternSelector;
76	fPatternSelector = selector;
77}
78
79
80void
81BAutoCompleter::CompletionStyle::SetChoiceModel(ChoiceModel* model)
82{
83	delete fChoiceModel;
84	fChoiceModel = model;
85}
86
87
88void
89BAutoCompleter::CompletionStyle::SetChoiceView(ChoiceView* view)
90{
91	delete fChoiceView;
92	fChoiceView = view;
93}
94
95
96// #pragma mark - BAutoCompleter
97
98
99BAutoCompleter::BAutoCompleter(CompletionStyle* completionStyle)
100	:
101	fCompletionStyle(completionStyle)
102{
103}
104
105
106BAutoCompleter::BAutoCompleter(EditView* editView, ChoiceModel* choiceModel,
107		ChoiceView* choiceView, PatternSelector* patternSelector)
108	:
109	fCompletionStyle(new BDefaultCompletionStyle(editView, choiceModel,
110		choiceView, patternSelector))
111{
112}
113
114
115BAutoCompleter::~BAutoCompleter()
116{
117	delete fCompletionStyle;
118}
119
120
121bool
122BAutoCompleter::Select(int32 index)
123{
124	if (fCompletionStyle)
125		return fCompletionStyle->Select(index);
126	else
127		return false;
128}
129
130
131bool
132BAutoCompleter::SelectNext(bool wrap)
133{
134	if (fCompletionStyle)
135		return fCompletionStyle->SelectNext(wrap);
136	else
137		return false;
138}
139
140
141bool
142BAutoCompleter::SelectPrevious(bool wrap)
143{
144	if (fCompletionStyle)
145		return fCompletionStyle->SelectPrevious(wrap);
146	else
147		return false;
148}
149
150
151bool
152BAutoCompleter::IsChoiceSelected() const
153{
154	if (fCompletionStyle)
155		return fCompletionStyle->IsChoiceSelected();
156	else
157		return false;
158}
159
160
161int32
162BAutoCompleter::CountChoices() const
163{
164	if (fCompletionStyle && fCompletionStyle->GetChoiceModel())
165		return fCompletionStyle->GetChoiceModel()->CountChoices();
166	else
167		return 0;
168}
169
170
171int32
172BAutoCompleter::CountVisibleChoices() const
173{
174	if (fCompletionStyle && fCompletionStyle->GetChoiceView())
175		return fCompletionStyle->GetChoiceView()->CountVisibleChoices();
176	else
177		return 0;
178}
179
180
181int32
182BAutoCompleter::SelectedChoiceIndex() const
183{
184	if (fCompletionStyle)
185		return fCompletionStyle->SelectedChoiceIndex();
186	else
187		return -1;
188}
189
190
191void
192BAutoCompleter::ApplyChoice(bool hideChoices)
193{
194	if (fCompletionStyle)
195		fCompletionStyle->ApplyChoice(hideChoices);
196}
197
198
199void
200BAutoCompleter::CancelChoice()
201{
202	if (fCompletionStyle)
203		fCompletionStyle->CancelChoice();
204}
205
206
207void
208BAutoCompleter::EditViewStateChanged(bool updateChoices)
209{
210	if (fCompletionStyle)
211		fCompletionStyle->EditViewStateChanged(updateChoices);
212}
213
214
215void
216BAutoCompleter::SetEditView(EditView* view)
217{
218	if (fCompletionStyle)
219		fCompletionStyle->SetEditView(view);
220}
221
222
223void
224BAutoCompleter::SetPatternSelector(PatternSelector* selector)
225{
226	if (fCompletionStyle)
227		fCompletionStyle->SetPatternSelector(selector);
228}
229
230
231void
232BAutoCompleter::SetChoiceModel(ChoiceModel* model)
233{
234	if (fCompletionStyle)
235		fCompletionStyle->SetChoiceModel(model);
236}
237
238
239void
240BAutoCompleter::SetChoiceView(ChoiceView* view)
241{
242	if (fCompletionStyle)
243		fCompletionStyle->SetChoiceView(view);
244}
245
246
247void
248BAutoCompleter::SetCompletionStyle(CompletionStyle* style)
249{
250	delete fCompletionStyle;
251	fCompletionStyle = style;
252}
253
254