label.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 1994-2014
  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 <stdlib.h>
  29. #include <string.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. /*** file scope variables ************************************************************************/
  40. /*** file scope functions ************************************************************************/
  41. static cb_ret_t
  42. label_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
  43. {
  44. WLabel *l = LABEL (w);
  45. WDialog *h = w->owner;
  46. switch (msg)
  47. {
  48. case MSG_INIT:
  49. return MSG_HANDLED;
  50. /* We don't want to get the focus */
  51. case MSG_FOCUS:
  52. return MSG_NOT_HANDLED;
  53. case MSG_DRAW:
  54. {
  55. char *p = l->text;
  56. int y = 0;
  57. gboolean disabled;
  58. align_crt_t align;
  59. if (l->text == NULL)
  60. return MSG_HANDLED;
  61. disabled = (w->options & W_DISABLED) != 0;
  62. if (l->transparent)
  63. tty_setcolor (disabled ? DISABLED_COLOR : DEFAULT_COLOR);
  64. else
  65. tty_setcolor (disabled ? DISABLED_COLOR : h->color[DLG_COLOR_NORMAL]);
  66. align = (w->pos_flags & WPOS_CENTER_HORZ) != 0 ? J_CENTER_LEFT : J_LEFT;
  67. while (TRUE)
  68. {
  69. char *q;
  70. char c = '\0';
  71. q = strchr (p, '\n');
  72. if (q != NULL)
  73. {
  74. c = q[0];
  75. q[0] = '\0';
  76. }
  77. widget_move (w, y, 0);
  78. tty_print_string (str_fit_to_term (p, w->cols, align));
  79. if (q == NULL)
  80. break;
  81. q[0] = c;
  82. p = q + 1;
  83. y++;
  84. }
  85. return MSG_HANDLED;
  86. }
  87. case MSG_DESTROY:
  88. g_free (l->text);
  89. return MSG_HANDLED;
  90. default:
  91. return widget_default_callback (w, sender, msg, parm, data);
  92. }
  93. }
  94. /* --------------------------------------------------------------------------------------------- */
  95. /*** public functions ****************************************************************************/
  96. /* --------------------------------------------------------------------------------------------- */
  97. WLabel *
  98. label_new (int y, int x, const char *text)
  99. {
  100. WLabel *l;
  101. Widget *w;
  102. int cols = 1;
  103. int lines = 1;
  104. if (text != NULL)
  105. str_msg_term_size (text, &lines, &cols);
  106. l = g_new (WLabel, 1);
  107. w = WIDGET (l);
  108. widget_init (w, y, x, lines, cols, label_callback, NULL);
  109. l->text = g_strdup (text);
  110. l->auto_adjust_cols = TRUE;
  111. l->transparent = FALSE;
  112. widget_want_cursor (w, FALSE);
  113. widget_want_hotkey (w, FALSE);
  114. return l;
  115. }
  116. /* --------------------------------------------------------------------------------------------- */
  117. void
  118. label_set_text (WLabel * label, const char *text)
  119. {
  120. Widget *w = WIDGET (label);
  121. int newcols = w->cols;
  122. int newlines;
  123. if (label->text != NULL && text != NULL && strcmp (label->text, text) == 0)
  124. return; /* Flickering is not nice */
  125. g_free (label->text);
  126. if (text == NULL)
  127. label->text = NULL;
  128. else
  129. {
  130. label->text = g_strdup (text);
  131. if (label->auto_adjust_cols)
  132. {
  133. str_msg_term_size (text, &newlines, &newcols);
  134. if (newcols > w->cols)
  135. w->cols = newcols;
  136. if (newlines > w->lines)
  137. w->lines = newlines;
  138. }
  139. }
  140. widget_redraw (w);
  141. if (newcols < w->cols)
  142. w->cols = newcols;
  143. }
  144. /* --------------------------------------------------------------------------------------------- */