1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
/*
* Dillo Widget
*
* Copyright 2005-2007 Sebastian Geerken <sgeerken@dillo.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "core.hh"
namespace dw {
namespace core {
FindtextState::FindtextState ()
{
key = NULL;
nexttab = NULL;
widget = NULL;
iterator = NULL;
hlIterator = NULL;
}
FindtextState::~FindtextState ()
{
if (key)
delete key;
if (nexttab)
delete[] nexttab;
if (iterator)
delete iterator;
if (hlIterator)
delete hlIterator;
}
void FindtextState::setWidget (Widget *widget)
{
this->widget = widget;
// A widget change will restart the search.
if (key)
delete key;
key = NULL;
if (nexttab)
delete[] nexttab;
nexttab = NULL;
if (iterator)
delete iterator;
iterator = NULL;
if (hlIterator)
delete hlIterator;
hlIterator = NULL;
}
FindtextState::Result FindtextState::search (const char *key, bool caseSens)
{
if (!widget || *key == 0) // empty keys are not found
return NOT_FOUND;
bool wasHighlighted = unhighlight ();
bool newKey;
// If the key (or the widget) changes (including case sensitivity),
// the search is started from the beginning.
if (this->key == NULL || this->caseSens != caseSens ||
strcmp (this->key, key) != 0) {
newKey = true;
if (this->key)
delete this->key;
this->key = strdup (key);
this->caseSens = caseSens;
if (nexttab)
delete[] nexttab;
nexttab = createNexttab (key, caseSens);
if (iterator)
delete iterator;
iterator = new CharIterator (widget);
iterator->next ();
} else
newKey = false;
bool firstTrial = !wasHighlighted || newKey;
if (search0 ()) {
// Highlighlighting is done with a clone.
hlIterator = iterator->cloneCharIterator ();
for (int i = 0; key[i]; i++)
hlIterator->next ();
CharIterator::highlight (iterator, hlIterator, HIGHLIGHT_FINDTEXT);
CharIterator::scrollTo (iterator, hlIterator,
HPOS_INTO_VIEW, VPOS_CENTER);
// The search will continue from the word after the found position.
iterator->next ();
return SUCCESS;
} else {
if (firstTrial)
return NOT_FOUND;
else {
// Nothing found anymore, reset the state for the next trial.
delete iterator;
iterator = new CharIterator (widget);
iterator->next ();
// We expect a success.
Result result2 = search (key, caseSens);
assert (result2 == SUCCESS);
return RESTART;
}
}
}
/**
* \brief This method is called when the user closes the "find text" dialog.
*/
void FindtextState::resetSearch ()
{
unhighlight ();
if (key)
delete key;
key = NULL;
}
int *FindtextState::createNexttab (const char *key, bool caseSens)
{
int i = 0;
int j = -1;
int l = strlen (key);
int *nexttab = new int[l + 1]; // + 1 is necessary for l == 1 case
nexttab[0] = -1;
do {
if (j == -1 || charsEqual (key[i], key[j], caseSens)) {
i++;
j++;
nexttab[i] = j;
//_MSG ("nexttab[%d] = %d\n", i, j);
} else
j = nexttab[j];
} while (i < l - 1);
return nexttab;
}
/**
* \brief Unhighlight, and return whether a region was highlighted.
*/
bool FindtextState::unhighlight ()
{
if (hlIterator) {
CharIterator *start = hlIterator->cloneCharIterator ();
for (int i = 0; key[i]; i++)
start->prev ();
CharIterator::unhighlight (start, hlIterator, HIGHLIGHT_FINDTEXT);
delete start;
delete hlIterator;
hlIterator = NULL;
return true;
} else
return false;
}
bool FindtextState::search0 ()
{
if (iterator->getChar () == CharIterator::END)
return false;
int j = 0;
bool nextit = true;
int l = strlen (key);
do {
if (j == -1 || charsEqual (iterator->getChar (), key[j], caseSens)) {
j++;
nextit = iterator->next ();
} else
j = nexttab[j];
} while (nextit && j < l);
if (j >= l) {
// Go back to where the word was found.
for (int i = 0; i < l; i++)
iterator->prev ();
return true;
} else
return false;
}
} // namespace dw
} // namespace core
|