listbox.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 1994-2017
  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. default:
  263. ret = MSG_NOT_HANDLED;
  264. }
  265. return ret;
  266. }
  267. /* --------------------------------------------------------------------------------------------- */
  268. /* Return MSG_HANDLED if we want a redraw */
  269. static cb_ret_t
  270. listbox_key (WListbox * l, int key)
  271. {
  272. long command;
  273. if (l->list == NULL)
  274. return MSG_NOT_HANDLED;
  275. /* focus on listbox item N by '0'..'9' keys */
  276. if (key >= '0' && key <= '9')
  277. {
  278. listbox_select_entry (l, key - '0');
  279. return MSG_HANDLED;
  280. }
  281. command = keybind_lookup_keymap_command (listbox_map, key);
  282. if (command == CK_IgnoreKey)
  283. return MSG_NOT_HANDLED;
  284. return listbox_execute_cmd (l, command);
  285. }
  286. /* --------------------------------------------------------------------------------------------- */
  287. /* Listbox item adding function */
  288. static inline void
  289. listbox_append_item (WListbox * l, WLEntry * e, listbox_append_t pos)
  290. {
  291. if (l->list == NULL)
  292. {
  293. l->list = g_queue_new ();
  294. pos = LISTBOX_APPEND_AT_END;
  295. }
  296. switch (pos)
  297. {
  298. case LISTBOX_APPEND_AT_END:
  299. g_queue_push_tail (l->list, e);
  300. break;
  301. case LISTBOX_APPEND_BEFORE:
  302. g_queue_insert_before (l->list, g_queue_peek_nth_link (l->list, (guint) l->pos), e);
  303. break;
  304. case LISTBOX_APPEND_AFTER:
  305. g_queue_insert_after (l->list, g_queue_peek_nth_link (l->list, (guint) l->pos), e);
  306. break;
  307. case LISTBOX_APPEND_SORTED:
  308. g_queue_insert_sorted (l->list, e, (GCompareDataFunc) listbox_entry_cmp, NULL);
  309. break;
  310. default:
  311. break;
  312. }
  313. }
  314. /* --------------------------------------------------------------------------------------------- */
  315. /* Call this whenever the user changes the selected item. */
  316. static void
  317. listbox_on_change (WListbox * l)
  318. {
  319. listbox_draw (l, TRUE);
  320. send_message (WIDGET (l)->owner, l, MSG_NOTIFY, l->pos, NULL);
  321. }
  322. /* --------------------------------------------------------------------------------------------- */
  323. static void
  324. listbox_do_action (WListbox * l)
  325. {
  326. int action;
  327. if (listbox_is_empty (l))
  328. return;
  329. if (l->callback != NULL)
  330. action = l->callback (l);
  331. else
  332. action = LISTBOX_DONE;
  333. if (action == LISTBOX_DONE)
  334. {
  335. WDialog *h = WIDGET (l)->owner;
  336. h->ret_value = B_ENTER;
  337. dlg_stop (h);
  338. }
  339. }
  340. /* --------------------------------------------------------------------------------------------- */
  341. static void
  342. listbox_run_hotkey (WListbox * l, int pos)
  343. {
  344. listbox_select_entry (l, pos);
  345. listbox_on_change (l);
  346. listbox_do_action (l);
  347. }
  348. /* --------------------------------------------------------------------------------------------- */
  349. static inline void
  350. listbox_destroy (WListbox * l)
  351. {
  352. listbox_remove_list (l);
  353. }
  354. /* --------------------------------------------------------------------------------------------- */
  355. static cb_ret_t
  356. listbox_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
  357. {
  358. WListbox *l = LISTBOX (w);
  359. cb_ret_t ret_code;
  360. switch (msg)
  361. {
  362. case MSG_HOTKEY:
  363. {
  364. int pos;
  365. pos = listbox_check_hotkey (l, parm);
  366. if (pos < 0)
  367. return MSG_NOT_HANDLED;
  368. listbox_run_hotkey (l, pos);
  369. return MSG_HANDLED;
  370. }
  371. case MSG_KEY:
  372. ret_code = listbox_key (l, parm);
  373. if (ret_code != MSG_NOT_HANDLED)
  374. listbox_on_change (l);
  375. return ret_code;
  376. case MSG_ACTION:
  377. return listbox_execute_cmd (l, parm);
  378. case MSG_CURSOR:
  379. widget_move (l, l->cursor_y, 0);
  380. return MSG_HANDLED;
  381. case MSG_DRAW:
  382. listbox_draw (l, widget_get_state (w, WST_FOCUSED));
  383. return MSG_HANDLED;
  384. case MSG_DESTROY:
  385. listbox_destroy (l);
  386. return MSG_HANDLED;
  387. case MSG_RESIZE:
  388. return MSG_HANDLED;
  389. default:
  390. return widget_default_callback (w, sender, msg, parm, data);
  391. }
  392. }
  393. /* --------------------------------------------------------------------------------------------- */
  394. static void
  395. listbox_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event)
  396. {
  397. WListbox *l = LISTBOX (w);
  398. int old_pos;
  399. old_pos = l->pos;
  400. switch (msg)
  401. {
  402. case MSG_MOUSE_DOWN:
  403. widget_select (w);
  404. listbox_select_entry (l, listbox_y_pos (l, event->y));
  405. break;
  406. case MSG_MOUSE_SCROLL_UP:
  407. listbox_back (l, FALSE);
  408. break;
  409. case MSG_MOUSE_SCROLL_DOWN:
  410. listbox_fwd (l, FALSE);
  411. break;
  412. case MSG_MOUSE_DRAG:
  413. event->result.repeat = TRUE; /* It'd be functional even without this. */
  414. listbox_select_entry (l, listbox_y_pos (l, event->y));
  415. break;
  416. case MSG_MOUSE_CLICK:
  417. /* We don't call listbox_select_entry() here: MSG_MOUSE_DOWN/DRAG did this already. */
  418. if (event->count == GPM_DOUBLE) /* Double click */
  419. listbox_do_action (l);
  420. break;
  421. default:
  422. break;
  423. }
  424. /* If the selection has changed, we redraw the widget and notify the dialog. */
  425. if (l->pos != old_pos)
  426. listbox_on_change (l);
  427. }
  428. /* --------------------------------------------------------------------------------------------- */
  429. /*** public functions ****************************************************************************/
  430. /* --------------------------------------------------------------------------------------------- */
  431. WListbox *
  432. listbox_new (int y, int x, int height, int width, gboolean deletable, lcback_fn callback)
  433. {
  434. WListbox *l;
  435. Widget *w;
  436. if (height <= 0)
  437. height = 1;
  438. l = g_new (WListbox, 1);
  439. w = WIDGET (l);
  440. widget_init (w, y, x, height, width, listbox_callback, listbox_mouse_callback);
  441. w->options |= WOP_SELECTABLE | WOP_WANT_HOTKEY;
  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 = 0;
  504. if (!listbox_is_empty (l))
  505. length = g_queue_get_length (l->list);
  506. l->pos = length > 0 ? length - 1 : 0;
  507. l->top = length > lines ? length - lines : 0;
  508. }
  509. /* --------------------------------------------------------------------------------------------- */
  510. void
  511. listbox_select_entry (WListbox * l, int dest)
  512. {
  513. GList *le;
  514. int pos;
  515. gboolean top_seen = FALSE;
  516. if (listbox_is_empty (l) || dest < 0)
  517. return;
  518. /* Special case */
  519. for (pos = 0, le = g_queue_peek_head_link (l->list); le != NULL; pos++, le = g_list_next (le))
  520. {
  521. if (pos == l->top)
  522. top_seen = TRUE;
  523. if (pos == dest)
  524. {
  525. l->pos = dest;
  526. if (!top_seen)
  527. l->top = l->pos;
  528. else
  529. {
  530. int lines = WIDGET (l)->lines;
  531. if (l->pos - l->top >= lines)
  532. l->top = l->pos - lines + 1;
  533. }
  534. return;
  535. }
  536. }
  537. /* If we are unable to find it, set decent values */
  538. l->pos = l->top = 0;
  539. }
  540. /* --------------------------------------------------------------------------------------------- */
  541. /* Returns the current string text as well as the associated extra data */
  542. void
  543. listbox_get_current (WListbox * l, char **string, void **extra)
  544. {
  545. WLEntry *e = NULL;
  546. gboolean ok;
  547. if (l != NULL)
  548. e = listbox_get_nth_item (l, l->pos);
  549. ok = (e != NULL);
  550. if (string != NULL)
  551. *string = ok ? e->text : NULL;
  552. if (extra != NULL)
  553. *extra = ok ? e->data : NULL;
  554. }
  555. /* --------------------------------------------------------------------------------------------- */
  556. WLEntry *
  557. listbox_get_nth_item (const WListbox * l, int pos)
  558. {
  559. if (!listbox_is_empty (l) && pos >= 0)
  560. {
  561. GList *item;
  562. item = g_queue_peek_nth_link (l->list, (guint) pos);
  563. if (item != NULL)
  564. return LENTRY (item->data);
  565. }
  566. return NULL;
  567. }
  568. /* --------------------------------------------------------------------------------------------- */
  569. GList *
  570. listbox_get_first_link (const WListbox * l)
  571. {
  572. return (l == NULL || l->list == NULL) ? NULL : g_queue_peek_head_link (l->list);
  573. }
  574. /* --------------------------------------------------------------------------------------------- */
  575. void
  576. listbox_remove_current (WListbox * l)
  577. {
  578. if (!listbox_is_empty (l))
  579. {
  580. GList *current;
  581. int length;
  582. current = g_queue_peek_nth_link (l->list, (guint) l->pos);
  583. listbox_entry_free (LENTRY (current->data));
  584. g_queue_delete_link (l->list, current);
  585. length = g_queue_get_length (l->list);
  586. if (length == 0)
  587. l->top = l->pos = 0;
  588. else if (l->pos >= length)
  589. l->pos = length - 1;
  590. }
  591. }
  592. /* --------------------------------------------------------------------------------------------- */
  593. gboolean
  594. listbox_is_empty (const WListbox * l)
  595. {
  596. return (l == NULL || l->list == NULL || g_queue_is_empty (l->list));
  597. }
  598. /* --------------------------------------------------------------------------------------------- */
  599. void
  600. listbox_set_list (WListbox * l, GList * list)
  601. {
  602. listbox_remove_list (l);
  603. if (l != NULL)
  604. {
  605. GList *ll;
  606. l->list = g_queue_new ();
  607. for (ll = list; ll != NULL; ll = g_list_next (ll))
  608. g_queue_push_tail (l->list, ll->data);
  609. g_list_free (list);
  610. }
  611. }
  612. /* --------------------------------------------------------------------------------------------- */
  613. void
  614. listbox_remove_list (WListbox * l)
  615. {
  616. if (l != NULL)
  617. {
  618. if (l->list != NULL)
  619. {
  620. g_queue_free_full (l->list, (GDestroyNotify) listbox_entry_free);
  621. l->list = NULL;
  622. }
  623. l->pos = l->top = 0;
  624. }
  625. }
  626. /* --------------------------------------------------------------------------------------------- */
  627. char *
  628. listbox_add_item (WListbox * l, listbox_append_t pos, int hotkey, const char *text, void *data,
  629. gboolean free_data)
  630. {
  631. WLEntry *entry;
  632. if (l == NULL)
  633. return NULL;
  634. if (!l->allow_duplicates && (listbox_search_text (l, text) >= 0))
  635. return NULL;
  636. entry = g_new (WLEntry, 1);
  637. entry->text = g_strdup (text);
  638. entry->data = data;
  639. entry->free_data = free_data;
  640. entry->hotkey = hotkey;
  641. listbox_append_item (l, entry, pos);
  642. return entry->text;
  643. }
  644. /* --------------------------------------------------------------------------------------------- */