| 1 |
#ifndef _BM_CAIRO_H_ |
| 2 |
#define _BM_CAIRO_H_ |
| 3 |
|
| 4 |
#include "internal.h" |
| 5 |
#include <string.h> |
| 6 |
#include <assert.h> |
| 7 |
#include <math.h> |
| 8 |
#include <cairo.h> |
| 9 |
#include <pango/pangocairo.h> |
| 10 |
|
| 11 |
struct cairo { |
| 12 |
cairo_t *cr; |
| 13 |
cairo_surface_t *surface; |
| 14 |
PangoContext *pango; |
| 15 |
double scale; |
| 16 |
bool antialiasing; |
| 17 |
}; |
| 18 |
|
| 19 |
struct cairo_color { |
| 20 |
float r, g, b, a; |
| 21 |
}; |
| 22 |
|
| 23 |
struct cairo_paint { |
| 24 |
struct cairo_color fg; |
| 25 |
struct cairo_color bg; |
| 26 |
const char *font; |
| 27 |
int32_t baseline; |
| 28 |
uint32_t cursor; |
| 29 |
uint32_t cursor_height; |
| 30 |
uint32_t cursor_width; |
| 31 |
struct cairo_color cursor_fg; |
| 32 |
struct cairo_color cursor_bg; |
| 33 |
uint32_t hpadding; |
| 34 |
bool draw_cursor; |
| 35 |
|
| 36 |
struct box { |
| 37 |
int32_t lx, rx; // left/right offset (pos.x - lx, box.w + rx) |
| 38 |
int32_t ty, by; // top/bottom offset (pos.y - ty, box.h + by) |
| 39 |
int32_t w, h; // 0 for text width/height |
| 40 |
} box; |
| 41 |
|
| 42 |
struct pos { |
| 43 |
int32_t x, y; |
| 44 |
} pos; |
| 45 |
}; |
| 46 |
|
| 47 |
struct cairo_result { |
| 48 |
uint32_t x_advance; |
| 49 |
uint32_t height; |
| 50 |
uint32_t baseline; |
| 51 |
}; |
| 52 |
|
| 53 |
struct cairo_paint_result { |
| 54 |
uint32_t displayed; |
| 55 |
uint32_t height; |
| 56 |
}; |
| 57 |
|
| 58 |
static size_t blen = 0; |
| 59 |
static char *buffer = NULL; |
| 60 |
|
| 61 |
static inline bool |
| 62 |
bm_cairo_create_for_surface(struct cairo *cairo, cairo_surface_t *surface) |
| 63 |
{ |
| 64 |
assert(cairo && surface); |
| 65 |
if (!(cairo->cr = cairo_create(surface))) |
| 66 |
goto fail; |
| 67 |
|
| 68 |
if (!(cairo->pango = pango_cairo_create_context(cairo->cr))) |
| 69 |
goto fail; |
| 70 |
|
| 71 |
if (!cairo->antialiasing) |
| 72 |
cairo_set_antialias(cairo->cr, CAIRO_ANTIALIAS_NONE); |
| 73 |
|
| 74 |
cairo->surface = surface; |
| 75 |
assert(cairo->scale > 0); |
| 76 |
cairo_surface_set_device_scale(surface, cairo->scale, cairo->scale); |
| 77 |
return true; |
| 78 |
|
| 79 |
fail: |
| 80 |
if (cairo->cr) |
| 81 |
cairo_destroy(cairo->cr); |
| 82 |
return false; |
| 83 |
} |
| 84 |
|
| 85 |
static inline void |
| 86 |
bm_cairo_destroy(struct cairo *cairo) |
| 87 |
{ |
| 88 |
if (cairo->cr) |
| 89 |
cairo_destroy(cairo->cr); |
| 90 |
if (cairo->pango) |
| 91 |
g_object_unref(cairo->pango); |
| 92 |
if (cairo->surface) |
| 93 |
cairo_surface_destroy(cairo->surface); |
| 94 |
} |
| 95 |
|
| 96 |
static inline PangoLayout* |
| 97 |
bm_pango_get_layout(struct cairo *cairo, struct cairo_paint *paint, const char *buffer) |
| 98 |
{ |
| 99 |
PangoLayout *layout = pango_cairo_create_layout(cairo->cr); |
| 100 |
pango_layout_set_text(layout, buffer, -1); |
| 101 |
PangoFontDescription *desc = pango_font_description_from_string(paint->font); |
| 102 |
pango_layout_set_font_description(layout, desc); |
| 103 |
pango_layout_set_single_paragraph_mode(layout, 1); |
| 104 |
pango_font_description_free(desc); |
| 105 |
return layout; |
| 106 |
} |
| 107 |
|
| 108 |
BM_LOG_ATTR(4, 5) static inline bool |
| 109 |
bm_pango_get_text_extents(struct cairo *cairo, struct cairo_paint *paint, struct cairo_result *result, const char *fmt, ...) |
| 110 |
{ |
| 111 |
assert(cairo && paint && result && fmt); |
| 112 |
memset(result, 0, sizeof(struct cairo_result)); |
| 113 |
|
| 114 |
va_list args; |
| 115 |
va_start(args, fmt); |
| 116 |
bool ret = bm_vrprintf(&buffer, &blen, fmt, args); |
| 117 |
va_end(args); |
| 118 |
|
| 119 |
if (!ret) |
| 120 |
return false; |
| 121 |
|
| 122 |
PangoRectangle rect; |
| 123 |
PangoLayout *layout = bm_pango_get_layout(cairo, paint, buffer); |
| 124 |
pango_layout_get_pixel_extents(layout, NULL, &rect); |
| 125 |
int baseline = pango_layout_get_baseline(layout) / PANGO_SCALE; |
| 126 |
g_object_unref(layout); |
| 127 |
|
| 128 |
result->x_advance = rect.x + rect.width; |
| 129 |
result->height = rect.height; |
| 130 |
result->baseline = baseline; |
| 131 |
return true; |
| 132 |
} |
| 133 |
|
| 134 |
static inline bool |
| 135 |
bm_cairo_draw_line_str(struct cairo *cairo, struct cairo_paint *paint, struct cairo_result *result, const char *buffer) |
| 136 |
{ |
| 137 |
cairo_save(cairo->cr); |
| 138 |
PangoLayout *layout = bm_pango_get_layout(cairo, paint, buffer); |
| 139 |
pango_cairo_update_layout(cairo->cr, layout); |
| 140 |
|
| 141 |
int width, height; |
| 142 |
pango_layout_get_pixel_size(layout, &width, &height); |
| 143 |
height = paint->box.h > 0 ? paint->box.h : height; |
| 144 |
int base = pango_layout_get_baseline(layout) / PANGO_SCALE; |
| 145 |
|
| 146 |
uint32_t line_height = height + paint->box.by + paint->box.ty; |
| 147 |
cairo_set_source_rgba(cairo->cr, paint->bg.r, paint->bg.b, paint->bg.g, paint->bg.a); |
| 148 |
cairo_rectangle(cairo->cr, |
| 149 |
paint->pos.x - paint->box.lx, paint->pos.y - paint->box.ty, |
| 150 |
(paint->box.w > 0 ? paint->box.w : width) + paint->box.rx + paint->box.lx, |
| 151 |
line_height); |
| 152 |
cairo_fill(cairo->cr); |
| 153 |
|
| 154 |
cairo_set_source_rgba(cairo->cr, paint->fg.r, paint->fg.b, paint->fg.g, paint->fg.a); |
| 155 |
cairo_move_to(cairo->cr, paint->box.lx + paint->pos.x, paint->pos.y - base + paint->baseline); |
| 156 |
pango_cairo_show_layout(cairo->cr, layout); |
| 157 |
|
| 158 |
if (paint->draw_cursor) { |
| 159 |
PangoRectangle rect; |
| 160 |
pango_layout_index_to_pos(layout, paint->cursor, &rect); |
| 161 |
|
| 162 |
if (!rect.width) { |
| 163 |
struct cairo_result result = {0}; |
| 164 |
bm_pango_get_text_extents(cairo, paint, &result, "#"); |
| 165 |
rect.width = result.x_advance * PANGO_SCALE; |
| 166 |
} |
| 167 |
|
| 168 |
uint32_t cursor_width = rect.width / PANGO_SCALE; |
| 169 |
if (paint->cursor_width > 0) { |
| 170 |
cursor_width = paint->cursor_width; |
| 171 |
} |
| 172 |
uint32_t cursor_height = fmin(paint->cursor_height == 0 ? line_height : paint->cursor_height, line_height); |
| 173 |
cairo_set_source_rgba(cairo->cr, paint->cursor_fg.r, paint->cursor_fg.b, paint->cursor_fg.g, paint->cursor_fg.a); |
| 174 |
cairo_rectangle(cairo->cr, |
| 175 |
paint->pos.x + paint->box.lx + rect.x / PANGO_SCALE, paint->pos.y - paint->box.ty + ((line_height - cursor_height) / 2), |
| 176 |
cursor_width, cursor_height); |
| 177 |
cairo_fill(cairo->cr); |
| 178 |
|
| 179 |
cairo_rectangle(cairo->cr, |
| 180 |
paint->pos.x + paint->box.lx + rect.x / PANGO_SCALE, paint->pos.y - paint->box.ty, |
| 181 |
cursor_width, line_height); |
| 182 |
cairo_clip(cairo->cr); |
| 183 |
|
| 184 |
cairo_set_source_rgba(cairo->cr, paint->cursor_bg.r, paint->cursor_bg.b, paint->cursor_bg.g, paint->cursor_bg.a); |
| 185 |
cairo_move_to(cairo->cr, paint->box.lx + paint->pos.x, paint->pos.y - base + paint->baseline); |
| 186 |
pango_cairo_show_layout(cairo->cr, layout); |
| 187 |
cairo_reset_clip(cairo->cr); |
| 188 |
} |
| 189 |
|
| 190 |
g_object_unref(layout); |
| 191 |
|
| 192 |
result->x_advance = width + paint->box.rx; |
| 193 |
result->height = line_height; |
| 194 |
|
| 195 |
cairo_identity_matrix(cairo->cr); |
| 196 |
cairo_restore(cairo->cr); |
| 197 |
return true; |
| 198 |
} |
| 199 |
|
| 200 |
BM_LOG_ATTR(4, 5) static inline bool |
| 201 |
bm_cairo_draw_line(struct cairo *cairo, struct cairo_paint *paint, struct cairo_result *result, const char *fmt, ...) |
| 202 |
{ |
| 203 |
assert(cairo && paint && result && fmt); |
| 204 |
memset(result, 0, sizeof(struct cairo_result)); |
| 205 |
|
| 206 |
va_list args; |
| 207 |
va_start(args, fmt); |
| 208 |
bool ret = bm_vrprintf(&buffer, &blen, fmt, args); |
| 209 |
va_end(args); |
| 210 |
|
| 211 |
if (!ret) |
| 212 |
return false; |
| 213 |
|
| 214 |
return bm_cairo_draw_line_str(cairo, paint, result, buffer); |
| 215 |
} |
| 216 |
|
| 217 |
static inline void |
| 218 |
bm_cairo_color_from_menu_color(const struct bm_menu *menu, enum bm_color color, struct cairo_color *c) |
| 219 |
{ |
| 220 |
assert(menu); |
| 221 |
c->r = (float)menu->colors[color].r / 255.0f; |
| 222 |
c->g = (float)menu->colors[color].g / 255.0f; |
| 223 |
c->b = (float)menu->colors[color].b / 255.0f; |
| 224 |
c->a = (float)menu->colors[color].a / 255.0f; |
| 225 |
} |
| 226 |
|
| 227 |
static char * |
| 228 |
bm_cairo_entry_message(char *entry_text, bool highlighted, uint32_t event_feedback, uint32_t index, uint32_t count) |
| 229 |
{ |
| 230 |
if (!highlighted || !event_feedback) { |
| 231 |
return entry_text ? entry_text : ""; |
| 232 |
} else { |
| 233 |
if (event_feedback & TOUCH_WILL_CANCEL) { |
| 234 |
return "Cancel…"; |
| 235 |
} |
| 236 |
if (event_feedback & TOUCH_WILL_SCROLL_FIRST) { |
| 237 |
if (index == 0) { |
| 238 |
return "Already on the first page…"; |
| 239 |
} else { |
| 240 |
return "First page…"; |
| 241 |
} |
| 242 |
} |
| 243 |
if (event_feedback & TOUCH_WILL_SCROLL_UP) { |
| 244 |
if (index == 0) { |
| 245 |
return "Already on the first page…"; |
| 246 |
} else { |
| 247 |
return "Previous page…"; |
| 248 |
} |
| 249 |
} |
| 250 |
if (event_feedback & TOUCH_WILL_SCROLL_DOWN) { |
| 251 |
if (index == count - 1) { |
| 252 |
return "Already on the last page…"; |
| 253 |
} else { |
| 254 |
return "Next page…"; |
| 255 |
} |
| 256 |
} |
| 257 |
if (event_feedback & TOUCH_WILL_SCROLL_LAST) { |
| 258 |
if (index == count - 1) { |
| 259 |
return "Already on the last page…"; |
| 260 |
} else { |
| 261 |
return "Last page…"; |
| 262 |
} |
| 263 |
} |
| 264 |
return "Not handled feedback…"; |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
static inline void |
| 269 |
bm_cairo_rounded_path(cairo_t *cr, double x, double y, double width, double height, double radius) |
| 270 |
{ |
| 271 |
double adjusted_radius = MIN(MIN(radius, height * 0.5), width * 0.5); /* Prevent border from intersecting itself. */ |
| 272 |
double degrees = M_PI / 180; |
| 273 |
cairo_new_sub_path(cr); |
| 274 |
cairo_arc(cr, x + width - adjusted_radius, y + adjusted_radius, adjusted_radius, -90 * degrees, 0 * degrees); |
| 275 |
cairo_arc(cr, x + width - adjusted_radius, y + height - adjusted_radius, adjusted_radius, 0 * degrees, 90 * degrees); |
| 276 |
cairo_arc(cr, x + adjusted_radius, y + height - adjusted_radius, adjusted_radius, 90 * degrees, 180 * degrees); |
| 277 |
cairo_arc(cr, x + adjusted_radius, y + adjusted_radius, adjusted_radius, 180 * degrees, 270 * degrees); |
| 278 |
cairo_close_path(cr); |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
static inline void |
| 283 |
bm_cairo_paint(struct cairo *cairo, uint32_t width, uint32_t max_height, struct bm_menu *menu, struct cairo_paint_result *out_result) |
| 284 |
{ |
| 285 |
assert(cairo && menu && out_result); |
| 286 |
|
| 287 |
max_height /= cairo->scale; |
| 288 |
|
| 289 |
memset(out_result, 0, sizeof(struct cairo_paint_result)); |
| 290 |
out_result->displayed = 1; |
| 291 |
|
| 292 |
struct cairo_paint paint = {0}; |
| 293 |
paint.font = menu->font.name; |
| 294 |
|
| 295 |
struct cairo_result result = {0}; |
| 296 |
int ascii_height; |
| 297 |
bm_pango_get_text_extents(cairo, &paint, &result, "!\"#$%%&'()*+,-./0123456789:;<=>?@ABCD" |
| 298 |
"EFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"); |
| 299 |
ascii_height = result.height; |
| 300 |
paint.baseline = result.baseline; |
| 301 |
|
| 302 |
double border_size = menu->border_size; |
| 303 |
width -= border_size; |
| 304 |
uint32_t height = fmin(fmax(menu->line_height, ascii_height), max_height); |
| 305 |
uint32_t vpadding = (height - ascii_height)/2; |
| 306 |
|
| 307 |
double border_radius = menu->border_radius; |
| 308 |
|
| 309 |
uint32_t total_item_count = menu->items.count; |
| 310 |
uint32_t filtered_item_count; |
| 311 |
bm_menu_get_filtered_items(menu, &filtered_item_count); |
| 312 |
|
| 313 |
cairo_save(cairo->cr); |
| 314 |
cairo_set_operator(cairo->cr, CAIRO_OPERATOR_CLEAR); |
| 315 |
cairo_reset_clip(cairo->cr); |
| 316 |
cairo_paint(cairo->cr); |
| 317 |
cairo_restore(cairo->cr); |
| 318 |
|
| 319 |
uint32_t count; |
| 320 |
struct bm_item **items = bm_menu_get_filtered_items(menu, &count); |
| 321 |
uint32_t lines = (menu->lines > 0 ? menu->lines : 1); |
| 322 |
uint32_t page_length = 0; |
| 323 |
|
| 324 |
if (menu->lines_mode == BM_LINES_UP && !menu->fixed_height) { |
| 325 |
int32_t new_y_offset = (count < lines ? (lines - count) * height : 0); |
| 326 |
bm_menu_set_y_offset(menu, new_y_offset); |
| 327 |
} |
| 328 |
|
| 329 |
memset(&result, 0, sizeof(result)); |
| 330 |
uint32_t title_x = 0; |
| 331 |
|
| 332 |
uint32_t total_height = menu->lines * height; |
| 333 |
if (!menu->fixed_height) total_height = MIN(count * height, total_height); |
| 334 |
total_height += height +border_size * 2; |
| 335 |
total_height = MIN(total_height, (max_height / height) * height); |
| 336 |
|
| 337 |
if (border_radius) { |
| 338 |
bm_cairo_rounded_path(cairo->cr, 1, 1, ((width + border_size) / cairo->scale) - 2, total_height - 2, border_radius); |
| 339 |
cairo_clip(cairo->cr); |
| 340 |
} |
| 341 |
|
| 342 |
if (menu->title) { |
| 343 |
/* Essentially hide the title text if we are drawing lines "up". */ |
| 344 |
enum bm_color title_fg = (menu->lines_mode == BM_LINES_UP ? BM_COLOR_ITEM_BG : BM_COLOR_TITLE_FG); |
| 345 |
enum bm_color title_bg = (menu->lines_mode == BM_LINES_UP ? BM_COLOR_ITEM_BG : BM_COLOR_TITLE_BG); |
| 346 |
bm_cairo_color_from_menu_color(menu, title_fg, &paint.fg); |
| 347 |
bm_cairo_color_from_menu_color(menu, title_bg, &paint.bg); |
| 348 |
paint.pos = (struct pos){ border_size + 4, vpadding + border_size }; |
| 349 |
paint.box = (struct box){ 4, 16, vpadding, -vpadding, 0, height }; |
| 350 |
bm_cairo_draw_line(cairo, &paint, &result, "%s", menu->title); |
| 351 |
title_x = result.x_advance; |
| 352 |
} |
| 353 |
|
| 354 |
enum bm_color filter_fg = (menu->lines_mode == BM_LINES_UP ? BM_COLOR_ITEM_BG : BM_COLOR_FILTER_FG); |
| 355 |
enum bm_color filter_bg = (menu->lines_mode == BM_LINES_UP ? BM_COLOR_ITEM_BG : BM_COLOR_FILTER_BG); |
| 356 |
enum bm_color cursor_fg = (menu->lines_mode == BM_LINES_UP ? BM_COLOR_ITEM_BG : BM_COLOR_CURSOR_FG); |
| 357 |
enum bm_color cursor_bg = (menu->lines_mode == BM_LINES_UP ? BM_COLOR_ITEM_BG : BM_COLOR_CURSOR_BG); |
| 358 |
bm_cairo_color_from_menu_color(menu, filter_fg, &paint.fg); |
| 359 |
bm_cairo_color_from_menu_color(menu, filter_bg, &paint.bg); |
| 360 |
bm_cairo_color_from_menu_color(menu, cursor_fg, &paint.cursor_fg); |
| 361 |
bm_cairo_color_from_menu_color(menu, cursor_bg, &paint.cursor_bg); |
| 362 |
paint.draw_cursor = true; |
| 363 |
paint.cursor = menu->cursor; |
| 364 |
paint.cursor_height = menu->cursor_height; |
| 365 |
paint.cursor_width = menu->cursor_width; |
| 366 |
paint.pos = (struct pos){ (menu->title ? 6 : 0) + result.x_advance + border_size, vpadding + border_size }; |
| 367 |
paint.box = (struct box){ (menu->title ? 2 : 4), 0, vpadding, -vpadding, width - paint.pos.x, height }; |
| 368 |
|
| 369 |
if (menu->lines == 0 || menu->lines_mode == BM_LINES_DOWN) { |
| 370 |
const char *filter_text = (menu->filter ? menu->filter : ""); |
| 371 |
if (menu->password == BM_PASSWORD_HIDE) { |
| 372 |
bm_cairo_draw_line_str(cairo, &paint, &result, ""); |
| 373 |
} else if (menu->password == BM_PASSWORD_INDICATOR) { |
| 374 |
char asterisk_print[1024] = ""; |
| 375 |
for (int i = 0; i < (int)(strlen(filter_text)); ++i) asterisk_print[i] = '*'; |
| 376 |
bm_cairo_draw_line(cairo, &paint, &result, "%s", asterisk_print); |
| 377 |
} else if (menu->password == BM_PASSWORD_NONE) { |
| 378 |
bm_cairo_draw_line(cairo, &paint, &result, "%s", filter_text); |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
paint.draw_cursor = false; |
| 383 |
uint32_t titleh = (result.height > 0 ? result.height : height); |
| 384 |
out_result->height = titleh; |
| 385 |
|
| 386 |
uint32_t posy = (menu->lines_mode == BM_LINES_UP ? 0 : titleh); |
| 387 |
uint32_t spacing_x = menu->spacing ? title_x : 0, spacing_y = 0; // 0 == variable width spacing |
| 388 |
|
| 389 |
if (menu->lines > 0) { |
| 390 |
/* vertical mode */ |
| 391 |
|
| 392 |
const bool scrollbar = (menu->scrollbar > BM_SCROLLBAR_NONE && (menu->scrollbar != BM_SCROLLBAR_AUTOHIDE || count > lines) ? true : false); |
| 393 |
if (lines > max_height / titleh) { |
| 394 |
/* there is more lines than screen can fit, enter fixed spacing mode */ |
| 395 |
lines = max_height / titleh - 1; |
| 396 |
spacing_y = titleh; |
| 397 |
} |
| 398 |
|
| 399 |
uint32_t prefix_x = 0; |
| 400 |
if (menu->prefix) { |
| 401 |
bm_pango_get_text_extents(cairo, &paint, &result, "%s ", menu->prefix); |
| 402 |
prefix_x += result.x_advance; |
| 403 |
} |
| 404 |
|
| 405 |
uint32_t scrollbar_w = 0; |
| 406 |
if (scrollbar) { |
| 407 |
bm_pango_get_text_extents(cairo, &paint, &result, "#"); |
| 408 |
scrollbar_w = result.x_advance; |
| 409 |
spacing_x += (spacing_x < scrollbar_w ? scrollbar_w : 0); |
| 410 |
} |
| 411 |
|
| 412 |
const uint32_t page = (menu->index / lines) * lines; |
| 413 |
|
| 414 |
for (uint32_t l = 0, i = page; l < lines && posy < max_height; ++i, ++l) { |
| 415 |
if (!menu->fixed_height && i >= count) { |
| 416 |
continue; |
| 417 |
} |
| 418 |
|
| 419 |
uint32_t is_fixed_up = (menu->fixed_height && menu->lines_mode == BM_LINES_UP); |
| 420 |
uint32_t required_empty = (count > lines ? 0 : lines - count); |
| 421 |
uint32_t last_item_index = (count < lines ? count - 1 + page : lines - 1 + page); |
| 422 |
last_item_index = MAX(MIN(last_item_index, count - 1), 0); |
| 423 |
uint32_t display_item_index = (menu->lines_mode == BM_LINES_DOWN ? i : menu->fixed_height ? last_item_index - i + page + required_empty : last_item_index - i + page); |
| 424 |
display_item_index = MAX(MIN(display_item_index, count - 1), 0); |
| 425 |
|
| 426 |
bool highlighted = false; |
| 427 |
if ((i < count && !is_fixed_up) || (is_fixed_up && display_item_index <= last_item_index)) { |
| 428 |
highlighted = (items[display_item_index] == bm_menu_get_highlighted_item(menu)); |
| 429 |
} |
| 430 |
|
| 431 |
if (highlighted) { |
| 432 |
if (menu->event_feedback) { |
| 433 |
bm_cairo_color_from_menu_color(menu, BM_COLOR_FEEDBACK_FG, &paint.fg); |
| 434 |
bm_cairo_color_from_menu_color(menu, BM_COLOR_FEEDBACK_BG, &paint.bg); |
| 435 |
} else { |
| 436 |
bm_cairo_color_from_menu_color(menu, BM_COLOR_HIGHLIGHTED_FG, &paint.fg); |
| 437 |
bm_cairo_color_from_menu_color(menu, BM_COLOR_HIGHLIGHTED_BG, &paint.bg); |
| 438 |
} |
| 439 |
} else if (i < count && bm_menu_item_is_selected(menu, items[i])) { |
| 440 |
bm_cairo_color_from_menu_color(menu, BM_COLOR_SELECTED_FG, &paint.fg); |
| 441 |
bm_cairo_color_from_menu_color(menu, BM_COLOR_SELECTED_BG, &paint.bg); |
| 442 |
} else if (i < count && i % 2 == 1) { |
| 443 |
bm_cairo_color_from_menu_color(menu, BM_COLOR_ALTERNATE_FG, &paint.fg); |
| 444 |
bm_cairo_color_from_menu_color(menu, BM_COLOR_ALTERNATE_BG, &paint.bg); |
| 445 |
} else { |
| 446 |
bm_cairo_color_from_menu_color(menu, BM_COLOR_ITEM_FG, &paint.fg); |
| 447 |
bm_cairo_color_from_menu_color(menu, BM_COLOR_ITEM_BG, &paint.bg); |
| 448 |
} |
| 449 |
|
| 450 |
char *line_str = ""; |
| 451 |
if ((i < count && !is_fixed_up) || (is_fixed_up && display_item_index <= last_item_index)) { |
| 452 |
line_str = bm_cairo_entry_message(items[display_item_index]->text, highlighted, menu->event_feedback, i, count); |
| 453 |
} |
| 454 |
|
| 455 |
if (menu->prefix && highlighted) { |
| 456 |
paint.pos = (struct pos){ spacing_x + border_size, posy + vpadding + border_size }; |
| 457 |
paint.box = (struct box){ 4, 0, vpadding, -vpadding, width - paint.pos.x, height }; |
| 458 |
bm_cairo_draw_line(cairo, &paint, &result, "%s %s", menu->prefix, line_str); |
| 459 |
} else { |
| 460 |
paint.pos = (struct pos){ spacing_x + border_size, posy+vpadding + border_size }; |
| 461 |
paint.box = (struct box){ 4 + prefix_x, 0, vpadding, -vpadding, width - paint.pos.x, height }; |
| 462 |
bm_cairo_draw_line(cairo, &paint, &result, "%s", line_str); |
| 463 |
} |
| 464 |
|
| 465 |
posy += (spacing_y ? spacing_y : result.height); |
| 466 |
out_result->height = posy; |
| 467 |
out_result->displayed++; |
| 468 |
page_length += 1; |
| 469 |
} |
| 470 |
|
| 471 |
if (spacing_x) { |
| 472 |
bm_cairo_color_from_menu_color(menu, BM_COLOR_ITEM_BG, &paint.bg); |
| 473 |
const uint32_t sheight = out_result->height - titleh; |
| 474 |
cairo_set_source_rgba(cairo->cr, paint.bg.r, paint.bg.b, paint.bg.g, paint.bg.a); |
| 475 |
cairo_rectangle(cairo->cr, scrollbar_w + border_size, titleh + border_size, spacing_x - scrollbar_w, sheight); |
| 476 |
cairo_fill(cairo->cr); |
| 477 |
} |
| 478 |
|
| 479 |
if (scrollbar && count > 0) { |
| 480 |
bm_cairo_color_from_menu_color(menu, BM_COLOR_SCROLLBAR_BG, &paint.bg); |
| 481 |
bm_cairo_color_from_menu_color(menu, BM_COLOR_SCROLLBAR_FG, &paint.fg); |
| 482 |
|
| 483 |
const uint32_t sheight = out_result->height - titleh; |
| 484 |
cairo_set_source_rgba(cairo->cr, paint.bg.r, paint.bg.b, paint.bg.g, paint.bg.a); |
| 485 |
cairo_rectangle(cairo->cr, border_size, titleh + border_size, scrollbar_w, sheight); |
| 486 |
cairo_fill(cairo->cr); |
| 487 |
|
| 488 |
const float percent = fmin(((float)page / (count - lines)), 1.0f); |
| 489 |
const float fraction = fmin((float)lines / count, 1.0f); |
| 490 |
const uint32_t size = fmax(sheight * fraction, 2.0f); |
| 491 |
const uint32_t posy = percent * (sheight - size); |
| 492 |
cairo_set_source_rgba(cairo->cr, paint.fg.r, paint.fg.b, paint.fg.g, paint.fg.a); |
| 493 |
cairo_rectangle(cairo->cr, border_size, titleh + posy + border_size, scrollbar_w, size); |
| 494 |
cairo_fill(cairo->cr); |
| 495 |
} |
| 496 |
} else { |
| 497 |
/* single-line mode */ |
| 498 |
bm_pango_get_text_extents(cairo, &paint, &result, "lorem ipsum lorem ipsum lorem ipsum lorem"); |
| 499 |
uint32_t cl = fmin(title_x + result.x_advance, width / 4); |
| 500 |
|
| 501 |
if (count > 0) { |
| 502 |
paint.pos = (struct pos){ cl, vpadding + border_size }; |
| 503 |
paint.box = (struct box){ 1, 2, vpadding, -vpadding, 0, height }; |
| 504 |
bm_cairo_draw_line(cairo, &paint, &result, (count > 0 && (menu->wrap || menu->index > 0) ? "<" : " ")); |
| 505 |
cl += result.x_advance + 1; |
| 506 |
} |
| 507 |
|
| 508 |
for (uint32_t i = menu->index; i < count && cl < (width/cairo->scale); ++i) { |
| 509 |
bool highlighted = (items[i] == bm_menu_get_highlighted_item(menu)); |
| 510 |
|
| 511 |
if (highlighted) { |
| 512 |
bm_cairo_color_from_menu_color(menu, BM_COLOR_HIGHLIGHTED_FG, &paint.fg); |
| 513 |
bm_cairo_color_from_menu_color(menu, BM_COLOR_HIGHLIGHTED_BG, &paint.bg); |
| 514 |
} else if (bm_menu_item_is_selected(menu, items[i])) { |
| 515 |
bm_cairo_color_from_menu_color(menu, BM_COLOR_SELECTED_FG, &paint.fg); |
| 516 |
bm_cairo_color_from_menu_color(menu, BM_COLOR_SELECTED_BG, &paint.bg); |
| 517 |
} else if (i % 2 == 1) { |
| 518 |
bm_cairo_color_from_menu_color(menu, BM_COLOR_ALTERNATE_FG, &paint.fg); |
| 519 |
bm_cairo_color_from_menu_color(menu, BM_COLOR_ALTERNATE_BG, &paint.bg); |
| 520 |
} else { |
| 521 |
bm_cairo_color_from_menu_color(menu, BM_COLOR_ITEM_FG, &paint.fg); |
| 522 |
bm_cairo_color_from_menu_color(menu, BM_COLOR_ITEM_BG, &paint.bg); |
| 523 |
} |
| 524 |
|
| 525 |
uint32_t hpadding = (menu->hpadding == 0 ? 2 : menu->hpadding); |
| 526 |
paint.pos = (struct pos){ cl + (hpadding/2), vpadding + border_size }; |
| 527 |
paint.box = (struct box){ hpadding/2, 1.5 * hpadding, vpadding, -vpadding, 0, height }; |
| 528 |
bm_cairo_draw_line(cairo, &paint, &result, "%s", (items[i]->text ? items[i]->text : "")); |
| 529 |
cl += result.x_advance + (0.5 * hpadding); |
| 530 |
out_result->displayed += (cl < width); |
| 531 |
out_result->height = fmax(out_result->height, result.height); |
| 532 |
} |
| 533 |
bm_cairo_color_from_menu_color(menu, BM_COLOR_FILTER_FG, &paint.fg); |
| 534 |
bm_cairo_color_from_menu_color(menu, BM_COLOR_FILTER_BG, &paint.bg); |
| 535 |
if (menu->wrap || menu->index + 1 < count) { |
| 536 |
bm_pango_get_text_extents(cairo, &paint, &result, ">"); |
| 537 |
paint.pos = (struct pos){ width/cairo->scale - result.x_advance - 2, vpadding + border_size }; |
| 538 |
paint.box = (struct box){ 1, 2, vpadding, -vpadding, 0, height }; |
| 539 |
bm_cairo_draw_line(cairo, &paint, &result, ">"); |
| 540 |
} |
| 541 |
|
| 542 |
} |
| 543 |
|
| 544 |
if (menu->lines_mode == BM_LINES_UP) { |
| 545 |
if (menu->title) { |
| 546 |
bm_cairo_color_from_menu_color(menu, BM_COLOR_TITLE_FG, &paint.fg); |
| 547 |
bm_cairo_color_from_menu_color(menu, BM_COLOR_TITLE_BG, &paint.bg); |
| 548 |
|
| 549 |
paint.pos = (struct pos){ border_size + 4, posy + vpadding + border_size }; |
| 550 |
paint.box = (struct box){ 4, 16, vpadding, -vpadding, 0, height }; |
| 551 |
bm_cairo_draw_line(cairo, &paint, &result, "%s", menu->title); |
| 552 |
} |
| 553 |
|
| 554 |
bm_cairo_color_from_menu_color(menu, BM_COLOR_FILTER_FG, &paint.fg); |
| 555 |
bm_cairo_color_from_menu_color(menu, BM_COLOR_FILTER_BG, &paint.bg); |
| 556 |
bm_cairo_color_from_menu_color(menu, BM_COLOR_CURSOR_FG, &paint.cursor_fg); |
| 557 |
bm_cairo_color_from_menu_color(menu, BM_COLOR_CURSOR_BG, &paint.cursor_bg); |
| 558 |
paint.draw_cursor = true; |
| 559 |
paint.cursor = menu->cursor; |
| 560 |
paint.cursor_height = menu->cursor_height; |
| 561 |
paint.cursor_width = menu->cursor_width; |
| 562 |
result.x_advance = (menu->title ? result.x_advance : 0); |
| 563 |
paint.pos = (struct pos){ (menu->title ? 2 : 0) + result.x_advance + border_size, posy + border_size }; |
| 564 |
paint.box = (struct box){ (menu->title ? 2 : 4), 0, vpadding, -vpadding, width - paint.pos.x, height }; |
| 565 |
|
| 566 |
const char *filter_text = (menu->filter ? menu->filter : ""); |
| 567 |
if (menu->password == BM_PASSWORD_HIDE) { |
| 568 |
bm_cairo_draw_line_str(cairo, &paint, &result, ""); |
| 569 |
} else if (menu->password == BM_PASSWORD_INDICATOR) { |
| 570 |
char asterisk_print[1024] = ""; |
| 571 |
for (int i = 0; i < (int)(strlen(filter_text)); ++i) asterisk_print[i] = '*'; |
| 572 |
bm_cairo_draw_line(cairo, &paint, &result, "%s", asterisk_print); |
| 573 |
} else if (menu->password == BM_PASSWORD_NONE) { |
| 574 |
bm_cairo_draw_line(cairo, &paint, &result, "%s", filter_text); |
| 575 |
} |
| 576 |
|
| 577 |
paint.draw_cursor = false; |
| 578 |
|
| 579 |
posy += (spacing_y ? spacing_y : result.height); |
| 580 |
out_result->height = posy; |
| 581 |
out_result->displayed++; |
| 582 |
} |
| 583 |
|
| 584 |
if (menu->counter) { |
| 585 |
char counter[128]; |
| 586 |
snprintf(counter, sizeof(counter), "[%u/%u]", filtered_item_count, total_item_count); |
| 587 |
bm_pango_get_text_extents(cairo, &paint, &result, "%s", counter); |
| 588 |
|
| 589 |
bm_cairo_color_from_menu_color(menu, BM_COLOR_ITEM_FG, &paint.fg); |
| 590 |
bm_cairo_color_from_menu_color(menu, BM_COLOR_ITEM_BG, &paint.bg); |
| 591 |
paint.pos = (struct pos){ width/cairo->scale - result.x_advance - 10, vpadding + border_size }; |
| 592 |
paint.box = (struct box){ 1, 2, vpadding, -vpadding, 0, height }; |
| 593 |
bm_cairo_draw_line(cairo, &paint, &result, "%s", counter); |
| 594 |
} |
| 595 |
|
| 596 |
// Draw borders |
| 597 |
bm_cairo_color_from_menu_color(menu, BM_COLOR_BORDER, &paint.fg); |
| 598 |
cairo_set_source_rgba(cairo->cr, paint.fg.r, paint.fg.b, paint.fg.g, paint.fg.a); |
| 599 |
if (!border_radius) { |
| 600 |
cairo_rectangle(cairo->cr, 0, 0, (width + border_size)/cairo->scale, total_height); |
| 601 |
} else { |
| 602 |
bm_cairo_rounded_path(cairo->cr, 1, 1, ((width + border_size)/cairo->scale)-2, total_height-2, border_radius); |
| 603 |
} |
| 604 |
cairo_set_line_width(cairo->cr, 2 * menu->border_size); |
| 605 |
|
| 606 |
cairo_stroke(cairo->cr); |
| 607 |
|
| 608 |
out_result->height += 2 * border_size; |
| 609 |
out_result->height *= cairo->scale; |
| 610 |
cairo_reset_clip(cairo->cr); |
| 611 |
} |
| 612 |
|
| 613 |
#endif /* _BM_CAIRO_H */ |
| 614 |
|
| 615 |
/* vim: set ts=8 sw=4 tw=0 :*/ |