Ticket #2384: allow rebind Fx keys in the file manager.
[midnight-commander.git] / lib / widget / listbox.c
blobc4f0d57625ead7baa8d81c4077f71f3b9a18cb9c
1 /*
2 Widgets for the Midnight Commander
4 Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
5 2004, 2005, 2006, 2007, 2009, 2010, 2011
6 The Free Software Foundation, Inc.
8 Authors:
9 Radek Doulik, 1994, 1995
10 Miguel de Icaza, 1994, 1995
11 Jakub Jelinek, 1995
12 Andrej Borsenkow, 1996
13 Norbert Warmuth, 1997
14 Andrew Borodin <aborodin@vmail.ru>, 2009, 2010
16 This file is part of the Midnight Commander.
18 The Midnight Commander is free software: you can redistribute it
19 and/or modify it under the terms of the GNU General Public License as
20 published by the Free Software Foundation, either version 3 of the License,
21 or (at your option) any later version.
23 The Midnight Commander is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
28 You should have received a copy of the GNU General Public License
29 along with this program. If not, see <http://www.gnu.org/licenses/>.
32 /** \file listbox.c
33 * \brief Source: WListbox widget
36 #include <config.h>
38 #include <stdlib.h>
40 #include "lib/global.h"
42 #include "lib/tty/tty.h"
43 #include "lib/tty/mouse.h"
44 #include "lib/skin.h"
45 #include "lib/strutil.h"
46 #include "lib/util.h" /* Q_() */
47 #include "lib/keybind.h" /* global_keymap_t */
48 #include "lib/widget.h"
50 /*** global variables ****************************************************************************/
52 const global_keymap_t *listbox_map = NULL;
54 /*** file scope macro definitions ****************************************************************/
56 /*** file scope type declarations ****************************************************************/
58 /*** file scope variables ************************************************************************/
60 /*** file scope functions ************************************************************************/
62 static int
63 listbox_entry_cmp (const void *a, const void *b)
65 const WLEntry *ea = (const WLEntry *) a;
66 const WLEntry *eb = (const WLEntry *) b;
68 return strcmp (ea->text, eb->text);
71 /* --------------------------------------------------------------------------------------------- */
73 static void
74 listbox_entry_free (void *data)
76 WLEntry *e = data;
77 g_free (e->text);
78 g_free (e);
81 /* --------------------------------------------------------------------------------------------- */
83 static void
84 listbox_drawscroll (WListbox * l)
86 const int max_line = l->widget.lines - 1;
87 int line = 0;
88 int i;
90 /* Are we at the top? */
91 widget_move (&l->widget, 0, l->widget.cols);
92 if (l->top == 0)
93 tty_print_one_vline (TRUE);
94 else
95 tty_print_char ('^');
97 /* Are we at the bottom? */
98 widget_move (&l->widget, max_line, l->widget.cols);
99 if ((l->top + l->widget.lines == l->count) || (l->widget.lines >= l->count))
100 tty_print_one_vline (TRUE);
101 else
102 tty_print_char ('v');
104 /* Now draw the nice relative pointer */
105 if (l->count != 0)
106 line = 1 + ((l->pos * (l->widget.lines - 2)) / l->count);
108 for (i = 1; i < max_line; i++)
110 widget_move (&l->widget, i, l->widget.cols);
111 if (i != line)
112 tty_print_one_vline (TRUE);
113 else
114 tty_print_char ('*');
118 /* --------------------------------------------------------------------------------------------- */
120 static void
121 listbox_draw (WListbox * l, gboolean focused)
123 const Dlg_head *h = l->widget.owner;
124 const gboolean disabled = (((Widget *) l)->options & W_DISABLED) != 0;
125 const int normalc = disabled ? DISABLED_COLOR : h->color[DLG_COLOR_NORMAL];
126 int selc =
127 disabled ? DISABLED_COLOR : focused ? h->
128 color[DLG_COLOR_HOT_FOCUS] : h->color[DLG_COLOR_FOCUS];
130 GList *le;
131 int pos;
132 int i;
133 int sel_line = -1;
135 le = g_list_nth (l->list, l->top);
136 /* pos = (le == NULL) ? 0 : g_list_position (l->list, le); */
137 pos = (le == NULL) ? 0 : l->top;
139 for (i = 0; i < l->widget.lines; i++)
141 const char *text;
143 /* Display the entry */
144 if (pos == l->pos && sel_line == -1)
146 sel_line = i;
147 tty_setcolor (selc);
149 else
150 tty_setcolor (normalc);
152 widget_move (&l->widget, i, 1);
154 if ((i > 0 && pos >= l->count) || (l->list == NULL) || (le == NULL))
155 text = "";
156 else
158 WLEntry *e = (WLEntry *) le->data;
159 text = e->text;
160 le = g_list_next (le);
161 pos++;
164 tty_print_string (str_fit_to_term (text, l->widget.cols - 2, J_LEFT_FIT));
167 l->cursor_y = sel_line;
169 if (l->scrollbar && (l->count > l->widget.lines))
171 tty_setcolor (normalc);
172 listbox_drawscroll (l);
176 /* --------------------------------------------------------------------------------------------- */
178 static int
179 listbox_check_hotkey (WListbox * l, int key)
181 int i;
182 GList *le;
184 for (i = 0, le = l->list; le != NULL; i++, le = g_list_next (le))
186 WLEntry *e = (WLEntry *) le->data;
188 if (e->hotkey == key)
189 return i;
192 return (-1);
195 /* --------------------------------------------------------------------------------------------- */
197 /* Selects from base the pos element */
198 static int
199 listbox_select_pos (WListbox * l, int base, int pos)
201 int last = l->count - 1;
203 base += pos;
204 base = min (base, last);
206 return base;
209 /* --------------------------------------------------------------------------------------------- */
211 static void
212 listbox_fwd (WListbox * l)
214 if (l->pos + 1 >= l->count)
215 listbox_select_first (l);
216 else
217 listbox_select_entry (l, l->pos + 1);
220 /* --------------------------------------------------------------------------------------------- */
222 static void
223 listbox_back (WListbox * l)
225 if (l->pos <= 0)
226 listbox_select_last (l);
227 else
228 listbox_select_entry (l, l->pos - 1);
231 /* --------------------------------------------------------------------------------------------- */
233 static cb_ret_t
234 listbox_execute_cmd (WListbox * l, unsigned long command)
236 cb_ret_t ret = MSG_HANDLED;
237 int i;
239 switch (command)
241 case CK_Up:
242 listbox_back (l);
243 break;
244 case CK_Down:
245 listbox_fwd (l);
246 break;
247 case CK_Top:
248 listbox_select_first (l);
249 break;
250 case CK_Bottom:
251 listbox_select_last (l);
252 break;
253 case CK_PageUp:
254 for (i = 0; (i < l->widget.lines - 1) && (l->pos > 0); i++)
255 listbox_back (l);
256 break;
257 case CK_PageDown:
258 for (i = 0; (i < l->widget.lines - 1) && (l->pos < l->count - 1); i++)
259 listbox_fwd (l);
260 break;
261 case CK_Delete:
262 if (l->deletable)
264 gboolean is_last = (l->pos + 1 >= l->count);
265 gboolean is_more = (l->top + l->widget.lines >= l->count);
267 listbox_remove_current (l);
268 if ((l->top > 0) && (is_last || is_more))
269 l->top--;
271 break;
272 case CK_Clear:
273 if (l->deletable && mc_global.widget.confirm_history_cleanup
274 /* TRANSLATORS: no need to translate 'DialogTitle', it's just a context prefix */
275 && (query_dialog (Q_ ("DialogTitle|History cleanup"),
276 _("Do you want clean this history?"),
277 D_ERROR, 2, _("&Yes"), _("&No")) == 0))
278 listbox_remove_list (l);
279 break;
280 default:
281 ret = MSG_NOT_HANDLED;
284 return ret;
287 /* --------------------------------------------------------------------------------------------- */
289 /* Return MSG_HANDLED if we want a redraw */
290 static cb_ret_t
291 listbox_key (WListbox * l, int key)
293 unsigned long command;
295 if (l->list == NULL)
296 return MSG_NOT_HANDLED;
298 /* focus on listbox item N by '0'..'9' keys */
299 if (key >= '0' && key <= '9')
301 int oldpos = l->pos;
302 listbox_select_entry (l, key - '0');
304 /* need scroll to item? */
305 if (abs (oldpos - l->pos) > l->widget.lines)
306 l->top = l->pos;
308 return MSG_HANDLED;
311 command = keybind_lookup_keymap_command (listbox_map, key);
312 if (command == CK_IgnoreKey)
313 return MSG_NOT_HANDLED;
314 return listbox_execute_cmd (l, command);
317 /* --------------------------------------------------------------------------------------------- */
319 /* Listbox item adding function */
320 static inline void
321 listbox_append_item (WListbox * l, WLEntry * e, listbox_append_t pos)
323 switch (pos)
325 case LISTBOX_APPEND_AT_END:
326 l->list = g_list_append (l->list, e);
327 break;
329 case LISTBOX_APPEND_BEFORE:
330 l->list = g_list_insert_before (l->list, g_list_nth (l->list, l->pos), e);
331 if (l->pos > 0)
332 l->pos--;
333 break;
335 case LISTBOX_APPEND_AFTER:
336 l->list = g_list_insert (l->list, e, l->pos + 1);
337 break;
339 case LISTBOX_APPEND_SORTED:
340 l->list = g_list_insert_sorted (l->list, e, (GCompareFunc) listbox_entry_cmp);
341 break;
343 default:
344 return;
347 l->count++;
350 /* --------------------------------------------------------------------------------------------- */
352 static inline void
353 listbox_destroy (WListbox * l)
355 listbox_remove_list (l);
358 /* --------------------------------------------------------------------------------------------- */
360 static cb_ret_t
361 listbox_callback (Widget * w, widget_msg_t msg, int parm)
363 WListbox *l = (WListbox *) w;
364 Dlg_head *h = l->widget.owner;
365 cb_ret_t ret_code;
367 switch (msg)
369 case WIDGET_INIT:
370 return MSG_HANDLED;
372 case WIDGET_HOTKEY:
374 int pos, action;
376 pos = listbox_check_hotkey (l, parm);
377 if (pos < 0)
378 return MSG_NOT_HANDLED;
380 listbox_select_entry (l, pos);
381 h->callback (h, w, DLG_ACTION, l->pos, NULL);
383 if (l->callback != NULL)
384 action = l->callback (l);
385 else
386 action = LISTBOX_DONE;
388 if (action == LISTBOX_DONE)
390 h->ret_value = B_ENTER;
391 dlg_stop (h);
394 return MSG_HANDLED;
397 case WIDGET_KEY:
398 ret_code = listbox_key (l, parm);
399 if (ret_code != MSG_NOT_HANDLED)
401 listbox_draw (l, TRUE);
402 h->callback (h, w, DLG_ACTION, l->pos, NULL);
404 return ret_code;
406 case WIDGET_COMMAND:
407 return listbox_execute_cmd (l, parm);
409 case WIDGET_CURSOR:
410 widget_move (&l->widget, l->cursor_y, 0);
411 h->callback (h, w, DLG_ACTION, l->pos, NULL);
412 return MSG_HANDLED;
414 case WIDGET_FOCUS:
415 case WIDGET_UNFOCUS:
416 case WIDGET_DRAW:
417 listbox_draw (l, msg != WIDGET_UNFOCUS);
418 return MSG_HANDLED;
420 case WIDGET_DESTROY:
421 listbox_destroy (l);
422 return MSG_HANDLED;
424 case WIDGET_RESIZED:
425 return MSG_HANDLED;
427 default:
428 return default_proc (msg, parm);
432 /* --------------------------------------------------------------------------------------------- */
434 static int
435 listbox_event (Gpm_Event * event, void *data)
437 WListbox *l = (WListbox *) data;
438 Widget *w = (Widget *) data;
440 if (!mouse_global_in_widget (event, w))
441 return MOU_UNHANDLED;
443 /* Single click */
444 if ((event->type & GPM_DOWN) != 0)
445 dlg_select_widget (l);
447 if (l->list == NULL)
448 return MOU_NORMAL;
450 if ((event->type & (GPM_DOWN | GPM_DRAG)) != 0)
452 int ret = MOU_REPEAT;
453 Gpm_Event local;
454 int i;
456 local = mouse_get_local (event, w);
457 if (local.y < 1)
458 for (i = -local.y; i >= 0; i--)
459 listbox_back (l);
460 else if (local.y > w->lines)
461 for (i = local.y - w->lines; i > 0; i--)
462 listbox_fwd (l);
463 else if ((local.buttons & GPM_B_UP) != 0)
465 listbox_back (l);
466 ret = MOU_NORMAL;
468 else if ((local.buttons & GPM_B_DOWN) != 0)
470 listbox_fwd (l);
471 ret = MOU_NORMAL;
473 else
474 listbox_select_entry (l, listbox_select_pos (l, l->top, local.y - 1));
476 /* We need to refresh ourselves since the dialog manager doesn't */
477 /* know about this event */
478 listbox_draw (l, TRUE);
479 return ret;
482 /* Double click */
483 if ((event->type & (GPM_DOUBLE | GPM_UP)) == (GPM_UP | GPM_DOUBLE))
485 Gpm_Event local;
486 int action;
488 local = mouse_get_local (event, w);
489 dlg_select_widget (l);
490 listbox_select_entry (l, listbox_select_pos (l, l->top, local.y - 1));
492 if (l->callback != NULL)
493 action = l->callback (l);
494 else
495 action = LISTBOX_DONE;
497 if (action == LISTBOX_DONE)
499 w->owner->ret_value = B_ENTER;
500 dlg_stop (w->owner);
504 return MOU_NORMAL;
507 /* --------------------------------------------------------------------------------------------- */
508 /*** public functions ****************************************************************************/
509 /* --------------------------------------------------------------------------------------------- */
511 WListbox *
512 listbox_new (int y, int x, int height, int width, gboolean deletable, lcback_fn callback)
514 WListbox *l;
516 if (height <= 0)
517 height = 1;
519 l = g_new (WListbox, 1);
520 init_widget (&l->widget, y, x, height, width, listbox_callback, listbox_event);
522 l->list = NULL;
523 l->top = l->pos = 0;
524 l->count = 0;
525 l->deletable = deletable;
526 l->callback = callback;
527 l->allow_duplicates = TRUE;
528 l->scrollbar = !mc_global.tty.slow_terminal;
529 widget_want_hotkey (l->widget, TRUE);
530 widget_want_cursor (l->widget, FALSE);
532 return l;
535 /* --------------------------------------------------------------------------------------------- */
538 listbox_search_text (WListbox * l, const char *text)
540 if (l != NULL)
542 int i;
543 GList *le;
545 for (i = 0, le = l->list; le != NULL; i++, le = g_list_next (le))
547 WLEntry *e = (WLEntry *) le->data;
549 if (strcmp (e->text, text) == 0)
550 return i;
554 return (-1);
557 /* --------------------------------------------------------------------------------------------- */
559 /* Selects the first entry and scrolls the list to the top */
560 void
561 listbox_select_first (WListbox * l)
563 l->pos = l->top = 0;
566 /* --------------------------------------------------------------------------------------------- */
568 /* Selects the last entry and scrolls the list to the bottom */
569 void
570 listbox_select_last (WListbox * l)
572 l->pos = l->count - 1;
573 l->top = l->count > l->widget.lines ? l->count - l->widget.lines : 0;
576 /* --------------------------------------------------------------------------------------------- */
578 void
579 listbox_select_entry (WListbox * l, int dest)
581 GList *le;
582 int pos;
583 gboolean top_seen = FALSE;
585 if (dest < 0)
586 return;
588 /* Special case */
589 for (pos = 0, le = l->list; le != NULL; pos++, le = g_list_next (le))
591 if (pos == l->top)
592 top_seen = TRUE;
594 if (pos == dest)
596 l->pos = dest;
597 if (!top_seen)
598 l->top = l->pos;
599 else if (l->pos - l->top >= l->widget.lines)
600 l->top = l->pos - l->widget.lines + 1;
601 return;
605 /* If we are unable to find it, set decent values */
606 l->pos = l->top = 0;
609 /* --------------------------------------------------------------------------------------------- */
611 /* Returns the current string text as well as the associated extra data */
612 void
613 listbox_get_current (WListbox * l, char **string, void **extra)
615 WLEntry *e = NULL;
616 gboolean ok;
618 if (l != NULL)
619 e = (WLEntry *) g_list_nth_data (l->list, l->pos);
621 ok = (e != NULL);
623 if (string != NULL)
624 *string = ok ? e->text : NULL;
626 if (extra != NULL)
627 *extra = ok ? e->data : NULL;
630 /* --------------------------------------------------------------------------------------------- */
632 void
633 listbox_remove_current (WListbox * l)
635 if ((l != NULL) && (l->count != 0))
637 GList *current;
639 current = g_list_nth (l->list, l->pos);
640 l->list = g_list_remove_link (l->list, current);
641 listbox_entry_free ((WLEntry *) current->data);
642 g_list_free_1 (current);
643 l->count--;
645 if (l->count == 0)
646 l->top = l->pos = 0;
647 else if (l->pos >= l->count)
648 l->pos = l->count - 1;
652 /* --------------------------------------------------------------------------------------------- */
654 void
655 listbox_set_list (WListbox * l, GList * list)
657 listbox_remove_list (l);
659 if (l != NULL)
661 l->list = list;
662 l->top = l->pos = 0;
663 l->count = g_list_length (list);
667 /* --------------------------------------------------------------------------------------------- */
669 void
670 listbox_remove_list (WListbox * l)
672 if ((l != NULL) && (l->count != 0))
674 g_list_foreach (l->list, (GFunc) listbox_entry_free, NULL);
675 g_list_free (l->list);
676 l->list = NULL;
677 l->count = l->pos = l->top = 0;
681 /* --------------------------------------------------------------------------------------------- */
683 char *
684 listbox_add_item (WListbox * l, listbox_append_t pos, int hotkey, const char *text, void *data)
686 WLEntry *entry;
688 if (l == NULL)
689 return NULL;
691 if (!l->allow_duplicates && (listbox_search_text (l, text) >= 0))
692 return NULL;
694 entry = g_new (WLEntry, 1);
695 entry->text = g_strdup (text);
696 entry->data = data;
697 entry->hotkey = hotkey;
699 listbox_append_item (l, entry, pos);
701 return entry->text;
704 /* --------------------------------------------------------------------------------------------- */