xref: /haiku/src/apps/webpositive/autocompletion/AutoCompleter.h (revision 25a7b01d15612846f332751841da3579db313082)
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:
Choice(const BString & choiceText,const BString & displayText,int32 matchPos,int32 matchLen)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 								}
~Choice()30fd5768c4Sstippi 		virtual					~Choice() {}
Text()31fd5768c4Sstippi 				const BString&	Text() const { return fText; }
DisplayText()32fd5768c4Sstippi 				const BString&	DisplayText() const { return fDisplayText; }
MatchPos()33fd5768c4Sstippi 				int32			MatchPos() const { return fMatchPos; }
MatchLen()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:
~EditView()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:
~PatternSelector()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 
~ChoiceModel()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:
~ChoiceView()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;
86*37876e4bSstippi 		virtual int32			CountVisibleChoices() const = 0;
87fd5768c4Sstippi 	};
88fd5768c4Sstippi 
89fd5768c4Sstippi 	class CompletionStyle {
90fd5768c4Sstippi 	public:
91fd5768c4Sstippi 								CompletionStyle(EditView* editView,
92fd5768c4Sstippi 									ChoiceModel* choiceModel,
93fd5768c4Sstippi 									ChoiceView* choiceView,
94fd5768c4Sstippi 									PatternSelector* patternSelector);
95fd5768c4Sstippi 		virtual					~CompletionStyle();
96fd5768c4Sstippi 
97fd5768c4Sstippi 		virtual	bool			Select(int32 index) = 0;
98fd5768c4Sstippi 		virtual	bool			SelectNext(bool wrap = false) = 0;
99fd5768c4Sstippi 		virtual	bool			SelectPrevious(bool wrap = false) = 0;
1001a52acbcSstippi 		virtual	bool			IsChoiceSelected() const = 0;
101*37876e4bSstippi 		virtual	int32			SelectedChoiceIndex() const = 0;
102fd5768c4Sstippi 
103fd5768c4Sstippi 		virtual	void			ApplyChoice(bool hideChoices = true) = 0;
104fd5768c4Sstippi 		virtual	void			CancelChoice() = 0;
105fd5768c4Sstippi 
10690a69c8eSstippi 		virtual	void			EditViewStateChanged(bool updateChoices) = 0;
107fd5768c4Sstippi 
108fd5768c4Sstippi 				void			SetEditView(EditView* view);
109fd5768c4Sstippi 				void			SetPatternSelector(PatternSelector* selector);
110fd5768c4Sstippi 				void			SetChoiceModel(ChoiceModel* model);
111fd5768c4Sstippi 				void			SetChoiceView(ChoiceView* view);
112fd5768c4Sstippi 
GetEditView()113fd5768c4Sstippi 				EditView*		GetEditView() { return fEditView; }
GetPatternSelector()114fd5768c4Sstippi 				PatternSelector* GetPatternSelector()
115fd5768c4Sstippi 									{ return fPatternSelector; }
GetChoiceModel()116fd5768c4Sstippi 				ChoiceModel*	GetChoiceModel() { return fChoiceModel; }
GetChoiceView()117fd5768c4Sstippi 				ChoiceView*		GetChoiceView() { return fChoiceView; }
118fd5768c4Sstippi 
119fd5768c4Sstippi 	protected:
120fd5768c4Sstippi 				EditView*		fEditView;
121fd5768c4Sstippi 				PatternSelector* fPatternSelector;
122fd5768c4Sstippi 				ChoiceModel*	fChoiceModel;
123fd5768c4Sstippi 				ChoiceView*		fChoiceView;
124fd5768c4Sstippi 	};
125fd5768c4Sstippi 
126fd5768c4Sstippi protected:
127fd5768c4Sstippi 								BAutoCompleter(
128fd5768c4Sstippi 									CompletionStyle* completionStyle = NULL);
129fd5768c4Sstippi 								BAutoCompleter(EditView* editView,
130fd5768c4Sstippi 									ChoiceModel* choiceModel,
131fd5768c4Sstippi 									ChoiceView* choiceView,
132fd5768c4Sstippi 									PatternSelector* patternSelector);
133fd5768c4Sstippi 	virtual						~BAutoCompleter();
134fd5768c4Sstippi 
13590a69c8eSstippi 			void				EditViewStateChanged(
13690a69c8eSstippi 									bool updateChoices = true);
137fd5768c4Sstippi 
138fd5768c4Sstippi 			bool				Select(int32 index);
139fd5768c4Sstippi 			bool				SelectNext(bool wrap = false);
140fd5768c4Sstippi 			bool				SelectPrevious(bool wrap = false);
1411a52acbcSstippi 			bool				IsChoiceSelected() const;
142*37876e4bSstippi 			int32				CountChoices() const;
143*37876e4bSstippi 			int32				CountVisibleChoices() const;
144*37876e4bSstippi 			int32				SelectedChoiceIndex() const;
145fd5768c4Sstippi 
146fd5768c4Sstippi 			void				ApplyChoice(bool hideChoices = true);
147fd5768c4Sstippi 			void				CancelChoice();
148fd5768c4Sstippi 
149fd5768c4Sstippi 			void				SetEditView(EditView* view);
150fd5768c4Sstippi 			void				SetPatternSelector(PatternSelector* selector);
151fd5768c4Sstippi 			void				SetChoiceModel(ChoiceModel* model);
152fd5768c4Sstippi 			void				SetChoiceView(ChoiceView* view);
153fd5768c4Sstippi 
154fd5768c4Sstippi 			void				SetCompletionStyle(CompletionStyle* style);
155fd5768c4Sstippi 
156fd5768c4Sstippi private:
157fd5768c4Sstippi 			CompletionStyle*	fCompletionStyle;
158fd5768c4Sstippi };
159fd5768c4Sstippi 
160fd5768c4Sstippi 
161572a4657Sstippi #endif // _AUTO_COMPLETER_H
162