groupbox.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 1994-2015
  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_INIT:
  46. return MSG_HANDLED;
  47. case MSG_FOCUS:
  48. return MSG_NOT_HANDLED;
  49. case MSG_DRAW:
  50. {
  51. gboolean disabled;
  52. disabled = (w->options & W_DISABLED) != 0;
  53. tty_setcolor (disabled ? DISABLED_COLOR : COLOR_NORMAL);
  54. tty_draw_box (w->y, w->x, w->lines, w->cols, TRUE);
  55. if (g->title != NULL)
  56. {
  57. Widget *wo = WIDGET (w->owner);
  58. tty_setcolor (disabled ? DISABLED_COLOR : COLOR_TITLE);
  59. widget_move (wo, w->y - wo->y, w->x - wo->x + 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. WGroupbox *g;
  78. Widget *w;
  79. g = g_new (WGroupbox, 1);
  80. w = WIDGET (g);
  81. widget_init (w, y, x, height, width, groupbox_callback, NULL);
  82. widget_want_cursor (w, FALSE);
  83. widget_want_hotkey (w, FALSE);
  84. g->title = NULL;
  85. groupbox_set_title (g, title);
  86. return g;
  87. }
  88. /* --------------------------------------------------------------------------------------------- */
  89. void
  90. groupbox_set_title (WGroupbox * g, const char *title)
  91. {
  92. MC_PTR_FREE (g->title);
  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. /* --------------------------------------------------------------------------------------------- */