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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
|
/*
* Copyright (C) 2012-2023 Robin Haberkorn
*
* This program 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.
*
* This program 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/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#include <glib.h>
#include <glib/gprintf.h>
#include <gdk/gdk.h>
#include <gtk/gtk.h>
#include "sciteco.h"
#include "string-utils.h"
#include "gtk-label.h"
#define GDK_TO_PANGO_COLOR(X) ((guint16)((X) * G_MAXUINT16))
struct _TecoGtkLabel {
GtkLabel parent_instance;
PangoColor fg, bg;
guint16 fg_alpha, bg_alpha;
teco_string_t string;
};
G_DEFINE_TYPE(TecoGtkLabel, teco_gtk_label, GTK_TYPE_LABEL)
/** Overrides GObject::finalize() (object destructor) */
static void
teco_gtk_label_finalize(GObject *obj_self)
{
TecoGtkLabel *self = TECO_GTK_LABEL(obj_self);
teco_string_clear(&self->string);
/* chain up to parent class */
G_OBJECT_CLASS(teco_gtk_label_parent_class)->finalize(obj_self);
}
/** Overrides GtkWidget::style_updated() */
static void
teco_gtk_label_style_updated(GtkWidget *widget)
{
TecoGtkLabel *self = TECO_GTK_LABEL(widget);
/* chain to parent class */
GTK_WIDGET_CLASS(teco_gtk_label_parent_class)->style_updated(widget);
GtkStyleContext *style = gtk_widget_get_style_context(widget);
GdkRGBA normal_color;
gtk_style_context_get_color(style, GTK_STATE_NORMAL, &normal_color);
self->bg.red = GDK_TO_PANGO_COLOR(normal_color.red);
self->bg.green = GDK_TO_PANGO_COLOR(normal_color.green);
self->bg.blue = GDK_TO_PANGO_COLOR(normal_color.blue);
self->bg_alpha = GDK_TO_PANGO_COLOR(normal_color.alpha);
/*
* If Pango does not support transparent foregrounds,
* it will at least use a high-contrast foreground.
*
* NOTE: It would be very hard to get an appropriate background
* color even if Gtk supports it since the label itself may
* not have one but one of its parents.
*
* FIXME: We may want to honour the background color,
* so we can at least get decent reverse text when setting
* the background color in the CSS.
*/
self->fg.red = normal_color.red > 0.5 ? 0 : G_MAXUINT16;
self->fg.green = normal_color.green > 0.5 ? 0 : G_MAXUINT16;
self->fg.blue = normal_color.blue > 0.5 ? 0 : G_MAXUINT16;
/* try hard to get a transparent foreground anyway */
self->fg_alpha = 0;
/*
* The widget might be styled after the text has been set on it,
* we must recreate the Pango attributes.
*/
if (self->string.len > 0) {
PangoAttrList *attribs = NULL;
g_autofree gchar *plaintext = NULL;
teco_gtk_label_parse_string(self->string.data, self->string.len,
&self->fg, self->fg_alpha,
&self->bg, self->bg_alpha,
&attribs, &plaintext);
gtk_label_set_attributes(GTK_LABEL(self), attribs);
pango_attr_list_unref(attribs);
g_assert(!g_strcmp0(plaintext, gtk_label_get_text(GTK_LABEL(self))));
}
}
static void
teco_gtk_label_class_init(TecoGtkLabelClass *klass)
{
GTK_WIDGET_CLASS(klass)->style_updated = teco_gtk_label_style_updated;
G_OBJECT_CLASS(klass)->finalize = teco_gtk_label_finalize;
}
static void teco_gtk_label_init(TecoGtkLabel *self) {}
GtkWidget *
teco_gtk_label_new(const gchar *str, gssize len)
{
TecoGtkLabel *widget = TECO_GTK_LABEL(g_object_new(TECO_TYPE_GTK_LABEL, NULL));
teco_gtk_label_set_text(widget, str, len);
return GTK_WIDGET(widget);
}
static void
teco_gtk_label_add_highlight_attribs(PangoAttrList *attribs, PangoColor *fg, guint16 fg_alpha,
PangoColor *bg, guint16 bg_alpha, guint index, gsize len)
{
PangoAttribute *attr;
/*
* NOTE: Transparent foreground do not seem to work,
* even in Pango v1.38.
* Perhaps, this has been fixed in later versions.
*/
#if PANGO_VERSION_CHECK(1,38,0)
attr = pango_attr_foreground_alpha_new(fg_alpha);
attr->start_index = index;
attr->end_index = index + len;
pango_attr_list_insert(attribs, attr);
attr = pango_attr_background_alpha_new(bg_alpha);
attr->start_index = index;
attr->end_index = index + len;
pango_attr_list_insert(attribs, attr);
#endif
attr = pango_attr_foreground_new(fg->red, fg->green, fg->blue);
attr->start_index = index;
attr->end_index = index + len;
pango_attr_list_insert(attribs, attr);
attr = pango_attr_background_new(bg->red, bg->green, bg->blue);
attr->start_index = index;
attr->end_index = index + len;
pango_attr_list_insert(attribs, attr);
}
void
teco_gtk_label_parse_string(const gchar *str, gssize len, PangoColor *fg, guint16 fg_alpha,
PangoColor *bg, guint16 bg_alpha, PangoAttrList **attribs, gchar **text)
{
if (len < 0)
len = strlen(str);
/*
* Approximate size of unformatted text.
*/
gsize text_len = 1; /* for trailing 0 */
for (gint i = 0; i < len; i++)
text_len += TECO_IS_CTL(str[i]) ? 3 : 1;
*attribs = pango_attr_list_new();
*text = g_malloc(text_len);
gint index = 0;
while (len > 0) {
/*
* NOTE: This mapping is similar to
* teco_view_set_presentations()
*/
switch (*str) {
case '\e':
teco_gtk_label_add_highlight_attribs(*attribs,
fg, fg_alpha,
bg, bg_alpha,
index, 1);
(*text)[index++] = '$';
break;
case '\r':
teco_gtk_label_add_highlight_attribs(*attribs,
fg, fg_alpha,
bg, bg_alpha,
index, 2);
(*text)[index++] = 'C';
(*text)[index++] = 'R';
break;
case '\n':
teco_gtk_label_add_highlight_attribs(*attribs,
fg, fg_alpha,
bg, bg_alpha,
index, 2);
(*text)[index++] = 'L';
(*text)[index++] = 'F';
break;
case '\t':
teco_gtk_label_add_highlight_attribs(*attribs,
fg, fg_alpha,
bg, bg_alpha,
index, 3);
(*text)[index++] = 'T';
(*text)[index++] = 'A';
(*text)[index++] = 'B';
break;
default:
if (TECO_IS_CTL(*str)) {
teco_gtk_label_add_highlight_attribs(*attribs,
fg, fg_alpha,
bg, bg_alpha,
index, 2);
(*text)[index++] = '^';
(*text)[index++] = TECO_CTL_ECHO(*str);
} else {
(*text)[index++] = *str;
}
break;
}
str++;
len--;
}
/* null-terminate generated text */
(*text)[index] = '\0';
}
void
teco_gtk_label_set_text(TecoGtkLabel *self, const gchar *str, gssize len)
{
g_return_if_fail(self != NULL);
g_return_if_fail(TECO_IS_GTK_LABEL(self));
teco_string_clear(&self->string);
teco_string_init(&self->string, str, len < 0 ? strlen(str) : len);
g_autofree gchar *plaintext = NULL;
if (self->string.len > 0) {
PangoAttrList *attribs = NULL;
teco_gtk_label_parse_string(self->string.data, self->string.len,
&self->fg, self->fg_alpha,
&self->bg, self->bg_alpha,
&attribs, &plaintext);
gtk_label_set_attributes(GTK_LABEL(self), attribs);
pango_attr_list_unref(attribs);
}
gtk_label_set_text(GTK_LABEL(self), plaintext);
}
|