groupbox.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
  4. 2004, 2005, 2006, 2007, 2009, 2010, 2011, 2013
  5. The Free Software Foundation, Inc.
  6. Authors:
  7. Radek Doulik, 1994, 1995
  8. Miguel de Icaza, 1994, 1995
  9. Jakub Jelinek, 1995
  10. Andrej Borsenkow, 1996
  11. Norbert Warmuth, 1997
  12. Andrew Borodin <aborodin@vmail.ru>, 2009, 2010, 2013
  13. This file is part of the Midnight Commander.
  14. The Midnight Commander is free software: you can redistribute it
  15. and/or modify it under the terms of the GNU General Public License as
  16. published by the Free Software Foundation, either version 3 of the License,
  17. or (at your option) any later version.
  18. The Midnight Commander is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. GNU General Public License for more details.
  22. You should have received a copy of the GNU General Public License
  23. along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. /** \file groupbox.c
  26. * \brief Source: WGroupbox widget
  27. */
  28. #include <config.h>
  29. #include "lib/global.h"
  30. #include "lib/tty/tty.h"
  31. #include "lib/tty/color.h"
  32. #include "lib/skin.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. g_free (g->title);
  93. g->title = NULL;
  94. /* Strip existing spaces, add one space before and after the title */
  95. if (title != NULL && *title != '\0')
  96. {
  97. char *t;
  98. t = g_strstrip (g_strdup (title));
  99. g->title = g_strconcat (" ", t, " ", (char *) NULL);
  100. g_free (t);
  101. }
  102. widget_redraw (WIDGET (g));
  103. }
  104. /* --------------------------------------------------------------------------------------------- */