listbox.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 1994-2021
  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, 2016
  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/skin.h"
  32. #include "lib/strutil.h"
  33. #include "lib/util.h" /* Q_() */
  34. #include "lib/widget.h"
  35. /*** global variables ****************************************************************************/
  36. const global_keymap_t *listbox_map = NULL;
  37. /*** file scope macro definitions ****************************************************************/
  38. /* Gives the position of the last item. */
  39. #define LISTBOX_LAST(l) (listbox_is_empty (l) ? 0 : (int) g_queue_get_length ((l)->list) - 1)
  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. if (e->free_data)
  58. g_free (e->data);
  59. g_free (e);
  60. }
  61. /* --------------------------------------------------------------------------------------------- */
  62. static void
  63. listbox_drawscroll (WListbox * l)
  64. {
  65. Widget *w = WIDGET (l);
  66. int max_line = w->lines - 1;
  67. int line = 0;
  68. int i;
  69. int length;
  70. /* Are we at the top? */
  71. widget_gotoyx (w, 0, w->cols);
  72. if (l->top == 0)
  73. tty_print_one_vline (TRUE);
  74. else
  75. tty_print_char ('^');
  76. length = g_queue_get_length (l->list);
  77. /* Are we at the bottom? */
  78. widget_gotoyx (w, max_line, w->cols);
  79. if (l->top + w->lines == length || w->lines >= length)
  80. tty_print_one_vline (TRUE);
  81. else
  82. tty_print_char ('v');
  83. /* Now draw the nice relative pointer */
  84. if (!g_queue_is_empty (l->list))
  85. line = 1 + ((l->pos * (w->lines - 2)) / length);
  86. for (i = 1; i < max_line; i++)
  87. {
  88. widget_gotoyx (w, i, w->cols);
  89. if (i != line)
  90. tty_print_one_vline (TRUE);
  91. else
  92. tty_print_char ('*');
  93. }
  94. }
  95. /* --------------------------------------------------------------------------------------------- */
  96. static void
  97. listbox_draw (WListbox * l, gboolean focused)
  98. {
  99. Widget *w = WIDGET (l);
  100. const int *colors;
  101. gboolean disabled;
  102. int normalc, selc;
  103. int length = 0;
  104. GList *le = NULL;
  105. int pos;
  106. int i;
  107. int sel_line = -1;
  108. colors = widget_get_colors (w);
  109. disabled = widget_get_state (w, WST_DISABLED);
  110. normalc = disabled ? DISABLED_COLOR : colors[DLG_COLOR_NORMAL];
  111. selc = disabled ? DISABLED_COLOR : colors[focused ? DLG_COLOR_HOT_FOCUS : DLG_COLOR_FOCUS];
  112. if (l->list != NULL)
  113. {
  114. length = g_queue_get_length (l->list);
  115. le = g_queue_peek_nth_link (l->list, (guint) l->top);
  116. }
  117. /* pos = (le == NULL) ? 0 : g_list_position (l->list, le); */
  118. pos = (le == NULL) ? 0 : l->top;
  119. for (i = 0; i < w->lines; i++)
  120. {
  121. const char *text = "";
  122. /* Display the entry */
  123. if (pos == l->pos && sel_line == -1)
  124. {
  125. sel_line = i;
  126. tty_setcolor (selc);
  127. }
  128. else
  129. tty_setcolor (normalc);
  130. widget_gotoyx (l, i, 1);
  131. if (l->list != NULL && le != NULL && (i == 0 || pos < length))
  132. {
  133. WLEntry *e = LENTRY (le->data);
  134. text = e->text;
  135. le = g_list_next (le);
  136. pos++;
  137. }
  138. tty_print_string (str_fit_to_term (text, w->cols - 2, J_LEFT_FIT));
  139. }
  140. l->cursor_y = sel_line;
  141. if (l->scrollbar && length > w->lines)
  142. {
  143. tty_setcolor (normalc);
  144. listbox_drawscroll (l);
  145. }
  146. }
  147. /* --------------------------------------------------------------------------------------------- */
  148. static int
  149. listbox_check_hotkey (WListbox * l, int key)
  150. {
  151. if (!listbox_is_empty (l))
  152. {
  153. int i;
  154. GList *le;
  155. for (i = 0, le = g_queue_peek_head_link (l->list); le != NULL; i++, le = g_list_next (le))
  156. {
  157. WLEntry *e = LENTRY (le->data);
  158. if (e->hotkey == key)
  159. return i;
  160. }
  161. }
  162. return (-1);
  163. }
  164. /* --------------------------------------------------------------------------------------------- */
  165. /* Calculates the item displayed at screen row 'y' (y==0 being the widget's 1st row). */
  166. static int
  167. listbox_y_pos (WListbox * l, int y)
  168. {
  169. return MIN (l->top + y, LISTBOX_LAST (l));
  170. }
  171. /* --------------------------------------------------------------------------------------------- */
  172. static void
  173. listbox_fwd (WListbox * l, gboolean wrap)
  174. {
  175. if (!listbox_is_empty (l))
  176. {
  177. if ((guint) l->pos + 1 < g_queue_get_length (l->list))
  178. listbox_select_entry (l, l->pos + 1);
  179. else if (wrap)
  180. listbox_select_first (l);
  181. }
  182. }
  183. /* --------------------------------------------------------------------------------------------- */
  184. static void
  185. listbox_fwd_n (WListbox * l, int n)
  186. {
  187. listbox_select_entry (l, MIN (l->pos + n, LISTBOX_LAST (l)));
  188. }
  189. /* --------------------------------------------------------------------------------------------- */
  190. static void
  191. listbox_back (WListbox * l, gboolean wrap)
  192. {
  193. if (!listbox_is_empty (l))
  194. {
  195. if (l->pos > 0)
  196. listbox_select_entry (l, l->pos - 1);
  197. else if (wrap)
  198. listbox_select_last (l);
  199. }
  200. }
  201. /* --------------------------------------------------------------------------------------------- */
  202. static void
  203. listbox_back_n (WListbox * l, int n)
  204. {
  205. listbox_select_entry (l, MAX (l->pos - n, 0));
  206. }
  207. /* --------------------------------------------------------------------------------------------- */
  208. static cb_ret_t
  209. listbox_execute_cmd (WListbox * l, long command)
  210. {
  211. cb_ret_t ret = MSG_HANDLED;
  212. Widget *w = WIDGET (l);
  213. if (l->list == NULL || g_queue_is_empty (l->list))
  214. return MSG_NOT_HANDLED;
  215. switch (command)
  216. {
  217. case CK_Up:
  218. listbox_back (l, TRUE);
  219. break;
  220. case CK_Down:
  221. listbox_fwd (l, TRUE);
  222. break;
  223. case CK_Top:
  224. listbox_select_first (l);
  225. break;
  226. case CK_Bottom:
  227. listbox_select_last (l);
  228. break;
  229. case CK_PageUp:
  230. listbox_back_n (l, w->lines - 1);
  231. break;
  232. case CK_PageDown:
  233. listbox_fwd_n (l, w->lines - 1);
  234. break;
  235. case CK_Delete:
  236. if (l->deletable)
  237. {
  238. gboolean is_last, is_more;
  239. int length;
  240. length = g_queue_get_length (l->list);
  241. is_last = (l->pos + 1 >= length);
  242. is_more = (l->top + w->lines >= length);
  243. listbox_remove_current (l);
  244. if ((l->top > 0) && (is_last || is_more))
  245. l->top--;
  246. }
  247. break;
  248. case CK_Clear:
  249. if (l->deletable && mc_global.widget.confirm_history_cleanup
  250. /* TRANSLATORS: no need to translate 'DialogTitle', it's just a context prefix */
  251. && (query_dialog (Q_ ("DialogTitle|History cleanup"),
  252. _("Do you want clean this history?"),
  253. D_ERROR, 2, _("&Yes"), _("&No")) == 0))
  254. listbox_remove_list (l);
  255. break;
  256. case CK_View:
  257. case CK_Edit:
  258. case CK_Enter:
  259. ret = send_message (WIDGET (l)->owner, l, MSG_NOTIFY, command, NULL);
  260. break;
  261. default:
  262. ret = MSG_NOT_HANDLED;
  263. }
  264. return ret;
  265. }
  266. /* --------------------------------------------------------------------------------------------- */
  267. /* Return MSG_HANDLED if we want a redraw */
  268. static cb_ret_t
  269. listbox_key (WListbox * l, int key)
  270. {
  271. long command;
  272. if (l->list == NULL)
  273. return MSG_NOT_HANDLED;
  274. /* focus on listbox item N by '0'..'9' keys */
  275. if (key >= '0' && key <= '9')
  276. {
  277. listbox_select_entry (l, key - '0');
  278. return MSG_HANDLED;
  279. }
  280. command = widget_lookup_key (WIDGET (l), key);
  281. if (command == CK_IgnoreKey)
  282. return MSG_NOT_HANDLED;
  283. return listbox_execute_cmd (l, command);
  284. }
  285. /* --------------------------------------------------------------------------------------------- */
  286. /* Listbox item adding function */
  287. static inline void
  288. listbox_append_item (WListbox * l, WLEntry * e, listbox_append_t pos)
  289. {
  290. if (l->list == NULL)
  291. {
  292. l->list = g_queue_new ();
  293. pos = LISTBOX_APPEND_AT_END;
  294. }
  295. switch (pos)
  296. {
  297. case LISTBOX_APPEND_AT_END:
  298. g_queue_push_tail (l->list, e);
  299. break;
  300. case LISTBOX_APPEND_BEFORE:
  301. g_queue_insert_before (l->list, g_queue_peek_nth_link (l->list, (guint) l->pos), e);
  302. break;
  303. case LISTBOX_APPEND_AFTER:
  304. g_queue_insert_after (l->list, g_queue_peek_nth_link (l->list, (guint) l->pos), e);
  305. break;
  306. case LISTBOX_APPEND_SORTED:
  307. g_queue_insert_sorted (l->list, e, (GCompareDataFunc) listbox_entry_cmp, NULL);
  308. break;
  309. default:
  310. break;
  311. }
  312. }
  313. /* --------------------------------------------------------------------------------------------- */
  314. /* Call this whenever the user changes the selected item. */
  315. static void
  316. listbox_on_change (WListbox * l)
  317. {
  318. listbox_draw (l, TRUE);
  319. send_message (WIDGET (l)->owner, l, MSG_NOTIFY, 0, NULL);
  320. }
  321. /* --------------------------------------------------------------------------------------------- */
  322. static void
  323. listbox_do_action (WListbox * l)
  324. {
  325. int action;
  326. if (listbox_is_empty (l))
  327. return;
  328. if (l->callback != NULL)
  329. action = l->callback (l);
  330. else
  331. action = LISTBOX_DONE;
  332. if (action == LISTBOX_DONE)
  333. {
  334. WDialog *h = DIALOG (WIDGET (l)->owner);
  335. h->ret_value = B_ENTER;
  336. dlg_stop (h);
  337. }
  338. }
  339. /* --------------------------------------------------------------------------------------------- */
  340. static void
  341. listbox_run_hotkey (WListbox * l, int pos)
  342. {
  343. listbox_select_entry (l, pos);
  344. listbox_on_change (l);
  345. listbox_do_action (l);
  346. }
  347. /* --------------------------------------------------------------------------------------------- */
  348. static inline void
  349. listbox_destroy (WListbox * l)
  350. {
  351. listbox_remove_list (l);
  352. }
  353. /* --------------------------------------------------------------------------------------------- */
  354. static cb_ret_t
  355. listbox_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
  356. {
  357. WListbox *l = LISTBOX (w);
  358. switch (msg)
  359. {
  360. case MSG_HOTKEY:
  361. {
  362. int pos;
  363. pos = listbox_check_hotkey (l, parm);
  364. if (pos < 0)
  365. return MSG_NOT_HANDLED;
  366. listbox_run_hotkey (l, pos);
  367. return MSG_HANDLED;
  368. }
  369. case MSG_KEY:
  370. {
  371. cb_ret_t ret_code;
  372. ret_code = listbox_key (l, parm);
  373. if (ret_code != MSG_NOT_HANDLED)
  374. listbox_on_change (l);
  375. return ret_code;
  376. }
  377. case MSG_ACTION:
  378. return listbox_execute_cmd (l, parm);
  379. case MSG_CURSOR:
  380. widget_gotoyx (l, l->cursor_y, 0);
  381. return MSG_HANDLED;
  382. case MSG_DRAW:
  383. listbox_draw (l, widget_get_state (w, WST_FOCUSED));
  384. return MSG_HANDLED;
  385. case MSG_DESTROY:
  386. listbox_destroy (l);
  387. return MSG_HANDLED;
  388. default:
  389. return widget_default_callback (w, sender, msg, parm, data);
  390. }
  391. }
  392. /* --------------------------------------------------------------------------------------------- */
  393. static void
  394. listbox_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event)
  395. {
  396. WListbox *l = LISTBOX (w);
  397. int old_pos;
  398. old_pos = l->pos;
  399. switch (msg)
  400. {
  401. case MSG_MOUSE_DOWN:
  402. widget_select (w);
  403. listbox_select_entry (l, listbox_y_pos (l, event->y));
  404. break;
  405. case MSG_MOUSE_SCROLL_UP:
  406. listbox_back (l, FALSE);
  407. break;
  408. case MSG_MOUSE_SCROLL_DOWN:
  409. listbox_fwd (l, FALSE);
  410. break;
  411. case MSG_MOUSE_DRAG:
  412. event->result.repeat = TRUE; /* It'd be functional even without this. */
  413. listbox_select_entry (l, listbox_y_pos (l, event->y));
  414. break;
  415. case MSG_MOUSE_CLICK:
  416. /* We don't call listbox_select_entry() here: MSG_MOUSE_DOWN/DRAG did this already. */
  417. if (event->count == GPM_DOUBLE) /* Double click */
  418. listbox_do_action (l);
  419. break;
  420. default:
  421. break;
  422. }
  423. /* If the selection has changed, we redraw the widget and notify the dialog. */
  424. if (l->pos != old_pos)
  425. listbox_on_change (l);
  426. }
  427. /* --------------------------------------------------------------------------------------------- */
  428. /*** public functions ****************************************************************************/
  429. /* --------------------------------------------------------------------------------------------- */
  430. WListbox *
  431. listbox_new (int y, int x, int height, int width, gboolean deletable, lcback_fn callback)
  432. {
  433. WListbox *l;
  434. Widget *w;
  435. if (height <= 0)
  436. height = 1;
  437. l = g_new (WListbox, 1);
  438. w = WIDGET (l);
  439. widget_init (w, y, x, height, width, listbox_callback, listbox_mouse_callback);
  440. w->options |= WOP_SELECTABLE | WOP_WANT_HOTKEY;
  441. w->keymap = listbox_map;
  442. l->list = NULL;
  443. l->top = l->pos = 0;
  444. l->deletable = deletable;
  445. l->callback = callback;
  446. l->allow_duplicates = TRUE;
  447. l->scrollbar = !mc_global.tty.slow_terminal;
  448. return l;
  449. }
  450. /* --------------------------------------------------------------------------------------------- */
  451. /**
  452. * Finds item by its label.
  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. /**
  472. * Finds item by its 'data' slot.
  473. */
  474. int
  475. listbox_search_data (WListbox * l, const void *data)
  476. {
  477. if (!listbox_is_empty (l))
  478. {
  479. int i;
  480. GList *le;
  481. for (i = 0, le = g_queue_peek_head_link (l->list); le != NULL; i++, le = g_list_next (le))
  482. {
  483. WLEntry *e = LENTRY (le->data);
  484. if (e->data == data)
  485. return i;
  486. }
  487. }
  488. return (-1);
  489. }
  490. /* --------------------------------------------------------------------------------------------- */
  491. /* Selects the first entry and scrolls the list to the top */
  492. void
  493. listbox_select_first (WListbox * l)
  494. {
  495. l->pos = l->top = 0;
  496. }
  497. /* --------------------------------------------------------------------------------------------- */
  498. /* Selects the last entry and scrolls the list to the bottom */
  499. void
  500. listbox_select_last (WListbox * l)
  501. {
  502. int lines = WIDGET (l)->lines;
  503. int length;
  504. length = listbox_get_length (l);
  505. l->pos = DOZ (length, 1);
  506. l->top = DOZ (length, lines);
  507. }
  508. /* --------------------------------------------------------------------------------------------- */
  509. void
  510. listbox_select_entry (WListbox * l, int dest)
  511. {
  512. GList *le;
  513. int pos;
  514. gboolean top_seen = FALSE;
  515. if (listbox_is_empty (l) || dest < 0)
  516. return;
  517. /* Special case */
  518. for (pos = 0, le = g_queue_peek_head_link (l->list); le != NULL; pos++, le = g_list_next (le))
  519. {
  520. if (pos == l->top)
  521. top_seen = TRUE;
  522. if (pos == dest)
  523. {
  524. l->pos = dest;
  525. if (!top_seen)
  526. l->top = l->pos;
  527. else
  528. {
  529. int lines = WIDGET (l)->lines;
  530. if (l->pos - l->top >= lines)
  531. l->top = l->pos - lines + 1;
  532. }
  533. return;
  534. }
  535. }
  536. /* If we are unable to find it, set decent values */
  537. l->pos = l->top = 0;
  538. }
  539. /* --------------------------------------------------------------------------------------------- */
  540. int
  541. listbox_get_length (const WListbox * l)
  542. {
  543. return listbox_is_empty (l) ? 0 : (int) g_queue_get_length (l->list);
  544. }
  545. /* --------------------------------------------------------------------------------------------- */
  546. /* Returns the current string text as well as the associated extra data */
  547. void
  548. listbox_get_current (WListbox * l, char **string, void **extra)
  549. {
  550. WLEntry *e = NULL;
  551. gboolean ok;
  552. if (l != NULL)
  553. e = listbox_get_nth_item (l, l->pos);
  554. ok = (e != NULL);
  555. if (string != NULL)
  556. *string = ok ? e->text : NULL;
  557. if (extra != NULL)
  558. *extra = ok ? e->data : NULL;
  559. }
  560. /* --------------------------------------------------------------------------------------------- */
  561. WLEntry *
  562. listbox_get_nth_item (const WListbox * l, int pos)
  563. {
  564. if (!listbox_is_empty (l) && pos >= 0)
  565. {
  566. GList *item;
  567. item = g_queue_peek_nth_link (l->list, (guint) pos);
  568. if (item != NULL)
  569. return LENTRY (item->data);
  570. }
  571. return NULL;
  572. }
  573. /* --------------------------------------------------------------------------------------------- */
  574. GList *
  575. listbox_get_first_link (const WListbox * l)
  576. {
  577. return (l == NULL || l->list == NULL) ? NULL : g_queue_peek_head_link (l->list);
  578. }
  579. /* --------------------------------------------------------------------------------------------- */
  580. void
  581. listbox_remove_current (WListbox * l)
  582. {
  583. if (!listbox_is_empty (l))
  584. {
  585. GList *current;
  586. int length;
  587. current = g_queue_peek_nth_link (l->list, (guint) l->pos);
  588. listbox_entry_free (current->data);
  589. g_queue_delete_link (l->list, current);
  590. length = g_queue_get_length (l->list);
  591. if (length == 0)
  592. l->top = l->pos = 0;
  593. else if (l->pos >= length)
  594. l->pos = length - 1;
  595. }
  596. }
  597. /* --------------------------------------------------------------------------------------------- */
  598. gboolean
  599. listbox_is_empty (const WListbox * l)
  600. {
  601. return (l == NULL || l->list == NULL || g_queue_is_empty (l->list));
  602. }
  603. /* --------------------------------------------------------------------------------------------- */
  604. /**
  605. * Set new listbox items list.
  606. *
  607. * @param l WListbox object
  608. * @param list list of WLEntry objects
  609. */
  610. void
  611. listbox_set_list (WListbox * l, GQueue * list)
  612. {
  613. listbox_remove_list (l);
  614. if (l != NULL)
  615. l->list = list;
  616. }
  617. /* --------------------------------------------------------------------------------------------- */
  618. void
  619. listbox_remove_list (WListbox * l)
  620. {
  621. if (l != NULL)
  622. {
  623. if (l->list != NULL)
  624. {
  625. g_queue_free_full (l->list, (GDestroyNotify) listbox_entry_free);
  626. l->list = NULL;
  627. }
  628. l->pos = l->top = 0;
  629. }
  630. }
  631. /* --------------------------------------------------------------------------------------------- */
  632. char *
  633. listbox_add_item (WListbox * l, listbox_append_t pos, int hotkey, const char *text, void *data,
  634. gboolean free_data)
  635. {
  636. WLEntry *entry;
  637. if (l == NULL)
  638. return NULL;
  639. if (!l->allow_duplicates && (listbox_search_text (l, text) >= 0))
  640. return NULL;
  641. entry = g_new (WLEntry, 1);
  642. entry->text = g_strdup (text);
  643. entry->data = data;
  644. entry->free_data = free_data;
  645. entry->hotkey = hotkey;
  646. listbox_append_item (l, entry, pos);
  647. return entry->text;
  648. }
  649. /* --------------------------------------------------------------------------------------------- */