summaryrefslogtreecommitdiff
path: root/riscos/search.c
blob: 9317e0124ece024708c1cc8b957b6016c4cee549 (plain)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
/*
 * This file is part of NetSurf, http://netsurf.sourceforge.net/
 * Licensed under the GNU General Public License,
 *                http://www.opensource.org/licenses/gpl-license
 * Copyright 2004 John M Bell <jmb202@ecs.soton.ac.uk>
 * Copyright 2005 Adrian Lees <adrianl@users.sourceforge.net> 
 */

/** \file
 * Free text search (implementation)
 */

#include <ctype.h>
#include <string.h>

#include "oslib/wimp.h"

#include "netsurf/utils/config.h"
#include "netsurf/content/content.h"
#include "netsurf/desktop/browser.h"
#include "netsurf/desktop/gui.h"
#include "netsurf/desktop/selection.h"
#include "netsurf/render/box.h"
#include "netsurf/render/html.h"
#include "netsurf/riscos/gui.h"
#include "netsurf/riscos/menus.h"
#include "netsurf/riscos/wimp.h"
#include "netsurf/utils/log.h"
#include "netsurf/utils/utils.h"


#ifdef WITH_SEARCH

#ifndef NOF_ELEMENTS
#define NOF_ELEMENTS(array) (sizeof(array)/sizeof(*(array)))
#endif

struct list_entry {
	/* start position of match */
	struct box *start_box;
	unsigned start_idx;
	/* end of match */
	struct box *end_box;
	unsigned end_idx;

	struct list_entry *prev;
	struct list_entry *next;
};

struct gui_window *search_current_window = 0;
static struct selection *search_selection = 0;

static char *search_string = 0;
static struct list_entry search_head = { 0, 0, 0, 0, 0, 0 };
static struct list_entry *search_found = &search_head;
static struct list_entry *search_current = 0;
static struct content *search_content = 0;
static bool search_prev_from_top = false;
static bool search_prev_case_sens = false;

static void start_search(void);
static void end_search(void);
static void do_search(char *string, bool from_top, bool case_sens, bool forwards);
static const char *find_pattern(const char *string, int s_len,
		const char *pattern, int p_len, bool case_sens, int *m_len);
static bool find_occurrences(const char *pattern, int p_len, struct box *cur,
		bool case_sens);
static void show_search_direction(bool forwards);


/**
 * Open the search dialog
 *
 * \param g the gui window to search
 */
void ro_gui_search_prepare(struct gui_window *g)
{
	struct content *c;

	assert(g != NULL);

	search_current_window = g;

	ro_gui_set_icon_string(dialog_search, ICON_SEARCH_TEXT, "");
	show_search_direction(true);
	ro_gui_set_icon_selected_state(dialog_search, ICON_SEARCH_START,
				false);
	ro_gui_set_icon_selected_state(dialog_search,
				ICON_SEARCH_CASE_SENSITIVE, false);

	c = search_current_window->bw->current_content;

	if (!c)
		return;

	/* only handle html contents */
	if (c->type != CONTENT_HTML)
		return;

/* \todo build me properly please! */
	search_selection = selection_create(g->bw);
	if (!search_selection)
		warn_user("NoMemory", 0);

	selection_init(search_selection, c->data.html.layout);
}

/**
 * Handle clicks in the search dialog
 *
 * \param pointer wimp_pointer block
 * \param parent The parent window of this persistent dialog
 */
void ro_gui_search_click(wimp_pointer *pointer)
{
	if (pointer->buttons == wimp_CLICK_MENU)
		return;

	switch(pointer->i) {
		case ICON_SEARCH_FORWARDS:
		case ICON_SEARCH_BACKWARDS:
			/* prevent deselection on adjust clicking */
			if (pointer->buttons == wimp_CLICK_ADJUST)
				ro_gui_set_icon_selected_state(dialog_search,
						pointer->i, true);
			break;
		case ICON_SEARCH_FIND:
			start_search();
			break;
		case ICON_SEARCH_CANCEL:
			end_search();
			break;
	}
}

