options_font: be able to have multiple instances

This commit is contained in:
Boris Faure 2018-01-12 23:27:19 +01:00
parent 32209a6dfd
commit 85ba744771
3 changed files with 132 additions and 104 deletions

View File

@ -79,7 +79,6 @@ _cb_op_del_delay(void *data)
evas_object_del(ctx->opbox); evas_object_del(ctx->opbox);
evas_object_del(ctx->frame); evas_object_del(ctx->frame);
options_font_clear();
options_theme_clear(); options_theme_clear();
free(ctx); free(ctx);

View File

@ -1,6 +1,7 @@
#include "private.h" #include "private.h"
#include <Elementary.h> #include <Elementary.h>
#include <assert.h>
#include "config.h" #include "config.h"
#include "termio.h" #include "termio.h"
#include "options.h" #include "options.h"
@ -12,37 +13,47 @@
#define FONT_MAX 45 #define FONT_MAX 45
#define FONT_STEP (1.0 / (FONT_MAX - FONT_MIN)) #define FONT_STEP (1.0 / (FONT_MAX - FONT_MIN))
static Evas_Object *op_fontslider, *op_fontlist, *op_fsml, *op_fbig;
typedef struct _Font Font; typedef struct _Font_Ctx
{
Evas_Object *op_fontslider;
Evas_Object *op_fontlist;
Evas_Object *op_fsml;
Evas_Object *op_fbig;
Evas_Object *term;
Eina_List *fonts;
Eina_Hash *fonthash;
Config *config;
Evas_Coord tsize_w;
Evas_Coord tsize_h;
int expecting_resize;
} Font_Ctx;
struct _Font typedef struct _Font
{ {
Elm_Object_Item *item; Elm_Object_Item *item;
const char *pretty_name; const char *pretty_name;
const char *full_name; const char *full_name;
Evas_Object *term; Font_Ctx *ctx;
unsigned char bitmap : 1; unsigned char bitmap : 1;
}; } Font;
static Eina_List *fonts = NULL;
static Eina_Hash *fonthash = NULL;
static Evas_Coord tsize_w = 0, tsize_h = 0;
static int expecting_resize = 0;
static void static void
_update_sizing(Evas_Object *term) _update_sizing(Font_Ctx *ctx)
{ {
Evas_Coord mw = 1, mh = 1, w, h; Evas_Coord mw = 1, mh = 1, w, h;
termio_config_update(term); termio_config_update(ctx->term);
evas_object_size_hint_min_get(term, &mw, &mh); evas_object_size_hint_min_get(ctx->term, &mw, &mh);
if (mw < 1) mw = 1; if (mw < 1)
if (mh < 1) mh = 1; mw = 1;
w = tsize_w / mw; if (mh < 1)
h = tsize_h / mh; mh = 1;
evas_object_size_hint_request_set(term, w * mw, h * mh); w = ctx->tsize_w / mw;
expecting_resize = 1; h = ctx->tsize_h / mh;
evas_object_size_hint_request_set(ctx->term, w * mw, h * mh);
ctx->expecting_resize = 1;
} }
static int static int
@ -133,18 +144,19 @@ _cb_op_font_sel(void *data,
void *_event EINA_UNUSED) void *_event EINA_UNUSED)
{ {
Font *f = data; Font *f = data;
Config *config = termio_config_get(f->term); Font_Ctx *ctx = f->ctx;
Term *term = termio_term_get(f->term); Config *config = ctx->config;
Term *term = termio_term_get(ctx->term);
if ((config->font.name) && (!strcmp(f->full_name, config->font.name))) if ((config->font.name) && (!strcmp(f->full_name, config->font.name)))
return; return;
if (config->font.name) eina_stringshare_del(config->font.name); if (config->font.name) eina_stringshare_del(config->font.name);
config->font.name = eina_stringshare_add(f->full_name); config->font.name = eina_stringshare_add(f->full_name);
config->font.bitmap = f->bitmap; config->font.bitmap = f->bitmap;
_update_sizing(f->term); _update_sizing(ctx);
elm_object_disabled_set(op_fsml, f->bitmap); elm_object_disabled_set(ctx->op_fsml, f->bitmap);
elm_object_disabled_set(op_fontslider, f->bitmap); elm_object_disabled_set(ctx->op_fontslider, f->bitmap);
elm_object_disabled_set(op_fbig, f->bitmap); elm_object_disabled_set(ctx->op_fbig, f->bitmap);
config_save(config, NULL); config_save(config, NULL);
win_font_update(term); win_font_update(term);
} }
@ -154,15 +166,16 @@ _cb_op_fontsize_sel(void *data,
Evas_Object *obj, Evas_Object *obj,
void *_event EINA_UNUSED) void *_event EINA_UNUSED)
{ {
Evas_Object *termio_obj = data; Font_Ctx *ctx = data;
Config *config = termio_config_get(termio_obj); Config *config = ctx->config;
Term *term = termio_term_get(termio_obj); Term *term = termio_term_get(ctx->term);
int size = elm_slider_value_get(obj) + 0.5; int size = elm_slider_value_get(obj) + 0.5;
if (config->font.size == size) return; if (config->font.size == size)
return;
config->font.size = size; config->font.size = size;
_update_sizing(termio_obj); _update_sizing(ctx);
elm_genlist_realized_items_update(op_fontlist); elm_genlist_realized_items_update(ctx->op_fontlist);
config_save(config, NULL); config_save(config, NULL);
win_font_update(term); win_font_update(term);
} }
@ -204,21 +217,26 @@ _cb_op_font_preview_delayed_eval(void *data)
Evas_Coord ox, oy, ow, oh, vx, vy, vw, vh; Evas_Coord ox, oy, ow, oh, vx, vy, vw, vh;
Config *config; Config *config;
if (!evas_object_visible_get(obj)) goto done; if (!evas_object_visible_get(obj))
if (edje_object_part_swallow_get(obj, "terminology.text.preview")) goto done; goto done;
if (edje_object_part_swallow_get(obj, "terminology.text.preview"))
goto done;
evas_object_geometry_get(obj, &ox, &oy, &ow, &oh); evas_object_geometry_get(obj, &ox, &oy, &ow, &oh);
if ((ow < 2) || (oh < 2)) goto done; if ((ow < 2) || (oh < 2))
goto done;
evas_output_viewport_get(evas_object_evas_get(obj), &vx, &vy, &vw, &vh); evas_output_viewport_get(evas_object_evas_get(obj), &vx, &vy, &vw, &vh);
f = evas_object_data_get(obj, "font"); f = evas_object_data_get(obj, "font");
if (!f) goto done; if (!f)
config = termio_config_get(f->term); goto done;
if (!config) goto done; config = f->ctx->config;
if (!config)
goto done;
if (ELM_RECTS_INTERSECT(ox, oy, ow, oh, vx, vy, vw, vh)) if (ELM_RECTS_INTERSECT(ox, oy, ow, oh, vx, vy, vw, vh))
{ {
char buf[4096]; char buf[4096];
int r, g, b, a; int r, g, b, a;
Evas *evas = evas_object_evas_get(obj); Evas *evas = evas_object_evas_get(obj);
Evas_Object *textgrid = termio_textgrid_get(f->term); Evas_Object *textgrid = termio_textgrid_get(f->ctx->term);
evas_object_textgrid_palette_get(textgrid, EVAS_TEXTGRID_PALETTE_STANDARD, evas_object_textgrid_palette_get(textgrid, EVAS_TEXTGRID_PALETTE_STANDARD,
0, &r, &g, &b, &a); 0, &r, &g, &b, &a);
@ -253,10 +271,13 @@ _cb_op_font_preview_eval(void *data,
Font *f = data; Font *f = data;
Evas_Coord ox, oy, ow, oh, vx, vy, vw, vh; Evas_Coord ox, oy, ow, oh, vx, vy, vw, vh;
if (!evas_object_visible_get(obj)) return; if (!evas_object_visible_get(obj))
if (edje_object_part_swallow_get(obj, "terminology.text.preview")) return; return;
if (edje_object_part_swallow_get(obj, "terminology.text.preview"))
return;
evas_object_geometry_get(obj, &ox, &oy, &ow, &oh); evas_object_geometry_get(obj, &ox, &oy, &ow, &oh);
if ((ow < 2) || (oh < 2)) return; if ((ow < 2) || (oh < 2))
return;
evas_output_viewport_get(evas_object_evas_get(obj), &vx, &vy, &vw, &vh); evas_output_viewport_get(evas_object_evas_get(obj), &vx, &vy, &vw, &vh);
if (ELM_RECTS_INTERSECT(ox, oy, ow, oh, vx, vy, vw, vh)) if (ELM_RECTS_INTERSECT(ox, oy, ow, oh, vx, vy, vw, vh))
{ {
@ -264,8 +285,10 @@ _cb_op_font_preview_eval(void *data,
double rnd = 0.2; double rnd = 0.2;
timer = evas_object_data_get(obj, "delay"); timer = evas_object_data_get(obj, "delay");
if (timer) return; if (timer)
else evas_object_data_set(obj, "font", f); return;
else
evas_object_data_set(obj, "font", f);
rnd += (double)(rand() % 100) / 500.0; rnd += (double)(rand() % 100) / 500.0;
timer = ecore_timer_add(rnd, _cb_op_font_preview_delayed_eval, obj); timer = ecore_timer_add(rnd, _cb_op_font_preview_delayed_eval, obj);
evas_object_data_set(obj, "delay", timer); evas_object_data_set(obj, "delay", timer);
@ -280,7 +303,7 @@ _cb_op_font_content_get(void *data, Evas_Object *obj, const char *part)
if (!strcmp(part, "elm.swallow.icon")) if (!strcmp(part, "elm.swallow.icon"))
{ {
Evas_Object *o; Evas_Object *o;
Config *config = termio_config_get(f->term); Config *config = f->ctx->config;
o = edje_object_add(evas_object_evas_get(obj)); o = edje_object_add(evas_object_evas_get(obj));
theme_apply(o, config, "terminology/fontpreview"); theme_apply(o, config, "terminology/fontpreview");
@ -324,13 +347,14 @@ _cb_term_resize(void *data,
Evas_Object *_obj EINA_UNUSED, Evas_Object *_obj EINA_UNUSED,
void *_event EINA_UNUSED) void *_event EINA_UNUSED)
{ {
Evas_Object *term = data; Font_Ctx *ctx = data;
if (expecting_resize)
if (ctx->expecting_resize)
{ {
expecting_resize = 0; ctx->expecting_resize = 0;
return; return;
} }
evas_object_geometry_get(term, NULL, NULL, &tsize_w, &tsize_h); evas_object_geometry_get(ctx->term, NULL, NULL, &ctx->tsize_w, &ctx->tsize_h);
} }
static void static void
@ -339,32 +363,29 @@ _cb_font_del(void *data,
Evas_Object *_obj EINA_UNUSED, Evas_Object *_obj EINA_UNUSED,
void *_event EINA_UNUSED) void *_event EINA_UNUSED)
{ {
Evas_Object *term = data; Font_Ctx *ctx = data;
evas_object_event_callback_del_full(term, EVAS_CALLBACK_RESIZE, evas_object_event_callback_del_full(ctx->term, EVAS_CALLBACK_RESIZE,
_cb_term_resize, term); _cb_term_resize, ctx);
} }
void static void
options_font_clear(void) _parent_del_cb(void *data,
Evas *_e EINA_UNUSED,
Evas_Object *_obj EINA_UNUSED,
void *_event_info EINA_UNUSED)
{ {
Font_Ctx *ctx = data;
Font *f; Font *f;
op_fontslider = NULL; EINA_LIST_FREE(ctx->fonts, f)
op_fontlist = NULL;
op_fsml = NULL;
op_fbig = NULL;
EINA_LIST_FREE(fonts, f)
{ {
eina_stringshare_del(f->full_name); eina_stringshare_del(f->full_name);
eina_stringshare_del(f->pretty_name); eina_stringshare_del(f->pretty_name);
free(f); free(f);
} }
if (fonthash) eina_hash_free(ctx->fonthash);
{
eina_hash_free(fonthash); free(ctx);
fonthash = NULL;
}
} }
static void static void
@ -372,11 +393,11 @@ _cb_font_bolditalic(void *data,
Evas_Object *obj, Evas_Object *obj,
void *_event EINA_UNUSED) void *_event EINA_UNUSED)
{ {
Evas_Object *term = data; Font_Ctx *ctx = data;
Config *config = termio_config_get(term); Config *config = ctx->config;
config->font.bolditalic = elm_check_state_get(obj); config->font.bolditalic = elm_check_state_get(obj);
termio_config_update(term); termio_config_update(ctx->term);
config_save(config, NULL); config_save(config, NULL);
} }
@ -390,8 +411,13 @@ options_font(Evas_Object *opbox, Evas_Object *term)
Elm_Object_Item *it, *sel_it = NULL, *sel_it2 = NULL, *grp_it = NULL; Elm_Object_Item *it, *sel_it = NULL, *sel_it2 = NULL, *grp_it = NULL;
Elm_Genlist_Item_Class *it_class, *it_group; Elm_Genlist_Item_Class *it_class, *it_group;
Config *config = termio_config_get(term); Config *config = termio_config_get(term);
Font_Ctx *ctx;
options_font_clear(); ctx = calloc(1, sizeof(*ctx));
assert(ctx);
ctx->config = config;
ctx->term = term;
fr = o = elm_frame_add(opbox); fr = o = elm_frame_add(opbox);
evas_object_size_hint_weight_set(o, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND); evas_object_size_hint_weight_set(o, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
@ -400,6 +426,9 @@ options_font(Evas_Object *opbox, Evas_Object *term)
elm_box_pack_end(opbox, o); elm_box_pack_end(opbox, o);
evas_object_show(o); evas_object_show(o);
evas_object_event_callback_add(fr, EVAS_CALLBACK_DEL,
_parent_del_cb, ctx);
bx0 = o = elm_box_add(opbox); bx0 = o = elm_box_add(opbox);
evas_object_size_hint_weight_set(o, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND); evas_object_size_hint_weight_set(o, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
evas_object_size_hint_align_set(o, EVAS_HINT_FILL, EVAS_HINT_FILL); evas_object_size_hint_align_set(o, EVAS_HINT_FILL, EVAS_HINT_FILL);
@ -411,12 +440,12 @@ options_font(Evas_Object *opbox, Evas_Object *term)
evas_object_size_hint_align_set(bx, EVAS_HINT_FILL, 0.5); evas_object_size_hint_align_set(bx, EVAS_HINT_FILL, 0.5);
elm_box_horizontal_set(o, EINA_TRUE); elm_box_horizontal_set(o, EINA_TRUE);
op_fsml = o = elm_label_add(opbox); ctx->op_fsml = o = elm_label_add(opbox);
elm_object_text_set(o, "<font_size=6>A</font_size>"); elm_object_text_set(o, "<font_size=6>A</font_size>");
elm_box_pack_end(bx, o); elm_box_pack_end(bx, o);
evas_object_show(o); evas_object_show(o);
op_fontslider = o = elm_slider_add(opbox); ctx->op_fontslider = o = elm_slider_add(opbox);
evas_object_size_hint_weight_set(o, EVAS_HINT_EXPAND, 0.0); evas_object_size_hint_weight_set(o, EVAS_HINT_EXPAND, 0.0);
evas_object_size_hint_align_set(o, EVAS_HINT_FILL, 0.5); evas_object_size_hint_align_set(o, EVAS_HINT_FILL, 0.5);
elm_slider_span_size_set(o, 40); elm_slider_span_size_set(o, 40);
@ -431,9 +460,9 @@ options_font(Evas_Object *opbox, Evas_Object *term)
evas_object_show(o); evas_object_show(o);
evas_object_smart_callback_add(o, "delay,changed", evas_object_smart_callback_add(o, "delay,changed",
_cb_op_fontsize_sel, term); _cb_op_fontsize_sel, ctx);
op_fbig = o = elm_label_add(opbox); ctx->op_fbig = o = elm_label_add(opbox);
elm_object_text_set(o, "<font_size=24>A</font_size>"); elm_object_text_set(o, "<font_size=24>A</font_size>");
elm_box_pack_end(bx, o); elm_box_pack_end(bx, o);
evas_object_show(o); evas_object_show(o);
@ -450,7 +479,7 @@ options_font(Evas_Object *opbox, Evas_Object *term)
it_group->item_style = "group_index"; it_group->item_style = "group_index";
it_group->func.text_get = _cb_op_font_group_text_get; it_group->func.text_get = _cb_op_font_group_text_get;
op_fontlist = o = elm_genlist_add(opbox); ctx->op_fontlist = o = elm_genlist_add(opbox);
evas_object_size_hint_weight_set(o, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND); evas_object_size_hint_weight_set(o, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
evas_object_size_hint_align_set(o, EVAS_HINT_FILL, EVAS_HINT_FILL); evas_object_size_hint_align_set(o, EVAS_HINT_FILL, EVAS_HINT_FILL);
elm_genlist_mode_set(o, ELM_LIST_COMPRESS); elm_genlist_mode_set(o, ELM_LIST_COMPRESS);
@ -476,9 +505,9 @@ options_font(Evas_Object *opbox, Evas_Object *term)
s = strchr(file, '.'); s = strchr(file, '.');
if (s != NULL) *s = '\0'; if (s != NULL) *s = '\0';
f->pretty_name = eina_stringshare_add(file); f->pretty_name = eina_stringshare_add(file);
f->term = term; f->ctx = ctx;
f->bitmap = EINA_TRUE; f->bitmap = EINA_TRUE;
fonts = eina_list_append(fonts, f); ctx->fonts = eina_list_append(ctx->fonts, f);
f->item = it = elm_genlist_item_append(o, it_class, f, grp_it, f->item = it = elm_genlist_item_append(o, it_class, f, grp_it,
ELM_GENLIST_ITEM_NONE, ELM_GENLIST_ITEM_NONE,
@ -488,9 +517,9 @@ options_font(Evas_Object *opbox, Evas_Object *term)
(!strcmp(config->font.name, file)))) (!strcmp(config->font.name, file))))
{ {
sel_it = it; sel_it = it;
elm_object_disabled_set(op_fsml, EINA_TRUE); elm_object_disabled_set(ctx->op_fsml, EINA_TRUE);
elm_object_disabled_set(op_fontslider, EINA_TRUE); elm_object_disabled_set(ctx->op_fontslider, EINA_TRUE);
elm_object_disabled_set(op_fbig, EINA_TRUE); elm_object_disabled_set(ctx->op_fbig, EINA_TRUE);
} }
free(file); free(file);
} }
@ -499,8 +528,8 @@ options_font(Evas_Object *opbox, Evas_Object *term)
fontlist = evas_font_available_list(evas_object_evas_get(opbox)); fontlist = evas_font_available_list(evas_object_evas_get(opbox));
fontlist = eina_list_sort(fontlist, eina_list_count(fontlist), fontlist = eina_list_sort(fontlist, eina_list_count(fontlist),
_cb_op_font_sort); _cb_op_font_sort);
fonthash = eina_hash_string_superfast_new(NULL); ctx->fonthash = eina_hash_string_superfast_new(NULL);
if (fonts) if (ctx->fonts)
{ {
grp_it = elm_genlist_item_append(o, it_group, _("Standard"), NULL, grp_it = elm_genlist_item_append(o, it_group, _("Standard"), NULL,
ELM_GENLIST_ITEM_GROUP, ELM_GENLIST_ITEM_GROUP,
@ -510,19 +539,20 @@ options_font(Evas_Object *opbox, Evas_Object *term)
} }
EINA_LIST_FOREACH(fontlist, l, fname) EINA_LIST_FOREACH(fontlist, l, fname)
{ {
if (!eina_hash_find(fonthash, fname)) if (!eina_hash_find(ctx->fonthash, fname))
{ {
f = calloc(1, sizeof(Font)); f = calloc(1, sizeof(Font));
if (!f) break; if (!f)
break;
if (_parse_font_name(fname, &f->full_name, &f->pretty_name) <0) if (_parse_font_name(fname, &f->full_name, &f->pretty_name) <0)
{ {
free(f); free(f);
continue; continue;
} }
f->term = term; f->ctx = ctx;
f->bitmap = EINA_FALSE; f->bitmap = EINA_FALSE;
eina_hash_add(fonthash, eina_stringshare_add(fname), f); eina_hash_add(ctx->fonthash, eina_stringshare_add(fname), f);
fonts = eina_list_append(fonts, f); ctx->fonts = eina_list_append(ctx->fonts, f);
f->item = it = elm_genlist_item_append(o, it_class, f, grp_it, f->item = it = elm_genlist_item_append(o, it_class, f, grp_it,
ELM_GENLIST_ITEM_NONE, ELM_GENLIST_ITEM_NONE,
_cb_op_font_sel, f); _cb_op_font_sel, f);
@ -568,12 +598,12 @@ options_font(Evas_Object *opbox, Evas_Object *term)
elm_box_pack_end(bx0, o); elm_box_pack_end(bx0, o);
evas_object_show(o); evas_object_show(o);
evas_object_smart_callback_add(o, "changed", evas_object_smart_callback_add(o, "changed",
_cb_font_bolditalic, term); _cb_font_bolditalic, ctx);
expecting_resize = 0; ctx->expecting_resize = 0;
evas_object_geometry_get(term, NULL, NULL, &tsize_w, &tsize_h); evas_object_geometry_get(term, NULL, NULL, &ctx->tsize_w, &ctx->tsize_h);
evas_object_event_callback_add(term, EVAS_CALLBACK_RESIZE, evas_object_event_callback_add(term, EVAS_CALLBACK_RESIZE,
_cb_term_resize, term); _cb_term_resize, ctx);
evas_object_event_callback_add(opbox, EVAS_CALLBACK_DEL, evas_object_event_callback_add(opbox, EVAS_CALLBACK_DEL,
_cb_font_del, term); _cb_font_del, ctx);
} }

View File

@ -1,7 +1,6 @@
#ifndef _OPTIONS_FONT_H__ #ifndef _OPTIONS_FONT_H__
#define _OPTIONS_FONT_H__ 1 #define _OPTIONS_FONT_H__ 1
void options_font_clear(void);
void options_font(Evas_Object *opbox, Evas_Object *term); void options_font(Evas_Object *opbox, Evas_Object *term);
#endif #endif