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 if (strlen(edje_object_part_text_get(wd->lbl, "elm.text")) <= ellen)
351 edje_object_part_geometry_get(wd->lbl,"elm.text",&x,&y,&w,&h);
353 evas_object_geometry_get (obj, &vx,&vy,&vw,&vh);
355 if (linemode == 0) // single line
357 if ((x >= 0) && (y >= 0))
359 if ((wd->wrap_w > 0) && (wd->wrap_w < w))
361 Evas_Coord minw, minh;
362 edje_object_size_min_calc(wd->lbl, &minw, &minh);
364 if (minw < wd->wrap_w)
365 { // min insufficient
375 if (ellen < wd->wrap_w && w > wd->wrap_w) return 1;
379 if (vy > h && h > wd->wrap_h) return 1;
386 _ellipsis_label_to_width(Evas_Object *obj, int linemode)
388 Widget_Data *wd = elm_widget_data_get(obj);
389 int cur_fontsize = 0, len, showcount;
390 Eina_Strbuf *fontbuf = NULL, *txtbuf = NULL;
391 char **kvalue = NULL;
392 const char *minfont, *deffont, *maxfont;
393 const char *ellipsis_string = "...";
394 int minfontsize, maxfontsize, minshowcount;
396 minshowcount = strlen(ellipsis_string) + 1;
397 minfont = edje_object_data_get(wd->lbl, "min_font_size");
398 if (minfont) minfontsize = atoi(minfont);
399 else minfontsize = 1;
400 maxfont = edje_object_data_get(wd->lbl, "max_font_size");
401 if (maxfont) maxfontsize = atoi(maxfont);
402 else maxfontsize = 1;
403 deffont = edje_object_data_get(wd->lbl, "default_font_size");
404 if (deffont) cur_fontsize = atoi(deffont);
405 else cur_fontsize = 1;
406 if (minfontsize > maxfontsize || cur_fontsize == 1) return; // theme is not ready for ellipsis
407 if (eina_stringshare_strlen(wd->label) <= 0) return;
409 if (_get_value_in_key_string(wd->label, "font_size", &kvalue) == 0)
411 if (*kvalue != NULL) cur_fontsize = atoi((char*)kvalue);
414 txtbuf = eina_strbuf_new();
415 eina_strbuf_append(txtbuf, wd->label);
417 while (_is_width_over(obj, linemode))
419 if (cur_fontsize > minfontsize)
424 eina_strbuf_free(fontbuf);
427 fontbuf = eina_strbuf_new();
428 eina_strbuf_append_printf(fontbuf, "%d", cur_fontsize);
429 _strbuf_key_value_replace(txtbuf, "font_size", eina_strbuf_string_get(fontbuf), 0);
430 edje_object_part_text_set(wd->lbl, "elm.text", eina_strbuf_string_get(txtbuf));
431 eina_strbuf_free(fontbuf);
438 eina_strbuf_free(txtbuf);
441 txtbuf = eina_strbuf_new();
442 eina_strbuf_append_printf(txtbuf, "%s", edje_object_part_text_get(wd->lbl, "elm.text"));
443 len = eina_strbuf_length_get(txtbuf);
445 while (showcount > minshowcount)
447 unsigned char *ltxt = eina_strbuf_string_get(txtbuf);
448 len = eina_strbuf_length_get(txtbuf);
449 // FIXME : more reliable truncate routine is needed
450 // it works on only EUC-KR
452 if (ltxt[len-minshowcount-delta] >= 0x80)
457 eina_strbuf_remove(txtbuf, len-minshowcount-delta, len);
458 eina_strbuf_append(txtbuf, ellipsis_string);
459 edje_object_part_text_set(wd->lbl, "elm.text", eina_strbuf_string_get(txtbuf));
461 if (_is_width_over(obj, linemode))
469 if (txtbuf) eina_strbuf_free(txtbuf);
475 * setting internal state of mulitline entry
476 * singleline doesn't need it
479 void _label_state_change(Evas_Object *obj)
481 Widget_Data *wd = elm_widget_data_get(obj);
487 edje_object_signal_emit(wd->lbl, "elm,state,wordwrap", "elm");
489 edje_object_signal_emit(wd->lbl, "elm,state,default", "elm");
493 void _label_sliding_change(Evas_Object *obj)
495 Widget_Data *wd = elm_widget_data_get(obj);
500 wd->slidingmode = EINA_FALSE;
501 fprintf(stderr, "ERR: elm_label dosen't support multiline sliding effect!!!\n");
502 fprintf(stderr, "ERR: elm_label dosen't support multiline sliding effect!!!\n");
503 fprintf(stderr, "ERR: elm_label dosen't support multiline sliding effect!!!\n");
510 wd->slidingellipsis = EINA_TRUE;
511 elm_label_ellipsis_set(obj, EINA_FALSE);
513 edje_object_signal_emit(wd->lbl, "elm,state,slide,start", "elm");
517 edje_object_signal_emit(wd->lbl, "elm,state,slide,stop", "elm");
518 if (wd->slidingellipsis)
520 wd->slidingellipsis = EINA_FALSE;
521 elm_label_ellipsis_set(obj, EINA_TRUE);
527 * Add a new label to the parent
529 * @param parent The parent object
530 * @return The new object or NULL if it cannot be created
535 elm_label_add(Evas_Object *parent)
541 wd = ELM_NEW(Widget_Data);
542 e = evas_object_evas_get(parent);
543 wd->bg = evas_object_rectangle_add(e);
544 evas_object_color_set(wd->bg, 0, 0, 0, 0);
545 obj = elm_widget_add(e);
546 ELM_SET_WIDTYPE(widtype, "label");
547 elm_widget_type_set(obj, "label");
548 elm_widget_sub_object_add(parent, obj);
549 elm_widget_data_set(obj, wd);
550 elm_widget_del_hook_set(obj, _del_hook);
551 elm_widget_theme_hook_set(obj, _theme_hook);
552 elm_widget_can_focus_set(obj, 0);
554 wd->linewrap = EINA_FALSE;
555 wd->bgcolor = EINA_FALSE;
556 wd->ellipsis = EINA_FALSE;
557 wd->wrapmode = EINA_FALSE;
558 wd->slidingmode = EINA_FALSE;
559 wd->slidingellipsis = EINA_FALSE;
563 wd->lbl = edje_object_add(e);
564 _elm_theme_object_set(obj, wd->lbl, "label", "base", "default");
565 wd->label = eina_stringshare_add("<br>");
566 edje_object_part_text_set(wd->lbl, "elm.text", "<br>");
567 elm_widget_resize_object_set(obj, wd->lbl);
569 evas_object_event_callback_add(wd->lbl, EVAS_CALLBACK_RESIZE, _resize, obj);
577 * Set the label on the label object
579 * @param obj The label object
580 * @param label The label will be used on the label object
585 elm_label_label_set(Evas_Object *obj, const char *label)
587 ELM_CHECK_WIDTYPE(obj, widtype);
588 Widget_Data *wd = elm_widget_data_get(obj);
590 if (!label) label = "";
591 eina_stringshare_replace(&wd->label, label);
592 edje_object_part_text_set(wd->lbl, "elm.text", label);
598 * Get the label used on the label object
600 * @param obj The label object
601 * @return The string inside the label
605 elm_label_label_get(const Evas_Object *obj)
607 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
608 Widget_Data *wd = elm_widget_data_get(obj);
609 if (!wd) return NULL;
614 * Set the wrapping behavior of the label
616 * @param obj The label object
617 * @param wrap To wrap text or not
621 elm_label_line_wrap_set(Evas_Object *obj, Eina_Bool wrap)
623 ELM_CHECK_WIDTYPE(obj, widtype);
624 Widget_Data *wd = elm_widget_data_get(obj);
627 if (wd->linewrap == wrap) return;
629 t = eina_stringshare_add(elm_label_label_get(obj));
631 elm_label_label_set(obj, t);
632 eina_stringshare_del(t);
638 * Get the wrapping behavior of the label
640 * @param obj The label object
641 * @return To wrap text or not
645 elm_label_line_wrap_get(const Evas_Object *obj)
647 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
648 Widget_Data *wd = elm_widget_data_get(obj);
649 if (!wd) return EINA_FALSE;
654 * Set wrap width of the label
656 * @param obj The label object
657 * @param w The wrap width in pixels at a minimum where words need to wrap
661 elm_label_wrap_width_set(Evas_Object *obj, Evas_Coord w)
663 ELM_CHECK_WIDTYPE(obj, widtype);
664 Widget_Data *wd = elm_widget_data_get(obj);
667 if (wd->wrap_w == w) return;
668 if (wd->ellipsis) edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
674 * get wrap width of the label
676 * @param obj The label object
677 * @return The wrap width in pixels at a minimum where words need to wrap
681 elm_label_wrap_width_get(const Evas_Object *obj)
683 ELM_CHECK_WIDTYPE(obj, widtype) 0;
684 Widget_Data *wd = elm_widget_data_get(obj);
690 * Set wrap height of the label
692 * @param obj The label object
693 * @param w The wrap width in pixels at a minimum where words need to wrap
697 elm_label_wrap_height_set(Evas_Object *obj, Evas_Coord h)
699 ELM_CHECK_WIDTYPE(obj, widtype);
700 Widget_Data *wd = elm_widget_data_get(obj);
703 if (wd->wrap_h == h) return;
704 if (wd->ellipsis) edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
710 * get wrap width of the label
712 * @param obj The label object
713 * @return The wrap height in pixels at a minimum where words need to wrap
717 elm_label_wrap_height_get(const Evas_Object *obj)
719 ELM_CHECK_WIDTYPE(obj, widtype) 0;
720 Widget_Data *wd = elm_widget_data_get(obj);
726 * Set the font size on the label object
728 * @param obj The label object
729 * @param size font size
734 elm_label_fontsize_set(Evas_Object *obj, int fontsize)
736 ELM_CHECK_WIDTYPE(obj, widtype);
737 Widget_Data *wd = elm_widget_data_get(obj);
738 Eina_Strbuf *fontbuf = NULL;
739 int len, removeflag = 0;
742 len = strlen(wd->label);
743 if (len <= 0) return;
744 fontbuf = eina_strbuf_new();
745 eina_strbuf_append_printf(fontbuf, "%d", fontsize);
747 if (fontsize == 0) removeflag = 1; // remove fontsize tag
749 if (_stringshare_key_value_replace(&wd->label, "font_size", eina_strbuf_string_get(fontbuf), removeflag) == 0)
751 edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
755 eina_strbuf_free(fontbuf);
759 * Set the text align on the label object
761 * @param obj The label object
762 * @param align align mode ("left", "center", "right")
767 elm_label_text_align_set(Evas_Object *obj, const char *alignmode)
769 ELM_CHECK_WIDTYPE(obj, widtype);
770 Widget_Data *wd = elm_widget_data_get(obj);
774 len = strlen(wd->label);
775 if (len <= 0) return;
777 if (_stringshare_key_value_replace(&wd->label, "align", alignmode, 0) == 0)
778 edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
785 * Set the text color on the label object
787 * @param obj The label object
788 * @param r Red property background color of The label object
789 * @param g Green property background color of The label object
790 * @param b Blue property background color of The label object
791 * @param a Alpha property background alpha of The label object
796 elm_label_text_color_set(Evas_Object *obj, unsigned int r, unsigned int g, unsigned int b, unsigned int a)
798 ELM_CHECK_WIDTYPE(obj, widtype);
799 Widget_Data *wd = elm_widget_data_get(obj);
800 Eina_Strbuf *colorbuf = NULL;
804 len = strlen(wd->label);
805 if (len <= 0) return;
806 colorbuf = eina_strbuf_new();
807 eina_strbuf_append_printf(colorbuf, "#%02X%02X%02X%02X", r, g, b, a);
809 if (_stringshare_key_value_replace(&wd->label, "color", eina_strbuf_string_get(colorbuf), 0) == 0)
811 edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
815 eina_strbuf_free(colorbuf);
820 * Set background color of the label
822 * @param obj The label object
823 * @param r Red property background color of The label object
824 * @param g Green property background color of The label object
825 * @param b Blue property background color of The label object
826 * @param a Alpha property background alpha of The label object
830 elm_label_background_color_set(Evas_Object *obj, unsigned int r, unsigned int g, unsigned int b, unsigned int a)
832 ELM_CHECK_WIDTYPE(obj, widtype);
833 Widget_Data *wd = elm_widget_data_get(obj);
834 evas_object_color_set(wd->bg, r, g, b, a);
836 if (wd->bgcolor == EINA_FALSE)
839 edje_object_part_swallow(wd->lbl, "label.swallow.background", wd->bg);
844 * Set the ellipsis behavior of the label
846 * @param obj The label object
847 * @param ellipsis To ellipsis text or not
851 elm_label_ellipsis_set(Evas_Object *obj, Eina_Bool ellipsis)
853 ELM_CHECK_WIDTYPE(obj, widtype);
854 Widget_Data *wd = elm_widget_data_get(obj);
855 if (wd->ellipsis == ellipsis) return;
856 wd->ellipsis = ellipsis;
857 if (wd->linewrap) _theme_change(obj);
858 edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
864 * Set the wrapmode of the label
866 * @param obj The label object
867 * @param wrapmode 0 is charwrap, 1 is wordwrap
871 elm_label_wrap_mode_set(Evas_Object *obj, Eina_Bool wrapmode)
873 ELM_CHECK_WIDTYPE(obj, widtype);
874 Widget_Data *wd = elm_widget_data_get(obj);
875 if (wd->wrapmode == wrapmode) return;
876 wd->wrapmode = wrapmode;
877 _label_state_change(obj);
883 * Set the text slide of the label
885 * @param obj The label object
886 * @param slide To start slide or stop
890 elm_label_slide_set(Evas_Object *obj, Eina_Bool slide)
892 ELM_CHECK_WIDTYPE(obj, widtype);
893 Widget_Data *wd = elm_widget_data_get(obj);
896 if (wd->slidingmode == slide) return;
897 wd->slidingmode = slide;
898 _label_sliding_change(obj);
904 * get the text slide mode of the label
906 * @param obj The label object
907 * @return slide slide mode value
911 elm_label_slide_get(Evas_Object *obj)
913 ELM_CHECK_WIDTYPE(obj, widtype);
914 Widget_Data *wd = elm_widget_data_get(obj);
915 if (!wd) return EINA_FALSE;
917 return wd->slidingmode;