Merge branch '4549_subshell_execl_argv0'
[midnight-commander.git] / lib / widget / label.c
blobb9fb6b61168f0bd5b2e7bc8d484ca85e9e84a1b6
1 /*
2 Widgets for the Midnight Commander
4 Copyright (C) 1994-2024
5 Free Software Foundation, Inc.
7 Authors:
8 Radek Doulik, 1994, 1995
9 Miguel de Icaza, 1994, 1995
10 Jakub Jelinek, 1995
11 Andrej Borsenkow, 1996
12 Norbert Warmuth, 1997
13 Andrew Borodin <aborodin@vmail.ru>, 2009-2022
15 This file is part of the Midnight Commander.
17 The Midnight Commander is free software: you can redistribute it
18 and/or modify it under the terms of the GNU General Public License as
19 published by the Free Software Foundation, either version 3 of the License,
20 or (at your option) any later version.
22 The Midnight Commander is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
27 You should have received a copy of the GNU General Public License
28 along with this program. If not, see <http://www.gnu.org/licenses/>.
31 /** \file label.c
32 * \brief Source: WLabel widget
35 #include <config.h>
37 #include <stdarg.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 /*** forward declarations (file scope functions) *************************************************/
57 /*** file scope variables ************************************************************************/
59 /* --------------------------------------------------------------------------------------------- */
60 /*** file scope functions ************************************************************************/
61 /* --------------------------------------------------------------------------------------------- */
63 static cb_ret_t
64 label_callback (Widget *w, Widget *sender, widget_msg_t msg, int parm, void *data)
66 WLabel *l = LABEL (w);
68 switch (msg)
70 case MSG_DRAW:
72 char *p = l->text;
73 int y = 0;
74 gboolean disabled;
75 align_crt_t align;
77 if (l->text == NULL)
78 return MSG_HANDLED;
80 disabled = widget_get_state (w, WST_DISABLED);
82 if (l->transparent)
83 tty_setcolor (disabled ? DISABLED_COLOR : DEFAULT_COLOR);
84 else
86 const int *colors;
88 colors = widget_get_colors (w);
89 tty_setcolor (disabled ? DISABLED_COLOR : colors[DLG_COLOR_NORMAL]);
92 align = (w->pos_flags & WPOS_CENTER_HORZ) != 0 ? J_CENTER_LEFT : J_LEFT;
94 while (TRUE)
96 char *q;
97 char c = '\0';
100 q = strchr (p, '\n');
101 if (q != NULL)
103 c = q[0];
104 q[0] = '\0';
107 widget_gotoyx (w, y, 0);
108 tty_print_string (str_fit_to_term (p, w->rect.cols, align));
110 if (q == NULL)
111 break;
113 q[0] = c;
114 p = q + 1;
115 y++;
117 return MSG_HANDLED;
120 case MSG_DESTROY:
121 g_free (l->text);
122 return MSG_HANDLED;
124 default:
125 return widget_default_callback (w, sender, msg, parm, data);
129 /* --------------------------------------------------------------------------------------------- */
130 /*** public functions ****************************************************************************/
131 /* --------------------------------------------------------------------------------------------- */
133 WLabel *
134 label_new (int y, int x, const char *text)
136 WRect r = { y, x, 1, 1 };
137 WLabel *l;
138 Widget *w;
140 if (text != NULL)
141 str_msg_term_size (text, &r.lines, &r.cols);
143 l = g_new (WLabel, 1);
144 w = WIDGET (l);
145 widget_init (w, &r, label_callback, NULL);
147 l->text = g_strdup (text);
148 l->auto_adjust_cols = TRUE;
149 l->transparent = FALSE;
151 return l;
154 /* --------------------------------------------------------------------------------------------- */
156 void
157 label_set_text (WLabel *label, const char *text)
159 Widget *w = WIDGET (label);
160 int newcols = w->rect.cols;
161 int newlines;
163 if (label->text != NULL && text != NULL && strcmp (label->text, text) == 0)
164 return; /* Flickering is not nice */
166 g_free (label->text);
168 if (text == NULL)
169 label->text = NULL;
170 else
172 label->text = g_strdup (text);
173 if (label->auto_adjust_cols)
175 str_msg_term_size (text, &newlines, &newcols);
176 w->rect.cols = MAX (newcols, w->rect.cols);
177 w->rect.lines = MAX (newlines, w->rect.lines);
181 widget_draw (w);
183 w->rect.cols = MIN (newcols, w->rect.cols);
186 /* --------------------------------------------------------------------------------------------- */
188 void
189 label_set_textv (WLabel *label, const char *format, ...)
191 va_list args;
192 char buf[BUF_1K]; /* FIXME: is it enough? */
194 va_start (args, format);
195 g_vsnprintf (buf, sizeof (buf), format, args);
196 va_end (args);
198 label_set_text (label, buf);
201 /* --------------------------------------------------------------------------------------------- */