xref: /haiku/src/apps/webpositive/autocompletion/AutoCompleter.h (revision 90a69c8e1d42bbe82b16a9caadad3fefca92f655)
1fd5768c4Sstippi /*
2fd5768c4Sstippi  * Copyright 2002-2006, project beam (http://sourceforge.net/projects/beam).
3fd5768c4Sstippi  * All rights reserved. Distributed under the terms of the MIT License.
4fd5768c4Sstippi  *
5fd5768c4Sstippi  * Authors:
6fd5768c4Sstippi  *		Oliver Tappe <beam@hirschkaefer.de>
7fd5768c4Sstippi  */
8572a4657Sstippi #ifndef _AUTO_COMPLETER_H
9572a4657Sstippi #define _AUTO_COMPLETER_H
10fd5768c4Sstippi 
11fd5768c4Sstippi #include <MessageFilter.h>
12fd5768c4Sstippi 
13fd5768c4Sstippi #include <Rect.h>
14fd5768c4Sstippi #include <String.h>
15fd5768c4Sstippi 
16fd5768c4Sstippi class BAutoCompleter {
17fd5768c4Sstippi public:
18fd5768c4Sstippi 	class Choice {
19fd5768c4Sstippi 	public:
20fd5768c4Sstippi 								Choice(const BString& choiceText,
21fd5768c4Sstippi 									const BString& displayText, int32 matchPos,
22fd5768c4Sstippi 									int32 matchLen)
23fd5768c4Sstippi 									:
24fd5768c4Sstippi 									fText(choiceText),
25fd5768c4Sstippi 									fDisplayText(displayText),
26fd5768c4Sstippi 									fMatchPos(matchPos),
27fd5768c4Sstippi 									fMatchLen(matchLen)
28fd5768c4Sstippi 								{
29fd5768c4Sstippi 								}
30fd5768c4Sstippi 		virtual					~Choice() {}
31fd5768c4Sstippi 				const BString&	Text() const { return fText; }
32fd5768c4Sstippi 				const BString&	DisplayText() const { return fDisplayText; }
33fd5768c4Sstippi 				int32			MatchPos() const { return fMatchPos; }
34fd5768c4Sstippi 				int32			MatchLen() const { return fMatchLen; }
35fd5768c4Sstippi 
36fd5768c4Sstippi 	private:
37fd5768c4Sstippi 				BString			fText;
38fd5768c4Sstippi 				BString			fDisplayText;
39fd5768c4Sstippi 				int32			fMatchPos;
40fd5768c4Sstippi 				int32			fMatchLen;
41fd5768c4Sstippi 	};
42fd5768c4Sstippi 
43fd5768c4Sstippi 	class EditView {
44fd5768c4Sstippi 	public:
45fd5768c4Sstippi 		virtual					~EditView()	{}
46fd5768c4Sstippi 
47fd5768c4Sstippi 		virtual	BRect			GetAdjustmentFrame() = 0;
48fd5768c4Sstippi 		virtual	void			GetEditViewState(BString& text,
49fd5768c4Sstippi 									int32* caretPos) = 0;
50fd5768c4Sstippi 		virtual	void			SetEditViewState(const BString& text,
51fd5768c4Sstippi 									int32 caretPos,
52fd5768c4Sstippi 									int32 selectionLength = 0) = 0;
53fd5768c4Sstippi 	};
54fd5768c4Sstippi 
55fd5768c4Sstippi 	class PatternSelector {
56fd5768c4Sstippi 	public:
57fd5768c4Sstippi 		virtual					~PatternSelector() {}
58fd5768c4Sstippi 
59fd5768c4Sstippi 		virtual	void			SelectPatternBounds(const BString& text,
60fd5768c4Sstippi 									int32 caretPos, int32* start,
61fd5768c4Sstippi 									int32* length) = 0;
62fd5768c4Sstippi 	};
63fd5768c4Sstippi 
64fd5768c4Sstippi 	class ChoiceModel {
65fd5768c4Sstippi 	public:
66fd5768c4Sstippi 
67fd5768c4Sstippi 		virtual					~ChoiceModel() {}
68fd5768c4Sstippi 
69fd5768c4Sstippi 		virtual	void			FetchChoicesFor(const BString& pattern) = 0;
70fd5768c4Sstippi 
71fd5768c4Sstippi 		virtual	int32			CountChoices() const = 0;
72fd5768c4Sstippi 		virtual	const Choice*	ChoiceAt(int32 index) const = 0;
73fd5768c4Sstippi 	};
74fd5768c4Sstippi 
75fd5768c4Sstippi 	class CompletionStyle;
76fd5768c4Sstippi 	class ChoiceView {
77fd5768c4Sstippi 	public:
78fd5768c4Sstippi 		virtual					~ChoiceView() {}
79fd5768c4Sstippi 
80fd5768c4Sstippi 		virtual	void			SelectChoiceAt(int32 index) = 0;
81fd5768c4Sstippi 		virtual	void			ShowChoices(
82fd5768c4Sstippi 									BAutoCompleter::CompletionStyle* completer)
83fd5768c4Sstippi 									= 0;
84fd5768c4Sstippi 		virtual	void			HideChoices() = 0;
85fd5768c4Sstippi 		virtual	bool			ChoicesAreShown() = 0;
86fd5768c4Sstippi 	};
87fd5768c4Sstippi 
88fd5768c4Sstippi 	class CompletionStyle {
89fd5768c4Sstippi 	public:
90fd5768c4Sstippi 								CompletionStyle(EditView* editView,
91fd5768c4Sstippi 									ChoiceModel* choiceModel,
92fd5768c4Sstippi 									ChoiceView* choiceView,
93fd5768c4Sstippi 									PatternSelector* patternSelector);
94fd5768c4Sstippi 		virtual					~CompletionStyle();
95fd5768c4Sstippi 
96fd5768c4Sstippi 		virtual	bool			Select(int32 index) = 0;
97fd5768c4Sstippi 		virtual	bool			SelectNext(bool wrap = false) = 0;
98fd5768c4Sstippi 		virtual	bool			SelectPrevious(bool wrap = false) = 0;
991a52acbcSstippi 		virtual	bool			IsChoiceSelected() const = 0;
100fd5768c4Sstippi 
101fd5768c4Sstippi 		virtual	void			ApplyChoice(bool hideChoices = true) = 0;
102fd5768c4Sstippi 		virtual	void			CancelChoice() = 0;
103fd5768c4Sstippi 
104*90a69c8eSstippi 		virtual	void			EditViewStateChanged(bool updateChoices) = 0;
105fd5768c4Sstippi 
106fd5768c4Sstippi 				void			SetEditView(EditView* view);
107fd5768c4Sstippi 				void			SetPatternSelector(PatternSelector* selector);
108fd5768c4Sstippi 				void			SetChoiceModel(ChoiceModel* model);
109fd5768c4Sstippi 				void			SetChoiceView(ChoiceView* view);
110fd5768c4Sstippi 
111fd5768c4Sstippi 				EditView*		GetEditView() { return fEditView; }
112fd5768c4Sstippi 				PatternSelector* GetPatternSelector()
113fd5768c4Sstippi 									{ return fPatternSelector; }
114fd5768c4Sstippi 				ChoiceModel*	GetChoiceModel() { return fChoiceModel; }
115fd5768c4Sstippi 				ChoiceView*		GetChoiceView() { return fChoiceView; }
116fd5768c4Sstippi 
117fd5768c4Sstippi 	protected:
118fd5768c4Sstippi 				EditView*		fEditView;
119fd5768c4Sstippi 				PatternSelector* fPatternSelector;
120fd5768c4Sstippi 				ChoiceModel*	fChoiceModel;
121fd5768c4Sstippi 				ChoiceView*		fChoiceView;
122fd5768c4Sstippi 	};
123fd5768c4Sstippi 
124fd5768c4Sstippi protected:
125fd5768c4Sstippi 								BAutoCompleter(
126fd5768c4Sstippi 									CompletionStyle* completionStyle = NULL);
127fd5768c4Sstippi 								BAutoCompleter(EditView* editView,
128fd5768c4Sstippi 									ChoiceModel* choiceModel,
129fd5768c4Sstippi 									ChoiceView* choiceView,
130fd5768c4Sstippi 									PatternSelector* patternSelector);
131fd5768c4Sstippi 	virtual						~BAutoCompleter();
132fd5768c4Sstippi 
133*90a69c8eSstippi 			void				EditViewStateChanged(
134*90a69c8eSstippi 									bool updateChoices = true);
135fd5768c4Sstippi 
136fd5768c4Sstippi 			bool				Select(int32 index);
137fd5768c4Sstippi 			bool				SelectNext(bool wrap = false);
138fd5768c4Sstippi 			bool				SelectPrevious(bool wrap = false);
1391a52acbcSstippi 			bool				IsChoiceSelected() const;
140fd5768c4Sstippi 
141fd5768c4Sstippi 			void				ApplyChoice(bool hideChoices = true);
142fd5768c4Sstippi 			void				CancelChoice();
143fd5768c4Sstippi 
144fd5768c4Sstippi 			void				SetEditView(EditView* view);
145fd5768c4Sstippi 			void				SetPatternSelector(PatternSelector* selector);
146fd5768c4Sstippi 			void				SetChoiceModel(ChoiceModel* model);
147fd5768c4Sstippi 			void				SetChoiceView(ChoiceView* view);
148fd5768c4Sstippi 
149fd5768c4Sstippi 			void				SetCompletionStyle(CompletionStyle* style);
150fd5768c4Sstippi 
151fd5768c4Sstippi private:
152fd5768c4Sstippi 			CompletionStyle*	fCompletionStyle;
153fd5768c4Sstippi };
154fd5768c4Sstippi 
155fd5768c4Sstippi 
156572a4657Sstippi #endif // _AUTO_COMPLETER_H
157