label.c 5.4 KB

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