This source file includes following definitions.
- label_callback
- label_new
- label_set_text
- label_set_textv
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 #include <config.h>
36
37 #include <stdarg.h>
38 #include <stdlib.h>
39 #include <string.h>
40
41 #include "lib/global.h"
42
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"
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63 static cb_ret_t
64 label_callback (Widget *w, Widget *sender, widget_msg_t msg, int parm, void *data)
65 {
66 WLabel *l = LABEL (w);
67
68 switch (msg)
69 {
70 case MSG_DRAW:
71 {
72 char *p = l->text;
73 int y = 0;
74 gboolean disabled;
75 align_crt_t align;
76
77 if (l->text == NULL)
78 return MSG_HANDLED;
79
80 disabled = widget_get_state (w, WST_DISABLED);
81
82 if (l->transparent)
83 tty_setcolor (disabled ? DISABLED_COLOR : DEFAULT_COLOR);
84 else
85 {
86 const int *colors;
87
88 colors = widget_get_colors (w);
89 tty_setcolor (disabled ? DISABLED_COLOR : colors[DLG_COLOR_NORMAL]);
90 }
91
92 align = (w->pos_flags & WPOS_CENTER_HORZ) != 0 ? J_CENTER_LEFT : J_LEFT;
93
94 while (TRUE)
95 {
96 char *q;
97 char c = '\0';
98
99
100 q = strchr (p, '\n');
101 if (q != NULL)
102 {
103 c = q[0];
104 q[0] = '\0';
105 }
106
107 widget_gotoyx (w, y, 0);
108 tty_print_string (str_fit_to_term (p, w->rect.cols, align));
109
110 if (q == NULL)
111 break;
112
113 q[0] = c;
114 p = q + 1;
115 y++;
116 }
117 return MSG_HANDLED;
118 }
119
120 case MSG_DESTROY:
121 g_free (l->text);
122 return MSG_HANDLED;
123
124 default:
125 return widget_default_callback (w, sender, msg, parm, data);
126 }
127 }
128
129
130
131
132
133 WLabel *
134 label_new (int y, int x, const char *text)
135 {
136 WRect r = { y, x, 1, 1 };
137 WLabel *l;
138 Widget *w;
139
140 if (text != NULL)
141 str_msg_term_size (text, &r.lines, &r.cols);
142
143 l = g_new (WLabel, 1);
144 w = WIDGET (l);
145 widget_init (w, &r, label_callback, NULL);
146
147 l->text = g_strdup (text);
148 l->auto_adjust_cols = TRUE;
149 l->transparent = FALSE;
150
151 return l;
152 }
153
154
155
156 void
157 label_set_text (WLabel *label, const char *text)
158 {
159 Widget *w = WIDGET (label);
160 int newcols = w->rect.cols;
161 int newlines;
162
163 if (label->text != NULL && text != NULL && strcmp (label->text, text) == 0)
164 return;
165
166 g_free (label->text);
167
168 if (text == NULL)
169 label->text = NULL;
170 else
171 {
172 label->text = g_strdup (text);
173 if (label->auto_adjust_cols)
174 {
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);
178 }
179 }
180
181 widget_draw (w);
182
183 w->rect.cols = MIN (newcols, w->rect.cols);
184 }
185
186
187
188 void
189 label_set_textv (WLabel *label, const char *format, ...)
190 {
191 va_list args;
192 char buf[BUF_1K];
193
194 va_start (args, format);
195 g_vsnprintf (buf, sizeof (buf), format, args);
196 va_end (args);
197
198 label_set_text (label, buf);
199 }
200
201