mirror of
https://github.com/labwc/labwc.git
synced 2026-03-09 05:33:53 -04:00
theme: move more button fallback logic to theme.c
...and simplify button_xbm_load() by splitting it into one function that loads an xbm file and another that creates an icon from a builtin bitmap.
This commit is contained in:
parent
27de4e6398
commit
b7ee8b16f3
3 changed files with 69 additions and 31 deletions
|
|
@ -4,9 +4,19 @@
|
||||||
|
|
||||||
struct lab_data_buffer;
|
struct lab_data_buffer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* button_xbm_from_bitmap() - create button from monochrome bitmap
|
||||||
|
* @bitmap: bitmap data array in hexadecimal xbm format
|
||||||
|
* @buffer: cairo-surface-buffer to create
|
||||||
|
* @rgba: color
|
||||||
|
*
|
||||||
|
* Example bitmap: char button[6] = { 0x3f, 0x3f, 0x21, 0x21, 0x21, 0x3f };
|
||||||
|
*/
|
||||||
|
void button_xbm_from_bitmap(const char *bitmap, struct lab_data_buffer **buffer,
|
||||||
|
float *rgba);
|
||||||
|
|
||||||
/* button_xbm_load - Convert xbm file to buffer with cairo surface */
|
/* button_xbm_load - Convert xbm file to buffer with cairo surface */
|
||||||
void button_xbm_load(const char *button_name, const char *alt_name,
|
void button_xbm_load(const char *button_name, struct lab_data_buffer **buffer,
|
||||||
struct lab_data_buffer **buffer, const char *fallback_button,
|
|
||||||
float *rgba);
|
float *rgba);
|
||||||
|
|
||||||
#endif /* LABWC_BUTTON_XBM_H */
|
#endif /* LABWC_BUTTON_XBM_H */
|
||||||
|
|
|
||||||
|
|
@ -259,16 +259,29 @@ parse_xbm_builtin(const char *button, int size)
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
button_xbm_load(const char *button_name, const char *alt_name,
|
button_xbm_from_bitmap(const char *bitmap, struct lab_data_buffer **buffer,
|
||||||
struct lab_data_buffer **buffer, const char *fallback_button,
|
float *rgba)
|
||||||
float *rgba)
|
|
||||||
{
|
{
|
||||||
struct pixmap pixmap = {0};
|
struct pixmap pixmap = {0};
|
||||||
if (*buffer) {
|
if (*buffer) {
|
||||||
wlr_buffer_drop(&(*buffer)->base);
|
wlr_buffer_drop(&(*buffer)->base);
|
||||||
*buffer = NULL;
|
*buffer = NULL;
|
||||||
}
|
}
|
||||||
|
color = u32(rgba);
|
||||||
|
pixmap = parse_xbm_builtin(bitmap, 6);
|
||||||
|
*buffer = buffer_create_wrap(pixmap.data, pixmap.width, pixmap.height,
|
||||||
|
pixmap.width * 4, /* free_on_destroy */ true);
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
button_xbm_load(const char *button_name, struct lab_data_buffer **buffer,
|
||||||
|
float *rgba)
|
||||||
|
{
|
||||||
|
struct pixmap pixmap = {0};
|
||||||
|
if (*buffer) {
|
||||||
|
wlr_buffer_drop(&(*buffer)->base);
|
||||||
|
*buffer = NULL;
|
||||||
|
}
|
||||||
color = u32(rgba);
|
color = u32(rgba);
|
||||||
|
|
||||||
/* Read file into memory as it's easier to tokenize that way */
|
/* Read file into memory as it's easier to tokenize that way */
|
||||||
|
|
@ -283,20 +296,8 @@ button_xbm_load(const char *button_name, const char *alt_name,
|
||||||
free(tokens);
|
free(tokens);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!pixmap.data && *alt_name) {
|
if (!pixmap.data) {
|
||||||
button_filename(alt_name, filename, sizeof(filename));
|
return;
|
||||||
char *token_buffer = grab_file(filename);
|
|
||||||
if (token_buffer) {
|
|
||||||
struct token *tokens = tokenize_xbm(token_buffer);
|
|
||||||
free(token_buffer);
|
|
||||||
pixmap = parse_xbm_tokens(tokens);
|
|
||||||
if (tokens) {
|
|
||||||
free(tokens);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (!pixmap.data && fallback_button) {
|
|
||||||
pixmap = parse_xbm_builtin(fallback_button, 6);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Create buffer with free_on_destroy being true */
|
/* Create buffer with free_on_destroy being true */
|
||||||
|
|
|
||||||
51
src/theme.c
51
src/theme.c
|
|
@ -219,21 +219,20 @@ load_buttons(struct theme *theme)
|
||||||
}, };
|
}, };
|
||||||
|
|
||||||
char filename[4096] = {0};
|
char filename[4096] = {0};
|
||||||
char alt_filename[4096] = {0};
|
|
||||||
for (size_t i = 0; i < ARRAY_SIZE(buttons); ++i) {
|
for (size_t i = 0; i < ARRAY_SIZE(buttons); ++i) {
|
||||||
struct button *b = &buttons[i];
|
struct button *b = &buttons[i];
|
||||||
|
|
||||||
drop(b->active.buffer);
|
drop(b->active.buffer);
|
||||||
drop(b->inactive.buffer);
|
drop(b->inactive.buffer);
|
||||||
|
|
||||||
/* Try png icon first */
|
/* PNG */
|
||||||
snprintf(filename, sizeof(filename), "%s-active.png", b->name);
|
snprintf(filename, sizeof(filename), "%s-active.png", b->name);
|
||||||
button_png_load(filename, b->active.buffer);
|
button_png_load(filename, b->active.buffer);
|
||||||
snprintf(filename, sizeof(filename), "%s-inactive.png", b->name);
|
snprintf(filename, sizeof(filename), "%s-inactive.png", b->name);
|
||||||
button_png_load(filename, b->inactive.buffer);
|
button_png_load(filename, b->inactive.buffer);
|
||||||
|
|
||||||
#if HAVE_RSVG
|
#if HAVE_RSVG
|
||||||
/* Then try svg icon */
|
/* SVG */
|
||||||
int size = theme->title_height - 2 * theme->padding_height;
|
int size = theme->title_height - 2 * theme->padding_height;
|
||||||
if (!*b->active.buffer) {
|
if (!*b->active.buffer) {
|
||||||
snprintf(filename, sizeof(filename), "%s-active.svg", b->name);
|
snprintf(filename, sizeof(filename), "%s-active.svg", b->name);
|
||||||
|
|
@ -245,19 +244,47 @@ load_buttons(struct theme *theme)
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* If there were no png/svg buttons, use xbm */
|
/* XBM */
|
||||||
snprintf(filename, sizeof(filename), "%s.xbm", b->name);
|
snprintf(filename, sizeof(filename), "%s.xbm", b->name);
|
||||||
alt_filename[0] = '\0';
|
|
||||||
if (b->alt_name) {
|
|
||||||
snprintf(alt_filename, sizeof(alt_filename), "%s.xbm", b->alt_name);
|
|
||||||
}
|
|
||||||
if (!*b->active.buffer) {
|
if (!*b->active.buffer) {
|
||||||
button_xbm_load(filename, alt_filename, b->active.buffer,
|
button_xbm_load(filename, b->active.buffer, b->active.rgba);
|
||||||
b->fallback_button, b->active.rgba);
|
|
||||||
}
|
}
|
||||||
if (!*b->inactive.buffer) {
|
if (!*b->inactive.buffer) {
|
||||||
button_xbm_load(filename, alt_filename, b->inactive.buffer,
|
button_xbm_load(filename, b->inactive.buffer, b->inactive.rgba);
|
||||||
b->fallback_button, b->inactive.rgba);
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* XBM (alternative name)
|
||||||
|
* For example max_hover_toggled instead of max_toggled_hover
|
||||||
|
*/
|
||||||
|
if (b->alt_name) {
|
||||||
|
snprintf(filename, sizeof(filename), "%s.xbm", b->name);
|
||||||
|
} else {
|
||||||
|
filename[0] = '\0';
|
||||||
|
}
|
||||||
|
if (!*b->active.buffer) {
|
||||||
|
button_xbm_load(filename, b->active.buffer, b->active.rgba);
|
||||||
|
}
|
||||||
|
if (!*b->inactive.buffer) {
|
||||||
|
button_xbm_load(filename, b->inactive.buffer, b->inactive.rgba);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Builtin bitmap
|
||||||
|
*
|
||||||
|
* Applicable to basic buttons such as max, max_toggled and
|
||||||
|
* iconify. There are no bitmap fallbacks for *_hover icons.
|
||||||
|
*/
|
||||||
|
if (!b->fallback_button) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!*b->active.buffer) {
|
||||||
|
button_xbm_from_bitmap(b->fallback_button,
|
||||||
|
b->active.buffer, b->active.rgba);
|
||||||
|
}
|
||||||
|
if (!*b->inactive.buffer) {
|
||||||
|
button_xbm_from_bitmap(b->fallback_button,
|
||||||
|
b->inactive.buffer, b->inactive.rgba);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue