groupbox.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 1994-2021
  4. Free Software Foundation, Inc.
  5. Authors:
  6. Radek Doulik, 1994, 1995
  7. Miguel de Icaza, 1994, 1995
  8. Jakub Jelinek, 1995
  9. Andrej Borsenkow, 1996
  10. Norbert Warmuth, 1997
  11. Andrew Borodin <aborodin@vmail.ru>, 2009, 2010, 2013
  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 groupbox.c
  25. * \brief Source: WGroupbox widget
  26. */
  27. #include <config.h>
  28. #include "lib/global.h"
  29. #include "lib/tty/tty.h"
  30. #include "lib/tty/color.h"
  31. #include "lib/skin.h"
  32. #include "lib/util.h"
  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. static cb_ret_t
  40. groupbox_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
  41. {
  42. WGroupbox *g = GROUPBOX (w);
  43. switch (msg)
  44. {
  45. case MSG_DRAW:
  46. {
  47. gboolean disabled;
  48. const int *colors;
  49. colors = widget_get_colors (w);
  50. disabled = widget_get_state (w, WST_DISABLED);
  51. tty_setcolor (disabled ? DISABLED_COLOR : colors[DLG_COLOR_NORMAL]);
  52. tty_draw_box (w->y, w->x, w->lines, w->cols, TRUE);
  53. if (g->title != NULL)
  54. {
  55. tty_setcolor (disabled ? DISABLED_COLOR : colors[DLG_COLOR_TITLE]);
  56. widget_gotoyx (w, 0, 1);
  57. tty_print_string (g->title);
  58. }
  59. return MSG_HANDLED;
  60. }
  61. case MSG_DESTROY:
  62. g_free (g->title);
  63. return MSG_HANDLED;
  64. default:
  65. return widget_default_callback (w, sender, msg, parm, data);
  66. }
  67. }
  68. /* --------------------------------------------------------------------------------------------- */
  69. /*** public functions ****************************************************************************/
  70. /* --------------------------------------------------------------------------------------------- */
  71. WGroupbox *
  72. groupbox_new (int y, int x, int height, int width, const char *title)
  73. {
  74. WGroupbox *g;
  75. Widget *w;
  76. g = g_new (WGroupbox, 1);
  77. w = WIDGET (g);
  78. widget_init (w, y, x, height, width, groupbox_callback, NULL);
  79. g->title = NULL;
  80. groupbox_set_title (g, title);
  81. return g;
  82. }
  83. /* --------------------------------------------------------------------------------------------- */
  84. void
  85. groupbox_set_title (WGroupbox * g, const char *title)
  86. {
  87. MC_PTR_FREE (g->title);
  88. /* Strip existing spaces, add one space before and after the title */
  89. if (title != NULL && *title != '\0')
  90. {
  91. char *t;
  92. t = g_strstrip (g_strdup (title));
  93. g->title = g_strconcat (" ", t, " ", (char *) NULL);
  94. g_free (t);
  95. }
  96. widget_draw (WIDGET (g));
  97. }
  98. /* --------------------------------------------------------------------------------------------- */