By default, WOP_WANT_HOTKEY option is off.
[midnight-commander.git] / lib / widget / listbox.c
blob6e89300a76f6fa257f6c62e2477c63dff99bbbe2
1 /*
2 Widgets for the Midnight Commander
4 Copyright (C) 1994-2016
5 Free Software Foundation, Inc.
7 Authors:
8 Radek Doulik, 1994, 1995
9 Miguel de Icaza, 1994, 1995
10 Jakub Jelinek, 1995
11 Andrej Borsenkow, 1996
12 Norbert Warmuth, 1997
13 Andrew Borodin <aborodin@vmail.ru>, 2009, 2010, 2013, 2016
15 This file is part of the Midnight Commander.
17 The Midnight Commander is free software: you can redistribute it
18 and/or modify it under the terms of the GNU General Public License as
19 published by the Free Software Foundation, either version 3 of the License,
20 or (at your option) any later version.
22 The Midnight Commander is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program. If not, see <http://www.gnu.org/licenses/>.
31 /** \file listbox.c
32 * \brief Source: WListbox widget
35 #include <config.h>
37 #include <stdlib.h>
39 #include "lib/global.h"
41 #include "lib/tty/tty.h"
42 #include "lib/skin.h"
43 #include "lib/strutil.h"
44 #include "lib/util.h" /* Q_() */
45 #include "lib/keybind.h" /* global_keymap_t */
46 #include "lib/widget.h"
48 /*** global variables ****************************************************************************/
50 const global_keymap_t *listbox_map = NULL;
52 /*** file scope macro definitions ****************************************************************/
54 /* Gives the position of the last item. */
55 #define LISTBOX_LAST(l) (g_queue_is_empty ((l)->list) ? 0 : (int) g_queue_get_length ((l)->list) - 1)
57 /*** file scope type declarations ****************************************************************/
59 /*** file scope variables ************************************************************************/
61 /*** file scope functions ************************************************************************/
63 static int
64 listbox_entry_cmp (const void *a, const void *b, void *user_data)
66 const WLEntry *ea = (const WLEntry *) a;
67 const WLEntry *eb = (const WLEntry *) b;
69 (void) user_data;
71 return strcmp (ea->text, eb->text);
74 /* --------------------------------------------------------------------------------------------- */
76 static void
77 listbox_entry_free (void *data)
79 WLEntry *e = data;
81 g_free (e->text);
82 if (e->free_data)
83 g_free (e->data);
84 g_free (e);
87 /* --------------------------------------------------------------------------------------------- */
89 static void
90 listbox_drawscroll (WListbox * l)
92 Widget *w = WIDGET (l);
93 int max_line = w->lines - 1;
94 int line = 0;
95 int i;
96 int length;
98 /* Are we at the top? */
99 widget_move (w, 0, w->cols);
100 if (l->top == 0)
101 tty_print_one_vline (TRUE);
102 else
103 tty_print_char ('^');
105 length = g_queue_get_length (l->list);
107 /* Are we at the bottom? */
108 widget_move (w, max_line, w->cols);
109 if (l->top + w->lines == length || w->lines >= length)
110 tty_print_one_vline (TRUE);
111 else
112 tty_print_char ('v');
114 /* Now draw the nice relative pointer */
115 if (!g_queue_is_empty (l->list))
116 line = 1 + ((l->pos * (w->lines - 2)) / length);
118 for (i = 1; i < max_line; i++)
120 widget_move (w, i, w->cols);
121 if (i != line)
122 tty_print_one_vline (TRUE);
123 else
124 tty_print_char ('*');
128 /* --------------------------------------------------------------------------------------------- */
130 static void
131 listbox_draw (WListbox * l, gboolean focused)
133 Widget *w = WIDGET (l);
134 const WDialog *h = w->owner;
135 gboolean disabled;
136 int normalc, selc;
137 int length = 0;
138 GList *le = NULL;
139 int pos;
140 int i;
141 int sel_line = -1;
143 disabled = widget_get_state (w, WST_DISABLED);
144 normalc = disabled ? DISABLED_COLOR : h->color[DLG_COLOR_NORMAL];
145 /* *INDENT-OFF* */
146 selc = disabled
147 ? DISABLED_COLOR
148 : focused
149 ? h->color[DLG_COLOR_HOT_FOCUS]
150 : h->color[DLG_COLOR_FOCUS];
151 /* *INDENT-ON* */
153 if (l->list != NULL)
155 length = g_queue_get_length (l->list);
156 le = g_queue_peek_nth_link (l->list, (guint) l->top);
159 /* pos = (le == NULL) ? 0 : g_list_position (l->list, le); */
160 pos = (le == NULL) ? 0 : l->top;
162 for (i = 0; i < w->lines; i++)
164 const char *text = "";
166 /* Display the entry */
167 if (pos == l->pos && sel_line == -1)
169 sel_line = i;
170 tty_setcolor (selc);
172 else
173 tty_setcolor (normalc);
175 widget_move (l, i, 1);
177 if (l->list != NULL && le != NULL && (i == 0 || pos < length))
179 WLEntry *e = LENTRY (le->data);
181 text = e->text;
182 le = g_list_next (le);
183 pos++;
186 tty_print_string (str_fit_to_term (text, w->cols - 2, J_LEFT_FIT));
189 l->cursor_y = sel_line;
191 if (l->scrollbar && length > w->lines)
193 tty_setcolor (normalc);
194 listbox_drawscroll (l);
198 /* --------------------------------------------------------------------------------------------- */
200 static int
201 listbox_check_hotkey (WListbox * l, int key)
203 if (!listbox_is_empty (l))
205 int i;
206 GList *le;
208 for (i = 0, le = g_queue_peek_head_link (l->list); le != NULL; i++, le = g_list_next (le))
210 WLEntry *e = LENTRY (le->data);
212 if (e->hotkey == key)
213 return i;
217 return (-1);
220 /* --------------------------------------------------------------------------------------------- */
222 /* Calculates the item displayed at screen row 'y' (y==0 being the widget's 1st row). */
223 static int
224 listbox_y_pos (WListbox * l, int y)
226 return MIN (l->top + y, LISTBOX_LAST (l));
229 /* --------------------------------------------------------------------------------------------- */
231 static void
232 listbox_fwd (WListbox * l, gboolean wrap)
234 if ((guint) l->pos + 1 < g_queue_get_length (l->list))
235 listbox_select_entry (l, l->pos + 1);
236 else if (wrap)
237 listbox_select_first (l);
240 /* --------------------------------------------------------------------------------------------- */
242 static void
243 listbox_fwd_n (WListbox * l, int n)
245 listbox_select_entry (l, MIN (l->pos + n, LISTBOX_LAST (l)));
248 /* --------------------------------------------------------------------------------------------- */
250 static void
251 listbox_back (WListbox * l, gboolean wrap)
253 if (l->pos > 0)
254 listbox_select_entry (l, l->pos - 1);
255 else if (wrap)
256 listbox_select_last (l);
259 /* --------------------------------------------------------------------------------------------- */
261 static void
262 listbox_back_n (WListbox * l, int n)
264 listbox_select_entry (l, MAX (l->pos - n, 0));
267 /* --------------------------------------------------------------------------------------------- */
269 static cb_ret_t
270 listbox_execute_cmd (WListbox * l, long command)
272 cb_ret_t ret = MSG_HANDLED;
273 Widget *w = WIDGET (l);
275 if (l->list == NULL || g_queue_is_empty (l->list))
276 return MSG_NOT_HANDLED;
278 switch (command)
280 case CK_Up:
281 listbox_back (l, TRUE);
282 break;
283 case CK_Down:
284 listbox_fwd (l, TRUE);
285 break;
286 case CK_Top:
287 listbox_select_first (l);
288 break;
289 case CK_Bottom:
290 listbox_select_last (l);
291 break;
292 case CK_PageUp:
293 listbox_back_n (l, w->lines - 1);
294 break;
295 case CK_PageDown:
296 listbox_fwd_n (l, w->lines - 1);
297 break;
298 case CK_Delete:
299 if (l->deletable)
301 gboolean is_last, is_more;
302 int length;
304 length = g_queue_get_length (l->list);
306 is_last = (l->pos + 1 >= length);
307 is_more = (l->top + w->lines >= length);
309 listbox_remove_current (l);
310 if ((l->top > 0) && (is_last || is_more))
311 l->top--;
313 break;
314 case CK_Clear:
315 if (l->deletable && mc_global.widget.confirm_history_cleanup
316 /* TRANSLATORS: no need to translate 'DialogTitle', it's just a context prefix */
317 && (query_dialog (Q_ ("DialogTitle|History cleanup"),
318 _("Do you want clean this history?"),
319 D_ERROR, 2, _("&Yes"), _("&No")) == 0))
320 listbox_remove_list (l);
321 break;
322 default:
323 ret = MSG_NOT_HANDLED;
326 return ret;
329 /* --------------------------------------------------------------------------------------------- */
331 /* Return MSG_HANDLED if we want a redraw */
332 static cb_ret_t
333 listbox_key (WListbox * l, int key)
335 long command;
337 if (l->list == NULL)
338 return MSG_NOT_HANDLED;
340 /* focus on listbox item N by '0'..'9' keys */
341 if (key >= '0' && key <= '9')
343 listbox_select_entry (l, key - '0');
344 return MSG_HANDLED;
347 command = keybind_lookup_keymap_command (listbox_map, key);
348 if (command == CK_IgnoreKey)
349 return MSG_NOT_HANDLED;
350 return listbox_execute_cmd (l, command);
353 /* --------------------------------------------------------------------------------------------- */
355 /* Listbox item adding function */
356 static inline void
357 listbox_append_item (WListbox * l, WLEntry * e, listbox_append_t pos)
359 if (l->list == NULL)
361 l->list = g_queue_new ();
362 pos = LISTBOX_APPEND_AT_END;
365 switch (pos)
367 case LISTBOX_APPEND_AT_END:
368 g_queue_push_tail (l->list, e);
369 break;
371 case LISTBOX_APPEND_BEFORE:
372 g_queue_insert_before (l->list, g_queue_peek_nth_link (l->list, (guint) l->pos), e);
373 break;
375 case LISTBOX_APPEND_AFTER:
376 g_queue_insert_after (l->list, g_queue_peek_nth_link (l->list, (guint) l->pos), e);
377 break;
379 case LISTBOX_APPEND_SORTED:
380 g_queue_insert_sorted (l->list, e, (GCompareDataFunc) listbox_entry_cmp, NULL);
381 break;
383 default:
384 break;
388 /* --------------------------------------------------------------------------------------------- */
390 /* Call this whenever the user changes the selected item. */
391 static void
392 listbox_on_change (WListbox * l)
394 listbox_draw (l, TRUE);
395 send_message (WIDGET (l)->owner, l, MSG_NOTIFY, l->pos, NULL);
398 /* --------------------------------------------------------------------------------------------- */
400 static void
401 listbox_do_action (WListbox * l)
403 int action;
405 if (l->callback != NULL)
406 action = l->callback (l);
407 else
408 action = LISTBOX_DONE;
410 if (action == LISTBOX_DONE)
412 WDialog *h = WIDGET (l)->owner;
414 h->ret_value = B_ENTER;
415 dlg_stop (h);
419 /* --------------------------------------------------------------------------------------------- */
421 static void
422 listbox_run_hotkey (WListbox * l, int pos)
424 listbox_select_entry (l, pos);
425 listbox_on_change (l);
426 listbox_do_action (l);
429 /* --------------------------------------------------------------------------------------------- */
431 static inline void
432 listbox_destroy (WListbox * l)
434 listbox_remove_list (l);
437 /* --------------------------------------------------------------------------------------------- */
439 static cb_ret_t
440 listbox_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
442 WListbox *l = LISTBOX (w);
443 cb_ret_t ret_code;
445 switch (msg)
447 case MSG_INIT:
448 return MSG_HANDLED;
450 case MSG_HOTKEY:
452 int pos;
454 pos = listbox_check_hotkey (l, parm);
455 if (pos < 0)
456 return MSG_NOT_HANDLED;
458 listbox_run_hotkey (l, pos);
460 return MSG_HANDLED;
463 case MSG_KEY:
464 ret_code = listbox_key (l, parm);
465 if (ret_code != MSG_NOT_HANDLED)
466 listbox_on_change (l);
467 return ret_code;
469 case MSG_ACTION:
470 return listbox_execute_cmd (l, parm);
472 case MSG_CURSOR:
473 widget_move (l, l->cursor_y, 0);
474 return MSG_HANDLED;
476 case MSG_FOCUS:
477 case MSG_UNFOCUS:
478 l->focused = msg == MSG_FOCUS;
479 /* fall through */
480 case MSG_DRAW:
481 listbox_draw (l, l->focused);
482 return MSG_HANDLED;
484 case MSG_DESTROY:
485 listbox_destroy (l);
486 return MSG_HANDLED;
488 case MSG_RESIZE:
489 return MSG_HANDLED;
491 default:
492 return widget_default_callback (w, sender, msg, parm, data);
496 /* --------------------------------------------------------------------------------------------- */
498 static void
499 listbox_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event)
501 WListbox *l = LISTBOX (w);
502 int old_pos;
504 old_pos = l->pos;
506 switch (msg)
508 case MSG_MOUSE_DOWN:
509 dlg_select_widget (l);
510 listbox_select_entry (l, listbox_y_pos (l, event->y));
511 break;
513 case MSG_MOUSE_SCROLL_UP:
514 listbox_back (l, FALSE);
515 break;
517 case MSG_MOUSE_SCROLL_DOWN:
518 listbox_fwd (l, FALSE);
519 break;
521 case MSG_MOUSE_DRAG:
522 event->result.repeat = TRUE; /* It'd be functional even without this. */
523 listbox_select_entry (l, listbox_y_pos (l, event->y));
524 break;
526 case MSG_MOUSE_CLICK:
527 /* We don't call listbox_select_entry() here: MSG_MOUSE_DOWN/DRAG did this already. */
528 if (event->count == GPM_DOUBLE) /* Double click */
529 listbox_do_action (l);
530 break;
532 default:
533 break;
536 /* If the selection has changed, we redraw the widget and notify the dialog. */
537 if (l->pos != old_pos)
538 listbox_on_change (l);
541 /* --------------------------------------------------------------------------------------------- */
542 /*** public functions ****************************************************************************/
543 /* --------------------------------------------------------------------------------------------- */
545 WListbox *
546 listbox_new (int y, int x, int height, int width, gboolean deletable, lcback_fn callback)
548 WListbox *l;
549 Widget *w;
551 if (height <= 0)
552 height = 1;
554 l = g_new (WListbox, 1);
555 w = WIDGET (l);
556 widget_init (w, y, x, height, width, listbox_callback, listbox_mouse_callback);
558 l->list = NULL;
559 l->top = l->pos = 0;
560 l->deletable = deletable;
561 l->callback = callback;
562 l->allow_duplicates = TRUE;
563 l->scrollbar = !mc_global.tty.slow_terminal;
564 l->focused = FALSE;
565 widget_want_hotkey (w, TRUE);
567 return l;
570 /* --------------------------------------------------------------------------------------------- */
573 listbox_search_text (WListbox * l, const char *text)
575 if (!listbox_is_empty (l))
577 int i;
578 GList *le;
580 for (i = 0, le = g_queue_peek_head_link (l->list); le != NULL; i++, le = g_list_next (le))
582 WLEntry *e = LENTRY (le->data);
584 if (strcmp (e->text, text) == 0)
585 return i;
589 return (-1);
592 /* --------------------------------------------------------------------------------------------- */
594 /* Selects the first entry and scrolls the list to the top */
595 void
596 listbox_select_first (WListbox * l)
598 l->pos = l->top = 0;
601 /* --------------------------------------------------------------------------------------------- */
603 /* Selects the last entry and scrolls the list to the bottom */
604 void
605 listbox_select_last (WListbox * l)
607 int lines = WIDGET (l)->lines;
608 int length = 0;
610 if (!listbox_is_empty (l))
611 length = g_queue_get_length (l->list);
613 l->pos = length > 0 ? length - 1 : 0;
614 l->top = length > lines ? length - lines : 0;
617 /* --------------------------------------------------------------------------------------------- */
619 void
620 listbox_select_entry (WListbox * l, int dest)
622 GList *le;
623 int pos;
624 gboolean top_seen = FALSE;
626 if (listbox_is_empty (l) || dest < 0)
627 return;
629 /* Special case */
630 for (pos = 0, le = g_queue_peek_head_link (l->list); le != NULL; pos++, le = g_list_next (le))
632 if (pos == l->top)
633 top_seen = TRUE;
635 if (pos == dest)
637 l->pos = dest;
638 if (!top_seen)
639 l->top = l->pos;
640 else
642 int lines = WIDGET (l)->lines;
644 if (l->pos - l->top >= lines)
645 l->top = l->pos - lines + 1;
647 return;
651 /* If we are unable to find it, set decent values */
652 l->pos = l->top = 0;
655 /* --------------------------------------------------------------------------------------------- */
657 /* Returns the current string text as well as the associated extra data */
658 void
659 listbox_get_current (WListbox * l, char **string, void **extra)
661 WLEntry *e = NULL;
662 gboolean ok;
664 if (l != NULL)
665 e = listbox_get_nth_item (l, l->pos);
667 ok = (e != NULL);
669 if (string != NULL)
670 *string = ok ? e->text : NULL;
672 if (extra != NULL)
673 *extra = ok ? e->data : NULL;
676 /* --------------------------------------------------------------------------------------------- */
678 WLEntry *
679 listbox_get_nth_item (const WListbox * l, int pos)
681 if (!listbox_is_empty (l) && pos >= 0)
683 GList *item;
685 item = g_queue_peek_nth_link (l->list, (guint) pos);
686 if (item != NULL)
687 return LENTRY (item->data);
690 return NULL;
693 /* --------------------------------------------------------------------------------------------- */
695 GList *
696 listbox_get_first_link (const WListbox * l)
698 return (l == NULL || l->list == NULL) ? NULL : g_queue_peek_head_link (l->list);
701 /* --------------------------------------------------------------------------------------------- */
703 void
704 listbox_remove_current (WListbox * l)
706 if (!listbox_is_empty (l))
708 GList *current;
709 int length;
711 current = g_queue_peek_nth_link (l->list, (guint) l->pos);
712 listbox_entry_free (LENTRY (current->data));
713 g_queue_delete_link (l->list, current);
715 length = g_queue_get_length (l->list);
717 if (length == 0)
718 l->top = l->pos = 0;
719 else if (l->pos >= length)
720 l->pos = length - 1;
724 /* --------------------------------------------------------------------------------------------- */
726 gboolean
727 listbox_is_empty (const WListbox * l)
729 return (l == NULL || l->list == NULL || g_queue_is_empty (l->list));
732 /* --------------------------------------------------------------------------------------------- */
734 void
735 listbox_set_list (WListbox * l, GList * list)
737 listbox_remove_list (l);
739 if (l != NULL)
741 GList *ll;
743 l->list = g_queue_new ();
745 for (ll = list; ll != NULL; ll = g_list_next (ll))
746 g_queue_push_tail (l->list, ll->data);
748 g_list_free (list);
752 /* --------------------------------------------------------------------------------------------- */
754 void
755 listbox_remove_list (WListbox * l)
757 if (l != NULL)
759 if (l->list != NULL)
761 g_queue_foreach (l->list, (GFunc) listbox_entry_free, NULL);
762 g_queue_free (l->list);
765 l->list = NULL;
766 l->pos = l->top = 0;
770 /* --------------------------------------------------------------------------------------------- */
772 char *
773 listbox_add_item (WListbox * l, listbox_append_t pos, int hotkey, const char *text, void *data,
774 gboolean free_data)
776 WLEntry *entry;
778 if (l == NULL)
779 return NULL;
781 if (!l->allow_duplicates && (listbox_search_text (l, text) >= 0))
782 return NULL;
784 entry = g_new (WLEntry, 1);
785 entry->text = g_strdup (text);
786 entry->data = data;
787 entry->free_data = free_data;
788 entry->hotkey = hotkey;
790 listbox_append_item (l, entry, pos);
792 return entry->text;
795 /* --------------------------------------------------------------------------------------------- */