Redo the back/forward list hack
[xombrero.git] / completion.c
blobf7ef19d41457d87cf3c07030b2ba6cc6391c0a1a
1 /*
2 * Copyright (c) 2010, 2011 Marco Peereboom <marco@peereboom.us>
3 * Copyright (c) 2011 Stevan Andjelkovic <stevan@student.chalmers.se>
4 * Copyright (c) 2010, 2011 Edd Barrett <vext01@gmail.com>
5 * Copyright (c) 2011 Todd T. Fries <todd@fries.net>
6 * Copyright (c) 2011 Raphael Graf <r@undefined.ch>
7 * Copyright (c) 2011 Michal Mazurek <akfaew@jasminek.net>
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22 #include <xombrero.h>
24 gboolean
25 completion_select_cb(GtkEntryCompletion *widget, GtkTreeModel *model,
26 GtkTreeIter *iter, struct tab *t)
28 gchar *value;
30 /* XXX may require changes for GTK3 */
31 gtk_tree_model_get(model, iter, 0, &value, -1);
32 load_uri(t, value);
33 g_free(value);
35 return (FALSE);
38 gboolean
39 completion_hover_cb(GtkEntryCompletion *widget, GtkTreeModel *model,
40 GtkTreeIter *iter, struct tab *t)
42 gchar *value;
44 /* XXX may require changes for GTK3 */
45 gtk_tree_model_get(model, iter, 0, &value, -1);
46 gtk_entry_set_text(GTK_ENTRY(t->uri_entry), value);
47 gtk_editable_set_position(GTK_EDITABLE(t->uri_entry), -1);
48 g_free(value);
50 return (TRUE);
53 void
54 completion_add_uri(const gchar *uri)
56 GtkTreeIter iter;
58 /* add uri to list_store */
59 gtk_list_store_append(completion_model, &iter);
60 gtk_list_store_set(completion_model, &iter, 0, uri, -1);
63 gboolean
64 completion_match(GtkEntryCompletion *completion, const gchar *key,
65 GtkTreeIter *iter, gpointer user_data)
67 gchar *value;
68 gboolean match = FALSE;
70 gtk_tree_model_get(GTK_TREE_MODEL(completion_model), iter, 0, &value,
71 -1);
73 if (value == NULL)
74 return FALSE;
76 match = match_uri(value, key);
78 g_free(value);
79 return (match);
82 void
83 completion_add(struct tab *t)
85 /* enable completion for tab */
86 t->completion = gtk_entry_completion_new();
87 gtk_entry_completion_set_text_column(t->completion, 0);
88 gtk_entry_set_completion(GTK_ENTRY(t->uri_entry), t->completion);
89 gtk_entry_completion_set_model(t->completion,
90 GTK_TREE_MODEL(completion_model));
91 gtk_entry_completion_set_match_func(t->completion, completion_match,
92 NULL, NULL);
93 gtk_entry_completion_set_minimum_key_length(t->completion, 1);
94 gtk_entry_completion_set_inline_selection(t->completion, TRUE);
95 g_signal_connect(G_OBJECT (t->completion), "match-selected",
96 G_CALLBACK(completion_select_cb), t);
97 g_signal_connect(G_OBJECT (t->completion), "cursor-on-match",
98 G_CALLBACK(completion_hover_cb), t);