listbox.c 22 KB

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