2004-08-06 00:32:00 +04:00
|
|
|
/*
|
|
|
|
* 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>
|
2005-04-17 07:30:35 +04:00
|
|
|
* Copyright 2005 Adrian Lees <adrianl@users.sourceforge.net>
|
2004-08-06 00:32:00 +04:00
|
|
|
*/
|
|
|
|
|
2004-08-06 02:03:56 +04:00
|
|
|
/** \file
|
|
|
|
* Free text search (implementation)
|
|
|
|
*/
|
|
|
|
|
2005-03-25 17:25:25 +03:00
|
|
|
#include <ctype.h>
|
2004-08-06 00:32:00 +04:00
|
|
|
#include <string.h>
|
|
|
|
|
2005-07-19 07:56:17 +04:00
|
|
|
#include "oslib/hourglass.h"
|
2004-08-06 00:32:00 +04:00
|
|
|
#include "oslib/wimp.h"
|
|
|
|
|
|
|
|
#include "netsurf/utils/config.h"
|
2004-08-06 02:38:26 +04:00
|
|
|
#include "netsurf/content/content.h"
|
2004-08-06 00:32:00 +04:00
|
|
|
#include "netsurf/desktop/browser.h"
|
|
|
|
#include "netsurf/desktop/gui.h"
|
2005-04-17 07:30:35 +04:00
|
|
|
#include "netsurf/desktop/selection.h"
|
2004-08-06 00:32:00 +04:00
|
|
|
#include "netsurf/render/box.h"
|
|
|
|
#include "netsurf/render/html.h"
|
2005-12-31 07:42:17 +03:00
|
|
|
#include "netsurf/riscos/dialog.h"
|
2005-04-08 00:46:22 +04:00
|
|
|
#include "netsurf/riscos/menus.h"
|
2004-08-06 00:32:00 +04:00
|
|
|
#include "netsurf/riscos/wimp.h"
|
2005-12-31 07:42:17 +03:00
|
|
|
#include "netsurf/riscos/wimp_event.h"
|
2004-08-06 00:32:00 +04:00
|
|
|
#include "netsurf/utils/log.h"
|
2005-07-30 19:11:23 +04:00
|
|
|
#include "netsurf/utils/messages.h"
|
2004-08-06 00:32:00 +04:00
|
|
|
#include "netsurf/utils/utils.h"
|
|
|
|
|
|
|
|
#ifdef WITH_SEARCH
|
|
|
|
|
2005-03-25 17:25:25 +03:00
|
|
|
#ifndef NOF_ELEMENTS
|
|
|
|
#define NOF_ELEMENTS(array) (sizeof(array)/sizeof(*(array)))
|
|
|
|
#endif
|
|
|
|
|
2004-08-06 00:32:00 +04:00
|
|
|
struct list_entry {
|
2005-04-17 07:30:35 +04:00
|
|
|
/* start position of match */
|
|
|
|
struct box *start_box;
|
|
|
|
unsigned start_idx;
|
|
|
|
/* end of match */
|
|
|
|
struct box *end_box;
|
|
|
|
unsigned end_idx;
|
|
|
|
|
2006-02-13 00:23:22 +03:00
|
|
|
struct selection *sel;
|
|
|
|
|
2004-08-06 00:32:00 +04:00
|
|
|
struct list_entry *prev;
|
|
|
|
struct list_entry *next;
|
|
|
|
};
|
|
|
|
|
2005-04-17 07:30:35 +04:00
|
|
|
struct gui_window *search_current_window = 0;
|
2004-08-06 02:03:56 +04:00
|
|
|
|
2004-08-06 00:32:00 +04:00
|
|
|
static char *search_string = 0;
|
2006-02-13 00:23:22 +03:00
|
|
|
static struct list_entry search_head = { 0, 0, 0, 0, 0, 0, 0 };
|
2004-08-06 02:03:56 +04:00
|
|
|
static struct list_entry *search_found = &search_head;
|
|
|
|
static struct list_entry *search_current = 0;
|
2004-08-06 02:38:26 +04:00
|
|
|
static struct content *search_content = 0;
|
2004-08-06 02:03:56 +04:00
|
|
|
static bool search_prev_case_sens = false;
|
|
|
|
|
2005-12-31 07:42:17 +03:00
|
|
|
#define RECENT_SEARCHES 8
|
|
|
|
bool search_insert;
|
|
|
|
static char *recent_search[RECENT_SEARCHES];
|
|
|
|
static wimp_MENU(RECENT_SEARCHES) menu_recent;
|
|
|
|
wimp_menu *recent_search_menu = (wimp_menu *)&menu_recent;
|
|
|
|
#define DEFAULT_FLAGS (wimp_ICON_TEXT | wimp_ICON_FILLED | \
|
|
|
|
(wimp_COLOUR_BLACK << wimp_ICON_FG_COLOUR_SHIFT) | \
|
|
|
|
(wimp_COLOUR_WHITE << wimp_ICON_BG_COLOUR_SHIFT))
|
|
|
|
|
|
|
|
|
2005-07-19 07:56:17 +04:00
|
|
|
static void start_search(bool forwards);
|
|
|
|
static void do_search(char *string, int string_len, bool case_sens, bool forwards);
|
2005-04-17 07:30:35 +04:00
|
|
|
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);
|
2006-02-13 00:23:22 +03:00
|
|
|
static void free_matches(void);
|
|
|
|
static void show_all(bool all);
|
2005-07-19 07:56:17 +04:00
|
|
|
static void show_status(bool found);
|
2004-08-06 00:32:00 +04:00
|
|
|
|
2005-12-31 07:42:17 +03:00
|
|
|
static bool ro_gui_search_next(wimp_w w);
|
|
|
|
static bool ro_gui_search_click(wimp_pointer *pointer);
|
|
|
|
static bool ro_gui_search_keypress(wimp_key *key);
|
|
|
|
static void ro_gui_search_add_recent(char *search);
|
|
|
|
|
|
|
|
void ro_gui_search_init(void) {
|
|
|
|
dialog_search = ro_gui_dialog_create("search");
|
|
|
|
ro_gui_wimp_event_register_keypress(dialog_search,
|
|
|
|
ro_gui_search_keypress);
|
|
|
|
ro_gui_wimp_event_register_close_window(dialog_search,
|
|
|
|
ro_gui_search_end);
|
|
|
|
ro_gui_wimp_event_register_menu_gright(dialog_search, ICON_SEARCH_TEXT,
|
|
|
|
ICON_SEARCH_MENU, recent_search_menu);
|
|
|
|
ro_gui_wimp_event_register_text_field(dialog_search, ICON_SEARCH_STATUS);
|
|
|
|
ro_gui_wimp_event_register_checkbox(dialog_search, ICON_SEARCH_CASE_SENSITIVE);
|
|
|
|
ro_gui_wimp_event_register_mouse_click(dialog_search,
|
|
|
|
ro_gui_search_click);
|
|
|
|
ro_gui_wimp_event_register_ok(dialog_search, ICON_SEARCH_FIND_NEXT,
|
|
|
|
ro_gui_search_next);
|
|
|
|
ro_gui_wimp_event_register_cancel(dialog_search, ICON_SEARCH_CANCEL);
|
|
|
|
ro_gui_wimp_event_set_help_prefix(dialog_search, "HelpSearch");
|
|
|
|
|
|
|
|
recent_search_menu->title_data.indirected_text.text =
|
|
|
|
(char*)messages_get("Search");
|
|
|
|
ro_gui_menu_init_structure(recent_search_menu, RECENT_SEARCHES);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wrapper for the pressing of an OK button for wimp_event.
|
|
|
|
*
|
|
|
|
* \return false, to indicate the window should not be closed
|
|
|
|
*/
|
|
|
|
bool ro_gui_search_next(wimp_w w) {
|
|
|
|
search_insert = true;
|
|
|
|
start_search(true);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ro_gui_search_click(wimp_pointer *pointer) {
|
|
|
|
switch (pointer->i) {
|
|
|
|
case ICON_SEARCH_FIND_PREV:
|
|
|
|
search_insert = true;
|
|
|
|
start_search(false);
|
|
|
|
return true;
|
|
|
|
case ICON_SEARCH_CASE_SENSITIVE:
|
|
|
|
start_search(true);
|
|
|
|
return true;
|
2006-02-13 00:23:22 +03:00
|
|
|
case ICON_SEARCH_SHOW_ALL:
|
|
|
|
show_all(ro_gui_get_icon_selected_state(pointer->w, pointer->i));
|
|
|
|
return true;
|
2005-12-31 07:42:17 +03:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ro_gui_search_add_recent(char *search) {
|
|
|
|
char *tmp;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if ((search == NULL) || (search[0] == '\0'))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!search_insert) {
|
|
|
|
free(recent_search[0]);
|
|
|
|
recent_search[0] = strdup(search);
|
|
|
|
ro_gui_search_prepare_menu();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((recent_search[0] != NULL) &&
|
2005-12-31 08:17:44 +03:00
|
|
|
(!strcmp(recent_search[0], search)))
|
2005-12-31 07:42:17 +03:00
|
|
|
return;
|
|
|
|
|
|
|
|
tmp = strdup(search);
|
|
|
|
if (!tmp) {
|
|
|
|
warn_user("NoMemory", 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
free(recent_search[RECENT_SEARCHES - 1]);
|
|
|
|
for (i = RECENT_SEARCHES - 1; i > 0; i--)
|
|
|
|
recent_search[i] = recent_search[i - 1];
|
|
|
|
recent_search[0] = tmp;
|
|
|
|
search_insert = false;
|
|
|
|
|
|
|
|
ro_gui_set_icon_shaded_state(dialog_search, ICON_SEARCH_MENU, false);
|
|
|
|
ro_gui_search_prepare_menu();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ro_gui_search_prepare_menu(void) {
|
|
|
|
os_error *error;
|
|
|
|
int i;
|
|
|
|
int suggestions = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < RECENT_SEARCHES; i++)
|
|
|
|
if (recent_search[i] != NULL)
|
|
|
|
suggestions++;
|
|
|
|
|
|
|
|
if (suggestions == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (i = 0; i < suggestions; i++) {
|
2005-12-31 08:17:44 +03:00
|
|
|
recent_search_menu->entries[i].menu_flags &= ~wimp_MENU_LAST;
|
2005-12-31 07:42:17 +03:00
|
|
|
recent_search_menu->entries[i].data.indirected_text.text =
|
|
|
|
recent_search[i];
|
|
|
|
recent_search_menu->entries[i].data.indirected_text.size =
|
|
|
|
strlen(recent_search[i]) + 1;
|
|
|
|
}
|
|
|
|
recent_search_menu->entries[suggestions - 1].menu_flags |= wimp_MENU_LAST;
|
|
|
|
|
|
|
|
if ((current_menu_open) && (current_menu == recent_search_menu)) {
|
|
|
|
error = xwimp_create_menu(current_menu, 0, 0);
|
|
|
|
if (error) {
|
|
|
|
LOG(("xwimp_create_menu: 0x%x: %s",
|
|
|
|
error->errnum, error->errmess));
|
|
|
|
warn_user("MenuError", error->errmess);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2004-08-06 00:32:00 +04:00
|
|
|
|
2004-08-06 02:03:56 +04:00
|
|
|
/**
|
|
|
|
* Open the search dialog
|
|
|
|
*
|
|
|
|
* \param g the gui window to search
|
|
|
|
*/
|
2005-04-08 00:46:22 +04:00
|
|
|
void ro_gui_search_prepare(struct gui_window *g)
|
2004-08-06 02:03:56 +04:00
|
|
|
{
|
2005-04-17 07:30:35 +04:00
|
|
|
struct content *c;
|
|
|
|
|
2004-08-06 02:03:56 +04:00
|
|
|
assert(g != NULL);
|
|
|
|
|
2005-07-21 03:27:28 +04:00
|
|
|
/* if the search dialogue is reopened over a new window, we still
|
|
|
|
need to cancel the previous search */
|
2005-12-31 07:42:17 +03:00
|
|
|
ro_gui_search_end(dialog_search);
|
2005-07-21 03:27:28 +04:00
|
|
|
|
2004-08-06 02:03:56 +04:00
|
|
|
search_current_window = g;
|
|
|
|
|
|
|
|
ro_gui_set_icon_string(dialog_search, ICON_SEARCH_TEXT, "");
|
|
|
|
ro_gui_set_icon_selected_state(dialog_search,
|
|
|
|
ICON_SEARCH_CASE_SENSITIVE, false);
|
2005-04-17 07:30:35 +04:00
|
|
|
|
|
|
|
c = search_current_window->bw->current_content;
|
|
|
|
|
|
|
|
/* only handle html contents */
|
2005-12-31 07:42:17 +03:00
|
|
|
if ((!c) || (c->type != CONTENT_HTML))
|
2005-04-17 07:30:35 +04:00
|
|
|
return;
|
|
|
|
|
2005-07-19 07:56:17 +04:00
|
|
|
show_status(true);
|
|
|
|
ro_gui_set_icon_shaded_state(dialog_search, ICON_SEARCH_FIND_PREV, true);
|
|
|
|
ro_gui_set_icon_shaded_state(dialog_search, ICON_SEARCH_FIND_NEXT, true);
|
|
|
|
|
2005-12-31 07:42:17 +03:00
|
|
|
ro_gui_wimp_event_memorise(dialog_search);
|
|
|
|
search_insert = true;
|
2004-08-06 00:32:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2004-08-06 02:03:56 +04:00
|
|
|
* Handle keypresses in the search dialog
|
|
|
|
*
|
|
|
|
* \param key wimp_key block
|
|
|
|
* \return true if keypress handled, false otherwise
|
2004-08-06 00:32:00 +04:00
|
|
|
*/
|
2004-08-06 02:03:56 +04:00
|
|
|
bool ro_gui_search_keypress(wimp_key *key)
|
2004-08-06 00:32:00 +04:00
|
|
|
{
|
2004-08-06 02:03:56 +04:00
|
|
|
bool state;
|
|
|
|
|
|
|
|
switch (key->c) {
|
2006-02-13 00:23:22 +03:00
|
|
|
case 1: { /* ctrl a */
|
|
|
|
bool sel = !ro_gui_get_icon_selected_state(key->w, ICON_SEARCH_SHOW_ALL);
|
|
|
|
ro_gui_set_icon_selected_state(key->w, ICON_SEARCH_SHOW_ALL, sel);
|
|
|
|
show_all(sel);
|
|
|
|
}
|
|
|
|
break;
|
2004-08-06 02:03:56 +04:00
|
|
|
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);
|
2005-07-19 07:56:17 +04:00
|
|
|
start_search(true);
|
2004-08-06 02:03:56 +04:00
|
|
|
return true;
|
2005-04-18 03:32:09 +04:00
|
|
|
case wimp_KEY_UP:
|
2005-12-31 07:42:17 +03:00
|
|
|
search_insert = true;
|
2005-07-19 07:56:17 +04:00
|
|
|
start_search(false);
|
2005-04-18 03:32:09 +04:00
|
|
|
return true;
|
|
|
|
case wimp_KEY_DOWN:
|
2005-12-31 08:17:44 +03:00
|
|
|
search_insert = true;
|
|
|
|
start_search(true);
|
2005-04-18 03:32:09 +04:00
|
|
|
return true;
|
2005-07-19 07:56:17 +04:00
|
|
|
|
|
|
|
default:
|
2005-12-31 07:42:17 +03:00
|
|
|
if (key->c == 21) /* ctrl+u means the user's starting a new search */
|
|
|
|
search_insert = true;
|
2005-07-19 07:56:17 +04:00
|
|
|
if (key->c == 8 || /* backspace */
|
|
|
|
key->c == 21 || /* ctrl u */
|
|
|
|
(key->c >= 0x20 && key->c <= 0x7f)) {
|
|
|
|
start_search(true);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
break;
|
2004-08-06 02:03:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2005-07-19 07:56:17 +04:00
|
|
|
* Begins/continues the search process
|
|
|
|
* Note that this may be called many times for a single search.
|
|
|
|
*
|
|
|
|
* \param forwards search forwards from start/current position
|
2004-08-06 02:03:56 +04:00
|
|
|
*/
|
2005-07-19 07:56:17 +04:00
|
|
|
|
|
|
|
void start_search(bool forwards)
|
2004-08-06 02:03:56 +04:00
|
|
|
{
|
2005-07-19 07:56:17 +04:00
|
|
|
int string_len;
|
2004-08-06 02:03:56 +04:00
|
|
|
char *string;
|
|
|
|
|
|
|
|
string = ro_gui_get_icon_string(dialog_search, ICON_SEARCH_TEXT);
|
2005-07-19 07:56:17 +04:00
|
|
|
assert(string);
|
|
|
|
|
2005-12-31 07:42:17 +03:00
|
|
|
ro_gui_search_add_recent(string);
|
|
|
|
|
2005-07-19 07:56:17 +04:00
|
|
|
string_len = strlen(string);
|
|
|
|
if (string_len <= 0) {
|
2006-02-13 00:23:22 +03:00
|
|
|
free_matches();
|
2005-07-19 07:56:17 +04:00
|
|
|
show_status(true);
|
|
|
|
ro_gui_set_icon_shaded_state(dialog_search, ICON_SEARCH_FIND_PREV, true);
|
|
|
|
ro_gui_set_icon_shaded_state(dialog_search, ICON_SEARCH_FIND_NEXT, true);
|
|
|
|
gui_window_set_scroll(search_current_window, 0, 0);
|
2004-08-06 02:03:56 +04:00
|
|
|
return;
|
2005-07-19 07:56:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
do_search(string, string_len,
|
2004-08-06 02:03:56 +04:00
|
|
|
ro_gui_get_icon_selected_state(dialog_search,
|
|
|
|
ICON_SEARCH_CASE_SENSITIVE),
|
2005-07-19 07:56:17 +04:00
|
|
|
forwards);
|
2004-08-06 02:03:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ends the search process, invalidating all global state and
|
|
|
|
* freeing the list of found boxes
|
2005-12-31 07:42:17 +03:00
|
|
|
*
|
|
|
|
* \param w the search window handle (not used)
|
2004-08-06 02:03:56 +04:00
|
|
|
*/
|
2005-12-31 07:42:17 +03:00
|
|
|
void ro_gui_search_end(wimp_w w)
|
2004-08-06 02:03:56 +04:00
|
|
|
{
|
|
|
|
search_current_window = 0;
|
|
|
|
|
2005-12-31 07:42:17 +03:00
|
|
|
if (search_string) {
|
|
|
|
ro_gui_search_add_recent(search_string);
|
2004-08-06 02:03:56 +04:00
|
|
|
free(search_string);
|
2005-12-31 07:42:17 +03:00
|
|
|
}
|
2004-08-06 02:03:56 +04:00
|
|
|
search_string = 0;
|
|
|
|
|
2006-02-13 00:23:22 +03:00
|
|
|
free_matches();
|
2004-08-06 02:03:56 +04:00
|
|
|
|
|
|
|
search_current = 0;
|
|
|
|
|
2004-08-06 02:38:26 +04:00
|
|
|
search_content = 0;
|
2004-08-06 02:03:56 +04:00
|
|
|
|
|
|
|
search_prev_case_sens = false;
|
2004-08-06 00:32:00 +04:00
|
|
|
}
|
|
|
|
|
2006-02-13 00:23:22 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Release the memory used by the list of matches,
|
|
|
|
* deleting selection objects too
|
|
|
|
*/
|
|
|
|
|
|
|
|
void free_matches(void)
|
|
|
|
{
|
|
|
|
struct list_entry *a = search_found->next;
|
|
|
|
struct list_entry *b;
|
|
|
|
|
|
|
|
/* empty the list before clearing and deleting the
|
|
|
|
selections because the the clearing updates the
|
|
|
|
screen immediately, causing nested accesses to the list */
|
|
|
|
|
|
|
|
search_found->prev = 0;
|
|
|
|
search_found->next = 0;
|
|
|
|
|
|
|
|
for (; a; a = b) {
|
|
|
|
b = a->next;
|
|
|
|
if (a->sel) {
|
|
|
|
selection_clear(a->sel, true);
|
|
|
|
selection_destroy(a->sel);
|
|
|
|
}
|
|
|
|
free(a);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-06 00:32:00 +04:00
|
|
|
/**
|
|
|
|
* Search for a string in the box tree
|
|
|
|
*
|
|
|
|
* \param string the string to search for
|
2005-07-19 07:56:17 +04:00
|
|
|
* \param string_len length of search string
|
2004-08-06 00:32:00 +04:00
|
|
|
* \param case_sens whether to perform a case sensitive search
|
|
|
|
* \param forwards direction to search in
|
|
|
|
*/
|
2005-07-19 07:56:17 +04:00
|
|
|
void do_search(char *string, int string_len, bool case_sens, bool forwards)
|
2004-08-06 00:32:00 +04:00
|
|
|
{
|
|
|
|
struct content *c;
|
|
|
|
struct box *box;
|
2006-01-25 20:28:29 +03:00
|
|
|
int x0,y0,x1,y1;
|
2004-08-06 00:32:00 +04:00
|
|
|
bool new = false;
|
|
|
|
|
2004-08-06 02:03:56 +04:00
|
|
|
if (!search_current_window)
|
2004-08-06 00:32:00 +04:00
|
|
|
return;
|
|
|
|
|
2004-08-06 02:03:56 +04:00
|
|
|
c = search_current_window->bw->current_content;
|
2004-08-06 00:32:00 +04:00
|
|
|
|
|
|
|
/* only handle html contents */
|
2005-12-31 07:42:17 +03:00
|
|
|
if ((!c) || (c->type != CONTENT_HTML))
|
2004-08-06 00:32:00 +04:00
|
|
|
return;
|
|
|
|
|
2004-10-18 01:16:00 +04:00
|
|
|
box = c->data.html.layout;
|
2004-08-06 00:32:00 +04:00
|
|
|
|
|
|
|
if (!box)
|
|
|
|
return;
|
|
|
|
|
2006-02-13 00:23:22 +03:00
|
|
|
// LOG(("do_search '%s' - '%s' (%p, %p) %p (%d, %d) %d",
|
|
|
|
// search_string, string, search_content, c, search_found->next,
|
|
|
|
// search_prev_case_sens, case_sens, forwards));
|
2004-08-06 00:32:00 +04:00
|
|
|
|
|
|
|
/* check if we need to start a new search or continue an old one */
|
2005-07-19 07:56:17 +04:00
|
|
|
if (!search_string || c != search_content || !search_found->next ||
|
2004-08-06 02:03:56 +04:00
|
|
|
search_prev_case_sens != case_sens ||
|
2004-08-06 00:32:00 +04:00
|
|
|
(case_sens && strcmp(string, search_string) != 0) ||
|
|
|
|
(!case_sens && strcasecmp(string, search_string) != 0)) {
|
2005-07-19 07:56:17 +04:00
|
|
|
|
2004-08-06 00:32:00 +04:00
|
|
|
if (search_string)
|
|
|
|
free(search_string);
|
2004-08-06 02:03:56 +04:00
|
|
|
search_current = 0;
|
2006-02-13 00:23:22 +03:00
|
|
|
free_matches();
|
2005-07-19 07:56:17 +04:00
|
|
|
|
2006-02-13 00:23:22 +03:00
|
|
|
search_string = malloc(string_len + 1);
|
|
|
|
if (search_string) {
|
|
|
|
memcpy(search_string, string, string_len);
|
|
|
|
search_string[string_len] = '\0';
|
|
|
|
}
|
2005-07-19 07:56:17 +04:00
|
|
|
|
|
|
|
xhourglass_on();
|
|
|
|
if (!find_occurrences(string, string_len, box, case_sens)) {
|
2006-02-13 00:23:22 +03:00
|
|
|
free_matches();
|
2005-07-19 07:56:17 +04:00
|
|
|
xhourglass_off();
|
2004-08-06 00:32:00 +04:00
|
|
|
return;
|
|
|
|
}
|
2005-07-19 07:56:17 +04:00
|
|
|
xhourglass_off();
|
|
|
|
|
2004-08-06 00:32:00 +04:00
|
|
|
new = true;
|
2004-08-06 02:38:26 +04:00
|
|
|
search_content = c;
|
2004-08-06 02:03:56 +04:00
|
|
|
search_prev_case_sens = case_sens;
|
2004-08-06 00:32:00 +04:00
|
|
|
}
|
|
|
|
|
2004-08-06 02:38:26 +04:00
|
|
|
// LOG(("%d %p %p (%p, %p)", new, search_found->next, search_current, search_current->prev, search_current->next));
|
2004-08-06 00:32:00 +04:00
|
|
|
|
2005-07-19 07:56:17 +04:00
|
|
|
if (new) {
|
2004-08-06 00:32:00 +04:00
|
|
|
/* new search, beginning at the top of the page */
|
2004-08-06 02:03:56 +04:00
|
|
|
search_current = search_found->next;
|
2004-08-06 00:32:00 +04:00
|
|
|
}
|
2005-07-19 07:56:17 +04:00
|
|
|
else if (search_current) {
|
2004-08-06 00:32:00 +04:00
|
|
|
/* continued search in the direction specified */
|
2005-07-19 07:56:17 +04:00
|
|
|
if (forwards) {
|
|
|
|
if (search_current->next)
|
|
|
|
search_current = search_current->next;
|
2004-08-06 00:32:00 +04:00
|
|
|
}
|
2005-07-19 07:56:17 +04:00
|
|
|
else {
|
|
|
|
if (search_current->prev)
|
|
|
|
search_current = search_current->prev;
|
2004-08-06 00:32:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-07-19 07:56:17 +04:00
|
|
|
show_status(search_current != NULL);
|
2006-02-13 00:23:22 +03:00
|
|
|
show_all(ro_gui_get_icon_selected_state(dialog_search, ICON_SEARCH_SHOW_ALL));
|
2005-07-19 07:56:17 +04:00
|
|
|
|
|
|
|
ro_gui_set_icon_shaded_state(dialog_search, ICON_SEARCH_FIND_PREV,
|
|
|
|
!search_current || !search_current->prev);
|
|
|
|
ro_gui_set_icon_shaded_state(dialog_search, ICON_SEARCH_FIND_NEXT,
|
|
|
|
!search_current || !search_current->next);
|
|
|
|
|
2004-08-06 02:03:56 +04:00
|
|
|
if (!search_current)
|
2004-08-06 00:32:00 +04:00
|
|
|
return;
|
|
|
|
|
|
|
|
/* get box position and jump to it */
|
2006-01-25 20:28:29 +03:00
|
|
|
box_coords(search_current->start_box, &x0, &y0);
|
|
|
|
x0 += 0; /* \todo: move x0 in by correct idx */
|
|
|
|
box_coords(search_current->end_box, &x1, &y1);
|
|
|
|
x1 += search_current->end_box->width; /* \todo: move x1 in by correct idx */
|
|
|
|
y1 += search_current->end_box->height;
|
|
|
|
|
|
|
|
gui_window_scroll_visible(search_current_window, x0, y0, x1, y1);
|
2004-08-06 00:32:00 +04:00
|
|
|
}
|
|
|
|
|
2005-03-25 17:25:25 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Find the first occurrence of 'match' in 'string' and return its index
|
|
|
|
*
|
2005-04-17 07:30:35 +04:00
|
|
|
* /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
|
2005-03-25 17:25:25 +03:00
|
|
|
*/
|
|
|
|
|
2005-04-17 07:30:35 +04:00
|
|
|
const char *find_pattern(const char *string, int s_len, const char *pattern,
|
|
|
|
int p_len, bool case_sens, int *m_len)
|
2005-03-25 17:25:25 +03:00
|
|
|
{
|
2005-04-17 07:30:35 +04:00
|
|
|
struct { const char *ss, *s, *p; bool first; } context[16];
|
2005-03-25 17:25:25 +03:00
|
|
|
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;
|
2005-04-17 07:30:35 +04:00
|
|
|
bool first = true;
|
2005-03-25 17:25:25 +03:00
|
|
|
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 */
|
2005-04-17 07:30:35 +04:00
|
|
|
if (p >= ep) break;
|
2005-03-25 17:25:25 +03:00
|
|
|
|
|
|
|
/* 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++;
|
|
|
|
}
|
|
|
|
}
|
2005-07-19 07:56:17 +04:00
|
|
|
|
2005-03-25 17:25:25 +03:00
|
|
|
if (s < es) {
|
2005-04-17 07:30:35 +04:00
|
|
|
/* remember where we are in case the match fails;
|
|
|
|
we can then resume */
|
2005-03-25 17:25:25 +03:00
|
|
|
if (top < (int)NOF_ELEMENTS(context)) {
|
|
|
|
context[top].ss = ss;
|
|
|
|
context[top].s = s + 1;
|
|
|
|
context[top].p = p - 1; /* ptr to last asterisk */
|
2005-04-17 07:30:35 +04:00
|
|
|
context[top].first = first;
|
2005-03-25 17:25:25 +03:00
|
|
|
top++;
|
|
|
|
}
|
2005-04-17 07:30:35 +04:00
|
|
|
|
|
|
|
if (first) {
|
|
|
|
ss = s; /* remember first non-'*' char */
|
|
|
|
first = false;
|
|
|
|
}
|
|
|
|
|
2005-03-25 17:25:25 +03:00
|
|
|
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));
|
|
|
|
}
|
2005-04-17 07:30:35 +04:00
|
|
|
if (matches && first) {
|
|
|
|
ss = s; /* remember first non-'*' char */
|
|
|
|
first = false;
|
|
|
|
}
|
2005-03-25 17:25:25 +03:00
|
|
|
}
|
|
|
|
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;
|
2005-04-17 07:30:35 +04:00
|
|
|
first = context[top].first;
|
2005-03-25 17:25:25 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* end of pattern reached */
|
2005-04-17 07:30:35 +04:00
|
|
|
*m_len = s - ss;
|
2005-03-25 17:25:25 +03:00
|
|
|
return ss;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-08-06 00:32:00 +04:00
|
|
|
/**
|
|
|
|
* Finds all occurrences of a given string in the box tree
|
|
|
|
*
|
2005-03-25 17:25:25 +03:00
|
|
|
* \param pattern the string pattern to search for
|
|
|
|
* \param p_len pattern length
|
|
|
|
* \param cur pointer to the current box
|
2004-08-06 00:32:00 +04:00
|
|
|
* \param case_sens whether to perform a case sensitive search
|
|
|
|
* \return true on success, false on memory allocation failure
|
|
|
|
*/
|
2005-04-17 07:30:35 +04:00
|
|
|
bool find_occurrences(const char *pattern, int p_len, struct box *cur,
|
|
|
|
bool case_sens)
|
2004-08-06 00:32:00 +04:00
|
|
|
{
|
|
|
|
struct box *a;
|
|
|
|
|
|
|
|
/* ignore this box, if there's no visible text */
|
|
|
|
if (!cur->object && cur->text) {
|
2005-04-17 08:13:13 +04:00
|
|
|
const char *text = cur->text;
|
|
|
|
unsigned length = cur->length;
|
|
|
|
|
|
|
|
while (length > 0) {
|
|
|
|
unsigned match_length;
|
2005-04-17 07:30:35 +04:00
|
|
|
unsigned match_offset;
|
2005-04-17 08:13:13 +04:00
|
|
|
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));
|
2004-08-06 00:32:00 +04:00
|
|
|
if (!entry) {
|
|
|
|
warn_user("NoMemory", 0);
|
|
|
|
return false;
|
|
|
|
}
|
2005-04-17 08:13:13 +04:00
|
|
|
|
2005-04-17 07:30:35 +04:00
|
|
|
match_offset = pos - cur->text;
|
2006-02-13 00:23:22 +03:00
|
|
|
|
2005-04-17 07:30:35 +04:00
|
|
|
entry->start_box = cur;
|
|
|
|
entry->start_idx = match_offset;
|
|
|
|
entry->end_box = cur;
|
|
|
|
entry->end_idx = match_offset + match_length;
|
2006-02-13 00:23:22 +03:00
|
|
|
entry->sel = NULL;
|
|
|
|
|
2004-08-06 00:32:00 +04:00
|
|
|
entry->next = 0;
|
2004-08-06 02:03:56 +04:00
|
|
|
entry->prev = search_found->prev;
|
|
|
|
if (!search_found->prev)
|
|
|
|
search_found->next = entry;
|
2004-08-06 00:32:00 +04:00
|
|
|
else
|
2004-08-06 02:03:56 +04:00
|
|
|
search_found->prev->next = entry;
|
|
|
|
search_found->prev = entry;
|
2005-04-17 08:13:13 +04:00
|
|
|
|
|
|
|
new_text = pos + match_length;
|
|
|
|
length -= (new_text - text);
|
|
|
|
text = new_text;
|
2004-08-06 00:32:00 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* and recurse */
|
|
|
|
for (a = cur->children; a; a = a->next) {
|
2005-03-25 17:25:25 +03:00
|
|
|
if (!find_occurrences(pattern, p_len, a, case_sens))
|
2004-08-06 02:03:56 +04:00
|
|
|
return false;
|
2004-08-06 00:32:00 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-04-17 07:30:35 +04:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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)
|
|
|
|
{
|
2006-02-13 00:23:22 +03:00
|
|
|
// if (g == search_current_window && search_selection) {
|
|
|
|
// if (selection_defined(search_selection))
|
|
|
|
// return selection_highlighted(search_selection, box,
|
|
|
|
// start_idx, end_idx);
|
|
|
|
// }
|
|
|
|
if (g == search_current_window) {
|
|
|
|
struct list_entry *a;
|
|
|
|
for(a = search_found->next; a; a = a->next)
|
|
|
|
if (a->sel && selection_defined(a->sel) &&
|
|
|
|
selection_highlighted(a->sel, box,
|
|
|
|
start_idx, end_idx))
|
|
|
|
return true;
|
2005-04-17 07:30:35 +04:00
|
|
|
}
|
2006-02-13 00:23:22 +03:00
|
|
|
|
2005-04-17 07:30:35 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-02-13 00:23:22 +03:00
|
|
|
/**
|
|
|
|
* Specifies whether all matches or just the current match should
|
|
|
|
* be highlighted in the search text.
|
|
|
|
*/
|
|
|
|
|
|
|
|
void show_all(bool all)
|
|
|
|
{
|
|
|
|
struct list_entry *a;
|
|
|
|
|
|
|
|
for (a = search_found->next; a; a = a->next) {
|
|
|
|
bool add = true;
|
|
|
|
if (!all && a != search_current) {
|
|
|
|
add = false;
|
|
|
|
if (a->sel) {
|
|
|
|
selection_clear(a->sel, true);
|
|
|
|
selection_destroy(a->sel);
|
|
|
|
a->sel = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (add && !a->sel) {
|
|
|
|
a->sel = selection_create(search_current_window->bw);
|
|
|
|
if (a->sel) {
|
|
|
|
struct content *c = search_current_window->bw->current_content;
|
|
|
|
selection_init(a->sel, c->data.html.layout);
|
|
|
|
selection_set_start(a->sel, a->start_box, a->start_idx);
|
|
|
|
selection_set_end(a->sel, a->end_box, a->end_idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-04-18 03:32:09 +04:00
|
|
|
/**
|
2005-07-19 07:56:17 +04:00
|
|
|
* Change the displayed search status.
|
2005-04-18 03:32:09 +04:00
|
|
|
*
|
2005-07-19 07:56:17 +04:00
|
|
|
* \param found search pattern matched in text
|
2005-04-18 03:32:09 +04:00
|
|
|
*/
|
|
|
|
|
2005-07-19 07:56:17 +04:00
|
|
|
void show_status(bool found)
|
2005-04-18 03:32:09 +04:00
|
|
|
{
|
2005-07-30 19:11:23 +04:00
|
|
|
ro_gui_set_icon_string(dialog_search, ICON_SEARCH_STATUS,
|
|
|
|
found ? "" : messages_get("Notfound"));
|
2005-04-18 03:32:09 +04:00
|
|
|
}
|
|
|
|
|
2004-08-06 00:32:00 +04:00
|
|
|
#endif
|