legacy/experimental/ecdb/trunk/src/ecdb_widgets.c

261 lines
6.8 KiB
C
Raw Normal View History

/* vim: set sw=3 ts=3 sts=3 expandtab: */
#include "ecdb.h"
static char *ecdb_entry_markup_to_text(const char *mkup);
static char *_str_append(char *str, const char *txt, int *len, int *alloc);
Evas_Object *
ecdb_widget_add(Evas_Object *parent, const char *name)
{
Evas_Object *o;
int x, y, w, h;
o = edje_object_add(evas_object_evas_get(parent));
evas_object_name_set(o, name);
edje_object_part_geometry_get(parent, name, &x, &y, &w, &h);
evas_object_move(o, x, y);
evas_object_resize(o, w, h);
edje_object_part_swallow(parent, name, o);
return o;
}
Evas_Object *
ecdb_button_add(Evas_Object *parent, const char *name)
{
Evas_Object *b;
b = ecdb_widget_add(parent, name);
edje_object_file_set(b, em->theme_path, "ecdb/button");
return b;
}
void
ecdb_button_label_set(Evas_Object *b, const char *label)
{
edje_object_part_text_set(b, "ecdb.label", label);
}
void
ecdb_button_icon_set(Evas_Object *b, const char *group)
{
Evas_Object *icon;
icon = ecdb_widget_add(b, "ecdb.swallow.icon");
edje_object_file_set(icon, em->theme_path, group);
evas_object_show(icon);
}
Evas_Object *
ecdb_check_add(Evas_Object *parent, const char *name)
{
Evas_Object *c;
c = ecdb_widget_add(parent, name);
edje_object_file_set(c, em->theme_path, "ecdb/check");
return c;
}
void
ecdb_check_label_set(Evas_Object *c, const char *label)
{
edje_object_part_text_set(c, "ecdb.label", label);
}
void
ecdb_check_states_set(Evas_Object *c, const char *ystate, const char *nstate)
{
edje_object_part_text_set(c, "ecdb.ystate", ystate);
edje_object_part_text_set(c, "ecdb.nstate", nstate);
}
void
ecdb_check_state_set(Evas_Object *c, int state)
{
if (state)
{
edje_object_signal_emit(c, "ecdb,check,on", "ecdb");
}
else
{
edje_object_signal_emit(c, "ecdb,check,off", "ecdb");
}
}
Evas_Object *
ecdb_entry_add(Evas_Object *parent, const char *name)
{
Evas_Object *e;
e = ecdb_widget_add(parent, name);
edje_object_file_set(e, em->theme_path, "ecdb/entry");
em->dnd_candidates = eina_list_append(em->dnd_candidates, e);
evas_object_data_set(e, "dnd_call_func", ecdb_dnd_entry_dnd_set);
return e;
}
void
ecdb_entry_text_set(Evas_Object *e, const char *text)
{
edje_object_part_text_set(e, "ecdb.text", text);
}
char *
ecdb_entry_text_get(Evas_Object *e)
{
return ecdb_entry_markup_to_text(edje_object_part_text_get(e, "ecdb.text"));
}
/* Shamelessly stolen from Elementary... */
static char *
ecdb_entry_markup_to_text(const char *mkup)
{
char *str = NULL;
int str_len = 0, str_alloc = 0;
// FIXME: markup -> text
char *s, *p;
char *tag_start, *tag_end, *esc_start, *esc_end, *ts;
tag_start = tag_end = esc_start = esc_end = NULL;
p = (char *)mkup;
s = p;
for (;;)
{
if ((*p == 0) ||
(tag_end) || (esc_end) ||
(tag_start) || (esc_start))
{
if (tag_end)
{
char *ttag;
ttag = malloc(tag_end - tag_start);
if (ttag)
{
strncpy(ttag, tag_start + 1, tag_end - tag_start - 1);
ttag[tag_end - tag_start - 1] = 0;
/*
if (!strcmp(ttag, "br"))
str = _str_append(str, "\n", &str_len, &str_alloc);
*/
if (!strcmp(ttag, "\n"))
str = _str_append(str, "\n", &str_len, &str_alloc);
else if (!strcmp(ttag, "\\n"))
str = _str_append(str, "\n", &str_len, &str_alloc);
else if (!strcmp(ttag, "\t"))
str = _str_append(str, "\t", &str_len, &str_alloc);
else if (!strcmp(ttag, "\\t"))
str = _str_append(str, "\t", &str_len, &str_alloc);
free(ttag);
}
tag_start = tag_end = NULL;
}
else if (esc_end)
{
ts = malloc(esc_end - esc_start + 1);
if (ts)
{
const char *esc;
strncpy(ts, esc_start, esc_end - esc_start);
ts[esc_end - esc_start] = 0;
esc = evas_textblock_escape_string_get(ts);
if (esc)
str = _str_append(str, esc, &str_len, &str_alloc);
free(ts);
}
esc_start = esc_end = NULL;
}
else if (*p == 0)
{
ts = malloc(p - s + 1);
if (ts)
{
strncpy(ts, s, p - s);
ts[p - s] = 0;
str = _str_append(str, ts, &str_len, &str_alloc);
free(ts);
}
s = NULL;
}
if (*p == 0)
break;
}
if (*p == '<')
{
if (!esc_start)
{
tag_start = p;
tag_end = NULL;
ts = malloc(p - s + 1);
if (ts)
{
strncpy(ts, s, p - s);
ts[p - s] = 0;
str = _str_append(str, ts, &str_len, &str_alloc);
free(ts);
}
s = NULL;
}
}
else if (*p == '>')
{
if (tag_start)
{
tag_end = p;
s = p + 1;
}
}
else if (*p == '&')
{
if (!tag_start)
{
esc_start = p;
esc_end = NULL;
ts = malloc(p - s + 1);
if (ts)
{
strncpy(ts, s, p - s);
ts[p - s] = 0;
str = _str_append(str, ts, &str_len, &str_alloc);
free(ts);
}
s = NULL;
}
}
else if (*p == ';')
{
if (esc_start)
{
esc_end = p;
s = p + 1;
}
}
p++;
}
return str;
}
static char *
_str_append(char *str, const char *txt, int *len, int *alloc)
{
int txt_len = strlen(txt);
if (txt_len <= 0) return str;
if ((*len + txt_len) >= *alloc)
{
char *str2;
int alloc2;
alloc2 = *alloc + txt_len + 128;
str2 = realloc(str, alloc2);
if (!str2) return str;
*alloc = alloc2;
str = str2;
}
strcpy(str + *len, txt);
*len += txt_len;
return str;
}