listbox-window.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. Widget based utility functions.
  3. Copyright (C) 1994-2015
  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. create_listbox_window_centered (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. dlg_flags_t dlg_flags = DLG_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. dlg_flags |= DLG_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,
  80. listbox_colors, NULL, NULL, help, title, dlg_flags);
  81. listbox->list = listbox_new (2, 2, lines, cols, FALSE, NULL);
  82. add_widget (listbox->dlg, listbox->list);
  83. return listbox;
  84. }
  85. /* --------------------------------------------------------------------------------------------- */
  86. Listbox *
  87. create_listbox_window (int lines, int cols, const char *title, const char *help)
  88. {
  89. return create_listbox_window_centered (-1, -1, lines, cols, title, help);
  90. }
  91. /* --------------------------------------------------------------------------------------------- */
  92. /** Returns the number of the item selected */
  93. int
  94. run_listbox (Listbox * l)
  95. {
  96. int val = -1;
  97. if (dlg_run (l->dlg) != B_CANCEL)
  98. val = l->list->pos;
  99. dlg_destroy (l->dlg);
  100. g_free (l);
  101. return val;
  102. }
  103. /* --------------------------------------------------------------------------------------------- */