vfs: check userland buffers before reading them.
[haiku.git] / src / apps / webpositive / autocompletion / AutoCompleter.cpp
blob09060512f69cf6739ba4802458fc70fca180a79e
1 /*
2 * Copyright 2002-2006, project beam (http://sourceforge.net/projects/beam).
3 * All rights reserved. Distributed under the terms of the MIT License.
5 * Authors:
6 * Oliver Tappe <beam@hirschkaefer.de>
7 */
9 #include "AutoCompleter.h"
11 #include <Looper.h>
12 #include <Message.h>
14 #include "AutoCompleterDefaultImpl.h"
17 // #pragma mark - DefaultPatternSelector
20 class DefaultPatternSelector : public BAutoCompleter::PatternSelector {
21 public:
22 virtual void SelectPatternBounds(const BString& text, int32 caretPos,
23 int32* start, int32* length);
27 void
28 DefaultPatternSelector::SelectPatternBounds(const BString& text,
29 int32 caretPos, int32* start, int32* length)
31 if (!start || !length)
32 return;
33 *start = 0;
34 *length = text.Length();
38 // #pragma mark - CompletionStyle
41 BAutoCompleter::CompletionStyle::CompletionStyle(EditView* editView,
42 ChoiceModel* choiceModel, ChoiceView* choiceView,
43 PatternSelector* patternSelector)
45 fEditView(editView),
46 fPatternSelector(patternSelector ? patternSelector
47 : new DefaultPatternSelector()),
48 fChoiceModel(choiceModel),
49 fChoiceView(choiceView)
54 BAutoCompleter::CompletionStyle::~CompletionStyle()
56 delete fEditView;
57 delete fChoiceModel;
58 delete fChoiceView;
59 delete fPatternSelector;
63 void
64 BAutoCompleter::CompletionStyle::SetEditView(EditView* view)
66 delete fEditView;
67 fEditView = view;
71 void
72 BAutoCompleter::CompletionStyle::SetPatternSelector(
73 PatternSelector* selector)
75 delete fPatternSelector;
76 fPatternSelector = selector;
80 void
81 BAutoCompleter::CompletionStyle::SetChoiceModel(ChoiceModel* model)
83 delete fChoiceModel;
84 fChoiceModel = model;
88 void
89 BAutoCompleter::CompletionStyle::SetChoiceView(ChoiceView* view)
91 delete fChoiceView;
92 fChoiceView = view;
96 // #pragma mark - BAutoCompleter
99 BAutoCompleter::BAutoCompleter(CompletionStyle* completionStyle)
101 fCompletionStyle(completionStyle)
106 BAutoCompleter::BAutoCompleter(EditView* editView, ChoiceModel* choiceModel,
107 ChoiceView* choiceView, PatternSelector* patternSelector)
109 fCompletionStyle(new BDefaultCompletionStyle(editView, choiceModel,
110 choiceView, patternSelector))
115 BAutoCompleter::~BAutoCompleter()
117 delete fCompletionStyle;
121 bool
122 BAutoCompleter::Select(int32 index)
124 if (fCompletionStyle)
125 return fCompletionStyle->Select(index);
126 else
127 return false;
131 bool
132 BAutoCompleter::SelectNext(bool wrap)
134 if (fCompletionStyle)
135 return fCompletionStyle->SelectNext(wrap);
136 else
137 return false;
141 bool
142 BAutoCompleter::SelectPrevious(bool wrap)
144 if (fCompletionStyle)
145 return fCompletionStyle->SelectPrevious(wrap);
146 else
147 return false;
151 bool
152 BAutoCompleter::IsChoiceSelected() const
154 if (fCompletionStyle)
155 return fCompletionStyle->IsChoiceSelected();
156 else
157 return false;
161 int32
162 BAutoCompleter::CountChoices() const
164 if (fCompletionStyle && fCompletionStyle->GetChoiceModel())
165 return fCompletionStyle->GetChoiceModel()->CountChoices();
166 else
167 return 0;
171 int32
172 BAutoCompleter::CountVisibleChoices() const
174 if (fCompletionStyle && fCompletionStyle->GetChoiceView())
175 return fCompletionStyle->GetChoiceView()->CountVisibleChoices();
176 else
177 return 0;
181 int32
182 BAutoCompleter::SelectedChoiceIndex() const
184 if (fCompletionStyle)
185 return fCompletionStyle->SelectedChoiceIndex();
186 else
187 return -1;
191 void
192 BAutoCompleter::ApplyChoice(bool hideChoices)
194 if (fCompletionStyle)
195 fCompletionStyle->ApplyChoice(hideChoices);
199 void
200 BAutoCompleter::CancelChoice()
202 if (fCompletionStyle)
203 fCompletionStyle->CancelChoice();
207 void
208 BAutoCompleter::EditViewStateChanged(bool updateChoices)
210 if (fCompletionStyle)
211 fCompletionStyle->EditViewStateChanged(updateChoices);
215 void
216 BAutoCompleter::SetEditView(EditView* view)
218 if (fCompletionStyle)
219 fCompletionStyle->SetEditView(view);
223 void
224 BAutoCompleter::SetPatternSelector(PatternSelector* selector)
226 if (fCompletionStyle)
227 fCompletionStyle->SetPatternSelector(selector);
231 void
232 BAutoCompleter::SetChoiceModel(ChoiceModel* model)
234 if (fCompletionStyle)
235 fCompletionStyle->SetChoiceModel(model);
239 void
240 BAutoCompleter::SetChoiceView(ChoiceView* view)
242 if (fCompletionStyle)
243 fCompletionStyle->SetChoiceView(view);
247 void
248 BAutoCompleter::SetCompletionStyle(CompletionStyle* style)
250 delete fCompletionStyle;
251 fCompletionStyle = style;