summaryrefslogtreecommitdiff
path: root/gtk/gtk_search.c
blob: 30075be02e2109554f0b7ecaac3cfffd24611292 (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
/*
 * Copyright 2009 Mark Benjamin <netsurf-browser.org.MarkBenjamin@dfgh.net>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf 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; version 2 of the License.
 *
 * NetSurf 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, see <http://www.gnu.org/licenses/>.
 */


 /** \file
 * Free text search (front component)
 */
#include <ctype.h>
#include <string.h>
#include <gdk/gdkkeysyms.h>
#include "gtk/gtk_search.h"
#include "gtk/gtk_scaffolding.h"
#include "gtk/gtk_window.h"
#include "utils/config.h"
#include "content/content.h"
#include "content/hlcache.h"
#include "desktop/browser.h"
#include "desktop/gui.h"
#include "desktop/search.h"
#include "desktop/searchweb.h"
#include "desktop/selection.h"
#include "render/box.h"
#include "render/html.h"
#include "utils/config.h"
#include "utils/log.h"
#include "utils/messages.h"
#include "utils/utils.h"

static void nsgtk_search_init(struct gtk_scaffolding *g);
static void nsgtk_search_set_status(bool found, void *p);
static void nsgtk_search_set_hourglass(bool active, void *p);
static void nsgtk_search_add_recent(const char *string, void *p);

static struct search_callbacks nsgtk_search_callbacks = {
	nsgtk_search_set_forward_state,
	nsgtk_search_set_back_state,
	nsgtk_search_set_status,
	nsgtk_search_set_hourglass,
	nsgtk_search_add_recent
};

/** connected to the search forward button */

gboolean nsgtk_search_forward_button_clicked(GtkWidget *widget, gpointer data)
{
	struct gtk_scaffolding *g = (struct gtk_scaffolding *)data;
	struct browser_window *bw = gui_window_get_browser_window(
			nsgtk_scaffolding_top_level(g));
	nsgtk_search_init(g);
	search_flags_t flags = SEARCH_FLAG_FORWARDS |
			(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
			nsgtk_scaffolding_search(g)->caseSens)) ?
			SEARCH_FLAG_CASE_SENSITIVE : 0) | 
			(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
			nsgtk_scaffolding_search(g)->checkAll)) ?
			SEARCH_FLAG_SHOWALL : 0);
	if (search_verify_new(bw, &nsgtk_search_callbacks, (void *)bw))
		search_step(bw->search_context, flags, gtk_entry_get_text(
				nsgtk_scaffolding_search(g)->entry));
	return TRUE;
}

/** connected to the search back button */

gboolean nsgtk_search_back_button_clicked(GtkWidget *widget, gpointer data)
{
	struct gtk_scaffolding *g = (struct gtk_scaffolding *)data;
	struct browser_window *bw = gui_window_get_browser_window(
			nsgtk_scaffolding_top_level(g));
	nsgtk_search_init(g);
	search_flags_t flags = 0 |(gtk_toggle_button_get_active(
			GTK_TOGGLE_BUTTON(
			nsgtk_scaffolding_search(g)->caseSens)) ?
			SEARCH_FLAG_CASE_SENSITIVE : 0) | 
			(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
			nsgtk_scaffolding_search(g)->checkAll)) ?
			SEARCH_FLAG_SHOWALL : 0);
	if (search_verify_new(bw, &nsgtk_search_callbacks, (void *)bw))
		search_step(bw->search_context, flags, gtk_entry_get_text(
				nsgtk_scaffolding_search(g)->entry));
	return TRUE;
}

/** preparatory code when the search bar is made visible initially */

void nsgtk_search_init(struct gtk_scaffolding *g)
{
	hlcache_handle *c;

	assert(gui_window_get_browser_window(nsgtk_scaffolding_top_level(g))
			!= NULL);

	c = gui_window_get_browser_window(nsgtk_scaffolding_top_level(g))->
			current_content;

	if ((!c) || (content_get_type(c) != CONTENT_HTML && 
			content_get_type(c) != CONTENT_TEXTPLAIN))
		return;
	
}

/** connected to the search close button */

gboolean nsgtk_search_close_button_clicked(GtkWidget *widget, gpointer data)
{
	struct gtk_scaffolding *g = (struct gtk_scaffolding *)data;
	nsgtk_scaffolding_toggle_search_bar_visibility(g);
	return TRUE;	
}

/** connected to the search entry [typing] */

