2 * This file is part of DisOrder
3 * Copyright (C) 2008 Richard Kettlewell
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 /** @file disobedience/search.c
21 * @brief Search support
24 * - cleverer focus to implement typeahead find
26 * - make arrow buttons work
27 * - delay search initiation until keyboard goes quiet for a bit
28 * (so that you don't get all the 'lo' tracks when you search for 'love')
30 #include "disobedience.h"
33 static GtkWidget *choose_search_entry;
34 static GtkWidget *choose_next;
35 static GtkWidget *choose_prev;
36 static GtkWidget *choose_clear;
38 /** @brief True if a search command is in flight */
39 static int choose_searching;
41 /** @brief True if in-flight search is now known to be obsolete */
42 static int choose_search_obsolete;
44 /** @brief Hash of all search result */
45 static hash *choose_search_hash;
47 /** @brief List of invisible search results
49 * This only lists search results not yet known to be visible, and is
52 static char **choose_search_results;
54 /** @brief Length of @ref choose_search_results */
55 static int choose_n_search_results;
57 /** @brief Row references for search results */
58 static GtkTreeRowReference **choose_search_references;
60 /** @brief Length of @ref choose_search_references */
61 static int choose_n_search_references;
63 /** @brief Event handle for monitoring newly inserted tracks */
64 static event_handle choose_inserted_handle;
66 static void choose_search_entry_changed(GtkEditable *editable,
69 int choose_is_search_result(const char *track) {
70 return choose_search_hash && hash_find(choose_search_hash, track);
73 /** @brief Called when the cancel search button is clicked */
74 static void choose_clear_clicked(GtkButton attribute((unused)) *button,
75 gpointer attribute((unused)) userdata) {
76 gtk_entry_set_text(GTK_ENTRY(choose_search_entry), "");
77 /* The changed signal will do the rest of the work for us */
80 static int is_prefix(const char *dir, const char *track) {
81 size_t nd = strlen(dir);
85 && !strncmp(track, dir, nd))
91 /** @brief Do some work towards making @p track visible
92 * @return True if we made it visible or it was missing
94 static int choose_make_one_visible(const char *track) {
95 //fprintf(stderr, " choose_make_one_visible %s\n", track);
96 /* We walk through nodes at the top level looking for directories that are
97 * prefixes of the target track.
99 * - if we find one and it's expanded we walk through its children
100 * - if we find one and it's NOT expanded then we expand it, and arrange
102 * - if we don't find one then we're probably out of date
105 gboolean itv = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(choose_store),
108 const char *dir = choose_get_track(it);
110 //fprintf(stderr, " %s\n", dir);
113 itv = gtk_tree_model_iter_next(GTK_TREE_MODEL(choose_store), it);
116 GtkTreePath *path = gtk_tree_model_get_path(GTK_TREE_MODEL(choose_store),
118 if(!strcmp(dir, track)) {
119 /* We found the track. If everything above it was expanded, it will be
120 * too. So we can report it as visible. */
121 //fprintf(stderr, " found %s\n", track);
122 choose_search_references[choose_n_search_references++]
123 = gtk_tree_row_reference_new(GTK_TREE_MODEL(choose_store), path);
124 gtk_tree_path_free(path);
127 if(is_prefix(dir, track)) {
128 /* We found a prefix of the target track. */
129 //fprintf(stderr, " is a prefix\n");
130 const gboolean expanded
131 = gtk_tree_view_row_expanded(GTK_TREE_VIEW(choose_view), path);
133 //fprintf(stderr, " is apparently expanded\n");
134 /* This directory is expanded, let's make like Augustus Gibbons and
135 * take it to the next level. */
136 GtkTreeIter child[1]; /* don't know if parent==iter allowed */
137 itv = gtk_tree_model_iter_children(GTK_TREE_MODEL(choose_store),
141 if(choose_is_placeholder(it)) {
142 //fprintf(stderr, " %s is expanded, has a placeholder child\n", dir);
143 /* We assume that placeholder children of expanded rows are about to
145 gtk_tree_path_free(path);
149 //fprintf(stderr, " requesting expansion of %s\n", dir);
150 /* Track is below a non-expanded directory. So let's expand it.
151 * choose_make_visible() will arrange a revisit in due course. */
152 gtk_tree_view_expand_row(GTK_TREE_VIEW(choose_view),
155 gtk_tree_path_free(path);
156 /* TODO: the old version would remember which rows had been expanded
157 * just to show search results and collapse them again. We should
158 * probably do that. */
162 itv = gtk_tree_model_iter_next(GTK_TREE_MODEL(choose_store), it);
163 gtk_tree_path_free(path);
165 /* If we reach the end then we didn't find the track at all. */
166 fprintf(stderr, "choose_make_one_visible: could not find %s\n",
171 /** @brief Compare two GtkTreeRowReferences
173 * Not very efficient since it does multiple memory operations per
176 static int choose_compare_references(const void *av, const void *bv) {
177 GtkTreeRowReference *a = *(GtkTreeRowReference **)av;
178 GtkTreeRowReference *b = *(GtkTreeRowReference **)bv;
179 GtkTreePath *pa = gtk_tree_row_reference_get_path(a);
180 GtkTreePath *pb = gtk_tree_row_reference_get_path(b);
181 const int rc = gtk_tree_path_compare(pa, pb);
182 gtk_tree_path_free(pa);
183 gtk_tree_path_free(pb);
187 /** @brief Move the cursor to @p ref
188 * @return 0 on success, nonzero if @p ref has gone stale
190 static int choose_set_cursor(GtkTreeRowReference *ref) {
191 GtkTreePath *path = gtk_tree_row_reference_get_path(ref);
194 gtk_tree_view_set_cursor(GTK_TREE_VIEW(choose_view), path, NULL, FALSE);
195 gtk_tree_path_free(path);
199 /** @brief Do some work towards ensuring that all search results are visible
201 * Assumes there's at least one results!
203 static void choose_make_visible(const char attribute((unused)) *event,
204 void attribute((unused)) *eventdata,
205 void attribute((unused)) *callbackdata) {
206 //fprintf(stderr, "choose_make_visible\n");
209 for(int n = 0; n < choose_n_search_results; ++n) {
210 if(!choose_search_results[n])
212 if(choose_make_one_visible(choose_search_results[n]))
213 choose_search_results[n] = 0;
217 //fprintf(stderr, "remaining=%d\n", remaining);
219 /* If there's work left to be done make sure we get a callback when
220 * something changes */
221 if(!choose_inserted_handle)
222 choose_inserted_handle = event_register("choose-inserted-tracks",
223 choose_make_visible, 0);
225 /* Suppress callbacks if there's nothing more to do */
226 event_cancel(choose_inserted_handle);
227 choose_inserted_handle = 0;
228 /* We've expanded everything, now we can mess with the cursor */
229 //fprintf(stderr, "sort %d references\n", choose_n_search_references);
230 qsort(choose_search_references,
231 choose_n_search_references,
232 sizeof (GtkTreeRowReference *),
233 choose_compare_references);
234 choose_set_cursor(choose_search_references[0]);
238 /** @brief Called with search results */
239 static void choose_search_completed(void attribute((unused)) *v,
241 int nvec, char **vec) {
242 //fprintf(stderr, "choose_search_completed\n");
244 popup_protocol_error(0, error);
247 choose_searching = 0;
248 /* If the search was obsoleted initiate another one */
249 if(choose_search_obsolete) {
250 choose_search_obsolete = 0;
251 choose_search_entry_changed(0, 0);
254 //fprintf(stderr, "*** %d search results\n", nvec);
255 choose_search_hash = hash_new(1);
257 for(int n = 0; n < nvec; ++n)
258 hash_add(choose_search_hash, vec[n], "", HASH_INSERT);
259 /* Stash results for choose_make_visible */
260 choose_n_search_results = nvec;
261 choose_search_results = vec;
262 /* Make a big-enough buffer for the results row reference list */
263 choose_n_search_references = 0;
264 choose_search_references = xcalloc(nvec, sizeof (GtkTreeRowReference *));
265 /* Start making rows visible */
266 choose_make_visible(0, 0, 0);
268 event_raise("search-results-changed", 0);
271 /** @brief Called when the search entry changes */
272 static void choose_search_entry_changed
273 (GtkEditable attribute((unused)) *editable,
274 gpointer attribute((unused)) user_data) {
275 //fprintf(stderr, "choose_search_entry_changed\n");
276 /* If a search is in flight don't initiate a new one until it comes back */
277 if(choose_searching) {
278 choose_search_obsolete = 1;
281 char *terms = xstrdup(gtk_entry_get_text(GTK_ENTRY(choose_search_entry)));
282 /* Strip leading and trailing space */
283 while(*terms == ' ') ++terms;
284 char *e = terms + strlen(terms);
285 while(e > terms && e[-1] == ' ') --e;
288 /* Nothing to search for. Fake a completion call. */
289 choose_search_completed(0, 0, 0, 0);
292 if(disorder_eclient_search(client, choose_search_completed, terms, 0)) {
293 /* Bad search terms. Fake a completion call. */
294 choose_search_completed(0, 0, 0, 0);
297 choose_searching = 1;
300 static void choose_next_clicked(GtkButton attribute((unused)) *button,
301 gpointer attribute((unused)) userdata) {
302 fprintf(stderr, "next\n"); /* TODO */
305 static void choose_prev_clicked(GtkButton attribute((unused)) *button,
306 gpointer attribute((unused)) userdata) {
307 fprintf(stderr, "prev\n"); /* TODO */
310 /** @brief Create the search widget */
311 GtkWidget *choose_search_widget(void) {
313 /* Text entry box for search terms */
314 choose_search_entry = gtk_entry_new();
315 gtk_widget_set_style(choose_search_entry, tool_style);
316 g_signal_connect(choose_search_entry, "changed",
317 G_CALLBACK(choose_search_entry_changed), 0);
318 gtk_tooltips_set_tip(tips, choose_search_entry,
319 "Enter search terms here; search is automatic", "");
321 /* Cancel button to clear the search */
322 choose_clear = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
323 gtk_widget_set_style(choose_clear, tool_style);
324 g_signal_connect(G_OBJECT(choose_clear), "clicked",
325 G_CALLBACK(choose_clear_clicked), 0);
326 gtk_tooltips_set_tip(tips, choose_clear, "Clear search terms", "");
328 /* Up and down buttons to find previous/next results; initially they are not
329 * usable as there are no search results. */
330 choose_prev = iconbutton("up.png", "Previous search result");
331 g_signal_connect(G_OBJECT(choose_prev), "clicked",
332 G_CALLBACK(choose_prev_clicked), 0);
333 gtk_widget_set_style(choose_prev, tool_style);
334 gtk_widget_set_sensitive(choose_prev, 0);
335 choose_next = iconbutton("down.png", "Next search result");
336 g_signal_connect(G_OBJECT(choose_next), "clicked",
337 G_CALLBACK(choose_next_clicked), 0);
338 gtk_widget_set_style(choose_next, tool_style);
339 gtk_widget_set_sensitive(choose_next, 0);
341 /* Pack the search tools button together on a line */
342 GtkWidget *hbox = gtk_hbox_new(FALSE/*homogeneous*/, 1/*spacing*/);
343 gtk_box_pack_start(GTK_BOX(hbox), choose_search_entry,
344 TRUE/*expand*/, TRUE/*fill*/, 0/*padding*/);
345 gtk_box_pack_start(GTK_BOX(hbox), choose_prev,
346 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
347 gtk_box_pack_start(GTK_BOX(hbox), choose_next,
348 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
349 gtk_box_pack_start(GTK_BOX(hbox), choose_clear,
350 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);