groupbox.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 1994-2024
  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-2022
  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. /*** forward declarations (file scope functions) *************************************************/
  38. /*** file scope variables ************************************************************************/
  39. /* --------------------------------------------------------------------------------------------- */
  40. /*** file scope functions ************************************************************************/
  41. /* --------------------------------------------------------------------------------------------- */
  42. static cb_ret_t
  43. groupbox_callback (Widget *w, Widget *sender, widget_msg_t msg, int parm, void *data)
  44. {
  45. WGroupbox *g = GROUPBOX (w);
  46. switch (msg)
  47. {
  48. case MSG_DRAW:
  49. {
  50. gboolean disabled;
  51. const int *colors;
  52. colors = widget_get_colors (w);
  53. disabled = widget_get_state (w, WST_DISABLED);
  54. tty_setcolor (disabled ? DISABLED_COLOR : colors[DLG_COLOR_NORMAL]);
  55. tty_draw_box (w->rect.y, w->rect.x, w->rect.lines, w->rect.cols, TRUE);
  56. if (g->title != NULL)
  57. {
  58. tty_setcolor (disabled ? DISABLED_COLOR : colors[DLG_COLOR_TITLE]);
  59. widget_gotoyx (w, 0, 1);
  60. tty_print_string (g->title);
  61. }
  62. return MSG_HANDLED;
  63. }
  64. case MSG_DESTROY:
  65. g_free (g->title);
  66. return MSG_HANDLED;
  67. default:
  68. return widget_default_callback (w, sender, msg, parm, data);
  69. }
  70. }
  71. /* --------------------------------------------------------------------------------------------- */
  72. /*** public functions ****************************************************************************/
  73. /* --------------------------------------------------------------------------------------------- */
  74. WGroupbox *
  75. groupbox_new (int y, int x, int height, int width, const char *title)
  76. {
  77. WRect r = { y, x, height, width };
  78. WGroupbox *g;
  79. Widget *w;
  80. g = g_new (WGroupbox, 1);
  81. w = WIDGET (g);
  82. widget_init (w, &r, groupbox_callback, NULL);
  83. g->title = NULL;
  84. groupbox_set_title (g, title);
  85. return g;
  86. }
  87. /* --------------------------------------------------------------------------------------------- */
  88. void
  89. groupbox_set_title (WGroupbox *g, const char *title)
  90. {
  91. MC_PTR_FREE (g->title);
  92. /* Strip existing spaces, add one space before and after the title */
  93. if (title != NULL && *title != '\0')
  94. {
  95. char *t;
  96. t = g_strstrip (g_strdup (title));
  97. g->title = g_strconcat (" ", t, " ", (char *) NULL);
  98. g_free (t);
  99. }
  100. widget_draw (WIDGET (g));
  101. }
  102. /* --------------------------------------------------------------------------------------------- */