listbox.c 21 KB

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