label.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 1994-2017
  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. WDialog *h = w->owner;
  47. switch (msg)
  48. {
  49. case MSG_DRAW:
  50. {
  51. char *p = l->text;
  52. int y = 0;
  53. gboolean disabled;
  54. align_crt_t align;
  55. if (l->text == NULL)
  56. return MSG_HANDLED;
  57. disabled = widget_get_state (w, WST_DISABLED);
  58. if (l->transparent)
  59. tty_setcolor (disabled ? DISABLED_COLOR : DEFAULT_COLOR);
  60. else
  61. tty_setcolor (disabled ? DISABLED_COLOR : h->color[DLG_COLOR_NORMAL]);
  62. align = (w->pos_flags & WPOS_CENTER_HORZ) != 0 ? J_CENTER_LEFT : J_LEFT;
  63. while (TRUE)
  64. {
  65. char *q;
  66. char c = '\0';
  67. q = strchr (p, '\n');
  68. if (q != NULL)
  69. {
  70. c = q[0];
  71. q[0] = '\0';
  72. }
  73. widget_move (w, y, 0);
  74. tty_print_string (str_fit_to_term (p, w->cols, align));
  75. if (q == NULL)
  76. break;
  77. q[0] = c;
  78. p = q + 1;
  79. y++;
  80. }
  81. return MSG_HANDLED;
  82. }
  83. case MSG_DESTROY:
  84. g_free (l->text);
  85. return MSG_HANDLED;
  86. default:
  87. return widget_default_callback (w, sender, msg, parm, data);
  88. }
  89. }
  90. /* --------------------------------------------------------------------------------------------- */
  91. /*** public functions ****************************************************************************/
  92. /* --------------------------------------------------------------------------------------------- */
  93. WLabel *
  94. label_new (int y, int x, const char *text)
  95. {
  96. WLabel *l;
  97. Widget *w;
  98. int cols = 1;
  99. int lines = 1;
  100. if (text != NULL)
  101. str_msg_term_size (text, &lines, &cols);
  102. l = g_new (WLabel, 1);
  103. w = WIDGET (l);
  104. widget_init (w, y, x, lines, cols, label_callback, NULL);
  105. l->text = g_strdup (text);
  106. l->auto_adjust_cols = TRUE;
  107. l->transparent = FALSE;
  108. return l;
  109. }
  110. /* --------------------------------------------------------------------------------------------- */
  111. void
  112. label_set_text (WLabel * label, const char *text)
  113. {
  114. Widget *w = WIDGET (label);
  115. int newcols = w->cols;
  116. int newlines;
  117. if (label->text != NULL && text != NULL && strcmp (label->text, text) == 0)
  118. return; /* Flickering is not nice */
  119. g_free (label->text);
  120. if (text == NULL)
  121. label->text = NULL;
  122. else
  123. {
  124. label->text = g_strdup (text);
  125. if (label->auto_adjust_cols)
  126. {
  127. str_msg_term_size (text, &newlines, &newcols);
  128. if (newcols > w->cols)
  129. w->cols = newcols;
  130. if (newlines > w->lines)
  131. w->lines = newlines;
  132. }
  133. }
  134. widget_redraw (w);
  135. if (newcols < w->cols)
  136. w->cols = newcols;
  137. }
  138. /* --------------------------------------------------------------------------------------------- */
  139. void
  140. label_set_textv (WLabel * label, const char *format, ...)
  141. {
  142. va_list args;
  143. char buf[BUF_1K]; /* FIXME: is it enough? */
  144. va_start (args, format);
  145. g_vsnprintf (buf, sizeof (buf), format, args);
  146. va_end (args);
  147. label_set_text (label, buf);
  148. }
  149. /* --------------------------------------------------------------------------------------------- */