- * We don't use the built-in tree widgets because they require that you know
- * the children of a node on demand, and we have to wait for the server to tell
- * us.
- */
-
-#include "disobedience.h"
-
-/* Choose track ------------------------------------------------------------ */
-
-WT(label);
-WT(event_box);
-WT(menu);
-WT(menu_item);
-WT(layout);
-WT(vbox);
-WT(arrow);
-WT(hbox);
-WT(button);
-WT(image);
-WT(entry);
-
-/* Types */
-
-struct choosenode;
-
-/** @brief Accumulated information about the tree widget */
-struct displaydata {
- /** @brief Maximum width required */
- guint width;
- /** @brief Maximum height required */
- guint height;
-};
-
-/* instantiate the node vector type */
-
-VECTOR_TYPE(nodevector, struct choosenode *, xrealloc);
-
-/** @brief Signature of function called when a choosenode is filled */
-typedef void (when_filled_callback)(struct choosenode *cn,
- void *wfu);
-
-/** @brief One node in the virtual filesystem */
-struct choosenode {
- struct choosenode *parent; /**< @brief parent node */
- const char *path; /**< @brief full path or 0 */
- const char *sort; /**< @brief sort key */
- const char *display; /**< @brief display name */
- int pending; /**< @brief pending resolve queries */
- unsigned flags;
-#define CN_EXPANDABLE 0x0001 /**< @brief node is expandable */
-#define CN_EXPANDED 0x0002 /**< @brief node is expanded
- *
- * Expandable items are directories;
- * non-expandable ones are files. */
-#define CN_DISPLAYED 0x0004 /**< @brief widget is displayed in layout */
-#define CN_SELECTED 0x0008 /**< @brief node is selected */
-#define CN_GETTING_FILES 0x0010 /**< @brief files inbound */
-#define CN_RESOLVING_FILES 0x0020 /**< @brief resolved files inbound */
-#define CN_GETTING_DIRS 0x0040 /**< @brief directories inbound */
-#define CN_GETTING_ANY 0x0070 /**< @brief getting something */
-#define CN_CONTINGENT 0x0080 /**< @brief expansion contingent on search */
- struct nodevector children; /**< @brief vector of children */
- void (*fill)(struct choosenode *); /**< @brief request child fill or 0 for leaf */
- GtkWidget *container; /**< @brief the container for this row */
- GtkWidget *hbox; /**< @brief the hbox for this row */
- GtkWidget *arrow; /**< @brief arrow widget or 0 */
- GtkWidget *label; /**< @brief text label for this node */
- GtkWidget *marker; /**< @brief queued marker */
-
- when_filled_callback *whenfilled; /**< @brief called when filled or 0 */
- void *wfu; /**< @brief passed to @c whenfilled */
- int ymin; /**< @brief least Y value */
- int ymax; /**< @brief greatest Y value */
-};
-
-/** @brief One item in the popup menu */
-struct choose_menuitem {
- /* Parameters */
- const char *name; /**< @brief name */
-
- /* Callbacks */
- void (*activate)(GtkMenuItem *menuitem, gpointer user_data);
- /**< @brief Called to activate the menu item.
- *
- * @p user_data is the choosenode the mouse pointer is over. */
-
- gboolean (*sensitive)(struct choosenode *cn);
- /* @brief Called to determine whether the menu item should be sensitive.
- *
- * TODO? */
-
- /* State */
- gulong handlerid; /**< @brief signal handler ID */
- GtkWidget *w; /**< @brief menu item widget */
-};
-
-/* Variables */
-
-static GtkWidget *chooselayout;
-static GtkAdjustment *vadjust;
-static GtkWidget *searchentry; /**< @brief search terms */
-static GtkWidget *nextsearch; /**< @brief next search result */
-static GtkWidget *prevsearch; /**< @brief previous search result */
-static struct choosenode *root;
-static GtkWidget *track_menu; /**< @brief track popup menu */
-static GtkWidget *dir_menu; /**< @brief directory popup menu */
-static struct choosenode *last_click; /**< @brief last clicked node for selection */
-static int files_visible; /**< @brief total files visible */
-static int files_selected; /**< @brief total files selected */
-static int gets_in_flight; /**< @brief total gets in flight */
-static int search_in_flight; /**< @brief a search is underway */
-static int search_obsolete; /**< @brief the current search is void */
-static char **searchresults; /**< @brief search results */
-static int nsearchresults; /**< @brief number of results */
-static int nsearchvisible; /**< @brief number of search results visible */
-static struct hash *searchhash; /**< @brief hash of search results */
-static struct progress_window *spw; /**< @brief progress window */
-static struct choosenode **searchnodes; /**< @brief choosenodes of search results */
-static int suppress_redisplay; /**< @brief suppress redisplay */
-
-/* Forward Declarations */
-
-static void clear_children(struct choosenode *cn);
-static struct choosenode *newnode(struct choosenode *parent,
- const char *path,
- const char *display,
- const char *sort,
- unsigned flags,
- void (*fill)(struct choosenode *));
-static void fill_root_node(struct choosenode *cn);
-static void fill_directory_node(struct choosenode *cn);
-static void got_files(void *v, int nvec, char **vec);
-static void got_resolved_file(void *v, const char *track);
-static void got_dirs(void *v, int nvec, char **vec);
-
-static void expand_node(struct choosenode *cn, int contingent);
-static void contract_node(struct choosenode *cn);
-static void updated_node(struct choosenode *cn, int redisplay,
- const char *why);
-
-static void display_selection(struct choosenode *cn);
-static void clear_selection(struct choosenode *cn);
-
-static void redisplay_tree(const char *why);
-static struct displaydata display_tree(struct choosenode *cn, int x, int y);
-static void undisplay_tree(struct choosenode *cn);
-static void initiate_search(void);
-static void delete_widgets(struct choosenode *cn);
-static void expand_from(struct choosenode *cn);
-static struct choosenode *first_search_result(struct choosenode *cn);
-
-static void clicked_choosenode(GtkWidget attribute((unused)) *widget,
- GdkEventButton *event,
- gpointer user_data);
-
-static void activate_track_play(GtkMenuItem *menuitem, gpointer user_data);
-static void activate_track_properties(GtkMenuItem *menuitem, gpointer user_data);
-
-static gboolean sensitive_track_play(struct choosenode *cn);
-static gboolean sensitive_track_properties(struct choosenode *cn);
-
-static void activate_dir_play(GtkMenuItem *menuitem, gpointer user_data);
-static void activate_dir_properties(GtkMenuItem *menuitem, gpointer user_data);
-static void activate_dir_select(GtkMenuItem *menuitem, gpointer user_data);
-
-static gboolean sensitive_dir_play(struct choosenode *cn);
-static gboolean sensitive_dir_properties(struct choosenode *cn);
-static gboolean sensitive_dir_select(struct choosenode *cn);
-
-/** @brief Track menu items */
-static struct choose_menuitem track_menuitems[] = {
- { "Play track", activate_track_play, sensitive_track_play, 0, 0 },
- { "Track properties", activate_track_properties, sensitive_track_properties, 0, 0 },
- { 0, 0, 0, 0, 0 }
-};
-
-/** @brief Directory menu items */
-static struct choose_menuitem dir_menuitems[] = {
- { "Play all tracks", activate_dir_play, sensitive_dir_play, 0, 0 },
- { "Track properties", activate_dir_properties, sensitive_dir_properties, 0, 0 },
- { "Select all tracks", activate_dir_select, sensitive_dir_select, 0, 0 },
- { 0, 0, 0, 0, 0 }
-};
-
-/* Maintaining the data structure ------------------------------------------ */
-
-static char *cnflags(const struct choosenode *cn) {
- unsigned f = cn->flags, n;
- struct dynstr d[1];
-
- static const char *bits[] = {
- "expandable",
- "expanded",
- "displayed",
- "selected",
- "getting_files",
- "resolving_files",
- "getting_dirs",
- "contingent"
- };
-#define NBITS (sizeof bits / sizeof *bits)
-
- dynstr_init(d);
- if(!f)
- dynstr_append(d, '0');
- else {
- for(n = 0; n < NBITS; ++n) {
- const unsigned bit = 1 << n;
- if(f & bit) {
- if(d->nvec)
- dynstr_append(d, '|');
- dynstr_append_string(d, bits[n]);
- f ^= bit;
- }
- }
- if(f) {
- char buf[32];
- if(d->nvec)
- dynstr_append(d, '|');
- sprintf(buf, "%#x", f);
- dynstr_append_string(d, buf);
- }
- }
- dynstr_terminate(d);
- return d->vec;
-}
-
-/** @brief Create a new node */
-static struct choosenode *newnode(struct choosenode *parent,
- const char *path,
- const char *display,
- const char *sort,
- unsigned flags,
- void (*fill)(struct choosenode *)) {
- struct choosenode *const n = xmalloc(sizeof *n);
-
- D(("newnode %s %s", path, display));
- if(flags & CN_EXPANDABLE)
- assert(fill);
- else
- assert(!fill);
- n->parent = parent;
- n->path = path;
- n->display = display;
- n->sort = sort;
- n->flags = flags;
- nodevector_init(&n->children);
- n->fill = fill;
- if(parent)
- nodevector_append(&parent->children, n);
- return n;
-}
-
-/** @brief Called when a node has been filled