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 wrapmode : 1;
25 Eina_Bool slidingmode : 1;
26 Eina_Bool slidingellipsis : 1;
27 Eina_Bool changed : 1;
28 Eina_Bool bgcolor : 1;
29 Eina_Bool ellipsis : 1;
32 static const char *widtype = NULL;
33 static void _del_hook(Evas_Object *obj);
34 static void _theme_hook(Evas_Object *obj);
35 static void _sizing_eval(Evas_Object *obj);
36 static int _get_value_in_key_string(const char *oldstring, char *key, char **value);
37 static int _strbuf_key_value_replace(Eina_Strbuf *srcbuf, char *key, const char *value, int deleteflag);
38 static int _stringshare_key_value_replace(const char **srcstring, char *key, const char *value, int deleteflag);
39 static int _is_width_over(Evas_Object *obj, int linemode);
40 static void _ellipsis_label_to_width(Evas_Object *obj, int linemode);
43 _elm_win_recalc_job(void *data)
45 Widget_Data *wd = elm_widget_data_get(data);
46 Evas_Coord minw = -1, minh = -1, maxh = -1;
47 Evas_Coord resw, resh, minminw, minminh;
49 wd->deferred_recalc_job = NULL;
50 evas_object_geometry_get(wd->lbl, NULL, NULL, &resw, &resh);
53 edje_object_size_min_restricted_calc(wd->lbl, &minw, &minh, 0, 0);
56 if (wd->wrap_w >= resw)
59 edje_object_size_min_restricted_calc(wd->lbl, &minw, &minh, resw, 0);
60 evas_object_size_hint_min_set(data, minw, minh);
64 if (wd->wrap_w > minminw) minminw = wd->wrap_w;
65 edje_object_size_min_restricted_calc(wd->lbl, &minw, &minh, resw, 0);
66 evas_object_size_hint_min_set(data, minminw, minh);
69 if (wd->ellipsis && wd->linewrap && wd->wrap_h > 0 && _is_width_over(data, 1) == 1)
70 _ellipsis_label_to_width(data, 1);
73 evas_object_size_hint_max_set(data, -1, maxh);
77 _del_hook(Evas_Object *obj)
79 Widget_Data *wd = elm_widget_data_get(obj);
81 if (wd->deferred_recalc_job) ecore_job_del(wd->deferred_recalc_job);
82 if (wd->label) eina_stringshare_del(wd->label);
83 if (wd->bg) evas_object_del(wd->bg);
88 _theme_change(Evas_Object *obj)
90 Widget_Data *wd = elm_widget_data_get(obj);
96 _elm_theme_object_set(obj, wd->lbl, "label", "base_wrap_ellipsis", elm_widget_style_get(obj));
98 _elm_theme_object_set(obj, wd->lbl, "label", "base_wrap", elm_widget_style_get(obj));
101 _elm_theme_object_set(obj, wd->lbl, "label", "base", elm_widget_style_get(obj));
106 _theme_hook(Evas_Object *obj)
108 Widget_Data *wd = elm_widget_data_get(obj);
111 edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
112 edje_object_scale_set(wd->lbl, elm_widget_scale_get(obj) *
118 _sizing_eval(Evas_Object *obj)
120 Widget_Data *wd = elm_widget_data_get(obj);
121 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
122 Evas_Coord resw, resh;
126 evas_object_geometry_get(wd->lbl, NULL, NULL, &resw, &resh);
127 if ((resw == wd->lastw) && (!wd->changed)) return;
128 wd->changed = EINA_FALSE;
130 _elm_win_recalc_job(obj);
131 // FIXME: works ok. but NOT for genlist. what should genlist do?
132 // if (wd->deferred_recalc_job) ecore_job_del(wd->deferred_recalc_job);
133 // wd->deferred_recalc_job = ecore_job_add(_elm_win_recalc_job, obj);
137 evas_object_geometry_get(wd->lbl, NULL, NULL, &resw, &resh);
138 edje_object_size_min_calc(wd->lbl, &minw, &minh);
139 if (wd->wrap_w > 0 && minw > wd->wrap_w)
141 evas_object_size_hint_min_set(obj, minw, minh);
143 evas_object_size_hint_max_set(obj, maxw, maxh);
145 if (wd->ellipsis && _is_width_over(obj, 0) == 1)
146 _ellipsis_label_to_width(obj, 0);
151 _resize(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
153 Widget_Data *wd = elm_widget_data_get(data);
155 if (wd->linewrap) _sizing_eval(data);
159 _get_value_in_key_string(const char *oldstring, char *key, char **value)
161 char *curlocater, *starttag, *endtag;
162 int firstindex = 0, foundflag = -1;
164 curlocater = strstr(oldstring, key);
167 starttag = curlocater;
168 endtag = curlocater + strlen(key);
169 if (endtag == NULL || *endtag != '=')
175 firstindex = abs(oldstring - curlocater);
176 firstindex += strlen(key)+1; // strlen("key") + strlen("=")
177 *value = (char*)oldstring + firstindex;
179 while (oldstring != starttag)
181 if (*starttag == '>')
186 if (*starttag == '<')
190 if (starttag == NULL) break;
193 while (NULL != endtag)
204 if (endtag == NULL) break;
207 if (foundflag != 0 && *starttag == '<' && *endtag == '>')
217 if (foundflag == 1) return 0;
223 _strbuf_key_value_replace(Eina_Strbuf *srcbuf, char *key, const char *value, int deleteflag)
225 const char *srcstring = NULL;
226 Eina_Strbuf *repbuf = NULL, *diffbuf = NULL;
227 char *curlocater, *replocater;
228 char *starttag, *endtag;
229 int tagtxtlen = 0, insertflag = 0;
231 srcstring = eina_strbuf_string_get(srcbuf);
232 curlocater = strstr(srcstring, key);
234 if (curlocater == NULL)
242 starttag = strchr(srcstring, '<');
243 endtag = strchr(srcstring, '>');
244 tagtxtlen = endtag - starttag;
245 if (tagtxtlen <= 0) tagtxtlen = 0;
246 if (starttag < curlocater && curlocater < endtag) break;
247 if (endtag != NULL && endtag+1 != NULL)
248 srcstring = endtag+1;
251 } while (strlen(srcstring) > 1);
253 if (starttag && endtag && tagtxtlen > strlen(key))
255 repbuf = eina_strbuf_new();
256 diffbuf = eina_strbuf_new();
257 eina_strbuf_append_n(repbuf, starttag, tagtxtlen);
258 srcstring = eina_strbuf_string_get(repbuf);
259 curlocater = strstr(srcstring, key);
261 if (curlocater != NULL)
263 replocater = curlocater + strlen(key) + 1;
265 while (*replocater == ' ' || *replocater == '=')
270 while (*replocater != NULL && *replocater != ' ' && *replocater != '>')
273 if (replocater-curlocater > strlen(key)+1)
276 eina_strbuf_append_n(diffbuf, curlocater, replocater-curlocater+1);
285 eina_strbuf_reset(repbuf);
293 if (repbuf == NULL) repbuf = eina_strbuf_new();
294 if (diffbuf == NULL) diffbuf = eina_strbuf_new();
298 eina_strbuf_append_printf(repbuf, "<%s=%s>", key, value);
299 eina_strbuf_prepend(srcbuf, eina_strbuf_string_get(repbuf));
305 eina_strbuf_prepend(diffbuf, "<");
306 eina_strbuf_append(diffbuf, ">");
307 eina_strbuf_replace_first(srcbuf, eina_strbuf_string_get(diffbuf), "");
311 eina_strbuf_append_printf(repbuf, "%s=%s", key, value);
312 eina_strbuf_replace_first(srcbuf, eina_strbuf_string_get(diffbuf), eina_strbuf_string_get(repbuf));
316 if (repbuf) eina_strbuf_free(repbuf);
317 if (diffbuf) eina_strbuf_free(diffbuf);
323 _stringshare_key_value_replace(const char **srcstring, char *key, const char *value, int deleteflag)
325 Eina_Strbuf *sharebuf = NULL;
327 sharebuf = eina_strbuf_new();
328 eina_strbuf_append(sharebuf, *srcstring);
329 _strbuf_key_value_replace(sharebuf, key, value, deleteflag);
330 eina_stringshare_del(*srcstring);
331 *srcstring = eina_stringshare_add(eina_strbuf_string_get(sharebuf));
332 eina_strbuf_free(sharebuf);
338 _is_width_over(Evas_Object *obj, int linemode)
340 Evas_Coord x, y, w, h;
341 Evas_Coord vx, vy, vw, vh;
342 Widget_Data *wd = elm_widget_data_get(obj);
343 const char *ellipsis_string = "...";
344 size_t ellen = strlen(ellipsis_string)+1;
348 edje_object_part_geometry_get(wd->lbl,"elm.text",&x,&y,&w,&h);
350 evas_object_geometry_get (obj, &vx,&vy,&vw,&vh);
352 if (linemode == 0) // single line
354 if ((x >= 0) && (y >= 0))
356 if ((wd->wrap_w > 0) && (wd->wrap_w < w))
358 Evas_Coord minw, minh;
359 edje_object_size_min_calc(wd->lbl, &minw, &minh);
361 if (minw < wd->wrap_w)
362 { // min insufficient
372 if (ellen < wd->wrap_w && w > wd->wrap_w) return 1;
376 if (vy > h && h > wd->wrap_h) return 1;
383 _ellipsis_label_to_width(Evas_Object *obj, int linemode)
385 Widget_Data *wd = elm_widget_data_get(obj);
386 int cur_fontsize = 0, len, showcount;
387 Eina_Strbuf *fontbuf = NULL, *txtbuf = NULL;
388 char **kvalue = NULL;
389 const char *minfont, *deffont, *maxfont;
390 const char *ellipsis_string = "...";
391 int minfontsize, maxfontsize, minshowcount;
393 minshowcount = strlen(ellipsis_string) + 1;
394 minfont = edje_object_data_get(wd->lbl, "min_font_size");
395 if (minfont) minfontsize = atoi(minfont);
396 else minfontsize = 1;
397 maxfont = edje_object_data_get(wd->lbl, "max_font_size");
398 if (maxfont) maxfontsize = atoi(maxfont);
399 else maxfontsize = 1;
400 deffont = edje_object_data_get(wd->lbl, "default_font_size");
401 if (deffont) cur_fontsize = atoi(deffont);
402 else cur_fontsize = 1;
403 if (minfontsize > maxfontsize || cur_fontsize == 1) return; // theme is not ready for ellipsis
404 if (eina_stringshare_strlen(wd->label) <= 0) return;
406 if (_get_value_in_key_string(wd->label, "font_size", &kvalue) == 0)
408 if (*kvalue != NULL) cur_fontsize = atoi((char*)kvalue);
411 txtbuf = eina_strbuf_new();
412 eina_strbuf_append(txtbuf, wd->label);
414 while (_is_width_over(obj, linemode))
416 if (cur_fontsize > minfontsize)
421 eina_strbuf_free(fontbuf);
424 fontbuf = eina_strbuf_new();
425 eina_strbuf_append_printf(fontbuf, "%d", cur_fontsize);
426 _strbuf_key_value_replace(txtbuf, "font_size", eina_strbuf_string_get(fontbuf), 0);
427 edje_object_part_text_set(wd->lbl, "elm.text", eina_strbuf_string_get(txtbuf));
428 eina_strbuf_free(fontbuf);
435 eina_strbuf_free(txtbuf);
438 txtbuf = eina_strbuf_new();
439 eina_strbuf_append_printf(txtbuf, "%s", edje_object_part_text_get(wd->lbl, "elm.text"));
440 len = eina_strbuf_length_get(txtbuf);
442 while (showcount > minshowcount)
444 unsigned char *ltxt = eina_strbuf_string_get(txtbuf);
445 len = eina_strbuf_length_get(txtbuf);
446 // FIXME : more reliable truncate routine is needed
447 // it works on only EUC-KR
449 if (ltxt[len-minshowcount-delta] >= 0x80)
454 eina_strbuf_remove(txtbuf, len-minshowcount-delta, len);
455 eina_strbuf_append(txtbuf, ellipsis_string);
456 edje_object_part_text_set(wd->lbl, "elm.text", eina_strbuf_string_get(txtbuf));
458 if (_is_width_over(obj, linemode))
466 if (txtbuf) eina_strbuf_free(txtbuf);
472 * setting internal state of mulitline entry
473 * singleline doesn't need it
476 void _label_state_change(Evas_Object *obj)
478 Widget_Data *wd = elm_widget_data_get(obj);
484 edje_object_signal_emit(wd->lbl, "elm,state,wordwrap", "elm");
486 edje_object_signal_emit(wd->lbl, "elm,state,default", "elm");
490 void _label_sliding_change(Evas_Object *obj)
492 Widget_Data *wd = elm_widget_data_get(obj);
497 wd->slidingmode = EINA_FALSE;
498 fprintf(stderr, "ERR: elm_label dosen't support multiline sliding effect!!!\n");
499 fprintf(stderr, "ERR: elm_label dosen't support multiline sliding effect!!!\n");
500 fprintf(stderr, "ERR: elm_label dosen't support multiline sliding effect!!!\n");
507 wd->slidingellipsis = EINA_TRUE;
508 elm_label_ellipsis_set(obj, EINA_FALSE);
510 edje_object_signal_emit(wd->lbl, "elm,state,slide,start", "elm");
514 edje_object_signal_emit(wd->lbl, "elm,state,slide,stop", "elm");
515 if (wd->slidingellipsis)
517 wd->slidingellipsis = EINA_FALSE;
518 elm_label_ellipsis_set(obj, EINA_TRUE);
524 * Add a new label to the parent
526 * @param parent The parent object
527 * @return The new object or NULL if it cannot be created
532 elm_label_add(Evas_Object *parent)
538 wd = ELM_NEW(Widget_Data);
539 e = evas_object_evas_get(parent);
540 wd->bg = evas_object_rectangle_add(e);
541 evas_object_color_set(wd->bg, 0, 0, 0, 0);
542 obj = elm_widget_add(e);
543 ELM_SET_WIDTYPE(widtype, "label");
544 elm_widget_type_set(obj, "label");
545 elm_widget_sub_object_add(parent, obj);
546 elm_widget_data_set(obj, wd);
547 elm_widget_del_hook_set(obj, _del_hook);
548 elm_widget_theme_hook_set(obj, _theme_hook);
549 elm_widget_can_focus_set(obj, 0);
551 wd->linewrap = EINA_FALSE;
552 wd->bgcolor = EINA_FALSE;
553 wd->ellipsis = EINA_FALSE;
554 wd->wrapmode = EINA_FALSE;
555 wd->slidingmode = EINA_FALSE;
556 wd->slidingellipsis = EINA_FALSE;
560 wd->lbl = edje_object_add(e);
561 _elm_theme_object_set(obj, wd->lbl, "label", "base", "default");
562 wd->label = eina_stringshare_add("<br>");
563 edje_object_part_text_set(wd->lbl, "elm.text", "<br>");
564 elm_widget_resize_object_set(obj, wd->lbl);
566 evas_object_event_callback_add(wd->lbl, EVAS_CALLBACK_RESIZE, _resize, obj);
574 * Set the label on the label object
576 * @param obj The label object
577 * @param label The label will be used on the label object
582 elm_label_label_set(Evas_Object *obj, const char *label)
584 ELM_CHECK_WIDTYPE(obj, widtype);
585 Widget_Data *wd = elm_widget_data_get(obj);
587 if (!label) label = "";
588 eina_stringshare_replace(&wd->label, label);
589 edje_object_part_text_set(wd->lbl, "elm.text", label);
595 * Get the label used on the label object
597 * @param obj The label object
598 * @return The string inside the label
602 elm_label_label_get(const Evas_Object *obj)
604 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
605 Widget_Data *wd = elm_widget_data_get(obj);
606 if (!wd) return NULL;
611 * Set the wrapping behavior of the label
613 * @param obj The label object
614 * @param wrap To wrap text or not
618 elm_label_line_wrap_set(Evas_Object *obj, Eina_Bool wrap)
620 ELM_CHECK_WIDTYPE(obj, widtype);
621 Widget_Data *wd = elm_widget_data_get(obj);
624 if (wd->linewrap == wrap) return;
626 t = eina_stringshare_add(elm_label_label_get(obj));
628 elm_label_label_set(obj, t);
629 eina_stringshare_del(t);
635 * Get the wrapping behavior of the label
637 * @param obj The label object
638 * @return To wrap text or not
642 elm_label_line_wrap_get(const Evas_Object *obj)
644 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
645 Widget_Data *wd = elm_widget_data_get(obj);
646 if (!wd) return EINA_FALSE;
651 * Set wrap width of the label
653 * @param obj The label object
654 * @param w The wrap width in pixels at a minimum where words need to wrap
658 elm_label_wrap_width_set(Evas_Object *obj, Evas_Coord w)
660 ELM_CHECK_WIDTYPE(obj, widtype);
661 Widget_Data *wd = elm_widget_data_get(obj);
664 if (wd->wrap_w == w) return;
665 if (wd->ellipsis) edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
671 * get wrap width of the label
673 * @param obj The label object
674 * @return The wrap width in pixels at a minimum where words need to wrap
678 elm_label_wrap_width_get(const Evas_Object *obj)
680 ELM_CHECK_WIDTYPE(obj, widtype) 0;
681 Widget_Data *wd = elm_widget_data_get(obj);
687 * Set wrap height of the label
689 * @param obj The label object
690 * @param w The wrap width in pixels at a minimum where words need to wrap
694 elm_label_wrap_height_set(Evas_Object *obj, Evas_Coord h)
696 ELM_CHECK_WIDTYPE(obj, widtype);
697 Widget_Data *wd = elm_widget_data_get(obj);
700 if (wd->wrap_h == h) return;
701 if (wd->ellipsis) edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
707 * get wrap width of the label
709 * @param obj The label object
710 * @return The wrap height in pixels at a minimum where words need to wrap
714 elm_label_wrap_height_get(const Evas_Object *obj)
716 ELM_CHECK_WIDTYPE(obj, widtype) 0;
717 Widget_Data *wd = elm_widget_data_get(obj);
723 * Set the font size on the label object
725 * @param obj The label object
726 * @param size font size
731 elm_label_fontsize_set(Evas_Object *obj, int fontsize)
733 ELM_CHECK_WIDTYPE(obj, widtype);
734 Widget_Data *wd = elm_widget_data_get(obj);
735 Eina_Strbuf *fontbuf = NULL;
736 int len, removeflag = 0;
739 len = strlen(wd->label);
740 if (len <= 0) return;
741 fontbuf = eina_strbuf_new();
742 eina_strbuf_append_printf(fontbuf, "%d", fontsize);
744 if (fontsize == 0) removeflag = 1; // remove fontsize tag
746 if (_stringshare_key_value_replace(&wd->label, "font_size", eina_strbuf_string_get(fontbuf), removeflag) == 0)
748 edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
752 eina_strbuf_free(fontbuf);
756 * Set the text align on the label object
758 * @param obj The label object
759 * @param align align mode ("left", "center", "right")
764 elm_label_text_align_set(Evas_Object *obj, const char *alignmode)
766 ELM_CHECK_WIDTYPE(obj, widtype);
767 Widget_Data *wd = elm_widget_data_get(obj);
771 len = strlen(wd->label);
772 if (len <= 0) return;
774 if (_stringshare_key_value_replace(&wd->label, "align", alignmode, 0) == 0)
775 edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
782 * Set the text color on the label object
784 * @param obj The label object
785 * @param r Red property background color of The label object
786 * @param g Green property background color of The label object
787 * @param b Blue property background color of The label object
788 * @param a Alpha property background alpha of The label object
793 elm_label_text_color_set(Evas_Object *obj, unsigned int r, unsigned int g, unsigned int b, unsigned int a)
795 ELM_CHECK_WIDTYPE(obj, widtype);
796 Widget_Data *wd = elm_widget_data_get(obj);
797 Eina_Strbuf *colorbuf = NULL;
801 len = strlen(wd->label);
802 if (len <= 0) return;
803 colorbuf = eina_strbuf_new();
804 eina_strbuf_append_printf(colorbuf, "#%02X%02X%02X%02X", r, g, b, a);
806 if (_stringshare_key_value_replace(&wd->label, "color", eina_strbuf_string_get(colorbuf), 0) == 0)
808 edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
812 eina_strbuf_free(colorbuf);
817 * Set background color of the label
819 * @param obj The label object
820 * @param r Red property background color of The label object
821 * @param g Green property background color of The label object
822 * @param b Blue property background color of The label object
823 * @param a Alpha property background alpha of The label object
827 elm_label_background_color_set(Evas_Object *obj, unsigned int r, unsigned int g, unsigned int b, unsigned int a)
829 ELM_CHECK_WIDTYPE(obj, widtype);
830 Widget_Data *wd = elm_widget_data_get(obj);
831 evas_object_color_set(wd->bg, r, g, b, a);
833 if (wd->bgcolor == EINA_FALSE)
836 edje_object_part_swallow(wd->lbl, "label.swallow.background", wd->bg);
841 * Set the ellipsis behavior of the label
843 * @param obj The label object
844 * @param ellipsis To ellipsis text or not
848 elm_label_ellipsis_set(Evas_Object *obj, Eina_Bool ellipsis)
850 ELM_CHECK_WIDTYPE(obj, widtype);
851 Widget_Data *wd = elm_widget_data_get(obj);
852 if (wd->ellipsis == ellipsis) return;
853 wd->ellipsis = ellipsis;
854 if (wd->linewrap) _theme_change(obj);
855 edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
861 * Set the wrapmode of the label
863 * @param obj The label object
864 * @param wrapmode 0 is charwrap, 1 is wordwrap
868 elm_label_wrap_mode_set(Evas_Object *obj, Eina_Bool wrapmode)
870 ELM_CHECK_WIDTYPE(obj, widtype);
871 Widget_Data *wd = elm_widget_data_get(obj);
872 if (wd->wrapmode == wrapmode) return;
873 wd->wrapmode = wrapmode;
874 _label_state_change(obj);
880 * Set the text slide of the label
882 * @param obj The label object
883 * @param slide To start slide or stop
887 elm_label_slide_set(Evas_Object *obj, Eina_Bool slide)
889 ELM_CHECK_WIDTYPE(obj, widtype);
890 Widget_Data *wd = elm_widget_data_get(obj);
893 if (wd->slidingmode == slide) return;
894 wd->slidingmode = slide;
895 _label_sliding_change(obj);
901 * get the text slide mode of the label
903 * @param obj The label object
904 * @return slide set flag value
908 elm_label_slide_get(Evas_Object *obj)
910 ELM_CHECK_WIDTYPE(obj, widtype);
911 Widget_Data *wd = elm_widget_data_get(obj);
912 if (!wd) return EINA_FALSE;
914 return wd->slidingmode;