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 changed : 1;
26 Eina_Bool bgcolor : 1;
27 Eina_Bool ellipsis : 1;
30 static const char *widtype = NULL;
31 static void _del_hook(Evas_Object *obj);
32 static void _theme_hook(Evas_Object *obj);
33 static void _sizing_eval(Evas_Object *obj);
34 static int _get_value_in_key_string(const char *oldstring, char *key, char **value);
35 static int _strbuf_key_value_replace(Eina_Strbuf *srcbuf, char *key, const char *value, int deleteflag);
36 static int _stringshare_key_value_replace(const char **srcstring, char *key, const char *value, int deleteflag);
37 static int _is_width_over(Evas_Object *obj, int linemode);
38 static void _ellipsis_label_to_width(Evas_Object *obj, int linemode);
41 _elm_win_recalc_job(void *data)
43 Widget_Data *wd = elm_widget_data_get(data);
44 Evas_Coord minw = -1, minh = -1, maxh = -1;
45 Evas_Coord resw, resh, minminw, minminh;
47 wd->deferred_recalc_job = NULL;
48 evas_object_geometry_get(wd->lbl, NULL, NULL, &resw, &resh);
51 edje_object_size_min_restricted_calc(wd->lbl, &minw, &minh, 0, 0);
54 if (wd->wrap_w >= resw)
57 edje_object_size_min_restricted_calc(wd->lbl, &minw, &minh, resw, 0);
58 evas_object_size_hint_min_set(data, minw, minh);
62 if (wd->wrap_w > minminw) minminw = wd->wrap_w;
63 edje_object_size_min_restricted_calc(wd->lbl, &minw, &minh, resw, 0);
64 evas_object_size_hint_min_set(data, minminw, minh);
67 if (wd->ellipsis && wd->linewrap && wd->wrap_h > 0 && _is_width_over(data, 1) == 1)
68 _ellipsis_label_to_width(data, 1);
71 evas_object_size_hint_max_set(data, -1, maxh);
75 _del_hook(Evas_Object *obj)
77 Widget_Data *wd = elm_widget_data_get(obj);
79 if (wd->deferred_recalc_job) ecore_job_del(wd->deferred_recalc_job);
80 if (wd->label) eina_stringshare_del(wd->label);
81 if (wd->bg) evas_object_del(wd->bg);
86 _theme_change(Evas_Object *obj)
88 Widget_Data *wd = elm_widget_data_get(obj);
94 _elm_theme_object_set(obj, wd->lbl, "label", "base_wrap_ellipsis", elm_widget_style_get(obj));
96 _elm_theme_object_set(obj, wd->lbl, "label", "base_wrap", elm_widget_style_get(obj));
99 _elm_theme_object_set(obj, wd->lbl, "label", "base", elm_widget_style_get(obj));
104 _theme_hook(Evas_Object *obj)
106 Widget_Data *wd = elm_widget_data_get(obj);
109 edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
110 edje_object_scale_set(wd->lbl, elm_widget_scale_get(obj) *
116 _sizing_eval(Evas_Object *obj)
118 Widget_Data *wd = elm_widget_data_get(obj);
119 Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
120 Evas_Coord resw, resh;
124 evas_object_geometry_get(wd->lbl, NULL, NULL, &resw, &resh);
125 if ((resw == wd->lastw) && (!wd->changed)) return;
126 wd->changed = EINA_FALSE;
128 _elm_win_recalc_job(obj);
129 // FIXME: works ok. but NOT for genlist. what should genlist do?
130 // if (wd->deferred_recalc_job) ecore_job_del(wd->deferred_recalc_job);
131 // wd->deferred_recalc_job = ecore_job_add(_elm_win_recalc_job, obj);
135 evas_object_geometry_get(wd->lbl, NULL, NULL, &resw, &resh);
136 edje_object_size_min_calc(wd->lbl, &minw, &minh);
137 evas_object_size_hint_min_set(obj, minw, minh);
139 evas_object_size_hint_max_set(obj, maxw, maxh);
141 if (wd->ellipsis && _is_width_over(obj, 0) == 1)
142 _ellipsis_label_to_width(obj, 0);
147 _resize(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
149 Widget_Data *wd = elm_widget_data_get(data);
151 if (wd->linewrap) _sizing_eval(data);
155 _get_value_in_key_string(const char *oldstring, char *key, char **value)
157 char *curlocater, *starttag, *endtag;
158 int firstindex = 0, foundflag = -1;
160 curlocater = strstr(oldstring, key);
163 starttag = curlocater;
164 endtag = curlocater + strlen(key);
165 if (endtag == NULL || *endtag != '=')
171 firstindex = abs(oldstring - curlocater);
172 firstindex += strlen(key)+1; // strlen("key") + strlen("=")
173 *value = (char*)oldstring + firstindex;
175 while (oldstring != starttag)
177 if (*starttag == '>')
182 if (*starttag == '<')
186 if (starttag == NULL) break;
189 while (NULL != endtag)
200 if (endtag == NULL) break;
203 if (foundflag != 0 && *starttag == '<' && *endtag == '>')
213 if (foundflag == 1) return 0;
219 _strbuf_key_value_replace(Eina_Strbuf *srcbuf, char *key, const char *value, int deleteflag)
221 const char *srcstring = NULL;
222 Eina_Strbuf *repbuf = NULL, *diffbuf = NULL;
223 char *curlocater, *replocater;
224 char *starttag, *endtag;
225 int tagtxtlen = 0, insertflag = 0;
227 srcstring = eina_strbuf_string_get(srcbuf);
228 curlocater = strstr(srcstring, key);
230 if (curlocater == NULL)
238 starttag = strchr(srcstring, '<');
239 endtag = strchr(srcstring, '>');
240 tagtxtlen = endtag - starttag;
241 if (tagtxtlen <= 0) tagtxtlen = 0;
242 if (starttag < curlocater && curlocater < endtag) break;
243 if (endtag != NULL && endtag+1 != NULL)
244 srcstring = endtag+1;
247 } while (strlen(srcstring) > 1);
249 if (starttag && endtag && tagtxtlen > strlen(key))
251 repbuf = eina_strbuf_new();
252 diffbuf = eina_strbuf_new();
253 eina_strbuf_append_n(repbuf, starttag, tagtxtlen);
254 srcstring = eina_strbuf_string_get(repbuf);
255 curlocater = strstr(srcstring, key);
257 if (curlocater != NULL)
259 replocater = curlocater + strlen(key) + 1;
261 while (*replocater == ' ' || *replocater == '=')
266 while (*replocater != NULL && *replocater != ' ' && *replocater != '>')
269 if (replocater-curlocater > strlen(key)+1)
272 eina_strbuf_append_n(diffbuf, curlocater, replocater-curlocater+1);
281 eina_strbuf_reset(repbuf);
289 if (repbuf == NULL) repbuf = eina_strbuf_new();
290 if (diffbuf == NULL) diffbuf = eina_strbuf_new();
294 eina_strbuf_append_printf(repbuf, "<%s=%s>", key, value);
295 eina_strbuf_prepend(srcbuf, eina_strbuf_string_get(repbuf));
301 eina_strbuf_prepend(diffbuf, "<");
302 eina_strbuf_append(diffbuf, ">");
303 eina_strbuf_replace_first(srcbuf, eina_strbuf_string_get(diffbuf), "");
307 eina_strbuf_append_printf(repbuf, "%s=%s", key, value);
308 eina_strbuf_replace_first(srcbuf, eina_strbuf_string_get(diffbuf), eina_strbuf_string_get(repbuf));
312 if (repbuf) eina_strbuf_free(repbuf);
313 if (diffbuf) eina_strbuf_free(diffbuf);
319 _stringshare_key_value_replace(const char **srcstring, char *key, const char *value, int deleteflag)
321 Eina_Strbuf *sharebuf = NULL;
323 sharebuf = eina_strbuf_new();
324 eina_strbuf_append(sharebuf, *srcstring);
325 _strbuf_key_value_replace(sharebuf, key, value, deleteflag);
326 eina_stringshare_del(*srcstring);
327 *srcstring = eina_stringshare_add(eina_strbuf_string_get(sharebuf));
328 eina_strbuf_free(sharebuf);
334 _is_width_over(Evas_Object *obj, int linemode)
336 Evas_Coord x, y, w, h;
337 Evas_Coord vx, vy, vw, vh;
338 Widget_Data *wd = elm_widget_data_get(obj);
339 const char *ellipsis_string = "...";
340 size_t ellen = strlen(ellipsis_string)+1;
344 edje_object_part_geometry_get(wd->lbl,"elm.text",&x,&y,&w,&h);
346 evas_object_geometry_get (obj, &vx,&vy,&vw,&vh);
348 if (linemode == 0) // single line
350 if ((x >= 0) && (y >= 0))
352 if ((wd->wrap_w > 0) && (wd->wrap_w < w))
354 Evas_Coord minw, minh;
355 edje_object_size_min_calc(wd->lbl, &minw, &minh);
357 if (minw < wd->wrap_w)
358 { // min insufficient
368 if (ellen < wd->wrap_w && w > wd->wrap_w) return 1;
372 if (vy > h && h > wd->wrap_h) return 1;
379 _ellipsis_label_to_width(Evas_Object *obj, int linemode)
381 Widget_Data *wd = elm_widget_data_get(obj);
382 int cur_fontsize = 0, len, showcount;
383 Eina_Strbuf *fontbuf = NULL, *txtbuf = NULL;
384 char **kvalue = NULL;
385 const char *minfont, *deffont, *maxfont;
386 const char *ellipsis_string = "...";
387 int minfontsize, maxfontsize, minshowcount;
389 minshowcount = strlen(ellipsis_string) + 1;
390 minfont = edje_object_data_get(wd->lbl, "min_font_size");
391 if (minfont) minfontsize = atoi(minfont);
392 else minfontsize = 1;
393 maxfont = edje_object_data_get(wd->lbl, "max_font_size");
394 if (maxfont) maxfontsize = atoi(maxfont);
395 else maxfontsize = 1;
396 deffont = edje_object_data_get(wd->lbl, "default_font_size");
397 if (deffont) cur_fontsize = atoi(deffont);
398 else cur_fontsize = 1;
399 if (minfontsize > maxfontsize || cur_fontsize == 1) return; // theme is not ready for ellipsis
400 if (eina_stringshare_strlen(wd->label) <= 0) return;
402 if (_get_value_in_key_string(wd->label, "font_size", &kvalue) == 0)
404 if (*kvalue != NULL) cur_fontsize = atoi((char*)kvalue);
407 txtbuf = eina_strbuf_new();
408 eina_strbuf_append(txtbuf, wd->label);
410 while (_is_width_over(obj, linemode))
412 if (cur_fontsize > minfontsize)
417 eina_strbuf_free(fontbuf);
420 fontbuf = eina_strbuf_new();
421 eina_strbuf_append_printf(fontbuf, "%d", cur_fontsize);
422 _strbuf_key_value_replace(txtbuf, "font_size", eina_strbuf_string_get(fontbuf), 0);
423 edje_object_part_text_set(wd->lbl, "elm.text", eina_strbuf_string_get(txtbuf));
424 eina_strbuf_free(fontbuf);
431 eina_strbuf_free(txtbuf);
434 txtbuf = eina_strbuf_new();
435 eina_strbuf_append_printf(txtbuf, "%s", edje_object_part_text_get(wd->lbl, "elm.text"));
436 len = eina_strbuf_length_get(txtbuf);
438 while (showcount > minshowcount)
440 unsigned char *ltxt = eina_strbuf_string_get(txtbuf);
441 len = eina_strbuf_length_get(txtbuf);
442 // FIXME : more reliable truncate routine is needed
443 // it works on only EUC-KR
445 if (ltxt[len-minshowcount-delta] >= 0x80)
450 eina_strbuf_remove(txtbuf, len-minshowcount-delta, len);
451 eina_strbuf_append(txtbuf, ellipsis_string);
452 edje_object_part_text_set(wd->lbl, "elm.text", eina_strbuf_string_get(txtbuf));
454 if (_is_width_over(obj, linemode))
462 if (txtbuf) eina_strbuf_free(txtbuf);
468 * setting internal state of mulitline entry
469 * singleline doesn't need it
472 void _label_state_change(Evas_Object *obj)
474 Widget_Data *wd = elm_widget_data_get(obj);
480 edje_object_signal_emit(wd->lbl, "elm,state,wordwrap", "elm");
482 edje_object_signal_emit(wd->lbl, "elm,state,default", "elm");
488 * Add a new label to the parent
490 * @param parent The parent object
491 * @return The new object or NULL if it cannot be created
496 elm_label_add(Evas_Object *parent)
502 wd = ELM_NEW(Widget_Data);
503 e = evas_object_evas_get(parent);
504 wd->bg = evas_object_rectangle_add(e);
505 evas_object_color_set(wd->bg, 0, 0, 0, 0);
506 obj = elm_widget_add(e);
507 ELM_SET_WIDTYPE(widtype, "label");
508 elm_widget_type_set(obj, "label");
509 elm_widget_sub_object_add(parent, obj);
510 elm_widget_data_set(obj, wd);
511 elm_widget_del_hook_set(obj, _del_hook);
512 elm_widget_theme_hook_set(obj, _theme_hook);
513 elm_widget_can_focus_set(obj, 0);
515 wd->linewrap = EINA_FALSE;
516 wd->bgcolor = EINA_FALSE;
517 wd->ellipsis = EINA_FALSE;
518 wd->wrapmode = EINA_FALSE;
522 wd->lbl = edje_object_add(e);
523 _elm_theme_object_set(obj, wd->lbl, "label", "base", "default");
524 wd->label = eina_stringshare_add("<br>");
525 edje_object_part_text_set(wd->lbl, "elm.text", "<br>");
526 elm_widget_resize_object_set(obj, wd->lbl);
528 evas_object_event_callback_add(wd->lbl, EVAS_CALLBACK_RESIZE, _resize, obj);
536 * Set the label on the label object
538 * @param obj The label object
539 * @param label The label will be used on the label object
544 elm_label_label_set(Evas_Object *obj, const char *label)
546 ELM_CHECK_WIDTYPE(obj, widtype);
547 Widget_Data *wd = elm_widget_data_get(obj);
549 if (!label) label = "";
550 eina_stringshare_replace(&wd->label, label);
551 edje_object_part_text_set(wd->lbl, "elm.text", label);
557 * Get the label used on the label object
559 * @param obj The label object
560 * @return The string inside the label
564 elm_label_label_get(const Evas_Object *obj)
566 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
567 Widget_Data *wd = elm_widget_data_get(obj);
568 if (!wd) return NULL;
573 * Set the wrapping behavior of the label
575 * @param obj The label object
576 * @param wrap To wrap text or not
580 elm_label_line_wrap_set(Evas_Object *obj, Eina_Bool wrap)
582 ELM_CHECK_WIDTYPE(obj, widtype);
583 Widget_Data *wd = elm_widget_data_get(obj);
586 if (wd->linewrap == wrap) return;
588 t = eina_stringshare_add(elm_label_label_get(obj));
590 elm_label_label_set(obj, t);
591 eina_stringshare_del(t);
597 * Get the wrapping behavior of the label
599 * @param obj The label object
600 * @return To wrap text or not
604 elm_label_line_wrap_get(const Evas_Object *obj)
606 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
607 Widget_Data *wd = elm_widget_data_get(obj);
608 if (!wd) return EINA_FALSE;
613 * Set wrap width of the label
615 * @param obj The label object
616 * @param w The wrap width in pixels at a minimum where words need to wrap
620 elm_label_wrap_width_set(Evas_Object *obj, Evas_Coord w)
622 ELM_CHECK_WIDTYPE(obj, widtype);
623 Widget_Data *wd = elm_widget_data_get(obj);
626 if (wd->wrap_w == w) return;
627 if (wd->ellipsis) edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
633 * get wrap width of the label
635 * @param obj The label object
636 * @return The wrap width in pixels at a minimum where words need to wrap
640 elm_label_wrap_width_get(const Evas_Object *obj)
642 ELM_CHECK_WIDTYPE(obj, widtype) 0;
643 Widget_Data *wd = elm_widget_data_get(obj);
649 * Set wrap height of the label
651 * @param obj The label object
652 * @param w The wrap width in pixels at a minimum where words need to wrap
656 elm_label_wrap_height_set(Evas_Object *obj, Evas_Coord h)
658 ELM_CHECK_WIDTYPE(obj, widtype);
659 Widget_Data *wd = elm_widget_data_get(obj);
662 if (wd->wrap_h == h) return;
663 if (wd->ellipsis) edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
669 * get wrap width of the label
671 * @param obj The label object
672 * @return The wrap height in pixels at a minimum where words need to wrap
676 elm_label_wrap_height_get(const Evas_Object *obj)
678 ELM_CHECK_WIDTYPE(obj, widtype) 0;
679 Widget_Data *wd = elm_widget_data_get(obj);
685 * Set the font size on the label object
687 * @param obj The label object
688 * @param size font size
693 elm_label_fontsize_set(Evas_Object *obj, int fontsize)
695 ELM_CHECK_WIDTYPE(obj, widtype);
696 Widget_Data *wd = elm_widget_data_get(obj);
697 Eina_Strbuf *fontbuf = NULL;
698 int len, removeflag = 0;
701 len = strlen(wd->label);
702 if (len <= 0) return;
703 fontbuf = eina_strbuf_new();
704 eina_strbuf_append_printf(fontbuf, "%d", fontsize);
706 if (fontsize == 0) removeflag = 1; // remove fontsize tag
708 if (_stringshare_key_value_replace(&wd->label, "font_size", eina_strbuf_string_get(fontbuf), removeflag) == 0)
710 edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
714 eina_strbuf_free(fontbuf);
718 * Set the text align on the label object
720 * @param obj The label object
721 * @param align align mode ("left", "center", "right")
726 elm_label_text_align_set(Evas_Object *obj, const char *alignmode)
728 ELM_CHECK_WIDTYPE(obj, widtype);
729 Widget_Data *wd = elm_widget_data_get(obj);
733 len = strlen(wd->label);
734 if (len <= 0) return;
736 if (_stringshare_key_value_replace(&wd->label, "align", alignmode, 0) == 0)
737 edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
744 * Set the text color on the label object
746 * @param obj The label object
747 * @param r Red property background color of The label object
748 * @param g Green property background color of The label object
749 * @param b Blue property background color of The label object
750 * @param a Alpha property background alpha of The label object
755 elm_label_text_color_set(Evas_Object *obj, unsigned int r, unsigned int g, unsigned int b, unsigned int a)
757 ELM_CHECK_WIDTYPE(obj, widtype);
758 Widget_Data *wd = elm_widget_data_get(obj);
759 Eina_Strbuf *colorbuf = NULL;
763 len = strlen(wd->label);
764 if (len <= 0) return;
765 colorbuf = eina_strbuf_new();
766 eina_strbuf_append_printf(colorbuf, "#%02X%02X%02X%02X", r, g, b, a);
768 if (_stringshare_key_value_replace(&wd->label, "color", eina_strbuf_string_get(colorbuf), 0) == 0)
770 edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
774 eina_strbuf_free(colorbuf);
779 * Set background color of the label
781 * @param obj The label object
782 * @param r Red property background color of The label object
783 * @param g Green property background color of The label object
784 * @param b Blue property background color of The label object
785 * @param a Alpha property background alpha of The label object
789 elm_label_background_color_set(Evas_Object *obj, unsigned int r, unsigned int g, unsigned int b, unsigned int a)
791 ELM_CHECK_WIDTYPE(obj, widtype);
792 Widget_Data *wd = elm_widget_data_get(obj);
793 evas_object_color_set(wd->bg, r, g, b, a);
795 if (wd->bgcolor == EINA_FALSE)
798 edje_object_part_swallow(wd->lbl, "label.swallow.background", wd->bg);
803 * Set the ellipsis behavior of the label
805 * @param obj The label object
806 * @param ellipsis To ellipsis text or not
810 elm_label_ellipsis_set(Evas_Object *obj, Eina_Bool ellipsis)
812 ELM_CHECK_WIDTYPE(obj, widtype);
813 Widget_Data *wd = elm_widget_data_get(obj);
814 if (wd->ellipsis == ellipsis) return;
815 wd->ellipsis = ellipsis;
816 if (wd->linewrap) _theme_change(obj);
822 * Set the wrapmode of the label
824 * @param obj The label object
825 * @param wrapmode 0 is charwrap, 1 is wordwrap
829 elm_label_wrap_mode_set(Evas_Object *obj, Eina_Bool wrapmode)
831 ELM_CHECK_WIDTYPE(obj, widtype);
832 Widget_Data *wd = elm_widget_data_get(obj);
833 if (wd->wrapmode == wrapmode) return;
834 wd->wrapmode = wrapmode;
835 _label_state_change(obj);
841 * Set the text slide of the label
843 * @param obj The label object
844 * @param slide To start slide or stop
848 elm_label_slide_set(Evas_Object *obj, Eina_Bool slide)
850 ELM_CHECK_WIDTYPE(obj, widtype);
851 Widget_Data *wd = elm_widget_data_get(obj);
855 edje_object_signal_emit(wd->lbl, "elm,state,slide,start", "elm");
857 edje_object_signal_emit(wd->lbl, "elm,state,slide,stop", "elm");