1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
/*
Widgets for the Midnight Commander
Copyright (C) 1994-2022
Free Software Foundation, Inc.
Authors:
Radek Doulik, 1994, 1995
Miguel de Icaza, 1994, 1995
Jakub Jelinek, 1995
Andrej Borsenkow, 1996
Norbert Warmuth, 1997
Andrew Borodin <aborodin@vmail.ru>, 2009-2022
This file is part of the Midnight Commander.
The Midnight Commander is free software: you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation, either version 3 of the License,
or (at your option) any later version.
The Midnight Commander is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/** \file label.c
* \brief Source: WLabel widget
*/
#include <config.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include "lib/global.h"
#include "lib/tty/tty.h"
#include "lib/tty/color.h"
#include "lib/skin.h"
#include "lib/strutil.h"
#include "lib/widget.h"
/*** global variables ****************************************************************************/
/*** file scope macro definitions ****************************************************************/
/*** file scope type declarations ****************************************************************/
/*** file scope variables ************************************************************************/
/*** file scope functions ************************************************************************/
static cb_ret_t
label_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
{
WLabel *l = LABEL (w);
switch (msg)
{
case MSG_DRAW:
{
char *p = l->text;
int y = 0;
gboolean disabled;
align_crt_t align;
if (l->text == NULL)
return MSG_HANDLED;
disabled = widget_get_state (w, WST_DISABLED);
if (l->transparent)
tty_setcolor (disabled ? DISABLED_COLOR : DEFAULT_COLOR);
else
{
const int *colors;
colors = widget_get_colors (w);
tty_setcolor (disabled ? DISABLED_COLOR : colors[DLG_COLOR_NORMAL]);
}
align = (w->pos_flags & WPOS_CENTER_HORZ) != 0 ? J_CENTER_LEFT : J_LEFT;
while (TRUE)
{
char *q;
char c = '\0';
q = strchr (p, '\n');
if (q != NULL)
{
c = q[0];
q[0] = '\0';
}
widget_gotoyx (w, y, 0);
tty_print_string (str_fit_to_term (p, w->rect.cols, align));
if (q == NULL)
break;
q[0] = c;
p = q + 1;
y++;
}
return MSG_HANDLED;
}
case MSG_DESTROY:
g_free (l->text);
return MSG_HANDLED;
default:
return widget_default_callback (w, sender, msg, parm, data);
}
}
/* --------------------------------------------------------------------------------------------- */
/*** public functions ****************************************************************************/
/* --------------------------------------------------------------------------------------------- */
WLabel *
label_new (int y, int x, const char *text)
{
WRect r = { y, x, 1, 1 };
WLabel *l;
Widget *w;
if (text != NULL)
str_msg_term_size (text, &r.lines, &r.cols);
l = g_new (WLabel, 1);
w = WIDGET (l);
widget_init (w, &r, label_callback, NULL);
l->text = g_strdup (text);
l->auto_adjust_cols = TRUE;
l->transparent = FALSE;
return l;
}
/* --------------------------------------------------------------------------------------------- */
void
label_set_text (WLabel * label, const char *text)
{
Widget *w = WIDGET (label);
int newcols = w->rect.cols;
int newlines;
if (label->text != NULL && text != NULL && strcmp (label->text, text) == 0)
return; /* Flickering is not nice */
g_free (label->text);
if (text == NULL)
label->text = NULL;
else
{
label->text = g_strdup (text);
if (label->auto_adjust_cols)
{
str_msg_term_size (text, &newlines, &newcols);
w->rect.cols = MAX (newcols, w->rect.cols);
w->rect.lines = MAX (newlines, w->rect.lines);
}
}
widget_draw (w);
w->rect.cols = MIN (newcols, w->rect.cols);
}
/* --------------------------------------------------------------------------------------------- */
void
label_set_textv (WLabel * label, const char *format, ...)
{
va_list args;
char buf[BUF_1K]; /* FIXME: is it enough? */
va_start (args, format);
g_vsnprintf (buf, sizeof (buf), format, args);
va_end (args);
label_set_text (label, buf);
}
/* --------------------------------------------------------------------------------------------- */
|