chiark / gitweb /
Merge branch 'master' of git.distorted.org.uk:~mdw/publish/public-git/disorder
[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
132a5a4a 19 * @brief Search support for Disobedience choose window
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);
224cac44
MW
292 /* If we were still setting up the chooser to show previous search results
293 * then turn that off now. We'll turn it back on again if necessary. */
294 event_cancel(choose_inserted_handle);
295 choose_inserted_handle = 0;
296 /* Start showing the results of our search. */
2a9a65e4
RK
297 if(nvec) {
298 for(int n = 0; n < nvec; ++n)
299 hash_add(choose_search_hash, vec[n], "", HASH_INSERT);
300 /* Stash results for choose_make_visible */
301 choose_n_search_results = nvec;
302 choose_search_results = vec;
303 /* Make a big-enough buffer for the results row reference list */
304 choose_n_search_references = 0;
305 choose_search_references = xcalloc(nvec, sizeof (GtkTreeRowReference *));
306 /* Start making rows visible */
307 choose_make_visible(0, 0, 0);
b96f65cf
RK
308 gtk_widget_set_sensitive(choose_next, TRUE);
309 gtk_widget_set_sensitive(choose_prev, TRUE);
310 } else {
311 gtk_widget_set_sensitive(choose_next, FALSE);
312 gtk_widget_set_sensitive(choose_prev, FALSE);
a59663d4
RK
313 choose_n_search_results = 0;
314 choose_search_results = 0;
315 choose_n_search_references = 0;
316 choose_search_references = 0;
2a9a65e4 317 }
cfa78eaa
RK
318 event_raise("search-results-changed", 0);
319}
320
9eeb9f12
RK
321/** @brief Actually initiate a search */
322static void initiate_search(void) {
323 //fprintf(stderr, "initiate_search\n");
cfa78eaa
RK
324 /* If a search is in flight don't initiate a new one until it comes back */
325 if(choose_searching) {
326 choose_search_obsolete = 1;
327 return;
328 }
329 char *terms = xstrdup(gtk_entry_get_text(GTK_ENTRY(choose_search_entry)));
330 /* Strip leading and trailing space */
9eeb9f12
RK
331 while(*terms == ' ')
332 ++terms;
cfa78eaa 333 char *e = terms + strlen(terms);
9eeb9f12
RK
334 while(e > terms && e[-1] == ' ')
335 --e;
cfa78eaa 336 *e = 0;
9eeb9f12
RK
337 if(choose_search_terms && !strcmp(terms, choose_search_terms)) {
338 /* Search terms have not actually changed in any way that matters */
339 return;
340 }
341 /* Remember the current terms */
342 choose_search_terms = terms;
cfa78eaa
RK
343 if(!*terms) {
344 /* Nothing to search for. Fake a completion call. */
345 choose_search_completed(0, 0, 0, 0);
346 return;
347 }
348 if(disorder_eclient_search(client, choose_search_completed, terms, 0)) {
349 /* Bad search terms. Fake a completion call. */
350 choose_search_completed(0, 0, 0, 0);
351 return;
352 }
353 choose_searching = 1;
354}
355
9eeb9f12
RK
356static gboolean choose_search_timeout(gpointer attribute((unused)) data) {
357 struct timeval now;
358 xgettimeofday(&now, NULL);
359 /*fprintf(stderr, "%ld.%06d choose_search_timeout\n",
360 now.tv_sec, now.tv_usec);*/
361 if(tvdouble(now) - tvdouble(choose_search_last_keypress)
362 < SEARCH_DELAY_MS / 1000.0) {
363 //fprintf(stderr, " ... too soon\n");
364 return TRUE; /* Call me again later */
365 }
366 //fprintf(stderr, " ... let's go\n");
367 choose_search_last_keypress.tv_sec = 0;
368 choose_search_last_keypress.tv_usec = 0;
369 choose_search_timeout_id = 0;
370 initiate_search();
371 return FALSE;
372}
373
374/** @brief Called when the search entry changes */
375static void choose_search_entry_changed
376 (GtkEditable attribute((unused)) *editable,
377 gpointer attribute((unused)) user_data) {
378 xgettimeofday(&choose_search_last_keypress, NULL);
379 /*fprintf(stderr, "%ld.%06d choose_search_entry_changed\n",
380 choose_search_last_keypress.tv_sec,
381 choose_search_last_keypress.tv_usec);*/
382 /* If there's already a timeout, remove it */
383 if(choose_search_timeout_id) {
384 g_source_remove(choose_search_timeout_id);
385 choose_search_timeout_id = 0;
386 }
387 /* Add a new timeout */
388 choose_search_timeout_id = g_timeout_add(SEARCH_DELAY_MS / 10,
389 choose_search_timeout,
390 0);
391 /* We really wanted to tell Glib what time we wanted the callback at rather
392 * than asking for calls at given intervals. But there's no interface for
393 * that, and defining a new source for it seems like overkill if we can
394 * reasonably avoid it. */
395}
396
b96f65cf
RK
397/** @brief Identify first and last visible paths
398 *
399 * We'd like to use gtk_tree_view_get_visible_range() for this, but that was
400 * introduced in GTK+ 2.8, and Fink only has 2.6 (which is around three years
401 * out of date at time of writing), and I'm not yet prepared to rule out Fink
402 * support.
403 */
404static gboolean choose_get_visible_range(GtkTreeView *tree_view,
405 GtkTreePath **startpathp,
406 GtkTreePath **endpathp) {
407 GdkRectangle visible_tc[1];
408
409 /* Get the visible rectangle in tree coordinates */
410 gtk_tree_view_get_visible_rect(tree_view, visible_tc);
411 /*fprintf(stderr, "visible: %dx%x at %dx%d\n",
412 visible_tc->width, visible_tc->height,
413 visible_tc->x, visible_tc->y);*/
414 if(startpathp) {
415 /* Convert top-left visible point to widget coordinates */
416 int x_wc, y_wc;
417 gtk_tree_view_tree_to_widget_coords(tree_view,
418 visible_tc->x, visible_tc->y,
419 &x_wc, &y_wc);
420 //fprintf(stderr, " start widget coords: %dx%d\n", x_wc, y_wc);
421 gtk_tree_view_get_path_at_pos(tree_view,
422 x_wc, y_wc,
423 startpathp,
424 NULL,
425 NULL, NULL);
426 }
427 if(endpathp) {
428 /* Convert bottom-left visible point to widget coordinates */
429 /* Convert top-left visible point to widget coordinates */
430 int x_wc, y_wc;
431 gtk_tree_view_tree_to_widget_coords(tree_view,
432 visible_tc->x,
433 visible_tc->y + visible_tc->height - 1,
434 &x_wc, &y_wc);
435 //fprintf(stderr, " end widget coords: %dx%d\n", x_wc, y_wc);
436 gtk_tree_view_get_path_at_pos(tree_view,
437 x_wc, y_wc,
438 endpathp,
439 NULL,
440 NULL, NULL);
441 }
442 return TRUE;
443}
444
fd9faf16
RK
445/** @brief Move to the next/prev match
446 * @param direction -1 for prev, +1 for next
447 */
448static void choose_move(int direction) {
9efa107e 449 assert(direction); /* placate analyzer */
fd9faf16 450 /* Refocus the main view so typahead find continues to work */
62dcb54f 451 gtk_widget_grab_focus(choose_view);
fd9faf16 452 /* If there's no results we have nothing to do */
a59663d4
RK
453 if(!choose_n_search_results)
454 return;
fd9faf16
RK
455 /* Compute bounds for searching over the array in the right direction */
456 const int first = direction > 0 ? 0 : choose_n_search_references - 1;
457 const int limit = direction > 0 ? choose_n_search_references : -1;
458 /* Find the first/last currently visible row */
459 GtkTreePath *limitpath;
460 if(!choose_get_visible_range(GTK_TREE_VIEW(choose_view),
461 direction < 0 ? &limitpath : 0,
462 direction > 0 ? &limitpath : 0))
b96f65cf 463 return;
fd9faf16
RK
464 /* Find a the first search result later/earlier than it. They're sorted so
465 * we could actually do much better than this if necessary. */
466 for(int n = first; n != limit; n += direction) {
b96f65cf
RK
467 GtkTreePath *path
468 = gtk_tree_row_reference_get_path(choose_search_references[n]);
469 if(!path)
470 continue;
fd9faf16
RK
471 /* gtk_tree_path_compare returns -1, 0 or 1 so we compare naively with
472 * direction */
473 if(gtk_tree_path_compare(limitpath, path) + direction == 0) {
b96f65cf
RK
474 choose_make_path_visible(path, 0.5);
475 gtk_tree_path_free(path);
476 return;
477 }
478 gtk_tree_path_free(path);
479 }
fd9faf16
RK
480 /* We didn't find one. Loop back to the first/las. */
481 for(int n = first; n != limit; n += direction) {
a59663d4
RK
482 GtkTreePath *path
483 = gtk_tree_row_reference_get_path(choose_search_references[n]);
484 if(!path)
485 continue;
486 choose_make_path_visible(path, 0.5);
487 gtk_tree_path_free(path);
488 return;
489 }
cfa78eaa
RK
490}
491
fd9faf16
RK
492void choose_next_clicked(GtkButton attribute((unused)) *button,
493 gpointer attribute((unused)) userdata) {
494 choose_move(1);
495}
496
a59663d4
RK
497void choose_prev_clicked(GtkButton attribute((unused)) *button,
498 gpointer attribute((unused)) userdata) {
fd9faf16 499 choose_move(-1);
cfa78eaa
RK
500}
501
9eeb9f12
RK
502/** @brief Called when the cancel search button is clicked */
503static void choose_clear_clicked(GtkButton attribute((unused)) *button,
504 gpointer attribute((unused)) userdata) {
505 gtk_entry_set_text(GTK_ENTRY(choose_search_entry), "");
62dcb54f 506 gtk_widget_grab_focus(choose_view);
9eeb9f12
RK
507 /* We start things off straight away in this case */
508 initiate_search();
509}
510
4d6a5c4e
RK
511/** @brief Called when the user hits ^F to start a new search */
512void choose_search_new(void) {
513 gtk_editable_select_region(GTK_EDITABLE(choose_search_entry), 0, -1);
514}
515
cfa78eaa
RK
516/** @brief Create the search widget */
517GtkWidget *choose_search_widget(void) {
518
519 /* Text entry box for search terms */
520 choose_search_entry = gtk_entry_new();
521 gtk_widget_set_style(choose_search_entry, tool_style);
522 g_signal_connect(choose_search_entry, "changed",
523 G_CALLBACK(choose_search_entry_changed), 0);
a31f7a72
RK
524 gtk_widget_set_tooltip_text(choose_search_entry,
525 "Enter search terms here; search is automatic");
cfa78eaa
RK
526
527 /* Cancel button to clear the search */
528 choose_clear = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
529 gtk_widget_set_style(choose_clear, tool_style);
530 g_signal_connect(G_OBJECT(choose_clear), "clicked",
531 G_CALLBACK(choose_clear_clicked), 0);
a31f7a72 532 gtk_widget_set_tooltip_text(choose_clear, "Clear search terms");
cfa78eaa
RK
533
534 /* Up and down buttons to find previous/next results; initially they are not
535 * usable as there are no search results. */
536 choose_prev = iconbutton("up.png", "Previous search result");
537 g_signal_connect(G_OBJECT(choose_prev), "clicked",
538 G_CALLBACK(choose_prev_clicked), 0);
539 gtk_widget_set_style(choose_prev, tool_style);
540 gtk_widget_set_sensitive(choose_prev, 0);
541 choose_next = iconbutton("down.png", "Next search result");
542 g_signal_connect(G_OBJECT(choose_next), "clicked",
543 G_CALLBACK(choose_next_clicked), 0);
544 gtk_widget_set_style(choose_next, tool_style);
545 gtk_widget_set_sensitive(choose_next, 0);
546
547 /* Pack the search tools button together on a line */
548 GtkWidget *hbox = gtk_hbox_new(FALSE/*homogeneous*/, 1/*spacing*/);
549 gtk_box_pack_start(GTK_BOX(hbox), choose_search_entry,
550 TRUE/*expand*/, TRUE/*fill*/, 0/*padding*/);
551 gtk_box_pack_start(GTK_BOX(hbox), choose_prev,
552 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
553 gtk_box_pack_start(GTK_BOX(hbox), choose_next,
554 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
555 gtk_box_pack_start(GTK_BOX(hbox), choose_clear,
556 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
557
558 return hbox;
559}
560
561/*
562Local Variables:
563c-basic-offset:2
564comment-column:40
565fill-column:79
566indent-tabs-mode:nil
567End:
568*/