hline.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 1994-2021
  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 <stdarg.h>
  29. #include <stdlib.h>
  30. #include "lib/global.h"
  31. #include "lib/tty/tty.h"
  32. #include "lib/tty/color.h"
  33. #include "lib/skin.h"
  34. #include "lib/strutil.h"
  35. #include "lib/widget.h"
  36. /*** global variables ****************************************************************************/
  37. /*** file scope macro definitions ****************************************************************/
  38. /*** file scope type declarations ****************************************************************/
  39. /*** file scope variables ************************************************************************/
  40. /* --------------------------------------------------------------------------------------------- */
  41. /*** file scope functions ************************************************************************/
  42. /* --------------------------------------------------------------------------------------------- */
  43. static void
  44. hline_adjust_cols (WHLine * l)
  45. {
  46. if (l->auto_adjust_cols)
  47. {
  48. Widget *w = WIDGET (l);
  49. Widget *wo = WIDGET (w->owner);
  50. if (DIALOG (wo)->compact)
  51. {
  52. w->x = wo->x;
  53. w->cols = wo->cols;
  54. }
  55. else
  56. {
  57. w->x = wo->x + 1;
  58. w->cols = wo->cols - 2;
  59. }
  60. }
  61. }
  62. /* --------------------------------------------------------------------------------------------- */
  63. static cb_ret_t
  64. hline_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
  65. {
  66. WHLine *l = HLINE (w);
  67. switch (msg)
  68. {
  69. case MSG_INIT:
  70. hline_adjust_cols (l);
  71. return MSG_HANDLED;
  72. case MSG_RESIZE:
  73. hline_adjust_cols (l);
  74. w->y = RECT (data)->y;
  75. return MSG_HANDLED;
  76. case MSG_DRAW:
  77. if (l->transparent)
  78. tty_setcolor (DEFAULT_COLOR);
  79. else
  80. {
  81. const int *colors;
  82. colors = widget_get_colors (w);
  83. tty_setcolor (colors[DLG_COLOR_NORMAL]);
  84. }
  85. tty_draw_hline (w->y, w->x + 1, ACS_HLINE, w->cols - 2);
  86. if (l->auto_adjust_cols)
  87. {
  88. widget_gotoyx (w, 0, 0);
  89. tty_print_alt_char (ACS_LTEE, FALSE);
  90. widget_gotoyx (w, 0, w->cols - 1);
  91. tty_print_alt_char (ACS_RTEE, FALSE);
  92. }
  93. if (l->text != NULL)
  94. {
  95. int text_width;
  96. text_width = str_term_width1 (l->text);
  97. widget_gotoyx (w, 0, (w->cols - text_width) / 2);
  98. tty_print_string (l->text);
  99. }
  100. return MSG_HANDLED;
  101. case MSG_DESTROY:
  102. g_free (l->text);
  103. return MSG_HANDLED;
  104. default:
  105. return widget_default_callback (w, sender, msg, parm, data);
  106. }
  107. }
  108. /* --------------------------------------------------------------------------------------------- */
  109. /*** public functions ****************************************************************************/
  110. /* --------------------------------------------------------------------------------------------- */
  111. WHLine *
  112. hline_new (int y, int x, int width)
  113. {
  114. WHLine *l;
  115. Widget *w;
  116. int lines = 1;
  117. l = g_new (WHLine, 1);
  118. w = WIDGET (l);
  119. widget_init (w, y, x, lines, width < 0 ? 1 : width, hline_callback, NULL);
  120. l->text = NULL;
  121. l->auto_adjust_cols = (width < 0);
  122. l->transparent = FALSE;
  123. return l;
  124. }
  125. /* --------------------------------------------------------------------------------------------- */
  126. void
  127. hline_set_text (WHLine * l, const char *text)
  128. {
  129. g_free (l->text);
  130. if (text == NULL || *text == '\0')
  131. l->text = NULL;
  132. else
  133. l->text = g_strdup (text);
  134. widget_draw (WIDGET (l));
  135. }
  136. /* --------------------------------------------------------------------------------------------- */
  137. void
  138. hline_set_textv (WHLine * l, const char *format, ...)
  139. {
  140. va_list args;
  141. char buf[BUF_1K]; /* FIXME: is it enough? */
  142. va_start (args, format);
  143. g_vsnprintf (buf, sizeof (buf), format, args);
  144. va_end (args);
  145. hline_set_text (l, buf);
  146. }
  147. /* --------------------------------------------------------------------------------------------- */