NetSurf
search.c
Go to the documentation of this file.
1/*
2 * Copyright 2013 Ole Loots <ole@monochrom.net>
3 *
4 * This file is part of NetSurf, http://www.netsurf-browser.org/
5 *
6 * NetSurf is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * NetSurf is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 */
19
20#include <limits.h>
21#include <stdlib.h>
22#include <stdio.h>
23#include <unistd.h>
24#include <string.h>
25#include <stdbool.h>
26#include <assert.h>
27
28#include "utils/log.h"
29#include "utils/messages.h"
31#include "desktop/search.h"
32#include "netsurf/search.h"
33
34#include "atari/gui.h"
35#include "atari/rootwin.h"
36#include "atari/misc.h"
37#include "atari/toolbar.h"
38#include "atari/search.h"
39#include "atari/gemtk/gemtk.h"
40#include "atari/res/netsurf.rsh"
41
42extern struct gui_window * input_window;
43
44
45static void nsatari_search_set_status(bool found, void *p);
46static void nsatari_search_set_hourglass(bool active, void *p);
47static void nsatari_search_add_recent(const char *string, void *p);
49void nsatari_search_set_back_state(bool active, void *p);
50
57};
58
60
61
62
63/**
64 * Change the displayed search status.
65 * \param found search pattern matched in text
66 * \param p the pointer sent to search_verify_new() / search_create_context()
67 */
68void nsatari_search_set_status(bool found, void *p)
69{
70 NSLOG(netsurf, INFO, "%p set status: %d\n", p, found);
71 // TODO: maybe update GUI
72}
73
74
75/**
76 * display hourglass while searching
77 * \param active start/stop indicator
78 * \param p the pointer sent to search_verify_new() / search_create_context()
79 */
80void nsatari_search_set_hourglass(bool active, void *p)
81{
83 NSLOG(netsurf, INFO, "active: %d, session: %p", active, p);
84 if (active) {
86 } else {
88 }
89}
90
91
92/**
93 * add search string to recent searches list
94 * front is at liberty how to implement the bare notification
95 * should normally store a strdup() of the string;
96 * core gives no guarantee of the integrity of the const char *
97 * \param string search pattern
98 * \param p the pointer sent to search_verify_new() / search_create_context()
99 */
100void nsatari_search_add_recent(const char *string, void *p)
101{
102 NSLOG(netsurf, INFO, "%p add recent: %s\n", p, string);
103}
104
105
106/**
107 * activate search forwards button in gui
108 * \param active activate/inactivate
109 * \param p the pointer sent to search_verify_new() / search_create_context()
110 */
111void nsatari_search_set_forward_state(bool active, void *p)
112{
113 struct gui_window *gw;
114 OBJECT *toolbar;
115 GRECT area;
117 /* deactivate back cb */
118 NSLOG(netsurf, INFO, "%p: set forward state: %d\n", p, active);
119
120 gw = s->g;
121
123 if (active) {
124 toolbar[TOOLBAR_BT_SEARCH_FWD].ob_state &= ~OS_DISABLED;
125 } else {
126 toolbar[TOOLBAR_BT_SEARCH_FWD].ob_state |= OS_DISABLED;
127 }
130}
131
132
133/**
134 * activate search back button in gui
135 * \param active activate/inactivate
136 * \param p the pointer sent to search_verify_new() / search_create_context()
137 */
139{
140 struct gui_window *gw;
141 OBJECT *toolbar;
142 GRECT area;
144 /* deactivate back cb */
145 NSLOG(netsurf, INFO, "%p: set back state: %d\n", p, active);
146
148 gw = s->g;
149
151 if (active) {
152 toolbar[TOOLBAR_BT_SEARCH_BACK].ob_state &= ~OS_DISABLED;
153 } else {
154 toolbar[TOOLBAR_BT_SEARCH_BACK].ob_state |= OS_DISABLED;
155 }
158}
159
160
161static int apply_form(OBJECT *obj, struct s_search_form_state *s)
162{
163 char * cstr;
164
165 assert(s != NULL);
166
167 s->flags = 0;
168
169 if ((obj[TOOLBAR_CB_CASESENSE].ob_state & OS_SELECTED) != 0 ) {
171 }
172 if ((obj[TOOLBAR_CB_SHOWALL].ob_state & OS_SELECTED) != 0 ) {
174 }
175
176 cstr = gemtk_obj_get_text(obj, TOOLBAR_TB_SRCH);
177 snprintf(s->text, 32, "%s", cstr);
178 return ( 0 );
179
180}
181
182static void set_text(OBJECT *obj, short idx, char * text, int len)
183{
184 char spare[255];
185
186 if (len > 254) {
187 len = 254;
188 }
189 if (text != NULL) {
190 strncpy(spare, text, 254);
191 } else {
192 strcpy(spare, "");
193 }
194
195 set_string(obj, idx, spare);
196}
197
198
200{
201 if ((s->state.flags & SEARCH_FLAG_SHOWALL) != 0) {
202 obj[TOOLBAR_CB_SHOWALL].ob_state |= OS_SELECTED;
203 } else {
204 obj[TOOLBAR_CB_SHOWALL].ob_state &= ~OS_SELECTED;
205 }
206
207 if ((s->state.flags & SEARCH_FLAG_CASE_SENSITIVE) != 0) {
208 obj[TOOLBAR_CB_CASESENSE].ob_state |= OS_SELECTED;
209 } else {
210 obj[TOOLBAR_CB_CASESENSE].ob_state &= ~OS_SELECTED;
211 }
212
213 if (s->state.back_avail == false) {
214 obj[TOOLBAR_BT_SEARCH_BACK].ob_state |= OS_DISABLED;
215 } else {
216 obj[TOOLBAR_BT_SEARCH_BACK].ob_state &= ~OS_DISABLED;
217 }
218
219 TEDINFO *t = ((TEDINFO *)get_obspec(obj, TOOLBAR_TB_SRCH));
220 set_text(obj, TOOLBAR_TB_SRCH, s->state.text, t->te_txtlen);
221}
222
223
225{
226 if (s != NULL) {
227 NSLOG(netsurf, INFO, "session %p", s);
229 free(s);
230 }
231}
232
233
234/** checks for search parameters changes */
235static bool search_session_compare(struct s_search_form_session *s, OBJECT *obj)
236{
237 uint32_t flags_old;
238 uint32_t flags_mask = SEARCH_FLAG_SHOWALL | SEARCH_FLAG_CASE_SENSITIVE;
239 struct s_search_form_state cur;
240
241 assert(s != NULL && obj != NULL);
242
243 flags_old = s->state.flags;
244
245 apply_form(obj, &cur);
246 if ((cur.flags&flags_mask) != (flags_old&flags_mask)) {
247 return( true );
248 }
249
250 char * cstr;
251 cstr = gemtk_obj_get_text(obj, TOOLBAR_TB_SRCH);
252 if (cstr != NULL) {
253 if (strcmp(cstr, (char*)&s->state.text) != 0) {
254 return (true);
255 }
256 }
257
258 return( false );
259}
260
261
264{
265 assert(s!=null);
266 assert(input_window->browser->bw == s->g->browser->bw);
267
268 if (search_session_compare(s, obj)) {
270 apply_form(obj, &s->state);
271 }
272
273 /* get search direction manually: */
274 if ( (f&SEARCH_FLAG_FORWARDS) != 0 ) {
276 } else {
277 s->state.flags &= (~SEARCH_FLAG_FORWARDS);
278 }
279
281 s->state.flags,
282 gemtk_obj_get_text(obj, TOOLBAR_TB_SRCH));
283}
284
285
287 struct gui_window *gw)
288{
289 struct s_search_form_session *sfs;
290
291 sfs = calloc(1, sizeof(struct s_search_form_session));
292
293 assert(obj);
294 assert(sfs);
295
296 sfs->g = gw;
297
298 apply_form(obj, &sfs->state);
299
301
302 return(sfs);
303}
void gui_window_set_pointer(struct gui_window *g, gui_pointer_shape shape)
Change mouse pointer shape.
Definition: theme.c:216
OBJECT * toolbar_get_form(struct s_toolbar *tb)
Definition: toolbar.c:910
Browser window creation and manipulation interface.
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_SHOWALL
Definition: search.h:34
@ SEARCH_FLAG_FORWARDS
Definition: search.h:32
static int apply_form(OBJECT *obj, struct s_search_form_state *s)
Definition: search.c:161
struct gui_window * input_window
Definition: gui.c:73
void nsatari_search_perform(struct s_search_form_session *s, OBJECT *obj, search_flags_t f)
Definition: search.c:262
struct s_search_form_session * nsatari_search_session_create(OBJECT *obj, struct gui_window *gw)
Definition: search.c:286
struct gui_search_table * atari_search_table
Definition: search.c:59
static void nsatari_search_set_hourglass(bool active, void *p)
display hourglass while searching
Definition: search.c:80
void nsatari_search_session_destroy(struct s_search_form_session *s)
Definition: search.c:224
void nsatari_search_set_forward_state(bool active, void *p)
activate search forwards button in gui
Definition: search.c:111
static bool search_session_compare(struct s_search_form_session *s, OBJECT *obj)
checks for search parameters changes
Definition: search.c:235
static void nsatari_search_set_status(bool found, void *p)
Change the displayed search status.
Definition: search.c:68
void nsatari_search_set_back_state(bool active, void *p)
activate search back button in gui
Definition: search.c:138
void nsatari_search_restore_form(struct s_search_form_session *s, OBJECT *obj)
Definition: search.c:199
static void nsatari_search_add_recent(const char *string, void *p)
add search string to recent searches list front is at liberty how to implement the bare notification ...
Definition: search.c:100
static struct gui_search_table search_table
Definition: search.c:51
static void set_text(OBJECT *obj, short idx, char *text, int len)
Definition: search.c:182
struct s_search_form_session * SEARCH_FORM_SESSION
Definition: search.h:41
@ GUI_POINTER_PROGRESS
Definition: mouse.h:108
@ GUI_POINTER_DEFAULT
Definition: mouse.h:90
Interface to platform-specific search operations.
#define NSLOG(catname, level, logmsg, args...)
Definition: log.h:116
Localised message support (interface).
void window_schedule_redraw_grect(ROOTWIN *rootwin, GRECT *area)
Definition: rootwin.c:807
void window_get_grect(ROOTWIN *rootwin, enum browser_area_e which, GRECT *d)
Definition: rootwin.c:601
@ BROWSER_AREA_SEARCH
Definition: rootwin.h:38
Interface to utility string handling.
function table for page text search.
Definition: search.h:31
first entry in window list
Definition: gui.c:296
struct fbtk_widget_s * toolbar
Definition: gui.h:46
bool active
Whether the throbber should be active.
Definition: gui.h:89
struct s_gui_win_root * root
Definition: gui.h:148
struct s_browser * browser
Definition: gui.h:149
struct browser_window * bw
Definition: gui.h:137
CMP_TOOLBAR toolbar
Definition: gui.h:122
struct gui_window * g
Definition: search.h:37
struct s_search_form_state state
Definition: search.h:38
uint32_t flags
Definition: search.h:32
char text[32]
Definition: search.h:31
static nserror text(const struct redraw_context *ctx, const struct plot_font_style *fstyle, int x, int y, const char *text, size_t length)
Text plotting.
Definition: plot.c:978