aboutsummaryrefslogtreecommitdiff
path: root/src/FbTk/TypeAhead.hh
diff options
context:
space:
mode:
authormarkt <markt>2007-03-03 19:35:34 (GMT)
committermarkt <markt>2007-03-03 19:35:34 (GMT)
commita233229bd854d2e925ca0f1e86846ff9fde46fcd (patch)
treea30fffa38994e8ee12096c31a256ba6b3fbfa2c6 /src/FbTk/TypeAhead.hh
parentd6a7bd786fd657e16c1ebad5c515d60ba1368d8a (diff)
downloadfluxbox-a233229bd854d2e925ca0f1e86846ff9fde46fcd.zip
fluxbox-a233229bd854d2e925ca0f1e86846ff9fde46fcd.tar.bz2
added support for typeahead in menus
Diffstat (limited to 'src/FbTk/TypeAhead.hh')
-rw-r--r--src/FbTk/TypeAhead.hh174
1 files changed, 174 insertions, 0 deletions
diff --git a/src/FbTk/TypeAhead.hh b/src/FbTk/TypeAhead.hh
new file mode 100644
index 0000000..15246b9
--- /dev/null
+++ b/src/FbTk/TypeAhead.hh
@@ -0,0 +1,174 @@
1// TypeAhead.hh for FbTk - Fluxbox Toolkit
2// Copyright (c) 2007 Fluxbox Team (fluxgen at fluxbox dot org)
3//
4// Permission is hereby granted, free of charge, to any person obtaining a
5// copy of this software and associated documentation files (the "Software"),
6// to deal in the Software without restriction, including without limitation
7// the rights to use, copy, modify, merge, publish, distribute, sublicense,
8// and/or sell copies of the Software, and to permit persons to whom the
9// Software is furnished to do so, subject to the following conditions:
10//
11// The above copyright notice and this permission notice shall be included in
12// all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20// DEALINGS IN THE SOFTWARE.
21
22#ifndef FBTK_TYPEAHEAD_HH
23#define FBTK_TYPEAHEAD_HH
24
25#include "ITypeAheadable.hh"
26#include <vector>
27#include "SearchResult.hh"
28
29namespace FbTk {
30
31template <typename Items, typename Item_Type>
32class TypeAhead {
33/*
34
35a class template can't be split into separate interface + implementation files, an interface summary is given here:
36
37public:
38 void init(Items const &items);
39
40// accessors:
41 inline int stringSize() const { return m_searchstr.size(); }
42 Items matched() const;
43
44// modifiers:
45 Items putCharacter(char ch);
46 void putBackSpace();
47 void reset()
48
49private:
50 SearchResults m_search_results;
51 std::string m_searchstr;
52 Items const *m_ref;
53
54// helper
55 void fillValues(BaseItems const &search, ValueVec &fillin) const;
56
57// reverts to searchstate before current
58 void revert();
59
60// search performs iteration and sets state
61 void search(char char_to_test);
62 void doSearch(char to_test,
63 Items const &items,
64 SearchResult &mySearchResult) const;
65 void doSearch(char to_test,
66 BaseItems const &search,
67 SearchResult &mySearchResult) const;
68*/
69
70public:
71 typedef std::vector < ITypeAheadable* > BaseItems;
72 typedef BaseItems::const_iterator BaseItemscIt;
73 typedef std::vector < SearchResult > SearchResults;
74 typedef typename Items::const_iterator ItemscIt;
75
76 void init(Items const &items) { m_ref = &items; }
77
78 inline size_t stringSize() const { return m_searchstr.size(); }
79
80 void seek() {
81 if (!m_search_results.empty())
82 m_searchstr = m_search_results.back().seekedString();
83 }
84
85 Items putCharacter(char ch) {
86 if (isprint(ch))
87 search(ch);
88 return matched();
89 }
90
91 void putBackSpace() {
92 if (!m_search_results.empty())
93 revert();
94 }
95
96 void reset() {
97 m_searchstr.clear();
98 m_search_results.clear();
99 }
100
101 Items matched() const {
102 Items last_matched;
103
104 if (!m_search_results.empty())
105 fillValues(m_search_results.back().result(), last_matched);
106 return last_matched;
107 }
108
109private:
110 SearchResults m_search_results;
111 std::string m_searchstr;
112 Items const *m_ref; // reference to vector we are operating on
113
114 void fillValues(BaseItems const &search, Items &fillin) const {
115 for (BaseItemscIt it = search.begin(); it != search.end(); it++) {
116 Item_Type tmp = dynamic_cast<Item_Type>(*it);
117 if (tmp)
118 fillin.push_back(tmp);
119 }
120 }
121
122 void revert() {
123 m_search_results.pop_back();
124 if (m_search_results.empty())
125 m_searchstr.clear();
126 else
127 m_searchstr = m_search_results.back().seekedString();
128 }
129
130 void search(char char_to_test) {
131 SearchResult mySearchResult(m_searchstr + char_to_test);
132 size_t num_items = m_ref->size();
133
134 // check if we have already a searched set
135 if (m_search_results.empty())
136 doSearch(char_to_test, *m_ref, mySearchResult);
137 else {
138 num_items = m_search_results.back().size();
139 doSearch(char_to_test, m_search_results.back().result(),
140 mySearchResult);
141 }
142
143 if (mySearchResult.size() > 0 ) {
144 if (mySearchResult.size() < num_items) {
145 mySearchResult.seek();
146 m_search_results.push_back(mySearchResult);
147 }
148 m_searchstr += char_to_test;
149 }
150 }
151
152 // iteration based on original list of items
153 void doSearch(char to_test, Items const &items,
154 SearchResult &mySearchResult) const {
155 for (ItemscIt it = items.begin(); it != items.end(); it++) {
156 if ((*it)->iTypeCompareChar(to_test, stringSize()) && (*it)->isEnabled())
157 mySearchResult.add(*it);
158 }
159 }
160
161 // iteration based on last SearchResult
162 void doSearch(char to_test, BaseItems const &search,
163 SearchResult &mySearchResult) const {
164 for (BaseItemscIt it = search.begin(); it != search.end(); it++) {
165 if ((*it)->iTypeCompareChar(to_test, stringSize()) && (*it)->isEnabled())
166 mySearchResult.add(*it);
167 }
168 }
169
170}; // end Class TypeAhead
171
172} // end namespace FbTk
173
174#endif // FBTK_TYPEAHEAD_HH