listbox.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
  4. 2004, 2005, 2006, 2007, 2009, 2010, 2011
  5. The Free Software Foundation, Inc.
  6. Authors:
  7. Radek Doulik, 1994, 1995
  8. Miguel de Icaza, 1994, 1995
  9. Jakub Jelinek, 1995
  10. Andrej Borsenkow, 1996
  11. Norbert Warmuth, 1997
  12. Andrew Borodin <aborodin@vmail.ru>, 2009, 2010
  13. This file is part of the Midnight Commander.
  14. The Midnight Commander is free software: you can redistribute it
  15. and/or modify it under the terms of the GNU General Public License as
  16. published by the Free Software Foundation, either version 3 of the License,
  17. or (at your option) any later version.
  18. The Midnight Commander is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. GNU General Public License for more details.
  22. You should have received a copy of the GNU General Public License
  23. along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. /** \file listbox.c
  26. * \brief Source: WListbox widget
  27. */
  28. #include <config.h>
  29. #include <stdlib.h>
  30. #include "lib/global.h"
  31. #include "lib/tty/tty.h"
  32. #include "lib/tty/mouse.h"
  33. #include "lib/skin.h"
  34. #include "lib/strutil.h"
  35. #include "lib/util.h" /* Q_() */
  36. #include "lib/keybind.h" /* global_keymap_t */
  37. #include "lib/widget.h"
  38. /*** global variables ****************************************************************************/
  39. const global_keymap_t *listbox_map = NULL;
  40. /*** file scope macro definitions ****************************************************************/
  41. /*** file scope type declarations ****************************************************************/
  42. /*** file scope variables ************************************************************************/
  43. /*** file scope functions ************************************************************************/
  44. static int
  45. listbox_entry_cmp (const void *a, const void *b)
  46. {
  47. const WLEntry *ea = (const WLEntry *) a;
  48. const WLEntry *eb = (const WLEntry *) b;
  49. return strcmp (ea->text, eb->text);
  50. }
  51. /* --------------------------------------------------------------------------------------------- */
  52. static void
  53. listbox_entry_free (void *data)
  54. {
  55. WLEntry *e = data;
  56. g_free (e->text);
  57. g_free (e);
  58. }
  59. /* --------------------------------------------------------------------------------------------- */
  60. static void
  61. listbox_drawscroll (WListbox * l)
  62. {
  63. const int max_line = l->widget.lines - 1;
  64. int line = 0;
  65. int i;
  66. /* Are we at the top? */
  67. widget_move (&l->widget, 0, l->widget.cols);
  68. if (l->top == 0)
  69. tty_print_one_vline (TRUE);
  70. else
  71. tty_print_char ('^');
  72. /* Are we at the bottom? */
  73. widget_move (&l->widget, max_line, l->widget.cols);
  74. if ((l->top + l->widget.lines == l->count) || (l->widget.lines >= l->count))
  75. tty_print_one_vline (TRUE);
  76. else
  77. tty_print_char ('v');
  78. /* Now draw the nice relative pointer */
  79. if (l->count != 0)
  80. line = 1 + ((l->pos * (l->widget.lines - 2)) / l->count);
  81. for (i = 1; i < max_line; i++)
  82. {
  83. widget_move (&l->widget, i, l->widget.cols);
  84. if (i != line)
  85. tty_print_one_vline (TRUE);
  86. else
  87. tty_print_char ('*');
  88. }
  89. }
  90. /* --------------------------------------------------------------------------------------------- */
  91. static void
  92. listbox_draw (WListbox * l, gboolean focused)
  93. {
  94. const Dlg_head *h = l->widget.owner;
  95. const gboolean disabled = (((Widget *) l)->options & W_DISABLED) != 0;
  96. const int normalc = disabled ? DISABLED_COLOR : h->color[DLG_COLOR_NORMAL];
  97. int selc =
  98. disabled ? DISABLED_COLOR : focused ? h->
  99. color[DLG_COLOR_HOT_FOCUS] : h->color[DLG_COLOR_FOCUS];
  100. GList *le;
  101. int pos;
  102. int i;
  103. int sel_line = -1;
  104. le = g_list_nth (l->list, l->top);
  105. /* pos = (le == NULL) ? 0 : g_list_position (l->list, le); */
  106. pos = (le == NULL) ? 0 : l->top;
  107. for (i = 0; i < l->widget.lines; i++)
  108. {
  109. const char *text;
  110. /* Display the entry */
  111. if (pos == l->pos && sel_line == -1)
  112. {
  113. sel_line = i;
  114. tty_setcolor (selc);
  115. }
  116. else
  117. tty_setcolor (normalc);
  118. widget_move (&l->widget, i, 1);
  119. if ((i > 0 && pos >= l->count) || (l->list == NULL) || (le == NULL))
  120. text = "";
  121. else
  122. {
  123. WLEntry *e = (WLEntry *) le->data;
  124. text = e->text;
  125. le = g_list_next (le);
  126. pos++;
  127. }
  128. tty_print_string (str_fit_to_term (text, l->widget.cols - 2, J_LEFT_FIT));
  129. }
  130. l->cursor_y = sel_line;
  131. if (l->scrollbar && (l->count > l->widget.lines))
  132. {
  133. tty_setcolor (normalc);
  134. listbox_drawscroll (l);
  135. }
  136. }
  137. /* --------------------------------------------------------------------------------------------- */
  138. static int
  139. listbox_check_hotkey (WListbox * l, int key)
  140. {
  141. int i;
  142. GList *le;
  143. for (i = 0, le = l->list; le != NULL; i++, le = g_list_next (le))
  144. {
  145. WLEntry *e = (WLEntry *) le->data;
  146. if (e->hotkey == key)
  147. return i;
  148. }
  149. return (-1);
  150. }
  151. /* --------------------------------------------------------------------------------------------- */
  152. /* Selects from base the pos element */
  153. static int
  154. listbox_select_pos (WListbox * l, int base, int pos)
  155. {
  156. int last = l->count - 1;
  157. base += pos;
  158. base = min (base, last);
  159. return base;
  160. }
  161. /* --------------------------------------------------------------------------------------------- */
  162. static void
  163. listbox_fwd (WListbox * l)
  164. {
  165. if (l->pos + 1 >= l->count)
  166. listbox_select_first (l);
  167. else
  168. listbox_select_entry (l, l->pos + 1);
  169. }
  170. /* --------------------------------------------------------------------------------------------- */
  171. static void
  172. listbox_back (WListbox * l)
  173. {
  174. if (l->pos <= 0)
  175. listbox_select_last (l);
  176. else
  177. listbox_select_entry (l, l->pos - 1);
  178. }
  179. /* --------------------------------------------------------------------------------------------- */
  180. static cb_ret_t
  181. listbox_execute_cmd (WListbox * l, unsigned long command)
  182. {
  183. cb_ret_t ret = MSG_HANDLED;
  184. int i;
  185. switch (command)
  186. {
  187. case CK_Up:
  188. listbox_back (l);
  189. break;
  190. case CK_Down:
  191. listbox_fwd (l);
  192. break;
  193. case CK_Top:
  194. listbox_select_first (l);
  195. break;
  196. case CK_Bottom:
  197. listbox_select_last (l);
  198. break;
  199. case CK_PageUp:
  200. for (i = 0; (i < l->widget.lines - 1) && (l->pos > 0); i++)
  201. listbox_back (l);
  202. break;
  203. case CK_PageDown:
  204. for (i = 0; (i < l->widget.lines - 1) && (l->pos < l->count - 1); i++)
  205. listbox_fwd (l);
  206. break;
  207. case CK_Delete:
  208. if (l->deletable)
  209. {
  210. gboolean is_last = (l->pos + 1 >= l->count);
  211. gboolean is_more = (l->top + l->widget.lines >= l->count);
  212. listbox_remove_current (l);
  213. if ((l->top > 0) && (is_last || is_more))
  214. l->top--;
  215. }
  216. break;
  217. case CK_Clear:
  218. if (l->deletable && mc_global.widget.confirm_history_cleanup
  219. /* TRANSLATORS: no need to translate 'DialogTitle', it's just a context prefix */
  220. && (query_dialog (Q_ ("DialogTitle|History cleanup"),
  221. _("Do you want clean this history?"),
  222. D_ERROR, 2, _("&Yes"), _("&No")) == 0))
  223. listbox_remove_list (l);
  224. break;
  225. default:
  226. ret = MSG_NOT_HANDLED;
  227. }
  228. return ret;
  229. }
  230. /* --------------------------------------------------------------------------------------------- */
  231. /* Return MSG_HANDLED if we want a redraw */
  232. static cb_ret_t
  233. listbox_key (WListbox * l, int key)
  234. {
  235. unsigned long command;
  236. if (l->list == NULL)
  237. return MSG_NOT_HANDLED;
  238. /* focus on listbox item N by '0'..'9' keys */
  239. if (key >= '0' && key <= '9')
  240. {
  241. int oldpos = l->pos;
  242. listbox_select_entry (l, key - '0');
  243. /* need scroll to item? */
  244. if (abs (oldpos - l->pos) > l->widget.lines)
  245. l->top = l->pos;
  246. return MSG_HANDLED;
  247. }
  248. command = keybind_lookup_keymap_command (listbox_map, key);
  249. if (command == CK_IgnoreKey)
  250. return MSG_NOT_HANDLED;
  251. return listbox_execute_cmd (l, command);
  252. }
  253. /* --------------------------------------------------------------------------------------------- */
  254. /* Listbox item adding function */
  255. static inline void
  256. listbox_append_item (WListbox * l, WLEntry * e, listbox_append_t pos)
  257. {
  258. switch (pos)
  259. {
  260. case LISTBOX_APPEND_AT_END:
  261. l->list = g_list_append (l->list, e);
  262. break;
  263. case LISTBOX_APPEND_BEFORE:
  264. l->list = g_list_insert_before (l->list, g_list_nth (l->list, l->pos), e);
  265. if (l->pos > 0)
  266. l->pos--;
  267. break;
  268. case LISTBOX_APPEND_AFTER:
  269. l->list = g_list_insert (l->list, e, l->pos + 1);
  270. break;
  271. case LISTBOX_APPEND_SORTED:
  272. l->list = g_list_insert_sorted (l->list, e, (GCompareFunc) listbox_entry_cmp);
  273. break;
  274. default:
  275. return;
  276. }
  277. l->count++;
  278. }
  279. /* --------------------------------------------------------------------------------------------- */
  280. static inline void
  281. listbox_destroy (WListbox * l)
  282. {
  283. listbox_remove_list (l);
  284. }
  285. /* --------------------------------------------------------------------------------------------- */
  286. static cb_ret_t
  287. listbox_callback (Widget * w, widget_msg_t msg, int parm)
  288. {
  289. WListbox *l = (WListbox *) w;
  290. Dlg_head *h = l->widget.owner;
  291. cb_ret_t ret_code;
  292. switch (msg)
  293. {
  294. case WIDGET_INIT:
  295. return MSG_HANDLED;
  296. case WIDGET_HOTKEY:
  297. {
  298. int pos, action;
  299. pos = listbox_check_hotkey (l, parm);
  300. if (pos < 0)
  301. return MSG_NOT_HANDLED;
  302. listbox_select_entry (l, pos);
  303. h->callback (h, w, DLG_ACTION, l->pos, NULL);
  304. if (l->callback != NULL)
  305. action = l->callback (l);
  306. else
  307. action = LISTBOX_DONE;
  308. if (action == LISTBOX_DONE)
  309. {
  310. h->ret_value = B_ENTER;
  311. dlg_stop (h);
  312. }
  313. return MSG_HANDLED;
  314. }
  315. case WIDGET_KEY:
  316. ret_code = listbox_key (l, parm);
  317. if (ret_code != MSG_NOT_HANDLED)
  318. {
  319. listbox_draw (l, TRUE);
  320. h->callback (h, w, DLG_ACTION, l->pos, NULL);
  321. }
  322. return ret_code;
  323. case WIDGET_COMMAND:
  324. return listbox_execute_cmd (l, parm);
  325. case WIDGET_CURSOR:
  326. widget_move (&l->widget, l->cursor_y, 0);
  327. h->callback (h, w, DLG_ACTION, l->pos, NULL);
  328. return MSG_HANDLED;
  329. case WIDGET_FOCUS:
  330. case WIDGET_UNFOCUS:
  331. case WIDGET_DRAW:
  332. listbox_draw (l, msg != WIDGET_UNFOCUS);
  333. return MSG_HANDLED;
  334. case WIDGET_DESTROY:
  335. listbox_destroy (l);
  336. return MSG_HANDLED;
  337. case WIDGET_RESIZED:
  338. return MSG_HANDLED;
  339. default:
  340. return default_proc (msg, parm);
  341. }
  342. }
  343. /* --------------------------------------------------------------------------------------------- */
  344. static int
  345. listbox_event (Gpm_Event * event, void *data)
  346. {
  347. WListbox *l = (WListbox *) data;
  348. Widget *w = (Widget *) data;
  349. if (!mouse_global_in_widget (event, w))
  350. return MOU_UNHANDLED;
  351. /* Single click */
  352. if ((event->type & GPM_DOWN) != 0)
  353. dlg_select_widget (l);
  354. if (l->list == NULL)
  355. return MOU_NORMAL;
  356. if ((event->type & (GPM_DOWN | GPM_DRAG)) != 0)
  357. {
  358. int ret = MOU_REPEAT;
  359. Gpm_Event local;
  360. int i;
  361. local = mouse_get_local (event, w);
  362. if (local.y < 1)
  363. for (i = -local.y; i >= 0; i--)
  364. listbox_back (l);
  365. else if (local.y > w->lines)
  366. for (i = local.y - w->lines; i > 0; i--)
  367. listbox_fwd (l);
  368. else if ((local.buttons & GPM_B_UP) != 0)
  369. {
  370. listbox_back (l);
  371. ret = MOU_NORMAL;
  372. }
  373. else if ((local.buttons & GPM_B_DOWN) != 0)
  374. {
  375. listbox_fwd (l);
  376. ret = MOU_NORMAL;
  377. }
  378. else
  379. listbox_select_entry (l, listbox_select_pos (l, l->top, local.y - 1));
  380. /* We need to refresh ourselves since the dialog manager doesn't */
  381. /* know about this event */
  382. listbox_draw (l, TRUE);
  383. return ret;
  384. }
  385. /* Double click */
  386. if ((event->type & (GPM_DOUBLE | GPM_UP)) == (GPM_UP | GPM_DOUBLE))
  387. {
  388. Gpm_Event local;
  389. int action;
  390. local = mouse_get_local (event, w);
  391. dlg_select_widget (l);
  392. listbox_select_entry (l, listbox_select_pos (l, l->top, local.y - 1));
  393. if (l->callback != NULL)
  394. action = l->callback (l);
  395. else
  396. action = LISTBOX_DONE;
  397. if (action == LISTBOX_DONE)
  398. {
  399. w->owner->ret_value = B_ENTER;
  400. dlg_stop (w->owner);
  401. }
  402. }
  403. return MOU_NORMAL;
  404. }
  405. /* --------------------------------------------------------------------------------------------- */
  406. /*** public functions ****************************************************************************/
  407. /* --------------------------------------------------------------------------------------------- */
  408. WListbox *
  409. listbox_new (int y, int x, int height, int width, gboolean deletable, lcback_fn callback)
  410. {
  411. WListbox *l;
  412. if (height <= 0)
  413. height = 1;
  414. l = g_new (WListbox, 1);
  415. init_widget (&l->widget, y, x, height, width, listbox_callback, listbox_event);
  416. l->list = NULL;
  417. l->top = l->pos = 0;
  418. l->count = 0;
  419. l->deletable = deletable;
  420. l->callback = callback;
  421. l->allow_duplicates = TRUE;
  422. l->scrollbar = !mc_global.tty.slow_terminal;
  423. widget_want_hotkey (l->widget, TRUE);
  424. widget_want_cursor (l->widget, FALSE);
  425. return l;
  426. }
  427. /* --------------------------------------------------------------------------------------------- */
  428. int
  429. listbox_search_text (WListbox * l, const char *text)
  430. {
  431. if (l != NULL)
  432. {
  433. int i;
  434. GList *le;
  435. for (i = 0, le = l->list; le != NULL; i++, le = g_list_next (le))
  436. {
  437. WLEntry *e = (WLEntry *) le->data;
  438. if (strcmp (e->text, text) == 0)
  439. return i;
  440. }
  441. }
  442. return (-1);
  443. }
  444. /* --------------------------------------------------------------------------------------------- */
  445. /* Selects the first entry and scrolls the list to the top */
  446. void
  447. listbox_select_first (WListbox * l)
  448. {
  449. l->pos = l->top = 0;
  450. }
  451. /* --------------------------------------------------------------------------------------------- */
  452. /* Selects the last entry and scrolls the list to the bottom */
  453. void
  454. listbox_select_last (WListbox * l)
  455. {
  456. l->pos = l->count - 1;
  457. l->top = l->count > l->widget.lines ? l->count - l->widget.lines : 0;
  458. }
  459. /* --------------------------------------------------------------------------------------------- */
  460. void
  461. listbox_select_entry (WListbox * l, int dest)
  462. {
  463. GList *le;
  464. int pos;
  465. gboolean top_seen = FALSE;
  466. if (dest < 0)
  467. return;
  468. /* Special case */
  469. for (pos = 0, le = l->list; le != NULL; pos++, le = g_list_next (le))
  470. {
  471. if (pos == l->top)
  472. top_seen = TRUE;
  473. if (pos == dest)
  474. {
  475. l->pos = dest;
  476. if (!top_seen)
  477. l->top = l->pos;
  478. else if (l->pos - l->top >= l->widget.lines)
  479. l->top = l->pos - l->widget.lines + 1;
  480. return;
  481. }
  482. }
  483. /* If we are unable to find it, set decent values */
  484. l->pos = l->top = 0;
  485. }
  486. /* --------------------------------------------------------------------------------------------- */
  487. /* Returns the current string text as well as the associated extra data */
  488. void
  489. listbox_get_current (WListbox * l, char **string, void **extra)
  490. {
  491. WLEntry *e = NULL;
  492. gboolean ok;
  493. if (l != NULL)
  494. e = (WLEntry *) g_list_nth_data (l->list, l->pos);
  495. ok = (e != NULL);
  496. if (string != NULL)
  497. *string = ok ? e->text : NULL;
  498. if (extra != NULL)
  499. *extra = ok ? e->data : NULL;
  500. }
  501. /* --------------------------------------------------------------------------------------------- */
  502. void
  503. listbox_remove_current (WListbox * l)
  504. {
  505. if ((l != NULL) && (l->count != 0))
  506. {
  507. GList *current;
  508. current = g_list_nth (l->list, l->pos);
  509. l->list = g_list_remove_link (l->list, current);
  510. listbox_entry_free ((WLEntry *) current->data);
  511. g_list_free_1 (current);
  512. l->count--;
  513. if (l->count == 0)
  514. l->top = l->pos = 0;
  515. else if (l->pos >= l->count)
  516. l->pos = l->count - 1;
  517. }
  518. }
  519. /* --------------------------------------------------------------------------------------------- */
  520. void
  521. listbox_set_list (WListbox * l, GList * list)
  522. {
  523. listbox_remove_list (l);
  524. if (l != NULL)
  525. {
  526. l->list = list;
  527. l->top = l->pos = 0;
  528. l->count = g_list_length (list);
  529. }
  530. }
  531. /* --------------------------------------------------------------------------------------------- */
  532. void
  533. listbox_remove_list (WListbox * l)
  534. {
  535. if ((l != NULL) && (l->count != 0))
  536. {
  537. g_list_foreach (l->list, (GFunc) listbox_entry_free, NULL);
  538. g_list_free (l->list);
  539. l->list = NULL;
  540. l->count = l->pos = l->top = 0;
  541. }
  542. }
  543. /* --------------------------------------------------------------------------------------------- */
  544. char *
  545. listbox_add_item (WListbox * l, listbox_append_t pos, int hotkey, const char *text, void *data)
  546. {
  547. WLEntry *entry;
  548. if (l == NULL)
  549. return NULL;
  550. if (!l->allow_duplicates && (listbox_search_text (l, text) >= 0))
  551. return NULL;
  552. entry = g_new (WLEntry, 1);
  553. entry->text = g_strdup (text);
  554. entry->data = data;
  555. entry->hotkey = hotkey;
  556. listbox_append_item (l, entry, pos);
  557. return entry->text;
  558. }
  559. /* --------------------------------------------------------------------------------------------- */