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
|
#include <cairo/cairo.h>
#include <glib.h>
#include <pango/pangocairo.h>
#include <pango/pango.h>
#include <wchar.h>
#include "entry.h"
#include "log.h"
#include "nelem.h"
void entry_init(struct entry *entry, uint32_t width, uint32_t height, uint32_t scale)
{
entry->image.width = width;
entry->image.height = height;
width /= scale;
height /= scale;
/*
* Create the cairo surface and context we'll be using.
*/
cairo_surface_t *surface = cairo_image_surface_create(
CAIRO_FORMAT_ARGB32,
width * scale,
height * scale
);
cairo_surface_set_device_scale(surface, scale, scale);
cairo_t *cr = cairo_create(surface);
/* Draw the outer outline */
struct color color = entry->border.outline_color;
cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a);
cairo_paint(cr);
/* Move and clip following draws to be within this outline */
cairo_translate(
cr,
entry->border.outline_width,
entry->border.outline_width);
width -= 2 * entry->border.outline_width;
height -= 2 * entry->border.outline_width;
cairo_rectangle(cr, 0, 0, width, height);
cairo_clip(cr);
/* Draw the border */
color = entry->border.color;
cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a);
cairo_paint(cr);
/* Move and clip following draws to be within the border */
cairo_translate(cr, entry->border.width, entry->border.width);
width -= 2 * entry->border.width;
height -= 2 * entry->border.width;
cairo_rectangle(cr, 0, 0, width, height);
cairo_clip(cr);
/* Draw the inner outline */
color = entry->border.outline_color;
cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a);
cairo_paint(cr);
/* Move and clip following draws to be within this outline */
cairo_translate(
cr,
entry->border.outline_width,
entry->border.outline_width);
width -= 2 * entry->border.outline_width;
height -= 2 * entry->border.outline_width;
cairo_rectangle(cr, 0, 0, width, height);
cairo_clip(cr);
/* Draw the entry background */
color = entry->background_color;
cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a);
cairo_paint(cr);
/* Move and clip following draws to be within the specified padding */
cairo_translate(cr, entry->padding, entry->padding);
width -= 2 * entry->padding;
height -= 2 * entry->padding;
cairo_rectangle(cr, 0, 0, width, height);
cairo_clip(cr);
/* Setup Pango. */
log_debug("Creating Pango context.\n");
PangoContext *context = pango_cairo_create_context(cr);
log_debug("Creating Pango font description.\n");
PangoFontDescription *font_description =
pango_font_description_from_string(entry->font_name);
pango_font_description_set_size(
font_description,
entry->font_size * PANGO_SCALE);
pango_context_set_font_description(context, font_description);
pango_font_description_free(font_description);
entry->pango.prompt_layout = pango_layout_new(context);
log_debug("Setting Pango text.\n");
pango_layout_set_text(entry->pango.prompt_layout, "run: ", -1);
int prompt_width;
int prompt_height;
log_debug("Get Pango pixel size.\n");
pango_layout_get_pixel_size(entry->pango.prompt_layout, &prompt_width, &prompt_height);
log_debug("First Pango draw.\n");
pango_cairo_update_layout(cr, entry->pango.prompt_layout);
/* Draw the prompt now, as this only needs to be done once */
color = entry->foreground_color;
cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a);
pango_cairo_show_layout(cr, entry->pango.prompt_layout);
/* Move and clip so we don't draw over the prompt */
cairo_translate(cr, prompt_width, 0);
width -= prompt_width;
cairo_rectangle(cr, 0, 0, width, height);
cairo_clip(cr);
log_debug("Creating Pango layout.\n");
entry->pango.entry_layout = pango_layout_new(context);
pango_layout_set_text(entry->pango.entry_layout, "", -1);
for (size_t i = 0; i < 5; i++) {
PangoLayout *layout = pango_layout_new(context);
pango_layout_set_text(layout, "", -1);
entry->pango.result_layouts[i] = layout;
}
entry->pango.context = context;
entry->cairo.surface = surface;
entry->cairo.cr = cr;
entry->image.buffer = cairo_image_surface_get_data(surface);
}
void entry_destroy(struct entry *entry)
{
for (size_t i = 0; i < 5; i++) {
g_object_unref(entry->pango.result_layouts[i]);
}
g_object_unref(entry->pango.entry_layout);
g_object_unref(entry->pango.prompt_layout);
g_object_unref(entry->pango.context);
cairo_destroy(entry->cairo.cr);
cairo_surface_destroy(entry->cairo.surface);
}
void entry_update(struct entry *entry)
{
cairo_t *cr = entry->cairo.cr;
cairo_save(cr);
entry->image.redraw = true;
/* Clear the image. */
struct color color = entry->background_color;
cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a);
cairo_paint(cr);
/* Draw our text. */
color = entry->foreground_color;
cairo_set_source_rgba(cr, color.r, color.g, color.b, color.a);
pango_layout_set_text(entry->pango.entry_layout, entry->input_mb, -1);
pango_cairo_update_layout(cr, entry->pango.entry_layout);
pango_cairo_show_layout(cr, entry->pango.entry_layout);
for (size_t i = 0; i < 5; i++) {
cairo_translate(cr, 0, 50);
PangoLayout *layout = entry->pango.result_layouts[i];
const char *str;
if (i < entry->results.count) {
str = entry->results.buf[i];
} else {
str = "";
}
pango_layout_set_text(layout, str, -1);
pango_cairo_update_layout(cr, layout);
pango_cairo_show_layout(cr, layout);
}
cairo_restore(cr);
}
void entry_set_scale(struct entry *entry, uint32_t scale)
{
cairo_surface_set_device_scale(entry->cairo.surface, scale, scale);
}
|