From 087cec20e5cb8f56fad91f0f808f13328527dd67 Mon Sep 17 00:00:00 2001 From: barbieri Date: Tue, 19 Jun 2012 15:59:49 +0000 Subject: [PATCH] utils/theme: unify code, error messages and allow fallback. Introduced two nice helpers to aid common Edje handling: * apply theme, using fallback if main is not found; * reload theme if file changed. git-svn-id: http://svn.enlightenment.org/svn/e/trunk/terminology@72490 7cbeb6ba-43b4-40fd-8cce-4c39aea84d33 --- src/bin/Makefile.am | 3 ++- src/bin/config.c | 17 +++++++++++++++++ src/bin/config.h | 1 + src/bin/main.c | 25 +++++++++--------------- src/bin/media.c | 15 ++------------- src/bin/options_font.c | 19 +++--------------- src/bin/termio.c | 18 +++-------------- src/bin/utils.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/bin/utils.h | 13 +++++++++++++ 9 files changed, 102 insertions(+), 61 deletions(-) create mode 100644 src/bin/utils.c create mode 100644 src/bin/utils.h diff --git a/src/bin/Makefile.am b/src/bin/Makefile.am index 10012fb..6c8ff11 100644 --- a/src/bin/Makefile.am +++ b/src/bin/Makefile.am @@ -22,4 +22,5 @@ options_video.c options_video.h \ termio.c termio.h \ termpty.c termpty.h \ utf8.c utf8.h \ -win.c win.h +win.c win.h \ +utils.c utils.h diff --git a/src/bin/config.c b/src/bin/config.c index 5ecc238..0f1dfc0 100644 --- a/src/bin/config.c +++ b/src/bin/config.c @@ -1,3 +1,5 @@ +#include "private.h" + #include #include "config.h" @@ -173,3 +175,18 @@ config_theme_path_get(const Config *config) elm_app_data_dir_get(), config->theme); return path; } + +const char * +config_theme_path_default_get(const Config *config __UNUSED__) +{ + static char path[PATH_MAX] = ""; + + EINA_SAFETY_ON_NULL_RETURN_VAL(config, NULL); + EINA_SAFETY_ON_NULL_RETURN_VAL(config->theme, NULL); + + if (path[0]) return path; + + snprintf(path, sizeof(path), "%s/themes/default.edj", + elm_app_data_dir_get()); + return path; +} diff --git a/src/bin/config.h b/src/bin/config.h index d385fc7..111cf8f 100644 --- a/src/bin/config.h +++ b/src/bin/config.h @@ -31,5 +31,6 @@ Config *config_load(const char *key); void config_del(Config *config); const char *config_theme_path_get(const Config *config); +const char *config_theme_path_default_get(const Config *config); #endif diff --git a/src/bin/main.c b/src/bin/main.c index 3844de9..a7d7d87 100644 --- a/src/bin/main.c +++ b/src/bin/main.c @@ -8,6 +8,7 @@ #include "config.h" #include "options.h" #include "media.h" +#include "utils.h" int _log_domain = -1; @@ -49,18 +50,6 @@ _cb_size_hint(void *data __UNUSED__, Evas *e __UNUSED__, Evas_Object *obj, void } static void -_reload_theme(void *data __UNUSED__, Evas_Object *obj, - const char *emission __UNUSED__, const char *source __UNUSED__) -{ - const char *file; - const char *group; - - edje_object_file_get(obj, &file, &group); - edje_object_file_set(obj, file, group); - fprintf(stderr, "RELOADING THEME main.c\n"); -} - -static void _cb_options(void *data __UNUSED__, Evas_Object *obj __UNUSED__, void *event __UNUSED__) { options_toggle(win, bg, term); @@ -273,9 +262,13 @@ elm_main(int argc, char **argv) bg = o = edje_object_add(evas_object_evas_get(win)); evas_object_size_hint_weight_set(o, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND); evas_object_size_hint_fill_set(o, EVAS_HINT_FILL, EVAS_HINT_FILL); - edje_object_file_set(o, config_theme_path_get(config), - "terminology/background"); - edje_object_signal_callback_add(o, "edje,change,file", "edje", _reload_theme, NULL); + if (!theme_apply(o, config, "terminology/background")) + { + CRITICAL("Couldn't find terminology theme! Forgot 'make install'?"); + retval = EXIT_FAILURE; + goto end; + } + theme_auto_reload_enable(o); elm_win_resize_object_add(win, o); evas_object_show(o); @@ -292,7 +285,7 @@ elm_main(int argc, char **argv) main_trans_update(config); main_media_update(config); - + evas_object_smart_callback_add(win, "focus,in", _cb_focus_in, term); evas_object_smart_callback_add(win, "focus,out", _cb_focus_out, term); _cb_size_hint(win, evas_object_evas_get(win), term, NULL); diff --git a/src/bin/media.c b/src/bin/media.c index 739be81..dad48fb 100644 --- a/src/bin/media.c +++ b/src/bin/media.c @@ -4,6 +4,7 @@ #include #include "media.h" #include "config.h" +#include "utils.h" typedef struct _Media Media; @@ -74,18 +75,6 @@ _is_fmt(const char *f, const char **extn) //////////////////////// img static void -_reload_theme(void *data __UNUSED__, Evas_Object *obj, - const char *emission __UNUSED__, const char *source __UNUSED__) -{ - const char *file; - const char *group; - - edje_object_file_get(obj, &file, &group); - edje_object_file_set(obj, file, group); - fprintf(stderr, "RELOADING THEME media.c\n"); -} - -static void _cb_img_preloaded(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event __UNUSED__) { Media *sd = evas_object_smart_data_get(data); @@ -286,8 +275,8 @@ _type_edje_init(Evas_Object *obj) { edje_object_signal_callback_add(o, "preload,done", "", _cb_edje_preloaded, obj); - edje_object_signal_callback_add(o, "edje,change,file", "edje", _reload_theme, NULL); edje_object_preload(o, EINA_FALSE); + theme_auto_reload_enable(o); return; } } diff --git a/src/bin/options_font.c b/src/bin/options_font.c index ce90dea..3d6ce6b 100644 --- a/src/bin/options_font.c +++ b/src/bin/options_font.c @@ -5,6 +5,7 @@ #include "termio.h" #include "options.h" #include "options_font.h" +#include "utils.h" static Evas_Object *op_fontslider, *op_fontlist, *op_fsml, *op_fbig; @@ -22,18 +23,6 @@ static Eina_List *fonts = NULL; static Eina_Hash *fonthash = NULL; static void -_reload_theme(void *data __UNUSED__, Evas_Object *obj, - const char *emission __UNUSED__, const char *source __UNUSED__) -{ - const char *file; - const char *group; - - edje_object_file_get(obj, &file, &group); - edje_object_file_set(obj, file, group); - fprintf(stderr, "RELOADING THEME\n"); -} - -static void _update_sizing(Evas_Object *term) { Evas_Coord ow = 0, oh = 0, mw = 1, mh = 1, w, h; @@ -140,10 +129,8 @@ _cb_op_font_content_get(void *data, Evas_Object *obj, const char *part) Config *config = termio_config_get(f->term); o = edje_object_add(evas_object_evas_get(obj)); - edje_object_file_set(o, config_theme_path_get(config), - "terminology/fontpreview"); - edje_object_signal_callback_add(o, "edje,change,file", "edje", - _reload_theme, NULL); + theme_apply(o, config, "terminology/fontpreview"); + theme_auto_reload_enable(o); evas_object_size_hint_min_set(o, 96 * elm_config_scale_get(), 40 * elm_config_scale_get()); diff --git a/src/bin/termio.c b/src/bin/termio.c index c8bdd89..e27e4e3 100644 --- a/src/bin/termio.c +++ b/src/bin/termio.c @@ -8,6 +8,7 @@ #include "col.h" #include "keyin.h" #include "config.h" +#include "utils.h" typedef struct _Termio Termio; @@ -55,18 +56,6 @@ static Evas_Smart_Class _parent_sc = EVAS_SMART_CLASS_INIT_NULL; static void _smart_calculate(Evas_Object *obj); static void -_reload_theme(void *data __UNUSED__, Evas_Object *obj, - const char *emission __UNUSED__, const char *source __UNUSED__) -{ - const char *file; - const char *group; - - edje_object_file_get(obj, &file, &group); - edje_object_file_set(obj, file, group); - fprintf(stderr, "RELOADING THEME termio\n"); -} - -static void _smart_apply(Evas_Object *obj) { Termio *sd = evas_object_smart_data_get(obj); @@ -800,9 +789,8 @@ _termio_config_set(Evas_Object *obj, Config *config) sd->font.chw = w; sd->font.chh = h; - edje_object_file_set(sd->cur.obj, - config_theme_path_get(config), "terminology/cursor"); - edje_object_signal_callback_add(sd->cur.obj, "edje,change,file", "edje", _reload_theme, NULL); + theme_apply(sd->cur.obj, config, "terminology/cursor"); + theme_auto_reload_enable(sd->cur.obj); evas_object_resize(sd->cur.obj, sd->font.chw, sd->font.chh); evas_object_show(sd->cur.obj); } diff --git a/src/bin/utils.c b/src/bin/utils.c new file mode 100644 index 0000000..136a664 --- /dev/null +++ b/src/bin/utils.c @@ -0,0 +1,52 @@ +#include "private.h" +#include "utils.h" + +#include + +Eina_Bool +theme_apply(Evas_Object *edje, const Config *config, const char *group) +{ + const char *errmsg; + + EINA_SAFETY_ON_NULL_RETURN_VAL(edje, EINA_FALSE); + EINA_SAFETY_ON_NULL_RETURN_VAL(config, EINA_FALSE); + EINA_SAFETY_ON_NULL_RETURN_VAL(group, EINA_FALSE); + + if (edje_object_file_set(edje, config_theme_path_get(config), group)) + return EINA_TRUE; + + errmsg = edje_load_error_str(edje_object_load_error_get(edje)); + INF("Cannot find theme: file=%s group=%s error='%s', trying default...", + config_theme_path_get(config), group, errmsg); + + if (edje_object_file_set(edje, config_theme_path_default_get(config), group)) + return EINA_TRUE; + + errmsg = edje_load_error_str(edje_object_load_error_get(edje)); + ERR("Could not load any theme for group=%s: %s", group, errmsg); + return EINA_FALSE; +} + +void +theme_reload(Evas_Object *edje) +{ + const char *file; + const char *group; + + edje_object_file_get(edje, &file, &group); + INF("file=%s, group=%s", file, group); + edje_object_file_set(edje, file, group); +} + +static void +theme_reload_cb(void *data __UNUSED__, Evas_Object *obj, const char *emission __UNUSED__, const char *source __UNUSED__) +{ + theme_reload(obj); +} + +void +theme_auto_reload_enable(Evas_Object *edje) +{ + edje_object_signal_callback_add + (edje, "edje,change,file", "edje", theme_reload_cb, NULL); +} diff --git a/src/bin/utils.h b/src/bin/utils.h new file mode 100644 index 0000000..139eca4 --- /dev/null +++ b/src/bin/utils.h @@ -0,0 +1,13 @@ +#ifndef _UTILS_H__ +#define _UTILS_H__ + +#include +#include "config.h" + +Eina_Bool theme_apply(Evas_Object *edje, const Config *config, const char *group); + +void theme_reload(Evas_Object *edje); + +void theme_auto_reload_enable(Evas_Object *edje); + +#endif -- 2.7.4