chiark / gitweb /
Make next/prev search result buttons work.
[disorder] / disobedience / choose-search.c
... / ...
CommitLineData
1/*
2 * This file is part of DisOrder
3 * Copyright (C) 2008 Richard Kettlewell
4 *
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.
9 *
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.
14 *
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
18 * USA
19 */
20/** @file disobedience/search.c
21 * @brief Search support
22 *
23 * TODO:
24 * - cleverer focus to implement typeahead find
25 * - don't steal ^A
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')
29 */
30#include "disobedience.h"
31#include "choose.h"
32
33static GtkWidget *choose_search_entry;
34static GtkWidget *choose_next;
35static GtkWidget *choose_prev;
36static GtkWidget *choose_clear;
37
38/** @brief True if a search command is in flight */
39static int choose_searching;
40
41/** @brief True if in-flight search is now known to be obsolete */
42static int choose_search_obsolete;
43
44/** @brief Hash of all search result */
45static hash *choose_search_hash;
46
47/** @brief List of invisible search results
48 *
49 * This only lists search results not yet known to be visible, and is
50 * gradually depleted.
51 */
52static char **choose_search_results;
53
54/** @brief Length of @ref choose_search_results */
55static int choose_n_search_results;
56
57/** @brief Row references for search results */
58static GtkTreeRowReference **choose_search_references;
59
60/** @brief Length of @ref choose_search_references */
61static int choose_n_search_references;
62
63/** @brief Event handle for monitoring newly inserted tracks */
64static event_handle choose_inserted_handle;
65
66static void choose_search_entry_changed(GtkEditable *editable,
67 gpointer user_data);
68
69int choose_is_search_result(const char *track) {
70 return choose_search_hash && hash_find(choose_search_hash, track);
71}
72
73/** @brief Called when the cancel search button is clicked */
74static 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 */
78}
79
80static int is_prefix(const char *dir, const char *track) {
81 size_t nd = strlen(dir);
82
83 if(nd < strlen(track)
84 && track[nd] == '/'
85 && !strncmp(track, dir, nd))
86 return 1;
87 else
88 return 0;
89}
90
91/** @brief Do some work towards making @p track visible
92 * @return True if we made it visible or it was missing
93 */
94static 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.
98 *
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
101 * to be revisited
102 * - if we don't find one then we're probably out of date
103 */
104 GtkTreeIter it[1];
105 gboolean itv = gtk_tree_model_get_iter_first(GTK_TREE_MODEL(choose_store),
106 it);
107 while(itv) {
108 const char *dir = choose_get_track(it);
109
110 //fprintf(stderr, " %s\n", dir);
111 if(!dir) {
112 /* Placeholder */
113 itv = gtk_tree_model_iter_next(GTK_TREE_MODEL(choose_store), it);
114 continue;
115 }
116 GtkTreePath *path = gtk_tree_model_get_path(GTK_TREE_MODEL(choose_store),
117 it);
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);
125 return 1;
126 }
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);
132 if(expanded) {
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),
138 child,
139 it);
140 *it = *child;
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
144 * be replaced */
145 gtk_tree_path_free(path);
146 return 0;
147 }
148 } else {
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),
153 path,
154 FALSE/*open_all*/);
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. */
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
187/** @brief Make @p path visible
188 * @param path Row reference to make visible
189 * @param row_align Row alignment (or -ve)
190 * @return 0 on success, nonzero if @p ref has gone stale
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);
199 /* Make sure the target is visible */
200 gtk_tree_view_scroll_to_cell(GTK_TREE_VIEW(choose_view), path, NULL,
201 row_align >= 0.0,
202 row_align,
203 0);
204 return 0;
205}
206
207/** @brief Make @p ref visible
208 * @param ref Row reference to make visible
209 * @param row_align Row alignment (or -ve)
210 * @return 0 on success, nonzero if @p ref has gone stale
211 *
212 * If @p row_align is negative no row alignemt is performed. Otherwise
213 * it must be between 0 (the top) and 1 (the bottom).
214 */
215static int choose_make_ref_visible(GtkTreeRowReference *ref,
216 gfloat row_align) {
217 GtkTreePath *path = gtk_tree_row_reference_get_path(ref);
218 if(!path)
219 return -1;
220 choose_make_path_visible(path, row_align);
221 gtk_tree_path_free(path);
222 return 0;
223}
224
225/** @brief Do some work towards ensuring that all search results are visible
226 *
227 * Assumes there's at least one results!
228 */
229static void choose_make_visible(const char attribute((unused)) *event,
230 void attribute((unused)) *eventdata,
231 void attribute((unused)) *callbackdata) {
232 //fprintf(stderr, "choose_make_visible\n");
233 int remaining = 0;
234
235 for(int n = 0; n < choose_n_search_results; ++n) {
236 if(!choose_search_results[n])
237 continue;
238 if(choose_make_one_visible(choose_search_results[n]))
239 choose_search_results[n] = 0;
240 else
241 ++remaining;
242 }
243 //fprintf(stderr, "remaining=%d\n", remaining);
244 if(remaining) {
245 /* If there's work left to be done make sure we get a callback when
246 * something changes */
247 if(!choose_inserted_handle)
248 choose_inserted_handle = event_register("choose-inserted-tracks",
249 choose_make_visible, 0);
250 } else {
251 /* Suppress callbacks if there's nothing more to do */
252 event_cancel(choose_inserted_handle);
253 choose_inserted_handle = 0;
254 /* We've expanded everything, now we can mess with the cursor */
255 //fprintf(stderr, "sort %d references\n", choose_n_search_references);
256 qsort(choose_search_references,
257 choose_n_search_references,
258 sizeof (GtkTreeRowReference *),
259 choose_compare_references);
260 choose_make_ref_visible(choose_search_references[0], 0.5);
261 }
262}
263
264/** @brief Called with search results */
265static void choose_search_completed(void attribute((unused)) *v,
266 const char *error,
267 int nvec, char **vec) {
268 //fprintf(stderr, "choose_search_completed\n");
269 if(error) {
270 popup_protocol_error(0, error);
271 return;
272 }
273 choose_searching = 0;
274 /* If the search was obsoleted initiate another one */
275 if(choose_search_obsolete) {
276 choose_search_obsolete = 0;
277 choose_search_entry_changed(0, 0);
278 return;
279 }
280 //fprintf(stderr, "*** %d search results\n", nvec);
281 choose_search_hash = hash_new(1);
282 if(nvec) {
283 for(int n = 0; n < nvec; ++n)
284 hash_add(choose_search_hash, vec[n], "", HASH_INSERT);
285 /* Stash results for choose_make_visible */
286 choose_n_search_results = nvec;
287 choose_search_results = vec;
288 /* Make a big-enough buffer for the results row reference list */
289 choose_n_search_references = 0;
290 choose_search_references = xcalloc(nvec, sizeof (GtkTreeRowReference *));
291 /* Start making rows visible */
292 choose_make_visible(0, 0, 0);
293 gtk_widget_set_sensitive(choose_next, TRUE);
294 gtk_widget_set_sensitive(choose_prev, TRUE);
295 } else {
296 gtk_widget_set_sensitive(choose_next, FALSE);
297 gtk_widget_set_sensitive(choose_prev, FALSE);
298 }
299 event_raise("search-results-changed", 0);
300}
301
302/** @brief Called when the search entry changes */
303static void choose_search_entry_changed
304 (GtkEditable attribute((unused)) *editable,
305 gpointer attribute((unused)) user_data) {
306 //fprintf(stderr, "choose_search_entry_changed\n");
307 /* If a search is in flight don't initiate a new one until it comes back */
308 if(choose_searching) {
309 choose_search_obsolete = 1;
310 return;
311 }
312 char *terms = xstrdup(gtk_entry_get_text(GTK_ENTRY(choose_search_entry)));
313 /* Strip leading and trailing space */
314 while(*terms == ' ') ++terms;
315 char *e = terms + strlen(terms);
316 while(e > terms && e[-1] == ' ') --e;
317 *e = 0;
318 if(!*terms) {
319 /* Nothing to search for. Fake a completion call. */
320 choose_search_completed(0, 0, 0, 0);
321 return;
322 }
323 if(disorder_eclient_search(client, choose_search_completed, terms, 0)) {
324 /* Bad search terms. Fake a completion call. */
325 choose_search_completed(0, 0, 0, 0);
326 return;
327 }
328 choose_searching = 1;
329}
330
331/** @brief Identify first and last visible paths
332 *
333 * We'd like to use gtk_tree_view_get_visible_range() for this, but that was
334 * introduced in GTK+ 2.8, and Fink only has 2.6 (which is around three years
335 * out of date at time of writing), and I'm not yet prepared to rule out Fink
336 * support.
337 */
338static gboolean choose_get_visible_range(GtkTreeView *tree_view,
339 GtkTreePath **startpathp,
340 GtkTreePath **endpathp) {
341 GdkRectangle visible_tc[1];
342
343 /* Get the visible rectangle in tree coordinates */
344 gtk_tree_view_get_visible_rect(tree_view, visible_tc);
345 /*fprintf(stderr, "visible: %dx%x at %dx%d\n",
346 visible_tc->width, visible_tc->height,
347 visible_tc->x, visible_tc->y);*/
348 if(startpathp) {
349 /* Convert top-left visible point to widget coordinates */
350 int x_wc, y_wc;
351 gtk_tree_view_tree_to_widget_coords(tree_view,
352 visible_tc->x, visible_tc->y,
353 &x_wc, &y_wc);
354 //fprintf(stderr, " start widget coords: %dx%d\n", x_wc, y_wc);
355 gtk_tree_view_get_path_at_pos(tree_view,
356 x_wc, y_wc,
357 startpathp,
358 NULL,
359 NULL, NULL);
360 }
361 if(endpathp) {
362 /* Convert bottom-left visible point to widget coordinates */
363 /* Convert top-left visible point to widget coordinates */
364 int x_wc, y_wc;
365 gtk_tree_view_tree_to_widget_coords(tree_view,
366 visible_tc->x,
367 visible_tc->y + visible_tc->height - 1,
368 &x_wc, &y_wc);
369 //fprintf(stderr, " end widget coords: %dx%d\n", x_wc, y_wc);
370 gtk_tree_view_get_path_at_pos(tree_view,
371 x_wc, y_wc,
372 endpathp,
373 NULL,
374 NULL, NULL);
375 }
376 return TRUE;
377}
378
379static void choose_next_clicked(GtkButton attribute((unused)) *button,
380 gpointer attribute((unused)) userdata) {
381 /* Find the last visible row */
382 GtkTreePath *endpath;
383 gboolean endvalid = choose_get_visible_range(GTK_TREE_VIEW(choose_view),
384 NULL,
385 &endpath);
386 if(!endvalid)
387 return;
388 /* Find a the first search result later than it. They're sorted so we could
389 * actually do much better than this if necessary. */
390 for(int n = 0; n < choose_n_search_references; ++n) {
391 GtkTreePath *path
392 = gtk_tree_row_reference_get_path(choose_search_references[n]);
393 if(!path)
394 continue;
395 if(gtk_tree_path_compare(endpath, path) < 0) {
396 choose_make_path_visible(path, 0.5);
397 gtk_tree_path_free(path);
398 return;
399 }
400 gtk_tree_path_free(path);
401 }
402}
403
404static void choose_prev_clicked(GtkButton attribute((unused)) *button,
405 gpointer attribute((unused)) userdata) {
406 /* TODO can we de-dupe with choose_next_clicked? Probably yes. */
407 /* Find the first visible row */
408 GtkTreePath *startpath;
409 gboolean startvalid = choose_get_visible_range(GTK_TREE_VIEW(choose_view),
410 &startpath,
411 NULL);
412 if(!startvalid)
413 return;
414 /* Find a the last search result earlier than it. They're sorted so we could
415 * actually do much better than this if necessary. */
416 for(int n = choose_n_search_references - 1; n >= 0; --n) {
417 GtkTreePath *path
418 = gtk_tree_row_reference_get_path(choose_search_references[n]);
419 if(!path)
420 continue;
421 if(gtk_tree_path_compare(startpath, path) > 0) {
422 choose_make_path_visible(path, 0.5);
423 gtk_tree_path_free(path);
424 return;
425 }
426 gtk_tree_path_free(path);
427 }
428}
429
430/** @brief Create the search widget */
431GtkWidget *choose_search_widget(void) {
432
433 /* Text entry box for search terms */
434 choose_search_entry = gtk_entry_new();
435 gtk_widget_set_style(choose_search_entry, tool_style);
436 g_signal_connect(choose_search_entry, "changed",
437 G_CALLBACK(choose_search_entry_changed), 0);
438 gtk_tooltips_set_tip(tips, choose_search_entry,
439 "Enter search terms here; search is automatic", "");
440
441 /* Cancel button to clear the search */
442 choose_clear = gtk_button_new_from_stock(GTK_STOCK_CANCEL);
443 gtk_widget_set_style(choose_clear, tool_style);
444 g_signal_connect(G_OBJECT(choose_clear), "clicked",
445 G_CALLBACK(choose_clear_clicked), 0);
446 gtk_tooltips_set_tip(tips, choose_clear, "Clear search terms", "");
447
448 /* Up and down buttons to find previous/next results; initially they are not
449 * usable as there are no search results. */
450 choose_prev = iconbutton("up.png", "Previous search result");
451 g_signal_connect(G_OBJECT(choose_prev), "clicked",
452 G_CALLBACK(choose_prev_clicked), 0);
453 gtk_widget_set_style(choose_prev, tool_style);
454 gtk_widget_set_sensitive(choose_prev, 0);
455 choose_next = iconbutton("down.png", "Next search result");
456 g_signal_connect(G_OBJECT(choose_next), "clicked",
457 G_CALLBACK(choose_next_clicked), 0);
458 gtk_widget_set_style(choose_next, tool_style);
459 gtk_widget_set_sensitive(choose_next, 0);
460
461 /* Pack the search tools button together on a line */
462 GtkWidget *hbox = gtk_hbox_new(FALSE/*homogeneous*/, 1/*spacing*/);
463 gtk_box_pack_start(GTK_BOX(hbox), choose_search_entry,
464 TRUE/*expand*/, TRUE/*fill*/, 0/*padding*/);
465 gtk_box_pack_start(GTK_BOX(hbox), choose_prev,
466 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
467 gtk_box_pack_start(GTK_BOX(hbox), choose_next,
468 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
469 gtk_box_pack_start(GTK_BOX(hbox), choose_clear,
470 FALSE/*expand*/, FALSE/*fill*/, 0/*padding*/);
471
472 return hbox;
473}
474
475/*
476Local Variables:
477c-basic-offset:2
478comment-column:40
479fill-column:79
480indent-tabs-mode:nil
481End:
482*/