listbox.c 20 KB

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