listbox-window.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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,
  43. const char *title, 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 =
  79. dlg_create (TRUE, ypos, xpos, lines + space, cols + space, pos_flags, FALSE, listbox_colors,
  80. NULL, NULL, help, title);
  81. listbox->list = listbox_new (2, 2, lines, cols, FALSE, NULL);
  82. group_add_widget (GROUP (listbox->dlg), listbox->list);
  83. return listbox;
  84. }
  85. /* --------------------------------------------------------------------------------------------- */
  86. Listbox *
  87. listbox_window_new (int lines, int cols, const char *title, const char *help)
  88. {
  89. return listbox_window_centered_new (-1, -1, lines, cols, title, help);
  90. }
  91. /* --------------------------------------------------------------------------------------------- */
  92. /** Returns the number of the item selected */
  93. int
  94. listbox_run (Listbox *l)
  95. {
  96. int val = -1;
  97. if (dlg_run (l->dlg) != B_CANCEL)
  98. val = l->list->current;
  99. widget_destroy (WIDGET (l->dlg));
  100. g_free (l);
  101. return val;
  102. }
  103. /* --------------------------------------------------------------------------------------------- */
  104. /**
  105. * A variant of listbox_run() which is more convenient to use when we
  106. * need to select arbitrary 'data'.
  107. *
  108. * @param select the item to select initially, by its 'data'. Optional.
  109. * @return the 'data' of the item selected, or NULL if none selected.
  110. */
  111. void *
  112. listbox_run_with_data (Listbox *l, const void *select)
  113. {
  114. void *val = NULL;
  115. if (select != NULL)
  116. listbox_set_current (l->list, listbox_search_data (l->list, select));
  117. if (dlg_run (l->dlg) != B_CANCEL)
  118. {
  119. WLEntry *e;
  120. e = listbox_get_nth_entry (l->list, l->list->current);
  121. if (e != NULL)
  122. {
  123. /* The assert guards against returning a soon-to-be deallocated
  124. * pointer (as in listbox_add_item(..., TRUE)). */
  125. g_assert (!e->free_data);
  126. val = e->data;
  127. }
  128. }
  129. widget_destroy (WIDGET (l->dlg));
  130. g_free (l);
  131. return val;
  132. }
  133. /* --------------------------------------------------------------------------------------------- */