hline.c 5.3 KB

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