NetSurf
completion.c
Go to the documentation of this file.
1/*
2 * Copyright 2006 Rob Kendrick <rjek@rjek.com>
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 * \file
21 * Implementation of url entry completion.
22 */
23
24#include <stdlib.h>
25
26#include "utils/log.h"
27#include "utils/messages.h"
28#include "utils/nsoption.h"
29#include "utils/nsurl.h"
30#include "netsurf/url_db.h"
32#include "desktop/searchweb.h"
33
34#include "gtk/compat.h"
35#include "gtk/warn.h"
36#include "gtk/scaffolding.h"
37#include "gtk/toolbar_items.h"
38#include "gtk/window.h"
39#include "gtk/completion.h"
40
42
44 /**
45 * callback to obtain a browser window for navigation
46 */
47 struct browser_window *(*get_bw)(void *ctx);
48
49 /**
50 * context passed to get_bw function
51 */
53};
54
55/**
56 * completion row matcher
57 */
58static gboolean nsgtk_completion_match(GtkEntryCompletion *completion,
59 const gchar *key,
60 GtkTreeIter *iter,
61 gpointer user_data)
62{
63 /* the completion list is modified to only contain valid
64 * entries so this simply returns TRUE to indicate all rows
65 * are in the list should be shown.
66 */
67 return TRUE;
68}
69
70
71/**
72 * callback for each entry to add to completion list
73 */
74static bool
76{
77 GtkTreeIter iter;
78
79 if (data->visits != 0) {
80 gtk_list_store_append(nsgtk_completion_list, &iter);
81 gtk_list_store_set(nsgtk_completion_list, &iter, 0,
82 nsurl_access(url), -1);
83 }
84 return true;
85}
86
87/**
88 * event handler for when a completion suggestion is selected.
89 */
90static gboolean
91nsgtk_completion_match_select(GtkEntryCompletion *widget,
92 GtkTreeModel *model,
93 GtkTreeIter *iter,
94 gpointer data)
95{
96 struct nsgtk_completion_ctx *cb_ctx;
97 GValue value = G_VALUE_INIT;
98 struct browser_window *bw;
99 nserror ret;
100 nsurl *url;
101
102 cb_ctx = data;
103 bw = cb_ctx->get_bw(cb_ctx->get_bw_ctx);
104
105 gtk_tree_model_get_value(model, iter, 0, &value);
106
107 ret = search_web_omni(g_value_get_string(&value),
109 &url);
110
111 g_value_unset(&value);
112
113 if (ret == NSERROR_OK) {
115 url, NULL, BW_NAVIGATE_HISTORY,
116 NULL, NULL, NULL);
117 nsurl_unref(url);
118 }
119 if (ret != NSERROR_OK) {
121 }
122
123 return TRUE;
124}
125
126/* exported interface documented in completion.h */
128{
129 nsgtk_completion_list = gtk_list_store_new(1, G_TYPE_STRING);
130
131}
132
133/* exported interface documented in completion.h */
134gboolean nsgtk_completion_update(GtkEntry *entry)
135{
136 gtk_list_store_clear(nsgtk_completion_list);
137
138 if (nsoption_bool(url_suggestion) == true) {
139 urldb_iterate_partial(gtk_entry_get_text(entry),
141 }
142
143 return TRUE;
144}
145
146/* exported interface documented in completion.h */
149 struct browser_window *(*get_bw)(void *ctx),
150 void *get_bw_ctx)
151{
152 GtkEntryCompletion *completion;
153 struct nsgtk_completion_ctx *cb_ctx;
154
155 cb_ctx = calloc(1, sizeof(struct nsgtk_completion_ctx));
156 cb_ctx->get_bw = get_bw;
157 cb_ctx->get_bw_ctx = get_bw_ctx;
158
159 completion = gtk_entry_get_completion(entry);
160
161 gtk_entry_completion_set_match_func(completion,
162 nsgtk_completion_match, NULL, NULL);
163
164 gtk_entry_completion_set_model(completion,
165 GTK_TREE_MODEL(nsgtk_completion_list));
166
167 gtk_entry_completion_set_text_column(completion, 0);
168
169 gtk_entry_completion_set_minimum_key_length(completion, 1);
170
171 /* enable popup for completion */
172 gtk_entry_completion_set_popup_completion(completion, TRUE);
173
174 /* when selected callback */
175 g_signal_connect(G_OBJECT(completion),
176 "match-selected",
178 cb_ctx);
179
180 g_object_set(G_OBJECT(completion),
181 "popup-set-width", TRUE,
182 "popup-single-match", TRUE,
183 NULL);
184
185 return NSERROR_OK;
186}
Browser window creation and manipulation interface.
nserror browser_window_navigate(struct browser_window *bw, struct nsurl *url, struct nsurl *referrer, enum browser_window_nav_flags flags, char *post_urlenc, struct fetch_multipart_data *post_multipart, struct hlcache_handle *parent)
Start fetching a page in a browser window.
@ BW_NAVIGATE_HISTORY
this will form a new history node (don't set for back/reload/etc)
Compatibility functions for older GTK versions (interface)
#define G_VALUE_INIT
Definition: compat.h:82
gboolean nsgtk_completion_update(GtkEntry *entry)
update completion list store.
Definition: completion.c:134
void nsgtk_completion_init(void)
initialise completion list store
Definition: completion.c:127
static gboolean nsgtk_completion_match(GtkEntryCompletion *completion, const gchar *key, GtkTreeIter *iter, gpointer user_data)
completion row matcher
Definition: completion.c:58
nserror nsgtk_completion_connect_signals(GtkEntry *entry, struct browser_window *(*get_bw)(void *ctx), void *get_bw_ctx)
connect signals on entry completion
Definition: completion.c:148
static gboolean nsgtk_completion_match_select(GtkEntryCompletion *widget, GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
event handler for when a completion suggestion is selected.
Definition: completion.c:91
GtkListStore * nsgtk_completion_list
Definition: completion.c:41
static bool nsgtk_completion_udb_callback(nsurl *url, const struct url_data *data)
callback for each entry to add to completion list
Definition: completion.c:75
Interface to url entry completion.
nserror search_web_omni(const char *term, enum search_web_omni_flags flags, struct nsurl **url_out)
Generate a nsurl from a search term.
Definition: searchweb.c:318
nserror
Enumeration of error codes.
Definition: errors.h:29
@ NSERROR_OK
No error.
Definition: errors.h:30
nserror nsgtk_warning(const char *warning, const char *detail)
Warn the user of an event.
Definition: gui.c:95
const char * messages_get_errorcode(nserror code)
lookup of a message by errorcode from the standard Messages hash.
Definition: messages.c:248
Localised message support (interface).
NetSurf URL handling (interface).
void nsurl_unref(nsurl *url)
Drop a reference to a NetSurf URL object.
const char * nsurl_access(const nsurl *url)
Access a NetSurf URL object as a string.
struct nsurl nsurl
NetSurf URL object.
Definition: nsurl.h:31
core web search facilities interface.
@ SEARCH_WEB_OMNI_NONE
no changes to default operation
Definition: searchweb.h:50
Browser window data.
struct browser_window * bw
void * get_bw_ctx
context passed to get_bw function
Definition: completion.c:52
struct browser_window *(* get_bw)(void *ctx)
callback to obtain a browser window for navigation
Definition: completion.c:47
unsigned int visits
Visit count.
Definition: url_db.h:38
Unified URL information database public interface.
void urldb_iterate_partial(const char *prefix, bool(*callback)(struct nsurl *url, const struct url_data *data))
Iterate over entries in the database which match the given prefix.
Option reading and saving interface.
#define nsoption_bool(OPTION)
Get the value of a boolean option.
Definition: nsoption.h:270