NetSurf
search.c
Go to the documentation of this file.
1/*
2 * Copyright 2004 John M Bell <jmb202@ecs.soton.ac.uk>
3 * Copyright 2005 Adrian Lees <adrianl@users.sourceforge.net>
4 *
5 * This file is part of NetSurf, http://www.netsurf-browser.org/
6 *
7 * NetSurf is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * NetSurf is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20/**
21 * \file
22 * Free text search implementation
23 */
24
25#include "utils/config.h"
26
27#include <ctype.h>
28#include <string.h>
29#include "oslib/hourglass.h"
30#include "oslib/wimp.h"
31
32#include "utils/log.h"
33#include "utils/messages.h"
35#include "netsurf/search.h"
36#include "netsurf/content.h"
37#include "desktop/search.h"
38
39#include "riscos/gui.h"
40#include "riscos/dialog.h"
41#include "riscos/menus.h"
42#include "riscos/wimp.h"
43#include "riscos/wimp_event.h"
44
45#define RECENT_SEARCHES 8
46
51};
52
54 { { NULL }, false, NULL };
55
56static wimp_MENU(RECENT_SEARCHES) menu_recent;
57wimp_menu *recent_search_menu = (wimp_menu *)&menu_recent;
58#define DEFAULT_FLAGS (wimp_ICON_TEXT | wimp_ICON_FILLED | \
59 (wimp_COLOUR_BLACK << wimp_ICON_FG_COLOUR_SHIFT) | \
60 (wimp_COLOUR_WHITE << wimp_ICON_BG_COLOUR_SHIFT))
61
62
63static void ro_gui_search_end(wimp_w w);
64static bool ro_gui_search_next(wimp_w w);
65static bool ro_gui_search_menu_prepare(wimp_w w, wimp_i i, wimp_menu *menu,
66 wimp_pointer *pointer);
67static bool ro_gui_search_prepare_menu(void);
68static bool ro_gui_search_click(wimp_pointer *pointer);
69static bool ro_gui_search_keypress(wimp_key *key);
71static void ro_gui_search_set_forward_state(bool active, void *p);
72static void ro_gui_search_set_back_state(bool active, void *p);
73static void ro_gui_search_set_status(bool found, void *p);
74static void ro_gui_search_set_hourglass(bool active, void *p);
75static void ro_gui_search_add_recent(const char *string, void *p);
76
77static struct gui_search_table search_table = {
83};
84
86
88{
109
110 recent_search_menu->title_data.indirected_text.text =
111 (char*)messages_get("Search");
113}
114
115/**
116 * Wrapper for the pressing of an OK button for wimp_event.
117 *
118 * \return false, to indicate the window should not be closed
119 */
120bool ro_gui_search_next(wimp_w w)
121{
128 return false;
129}
130
131
132/**
133 * Callback to prepare menus in the Search dialog. At present, this
134 * only has to handle the previous search pop-up.
135 *
136 * \param w The window handle owning the menu.
137 * \param i The icon handle owning the menu.
138 * \param *menu The menu to be prepared.
139 * \param *pointer The associated mouse click event block, or NULL
140 * on an Adjust-click re-opening.
141 * \return true if the event was handled; false if not.
142 */
143
144bool ro_gui_search_menu_prepare(wimp_w w, wimp_i i, wimp_menu *menu,
145 wimp_pointer *pointer)
146{
147 if (menu != recent_search_menu || i != ICON_SEARCH_MENU)
148 return false;
149
150 if (pointer != NULL)
152
153 return true;
154}
155
156
157bool ro_gui_search_click(wimp_pointer *pointer)
158{
159 search_flags_t flags;
160 switch (pointer->i) {
163 flags = ~SEARCH_FLAG_FORWARDS &
166 flags,
169 return true;
171 flags = SEARCH_FLAG_FORWARDS |
176 flags,
179 return true;
182 pointer->w, pointer->i) ?
185 flags,
188 return true;
189 }
190 return false;
191}
192
193/**
194 * add search string to recent searches list
195 *
196 * front is at liberty how to implement the bare notification
197 * should normally store a strdup() of the string in
198 * search_global_data.recent[];
199 * core gives no guarantee of the integrity of the const char *
200 *
201 * \param search string search pattern
202 * \param p the pointer sent to search_verify_new()
203 */
204
205void ro_gui_search_add_recent(const char *search, void *p)
206{
207 char *tmp;
208 int i;
209
210 if ((search == NULL) || (search[0] == '\0'))
211 return;
212
215 search_data.recent_searches[0] = strdup(search);
217 return;
218 }
219
220 if ((search_data.recent_searches[0] != NULL) &&
221 (!strcmp(search_data.recent_searches[0], search)))
222 return;
223
224 tmp = strdup(search);
225 if (!tmp) {
226 ro_warn_user("NoMemory", 0);
227 return;
228 }
230 for (i = RECENT_SEARCHES - 1; i > 0; i--)
234
237}
238
240{
241 int i;
242 int suggestions = 0;
243
244 for (i = 0; i < RECENT_SEARCHES; i++)
245 if (search_data.recent_searches[i] != NULL)
246 suggestions++;
247
248 if (suggestions == 0)
249 return false;
250
251 for (i = 0; i < suggestions; i++) {
252 recent_search_menu->entries[i].menu_flags &= ~wimp_MENU_LAST;
253 recent_search_menu->entries[i].data.indirected_text.text =
255 recent_search_menu->entries[i].data.indirected_text.size =
256 strlen(search_data.recent_searches[i]) + 1;
257 }
258 recent_search_menu->entries[suggestions - 1].menu_flags |=
259 wimp_MENU_LAST;
260
261 return true;
262}
263
264/**
265 * Determine of the browser window is searchable.
266 *
267 * \param bw The browser window to check.
268 */
270{
271 struct hlcache_handle *h;
272
273 assert(bw != NULL);
274
276
277 /* only handle html/textplain contents */
278 /** \todo Should have content_is_searchable() api */
279 if ((!h) || (content_get_type(h) != CONTENT_HTML &&
281 return false;
282
283 return true;
284}
285
286
287/**
288 * Open the search dialog
289 *
290 * \param bw the browser window to search
291 */
293{
294 /* only handle searchable contents */
296 return;
297
298 /* if the search dialogue is reopened over a new window, we may
299 need to cancel the previous search */
302
304
309 ICON_SEARCH_SHOW_ALL, false);
310
311 ro_gui_search_set_status(true, NULL);
312
315}
316
317/**
318 * Handle keypresses in the search dialog
319 *
320 * \param key wimp_key block
321 * \return true if keypress handled, false otherwise
322 */
323bool ro_gui_search_keypress(wimp_key *key)
324{
325 bool state;
326 search_flags_t flags;
327
328 switch (key->c) {
329 case 1: {
333 flags,
336 }
337 break;
338 case 9: /* ctrl i */
343 flags = SEARCH_FLAG_FORWARDS |
346 flags,
349 return true;
350 case IS_WIMP_KEY | wimp_KEY_UP:
352 flags = ~SEARCH_FLAG_FORWARDS &
355 flags,
358 return true;
359 case IS_WIMP_KEY | wimp_KEY_DOWN:
361 flags = SEARCH_FLAG_FORWARDS |
364 flags,
367 return true;
368
369 default:
370 if (key->c == 21) {
371 /* ctrl+u means the user's starting
372 * a new search */
376 }
377 if (key->c == 8 || /* backspace */
378 key->c == 21 || /* ctrl u */
379 (key->c >= 0x20 && key->c <= 0x7f)) {
380 flags = SEARCH_FLAG_FORWARDS |
387 NULL,
388 flags,
392 return true;
393 }
394 break;
395 }
396
397 return false;
398}
399
400/**
401 * Ends the search
402 * \param w the search window handle (not used)
403 */
404void ro_gui_search_end(wimp_w w)
405{
407}
408
409/**
410* Change the displayed search status.
411* \param found search pattern matched in text
412* \param p the pointer sent to search_verify_new() / search_create_context()
413*/
414
415void ro_gui_search_set_status(bool found, void *p)
416{
418 messages_get("NotFound"), true);
419}
420
421/**
422* display hourglass while searching
423* \param active start/stop indicator
424* \param p the pointer sent to search_verify_new() / search_create_context()
425*/
426
427void ro_gui_search_set_hourglass(bool active, void *p)
428{
429 if (active)
430 xhourglass_on();
431
432 else
433 xhourglass_off();
434}
435
436/**
437* activate search forwards button in gui
438* \param active activate/inactivate
439* \param p the pointer sent to search_verify_new() / search_create_context()
440*/
441
442void ro_gui_search_set_forward_state(bool active, void *p)
443{
445 !active);
446}
447
448/**
449* activate search forwards button in gui
450* \param active activate/inactivate
451* \param p the pointer sent to search_verify_new() / search_create_context()
452*/
453
454void ro_gui_search_set_back_state(bool active, void *p)
455{
457 !active);
458}
459
460/**
461* retrieve state of 'case sensitive', 'show all' checks in gui
462*/
464{
465 search_flags_t flags;
471 return flags;
472}
473
Browser window creation and manipulation interface.
struct hlcache_handle * browser_window_get_content(struct browser_window *bw)
Get a cache handle for the content within a browser window.
@ CONTENT_HTML
content is HTML
Definition: content_type.h:58
@ CONTENT_TEXTPLAIN
content is plain text
Definition: content_type.h:61
void browser_window_search(struct browser_window *bw, void *context, search_flags_t flags, const char *string)
Starts or continues an existing search.
Definition: search.c:37
void browser_window_search_clear(struct browser_window *bw)
Clear up a search.
Definition: search.c:47
Browseing window text search interface.
search_flags_t
Definition: search.h:29
@ SEARCH_FLAG_CASE_SENSITIVE
Definition: search.h:31
@ SEARCH_FLAG_NONE
Definition: search.h:30
@ SEARCH_FLAG_SHOWALL
Definition: search.h:34
@ SEARCH_FLAG_FORWARDS
Definition: search.h:32
wimp_w dialog_search
Definition: dialog.c:78
wimp_w ro_gui_dialog_create(const char *template_name)
Create a window from a template.
Definition: dialog.c:208
static struct gui_search_table search_table
Definition: search.c:112
bool ro_gui_search_next(wimp_w w)
Wrapper for the pressing of an OK button for wimp_event.
Definition: search.c:120
void ro_gui_search_add_recent(const char *search, void *p)
add search string to recent searches list
Definition: search.c:205
bool ro_gui_search_click(wimp_pointer *pointer)
Definition: search.c:157
void ro_gui_search_set_status(bool found, void *p)
Change the displayed search status.
Definition: search.c:415
bool ro_gui_search_prepare_menu(void)
Definition: search.c:239
static wimp_MENU(RECENT_SEARCHES)
Definition: search.c:56
void ro_gui_search_end(wimp_w w)
Ends the search.
Definition: search.c:404
static bool ro_gui_search_bw_searchable(struct browser_window *bw)
Determine of the browser window is searchable.
Definition: search.c:269
void ro_gui_search_set_hourglass(bool active, void *p)
display hourglass while searching
Definition: search.c:427
void ro_gui_search_set_back_state(bool active, void *p)
activate search forwards button in gui
Definition: search.c:454
static struct search_static_data search_data
Definition: search.c:53
bool ro_gui_search_keypress(wimp_key *key)
Handle keypresses in the search dialog.
Definition: search.c:323
struct gui_search_table * riscos_search_table
Definition: search.c:85
void ro_gui_search_set_forward_state(bool active, void *p)
activate search forwards button in gui
Definition: search.c:442
search_flags_t ro_gui_search_update_flags(void)
retrieve state of 'case sensitive', 'show all' checks in gui
Definition: search.c:463
void ro_gui_search_prepare(struct browser_window *bw)
Open the search dialog.
Definition: search.c:292
bool ro_gui_search_menu_prepare(wimp_w w, wimp_i i, wimp_menu *menu, wimp_pointer *pointer)
Callback to prepare menus in the Search dialog.
Definition: search.c:144
#define RECENT_SEARCHES
Definition: search.c:45
void ro_gui_search_init(void)
Definition: search.c:87
Public content interface.
content_type content_get_type(struct hlcache_handle *h)
Retrieve computed type of content.
Definition: content.c:1061
Interface to platform-specific search operations.
void ro_gui_menu_init_structure(wimp_menu *menu, int entries)
Initialise the basic state of a menu structure so all entries are indirected text with no flags,...
Definition: menus.c:682
const char * messages_get(const char *key)
Fast lookup of a message by key from the standard Messages hash.
Definition: messages.c:241
Localised message support (interface).
nserror ro_warn_user(const char *warning, const char *detail)
Display a warning for a serious problem (eg memory exhaustion).
Definition: gui.c:2077
#define ICON_SEARCH_FIND_NEXT
Definition: gui.h:201
#define ICON_SEARCH_CANCEL
Definition: gui.h:203
#define ICON_SEARCH_FIND_PREV
Definition: gui.h:202
#define ICON_SEARCH_CASE_SENSITIVE
Definition: gui.h:200
#define ICON_SEARCH_STATUS
Definition: gui.h:204
wimp_menu * recent_search_menu
#define ICON_SEARCH_TEXT
Definition: gui.h:199
#define ICON_SEARCH_SHOW_ALL
Definition: gui.h:206
#define ICON_SEARCH_MENU
Definition: gui.h:205
Interface to utility string handling.
Browser window data.
function table for page text search.
Definition: search.h:31
High-level cache handle.
Definition: hlcache.c:66
struct browser_window * search_window
Definition: search.c:50
char * recent_searches[RECENT_SEARCHES]
Definition: search.c:48
bool search_insert
Definition: search.c:49
void ro_gui_set_icon_string(wimp_w w, wimp_i i, const char *text, bool is_utf8)
Set the contents of a text or sprite icon to a string.
Definition: wimp.c:269
void ro_gui_set_icon_shaded_state(wimp_w w, wimp_i i, bool state)
Set the shaded state of an icon.
Definition: wimp.c:487
const char * ro_gui_get_icon_string(wimp_w w, wimp_i i)
Read the contents of a text or sprite icon.
Definition: wimp.c:235
void ro_gui_set_icon_selected_state(wimp_w w, wimp_i i, bool state)
Set the selected state of an icon.
Definition: wimp.c:444
bool ro_gui_get_icon_selected_state(wimp_w w, wimp_i i)
Gets the selected state of an icon.
Definition: wimp.c:463
General RISC OS WIMP/OS library functions (interface).
bool ro_gui_wimp_event_register_text_field(wimp_w w, wimp_i i)
Register a text field to be automatically handled.
Definition: wimp_event.c:1318
bool ro_gui_wimp_event_register_keypress(wimp_w w, bool(*callback)(wimp_key *key))
Register a function to be called for all keypresses within a particular window.
Definition: wimp_event.c:1461
bool ro_gui_wimp_event_set_help_prefix(wimp_w w, const char *help_prefix)
Set the associated help prefix for a given window.
Definition: wimp_event.c:390
bool ro_gui_wimp_event_register_menu_gright(wimp_w w, wimp_i i, wimp_i gright, wimp_menu *menu)
Register an icon menu to be automatically handled.
Definition: wimp_event.c:1331
bool ro_gui_wimp_event_register_cancel(wimp_w w, wimp_i i)
Register a function to be called for the Cancel action on a window.
Definition: wimp_event.c:1403
bool ro_gui_wimp_event_memorise(wimp_w w)
Memorises the current state of any registered components in a window.
Definition: wimp_event.c:139
bool ro_gui_wimp_event_register_mouse_click(wimp_w w, bool(*callback)(wimp_pointer *pointer))
Register a function to be called for all mouse-clicks to icons in a window that don't have registered...
Definition: wimp_event.c:1439
bool ro_gui_wimp_event_register_checkbox(wimp_w w, wimp_i i)
Register a checkbox to be automatically handled.
Definition: wimp_event.c:1349
bool ro_gui_wimp_event_register_menu_prepare(wimp_w w, bool(*callback)(wimp_w w, wimp_i i, wimp_menu *m, wimp_pointer *p))
Register a function to be called before a menu is (re-)opened.
Definition: wimp_event.c:1559
bool ro_gui_wimp_event_register_ok(wimp_w w, wimp_i i, bool(*callback)(wimp_w w))
Register a function to be called for the OK action on a window.
Definition: wimp_event.c:1417
bool ro_gui_wimp_event_register_close_window(wimp_w w, void(*callback)(wimp_w w))
Register a function to be called after the window has been closed.
Definition: wimp_event.c:1492
Automated RISC OS WIMP event handling (interface).
#define IS_WIMP_KEY
Definition: wimp_event.h:37