label.c 5.0 KB

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