listbox.c 21 KB

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