mirror of
https://github.com/MidnightCommander/mc
synced 2025-01-24 12:12:19 +03:00
Merge branch '3763_run_listbox_with_data'
* 3763_run_listbox_with_data: (dialog_switch_list): simplify the listbox selector. (edit_window_list): simplify the listbox selector. Ticket #3763: introduce run_listbox_with_data().
This commit is contained in:
commit
b39c1b9446
@ -220,9 +220,8 @@ dialog_switch_list (void)
|
|||||||
const size_t dlg_num = g_list_length (mc_dialogs);
|
const size_t dlg_num = g_list_length (mc_dialogs);
|
||||||
int lines, cols;
|
int lines, cols;
|
||||||
Listbox *listbox;
|
Listbox *listbox;
|
||||||
GList *h;
|
GList *h, *selected;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
int rv;
|
|
||||||
|
|
||||||
if (mc_global.midnight_shutdown || mc_current == NULL)
|
if (mc_global.midnight_shutdown || mc_current == NULL)
|
||||||
return;
|
return;
|
||||||
@ -244,20 +243,14 @@ dialog_switch_list (void)
|
|||||||
else
|
else
|
||||||
title = g_strdup ("");
|
title = g_strdup ("");
|
||||||
|
|
||||||
listbox_add_item (listbox->list, LISTBOX_APPEND_BEFORE, get_hotkey (i++), title, NULL,
|
listbox_add_item (listbox->list, LISTBOX_APPEND_BEFORE, get_hotkey (i++), title, h, FALSE);
|
||||||
FALSE);
|
|
||||||
|
|
||||||
g_free (title);
|
g_free (title);
|
||||||
}
|
}
|
||||||
|
|
||||||
listbox_select_entry (listbox->list, dlg_num - 1 - g_list_position (mc_dialogs, mc_current));
|
selected = run_listbox_with_data (listbox, mc_current);
|
||||||
rv = run_listbox (listbox);
|
if (selected != NULL)
|
||||||
|
dialog_switch_goto (selected);
|
||||||
if (rv >= 0)
|
|
||||||
{
|
|
||||||
h = g_list_nth (mc_dialogs, dlg_num - 1 - rv);
|
|
||||||
dialog_switch_goto (h);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* --------------------------------------------------------------------------------------------- */
|
/* --------------------------------------------------------------------------------------------- */
|
||||||
|
@ -138,3 +138,38 @@ run_listbox (Listbox * l)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* --------------------------------------------------------------------------------------------- */
|
/* --------------------------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A variant of run_listbox() which is more convenient to use when we
|
||||||
|
* need to select arbitrary 'data'.
|
||||||
|
*
|
||||||
|
* @param select the item to select initially, by its 'data'. Optional.
|
||||||
|
* @return the 'data' of the item selected, or NULL if none selected.
|
||||||
|
*/
|
||||||
|
void *
|
||||||
|
run_listbox_with_data (Listbox * l, const void *select)
|
||||||
|
{
|
||||||
|
void *val = NULL;
|
||||||
|
|
||||||
|
if (select != NULL)
|
||||||
|
listbox_select_entry (l->list, listbox_search_data (l->list, select));
|
||||||
|
|
||||||
|
if (dlg_run (l->dlg) != B_CANCEL)
|
||||||
|
{
|
||||||
|
WLEntry *e;
|
||||||
|
e = listbox_get_nth_item (l->list, l->list->pos);
|
||||||
|
if (e != NULL)
|
||||||
|
{
|
||||||
|
/* The assert guards against returning a soon-to-be deallocated
|
||||||
|
* pointer (as in listbox_add_item(..., TRUE)). */
|
||||||
|
g_assert (!e->free_data);
|
||||||
|
val = e->data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dlg_destroy (l->dlg);
|
||||||
|
g_free (l);
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* --------------------------------------------------------------------------------------------- */
|
||||||
|
@ -29,6 +29,7 @@ Listbox *create_listbox_window_centered (int center_y, int center_x, int lines,
|
|||||||
const char *title, const char *help);
|
const char *title, const char *help);
|
||||||
Listbox *create_listbox_window (int lines, int cols, const char *title, const char *help);
|
Listbox *create_listbox_window (int lines, int cols, const char *title, const char *help);
|
||||||
int run_listbox (Listbox * l);
|
int run_listbox (Listbox * l);
|
||||||
|
void *run_listbox_with_data (Listbox * l, const void *select);
|
||||||
|
|
||||||
/*** inline functions ****************************************************************************/
|
/*** inline functions ****************************************************************************/
|
||||||
|
|
||||||
|
@ -570,6 +570,9 @@ listbox_new (int y, int x, int height, int width, gboolean deletable, lcback_fn
|
|||||||
|
|
||||||
/* --------------------------------------------------------------------------------------------- */
|
/* --------------------------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds item by its label.
|
||||||
|
*/
|
||||||
int
|
int
|
||||||
listbox_search_text (WListbox * l, const char *text)
|
listbox_search_text (WListbox * l, const char *text)
|
||||||
{
|
{
|
||||||
@ -592,6 +595,31 @@ listbox_search_text (WListbox * l, const char *text)
|
|||||||
|
|
||||||
/* --------------------------------------------------------------------------------------------- */
|
/* --------------------------------------------------------------------------------------------- */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Finds item by its 'data' slot.
|
||||||
|
*/
|
||||||
|
int
|
||||||
|
listbox_search_data (WListbox * l, const void *data)
|
||||||
|
{
|
||||||
|
if (!listbox_is_empty (l))
|
||||||
|
{
|
||||||
|
int i;
|
||||||
|
GList *le;
|
||||||
|
|
||||||
|
for (i = 0, le = g_queue_peek_head_link (l->list); le != NULL; i++, le = g_list_next (le))
|
||||||
|
{
|
||||||
|
WLEntry *e = LENTRY (le->data);
|
||||||
|
|
||||||
|
if (e->data == data)
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (-1);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* --------------------------------------------------------------------------------------------- */
|
||||||
|
|
||||||
/* Selects the first entry and scrolls the list to the top */
|
/* Selects the first entry and scrolls the list to the top */
|
||||||
void
|
void
|
||||||
listbox_select_first (WListbox * l)
|
listbox_select_first (WListbox * l)
|
||||||
|
@ -64,6 +64,7 @@ extern const global_keymap_t *listbox_map;
|
|||||||
|
|
||||||
WListbox *listbox_new (int y, int x, int height, int width, gboolean deletable, lcback_fn callback);
|
WListbox *listbox_new (int y, int x, int height, int width, gboolean deletable, lcback_fn callback);
|
||||||
int listbox_search_text (WListbox * l, const char *text);
|
int listbox_search_text (WListbox * l, const char *text);
|
||||||
|
int listbox_search_data (WListbox * l, const void *data);
|
||||||
void listbox_select_first (WListbox * l);
|
void listbox_select_first (WListbox * l);
|
||||||
void listbox_select_last (WListbox * l);
|
void listbox_select_last (WListbox * l);
|
||||||
void listbox_select_entry (WListbox * l, int dest);
|
void listbox_select_entry (WListbox * l, int dest);
|
||||||
|
@ -319,13 +319,12 @@ get_hotkey (int n)
|
|||||||
static void
|
static void
|
||||||
edit_window_list (const WDialog * h)
|
edit_window_list (const WDialog * h)
|
||||||
{
|
{
|
||||||
const size_t offset = 2; /* skip menu and buttonbar */
|
const size_t dlg_num = g_list_length (h->widgets) - 2; /* 2 = skip menu and buttonbar */
|
||||||
const size_t dlg_num = g_list_length (h->widgets) - offset;
|
|
||||||
int lines, cols;
|
int lines, cols;
|
||||||
Listbox *listbox;
|
Listbox *listbox;
|
||||||
GList *w;
|
GList *w;
|
||||||
|
WEdit *selected;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
int rv;
|
|
||||||
|
|
||||||
lines = MIN ((size_t) (LINES * 2 / 3), dlg_num);
|
lines = MIN ((size_t) (LINES * 2 / 3), dlg_num);
|
||||||
cols = COLS * 2 / 3;
|
cols = COLS * 2 / 3;
|
||||||
@ -346,18 +345,13 @@ edit_window_list (const WDialog * h)
|
|||||||
vfs_path_as_str (e->filename_vpath));
|
vfs_path_as_str (e->filename_vpath));
|
||||||
|
|
||||||
listbox_add_item (listbox->list, LISTBOX_APPEND_AT_END, get_hotkey (i++),
|
listbox_add_item (listbox->list, LISTBOX_APPEND_AT_END, get_hotkey (i++),
|
||||||
str_term_trim (fname, WIDGET (listbox->list)->cols - 2), NULL, FALSE);
|
str_term_trim (fname, WIDGET (listbox->list)->cols - 2), e, FALSE);
|
||||||
g_free (fname);
|
g_free (fname);
|
||||||
}
|
}
|
||||||
|
|
||||||
rv = g_list_position (h->widgets, h->current) - offset;
|
selected = run_listbox_with_data (listbox, h->current->data);
|
||||||
listbox_select_entry (listbox->list, rv);
|
if (selected != NULL)
|
||||||
rv = run_listbox (listbox);
|
widget_select (WIDGET (selected));
|
||||||
if (rv >= 0)
|
|
||||||
{
|
|
||||||
w = g_list_nth (h->widgets, rv + offset);
|
|
||||||
widget_select (w->data);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* --------------------------------------------------------------------------------------------- */
|
/* --------------------------------------------------------------------------------------------- */
|
||||||
|
Loading…
Reference in New Issue
Block a user