label.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. Widgets for the Midnight Commander
  3. Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
  4. 2004, 2005, 2006, 2007, 2009, 2010, 2011
  5. The Free Software Foundation, Inc.
  6. Authors:
  7. Radek Doulik, 1994, 1995
  8. Miguel de Icaza, 1994, 1995
  9. Jakub Jelinek, 1995
  10. Andrej Borsenkow, 1996
  11. Norbert Warmuth, 1997
  12. Andrew Borodin <aborodin@vmail.ru>, 2009, 2010
  13. This file is part of the Midnight Commander.
  14. The Midnight Commander is free software: you can redistribute it
  15. and/or modify it under the terms of the GNU General Public License as
  16. published by the Free Software Foundation, either version 3 of the License,
  17. or (at your option) any later version.
  18. The Midnight Commander is distributed in the hope that it will be useful,
  19. but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. GNU General Public License for more details.
  22. You should have received a copy of the GNU General Public License
  23. along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. /** \file label.c
  26. * \brief Source: WLabel widget
  27. */
  28. #include <config.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_msg_t msg, int parm)
  44. {
  45. WLabel *l = (WLabel *) w;
  46. Dlg_head *h = l->widget.owner;
  47. switch (msg)
  48. {
  49. case WIDGET_INIT:
  50. return MSG_HANDLED;
  51. /* We don't want to get the focus */
  52. case WIDGET_FOCUS:
  53. return MSG_NOT_HANDLED;
  54. case WIDGET_DRAW:
  55. {
  56. char *p = l->text;
  57. int y = 0;
  58. gboolean disabled = (w->options & W_DISABLED) != 0;
  59. if (l->text == NULL)
  60. return MSG_HANDLED;
  61. if (l->transparent)
  62. tty_setcolor (disabled ? DISABLED_COLOR : DEFAULT_COLOR);
  63. else
  64. tty_setcolor (disabled ? DISABLED_COLOR : h->color[DLG_COLOR_NORMAL]);
  65. while (TRUE)
  66. {
  67. char *q;
  68. char c = '\0';
  69. q = strchr (p, '\n');
  70. if (q != NULL)
  71. {
  72. c = q[0];
  73. q[0] = '\0';
  74. }
  75. widget_move (&l->widget, y, 0);
  76. tty_print_string (str_fit_to_term (p, l->widget.cols, J_LEFT));
  77. if (q == NULL)
  78. break;
  79. q[0] = c;
  80. p = q + 1;
  81. y++;
  82. }
  83. return MSG_HANDLED;
  84. }
  85. case WIDGET_DESTROY:
  86. g_free (l->text);
  87. return MSG_HANDLED;
  88. default:
  89. return default_proc (msg, parm);
  90. }
  91. }
  92. /* --------------------------------------------------------------------------------------------- */
  93. /*** public functions ****************************************************************************/
  94. /* --------------------------------------------------------------------------------------------- */
  95. WLabel *
  96. label_new (int y, int x, const char *text)
  97. {
  98. WLabel *l;
  99. int cols = 1;
  100. int lines = 1;
  101. if (text != NULL)
  102. str_msg_term_size (text, &lines, &cols);
  103. l = g_new (WLabel, 1);
  104. init_widget (&l->widget, y, x, lines, cols, label_callback, NULL);
  105. l->text = g_strdup (text);
  106. l->auto_adjust_cols = TRUE;
  107. l->transparent = FALSE;
  108. widget_want_cursor (l->widget, FALSE);
  109. widget_want_hotkey (l->widget, FALSE);
  110. return l;
  111. }
  112. /* --------------------------------------------------------------------------------------------- */
  113. void
  114. label_set_text (WLabel * label, const char *text)
  115. {
  116. int newcols = label->widget.cols;
  117. int newlines;
  118. if (label->text != NULL && text != NULL && strcmp (label->text, text) == 0)
  119. return; /* Flickering is not nice */
  120. g_free (label->text);
  121. if (text == NULL)
  122. label->text = NULL;
  123. else
  124. {
  125. label->text = g_strdup (text);
  126. if (label->auto_adjust_cols)
  127. {
  128. str_msg_term_size (text, &newlines, &newcols);
  129. if (newcols > label->widget.cols)
  130. label->widget.cols = newcols;
  131. if (newlines > label->widget.lines)
  132. label->widget.lines = newlines;
  133. }
  134. }
  135. if (label->widget.owner != NULL)
  136. label_callback ((Widget *) label, WIDGET_DRAW, 0);
  137. if (newcols < label->widget.cols)
  138. label->widget.cols = newcols;
  139. }
  140. /* --------------------------------------------------------------------------------------------- */