groupbox.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 1994-2014
  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/widget.h"
  33. /*** global variables ****************************************************************************/
  34. /*** file scope macro definitions ****************************************************************/
  35. /*** file scope type declarations ****************************************************************/
  36. /*** file scope variables ************************************************************************/
  37. /*** file scope functions ************************************************************************/
  38. static cb_ret_t
  39. groupbox_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
  40. {
  41. WGroupbox *g = GROUPBOX (w);
  42. switch (msg)
  43. {
  44. case MSG_INIT:
  45. return MSG_HANDLED;
  46. case MSG_FOCUS:
  47. return MSG_NOT_HANDLED;
  48. case MSG_DRAW:
  49. {
  50. gboolean disabled;
  51. disabled = (w->options & W_DISABLED) != 0;
  52. tty_setcolor (disabled ? DISABLED_COLOR : COLOR_NORMAL);
  53. tty_draw_box (w->y, w->x, w->lines, w->cols, TRUE);
  54. if (g->title != NULL)
  55. {
  56. Widget *wo = WIDGET (w->owner);
  57. tty_setcolor (disabled ? DISABLED_COLOR : COLOR_TITLE);
  58. widget_move (wo, w->y - wo->y, w->x - wo->x + 1);
  59. tty_print_string (g->title);
  60. }
  61. return MSG_HANDLED;
  62. }
  63. case MSG_DESTROY:
  64. g_free (g->title);
  65. return MSG_HANDLED;
  66. default:
  67. return widget_default_callback (w, sender, msg, parm, data);
  68. }
  69. }
  70. /* --------------------------------------------------------------------------------------------- */
  71. /*** public functions ****************************************************************************/
  72. /* --------------------------------------------------------------------------------------------- */
  73. WGroupbox *
  74. groupbox_new (int y, int x, int height, int width, const char *title)
  75. {
  76. WGroupbox *g;
  77. Widget *w;
  78. g = g_new (WGroupbox, 1);
  79. w = WIDGET (g);
  80. widget_init (w, y, x, height, width, groupbox_callback, NULL);
  81. widget_want_cursor (w, FALSE);
  82. widget_want_hotkey (w, FALSE);
  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. g_free (g->title);
  92. g->title = NULL;
  93. /* Strip existing spaces, add one space before and after the title */
  94. if (title != NULL && *title != '\0')
  95. {
  96. char *t;
  97. t = g_strstrip (g_strdup (title));
  98. g->title = g_strconcat (" ", t, " ", (char *) NULL);
  99. g_free (t);
  100. }
  101. widget_redraw (WIDGET (g));
  102. }
  103. /* --------------------------------------------------------------------------------------------- */