/**
 * Handle keypresses in the search dialog
 *
 * \param key wimp_key block
 * \return true if keypress handled, false otherwise
 */
bool ro_gui_search_keypress(wimp_key *key)
{
	bool state;

	switch (key->c) {
		case 2: /* ctrl b */
			show_search_direction(false);
			return true;
		case 6: /* ctrl f */
			show_search_direction(true);
			return true;
		case 9: /* ctrl i */
			state = ro_gui_get_icon_selected_state(dialog_search, ICON_SEARCH_CASE_SENSITIVE);
			ro_gui_set_icon_selected_state(dialog_search, ICON_SEARCH_CASE_SENSITIVE, !state);
			return true;
		case 19: /* ctrl s */
			state = ro_gui_get_icon_selected_state(dialog_search, ICON_SEARCH_START);
			ro_gui_set_icon_selected_state(dialog_search, ICON_SEARCH_START, !state);
			return true;
		case wimp_KEY_RETURN:
			start_search();
			return true;
		case wimp_KEY_ESCAPE:
			end_search();
			return true;
		case wimp_KEY_UP:
			show_search_direction(false);
			start_search();
			return true;
		case wimp_KEY_DOWN:
			show_search_direction(true);
			start_search();
			return true;
	}

	return false;
}

/**
 * Begins the search process
 */
void start_search(void)
{
	char *string;

	string = ro_gui_get_icon_string(dialog_search, ICON_SEARCH_TEXT);
	if (strlen(string) == 0)
		return;
	do_search(string,
		ro_gui_get_icon_selected_state(dialog_search,
						ICON_SEARCH_START),
		ro_gui_get_icon_selected_state(dialog_search,
						ICON_SEARCH_CASE_SENSITIVE),
		ro_gui_get_icon_selected_state(dialog_search,
						ICON_SEARCH_FORWARDS));
}

/**
 * Ends the search process, invalidating all global state and
 * freeing the list of found boxes
 */
void end_search(void)
{
	struct list_entry *a, *b;

	if (search_selection) {
		selection_clear(search_selection, true);
		selection_destroy(search_selection);
	}
	search_selection = 0;

	search_current_window = 0;

	if (search_string)
		free(search_string);
	search_string = 0;

	for (a = search_found->next; a; a = b) {
		b = a->next;
		free(a);
	}
	search_found->prev = 0;
	search_found->next = 0;

	search_current = 0;

	search_content = 0;

	search_prev_from_top = false;
	search_prev_case_sens = false;

	/* and close the window */
	ro_gui_menu_closed();
	ro_gui_dialog_close(dialog_search);
}

/**
 * Search for a string in the box tree
 *
 * \param string the string to search for
 * \param from_top whether to display results from the top of the page, or
 *                 the current scroll position
 * \param case_sens whether to perform a case sensitive search
 * \param forwards direction to search in
 */
