chiark / gitweb /
Doxygen-clean
[disorder] / disobedience / choose-search.c
CommitLineData
cfa78eaa
RK
1/*
2 * This file is part of DisOrder
3 * Copyright (C) 2008 Richard Kettlewell
4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
cfa78eaa 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
cfa78eaa
RK
8 * (at your option) any later version.
9 *
e7eb3a27
RK
10 * This program 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 *
cfa78eaa 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
cfa78eaa 17 */
59cf25c4 18/** @file disobedience/choose-search.c
bb9bcc90 19 * @brief Search support
bb9bcc90 20 */
cfa78eaa
RK
21#include "disobedience.h"
22#include "choose.h"
23
bd802f22
RK
24int choose_auto_expanding;
25
a59663d4 26GtkWidget *choose_search_entry;
cfa78eaa
RK
27static GtkWidget *choose_next;
28static GtkWidget *choose_prev;
29static GtkWidget *choose_clear;
30
31/** @brief True if a search command is in flight */
32static int choose_searching;
33
34/** @brief True if in-flight search is now known to be obsolete */
35static int choose_search_obsolete;
36
9eeb9f12
RK
37/** @brief Current search terms */
38static char *choose_search_terms;
39
cfa78eaa
RK
40/** @brief Hash of all search result */
41static hash *choose_search_hash;
42
2a9a65e4
RK
43/** @brief List of invisible search results
44 *
45 * This only lists search results not yet known to be visible, and is
46 * gradually depleted.
47 */
48static char **choose_search_results;
49
50/** @brief Length of @ref choose_search_results */
51static int choose_n_search_results;
52
53/** @brief Row references for search results */
54static GtkTreeRowReference **choose_search_references;
55
56/** @brief Length of @ref choose_search_references */
57static int choose_n_search_references;
58
59/** @brief Event handle for monitoring newly inserted tracks */
60static event_handle choose_inserted_handle;
61
9eeb9f12
RK
62/** @brief Time of last search entry keypress (or 0.0) */
63static struct timeval choose_search_last_keypress;
64
65/** @brief Timeout ID for search delay */
66static guint choose_search_timeout_id;
67
cfa78eaa
RK
68static void choose_search_entry_changed(GtkEditable *editable,
69 gpointer user_data);
30fc14d8
RK
70static gboolean choose_get_visible_range(GtkTreeView *tree_view,
71 GtkTreePath **startpathp,
72 GtkTreePath **endpathp);
cfa78eaa
RK
73
74int choose_is_search_result(const char *track) {
75 return choose_search_hash && hash_find(choose_search_hash, track);
76}
77
2a9a65e4
RK
78static int is_prefix(const char *dir, const char *track) {
79 size_t nd = strlen(dir);
80
81 if(nd < strlen(track)
82 && track[nd] == '/'
83 && !strncmp(track, dir, nd))
84 return 1;
85 else
86 return 0;
87}
88
89/** @brief Do some work towards making @p track visible
90 * @return True if we made it visible or it was missing
91 */
92static int choose_make_one_visible(const char *track) {
93 //fprintf(stderr, " choose_make_one_visible %s\n", track);
94 /* We walk through nodes at the top level looking for directories that are
95 * prefixes of the target track.
96 *
97 * - if we find one and it's expanded we walk through its children
98 * - if we find one and it's NOT expanded then we expand it, and arrange
99 * to be revisited
100 * - if we don't find one then we're probably out of date
101 */
102 GtkTreeIter it[1];
103 gboolean itv = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(choose_store),
104 it);
105 while(itv) {
106 const char *dir = choose_get_track(it);
107
108 //fprintf(stderr, " %s\n", dir);
109 if(!dir) {
110 /* Placeholder */
111 itv = gtk_tree_model_iter_next(GTK_TREE_MODEL(choose_store), it);
112 continue;
113 }
114 GtkTreePath *path = gtk_tree_model_get_path(GTK_TREE_MODEL(choose_store),
115 it);
116 if(!strcmp(dir, track)) {
117 /* We found the track. If everything above it was expanded, it will be
118 * too. So we can report it as visible. */
119 //fprintf(stderr, " found %s\n", track);
120 choose_search_references[choose_n_search_references++]
121 = gtk_tree_row_reference_new(GTK_TREE_MODEL(choose_store), path);
122 gtk_tree_path_free(path);
123 return 1;
124 }
125 if(is_prefix(dir, track)) {
126 /* We found a prefix of the target track. */
cab9a17c 127 //fprintf(stderr, " %s is a prefix\n", dir);
2a9a65e4
RK
128 const gboolean expanded
129 = gtk_tree_view_row_expanded(GTK_TREE_VIEW(choose_view), path);
130 if(expanded) {
131 //fprintf(stderr, " is apparently expanded\n");
132 /* This directory is expanded, let's make like Augustus Gibbons and
133 * take it to the next level. */
134 GtkTreeIter child[1]; /* don't know if parent==iter allowed */
135 itv = gtk_tree_model_iter_children(GTK_TREE_MODEL(choose_store),
136 child,
137 it);
138 *it = *child;
139 if(choose_is_placeholder(it)) {
140 //fprintf(stderr, " %s is expanded, has a placeholder child\n", dir);
141 /* We assume that placeholder children of expanded rows are about to
142 * be replaced */
143 gtk_tree_path_free(path);
144 return 0;
145 }
146 } else {
147 //fprintf(stderr, " requesting expansion of %s\n", dir);
148 /* Track is below a non-expanded directory. So let's expand it.
bd802f22
RK
149 * choose_make_visible() will arrange a revisit in due course.
150 *
151 * We mark the row as auto-expanded.
152 */
153 ++choose_auto_expanding;
2a9a65e4
RK
154 gtk_tree_view_expand_row(GTK_TREE_VIEW(choose_view),
155 path,
156 FALSE/*open_all*/);
157 gtk_tree_path_free(path);
bd802f22 158 --choose_auto_expanding;
2a9a65e4
RK
159 return 0;
160 }
161 } else
162 itv = gtk_tree_model_iter_next(GTK_TREE_MODEL(choose_store), it);
163 gtk_tree_path_free(path);
164 }
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",
167 track);
168 return 1;
169}
170
171/** @brief Compare two GtkTreeRowReferences
172 *
173 * Not very efficient since it does multiple memory operations per
174 * comparison!
175 */
176static 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);
184 return rc;
185}
186
b96f65cf
RK
187/** @brief Make @p path visible
188 * @param path Row reference to make visible
189 * @param row_align Row alignment (or -ve)
2a9a65e4 190 * @return 0 on success, nonzero if @p ref has gone stale
b96f65cf
RK
191 *
192 * If @p row_align is negative no row alignemt is performed. Otherwise
193 * it must be between 0 (the top) and 1 (the bottom).
194 */
195static int choose_make_path_visible(GtkTreePath *path,
196 gfloat row_align) {
197 /* Make sure that the target's parents are all expanded */
198 gtk_tree_view_expand_to_path(GTK_TREE_VIEW(choose_view), path);
30fc14d8
RK
199 /* Find out what's currently visible */
200 GtkTreePath *startpath, *endpath;
201 choose_get_visible_range(GTK_TREE_VIEW(choose_view), &startpath, &endpath);
b96f65cf 202 /* Make sure the target is visible */
30fc14d8
RK
203 if(gtk_tree_path_compare(path, startpath) < 0
204 || gtk_tree_path_compare(path, endpath) > 0)
205 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(choose_view), path, NULL,
206 row_align >= 0.0,
207 row_align,
208 0);
209 gtk_tree_path_free(startpath);
210 gtk_tree_path_free(endpath);
b96f65cf
RK
211 return 0;
212}
213
214/** @brief Make @p ref visible
215 * @param ref Row reference to make visible
216 * @param row_align Row alignment (or -ve)
217 * @return 0 on success, nonzero if @p ref has gone stale
218 *
219 * If @p row_align is negative no row alignemt is performed. Otherwise
220 * it must be between 0 (the top) and 1 (the bottom).
2a9a65e4 221 */
b96f65cf
RK
222static int choose_make_ref_visible(GtkTreeRowReference *ref,
223 gfloat row_align) {
2a9a65e4
RK
224 GtkTreePath *path = gtk_tree_row_reference_get_path(ref);
225 if(!path)
226 return -1;
b96f65cf 227 choose_make_path_visible(path, row_align);
2a9a65e4
RK
228 gtk_tree_path_free(path);
229 return 0;
230}
231
232/** @brief Do some work towards ensuring that all search results are visible
233 *
234 * Assumes there's at least one results!
235 */
236static void choose_make_visible(const char attribute((unused)) *event,
237 void attribute((unused)) *eventdata,
238 void attribute((unused)) *callbackdata) {
239 //fprintf(stderr, "choose_make_visible\n");
240 int remaining = 0;
241
242 for(int n = 0; n < choose_n_search_results; ++n) {
243 if(!choose_search_results[n])
244 continue;
245 if(choose_make_one_visible(choose_search_results[n]))
246 choose_search_results[n] = 0;
247 else
248 ++remaining;
249 }
250 //fprintf(stderr, "remaining=%d\n", remaining);
251 if(remaining) {
252 /* If there's work left to be done make sure we get a callback when
253 * something changes */
254 if(!choose_inserted_handle)
cab9a17c 255 choose_inserted_handle = event_register("choose-more-tracks",
2a9a65e4
RK
256 choose_make_visible, 0);
257 } else {
258 /* Suppress callbacks if there's nothing more to do */
259 event_cancel(choose_inserted_handle);
260 choose_inserted_handle = 0;
261 /* We've expanded everything, now we can mess with the cursor */
262 //fprintf(stderr, "sort %d references\n", choose_n_search_references);
263 qsort(choose_search_references,
264 choose_n_search_references,
265 sizeof (GtkTreeRowReference *),
266 choose_compare_references);
b96f65cf 267 choose_make_ref_visible(choose_search_references[0], 0.5);
2a9a65e4
RK
268 }
269}
270
cfa78eaa
RK
271/** @brief Called with search results */
272static void choose_search_completed(void attribute((unused)) *v,
abf99697 273 const char *err,
cfa78eaa 274 int nvec, char **vec) {
2a9a65e4 275 //fprintf(stderr, "choose_search_completed\n");
abf99697
RK
276 if(err) {
277 popup_protocol_error(0, err);
cfa78eaa
RK
278 return;
279 }
280 choose_searching = 0;
281 /* If the search was obsoleted initiate another one */
282 if(choose_search_obsolete) {
2a9a65e4 283 choose_search_obsolete = 0;
cfa78eaa
RK
284 choose_search_entry_changed(0, 0);
285 return;
286 }
2a9a65e4 287 //fprintf(stderr, "*** %d search results\n", nvec);
bd802f22
RK
288 /* We're actually going to use these search results. Autocollapse anything
289 * left over from the old search. */
290 choose_auto_collapse();
cfa78eaa 291 choose_search_hash = hash_new(1);
2a9a65e4
RK
292 if(nvec) {
293 for(int n = 0; n < nvec; ++n)
294 hash_add(choose_search_hash, vec[n], "", HASH_INSERT);
295 /* Stash results for choose_make_visible */
296 choose_n_search_results = nvec;
297 choose_search_results = vec;
298 /* Make a big-enough buffer for the results row reference list */
299 choose_n_search_references = 0;
300 choose_search_references = xcalloc(nvec, sizeof (GtkTreeRowReference *));
301 /* Start making rows visible */
302 choose_make_visible(0, 0, 0);
b96f65cf
RK
303 gtk_widget_set_sensitive(choose_next, TRUE);
304 gtk_widget_set_sensitive(choose_prev, TRUE);
305 } else {
306 gtk_widget_set_sensitive(choose_next, FALSE);
307 gtk_widget_set_sensitive(choose_prev, FALSE);
a59663d4
RK
308 choose_n_search_results = 0;
309 choose_search_results = 0;
310 choose_n_search_references = 0;
311 choose_search_references = 0;
2a9a65e4 312 }
cfa78eaa
RK
313 event_raise("search-results-changed", 0);
314}
315
9eeb9f12
RK
316/** @brief Actually initiate a search */
317static void initiate_search(void) {
318 //fprintf(stderr, "initiate_search\n");
cfa78eaa
RK
319 /* If a search is in flight don't initiate a new one until it comes back */
320 if(choose_searching) {
321 choose_search_obsolete = 1;
322 return;
323 }
324 char *terms = xstrdup(gtk_entry_get_text(GTK_ENTRY(choose_search_entry)));
325 /* Strip leading and trailing space */
9eeb9f12
RK
326 while(*terms == ' ')
327 ++terms;
cfa78eaa 328 char *e = terms + strlen(terms);
9eeb9f12
RK
329 while(e > terms && e[-1] == ' ')
330 --e;
cfa78eaa 331 *e = 0;
9eeb9f12
RK
332 if(choose_search_terms && !strcmp(terms, choose_search_terms)) {
333 /* Search terms have not actually changed in any way that matters */
334 return;
335 }
336 /* Remember the current terms */
337 choose_search_terms = terms;
cfa78eaa
RK
338 if(!*terms) {
339 /* Nothing to search for. Fake a completion call. */
340 choose_search_completed(0, 0, 0, 0);
341 return;
342 }
343 if(disorder_eclient_search(client, choose_search_completed, terms, 0)) {
344 /* Bad search terms. Fake a completion call. */
345 choose_search_completed(0, 0, 0, 0);
346 return;
347 }
348 choose_searching = 1;
349}
350
9eeb9f12
RK
351static gboolean choose_search_timeout(gpointer attribute((unused)) data) {
352 struct timeval now;
353 xgettimeofday(&now, NULL);
354 /*fprintf(stderr, "%ld.%06d choose_search_timeout\n",
355 now.tv_sec, now.tv_usec);*/
356 if(tvdouble(now) - tvdouble(choose_search_last_keypress)
357 < SEARCH_DELAY_MS / 1000.0) {
358 //fprintf(stderr, " ... too soon\n");
359 return TRUE; /* Call me again later */
360 }
361 //fprintf(stderr, " ... let's go\n");
362 choose_search_last_keypress.tv_sec = 0;
363 choose_search_last_keypress.tv_usec = 0;
364 choose_search_timeout_id = 0;
365 initiate_search();
366 return FALSE;
367}
368
369/** @brief Called when the search entry changes */
370static void choose_search_entry_changed
371 (GtkEditable attribute((unused)) *editable,
372 gpointer attribute((unused)) user_data) {
373 xgettimeofday(&choose_search_last_keypress, NULL);
374 /*fprintf(stderr, "%ld.%06d choose_search_entry_changed\n",
375 choose_search_last_keypress.tv_sec,
376 choose_search_last_keypress.tv_usec);*/
377 /* If there's already a timeout, remove it */
378 if(choose_search_timeout_id) {
379 g_source_remove(choose_search_timeout_id);
380 choose_search_timeout_id = 0;
381 }
382 /* Add a new timeout */
383 choose_search_timeout_id = g_timeout_add(SEARCH_DELAY_MS / 10,
384 choose_search_timeout,
385 0);
386 /* We really wanted to tell Glib what time we wanted the callback at rather
387 * than asking for calls at given intervals. But there's no interface for
388 * that, and defining a new source for it seems like overkill if we can
389 * reasonably avoid it. */
390}
391
b96f65cf
RK
392/** @brief Identify first and last visible paths
393 *
394 * We'd like to use gtk_tree_view_get_visible_range() for this, but that was
395 * introduced in GTK+ 2.8, and Fink only has 2.6 (which is around three years
396 * out of date at time of writing), and I'm not yet prepared to rule out Fink
397 * support.
398 */
399static gboolean choose_get_visible_range(GtkTreeView *tree_view,
400 GtkTreePath **startpathp,
401 GtkTreePath **endpathp) {
402 GdkRectangle visible_tc[1];
403
404 /* Get the visible rectangle in tree coordinates */
405 gtk_tree_view_get_visible_rect(tree_view, visible_tc);
406 /*fprintf(stderr, "visible: %dx%x at %dx%d\n",
407 visible_tc->width, visible_tc->height,
408 visible_tc->x, visible_tc->y);*/
409 if(startpathp) {
410 /* Convert top-left visible point to widget coordinates */
411 int x_wc, y_wc;
412 gtk_tree_view_tree_to_widget_coords(tree_view,
413 visible_tc->x, visible_tc->y,
414 &x_wc, &y_wc);
415 //fprintf(stderr, " start widget coords: %dx%d\n", x_wc, y_wc);
416 gtk_tree_view_get_path_at_pos(tree_view,
417 x_wc, y_wc,
418 startpathp,
419 NULL,
420 NULL, NULL);
421 }
422 if(endpathp) {
423 /* Convert bottom-left visible point to widget coordinates */
424 /* Convert top-left visible point to widget coordinates */
425 int x_wc, y_wc;
426 gtk_tree_view_tree_to_widget_coords(tree_view,
427 visible_tc->x,
428 visible_tc->y + visible_tc->height - 1,
429 &x_wc, &y_wc);
430 //fprintf(stderr, " end widget coords: %dx%d\n", x_wc, y_wc);
431 gtk_tree_view_get_path_at_pos(tree_view,
432 x_wc, y_wc,
433 endpathp,
434 NULL,
435 NULL, NULL);
436 }
437 return TRUE;
438}
439
fd9faf16
RK
440/** @brief Move to the next/prev match
441 * @param direction -1 for prev, +1 for next
442 */
443static void choose_move(int direction) {
444 /* Refocus the main view so typahead find continues to work */
62dcb54f 445 gtk_widget_grab_focus(choose_view);
fd9faf16 446 /* If there's no results we have nothing to do */
a59663d4
RK
447 if(!choose_n_search_results)
448 return;
fd9faf16
RK
449 /* Compute bounds for searching over the array in the right direction */
450 const int first = direction > 0 ? 0 : choose_n_search_references - 1;
451 const int limit = direction > 0 ? choose_n_search_references : -1;
452 /* Find the first/last currently visible row */
453 GtkTreePath *limitpath;
454 if(!choose_get_visible_range(GTK_TREE_VIEW(choose_view),
455 direction < 0 ? &limitpath : 0,
456 direction > 0 ? &limitpath : 0))
b96f65cf 457 return;
fd9faf16
RK
458 /* Find a the first search result later/earlier than it. They're sorted so
459 * we could actually do much better than this if necessary. */
460 for(int n = first; n != limit; n += direction) {
b96f65cf
RK
461 GtkTreePath *path
462 = gtk_tree_row_reference_get_path(choose_search_references[n]);
463 if(!path)
464 continue;
fd9faf16
RK
465 /* gtk_tree_path_compare returns -1, 0 or 1 so we compare naively with
466 * direction */
467 if(gtk_tree_path_compare(limitpath, path) + direction == 0) {
b96f65cf
RK
468 choose_make_path_visible(path, 0.5);
469 gtk_tree_path_free(path);
470 return;
471 }
472 gtk_tree_path_free(path);
473 }
fd9faf16
RK
474 /* We didn't find one. Loop back to the first/las. */
475 for(int n = first; n != limit; n += direction) {
a59663d4
RK
476 GtkTreePath *path
477 = gtk_tree_row_reference_get_path(choose_search_references[n]);
478 if(!path)
479 continue;
480 choose_make_path_visible(path, 0.5);
481 gtk_tree_path_free(path);
482 return;
483 }
cfa78eaa
RK
484}
485
fd9faf16
RK
486void choose_next_clicked(GtkButton attribute((unused)) *button,
487 gpointer attribute((unused)) userdata) {
488 choose_move(1);
489}
490
a59663d4
RK
491void choose_prev_clicked(GtkButton attribute((unused)) *button,
492 gpointer attribute((unused)) userdata) {
fd9faf16 493 choose_move(-1);
cfa78eaa
RK
494}
495
9eeb9f12
RK
496/** @brief Called when the cancel search button is clicked */
497static void choose_clear_clicked(GtkButton attribute((unused)) *button,
498 gpointer attribute((unused)) userdata) {
499 gtk_entry_set_text(GTK_ENTRY(choose_search_entry), "");
62dcb54f 500 gtk_widget_grab_focus(choose_view);
9eeb9f12
RK
501 /* We start things off straight away in this case */
502 initiate_search();
503}
504
4d6a5c4e
RK
505/** @brief Called when the user hits ^F to start a new search */
506void choose_search_new(void) {
507 gtk_editable_select_region(GTK_EDITABLE(choose_search_entry), 0, -1);
508}
509
cfa78eaa
RK
510/** @brief Create the search widget */
511GtkWidget *choose_search_widget(void) {
512
513 /* Text entry box for search terms */
514 choose_search_entry = gtk_entry_new();
515 gtk_widget_set_style(choose_search_entry, tool_style);
516 g_signal_connect(choose_search_entry, "changed",
517 G_CALLBACK(choose_search_entry_changed), 0);
518 gtk_tooltips_set_tip(tips, choose_search_entry,
519 "Enter search terms here; search is automatic", "");
520
521 /* Cancel button to clear the search */
522 choose_clear = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
523 gtk_widget_set_style(choose_clear, tool_style);
524 g_signal_connect(G_OBJECT(choose_clear), "clicked",
525 G_CALLBACK(choose_clear_clicked), 0);
526 gtk_tooltips_set_tip(tips, choose_clear, "Clear search terms", "");
527
528 /* Up and down buttons to find previous/next results; initially they are not
529 * usable as there are no search results. */
530 choose_prev = iconbutton("up.png", "Previous search result");
531 g_signal_connect(G_OBJECT(choose_prev), "clicked",
532 G_CALLBACK(choose_prev_clicked), 0);
533 gtk_widget_set_style(choose_prev, tool_style);
534 gtk_widget_set_sensitive(choose_prev, 0);
535 choose_next = iconbutton("down.png", "Next search result");
536 g_signal_connect(G_OBJECT(choose_next), "clicked",
537 G_CALLBACK(choose_next_clicked), 0);
538 gtk_widget_set_style(choose_next, tool_style);
539 gtk_widget_set_sensitive(choose_next, 0);
540
541 /* Pack the search tools button together on a line */
542 GtkWidget *hbox = gtk_hbox_new(FALSE/*homogeneous*/, 1/*spacing*/);
543 gtk_box_pack_start(GTK_BOX(hbox), choose_search_entry,
544 TRUE/*expand*/, TRUE/*fill*/, 0/*padding*/);
545 gtk_box_pack_start(GTK_BOX(hbox), choose_prev,
546 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
547 gtk_box_pack_start(GTK_BOX(hbox), choose_next,
548 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
549 gtk_box_pack_start(GTK_BOX(hbox), choose_clear,
550 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
551
552 return hbox;
553}
554
555/*
556Local Variables:
557c-basic-offset:2
558comment-column:40
559fill-column:79
560indent-tabs-mode:nil
561End:
562*/