1 #include <Elementary.h>
5 * @defgroup Label Label
8 * Display text, with simple html-like markup. The theme of course
9 * can invent new markup tags and style them any way it likes
12 typedef struct _Widget_Data Widget_Data;
20 Ecore_Job *deferred_recalc_job;
23 Eina_Bool linewrap : 1;
24 Eina_Bool changed : 1;
25 Eina_Bool bgcolor : 1;
26 Eina_Bool ellipsis : 1;
29 static const char *widtype = NULL;
30 static void _del_hook(Evas_Object *obj);
31 static void _theme_hook(Evas_Object *obj);
32 static void _sizing_eval(Evas_Object *obj);
33 static int _get_value_in_key_string(const char *oldstring, char *key, char **value);
34 static int _strbuf_key_value_replace(Eina_Strbuf *srcbuf, char *key, const char *value, int deleteflag);
35 static int _stringshare_key_value_replace(const char **srcstring, char *key, const char *value, int deleteflag);
36 static int _is_width_over(Evas_Object *obj, int linemode);
37 static void _ellipsis_label_to_width(Evas_Object *obj, int linemode);
40 _elm_win_recalc_job(void *data)
42 Widget_Data *wd = elm_widget_data_get(data);
43 Evas_Coord minw = -1, minh = -1, maxh = -1;
44 Evas_Coord resw, resh, minminw, minminh;
46 wd->deferred_recalc_job = NULL;
47 evas_object_geometry_get(wd->lbl, NULL, NULL, &resw, &resh);
50 edje_object_size_min_restricted_calc(wd->lbl, &minw, &minh, 0, 0);
53 if (wd->wrap_w >= resw)
56 edje_object_size_min_restricted_calc(wd->lbl, &minw, &minh, resw, 0);
57 evas_object_size_hint_min_set(data, minw, minh);
61 if (wd->wrap_w > minminw) minminw = wd->wrap_w;
62 edje_object_size_min_restricted_calc(wd->lbl, &minw, &minh, resw, 0);
63 evas_object_size_hint_min_set(data, minminw, minh);
66 if (wd->ellipsis && wd->wrap_h > 0 && _is_width_over(data, 1) == 1)
67 _ellipsis_label_to_width(data, 1);
70 evas_object_size_hint_max_set(data, -1, maxh);
74 _del_hook(Evas_Object *obj)
76 Widget_Data *wd = elm_widget_data_get(obj);
78 if (wd->deferred_recalc_job) ecore_job_del(wd->deferred_recalc_job);
79 if (wd->label) eina_stringshare_del(wd->label);
80 if (wd->bg) evas_object_del(wd->bg);
85 _theme_hook(Evas_Object *obj)
87 Widget_Data *wd = elm_widget_data_get(obj);
90 _elm_theme_object_set(obj, wd->lbl, "label", "base_wrap", elm_widget_style_get(obj));
92 _elm_theme_object_set(obj, wd->lbl, "label", "base", elm_widget_style_get(obj));
93 edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
94 edje_object_scale_set(wd->lbl, elm_widget_scale_get(obj) *
100 _sizing_eval(Evas_Object *obj)
102 Widget_Data *wd = elm_widget_data_get(obj);
103 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
104 Evas_Coord resw, resh;
108 evas_object_geometry_get(wd->lbl, NULL, NULL, &resw, &resh);
109 if ((resw == wd->lastw) && (!wd->changed)) return;
110 wd->changed = EINA_FALSE;
112 _elm_win_recalc_job(obj);
113 // FIXME: works ok. but NOT for genlist. what should genlist do?
114 // if (wd->deferred_recalc_job) ecore_job_del(wd->deferred_recalc_job);
115 // wd->deferred_recalc_job = ecore_job_add(_elm_win_recalc_job, obj);
119 evas_object_geometry_get(wd->lbl, NULL, NULL, &resw, &resh);
120 edje_object_size_min_calc(wd->lbl, &minw, &minh);
121 evas_object_size_hint_min_set(obj, minw, minh);
123 evas_object_size_hint_max_set(obj, maxw, maxh);
125 if (wd->ellipsis && _is_width_over(obj, 0) == 1)
126 _ellipsis_label_to_width(obj, 0);
131 _resize(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
133 Widget_Data *wd = elm_widget_data_get(data);
135 if (wd->linewrap) _sizing_eval(data);
139 _get_value_in_key_string(const char *oldstring, char *key, char **value)
141 char *curlocater, *starttag, *endtag;
142 int firstindex = 0, foundflag = -1;
144 curlocater = strstr(oldstring, key);
147 starttag = curlocater;
148 endtag = curlocater + strlen(key);
149 if (endtag == NULL || *endtag != '=')
155 firstindex = abs(oldstring - curlocater);
156 firstindex += strlen(key)+1; // strlen("key") + strlen("=")
157 *value = (char*)oldstring + firstindex;
159 while (oldstring != starttag)
161 if (*starttag == '>')
166 if (*starttag == '<')
170 if (starttag == NULL) break;
173 while (NULL != endtag)
184 if (endtag == NULL) break;
187 if (foundflag != 0 && *starttag == '<' && *endtag == '>')
197 if (foundflag == 1) return 0;
203 _strbuf_key_value_replace(Eina_Strbuf *srcbuf, char *key, const char *value, int deleteflag)
205 const char *srcstring = NULL;
206 Eina_Strbuf *repbuf = NULL, *diffbuf = NULL;
207 char *curlocater, *replocater;
208 char *starttag, *endtag;
209 int tagtxtlen = 0, insertflag = 0;
211 srcstring = eina_strbuf_string_get(srcbuf);
212 curlocater = strstr(srcstring, key);
214 if (curlocater == NULL)
222 starttag = strchr(srcstring, '<');
223 endtag = strchr(srcstring, '>');
224 tagtxtlen = endtag - starttag;
225 if (tagtxtlen <= 0) tagtxtlen = 0;
226 if (starttag < curlocater && curlocater < endtag) break;
227 if (endtag != NULL && endtag+1 != NULL)
228 srcstring = endtag+1;
231 } while (strlen(srcstring) > 1);
233 if (starttag && endtag && tagtxtlen > strlen(key))
235 repbuf = eina_strbuf_new();
236 diffbuf = eina_strbuf_new();
237 eina_strbuf_append_n(repbuf, starttag, tagtxtlen);
238 srcstring = eina_strbuf_string_get(repbuf);
239 curlocater = strstr(srcstring, key);
241 if (curlocater != NULL)
243 replocater = curlocater + strlen(key) + 1;
245 while (*replocater == ' ' || *replocater == '=')
250 while (*replocater != NULL && *replocater != ' ' && *replocater != '>')
253 if (replocater-curlocater > strlen(key)+1)
256 eina_strbuf_append_n(diffbuf, curlocater, replocater-curlocater+1);
265 eina_strbuf_reset(repbuf);
273 if (repbuf == NULL) repbuf = eina_strbuf_new();
274 if (diffbuf == NULL) diffbuf = eina_strbuf_new();
278 eina_strbuf_append_printf(repbuf, "<%s=%s>", key, value);
279 eina_strbuf_prepend(srcbuf, eina_strbuf_string_get(repbuf));
285 eina_strbuf_prepend(diffbuf, "<");
286 eina_strbuf_append(diffbuf, ">");
287 eina_strbuf_replace_first(srcbuf, eina_strbuf_string_get(diffbuf), "");
291 eina_strbuf_append_printf(repbuf, "%s=%s", key, value);
292 eina_strbuf_replace_first(srcbuf, eina_strbuf_string_get(diffbuf), eina_strbuf_string_get(repbuf));
296 if (repbuf) eina_strbuf_free(repbuf);
297 if (diffbuf) eina_strbuf_free(diffbuf);
303 _stringshare_key_value_replace(const char **srcstring, char *key, const char *value, int deleteflag)
305 Eina_Strbuf *sharebuf = NULL;
307 sharebuf = eina_strbuf_new();
308 eina_strbuf_append(sharebuf, *srcstring);
309 _strbuf_key_value_replace(sharebuf, key, value, deleteflag);
310 eina_stringshare_del(*srcstring);
311 *srcstring = eina_stringshare_add(eina_strbuf_string_get(sharebuf));
312 eina_strbuf_free(sharebuf);
318 _is_width_over(Evas_Object *obj, int linemode)
320 Evas_Coord x, y, w, h;
321 Evas_Coord vx, vy, vw, vh;
322 Widget_Data *wd = elm_widget_data_get(obj);
323 const char *ellipsis_string = "...";
324 size_t ellen = strlen(ellipsis_string)+1;
328 edje_object_part_geometry_get(wd->lbl,"elm.text",&x,&y,&w,&h);
330 evas_object_geometry_get (obj, &vx,&vy,&vw,&vh);
332 if (linemode == 0) // single line
334 if (x >= 0 && y >= 0) return 0;
336 if (ellen < wd->wrap_w && w > wd->wrap_w) return 1;
340 if (vy > h && h > wd->wrap_h) return 1;
347 _ellipsis_label_to_width(Evas_Object *obj, int linemode)
349 Widget_Data *wd = elm_widget_data_get(obj);
350 int cur_fontsize = 0, len, showcount;
351 Eina_Strbuf *fontbuf = NULL, *txtbuf = NULL;
352 char **kvalue = NULL;
353 const char *minfont, *deffont, *maxfont;
354 const char *ellipsis_string = "...";
355 int minfontsize, maxfontsize, minshowcount;
357 minshowcount = strlen(ellipsis_string) + 1;
358 minfont = edje_object_data_get(wd->lbl, "min_font_size");
359 if (minfont) minfontsize = atoi(minfont);
360 else minfontsize = 1;
361 maxfont = edje_object_data_get(wd->lbl, "max_font_size");
362 if (maxfont) maxfontsize = atoi(maxfont);
363 else maxfontsize = 1;
364 deffont = edje_object_data_get(wd->lbl, "default_font_size");
365 if (deffont) cur_fontsize = atoi(deffont);
366 else cur_fontsize = 1;
367 if (minfontsize > maxfontsize || cur_fontsize == 1) return; // theme is not ready for ellipsis
368 if (eina_stringshare_strlen(wd->label) <= 0) return;
370 if (_get_value_in_key_string(wd->label, "font_size", &kvalue) == 0)
372 if (*kvalue != NULL) cur_fontsize = atoi((char*)kvalue);
375 txtbuf = eina_strbuf_new();
376 eina_strbuf_append(txtbuf, wd->label);
378 while (_is_width_over(obj, linemode))
380 if (cur_fontsize > minfontsize)
385 eina_strbuf_free(fontbuf);
388 fontbuf = eina_strbuf_new();
389 eina_strbuf_append_printf(fontbuf, "%d", cur_fontsize);
390 _strbuf_key_value_replace(txtbuf, "font_size", eina_strbuf_string_get(fontbuf), 0);
391 edje_object_part_text_set(wd->lbl, "elm.text", eina_strbuf_string_get(txtbuf));
392 eina_strbuf_free(fontbuf);
399 eina_strbuf_free(txtbuf);
402 txtbuf = eina_strbuf_new();
403 eina_strbuf_append_printf(txtbuf, "%s", edje_object_part_text_get(wd->lbl, "elm.text"));
404 len = eina_strbuf_length_get(txtbuf);
406 while (showcount > minshowcount)
408 unsigned char *ltxt = eina_strbuf_string_get(txtbuf);
409 len = eina_strbuf_length_get(txtbuf);
410 // FIXME : more reliable truncate routine is needed
411 // it works on only EUC-KR
413 if (ltxt[len-minshowcount-delta] >= 0x80)
418 eina_strbuf_remove(txtbuf, len-minshowcount-delta, len);
419 eina_strbuf_append(txtbuf, ellipsis_string);
420 edje_object_part_text_set(wd->lbl, "elm.text", eina_strbuf_string_get(txtbuf));
422 if (_is_width_over(obj, linemode))
430 if (txtbuf) eina_strbuf_free(txtbuf);
436 * Add a new label to the parent
438 * @param parent The parent object
439 * @return The new object or NULL if it cannot be created
444 elm_label_add(Evas_Object *parent)
450 wd = ELM_NEW(Widget_Data);
451 e = evas_object_evas_get(parent);
452 wd->bg = evas_object_rectangle_add(e);
453 evas_object_color_set(wd->bg, 0, 0, 0, 0);
454 obj = elm_widget_add(e);
455 ELM_SET_WIDTYPE(widtype, "label");
456 elm_widget_type_set(obj, "label");
457 elm_widget_sub_object_add(parent, obj);
458 elm_widget_data_set(obj, wd);
459 elm_widget_del_hook_set(obj, _del_hook);
460 elm_widget_theme_hook_set(obj, _theme_hook);
461 elm_widget_can_focus_set(obj, 0);
463 wd->linewrap = EINA_FALSE;
464 wd->bgcolor = EINA_FALSE;
465 wd->ellipsis = EINA_FALSE;
469 wd->lbl = edje_object_add(e);
470 _elm_theme_object_set(obj, wd->lbl, "label", "base", "default");
471 wd->label = eina_stringshare_add("<br>");
472 edje_object_part_text_set(wd->lbl, "elm.text", "<br>");
473 elm_widget_resize_object_set(obj, wd->lbl);
475 evas_object_event_callback_add(wd->lbl, EVAS_CALLBACK_RESIZE, _resize, obj);
483 * Set the label on the label object
485 * @param obj The label object
486 * @param label The label will be used on the label object
491 elm_label_label_set(Evas_Object *obj, const char *label)
493 ELM_CHECK_WIDTYPE(obj, widtype);
494 Widget_Data *wd = elm_widget_data_get(obj);
496 if (!label) label = "";
497 eina_stringshare_replace(&wd->label, label);
498 edje_object_part_text_set(wd->lbl, "elm.text", label);
504 * Get the label used on the label object
506 * @param obj The label object
507 * @return The string inside the label
511 elm_label_label_get(const Evas_Object *obj)
513 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
514 Widget_Data *wd = elm_widget_data_get(obj);
515 if (!wd) return NULL;
520 * Set the wrapping behavior of the label
522 * @param obj The label object
523 * @param wrap To wrap text or not
527 elm_label_line_wrap_set(Evas_Object *obj, Eina_Bool wrap)
529 ELM_CHECK_WIDTYPE(obj, widtype);
530 Widget_Data *wd = elm_widget_data_get(obj);
533 if (wd->linewrap == wrap) return;
535 t = eina_stringshare_add(elm_label_label_get(obj));
537 _elm_theme_object_set(obj, wd->lbl, "label", "base_wrap", elm_widget_style_get(obj));
539 _elm_theme_object_set(obj, wd->lbl, "label", "base", elm_widget_style_get(obj));
540 elm_label_label_set(obj, t);
541 eina_stringshare_del(t);
547 * Get the wrapping behavior of the label
549 * @param obj The label object
550 * @return To wrap text or not
554 elm_label_line_wrap_get(const Evas_Object *obj)
556 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
557 Widget_Data *wd = elm_widget_data_get(obj);
558 if (!wd) return EINA_FALSE;
563 * Set wrap width of the label
565 * @param obj The label object
566 * @param w The wrap width in pixels at a minimum where words need to wrap
570 elm_label_wrap_width_set(Evas_Object *obj, Evas_Coord w)
572 ELM_CHECK_WIDTYPE(obj, widtype);
573 Widget_Data *wd = elm_widget_data_get(obj);
575 if (wd->wrap_w == w) return;
581 * get wrap width of the label
583 * @param obj The label object
584 * @return The wrap width in pixels at a minimum where words need to wrap
588 elm_label_wrap_width_get(const Evas_Object *obj)
590 ELM_CHECK_WIDTYPE(obj, widtype) 0;
591 Widget_Data *wd = elm_widget_data_get(obj);
597 * Set wrap height of the label
599 * @param obj The label object
600 * @param w The wrap width in pixels at a minimum where words need to wrap
604 elm_label_wrap_height_set(Evas_Object *obj, Evas_Coord h)
606 ELM_CHECK_WIDTYPE(obj, widtype);
607 Widget_Data *wd = elm_widget_data_get(obj);
609 if (wd->wrap_h == h) return;
615 * get wrap width of the label
617 * @param obj The label object
618 * @return The wrap height in pixels at a minimum where words need to wrap
622 elm_label_wrap_height_get(const Evas_Object *obj)
624 ELM_CHECK_WIDTYPE(obj, widtype) 0;
625 Widget_Data *wd = elm_widget_data_get(obj);
631 * Set the font size on the label object
633 * @param obj The label object
634 * @param size font size
639 elm_label_fontsize_set(Evas_Object *obj, int fontsize)
641 ELM_CHECK_WIDTYPE(obj, widtype);
642 Widget_Data *wd = elm_widget_data_get(obj);
643 Eina_Strbuf *fontbuf = NULL;
644 int len, removeflag = 0;
647 len = strlen(wd->label);
648 if (len <= 0) return;
649 fontbuf = eina_strbuf_new();
650 eina_strbuf_append_printf(fontbuf, "%d", fontsize);
652 if (fontsize == 0) removeflag = 1; // remove fontsize tag
654 if (_stringshare_key_value_replace(&wd->label, "font_size", eina_strbuf_string_get(fontbuf), removeflag) == 0)
656 edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
660 eina_strbuf_free(fontbuf);
664 * Set the text align on the label object
666 * @param obj The label object
667 * @param align align mode ("left", "center", "right")
672 elm_label_text_align_set(Evas_Object *obj, const char *alignmode)
674 ELM_CHECK_WIDTYPE(obj, widtype);
675 Widget_Data *wd = elm_widget_data_get(obj);
679 len = strlen(wd->label);
680 if (len <= 0) return;
682 if (_stringshare_key_value_replace(&wd->label, "align", alignmode, 0) == 0)
683 edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
690 * Set the text color on the label object
692 * @param obj The label object
693 * @param r Red property background color of The label object
694 * @param g Green property background color of The label object
695 * @param b Blue property background color of The label object
696 * @param a Alpha property background alpha of The label object
701 elm_label_text_color_set(Evas_Object *obj, unsigned int r, unsigned int g, unsigned int b, unsigned int a)
703 ELM_CHECK_WIDTYPE(obj, widtype);
704 Widget_Data *wd = elm_widget_data_get(obj);
705 Eina_Strbuf *colorbuf = NULL;
709 len = strlen(wd->label);
710 if (len <= 0) return;
711 colorbuf = eina_strbuf_new();
712 eina_strbuf_append_printf(colorbuf, "#%02X%02X%02X%02X", r, g, b, a);
714 if (_stringshare_key_value_replace(&wd->label, "color", eina_strbuf_string_get(colorbuf), 0) == 0)
716 edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
720 eina_strbuf_free(colorbuf);
725 * Set background color of the label
727 * @param obj The label object
728 * @param r Red property background color of The label object
729 * @param g Green property background color of The label object
730 * @param b Blue property background color of The label object
731 * @param a Alpha property background alpha of The label object
735 elm_label_background_color_set(Evas_Object *obj, unsigned int r, unsigned int g, unsigned int b, unsigned int a)
737 ELM_CHECK_WIDTYPE(obj, widtype);
738 Widget_Data *wd = elm_widget_data_get(obj);
739 evas_object_color_set(wd->bg, r, g, b, a);
741 if (wd->bgcolor == EINA_FALSE)
744 edje_object_part_swallow(wd->lbl, "label.swallow.background", wd->bg);
749 * Set the ellipsis behavior of the label
751 * @param obj The label object
752 * @param ellipsis To ellipsis text or not
756 elm_label_ellipsis_set(Evas_Object *obj, Eina_Bool ellipsis)
758 ELM_CHECK_WIDTYPE(obj, widtype);
759 Widget_Data *wd = elm_widget_data_get(obj);
760 if (wd->ellipsis == ellipsis) return;
761 wd->ellipsis = ellipsis;