hline.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 1994-2018
  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 hline.c
  25. * \brief Source: WHLine widget (horizontal line)
  26. */
  27. #include <config.h>
  28. #include <stdlib.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/strutil.h"
  34. #include "lib/widget.h"
  35. /*** global variables ****************************************************************************/
  36. /*** file scope macro definitions ****************************************************************/
  37. /*** file scope type declarations ****************************************************************/
  38. /*** file scope variables ************************************************************************/
  39. /*** file scope functions ************************************************************************/
  40. static cb_ret_t
  41. hline_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
  42. {
  43. WHLine *l = HLINE (w);
  44. WDialog *h = w->owner;
  45. switch (msg)
  46. {
  47. case MSG_INIT:
  48. case MSG_RESIZE:
  49. if (l->auto_adjust_cols)
  50. {
  51. Widget *wo = WIDGET (h);
  52. if (h->compact)
  53. {
  54. w->x = wo->x;
  55. w->cols = wo->cols;
  56. }
  57. else
  58. {
  59. w->x = wo->x + 1;
  60. w->cols = wo->cols - 2;
  61. }
  62. }
  63. return MSG_HANDLED;
  64. case MSG_DRAW:
  65. if (l->transparent)
  66. tty_setcolor (DEFAULT_COLOR);
  67. else
  68. tty_setcolor (h->color[DLG_COLOR_NORMAL]);
  69. tty_draw_hline (w->y, w->x + 1, ACS_HLINE, w->cols - 2);
  70. if (l->auto_adjust_cols)
  71. {
  72. widget_move (w, 0, 0);
  73. tty_print_alt_char (ACS_LTEE, FALSE);
  74. widget_move (w, 0, w->cols - 1);
  75. tty_print_alt_char (ACS_RTEE, FALSE);
  76. }
  77. if (l->text != NULL)
  78. {
  79. int text_width;
  80. text_width = str_term_width1 (l->text);
  81. widget_move (w, 0, (w->cols - text_width) / 2);
  82. tty_print_string (l->text);
  83. }
  84. return MSG_HANDLED;
  85. case MSG_DESTROY:
  86. g_free (l->text);
  87. return MSG_HANDLED;
  88. default:
  89. return widget_default_callback (w, sender, msg, parm, data);
  90. }
  91. }
  92. /* --------------------------------------------------------------------------------------------- */
  93. /*** public functions ****************************************************************************/
  94. /* --------------------------------------------------------------------------------------------- */
  95. WHLine *
  96. hline_new (int y, int x, int width)
  97. {
  98. WHLine *l;
  99. Widget *w;
  100. int lines = 1;
  101. l = g_new (WHLine, 1);
  102. w = WIDGET (l);
  103. widget_init (w, y, x, lines, width < 0 ? 1 : width, hline_callback, NULL);
  104. l->text = NULL;
  105. l->auto_adjust_cols = (width < 0);
  106. l->transparent = FALSE;
  107. return l;
  108. }
  109. /* --------------------------------------------------------------------------------------------- */
  110. void
  111. hline_set_text (WHLine * l, const char *text)
  112. {
  113. g_free (l->text);
  114. if (text == NULL || *text == '\0')
  115. l->text = NULL;
  116. else
  117. l->text = g_strdup (text);
  118. widget_redraw (WIDGET (l));
  119. }
  120. /* --------------------------------------------------------------------------------------------- */