Ticket #2991: fix redraw of WLabel after text change.
[midnight-commander.git] / lib / widget / label.c
blob9201f202e954e6ea057ee95ce0ade97219db0e9f
1 /*
2 Widgets for the Midnight Commander
4 Copyright (C) 1994, 1995, 1996, 1998, 1999, 2000, 2001, 2002, 2003,
5 2004, 2005, 2006, 2007, 2009, 2010, 2011
6 The Free Software Foundation, Inc.
8 Authors:
9 Radek Doulik, 1994, 1995
10 Miguel de Icaza, 1994, 1995
11 Jakub Jelinek, 1995
12 Andrej Borsenkow, 1996
13 Norbert Warmuth, 1997
14 Andrew Borodin <aborodin@vmail.ru>, 2009, 2010
16 This file is part of the Midnight Commander.
18 The Midnight Commander is free software: you can redistribute it
19 and/or modify it under the terms of the GNU General Public License as
20 published by the Free Software Foundation, either version 3 of the License,
21 or (at your option) any later version.
23 The Midnight Commander is distributed in the hope that it will be useful,
24 but WITHOUT ANY WARRANTY; without even the implied warranty of
25 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 GNU General Public License for more details.
28 You should have received a copy of the GNU General Public License
29 along with this program. If not, see <http://www.gnu.org/licenses/>.
32 /** \file label.c
33 * \brief Source: WLabel widget
36 #include <config.h>
38 #include <stdlib.h>
39 #include <string.h>
41 #include "lib/global.h"
43 #include "lib/tty/tty.h"
44 #include "lib/tty/color.h"
45 #include "lib/skin.h"
46 #include "lib/strutil.h"
47 #include "lib/widget.h"
49 /*** global variables ****************************************************************************/
51 /*** file scope macro definitions ****************************************************************/
53 /*** file scope type declarations ****************************************************************/
55 /*** file scope variables ************************************************************************/
57 /*** file scope functions ************************************************************************/
59 static cb_ret_t
60 label_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
62 WLabel *l = LABEL (w);
63 WDialog *h = w->owner;
65 switch (msg)
67 case MSG_INIT:
68 return MSG_HANDLED;
70 /* We don't want to get the focus */
71 case MSG_FOCUS:
72 return MSG_NOT_HANDLED;
74 case MSG_DRAW:
76 char *p = l->text;
77 int y = 0;
78 gboolean disabled;
79 align_crt_t align;
81 if (l->text == NULL)
82 return MSG_HANDLED;
84 disabled = (w->options & W_DISABLED) != 0;
86 if (l->transparent)
87 tty_setcolor (disabled ? DISABLED_COLOR : DEFAULT_COLOR);
88 else
89 tty_setcolor (disabled ? DISABLED_COLOR : h->color[DLG_COLOR_NORMAL]);
91 align = (w->pos_flags & WPOS_CENTER_HORZ) != 0 ? J_CENTER_LEFT : J_LEFT;
93 while (TRUE)
95 char *q;
96 char c = '\0';
99 q = strchr (p, '\n');
100 if (q != NULL)
102 c = q[0];
103 q[0] = '\0';
106 widget_move (w, y, 0);
107 tty_print_string (str_fit_to_term (p, w->cols, align));
109 if (q == NULL)
110 break;
112 q[0] = c;
113 p = q + 1;
114 y++;
116 return MSG_HANDLED;
119 case MSG_DESTROY:
120 g_free (l->text);
121 return MSG_HANDLED;
123 default:
124 return widget_default_callback (w, sender, msg, parm, data);
128 /* --------------------------------------------------------------------------------------------- */
129 /*** public functions ****************************************************************************/
130 /* --------------------------------------------------------------------------------------------- */
132 WLabel *
133 label_new (int y, int x, const char *text)
135 WLabel *l;
136 Widget *w;
137 int cols = 1;
138 int lines = 1;
140 if (text != NULL)
141 str_msg_term_size (text, &lines, &cols);
143 l = g_new (WLabel, 1);
144 w = WIDGET (l);
145 init_widget (w, y, x, lines, cols, label_callback, NULL);
147 l->text = g_strdup (text);
148 l->auto_adjust_cols = TRUE;
149 l->transparent = FALSE;
150 widget_want_cursor (w, FALSE);
151 widget_want_hotkey (w, FALSE);
153 return l;
156 /* --------------------------------------------------------------------------------------------- */
158 void
159 label_set_text (WLabel * label, const char *text)
161 Widget *w = WIDGET (label);
162 int newcols = w->cols;
163 int newlines;
165 if (label->text != NULL && text != NULL && strcmp (label->text, text) == 0)
166 return; /* Flickering is not nice */
168 g_free (label->text);
170 if (text == NULL)
171 label->text = NULL;
172 else
174 label->text = g_strdup (text);
175 if (label->auto_adjust_cols)
177 str_msg_term_size (text, &newlines, &newcols);
178 if (newcols > w->cols)
179 w->cols = newcols;
180 if (newlines > w->lines)
181 w->lines = newlines;
185 widget_redraw (w);
187 if (newcols < w->cols)
188 w->cols = newcols;
191 /* --------------------------------------------------------------------------------------------- */