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 3 of the License, or
8 * (at your option) any later version.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 /** @file disobedience/choose-search.c
19 * @brief Search support for Disobedience choose window
21 #include "disobedience.h"
24 int choose_auto_expanding
;
26 GtkWidget
*choose_search_entry
;
27 static GtkWidget
*choose_next
;
28 static GtkWidget
*choose_prev
;
29 static GtkWidget
*choose_clear
;
31 /** @brief True if a search command is in flight */
32 static int choose_searching
;
34 /** @brief True if in-flight search is now known to be obsolete */
35 static int choose_search_obsolete
;
37 /** @brief Current search terms */
38 static char *choose_search_terms
;
40 /** @brief Hash of all search result */
41 static hash
*choose_search_hash
;
43 /** @brief List of invisible search results
45 * This only lists search results not yet known to be visible, and is
48 static char **choose_search_results
;
50 /** @brief Length of @ref choose_search_results */
51 static int choose_n_search_results
;
53 /** @brief Row references for search results */
54 static GtkTreeRowReference
**choose_search_references
;
56 /** @brief Length of @ref choose_search_references */
57 static int choose_n_search_references
;
59 /** @brief Event handle for monitoring newly inserted tracks */
60 static event_handle choose_inserted_handle
;
62 /** @brief Time of last search entry keypress (or 0.0) */
63 static struct timeval choose_search_last_keypress
;
65 /** @brief Timeout ID for search delay */
66 static guint choose_search_timeout_id
;
68 static void choose_search_entry_changed(GtkEditable
*editable
,
70 static gboolean
choose_get_visible_range(GtkTreeView
*tree_view
,
71 GtkTreePath
**startpathp
,
72 GtkTreePath
**endpathp
);
74 int choose_is_search_result(const char *track
) {
75 return choose_search_hash
&& hash_find(choose_search_hash
, track
);
78 static int is_prefix(const char *dir
, const char *track
) {
79 size_t nd
= strlen(dir
);
83 && !strncmp(track
, dir
, nd
))
89 /** @brief Do some work towards making @p track visible
90 * @return True if we made it visible or it was missing
92 static 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.
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
100 * - if we don't find one then we're probably out of date
103 gboolean itv
= gtk_tree_model_get_iter_first(GTK_TREE_MODEL(choose_store
),
106 const char *dir
= choose_get_track(it
);
108 //fprintf(stderr, " %s\n", dir);
111 itv
= gtk_tree_model_iter_next(GTK_TREE_MODEL(choose_store
), it
);
114 GtkTreePath
*path
= gtk_tree_model_get_path(GTK_TREE_MODEL(choose_store
),
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
);
125 if(is_prefix(dir
, track
)) {
126 /* We found a prefix of the target track. */
127 //fprintf(stderr, " %s is a prefix\n", dir);
128 const gboolean expanded
129 = gtk_tree_view_row_expanded(GTK_TREE_VIEW(choose_view
), path
);
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
),
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
143 gtk_tree_path_free(path
);
147 //fprintf(stderr, " requesting expansion of %s\n", dir);
148 /* Track is below a non-expanded directory. So let's expand it.
149 * choose_make_visible() will arrange a revisit in due course.
151 * We mark the row as auto-expanded.
153 ++choose_auto_expanding
;
154 gtk_tree_view_expand_row(GTK_TREE_VIEW(choose_view
),
157 gtk_tree_path_free(path
);
158 --choose_auto_expanding
;
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 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
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).
195 static int choose_make_path_visible(GtkTreePath
*path
,
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 /* Find out what's currently visible */
200 GtkTreePath
*startpath
, *endpath
;
201 choose_get_visible_range(GTK_TREE_VIEW(choose_view
), &startpath
, &endpath
);
202 /* Make sure the target is visible */
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
,
209 gtk_tree_path_free(startpath
);
210 gtk_tree_path_free(endpath
);
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
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).
222 static int choose_make_ref_visible(GtkTreeRowReference
*ref
,
224 GtkTreePath
*path
= gtk_tree_row_reference_get_path(ref
);
227 choose_make_path_visible(path
, row_align
);
228 gtk_tree_path_free(path
);
232 /** @brief Do some work towards ensuring that all search results are visible
234 * Assumes there's at least one results!
236 static 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");
242 for(int n
= 0; n
< choose_n_search_results
; ++n
) {
243 if(!choose_search_results
[n
])
245 if(choose_make_one_visible(choose_search_results
[n
]))
246 choose_search_results
[n
] = 0;
250 //fprintf(stderr, "remaining=%d\n", 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
)
255 choose_inserted_handle
= event_register("choose-more-tracks",
256 choose_make_visible
, 0);
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
);
267 choose_make_ref_visible(choose_search_references
[0], 0.5);
271 /** @brief Called with search results */
272 static void choose_search_completed(void attribute((unused
)) *v
,
274 int nvec
, char **vec
) {
275 //fprintf(stderr, "choose_search_completed\n");
277 popup_protocol_error(0, err
);
280 choose_searching
= 0;
281 /* If the search was obsoleted initiate another one */
282 if(choose_search_obsolete
) {
283 choose_search_obsolete
= 0;
284 choose_search_entry_changed(0, 0);
287 //fprintf(stderr, "*** %d search results\n", nvec);
288 /* We're actually going to use these search results. Autocollapse anything
289 * left over from the old search. */
290 choose_auto_collapse();
291 choose_search_hash
= hash_new(1);
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);
303 gtk_widget_set_sensitive(choose_next
, TRUE
);
304 gtk_widget_set_sensitive(choose_prev
, TRUE
);
306 gtk_widget_set_sensitive(choose_next
, FALSE
);
307 gtk_widget_set_sensitive(choose_prev
, FALSE
);
308 choose_n_search_results
= 0;
309 choose_search_results
= 0;
310 choose_n_search_references
= 0;
311 choose_search_references
= 0;
313 event_raise("search-results-changed", 0);
316 /** @brief Actually initiate a search */
317 static void initiate_search(void) {
318 //fprintf(stderr, "initiate_search\n");
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;
324 char *terms
= xstrdup(gtk_entry_get_text(GTK_ENTRY(choose_search_entry
)));
325 /* Strip leading and trailing space */
328 char *e
= terms
+ strlen(terms
);
329 while(e
> terms
&& e
[-1] == ' ')
332 if(choose_search_terms
&& !strcmp(terms
, choose_search_terms
)) {
333 /* Search terms have not actually changed in any way that matters */
336 /* Remember the current terms */
337 choose_search_terms
= terms
;
339 /* Nothing to search for. Fake a completion call. */
340 choose_search_completed(0, 0, 0, 0);
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);
348 choose_searching
= 1;
351 static gboolean
choose_search_timeout(gpointer
attribute((unused
)) data
) {
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 */
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;
369 /** @brief Called when the search entry changes */
370 static 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;
382 /* Add a new timeout */
383 choose_search_timeout_id
= g_timeout_add(SEARCH_DELAY_MS
/ 10,
384 choose_search_timeout
,
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. */
392 /** @brief Identify first and last visible paths
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
399 static gboolean
choose_get_visible_range(GtkTreeView
*tree_view
,
400 GtkTreePath
**startpathp
,
401 GtkTreePath
**endpathp
) {
402 GdkRectangle visible_tc
[1];
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);*/
410 /* Convert top-left visible point to widget coordinates */
412 gtk_tree_view_tree_to_widget_coords(tree_view
,
413 visible_tc
->x
, visible_tc
->y
,
415 //fprintf(stderr, " start widget coords: %dx%d\n", x_wc, y_wc);
416 gtk_tree_view_get_path_at_pos(tree_view
,
423 /* Convert bottom-left visible point to widget coordinates */
424 /* Convert top-left visible point to widget coordinates */
426 gtk_tree_view_tree_to_widget_coords(tree_view
,
428 visible_tc
->y
+ visible_tc
->height
- 1,
430 //fprintf(stderr, " end widget coords: %dx%d\n", x_wc, y_wc);
431 gtk_tree_view_get_path_at_pos(tree_view
,
440 /** @brief Move to the next/prev match
441 * @param direction -1 for prev, +1 for next
443 static void choose_move(int direction
) {
444 /* Refocus the main view so typahead find continues to work */
445 gtk_widget_grab_focus(choose_view
);
446 /* If there's no results we have nothing to do */
447 if(!choose_n_search_results
)
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))
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
) {
462 = gtk_tree_row_reference_get_path(choose_search_references
[n
]);
465 /* gtk_tree_path_compare returns -1, 0 or 1 so we compare naively with
467 if(gtk_tree_path_compare(limitpath
, path
) + direction
== 0) {
468 choose_make_path_visible(path
, 0.5);
469 gtk_tree_path_free(path
);
472 gtk_tree_path_free(path
);
474 /* We didn't find one. Loop back to the first/las. */
475 for(int n
= first
; n
!= limit
; n
+= direction
) {
477 = gtk_tree_row_reference_get_path(choose_search_references
[n
]);
480 choose_make_path_visible(path
, 0.5);
481 gtk_tree_path_free(path
);
486 void choose_next_clicked(GtkButton
attribute((unused
)) *button
,
487 gpointer
attribute((unused
)) userdata
) {
491 void choose_prev_clicked(GtkButton
attribute((unused
)) *button
,
492 gpointer
attribute((unused
)) userdata
) {
496 /** @brief Called when the cancel search button is clicked */
497 static void choose_clear_clicked(GtkButton
attribute((unused
)) *button
,
498 gpointer
attribute((unused
)) userdata
) {
499 gtk_entry_set_text(GTK_ENTRY(choose_search_entry
), "");
500 gtk_widget_grab_focus(choose_view
);
501 /* We start things off straight away in this case */
505 /** @brief Called when the user hits ^F to start a new search */
506 void choose_search_new(void) {
507 gtk_editable_select_region(GTK_EDITABLE(choose_search_entry
), 0, -1);
510 /** @brief Create the search widget */
511 GtkWidget
*choose_search_widget(void) {
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", "");
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", "");
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);
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*/);