label.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 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. /*** forward declarations (file scope functions) *************************************************/
  41. /*** file scope variables ************************************************************************/
  42. /* --------------------------------------------------------------------------------------------- */
  43. /*** file scope functions ************************************************************************/
  44. /* --------------------------------------------------------------------------------------------- */
  45. static cb_ret_t
  46. label_callback (Widget *w, Widget *sender, widget_msg_t msg, int parm, void *data)
  47. {
  48. WLabel *l = LABEL (w);
  49. switch (msg)
  50. {
  51. case MSG_DRAW:
  52. {
  53. char *p = l->text;
  54. int y = 0;
  55. gboolean disabled;
  56. align_crt_t align;
  57. if (l->text == NULL)
  58. return MSG_HANDLED;
  59. disabled = widget_get_state (w, WST_DISABLED);
  60. if (l->transparent)
  61. tty_setcolor (disabled ? DISABLED_COLOR : DEFAULT_COLOR);
  62. else
  63. {
  64. const int *colors;
  65. colors = widget_get_colors (w);
  66. tty_setcolor (disabled ? DISABLED_COLOR : colors[DLG_COLOR_NORMAL]);
  67. }
  68. align = (w->pos_flags & WPOS_CENTER_HORZ) != 0 ? J_CENTER_LEFT : J_LEFT;
  69. while (TRUE)
  70. {
  71. char *q;
  72. char c = '\0';
  73. q = strchr (p, '\n');
  74. if (q != NULL)
  75. {
  76. c = q[0];
  77. q[0] = '\0';
  78. }
  79. widget_gotoyx (w, y, 0);
  80. tty_print_string (str_fit_to_term (p, w->rect.cols, align));
  81. if (q == NULL)
  82. break;
  83. q[0] = c;
  84. p = q + 1;
  85. y++;
  86. }
  87. return MSG_HANDLED;
  88. }
  89. case MSG_DESTROY:
  90. g_free (l->text);
  91. return MSG_HANDLED;
  92. default:
  93. return widget_default_callback (w, sender, msg, parm, data);
  94. }
  95. }
  96. /* --------------------------------------------------------------------------------------------- */
  97. /*** public functions ****************************************************************************/
  98. /* --------------------------------------------------------------------------------------------- */
  99. WLabel *
  100. label_new (int y, int x, const char *text)
  101. {
  102. WRect r = { y, x, 1, 1 };
  103. WLabel *l;
  104. Widget *w;
  105. if (text != NULL)
  106. str_msg_term_size (text, &r.lines, &r.cols);
  107. l = g_new (WLabel, 1);
  108. w = WIDGET (l);
  109. widget_init (w, &r, label_callback, NULL);
  110. l->text = g_strdup (text);
  111. l->auto_adjust_cols = TRUE;
  112. l->transparent = FALSE;
  113. return l;
  114. }
  115. /* --------------------------------------------------------------------------------------------- */
  116. void
  117. label_set_text (WLabel *label, const char *text)
  118. {
  119. Widget *w = WIDGET (label);
  120. int newcols = w->rect.cols;
  121. int newlines;
  122. if (label->text != NULL && text != NULL && strcmp (label->text, text) == 0)
  123. return; /* Flickering is not nice */
  124. g_free (label->text);
  125. if (text == NULL)
  126. label->text = NULL;
  127. else
  128. {
  129. label->text = g_strdup (text);
  130. if (label->auto_adjust_cols)
  131. {
  132. str_msg_term_size (text, &newlines, &newcols);
  133. w->rect.cols = MAX (newcols, w->rect.cols);
  134. w->rect.lines = MAX (newlines, w->rect.lines);
  135. }
  136. }
  137. widget_draw (w);
  138. w->rect.cols = MIN (newcols, w->rect.cols);
  139. }
  140. /* --------------------------------------------------------------------------------------------- */
  141. void
  142. label_set_textv (WLabel *label, 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. label_set_text (label, buf);
  150. }
  151. /* --------------------------------------------------------------------------------------------- */