listbox-window.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* Widget based utility functions.
  2. Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
  3. 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
  4. Authors: 1994, 1995, 1996 Miguel de Icaza
  5. 1994, 1995 Radek Doulik
  6. 1995 Jakub Jelinek
  7. 1995 Andrej Borsenkow
  8. 2009, 2010 Andrew Borodin
  9. This program is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or
  12. (at your option) any later version.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. */
  21. /** \file listbox-window.c
  22. * \brief Source: Listbox widget, a listbox within dialog window
  23. */
  24. #include <config.h>
  25. #include <stdlib.h>
  26. #include "lib/global.h"
  27. #include "lib/tty/tty.h" /* COLS */
  28. #include "lib/skin.h"
  29. #include "lib/strutil.h" /* str_term_width1() */
  30. #include "lib/widget.h"
  31. /*** global variables ****************************************************************************/
  32. /*** file scope macro definitions ****************************************************************/
  33. /*** file scope type declarations ****************************************************************/
  34. /*** file scope variables ************************************************************************/
  35. /*** file scope functions ************************************************************************/
  36. /* --------------------------------------------------------------------------------------------- */
  37. /*** public functions ****************************************************************************/
  38. /* --------------------------------------------------------------------------------------------- */
  39. Listbox *
  40. create_listbox_window_centered (int center_y, int center_x, int lines, int cols,
  41. const char *title, const char *help)
  42. {
  43. const dlg_colors_t listbox_colors = {
  44. PMENU_ENTRY_COLOR,
  45. PMENU_SELECTED_COLOR,
  46. PMENU_ENTRY_COLOR,
  47. PMENU_SELECTED_COLOR,
  48. PMENU_TITLE_COLOR
  49. };
  50. const int space = 4;
  51. int xpos, ypos, len;
  52. Listbox *listbox;
  53. /* Adjust sizes */
  54. lines = min (lines, LINES - 6);
  55. if (title != NULL)
  56. {
  57. len = str_term_width1 (title) + 4;
  58. cols = max (cols, len);
  59. }
  60. cols = min (cols, COLS - 6);
  61. /* adjust position */
  62. if ((center_y < 0) || (center_x < 0))
  63. {
  64. ypos = LINES / 2;
  65. xpos = COLS / 2;
  66. }
  67. else
  68. {
  69. ypos = center_y;
  70. xpos = center_x;
  71. }
  72. ypos -= lines / 2;
  73. xpos -= cols / 2;
  74. if (ypos + lines >= LINES)
  75. ypos = LINES - lines - space;
  76. if (ypos < 0)
  77. ypos = 0;
  78. if (xpos + cols >= COLS)
  79. xpos = COLS - cols - space;
  80. if (xpos < 0)
  81. xpos = 0;
  82. listbox = g_new (Listbox, 1);
  83. listbox->dlg =
  84. create_dlg (TRUE, ypos, xpos, lines + space, cols + space,
  85. listbox_colors, NULL, help, title, DLG_REVERSE | DLG_TRYUP);
  86. listbox->list = listbox_new (2, 2, lines, cols, FALSE, NULL);
  87. add_widget (listbox->dlg, listbox->list);
  88. return listbox;
  89. }
  90. /* --------------------------------------------------------------------------------------------- */
  91. Listbox *
  92. create_listbox_window (int lines, int cols, const char *title, const char *help)
  93. {
  94. return create_listbox_window_centered (-1, -1, lines, cols, title, help);
  95. }
  96. /* --------------------------------------------------------------------------------------------- */
  97. /** Returns the number of the item selected */
  98. int
  99. run_listbox (Listbox * l)
  100. {
  101. int val = -1;
  102. if (run_dlg (l->dlg) != B_CANCEL)
  103. val = l->list->pos;
  104. destroy_dlg (l->dlg);
  105. g_free (l);
  106. return val;
  107. }
  108. /* --------------------------------------------------------------------------------------------- */