void do_search(char *string, bool from_top, bool case_sens, bool forwards)
{
	struct content *c;
	struct box *box;
	struct list_entry *a, *b;
	int x,y;
	bool new = false;

	if (!search_current_window)
		return;

	c = search_current_window->bw->current_content;

	if (!c)
		return;

	/* only handle html contents */
	if (c->type != CONTENT_HTML)
		return;

	box = c->data.html.layout;

	if (!box)
		return;

//	LOG(("'%s' - '%s' (%p, %p) %p (%d, %d) (%d, %d) %d", search_string, string, search_content, c, search_found->next, search_prev_from_top, from_top, search_prev_case_sens, case_sens, forwards));

	/* check if we need to start a new search or continue an old one */
	if (!search_string || c != search_content ||
	    !search_found->next || search_prev_from_top != from_top ||
	    search_prev_case_sens != case_sens ||
	    (case_sens && strcmp(string, search_string) != 0) ||
	    (!case_sens && strcasecmp(string, search_string) != 0)) {
		if (search_string)
			free(search_string);
		search_string = strdup(string);
		search_current = 0;
		for (a = search_found->next; a; a = b) {
			b = a->next;
			free(a);
		}
		search_found->prev = 0;
		search_found->next = 0;
		if (!find_occurrences(string, strlen(string), box, case_sens)) {
			for (a = search_found->next; a; a = b) {
				b = a->next;
				free(a);
			}
			search_found->prev = 0;
			search_found->next = 0;
			return;
		}
		new = true;
		search_content = c;
		search_prev_from_top = from_top;
		search_prev_case_sens = case_sens;
	}

//	LOG(("%d %p %p (%p, %p)", new, search_found->next, search_current, search_current->prev, search_current->next));

	if (!search_found->next)
		return;

	if (new && from_top) {
		/* new search, beginning at the top of the page */
		search_current = search_found->next;
	}
	else if (new) {
		/* new search, beginning from user's current scroll
		 * position */
		wimp_window_state state;
		os_error *error;

		state.w = search_current_window->window;
		error = xwimp_get_window_state(&state);
		if (error) {
			LOG(("xwimp_get_window_state: 0x%x: %s",
					error->errnum, error->errmess));
			warn_user("WimpError", error->errmess);
			return;
		}

		for (a = search_found->next; a; a = a->next) {
			box_coords(a->start_box, &x, &y);
			LOG(("%d, %d", y, state.yscroll / 2));
			if (forwards && -y <= state.yscroll / 2)
				break;
			if (!forwards && -y >= state.yscroll / 2)
				break;
		}

		if (a)
			search_current = a;
		else
			return;
	}
	else {
		/* continued search in the direction specified */
		if (forwards && search_current && search_current->next) {
			search_current = search_current->next;
		}
		else if (!forwards && search_current && search_current->prev) {
			search_current = search_current->prev;
		}
	}

	if (!search_current)
		return;

	box = search_current->start_box;

	selection_clear(search_selection, true);
	selection_set_start(search_selection, search_current->start_box,
		search_current->start_idx);
	selection_set_end(search_selection, search_current->end_box,
		search_current->end_idx);

	/* get box position and jump to it */
	box_coords(search_current->start_box, &x, &y);
//	LOG(("%p (%d, %d)", search_current, x, y));
	gui_window_set_scroll(search_current_window, x, y);

}


/**
 * Find the first occurrence of 'match' in 'string' and return its index
 *
 * /param  string     the string to be searched (unterminated)
 * /param  s_len      length of the string to be searched
 * /param  pattern    the pattern for which we are searching (unterminated)
 * /param  p_len      length of pattern
 * /param  case_sens  true iff case sensitive match required
 * /param  m_len      accepts length of match in bytes
 * /return pointer to first match, NULL if none
 */

