Enable -Wunused-but-set-variable flag by default.
[midnight-commander.git] / lib / widget / listbox.c
blob1bcda01bb40a1094ed97073e0f2171dd08ee2a31
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 Widget *w = WIDGET (l);
87 int max_line = w->lines - 1;
88 int line = 0;
89 int i;
91 /* Are we at the top? */
92 widget_move (w, 0, w->cols);
93 if (l->top == 0)
94 tty_print_one_vline (TRUE);
95 else
96 tty_print_char ('^');
98 /* Are we at the bottom? */
99 widget_move (w, max_line, w->cols);
100 if ((l->top + w->lines == l->count) || (w->lines >= l->count))
101 tty_print_one_vline (TRUE);
102 else
103 tty_print_char ('v');
105 /* Now draw the nice relative pointer */
106 if (l->count != 0)
107 line = 1 + ((l->pos * (w->lines - 2)) / l->count);
109 for (i = 1; i < max_line; i++)
111 widget_move (w, i, w->cols);
112 if (i != line)
113 tty_print_one_vline (TRUE);
114 else
115 tty_print_char ('*');
119 /* --------------------------------------------------------------------------------------------- */
121 static void
122 listbox_draw (WListbox * l, gboolean focused)
124 Widget *w = WIDGET (l);
125 const WDialog *h = w->owner;
126 const gboolean disabled = (w->options & W_DISABLED) != 0;
127 const int normalc = disabled ? DISABLED_COLOR : h->color[DLG_COLOR_NORMAL];
128 /* *INDENT-OFF* */
129 int selc = disabled
130 ? DISABLED_COLOR
131 : focused
132 ? h->color[DLG_COLOR_HOT_FOCUS]
133 : h->color[DLG_COLOR_FOCUS];
134 /* *INDENT-ON* */
136 GList *le;
137 int pos;
138 int i;
139 int sel_line = -1;
141 le = g_list_nth (l->list, l->top);
142 /* pos = (le == NULL) ? 0 : g_list_position (l->list, le); */
143 pos = (le == NULL) ? 0 : l->top;
145 for (i = 0; i < w->lines; i++)
147 const char *text;
149 /* Display the entry */
150 if (pos == l->pos && sel_line == -1)
152 sel_line = i;
153 tty_setcolor (selc);
155 else
156 tty_setcolor (normalc);
158 widget_move (l, i, 1);
160 if ((i > 0 && pos >= l->count) || (l->list == NULL) || (le == NULL))
161 text = "";
162 else
164 WLEntry *e = LENTRY (le->data);
166 text = e->text;
167 le = g_list_next (le);
168 pos++;
171 tty_print_string (str_fit_to_term (text, w->cols - 2, J_LEFT_FIT));
174 l->cursor_y = sel_line;
176 if (l->scrollbar && (l->count > w->lines))
178 tty_setcolor (normalc);
179 listbox_drawscroll (l);
183 /* --------------------------------------------------------------------------------------------- */
185 static int
186 listbox_check_hotkey (WListbox * l, int key)
188 int i;
189 GList *le;
191 for (i = 0, le = l->list; le != NULL; i++, le = g_list_next (le))
193 WLEntry *e = LENTRY (le->data);
195 if (e->hotkey == key)
196 return i;
199 return (-1);
202 /* --------------------------------------------------------------------------------------------- */
204 /* Selects from base the pos element */
205 static int
206 listbox_select_pos (WListbox * l, int base, int pos)
208 int last = l->count - 1;
210 base += pos;
211 base = min (base, last);
213 return base;
216 /* --------------------------------------------------------------------------------------------- */
218 static void
219 listbox_fwd (WListbox * l)
221 if (l->pos + 1 >= l->count)
222 listbox_select_first (l);
223 else
224 listbox_select_entry (l, l->pos + 1);
227 /* --------------------------------------------------------------------------------------------- */
229 static void
230 listbox_back (WListbox * l)
232 if (l->pos <= 0)
233 listbox_select_last (l);
234 else
235 listbox_select_entry (l, l->pos - 1);
238 /* --------------------------------------------------------------------------------------------- */
240 static cb_ret_t
241 listbox_execute_cmd (WListbox * l, unsigned long command)
243 cb_ret_t ret = MSG_HANDLED;
244 int i;
245 Widget *w = WIDGET (l);
247 switch (command)
249 case CK_Up:
250 listbox_back (l);
251 break;
252 case CK_Down:
253 listbox_fwd (l);
254 break;
255 case CK_Top:
256 listbox_select_first (l);
257 break;
258 case CK_Bottom:
259 listbox_select_last (l);
260 break;
261 case CK_PageUp:
262 for (i = 0; (i < w->lines - 1) && (l->pos > 0); i++)
263 listbox_back (l);
264 break;
265 case CK_PageDown:
266 for (i = 0; (i < w->lines - 1) && (l->pos < l->count - 1); i++)
267 listbox_fwd (l);
268 break;
269 case CK_Delete:
270 if (l->deletable)
272 gboolean is_last = (l->pos + 1 >= l->count);
273 gboolean is_more = (l->top + w->lines >= l->count);
275 listbox_remove_current (l);
276 if ((l->top > 0) && (is_last || is_more))
277 l->top--;
279 break;
280 case CK_Clear:
281 if (l->deletable && mc_global.widget.confirm_history_cleanup
282 /* TRANSLATORS: no need to translate 'DialogTitle', it's just a context prefix */
283 && (query_dialog (Q_ ("DialogTitle|History cleanup"),
284 _("Do you want clean this history?"),
285 D_ERROR, 2, _("&Yes"), _("&No")) == 0))
286 listbox_remove_list (l);
287 break;
288 default:
289 ret = MSG_NOT_HANDLED;
292 return ret;
295 /* --------------------------------------------------------------------------------------------- */
297 /* Return MSG_HANDLED if we want a redraw */
298 static cb_ret_t
299 listbox_key (WListbox * l, int key)
301 unsigned long command;
303 if (l->list == NULL)
304 return MSG_NOT_HANDLED;
306 /* focus on listbox item N by '0'..'9' keys */
307 if (key >= '0' && key <= '9')
309 int oldpos = l->pos;
310 listbox_select_entry (l, key - '0');
312 /* need scroll to item? */
313 if (abs (oldpos - l->pos) > WIDGET (l)->lines)
314 l->top = l->pos;
316 return MSG_HANDLED;
319 command = keybind_lookup_keymap_command (listbox_map, key);
320 if (command == CK_IgnoreKey)
321 return MSG_NOT_HANDLED;
322 return listbox_execute_cmd (l, command);
325 /* --------------------------------------------------------------------------------------------- */
327 /* Listbox item adding function */
328 static inline void
329 listbox_append_item (WListbox * l, WLEntry * e, listbox_append_t pos)
331 switch (pos)
333 case LISTBOX_APPEND_AT_END:
334 l->list = g_list_append (l->list, e);
335 break;
337 case LISTBOX_APPEND_BEFORE:
338 l->list = g_list_insert_before (l->list, g_list_nth (l->list, l->pos), e);
339 if (l->pos > 0)
340 l->pos--;
341 break;
343 case LISTBOX_APPEND_AFTER:
344 l->list = g_list_insert (l->list, e, l->pos + 1);
345 break;
347 case LISTBOX_APPEND_SORTED:
348 l->list = g_list_insert_sorted (l->list, e, (GCompareFunc) listbox_entry_cmp);
349 break;
351 default:
352 return;
355 l->count++;
358 /* --------------------------------------------------------------------------------------------- */
360 static inline void
361 listbox_destroy (WListbox * l)
363 listbox_remove_list (l);
366 /* --------------------------------------------------------------------------------------------- */
368 static cb_ret_t
369 listbox_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
371 WListbox *l = LISTBOX (w);
372 WDialog *h = w->owner;
373 cb_ret_t ret_code;
375 switch (msg)
377 case MSG_INIT:
378 return MSG_HANDLED;
380 case MSG_HOTKEY:
382 int pos, action;
384 pos = listbox_check_hotkey (l, parm);
385 if (pos < 0)
386 return MSG_NOT_HANDLED;
388 listbox_select_entry (l, pos);
389 send_message (h, w, MSG_ACTION, l->pos, NULL);
391 if (l->callback != NULL)
392 action = l->callback (l);
393 else
394 action = LISTBOX_DONE;
396 if (action == LISTBOX_DONE)
398 h->ret_value = B_ENTER;
399 dlg_stop (h);
402 return MSG_HANDLED;
405 case MSG_KEY:
406 ret_code = listbox_key (l, parm);
407 if (ret_code != MSG_NOT_HANDLED)
409 listbox_draw (l, TRUE);
410 send_message (h, w, MSG_ACTION, l->pos, NULL);
412 return ret_code;
414 case MSG_ACTION:
415 return listbox_execute_cmd (l, parm);
417 case MSG_CURSOR:
418 widget_move (l, l->cursor_y, 0);
419 send_message (h, w, MSG_ACTION, l->pos, NULL);
420 return MSG_HANDLED;
422 case MSG_FOCUS:
423 case MSG_UNFOCUS:
424 case MSG_DRAW:
425 listbox_draw (l, msg != MSG_UNFOCUS);
426 return MSG_HANDLED;
428 case MSG_DESTROY:
429 listbox_destroy (l);
430 return MSG_HANDLED;
432 case MSG_RESIZE:
433 return MSG_HANDLED;
435 default:
436 return widget_default_callback (w, sender, msg, parm, data);
440 /* --------------------------------------------------------------------------------------------- */
442 static int
443 listbox_event (Gpm_Event * event, void *data)
445 WListbox *l = LISTBOX (data);
446 Widget *w = WIDGET (data);
448 if (!mouse_global_in_widget (event, w))
449 return MOU_UNHANDLED;
451 /* Single click */
452 if ((event->type & GPM_DOWN) != 0)
453 dlg_select_widget (l);
455 if (l->list == NULL)
456 return MOU_NORMAL;
458 if ((event->type & (GPM_DOWN | GPM_DRAG)) != 0)
460 int ret = MOU_REPEAT;
461 Gpm_Event local;
462 int i;
464 local = mouse_get_local (event, w);
465 if (local.y < 1)
466 for (i = -local.y; i >= 0; i--)
467 listbox_back (l);
468 else if (local.y > w->lines)
469 for (i = local.y - w->lines; i > 0; i--)
470 listbox_fwd (l);
471 else if ((local.buttons & GPM_B_UP) != 0)
473 listbox_back (l);
474 ret = MOU_NORMAL;
476 else if ((local.buttons & GPM_B_DOWN) != 0)
478 listbox_fwd (l);
479 ret = MOU_NORMAL;
481 else
482 listbox_select_entry (l, listbox_select_pos (l, l->top, local.y - 1));
484 /* We need to refresh ourselves since the dialog manager doesn't */
485 /* know about this event */
486 listbox_draw (l, TRUE);
487 return ret;
490 /* Double click */
491 if ((event->type & (GPM_DOUBLE | GPM_UP)) == (GPM_UP | GPM_DOUBLE))
493 Gpm_Event local;
494 int action;
496 local = mouse_get_local (event, w);
497 dlg_select_widget (l);
498 listbox_select_entry (l, listbox_select_pos (l, l->top, local.y - 1));
500 if (l->callback != NULL)
501 action = l->callback (l);
502 else
503 action = LISTBOX_DONE;
505 if (action == LISTBOX_DONE)
507 w->owner->ret_value = B_ENTER;
508 dlg_stop (w->owner);
512 return MOU_NORMAL;
515 /* --------------------------------------------------------------------------------------------- */
516 /*** public functions ****************************************************************************/
517 /* --------------------------------------------------------------------------------------------- */
519 WListbox *
520 listbox_new (int y, int x, int height, int width, gboolean deletable, lcback_fn callback)
522 WListbox *l;
523 Widget *w;
525 if (height <= 0)
526 height = 1;
528 l = g_new (WListbox, 1);
529 w = WIDGET (l);
530 init_widget (w, y, x, height, width, listbox_callback, listbox_event);
532 l->list = NULL;
533 l->top = l->pos = 0;
534 l->count = 0;
535 l->deletable = deletable;
536 l->callback = callback;
537 l->allow_duplicates = TRUE;
538 l->scrollbar = !mc_global.tty.slow_terminal;
539 widget_want_hotkey (w, TRUE);
540 widget_want_cursor (w, FALSE);
542 return l;
545 /* --------------------------------------------------------------------------------------------- */
548 listbox_search_text (WListbox * l, const char *text)
550 if (l != NULL)
552 int i;
553 GList *le;
555 for (i = 0, le = l->list; le != NULL; i++, le = g_list_next (le))
557 WLEntry *e = LENTRY (le->data);
559 if (strcmp (e->text, text) == 0)
560 return i;
564 return (-1);
567 /* --------------------------------------------------------------------------------------------- */
569 /* Selects the first entry and scrolls the list to the top */
570 void
571 listbox_select_first (WListbox * l)
573 l->pos = l->top = 0;
576 /* --------------------------------------------------------------------------------------------- */
578 /* Selects the last entry and scrolls the list to the bottom */
579 void
580 listbox_select_last (WListbox * l)
582 int lines = WIDGET (l)->lines;
584 l->pos = l->count - 1;
585 l->top = l->count > lines ? l->count - lines : 0;
588 /* --------------------------------------------------------------------------------------------- */
590 void
591 listbox_select_entry (WListbox * l, int dest)
593 GList *le;
594 int pos;
595 gboolean top_seen = FALSE;
597 if (dest < 0)
598 return;
600 /* Special case */
601 for (pos = 0, le = l->list; le != NULL; pos++, le = g_list_next (le))
603 if (pos == l->top)
604 top_seen = TRUE;
606 if (pos == dest)
608 l->pos = dest;
609 if (!top_seen)
610 l->top = l->pos;
611 else
613 int lines = WIDGET (l)->lines;
615 if (l->pos - l->top >= lines)
616 l->top = l->pos - lines + 1;
618 return;
622 /* If we are unable to find it, set decent values */
623 l->pos = l->top = 0;
626 /* --------------------------------------------------------------------------------------------- */
628 /* Returns the current string text as well as the associated extra data */
629 void
630 listbox_get_current (WListbox * l, char **string, void **extra)
632 WLEntry *e = NULL;
633 gboolean ok;
635 if (l != NULL)
636 e = LENTRY (g_list_nth_data (l->list, l->pos));
638 ok = (e != NULL);
640 if (string != NULL)
641 *string = ok ? e->text : NULL;
643 if (extra != NULL)
644 *extra = ok ? e->data : NULL;
647 /* --------------------------------------------------------------------------------------------- */
649 void
650 listbox_remove_current (WListbox * l)
652 if ((l != NULL) && (l->count != 0))
654 GList *current;
656 current = g_list_nth (l->list, l->pos);
657 l->list = g_list_remove_link (l->list, current);
658 listbox_entry_free (LENTRY (current->data));
659 g_list_free_1 (current);
660 l->count--;
662 if (l->count == 0)
663 l->top = l->pos = 0;
664 else if (l->pos >= l->count)
665 l->pos = l->count - 1;
669 /* --------------------------------------------------------------------------------------------- */
671 void
672 listbox_set_list (WListbox * l, GList * list)
674 listbox_remove_list (l);
676 if (l != NULL)
678 l->list = list;
679 l->top = l->pos = 0;
680 l->count = g_list_length (list);
684 /* --------------------------------------------------------------------------------------------- */
686 void
687 listbox_remove_list (WListbox * l)
689 if ((l != NULL) && (l->count != 0))
691 g_list_foreach (l->list, (GFunc) listbox_entry_free, NULL);
692 g_list_free (l->list);
693 l->list = NULL;
694 l->count = l->pos = l->top = 0;
698 /* --------------------------------------------------------------------------------------------- */
700 char *
701 listbox_add_item (WListbox * l, listbox_append_t pos, int hotkey, const char *text, void *data)
703 WLEntry *entry;
705 if (l == NULL)
706 return NULL;
708 if (!l->allow_duplicates && (listbox_search_text (l, text) >= 0))
709 return NULL;
711 entry = g_new (WLEntry, 1);
712 entry->text = g_strdup (text);
713 entry->data = data;
714 entry->hotkey = hotkey;
716 listbox_append_item (l, entry, pos);
718 return entry->text;
721 /* --------------------------------------------------------------------------------------------- */