listbox-window.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. Widget based utility functions.
  3. Copyright (C) 1994-2024
  4. Free Software Foundation, Inc.
  5. Authors:
  6. Miguel de Icaza, 1994, 1995, 1996
  7. Radek Doulik, 1994, 1995
  8. Jakub Jelinek, 1995
  9. Andrej Borsenkow, 1995
  10. Andrew Borodin <aborodin@vmail.ru>, 2009, 2010, 2013
  11. This file is part of the Midnight Commander.
  12. The Midnight Commander is free software: you can redistribute it
  13. and/or modify it under the terms of the GNU General Public License as
  14. published by the Free Software Foundation, either version 3 of the License,
  15. or (at your option) any later version.
  16. The Midnight Commander is distributed in the hope that it will be useful,
  17. but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. GNU General Public License for more details.
  20. You should have received a copy of the GNU General Public License
  21. along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. /** \file listbox-window.c
  24. * \brief Source: Listbox widget, a listbox within dialog window
  25. */
  26. #include <config.h>
  27. #include <stdlib.h>
  28. #include "lib/global.h"
  29. #include "lib/tty/tty.h" /* COLS */
  30. #include "lib/skin.h"
  31. #include "lib/strutil.h" /* str_term_width1() */
  32. #include "lib/widget.h"
  33. /*** global variables ****************************************************************************/
  34. /*** file scope macro definitions ****************************************************************/
  35. /*** file scope type declarations ****************************************************************/
  36. /*** file scope variables ************************************************************************/
  37. /*** file scope functions ************************************************************************/
  38. /* --------------------------------------------------------------------------------------------- */
  39. /*** public functions ****************************************************************************/
  40. /* --------------------------------------------------------------------------------------------- */
  41. Listbox *
  42. listbox_window_centered_new (int center_y, int center_x, int lines, int cols, const char *title,
  43. const char *help)
  44. {
  45. const int space = 4;
  46. int xpos = 0, ypos = 0;
  47. Listbox *listbox;
  48. widget_pos_flags_t pos_flags = WPOS_TRYUP;
  49. /* Adjust sizes */
  50. lines = MIN (lines, LINES - 6);
  51. if (title != NULL)
  52. {
  53. int len;
  54. len = str_term_width1 (title) + 4;
  55. cols = MAX (cols, len);
  56. }
  57. cols = MIN (cols, COLS - 6);
  58. /* adjust position */
  59. if ((center_y < 0) || (center_x < 0))
  60. pos_flags |= WPOS_CENTER;
  61. else
  62. {
  63. /* Actually, this this is not used in MC. */
  64. ypos = center_y;
  65. xpos = center_x;
  66. ypos -= lines / 2;
  67. xpos -= cols / 2;
  68. if (ypos + lines >= LINES)
  69. ypos = LINES - lines - space;
  70. if (ypos < 0)
  71. ypos = 0;
  72. if (xpos + cols >= COLS)
  73. xpos = COLS - cols - space;
  74. if (xpos < 0)
  75. xpos = 0;
  76. }
  77. listbox = g_new (Listbox, 1);
  78. listbox->dlg = dlg_create (TRUE, ypos, xpos, lines + space, cols + space, pos_flags, FALSE,
  79. listbox_colors, NULL, NULL, help, title);
  80. listbox->list = listbox_new (2, 2, lines, cols, FALSE, NULL);
  81. group_add_widget (GROUP (listbox->dlg), listbox->list);
  82. return listbox;
  83. }
  84. /* --------------------------------------------------------------------------------------------- */
  85. Listbox *
  86. listbox_window_new (int lines, int cols, const char *title, const char *help)
  87. {
  88. return listbox_window_centered_new (-1, -1, lines, cols, title, help);
  89. }
  90. /* --------------------------------------------------------------------------------------------- */
  91. /** Returns the number of the item selected */
  92. int
  93. listbox_run (Listbox *l)
  94. {
  95. int val = -1;
  96. if (dlg_run (l->dlg) != B_CANCEL)
  97. val = l->list->current;
  98. widget_destroy (WIDGET (l->dlg));
  99. g_free (l);
  100. return val;
  101. }
  102. /* --------------------------------------------------------------------------------------------- */
  103. /**
  104. * A variant of listbox_run() which is more convenient to use when we
  105. * need to select arbitrary 'data'.
  106. *
  107. * @param select the item to select initially, by its 'data'. Optional.
  108. * @return the 'data' of the item selected, or NULL if none selected.
  109. */
  110. void *
  111. listbox_run_with_data (Listbox *l, const void *select)
  112. {
  113. void *val = NULL;
  114. if (select != NULL)
  115. listbox_set_current (l->list, listbox_search_data (l->list, select));
  116. if (dlg_run (l->dlg) != B_CANCEL)
  117. {
  118. WLEntry *e;
  119. e = listbox_get_nth_entry (l->list, l->list->current);
  120. if (e != NULL)
  121. {
  122. /* The assert guards against returning a soon-to-be deallocated
  123. * pointer (as in listbox_add_item(..., TRUE)). */
  124. g_assert (!e->free_data);
  125. val = e->data;
  126. }
  127. }
  128. widget_destroy (WIDGET (l->dlg));
  129. g_free (l);
  130. return val;
  131. }
  132. /* --------------------------------------------------------------------------------------------- */