const char *find_pattern(const char *string, int s_len, const char *pattern,
		int p_len, bool case_sens, int *m_len)
{
	struct { const char *ss, *s, *p; bool first; } context[16];
	const char *ep = pattern + p_len;
	const char *es = string  + s_len;
	const char *p = pattern - 1;  /* a virtual '*' before the pattern */
	const char *ss = string;
	const char *s = string;
	bool first = true;
	int top = 0;

	while (p < ep) {
		bool matches;
		if (p < pattern || *p == '*') {
			char ch;

			/* skip any further asterisks; one is the same as many */
			do p++; while (p < ep && *p == '*');

			/* if we're at the end of the pattern, yes, it matches */
			if (p >= ep) break;

			/* anything matches a # so continue matching from
			   here, and stack a context that will try to match
			   the wildcard against the next character */
			
			ch = *p;
			if (ch != '#') {
				/* scan forwards until we find a match for this char */
				if (!case_sens) ch = toupper(ch);
				while (s < es) {
					if (case_sens) {
						if (*s == ch) break;
					} else if (toupper(*s) == ch)
						break;
					s++;
				}
			}
			
			if (s < es) {
				/* remember where we are in case the match fails;
					we can then resume */
				if (top < (int)NOF_ELEMENTS(context)) {
					context[top].ss = ss;
					context[top].s  = s + 1;
					context[top].p  = p - 1;    /* ptr to last asterisk */
					context[top].first = first;
					top++;
				}

				if (first) {
					ss = s;  /* remember first non-'*' char */
					first = false;
				}

				matches = true;
			}
			else
				matches = false;
		}
		else if (s < es) {
			char ch = *p;
			if (ch == '#')
				matches = true;
			else {
				if (case_sens)
					matches = (*s == ch);
				else
					matches = (toupper(*s) == toupper(ch));
			}
			if (matches && first) {
				ss = s;  /* remember first non-'*' char */
				first = false;
			}
		}
		else
			matches = false;

		if (matches) {
			p++; s++;
		}
		else {
			/* doesn't match, resume with stacked context if we have one */
			if (--top < 0) return NULL;  /* no match, give up */
			
			ss = context[top].ss;
			s  = context[top].s;
			p  = context[top].p;
			first = context[top].first;
		}
	}

	/* end of pattern reached */
	*m_len = s - ss;
	return ss;
}


/**
 * Finds all occurrences of a given string in the box tree
 *
 * \param pattern   the string pattern to search for
 * \param p_len     pattern length
 * \param cur       pointer to the current box
 * \param case_sens whether to perform a case sensitive search
 * \return true on success, false on memory allocation failure
 */
bool find_occurrences(const char *pattern, int p_len, struct box *cur,
		bool case_sens)
{
	struct box *a;

	/* ignore this box, if there's no visible text */
	if (!cur->object && cur->text) {
		const char *text = cur->text;
		unsigned length = cur->length;

		while (length > 0) {
			unsigned match_length;
			unsigned match_offset;
			const char *new_text;
			struct list_entry *entry;
			const char *pos = find_pattern(text, length,
					pattern, p_len, case_sens, &match_length);
			if (!pos) break;

			/* found string in box => add to list */
			entry = calloc(1, sizeof(*entry));
			if (!entry) {
				warn_user("NoMemory", 0);
				return false;
			}
	
			match_offset = pos - cur->text;
	
			entry->start_box = cur;
			entry->start_idx = match_offset;
			entry->end_box = cur;
			entry->end_idx = match_offset + match_length;
			entry->next = 0;
			entry->prev = search_found->prev;
			if (!search_found->prev)
				search_found->next = entry;
			else
				search_found->prev->next = entry;
			search_found->prev = entry;

			new_text = pos + match_length;
			length -= (new_text - text);
			text = new_text;
		}
	}

	/* and recurse */
	for (a = cur->children; a; a = a->next) {
		if (!find_occurrences(pattern, p_len, a, case_sens))
			return false;
	}

	return true;
}



/**
 * Determines whether any portion of the given text box should be
 * selected because it matches the current search string.
 *
 * \param  g          gui window
 * \param  box        box being tested
 * \param  start_idx  byte offset within text box of highlight start
 * \param  end_idx    byte offset of highlight end
 * \return true iff part of the box should be highlighted
 */

bool gui_search_term_highlighted(struct gui_window *g, struct box *box,
		unsigned *start_idx, unsigned *end_idx) 
{
	if (g == search_current_window && search_selection) {
		if (selection_defined(search_selection))
			return selection_highlighted(search_selection, box, start_idx, end_idx);
	}
	return false;
}


/**
 * Change the displayed search direction.
 *
 * \param forwards  true for forwards, else backwards
 */

void show_search_direction(bool forwards)
{
	ro_gui_set_icon_selected_state(dialog_search, ICON_SEARCH_FORWARDS, forwards);
	ro_gui_set_icon_selected_state(dialog_search, ICON_SEARCH_BACKWARDS, !forwards);
}

#endif