listbox-window.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. Widget based utility functions.
  3. Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
  4. 2005, 2006, 2007, 2008, 2009, 2010, 2011
  5. The Free Software Foundation, Inc.
  6. Authors:
  7. Miguel de Icaza, 1994, 1995, 1996
  8. Radek Doulik, 1994, 1995
  9. Jakub Jelinek, 1995
  10. Andrej Borsenkow, 1995
  11. Andrew Borodin <aborodin@vmail.ru>, 2009, 2010
  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-window.c
  25. * \brief Source: Listbox widget, a listbox within dialog window
  26. */
  27. #include <config.h>
  28. #include <stdlib.h>
  29. #include "lib/global.h"
  30. #include "lib/tty/tty.h" /* COLS */
  31. #include "lib/skin.h"
  32. #include "lib/strutil.h" /* str_term_width1() */
  33. #include "lib/widget.h"
  34. /*** global variables ****************************************************************************/
  35. /*** file scope macro definitions ****************************************************************/
  36. /*** file scope type declarations ****************************************************************/
  37. /*** file scope variables ************************************************************************/
  38. /*** file scope functions ************************************************************************/
  39. /* --------------------------------------------------------------------------------------------- */
  40. /*** public functions ****************************************************************************/
  41. /* --------------------------------------------------------------------------------------------- */
  42. Listbox *
  43. create_listbox_window_centered (int center_y, int center_x, int lines, int cols,
  44. const char *title, const char *help)
  45. {
  46. const dlg_colors_t listbox_colors = {
  47. PMENU_ENTRY_COLOR,
  48. PMENU_SELECTED_COLOR,
  49. PMENU_ENTRY_COLOR,
  50. PMENU_SELECTED_COLOR,
  51. PMENU_TITLE_COLOR
  52. };
  53. const int space = 4;
  54. int xpos, ypos, len;
  55. Listbox *listbox;
  56. /* Adjust sizes */
  57. lines = min (lines, LINES - 6);
  58. if (title != NULL)
  59. {
  60. len = str_term_width1 (title) + 4;
  61. cols = max (cols, len);
  62. }
  63. cols = min (cols, COLS - 6);
  64. /* adjust position */
  65. if ((center_y < 0) || (center_x < 0))
  66. {
  67. ypos = LINES / 2;
  68. xpos = COLS / 2;
  69. }
  70. else
  71. {
  72. ypos = center_y;
  73. xpos = center_x;
  74. }
  75. ypos -= lines / 2;
  76. xpos -= cols / 2;
  77. if (ypos + lines >= LINES)
  78. ypos = LINES - lines - space;
  79. if (ypos < 0)
  80. ypos = 0;
  81. if (xpos + cols >= COLS)
  82. xpos = COLS - cols - space;
  83. if (xpos < 0)
  84. xpos = 0;
  85. listbox = g_new (Listbox, 1);
  86. listbox->dlg =
  87. create_dlg (TRUE, ypos, xpos, lines + space, cols + space,
  88. listbox_colors, NULL, NULL, help, title, DLG_REVERSE | DLG_TRYUP);
  89. listbox->list = listbox_new (2, 2, lines, cols, FALSE, NULL);
  90. add_widget (listbox->dlg, listbox->list);
  91. return listbox;
  92. }
  93. /* --------------------------------------------------------------------------------------------- */
  94. Listbox *
  95. create_listbox_window (int lines, int cols, const char *title, const char *help)
  96. {
  97. return create_listbox_window_centered (-1, -1, lines, cols, title, help);
  98. }
  99. /* --------------------------------------------------------------------------------------------- */
  100. /** Returns the number of the item selected */
  101. int
  102. run_listbox (Listbox * l)
  103. {
  104. int val = -1;
  105. if (run_dlg (l->dlg) != B_CANCEL)
  106. val = l->list->pos;
  107. destroy_dlg (l->dlg);
  108. g_free (l);
  109. return val;
  110. }
  111. /* --------------------------------------------------------------------------------------------- */