[theme] theme errors are propogated at ax_select_theme, no earlier

This commit is contained in:
Milo Turner 2020-02-13 12:41:14 -05:00
parent 45962af503
commit f2b0c145a0
6 changed files with 68 additions and 39 deletions

View File

@ -7,6 +7,8 @@ struct ax_ctxt;
struct ax_theme; struct ax_theme;
struct ax_window; struct ax_window;
#define AX_OK 0
/* ----------------------------------------------------------------------------- /* -----------------------------------------------------------------------------
* Context * Context
* -------------------------------------------------------------------------- */ * -------------------------------------------------------------------------- */
@ -24,16 +26,19 @@ void ax_log(struct ax_ctxt* ax, const char* string);
* -------------------------------------------------------------------------- */ * -------------------------------------------------------------------------- */
void ax_begin_theme(struct ax_ctxt* ax); void ax_begin_theme(struct ax_ctxt* ax);
int ax_end_theme(struct ax_ctxt* ax, struct ax_theme** out_thm); void ax_end_theme(struct ax_ctxt* ax, struct ax_theme** out_thm);
int ax_set_theme_color( void ax_set_theme_color(
struct ax_ctxt* ax, const char* cat, int64_t rgb); struct ax_ctxt* ax, const char* cat, int64_t rgb);
int ax_set_theme_font( void ax_set_theme_font(
struct ax_ctxt* ax, const char* cat, const char* fnt_path, size_t fnt_size); struct ax_ctxt* ax, const char* cat, const char* fnt_path, size_t fnt_size);
int ax_set_theme_iconset( void ax_set_theme_iconset(
struct ax_ctxt* ax, const char* iconset_dir); struct ax_ctxt* ax, const char* iconset_dir);
void ax_select_theme(struct ax_ctxt* ax, struct ax_theme* thm); int ax_select_theme(struct ax_ctxt* ax, struct ax_theme* thm);
#define AX_ERR_THEME_LOADING 1
#define AX_ERR_THEME_INVALID 2
/* ----------------------------------------------------------------------------- /* -----------------------------------------------------------------------------
* Windows * Windows

View File

@ -10,7 +10,7 @@ struct ax_ctxt {
// errors // errors
struct rgn err_rgn; struct rgn err_rgn;
char* err; const char* err;
// theme // theme
struct rgn thmb_rgn; struct rgn thmb_rgn;
@ -24,7 +24,9 @@ void ax__ctxt_cleanup(struct ax_ctxt* ax);
static inline char* ax__make_error_buf(struct ax_ctxt* ax, size_t n) static inline char* ax__make_error_buf(struct ax_ctxt* ax, size_t n)
{ {
rgn_clear(&ax->err_rgn); rgn_clear(&ax->err_rgn);
return (ax->err = ralloc_typed(&ax->err_rgn, char, n)); char* buf = ralloc_typed(&ax->err_rgn, char, n);
ax->err = buf;
return buf;
} }
static inline void ax__set_error(struct ax_ctxt* ax, const char* text) static inline void ax__set_error(struct ax_ctxt* ax, const char* text)

View File

@ -63,56 +63,58 @@ void ax_begin_theme(struct ax_ctxt* ax)
ax__theme_builder_init(ax->thmb, &ax->thmb_rgn); ax__theme_builder_init(ax->thmb, &ax->thmb_rgn);
} }
int ax_end_theme(struct ax_ctxt* ax, struct ax_theme** out_thm) void ax_end_theme(struct ax_ctxt* ax, struct ax_theme** out_thm)
{ {
ASSERT(ax->thmb != NULL, "`ax_end_theme' called while not building a theme"); ASSERT(ax->thmb != NULL, "`ax_end_theme' called while not building a theme");
ax__theme_builder_finish(ax->thmb, ax->init_rgn, out_thm); ax__theme_builder_finish(ax->thmb, ax->init_rgn, out_thm);
return 0; rgn_clear(&ax->thmb_rgn);
} }
int ax_set_theme_color( void ax_set_theme_color(
struct ax_ctxt* ax, const char* cat_name, int64_t rgb) struct ax_ctxt* ax, const char* cat_name, int64_t rgb)
{ {
ASSERT(ax->thmb != NULL, "`ax_set_theme_color' called while not building a theme"); ASSERT(ax->thmb != NULL, "`ax_set_theme_color' called while not building a theme");
int rv;
enum ax_color_cat cat; enum ax_color_cat cat;
if ((rv = ax__string_to_color_cat(cat_name, &cat)) != 0) { if (ax__string_to_color_cat(cat_name, &cat) != 0) {
#define FMT "invalid color category `%s'" #define FMT "invalid color category `%s'"
sprintf(ax__make_error_buf(ax, strlen(FMT) + strlen(cat_name)), sprintf(ax__theme_make_error_buf(ax->thmb, strlen(FMT) + strlen(cat_name)),
FMT, cat_name); FMT, cat_name);
return rv;
#undef FMT #undef FMT
} } else {
ax__theme_set_color(ax->thmb, cat, rgb); ax__theme_set_color(ax->thmb, cat, rgb);
return 0; }
} }
int ax_set_theme_font( void ax_set_theme_font(
struct ax_ctxt* ax, const char* cat_name, const char* fnt_path, size_t fnt_size) struct ax_ctxt* ax, const char* cat_name, const char* fnt_path, size_t fnt_size)
{ {
ASSERT(ax->thmb != NULL, "`ax_set_theme_font' called while not building a theme"); ASSERT(ax->thmb != NULL, "`ax_set_theme_font' called while not building a theme");
int rv;
enum ax_font_cat cat; enum ax_font_cat cat;
if ((rv = ax__string_to_font_cat(cat_name, &cat)) != 0) { if (ax__string_to_font_cat(cat_name, &cat) != 0) {
#define FMT "invalid font category `%s'" #define FMT "invalid font category `%s'"
sprintf(ax__make_error_buf(ax, strlen(FMT) + strlen(cat_name)), sprintf(ax__theme_make_error_buf(ax->thmb, strlen(FMT) + strlen(cat_name)),
FMT, cat_name); FMT, cat_name);
return rv;
#undef FMT #undef FMT
} } else {
ax__theme_set_font(ax->thmb, cat, fnt_path, fnt_size); ax__theme_set_font(ax->thmb, cat, fnt_path, fnt_size);
return 0; }
} }
int ax_set_theme_iconset( void ax_set_theme_iconset(
struct ax_ctxt* ax, const char* iconset_dir) struct ax_ctxt* ax, const char* iconset_dir)
{ {
ASSERT(ax->thmb != NULL, "`ax_set_theme_iconset' called while not building a theme"); ASSERT(ax->thmb != NULL, "`ax_set_theme_iconset' called while not building a theme");
UNIMPLEMENTED(); UNIMPLEMENTED();
} }
void ax_select_theme(struct ax_ctxt* ax, struct ax_theme* thm) int ax_select_theme(struct ax_ctxt* ax, struct ax_theme* thm)
{ {
ASSERT_NON_NULL(thm, "theme"); ASSERT_NON_NULL(thm, "theme");
ax->sel_theme = thm; if (thm->fatal_err != NULL) {
ax->err = thm->fatal_err;
return AX_ERR_THEME_INVALID;
}
// TODO: see if font is loaded
ax->sel_theme = thm;
return 0;
} }

View File

@ -11,6 +11,8 @@ static const char* default_font_path(struct rgn* rgn)
static void theme_init(struct ax_theme* thm) static void theme_init(struct ax_theme* thm)
{ {
thm->fatal_err = NULL;
thm->colors[AX_COLOR_BACKGROUND] = 0xffffff; thm->colors[AX_COLOR_BACKGROUND] = 0xffffff;
thm->colors[AX_COLOR_SURFACE] = 0xffffff; thm->colors[AX_COLOR_SURFACE] = 0xffffff;
thm->colors[AX_COLOR_PRIMARY] = 0xff0000; thm->colors[AX_COLOR_PRIMARY] = 0xff0000;
@ -41,6 +43,15 @@ void ax__theme_builder_init(struct ax_theme_builder* thmb, struct rgn* rgn)
theme_init(&thmb->thm); theme_init(&thmb->thm);
} }
char* ax__theme_make_error_buf(
struct ax_theme_builder* thmb,
size_t len)
{
char* buf = ralloc_typed(thmb->rgn, char, len);
thmb->thm.fatal_err = buf;
return buf;
}
void ax__theme_set_color( void ax__theme_set_color(
struct ax_theme_builder* thmb, struct ax_theme_builder* thmb,
enum ax_color_cat cat, enum ax_color_cat cat,
@ -82,13 +93,19 @@ void ax__theme_builder_finish(
*thm = thmb->thm; *thm = thmb->thm;
*out_thm = thm; *out_thm = thm;
// fix font path strings by copying into dest region /* fix strings by copying into dest region */
if (thmb->thm.fatal_err != NULL) {
thmb->thm.fatal_err = rstrdup(dst_rgn, thmb->thm.fatal_err);
}
const char* dflt_font_path; const char* dflt_font_path;
if (thmb->dflt_font_path != NULL) { if (thmb->dflt_font_path != NULL) {
dflt_font_path = rstrdup(dst_rgn, thmb->dflt_font_path); dflt_font_path = rstrdup(dst_rgn, thmb->dflt_font_path);
} else { } else {
dflt_font_path = default_font_path(dst_rgn); dflt_font_path = default_font_path(dst_rgn);
} }
for (size_t i = 0; i < AX_FONT__COUNT; i++) { for (size_t i = 0; i < AX_FONT__COUNT; i++) {
if (thm->font_path[i] == NULL) { if (thm->font_path[i] == NULL) {
thm->font_path[i] = dflt_font_path; thm->font_path[i] = dflt_font_path;

View File

@ -32,6 +32,7 @@ enum ax_font_cat {
}; };
struct ax_theme { struct ax_theme {
const char* fatal_err;
int64_t colors[AX_COLOR__COUNT]; int64_t colors[AX_COLOR__COUNT];
const char* font_path[AX_FONT__COUNT]; const char* font_path[AX_FONT__COUNT];
size_t font_size[AX_FONT__COUNT]; size_t font_size[AX_FONT__COUNT];
@ -45,14 +46,18 @@ struct ax_theme_builder {
int ax__string_to_color_cat(const char* str, enum ax_color_cat* out_cat); int ax__string_to_color_cat(const char* str, enum ax_color_cat* out_cat);
int ax__string_to_font_cat(const char* str, enum ax_font_cat* out_cat); int ax__string_to_font_cat(const char* str, enum ax_font_cat* out_cat);
//void ax__color_cat_to_string(enum ax_color_cat cat, const char** out_str);
void ax__theme_builder_init( void ax__theme_builder_init(
struct ax_theme_builder* thmb, struct rgn* rgn); struct ax_theme_builder* thmb, struct rgn* rgn);
char* ax__theme_make_error_buf(
struct ax_theme_builder* thmb,
size_t len);
void ax__theme_set_color( void ax__theme_set_color(
struct ax_theme_builder* thmb, struct ax_theme_builder* thmb,
enum ax_color_cat cat, int64_t val); enum ax_color_cat cat,
int64_t val);
void ax__theme_set_font( void ax__theme_set_font(
struct ax_theme_builder* thmb, struct ax_theme_builder* thmb,
@ -83,5 +88,4 @@ static inline const char* ax__theme_font(
} }
void ax__debug_theme(struct ax_theme* thm); void ax__debug_theme(struct ax_theme* thm);

View File

@ -7,22 +7,21 @@ int main(void)
{ {
struct ax_ctxt* ax = ax_new(); struct ax_ctxt* ax = ax_new();
int rv; int rv;
#define GUARD(f, ...) if ((rv = f(__VA_ARGS__)) != 0) goto cleanup
ax_begin_theme(ax); ax_begin_theme(ax);
if ((rv = ax_set_theme_color(ax, "primary", 0x00ff00)) != 0) { ax_set_theme_color(ax, "primary", 0x00ff00);
goto cleanup; ax_set_theme_font(ax, "default", "/usr/share/fonts/TTF/DejaVuSans.ttf", 0);
} ax_set_theme_font(ax, "h1", NULL, 95);
if ((rv = ax_set_theme_font(ax, "default", "/usr/share/fonts/TTF/DejaVuSans.ttf", 0)) != 0) {
goto cleanup;
}
if ((rv = ax_set_theme_font(ax, "h1", NULL, 95)) != 0) {
goto cleanup;
}
struct ax_theme* thm; struct ax_theme* thm;
ax_end_theme(ax, &thm); ax_end_theme(ax, &thm);
// ax_select_theme(ax, thm); printf("----\ngot here\n----\n");
if ((rv = ax_select_theme(ax, thm)) != 0) {
goto cleanup;
}
ax__debug_theme(thm); ax__debug_theme(thm);
cleanup: cleanup: