label.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 label.c
  25. * \brief Source: WLabel widget
  26. */
  27. #include <config.h>
  28. #include <stdarg.h>
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include "lib/global.h"
  32. #include "lib/tty/tty.h"
  33. #include "lib/tty/color.h"
  34. #include "lib/skin.h"
  35. #include "lib/strutil.h"
  36. #include "lib/widget.h"
  37. /*** global variables ****************************************************************************/
  38. /*** file scope macro definitions ****************************************************************/
  39. /*** file scope type declarations ****************************************************************/
  40. /*** file scope variables ************************************************************************/
  41. /*** file scope functions ************************************************************************/
  42. static cb_ret_t
  43. label_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
  44. {
  45. WLabel *l = LABEL (w);
  46. switch (msg)
  47. {
  48. case MSG_DRAW:
  49. {
  50. char *p = l->text;
  51. int y = 0;
  52. gboolean disabled;
  53. align_crt_t align;
  54. if (l->text == NULL)
  55. return MSG_HANDLED;
  56. disabled = widget_get_state (w, WST_DISABLED);
  57. if (l->transparent)
  58. tty_setcolor (disabled ? DISABLED_COLOR : DEFAULT_COLOR);
  59. else
  60. {
  61. const int *colors;
  62. colors = widget_get_colors (w);
  63. tty_setcolor (disabled ? DISABLED_COLOR : colors[DLG_COLOR_NORMAL]);
  64. }
  65. align = (w->pos_flags & WPOS_CENTER_HORZ) != 0 ? J_CENTER_LEFT : J_LEFT;
  66. while (TRUE)
  67. {
  68. char *q;
  69. char c = '\0';
  70. q = strchr (p, '\n');
  71. if (q != NULL)
  72. {
  73. c = q[0];
  74. q[0] = '\0';
  75. }
  76. widget_gotoyx (w, y, 0);
  77. tty_print_string (str_fit_to_term (p, w->cols, align));
  78. if (q == NULL)
  79. break;
  80. q[0] = c;
  81. p = q + 1;
  82. y++;
  83. }
  84. return MSG_HANDLED;
  85. }
  86. case MSG_DESTROY:
  87. g_free (l->text);
  88. return MSG_HANDLED;
  89. default:
  90. return widget_default_callback (w, sender, msg, parm, data);
  91. }
  92. }
  93. /* --------------------------------------------------------------------------------------------- */
  94. /*** public functions ****************************************************************************/
  95. /* --------------------------------------------------------------------------------------------- */
  96. WLabel *
  97. label_new (int y, int x, const char *text)
  98. {
  99. WLabel *l;
  100. Widget *w;
  101. int cols = 1;
  102. int lines = 1;
  103. if (text != NULL)
  104. str_msg_term_size (text, &lines, &cols);
  105. l = g_new (WLabel, 1);
  106. w = WIDGET (l);
  107. widget_init (w, y, x, lines, cols, label_callback, NULL);
  108. l->text = g_strdup (text);
  109. l->auto_adjust_cols = TRUE;
  110. l->transparent = FALSE;
  111. return l;
  112. }
  113. /* --------------------------------------------------------------------------------------------- */
  114. void
  115. label_set_text (WLabel * label, const char *text)
  116. {
  117. Widget *w = WIDGET (label);
  118. int newcols = w->cols;
  119. int newlines;
  120. if (label->text != NULL && text != NULL && strcmp (label->text, text) == 0)
  121. return; /* Flickering is not nice */
  122. g_free (label->text);
  123. if (text == NULL)
  124. label->text = NULL;
  125. else
  126. {
  127. label->text = g_strdup (text);
  128. if (label->auto_adjust_cols)
  129. {
  130. str_msg_term_size (text, &newlines, &newcols);
  131. if (newcols > w->cols)
  132. w->cols = newcols;
  133. if (newlines > w->lines)
  134. w->lines = newlines;
  135. }
  136. }
  137. widget_draw (w);
  138. if (newcols < w->cols)
  139. w->cols = newcols;
  140. }
  141. /* --------------------------------------------------------------------------------------------- */
  142. void
  143. label_set_textv (WLabel * label, const char *format, ...)
  144. {
  145. va_list args;
  146. char buf[BUF_1K]; /* FIXME: is it enough? */
  147. va_start (args, format);
  148. g_vsnprintf (buf, sizeof (buf), format, args);
  149. va_end (args);
  150. label_set_text (label, buf);
  151. }
  152. /* --------------------------------------------------------------------------------------------- */