groupbox.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 1994-2017
  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. WDialog *h = w->owner;
  48. gboolean disabled;
  49. disabled = widget_get_state (w, WST_DISABLED);
  50. tty_setcolor (disabled ? DISABLED_COLOR : h->color[DLG_COLOR_NORMAL]);
  51. tty_draw_box (w->y, w->x, w->lines, w->cols, TRUE);
  52. if (g->title != NULL)
  53. {
  54. tty_setcolor (disabled ? DISABLED_COLOR : h->color[DLG_COLOR_TITLE]);
  55. widget_move (w, 0, 1);
  56. tty_print_string (g->title);
  57. }
  58. return MSG_HANDLED;
  59. }
  60. case MSG_DESTROY:
  61. g_free (g->title);
  62. return MSG_HANDLED;
  63. default:
  64. return widget_default_callback (w, sender, msg, parm, data);
  65. }
  66. }
  67. /* --------------------------------------------------------------------------------------------- */
  68. /*** public functions ****************************************************************************/
  69. /* --------------------------------------------------------------------------------------------- */
  70. WGroupbox *
  71. groupbox_new (int y, int x, int height, int width, const char *title)
  72. {
  73. WGroupbox *g;
  74. Widget *w;
  75. g = g_new (WGroupbox, 1);
  76. w = WIDGET (g);
  77. widget_init (w, y, x, height, width, groupbox_callback, NULL);
  78. g->title = NULL;
  79. groupbox_set_title (g, title);
  80. return g;
  81. }
  82. /* --------------------------------------------------------------------------------------------- */
  83. void
  84. groupbox_set_title (WGroupbox * g, const char *title)
  85. {
  86. MC_PTR_FREE (g->title);
  87. /* Strip existing spaces, add one space before and after the title */
  88. if (title != NULL && *title != '\0')
  89. {
  90. char *t;
  91. t = g_strstrip (g_strdup (title));
  92. g->title = g_strconcat (" ", t, " ", (char *) NULL);
  93. g_free (t);
  94. }
  95. widget_redraw (WIDGET (g));
  96. }
  97. /* --------------------------------------------------------------------------------------------- */