NetSurf
search.c
Go to the documentation of this file.
1/*
2 * Copyright 2019 Vincent Sanders <vince@netsurf-browser.org>
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/**
21 * \file
22 * find in page gtk frontend implementation
23 *
24 * \todo this whole thing should be named find rather than search as
25 * that generally means web search and is confusing.
26 */
27
28#include <stdlib.h>
29#include <stdbool.h>
30#include <gtk/gtk.h>
31
32#include "utils/nsoption.h"
33#include "netsurf/search.h"
34#include "desktop/search.h"
35
36#include "gtk/compat.h"
37#include "gtk/toolbar_items.h"
38#include "gtk/window.h"
39#include "gtk/search.h"
40
41
42struct gtk_search {
43 GtkToolbar *bar;
44 GtkEntry *entry;
45 GtkToolButton *back;
46 GtkToolButton *forward;
47 GtkToolButton *close;
48 GtkCheckButton *checkAll;
49 GtkCheckButton *caseSens;
50
52};
53
54/**
55 * activate search forwards button in gui.
56 *
57 * \param active activate/inactivate
58 * \param search the gtk search context
59 */
60static void nsgtk_search_set_forward_state(bool active, struct gtk_search *search)
61{
62 gtk_widget_set_sensitive(GTK_WIDGET(search->forward), active);
63}
64
65
66/**
67 * activate search back button in gui.
68 *
69 * \param active activate/inactivate
70 * \param search the gtk search context
71 */
72static void nsgtk_search_set_back_state(bool active, struct gtk_search *search)
73{
74 gtk_widget_set_sensitive(GTK_WIDGET(search->back), active);
75}
76
77
78/**
79 * connected to the search forward button
80 */
81static gboolean
82nsgtk_search_forward_button_clicked(GtkWidget *widget, gpointer data)
83{
84 struct gtk_search *search;
85 search_flags_t flags;
86
87 search = (struct gtk_search *)data;
88
90
91 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(search->caseSens))) {
93 }
94
95 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(search->checkAll))) {
96 flags |= SEARCH_FLAG_SHOWALL;
97 }
98
99 browser_window_search(search->bw, search, flags,
100 gtk_entry_get_text(search->entry));
101
102 return TRUE;
103}
104
105/**
106 * connected to the search back button
107 */
108static gboolean
109nsgtk_search_back_button_clicked(GtkWidget *widget, gpointer data)
110{
111 struct gtk_search *search;
112 search_flags_t flags;
113
114 search = (struct gtk_search *)data;
115
116 flags = 0;
117
118 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(search->caseSens))) {
120 }
121
122 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(search->checkAll))) {
123 flags |= SEARCH_FLAG_SHOWALL;
124 }
125
126 browser_window_search(search->bw, search, flags,
127 gtk_entry_get_text(search->entry));
128
129 return TRUE;
130}
131
132/**
133 * connected to the search close button
134 */
135static gboolean
136nsgtk_search_close_button_clicked(GtkWidget *widget, gpointer data)
137{
138 struct gtk_search *search;
139
140 search = (struct gtk_search *)data;
141
143
144 return TRUE;
145}
146
147
148/**
149 * connected to the search entry [typing]
150 */
151static gboolean nsgtk_search_entry_changed(GtkWidget *widget, gpointer data)
152{
153 struct gtk_search *search;
154 search_flags_t flags;
155
156 search = (struct gtk_search *)data;
157
158 flags = 0;
159
160 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(search->caseSens))) {
162 }
163
164 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(search->checkAll))) {
165 flags |= SEARCH_FLAG_SHOWALL;
166 }
167
168 browser_window_search(search->bw, search, flags,
169 gtk_entry_get_text(search->entry));
170
171 return TRUE;
172}
173
174/**
175 * connected to the search entry [return key]
176 */
177static gboolean nsgtk_search_entry_activate(GtkWidget *widget, gpointer data)
178{
179 struct gtk_search *search;
180 search_flags_t flags;
181
182 search = (struct gtk_search *)data;
183
184 flags = SEARCH_FLAG_FORWARDS;
185
186 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(search->caseSens))) {
188 }
189
190 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(search->checkAll))) {
191 flags |= SEARCH_FLAG_SHOWALL;
192 }
193
194 browser_window_search(search->bw, search, flags,
195 gtk_entry_get_text(search->entry));
196
197 return FALSE;
198}
199
200/**
201 * allows escape key to close search bar too
202 */
203static gboolean
204nsgtk_search_entry_key(GtkWidget *widget, GdkEventKey *event, gpointer data)
205{
206 if (event->keyval == GDK_KEY(Escape)) {
207 struct gtk_search *search;
208 search = (struct gtk_search *)data;
209
211 }
212 return FALSE;
213}
214
215
219};
220
222
223
224/* exported interface documented in gtk/scaffolding.h */
226{
227 gboolean vis;
228
230
231 g_object_get(G_OBJECT(search->bar), "visible", &vis, NULL);
232 if (vis) {
233 gtk_widget_hide(GTK_WIDGET(search->bar));
234 } else {
235 gtk_widget_show(GTK_WIDGET(search->bar));
236 gtk_widget_grab_focus(GTK_WIDGET(search->entry));
237 nsgtk_search_entry_changed(GTK_WIDGET(search->entry), search);
238 }
239
240 return NSERROR_OK;
241}
242
243
244/* exported interface documented in gtk/search.h */
246{
247 switch (nsoption_int(button_type)) {
248
249 case 1: /* Small icons */
250 gtk_toolbar_set_style(GTK_TOOLBAR(search->bar),
251 GTK_TOOLBAR_ICONS);
252 gtk_toolbar_set_icon_size(GTK_TOOLBAR(search->bar),
253 GTK_ICON_SIZE_SMALL_TOOLBAR);
254 break;
255
256 case 2: /* Large icons */
257 gtk_toolbar_set_style(GTK_TOOLBAR(search->bar),
258 GTK_TOOLBAR_ICONS);
259 gtk_toolbar_set_icon_size(GTK_TOOLBAR(search->bar),
260 GTK_ICON_SIZE_LARGE_TOOLBAR);
261 break;
262
263 case 3: /* Large icons with text */
264 gtk_toolbar_set_style(GTK_TOOLBAR(search->bar),
265 GTK_TOOLBAR_BOTH);
266 gtk_toolbar_set_icon_size(GTK_TOOLBAR(search->bar),
267 GTK_ICON_SIZE_LARGE_TOOLBAR);
268 break;
269
270 case 4: /* Text icons only */
271 gtk_toolbar_set_style(GTK_TOOLBAR(search->bar),
272 GTK_TOOLBAR_TEXT);
273 break;
274
275 default:
276 break;
277 }
278 return NSERROR_OK;
279}
280
281
282/* exported interface documented in gtk/search.h */
284nsgtk_search_create(GtkBuilder *builder,
285 struct browser_window *bw,
286 struct gtk_search **search_out)
287{
288 struct gtk_search *search;
289
290 search = malloc(sizeof(struct gtk_search));
291 if (search == NULL) {
292 return NSERROR_NOMEM;
293 }
294
295 search->bw = bw;
296
297 search->bar = GTK_TOOLBAR(gtk_builder_get_object(builder, "findbar"));
298 search->entry = GTK_ENTRY(gtk_builder_get_object(builder, "Find"));
299 search->back = GTK_TOOL_BUTTON(gtk_builder_get_object(builder,
300 "FindBack"));
301 search->forward = GTK_TOOL_BUTTON(gtk_builder_get_object(builder,
302 "FindForward"));
303 search->close = GTK_TOOL_BUTTON(gtk_builder_get_object(builder,
304 "FindClose"));
305 search->checkAll = GTK_CHECK_BUTTON(gtk_builder_get_object(builder,
306 "FindHighlightAll"));
307 search->caseSens = GTK_CHECK_BUTTON(gtk_builder_get_object(builder,
308 "FindMatchCase"));
309
310 g_signal_connect(search->forward,
311 "clicked",
313 search);
314
315 g_signal_connect(search->back,
316 "clicked",
318 search);
319
320 g_signal_connect(search->entry,
321 "changed",
322 G_CALLBACK(nsgtk_search_entry_changed),
323 search);
324
325 g_signal_connect(search->entry,
326 "activate",
327 G_CALLBACK(nsgtk_search_entry_activate),
328 search);
329
330 g_signal_connect(search->entry,
331 "key-press-event",
332 G_CALLBACK(nsgtk_search_entry_key),
333 search);
334
335 g_signal_connect(search->close,
336 "clicked",
338 search);
339
340 g_signal_connect(search->caseSens,
341 "toggled",
342 G_CALLBACK(nsgtk_search_entry_changed),
343 search);
344
345 g_signal_connect(search->checkAll,
346 "toggled",
347 G_CALLBACK(nsgtk_search_entry_changed),
348 search);
349
350 nsgtk_search_restyle(search);
351
352
353 *search_out = search;
354
355 return NSERROR_OK;
356}
Compatibility functions for older GTK versions (interface)
#define GDK_KEY(symbol)
Definition: compat.h:152
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
nserror
Enumeration of error codes.
Definition: errors.h:29
@ NSERROR_NOMEM
Memory exhaustion.
Definition: errors.h:32
@ NSERROR_OK
No error.
Definition: errors.h:30
nserror nsgtk_search_create(GtkBuilder *builder, struct browser_window *bw, struct gtk_search **search_out)
create text search context
Definition: search.c:284
static gboolean nsgtk_search_back_button_clicked(GtkWidget *widget, gpointer data)
connected to the search back button
Definition: search.c:109
struct gui_search_table * nsgtk_search_table
Definition: search.c:221
static gboolean nsgtk_search_forward_button_clicked(GtkWidget *widget, gpointer data)
connected to the search forward button
Definition: search.c:82
nserror nsgtk_search_restyle(struct gtk_search *search)
update search toolbar size and style
Definition: search.c:245
nserror nsgtk_search_toggle_visibility(struct gtk_search *search)
toggle search bar visibility
Definition: search.c:225
static void nsgtk_search_set_back_state(bool active, struct gtk_search *search)
activate search back button in gui.
Definition: search.c:72
static gboolean nsgtk_search_close_button_clicked(GtkWidget *widget, gpointer data)
connected to the search close button
Definition: search.c:136
static gboolean nsgtk_search_entry_key(GtkWidget *widget, GdkEventKey *event, gpointer data)
allows escape key to close search bar too
Definition: search.c:204
static gboolean nsgtk_search_entry_activate(GtkWidget *widget, gpointer data)
connected to the search entry [return key]
Definition: search.c:177
static struct gui_search_table search_table
Definition: search.c:216
static void nsgtk_search_set_forward_state(bool active, struct gtk_search *search)
activate search forwards button in gui.
Definition: search.c:60
static gboolean nsgtk_search_entry_changed(GtkWidget *widget, gpointer data)
connected to the search entry [typing]
Definition: search.c:151
free text page search for gtk interface
Interface to platform-specific search operations.
Browser window data.
GtkCheckButton * checkAll
Definition: search.c:48
GtkEntry * entry
Definition: search.c:44
struct browser_window * bw
Definition: search.c:51
GtkCheckButton * caseSens
Definition: search.c:49
GtkToolButton * forward
Definition: search.c:46
GtkToolbar * bar
Definition: search.c:43
GtkToolButton * close
Definition: search.c:47
GtkToolButton * back
Definition: search.c:45
function table for page text search.
Definition: search.h:31
void(* back_state)(bool active, void *p)
activate search back button in gui
Definition: search.h:72
void(* forward_state)(bool active, void *p)
activate search forwards button in gui
Definition: search.h:64
Option reading and saving interface.
#define nsoption_int(OPTION)
Get the value of an integer option.
Definition: nsoption.h:313