gboolean nsgtk_search_entry_changed(GtkWidget *widget, gpointer data)
{
	nsgtk_scaffolding *g = (nsgtk_scaffolding *)data;
	struct browser_window *bw = gui_window_get_browser_window(
			nsgtk_scaffolding_top_level(g));
	if ((bw != NULL) && (bw->search_context != NULL))
		search_destroy_context(bw->search_context);
	nsgtk_search_set_forward_state(true, (void *)bw);
	nsgtk_search_set_back_state(true, (void *)bw);
	return TRUE;
}

/** connected to the search entry [return key] */

gboolean nsgtk_search_entry_activate(GtkWidget *widget, gpointer data)
{
	nsgtk_scaffolding *g = (nsgtk_scaffolding *)data;
	struct browser_window *bw = gui_window_get_browser_window(
			nsgtk_scaffolding_top_level(g));
	nsgtk_search_init(g);
	search_flags_t flags = SEARCH_FLAG_FORWARDS |
			(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
			nsgtk_scaffolding_search(g)->caseSens)) ?
			SEARCH_FLAG_CASE_SENSITIVE : 0) | 
			(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(
			nsgtk_scaffolding_search(g)->checkAll)) ?
			SEARCH_FLAG_SHOWALL : 0);
	if (search_verify_new(bw, &nsgtk_search_callbacks, (void *)bw))
		search_step(bw->search_context, flags, gtk_entry_get_text(
				nsgtk_scaffolding_search(g)->entry));
	return FALSE;
}

/** allows escape key to close search bar too */

gboolean nsgtk_search_entry_key(GtkWidget *widget, GdkEventKey *event, 
		gpointer data)
{
	if (event->keyval == GDK_Escape) {
		struct gtk_scaffolding *g = (struct gtk_scaffolding *)data;
		nsgtk_scaffolding_toggle_search_bar_visibility(g);
	}
	return FALSE;
}

/** connected to the websearch entry [return key] */

gboolean nsgtk_websearch_activate(GtkWidget *widget, gpointer data)
{
	struct gtk_scaffolding *g = (struct gtk_scaffolding *)data;
	temp_open_background = 0;
	search_web_new_window(gui_window_get_browser_window(
			nsgtk_scaffolding_top_level(g)),
			(char *)gtk_entry_get_text(GTK_ENTRY(
			nsgtk_scaffolding_websearch(g))));
	temp_open_background = -1;
	return TRUE;
}

/**
 * allows a click in the websearch entry field to clear the name of the
 * provider
 */

gboolean nsgtk_websearch_clear(GtkWidget *widget, GdkEventFocus *f, 
		gpointer data)
{
	struct gtk_scaffolding *g = (struct gtk_scaffolding *)data;
	gtk_editable_select_region(GTK_EDITABLE(
			nsgtk_scaffolding_websearch(g)), 0, -1);
	gtk_widget_grab_focus(GTK_WIDGET(nsgtk_scaffolding_websearch(g)));
	return TRUE;
}

/**
* Change the displayed search status.
* \param found  search pattern matched in text
* \param p the pointer sent to search_verify_new() / search_create_context()
*/

void nsgtk_search_set_status(bool found, void *p)
{
}

/**
* display hourglass while searching
* \param active start/stop indicator
* \param p the pointer sent to search_verify_new() / search_create_context()
*/

void nsgtk_search_set_hourglass(bool active, void *p)
{
}

/**
* add search string to recent searches list
* front is at liberty how to implement the bare notification
* should normally store a strdup() of the string;
* core gives no guarantee of the integrity of the const char *
* \param string search pattern
* \param p the pointer sent to search_verify_new() / search_create_context()
*/

void nsgtk_search_add_recent(const char *string, void *p)
{
}

/**
* activate search forwards button in gui
* \param active activate/inactivate
* \param p the pointer sent to search_verify_new() / search_create_context()
*/

void nsgtk_search_set_forward_state(bool active, void *p)
{
	struct browser_window *bw = (struct browser_window *)p;
	if ((bw != NULL) && (bw->window != NULL)) {
		struct gtk_scaffolding *g = nsgtk_get_scaffold(bw->window);
		gtk_widget_set_sensitive(GTK_WIDGET(nsgtk_scaffolding_search(
				g)->buttons[1]), active);
	}
}

/**
* activate search back button in gui
* \param active activate/inactivate
* \param p the pointer sent to search_verify_new() / search_create_context()
*/

void nsgtk_search_set_back_state(bool active, void *p)
{
	struct browser_window *bw = (struct browser_window *)p;
	if ((bw != NULL) && (bw->window != NULL)) {
		struct gtk_scaffolding *g = nsgtk_get_scaffold(bw->window);
		gtk_widget_set_sensitive(GTK_WIDGET(nsgtk_scaffolding_search(
				g)->buttons[0]), active);
	}
}