xref: /haiku/src/apps/webpositive/autocompletion/AutoCompleter.cpp (revision 25a7b01d15612846f332751841da3579db313082)
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 
20 class DefaultPatternSelector : public BAutoCompleter::PatternSelector {
21 public:
22 	virtual void SelectPatternBounds(const BString& text, int32 caretPos,
23 		int32* start, int32* length);
24 };
25 
26 
27 void
SelectPatternBounds(const BString & text,int32 caretPos,int32 * start,int32 * length)28 DefaultPatternSelector::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 
CompletionStyle(EditView * editView,ChoiceModel * choiceModel,ChoiceView * choiceView,PatternSelector * patternSelector)41 BAutoCompleter::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 
~CompletionStyle()54 BAutoCompleter::CompletionStyle::~CompletionStyle()
55 {
56 	delete fEditView;
57 	delete fChoiceModel;
58 	delete fChoiceView;
59 	delete fPatternSelector;
60 }
61 
62 
63 void
SetEditView(EditView * view)64 BAutoCompleter::CompletionStyle::SetEditView(EditView* view)
65 {
66 	delete fEditView;
67 	fEditView = view;
68 }
69 
70 
71 void
SetPatternSelector(PatternSelector * selector)72 BAutoCompleter::CompletionStyle::SetPatternSelector(
73 	PatternSelector* selector)
74 {
75 	delete fPatternSelector;
76 	fPatternSelector = selector;
77 }
78 
79 
80 void
SetChoiceModel(ChoiceModel * model)81 BAutoCompleter::CompletionStyle::SetChoiceModel(ChoiceModel* model)
82 {
83 	delete fChoiceModel;
84 	fChoiceModel = model;
85 }
86 
87 
88 void
SetChoiceView(ChoiceView * view)89 BAutoCompleter::CompletionStyle::SetChoiceView(ChoiceView* view)
90 {
91 	delete fChoiceView;
92 	fChoiceView = view;
93 }
94 
95 
96 // #pragma mark - BAutoCompleter
97 
98 
BAutoCompleter(CompletionStyle * completionStyle)99 BAutoCompleter::BAutoCompleter(CompletionStyle* completionStyle)
100 	:
101 	fCompletionStyle(completionStyle)
102 {
103 }
104 
105 
BAutoCompleter(EditView * editView,ChoiceModel * choiceModel,ChoiceView * choiceView,PatternSelector * patternSelector)106 BAutoCompleter::BAutoCompleter(EditView* editView, ChoiceModel* choiceModel,
107 		ChoiceView* choiceView, PatternSelector* patternSelector)
108 	:
109 	fCompletionStyle(new BDefaultCompletionStyle(editView, choiceModel,
110 		choiceView, patternSelector))
111 {
112 }
113 
114 
~BAutoCompleter()115 BAutoCompleter::~BAutoCompleter()
116 {
117 	delete fCompletionStyle;
118 }
119 
120 
121 bool
Select(int32 index)122 BAutoCompleter::Select(int32 index)
123 {
124 	if (fCompletionStyle)
125 		return fCompletionStyle->Select(index);
126 	else
127 		return false;
128 }
129 
130 
131 bool
SelectNext(bool wrap)132 BAutoCompleter::SelectNext(bool wrap)
133 {
134 	if (fCompletionStyle)
135 		return fCompletionStyle->SelectNext(wrap);
136 	else
137 		return false;
138 }
139 
140 
141 bool
SelectPrevious(bool wrap)142 BAutoCompleter::SelectPrevious(bool wrap)
143 {
144 	if (fCompletionStyle)
145 		return fCompletionStyle->SelectPrevious(wrap);
146 	else
147 		return false;
148 }
149 
150 
151 bool
IsChoiceSelected() const152 BAutoCompleter::IsChoiceSelected() const
153 {
154 	if (fCompletionStyle)
155 		return fCompletionStyle->IsChoiceSelected();
156 	else
157 		return false;
158 }
159 
160 
161 int32
CountChoices() const162 BAutoCompleter::CountChoices() const
163 {
164 	if (fCompletionStyle && fCompletionStyle->GetChoiceModel())
165 		return fCompletionStyle->GetChoiceModel()->CountChoices();
166 	else
167 		return 0;
168 }
169 
170 
171 int32
CountVisibleChoices() const172 BAutoCompleter::CountVisibleChoices() const
173 {
174 	if (fCompletionStyle && fCompletionStyle->GetChoiceView())
175 		return fCompletionStyle->GetChoiceView()->CountVisibleChoices();
176 	else
177 		return 0;
178 }
179 
180 
181 int32
SelectedChoiceIndex() const182 BAutoCompleter::SelectedChoiceIndex() const
183 {
184 	if (fCompletionStyle)
185 		return fCompletionStyle->SelectedChoiceIndex();
186 	else
187 		return -1;
188 }
189 
190 
191 void
ApplyChoice(bool hideChoices)192 BAutoCompleter::ApplyChoice(bool hideChoices)
193 {
194 	if (fCompletionStyle)
195 		fCompletionStyle->ApplyChoice(hideChoices);
196 }
197 
198 
199 void
CancelChoice()200 BAutoCompleter::CancelChoice()
201 {
202 	if (fCompletionStyle)
203 		fCompletionStyle->CancelChoice();
204 }
205 
206 
207 void
EditViewStateChanged(bool updateChoices)208 BAutoCompleter::EditViewStateChanged(bool updateChoices)
209 {
210 	if (fCompletionStyle)
211 		fCompletionStyle->EditViewStateChanged(updateChoices);
212 }
213 
214 
215 void
SetEditView(EditView * view)216 BAutoCompleter::SetEditView(EditView* view)
217 {
218 	if (fCompletionStyle)
219 		fCompletionStyle->SetEditView(view);
220 }
221 
222 
223 void
SetPatternSelector(PatternSelector * selector)224 BAutoCompleter::SetPatternSelector(PatternSelector* selector)
225 {
226 	if (fCompletionStyle)
227 		fCompletionStyle->SetPatternSelector(selector);
228 }
229 
230 
231 void
SetChoiceModel(ChoiceModel * model)232 BAutoCompleter::SetChoiceModel(ChoiceModel* model)
233 {
234 	if (fCompletionStyle)
235 		fCompletionStyle->SetChoiceModel(model);
236 }
237 
238 
239 void
SetChoiceView(ChoiceView * view)240 BAutoCompleter::SetChoiceView(ChoiceView* view)
241 {
242 	if (fCompletionStyle)
243 		fCompletionStyle->SetChoiceView(view);
244 }
245 
246 
247 void
SetCompletionStyle(CompletionStyle * style)248 BAutoCompleter::SetCompletionStyle(CompletionStyle* style)
249 {
250 	delete fCompletionStyle;
251 	fCompletionStyle = style;
252 }
253 
254