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