Merge branch 'master' of 165.213.180.234:/git/slp/pkgs/elementary
[framework/uifw/elementary.git] / src / lib / elm_label.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 /**
5  * @defgroup Label Label
6  * @ingroup Elementary
7  *
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
10  */
11
12 typedef struct _Widget_Data Widget_Data;
13
14 struct _Widget_Data
15 {
16    Evas_Object *lbl;
17    Evas_Object *bg;
18    const char *label;
19    Evas_Coord lastw;
20    Ecore_Job *deferred_recalc_job;
21    Evas_Coord wrap_w;
22    Evas_Coord wrap_h;
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;
30    int slide_duration;
31 };
32
33 static const char *widtype = NULL;
34 static void _del_hook(Evas_Object *obj);
35 static void _theme_hook(Evas_Object *obj);
36 static void _sizing_eval(Evas_Object *obj);
37 static int _get_value_in_key_string(const char *oldstring, char *key, char **value);
38 static int _strbuf_key_value_replace(Eina_Strbuf *srcbuf, char *key, const char *value, int deleteflag);
39 static int _stringshare_key_value_replace(const char **srcstring, char *key, const char *value, int deleteflag);
40 static int _is_width_over(Evas_Object *obj, int linemode);
41 static void _ellipsis_label_to_width(Evas_Object *obj, int linemode);
42
43 static void
44 _elm_win_recalc_job(void *data)
45 {
46    Widget_Data *wd = elm_widget_data_get(data);
47    Evas_Coord minw = -1, minh = -1, maxh = -1;
48    Evas_Coord resw, resh, minminw, minminh;
49    if (!wd) return;
50    wd->deferred_recalc_job = NULL;
51    evas_object_geometry_get(wd->lbl, NULL, NULL, &resw, &resh);
52    resh = 0;
53    minminw = 0;
54    edje_object_size_min_restricted_calc(wd->lbl, &minw, &minh, 0, 0);
55    minminw = minw;
56    minminh = minh;
57    if (wd->wrap_w >= resw) 
58      {
59         resw = wd->wrap_w;
60         edje_object_size_min_restricted_calc(wd->lbl, &minw, &minh, resw, 0);
61         evas_object_size_hint_min_set(data, minw, minh);
62      }
63    else
64      {
65         if (wd->wrap_w > minminw) minminw = wd->wrap_w;
66         edje_object_size_min_restricted_calc(wd->lbl, &minw, &minh, resw, 0);
67         evas_object_size_hint_min_set(data, minminw, minh);
68      }
69
70    if (wd->ellipsis && wd->linewrap && wd->wrap_h > 0 && _is_width_over(data, 1) == 1) 
71      _ellipsis_label_to_width(data, 1);
72
73    maxh = minh;
74    evas_object_size_hint_max_set(data, -1, maxh);
75 }
76
77 static void
78 _del_hook(Evas_Object *obj)
79 {
80    Widget_Data *wd = elm_widget_data_get(obj);
81    if (!wd) return;
82    if (wd->deferred_recalc_job) ecore_job_del(wd->deferred_recalc_job);
83    if (wd->label) eina_stringshare_del(wd->label);
84    if (wd->bg) evas_object_del(wd->bg);
85    free(wd);
86 }
87
88 static void
89 _theme_change(Evas_Object *obj)
90 {
91    Widget_Data *wd = elm_widget_data_get(obj);
92    if (!wd) return;
93
94    if (wd->linewrap)
95      {
96        if (wd->ellipsis)
97          _elm_theme_object_set(obj, wd->lbl, "label", "base_wrap_ellipsis", elm_widget_style_get(obj));
98        else
99          _elm_theme_object_set(obj, wd->lbl, "label", "base_wrap", elm_widget_style_get(obj));
100      }
101    else
102      _elm_theme_object_set(obj, wd->lbl, "label", "base", elm_widget_style_get(obj));
103
104 }
105
106 static void
107 _theme_hook(Evas_Object *obj)
108 {
109    Widget_Data *wd = elm_widget_data_get(obj);
110    if (!wd) return;
111    _theme_change(obj);
112    edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
113    edje_object_scale_set(wd->lbl, elm_widget_scale_get(obj) * 
114                          _elm_config->scale);
115    _sizing_eval(obj);
116 }
117
118 static void
119 _sizing_eval(Evas_Object *obj)
120 {
121    Widget_Data *wd = elm_widget_data_get(obj);
122    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
123    Evas_Coord resw, resh;
124    if (!wd) return;
125    if (wd->linewrap)
126      {
127         evas_object_geometry_get(wd->lbl, NULL, NULL, &resw, &resh);
128         if ((resw == wd->lastw) && (!wd->changed)) return;
129         wd->changed = EINA_FALSE;
130         wd->lastw = resw;
131         _elm_win_recalc_job(obj);
132 // FIXME: works ok. but NOT for genlist. what should genlist do?        
133 //        if (wd->deferred_recalc_job) ecore_job_del(wd->deferred_recalc_job);
134 //        wd->deferred_recalc_job = ecore_job_add(_elm_win_recalc_job, obj);
135      }
136    else
137      {
138         evas_object_geometry_get(wd->lbl, NULL, NULL, &resw, &resh);
139         edje_object_size_min_calc(wd->lbl, &minw, &minh);
140                 if (wd->wrap_w > 0 && minw > wd->wrap_w)
141                         minw = wd->wrap_w;
142         evas_object_size_hint_min_set(obj, minw, minh);
143         maxh = minh;
144         evas_object_size_hint_max_set(obj, maxw, maxh);
145
146         if (wd->ellipsis && _is_width_over(obj, 0) == 1) 
147           _ellipsis_label_to_width(obj, 0);
148      }
149 }
150
151 static void 
152 _resize(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
153 {
154    Widget_Data *wd = elm_widget_data_get(data);
155    if (!wd) return;
156    if (wd->linewrap) _sizing_eval(data);
157 }
158
159 static int
160 _get_value_in_key_string(const char *oldstring, char *key, char **value)
161 {
162    char *curlocater, *starttag, *endtag;
163    int firstindex = 0, foundflag = -1;
164
165    curlocater = strstr(oldstring, key);
166    if (curlocater)
167      {
168         starttag = curlocater;
169         endtag = curlocater + strlen(key);
170         if (endtag == NULL || *endtag != '=') 
171           {
172             foundflag = 0;
173             return -1;
174           }
175
176         firstindex = abs(oldstring - curlocater);
177         firstindex += strlen(key)+1; // strlen("key") + strlen("=")
178         *value = (char*)oldstring + firstindex;
179
180         while (oldstring != starttag)
181           {
182             if (*starttag == '>')
183               {
184                 foundflag = 0;
185                 break;
186               }
187             if (*starttag == '<') 
188               break;
189             else 
190               starttag--;
191             if (starttag == NULL) break;
192           }
193
194         while (NULL != endtag)
195           {
196             if (*endtag == '<')
197               {
198                 foundflag = 0;
199                 break;
200               }
201             if (*endtag == '>') 
202               break;
203             else 
204               endtag++;
205             if (endtag == NULL) break;
206           }
207
208         if (foundflag != 0 && *starttag == '<' && *endtag == '>') 
209           foundflag = 1;
210         else 
211           foundflag = 0;
212      }
213    else
214      {
215        foundflag = 0;
216      }
217
218    if (foundflag == 1) return 0;
219
220    return -1;
221 }
222
223 static int
224 _strbuf_key_value_replace(Eina_Strbuf *srcbuf, char *key, const char *value, int deleteflag)
225 {
226    const char *srcstring = NULL;
227    Eina_Strbuf *repbuf = NULL, *diffbuf = NULL;
228    char *curlocater, *replocater;
229    char *starttag, *endtag;
230    int tagtxtlen = 0, insertflag = 0;
231
232    srcstring = eina_strbuf_string_get(srcbuf);
233    curlocater = strstr(srcstring, key);
234
235    if (curlocater == NULL)
236      {
237        insertflag = 1;
238      }
239    else
240      {
241        do 
242          {
243            starttag = strchr(srcstring, '<');
244            endtag = strchr(srcstring, '>');
245            tagtxtlen = endtag - starttag;
246            if (tagtxtlen <= 0) tagtxtlen = 0;
247            if (starttag < curlocater && curlocater < endtag) break;
248            if (endtag != NULL && endtag+1 != NULL)
249              srcstring = endtag+1;
250            else
251              break;
252          } while (strlen(srcstring) > 1);
253
254        if (starttag && endtag && tagtxtlen > strlen(key))
255          {
256            repbuf = eina_strbuf_new();
257            diffbuf = eina_strbuf_new();
258            eina_strbuf_append_n(repbuf, starttag, tagtxtlen);
259            srcstring = eina_strbuf_string_get(repbuf);
260            curlocater = strstr(srcstring, key);
261
262            if (curlocater != NULL)
263              {
264                replocater = curlocater + strlen(key) + 1;
265
266                while (*replocater == ' ' || *replocater == '=')
267                            {
268                  replocater++;
269                            }
270
271                 while (*replocater != NULL && *replocater != ' ' && *replocater != '>')
272                   replocater++;
273
274                if (replocater-curlocater > strlen(key)+1)
275                  {
276                    replocater--;
277                    eina_strbuf_append_n(diffbuf, curlocater, replocater-curlocater+1);
278                  }
279                else
280                  insertflag = 1;
281              }
282            else
283              {
284                insertflag = 1;
285              }
286            eina_strbuf_reset(repbuf);
287          }
288        else
289          {
290            insertflag = 1; 
291          }
292      }
293
294    if (repbuf == NULL) repbuf = eina_strbuf_new();
295    if (diffbuf == NULL) diffbuf = eina_strbuf_new();
296
297    if (insertflag)
298      {
299        eina_strbuf_append_printf(repbuf, "<%s=%s>", key, value);
300        eina_strbuf_prepend(srcbuf, eina_strbuf_string_get(repbuf));
301      }
302    else
303      {
304         if (deleteflag)
305           {
306             eina_strbuf_prepend(diffbuf, "<");
307             eina_strbuf_append(diffbuf, ">");
308             eina_strbuf_replace_first(srcbuf, eina_strbuf_string_get(diffbuf), "");
309           }
310         else
311           {
312             eina_strbuf_append_printf(repbuf, "%s=%s", key, value);
313             eina_strbuf_replace_first(srcbuf, eina_strbuf_string_get(diffbuf), eina_strbuf_string_get(repbuf));
314           }
315      }
316
317    if (repbuf) eina_strbuf_free(repbuf);
318    if (diffbuf) eina_strbuf_free(diffbuf);
319   
320    return 0;           
321 }
322
323 static int
324 _stringshare_key_value_replace(const char **srcstring, char *key, const char *value, int deleteflag)
325 {
326    Eina_Strbuf *sharebuf = NULL;   
327    
328    sharebuf = eina_strbuf_new();
329    eina_strbuf_append(sharebuf, *srcstring);
330    _strbuf_key_value_replace(sharebuf, key, value, deleteflag);
331    eina_stringshare_del(*srcstring);
332    *srcstring = eina_stringshare_add(eina_strbuf_string_get(sharebuf));
333    eina_strbuf_free(sharebuf);
334
335    return 0;
336 }
337
338
339 // FIXME: move to some where(such as elm_util??).
340 //        copied from elm_entry for check pure text length w/o tags.
341 static char *
342 _str_append(char *str, const char *txt, int *len, int *alloc)
343 {
344    int txt_len = strlen(txt);
345
346    if (txt_len <= 0) return str;
347    if ((*len + txt_len) >= *alloc)
348      {
349         char *str2;
350         int alloc2;
351
352         alloc2 = *alloc + txt_len + 128;
353         str2 = realloc(str, alloc2);
354         if (!str2) return str;
355         *alloc = alloc2;
356         str = str2;
357      }
358    strcpy(str + *len, txt);
359    *len += txt_len;
360    return str;
361 }
362
363 // FIXME: move to some where(such as elm_util??).
364 //        copied from elm_entry for check pure text length w/o tags.
365 static char *
366 _strncpy(char* dest, const char* src, size_t count)
367 {
368    if (!dest) 
369      {
370         ERR( "dest is NULL" );
371         return NULL;
372      }
373    if (!src) 
374      {
375         ERR( "src is NULL" );
376         return NULL;
377      }
378    if (count < 0)
379      {
380         ERR( "count is smaller than 0" );
381         return NULL;
382      }
383
384    return strncpy( dest, src, count );
385 }
386
387 // FIXME: move to some where(such as elm_util??).
388 //        copied from elm_entry for check pure text length w/o tags.
389 static char *
390 _mkup_to_text(const char *mkup)
391 {
392    char *str = NULL;
393    int str_len = 0, str_alloc = 0;
394    char *s, *p;
395    char *tag_start, *tag_end, *esc_start, *esc_end, *ts;
396
397    if (!mkup) return NULL;
398    s=p=NULL;
399    tag_start = tag_end = esc_start = esc_end = NULL;
400    p = (char *)mkup;
401    s = p;
402    for (;;)
403      {
404         if (((p!=NULL)&&(*p == 0)) ||
405             (tag_end) || (esc_end) ||
406             (tag_start) || (esc_start))
407           {
408              if (tag_end)
409                {
410                   char *ttag;
411
412                   ttag = malloc(tag_end - tag_start);
413                   if (ttag)
414                     {
415                        _strncpy(ttag, tag_start + 1, tag_end - tag_start - 1);
416                        ttag[tag_end - tag_start - 1] = 0;
417                        if (!strcmp(ttag, "br"))
418                          str = _str_append(str, "\n", &str_len, &str_alloc);
419                        else if (!strcmp(ttag, "\n"))
420                          str = _str_append(str, "\n", &str_len, &str_alloc);
421                        else if (!strcmp(ttag, "\\n"))
422                          str = _str_append(str, "\n", &str_len, &str_alloc);
423                        else if (!strcmp(ttag, "\t"))
424                          str = _str_append(str, "\t", &str_len, &str_alloc);
425                        else if (!strcmp(ttag, "\\t"))
426                          str = _str_append(str, "\t", &str_len, &str_alloc);
427                        else if (!strcmp(ttag, "ps")) /* Unicode paragraph separator */
428                          str = _str_append(str, "\xE2\x80\xA9", &str_len, &str_alloc);
429                        free(ttag);
430                     }
431                   tag_start = tag_end = NULL;
432                }
433              else if (esc_end)
434                {
435                   ts = malloc(esc_end - esc_start + 1);
436                   if (ts)
437                     {
438                        const char *esc;
439                        _strncpy(ts, esc_start, esc_end - esc_start);
440                        ts[esc_end - esc_start] = 0;
441                        esc = evas_textblock_escape_string_get(ts);
442                        if (esc)
443                          str = _str_append(str, esc, &str_len, &str_alloc);
444                        free(ts);
445                     }
446                   esc_start = esc_end = NULL;
447                }
448              else if ((p!=NULL)&&(*p == 0))
449                {
450                   ts = malloc(p - s + 1);
451                   if (ts)
452                     {
453                        _strncpy(ts, s, p - s);
454                        ts[p - s] = 0;
455                        str = _str_append(str, ts, &str_len, &str_alloc);
456                        free(ts);
457                     }
458                   break;
459                }
460           }
461         if ((p!=NULL)&&(*p == '<'))
462           {
463              if (!esc_start)
464                {
465                   tag_start = p;
466                   tag_end = NULL;
467                   ts = malloc(p - s + 1);
468                   if (ts)
469                     {
470                        _strncpy(ts, s, p - s);
471                        ts[p - s] = 0;
472                        str = _str_append(str, ts, &str_len, &str_alloc);
473                        free(ts);
474                     }
475                   s = NULL;
476                }
477           }
478         else if ((p!=NULL)&&(*p == '>'))
479           {
480              if (tag_start)
481                {
482                   tag_end = p;
483                   s = p + 1;
484                }
485           }
486         else if ((p!=NULL)&&(*p == '&'))
487           {
488              if (!tag_start)
489                {
490                   esc_start = p;
491                   esc_end = NULL;
492                   ts = malloc(p - s + 1);
493                   if (ts)
494                     {
495                        _strncpy(ts, s, p - s);
496                        ts[p - s] = 0;
497                        str = _str_append(str, ts, &str_len, &str_alloc);
498                        free(ts);
499                     }
500                   s = NULL;
501                }
502           }
503         else if ((p!=NULL)&&(*p == ';'))
504           {
505              if (esc_start)
506                {
507                   esc_end = p;
508                   s = p + 1;
509                }
510           }
511         p++;
512      }
513    return str;
514 }
515
516 static int
517 _is_width_over(Evas_Object *obj, int linemode)
518 {
519    Evas_Coord x, y, w, h;
520    Evas_Coord vx, vy, vw, vh;
521    Widget_Data *wd = elm_widget_data_get(obj);
522    const char *ellipsis_string = "...";
523    size_t ellen = strlen(ellipsis_string)+1;
524
525    if (!wd) return 0;
526
527    // too short to ellipsis
528    char *plaintxt = _mkup_to_text(edje_object_part_text_get(wd->lbl, "elm.text"));
529    int plainlen = 0;
530    if (plaintxt != NULL)
531    {
532            plainlen = strlen(plaintxt);
533        free(plaintxt);
534    }
535    if (plainlen <= ellen) return 0;   
536
537    edje_object_part_geometry_get(wd->lbl, "elm.text", &x, &y, &w, &h);
538
539    evas_object_geometry_get(obj, &vx, &vy, &vw, &vh);
540
541 /*
542    fprintf(stderr, "## _is_width_over\n");
543    fprintf(stderr, "## x = %d, y = %d, w = %d, h = %d\n", x, y, w, h);
544    fprintf(stderr, "## vx = %d, vy = %d, vw = %d, vh = %d\n", vx, vy, vw, vh);
545    if (linemode)
546            fprintf(stderr, "## wd->wrap_w = %d, wd->wrap_h = %d\n", wd->wrap_w, wd->wrap_h);
547    else
548            fprintf(stderr, "## wd->wrap_w = %d\n", wd->wrap_w);
549    fprintf(stderr, "## check str = %s\n", edje_object_part_text_get(wd->lbl, "elm.text"));
550 */
551
552    if (linemode == 0) // single line
553      {
554        // skip if too early to check widget size
555        if (w < 0 && h < 0)
556          return 0;
557        // if string fits at widget
558        if ((x >= 0) && (y >= 0))
559            {
560                    if ((wd->wrap_w > 0) && (wd->wrap_w < w))
561                    {
562                            Evas_Coord minw, minh;
563                            edje_object_size_min_calc(wd->lbl, &minw, &minh);
564
565                            if (minw < wd->wrap_w)
566                            { // min insufficient
567                                    return 0;
568                            }
569                            else
570                                    return 1;
571                    }
572                    else
573                            return 0;
574            }
575
576        if (0 < wd->wrap_w && w > wd->wrap_w) return 1;
577      }
578    else // multiline
579      {
580 //       if (vy > h && h > wd->wrap_h) return 1;
581                  if ((x >= 0 || y >= 0) && h > wd->wrap_h) return 1;
582      }
583
584    return 0;
585 }
586
587 static void
588 _ellipsis_fontsize_set(Evas_Object *obj, int fontsize)
589 {
590    Widget_Data *wd = elm_widget_data_get(obj);
591    if (!wd) return;
592
593    Eina_Strbuf *fontbuf = NULL;
594    Eina_Strbuf *txtbuf = NULL;
595    txtbuf = eina_strbuf_new();
596    fontbuf = eina_strbuf_new();
597    eina_strbuf_append(txtbuf, edje_object_part_text_get(wd->lbl, "elm.text"));
598    eina_strbuf_append_printf(fontbuf, "%d", fontsize);
599    _strbuf_key_value_replace(txtbuf, "font_size", eina_strbuf_string_get(fontbuf), 0);
600    edje_object_part_text_set(wd->lbl, "elm.text", eina_strbuf_string_get(txtbuf));
601    eina_strbuf_free(fontbuf);
602    eina_strbuf_free(txtbuf);
603 }
604
605 static Eina_Bool
606 _ellipsis_cut_chars_to_widget(Evas_Object *obj, int fontsize, int linemode)
607 {
608    Widget_Data *wd = elm_widget_data_get(obj);
609    if (!wd) return EINA_FALSE;
610
611    const char *ellipsis_string = "...";
612    int minshowcount = strlen(ellipsis_string);
613    Evas_Coord w, h;
614    Evas_Textblock_Cursor *tc1, *tc2;
615    char *cutstr, *elstr;
616    int limitw = 0;
617    int lencutstr = 0;
618    int i = 0;
619
620    edje_object_part_geometry_get(wd->lbl,"elm.text", NULL, NULL, &w, &h);
621    if (w <= 0)
622            return EINA_FALSE;
623    tc1 = evas_object_textblock_cursor_new(edje_object_part_object_get(wd->lbl, "elm.text"));
624    tc2 = evas_object_textblock_cursor_new(edje_object_part_object_get(wd->lbl, "elm.text"));
625    
626    if (wd->wrap_w > 0 && wd->wrap_w < w)
627            limitw = wd->wrap_w;
628    else
629            limitw = w;
630    evas_textblock_cursor_pos_set(tc1, 0);
631    evas_textblock_cursor_char_coord_set(tc2, limitw, 0);
632    for (i = 0; i <= minshowcount; i++)
633            evas_textblock_cursor_char_prev(tc2);
634    cutstr = evas_textblock_cursor_range_text_get(tc1, tc2, EVAS_TEXTBLOCK_TEXT_PLAIN);
635
636 /*
637    int eolpos = evas_textblock_cursor_paragraph_text_length_get(tc1);
638    Evas_Coord cx, cy, cw, ch;
639    for (i = eolpos; i > minshowcount; i--)
640    {
641            evas_textblock_cursor_pos_set(tc2, i);
642            fprintf(stderr, "## tc2 = %d\n", evas_textblock_cursor_pos_get(tc2));
643            evas_textblock_cursor_char_geometry_get(tc2, &cx, &cy, &cw, &ch);
644            fprintf(stderr, "## limitw = %d\n", limitw);
645            fprintf(stderr, "## cx = %d, cy = %d, cw = %d, ch = %d\n", cx, cy, cw, ch);
646            if (cx <= limitw)
647                    break;
648    }
649 */
650
651    // FIXME: consider other unicode encoding, currently only care about utf-8
652    lencutstr = strlen(cutstr);
653    elstr = malloc(sizeof(char)*(lencutstr+minshowcount+1));
654    strcpy(elstr, cutstr);
655    strcat(elstr, ellipsis_string);
656
657    edje_object_part_text_set(wd->lbl, "elm.text", elstr);
658
659    free(elstr);
660    evas_textblock_cursor_free(tc1);
661    evas_textblock_cursor_free(tc2);
662         
663    return EINA_TRUE;
664 }
665
666 static Eina_Bool
667 _ellipsis_cut_lines_to_widget(Evas_Object *obj, int fontsize, int linemode)
668 {
669    Widget_Data *wd = elm_widget_data_get(obj);
670    if (!wd) return EINA_FALSE;
671
672    const char *ellipsis_string = "...";
673    int minshowcount = strlen(ellipsis_string);
674    Evas_Coord w, h;
675    Evas_Textblock_Cursor *tc1, *tc2;
676    int linenum = 0, cutline = 0;
677    double lineheight = 0.0;
678    char *cutstr, *elstr;
679    int lencutstr = 0;
680    int limith = 0;
681    int i;
682
683    edje_object_part_geometry_get(wd->lbl,"elm.text", NULL, NULL, &w, &h);
684
685    tc1 = evas_object_textblock_cursor_new(edje_object_part_object_get(wd->lbl, "elm.text"));
686    tc2 = evas_object_textblock_cursor_new(edje_object_part_object_get(wd->lbl, "elm.text"));
687    // goto last paragraph
688    while (evas_textblock_cursor_paragraph_next(tc2) == EINA_TRUE)
689            ;
690    evas_textblock_cursor_paragraph_last(tc2);
691    // get total linenumber
692    linenum = evas_textblock_cursor_line_geometry_get(tc2, NULL, NULL, NULL, NULL);
693    lineheight = h/linenum * 1.0;
694    if (wd->wrap_h > 0 && wd->wrap_h < h)
695            limith = wd->wrap_h;
696    else
697            limith = h;
698    cutline = limith / lineheight;
699    if (cutline < 1)
700            cutline = 1;
701
702    evas_textblock_cursor_pos_set(tc1, 0);
703    evas_textblock_cursor_line_set(tc2, cutline-1);
704    evas_textblock_cursor_line_char_last(tc2);
705    for (i = 0; i <= minshowcount; i++)
706            evas_textblock_cursor_char_prev(tc2);
707    cutstr = evas_textblock_cursor_range_text_get(tc1, tc2, EVAS_TEXTBLOCK_TEXT_PLAIN);
708
709    // FIXME: consider other unicode encoding, currently only care about utf-8
710    lencutstr = strlen(cutstr);
711    elstr = malloc(sizeof(char)*(lencutstr+minshowcount+1));
712    strcpy(elstr, cutstr);
713    strcat(elstr, ellipsis_string);
714
715    edje_object_part_text_set(wd->lbl, "elm.text", elstr);
716
717    free(elstr);
718    evas_textblock_cursor_free(tc1);
719    evas_textblock_cursor_free(tc2);
720
721    return EINA_TRUE;
722 }
723
724 static void
725 _ellipsis_label_to_width(Evas_Object *obj, int linemode)
726 {
727    Widget_Data *wd = elm_widget_data_get(obj);
728    if (!wd) return;
729
730    int cur_fontsize = 0;
731    char **kvalue = NULL;
732    const char *minfont, *deffont, *maxfont;
733    int minfontsize, maxfontsize;
734
735    minfont = edje_object_data_get(wd->lbl, "min_font_size");
736    if (minfont) minfontsize = atoi(minfont);
737    else minfontsize = 1;
738    maxfont = edje_object_data_get(wd->lbl, "max_font_size");
739    if (maxfont) maxfontsize = atoi(maxfont);
740    else maxfontsize = 1;
741    deffont = edje_object_data_get(wd->lbl, "default_font_size");
742    if (deffont) cur_fontsize = atoi(deffont);
743    else cur_fontsize = 1;
744    if (minfontsize > maxfontsize || cur_fontsize == 1) return; // theme is not ready for ellipsis
745    if (eina_stringshare_strlen(wd->label) <= 0) return;
746
747    if (_get_value_in_key_string(wd->label, "font_size", &kvalue) == 0)
748      {
749        if (*kvalue != NULL) cur_fontsize = atoi((char*)kvalue);
750      }
751
752    while (_is_width_over(obj, linemode))
753      {
754        if (cur_fontsize > minfontsize)
755          {
756            cur_fontsize -= 3;
757                    if (cur_fontsize < minfontsize)
758              cur_fontsize = minfontsize;
759                    _ellipsis_fontsize_set(obj, cur_fontsize);
760          }
761        else
762          {
763            if (linemode == 0) // single line
764              {
765                _ellipsis_cut_chars_to_widget(obj, cur_fontsize, linemode);
766                            break;
767                          }
768            else // multiline
769              {
770                _ellipsis_cut_lines_to_widget(obj, cur_fontsize, linemode);
771                            break;
772                          }
773          }
774      }
775 }
776
777 /*
778  * setting internal state of mulitline label
779  * singleline doesn't need it
780  */
781
782 void _label_state_change(Evas_Object *obj)
783 {
784    Widget_Data *wd = elm_widget_data_get(obj);
785    if (!wd) return;
786
787    if (wd->linewrap)
788    {
789            if (wd->wrapmode)
790                    edje_object_signal_emit(wd->lbl, "elm,state,wordwrap", "elm");
791            else
792                    edje_object_signal_emit(wd->lbl, "elm,state,default", "elm");
793    }
794 }
795
796 void _label_sliding_change(Evas_Object *obj)
797 {
798    Widget_Data *wd = elm_widget_data_get(obj);
799    if (!wd) return;
800
801    if (wd->linewrap)
802    {
803            wd->slidingmode = EINA_FALSE;
804            fprintf(stderr, "ERR: elm_label dosen't support multiline sliding effect!!!\n");
805            fprintf(stderr, "ERR: elm_label dosen't support multiline sliding effect!!!\n");
806            fprintf(stderr, "ERR: elm_label dosen't support multiline sliding effect!!!\n");
807            return;
808    }
809
810    char *plaintxt = _mkup_to_text(edje_object_part_text_get(wd->lbl, "elm.text"));
811    int plainlen = 0;
812    if (plaintxt != NULL)
813    {
814            plainlen = strlen(plaintxt);
815        free(plaintxt);
816    }
817    if (plainlen < 1) 
818    {
819            wd->slidingmode = EINA_TRUE;
820            fprintf(stderr, "ERR: too short to slide label!!!\n");
821            return;
822    }
823
824    if (wd->slidingmode)
825    {
826            if (wd->ellipsis)
827            {
828                    wd->slidingellipsis = EINA_TRUE;
829                    elm_label_ellipsis_set(obj, EINA_FALSE);
830            }
831            Edje_Message_Int_Set *msg = alloca(sizeof(Edje_Message_Int_Set) + (sizeof(int)));
832            
833            msg->count=1;
834            msg->val[0] = (int)wd->slide_duration;
835
836            edje_object_message_send(wd->lbl, EDJE_MESSAGE_INT_SET, 0, msg);
837            edje_object_signal_emit(wd->lbl, "elm,state,slide,start", "elm");
838    }
839    else
840    {
841            edje_object_signal_emit(wd->lbl, "elm,state,slide,stop", "elm");
842            if (wd->slidingellipsis)
843            {
844                    wd->slidingellipsis = EINA_FALSE;
845                    elm_label_ellipsis_set(obj, EINA_TRUE);
846            }
847    }
848 }
849
850 /**
851  * Add a new label to the parent
852  *
853  * @param parent The parent object
854  * @return The new object or NULL if it cannot be created
855  *
856  * @ingroup Label
857  */
858 EAPI Evas_Object *
859 elm_label_add(Evas_Object *parent)
860 {
861    Evas_Object *obj;
862    Evas *e;
863    Widget_Data *wd;
864
865    wd = ELM_NEW(Widget_Data);
866    e = evas_object_evas_get(parent);
867    wd->bg = evas_object_rectangle_add(e);
868    evas_object_color_set(wd->bg, 0, 0, 0, 0);
869    obj = elm_widget_add(e);
870    ELM_SET_WIDTYPE(widtype, "label");
871    elm_widget_type_set(obj, "label");
872    elm_widget_sub_object_add(parent, obj);
873    elm_widget_data_set(obj, wd);
874    elm_widget_del_hook_set(obj, _del_hook);
875    elm_widget_theme_hook_set(obj, _theme_hook);
876    elm_widget_can_focus_set(obj, 0);
877
878    wd->linewrap = EINA_FALSE;
879    wd->bgcolor = EINA_FALSE;
880    wd->ellipsis = EINA_FALSE;
881    wd->wrapmode = EINA_FALSE;
882    wd->slidingmode = EINA_FALSE;
883    wd->slidingellipsis = EINA_FALSE;
884    wd->wrap_w = 0;
885    wd->wrap_h = 0;
886    wd->slide_duration = 10;
887
888    wd->lbl = edje_object_add(e);
889    _elm_theme_object_set(obj, wd->lbl, "label", "base", "default");
890    wd->label = eina_stringshare_add("<br>");
891    edje_object_part_text_set(wd->lbl, "elm.text", "<br>");
892    elm_widget_resize_object_set(obj, wd->lbl);
893    
894    evas_object_event_callback_add(wd->lbl, EVAS_CALLBACK_RESIZE, _resize, obj);
895    
896    wd->changed = 1;
897    _sizing_eval(obj);
898    return obj;
899 }
900
901 /**
902  * Set the label on the label object
903  *
904  * @param obj The label object
905  * @param label The label will be used on the label object
906  *
907  * @ingroup Label
908  */
909 EAPI void
910 elm_label_label_set(Evas_Object *obj, const char *label)
911 {
912    ELM_CHECK_WIDTYPE(obj, widtype);
913    Widget_Data *wd = elm_widget_data_get(obj);
914    if (!wd) return;
915    if (!label) label = "";
916    eina_stringshare_replace(&wd->label, label);
917    edje_object_part_text_set(wd->lbl, "elm.text", label);
918    wd->changed = 1;
919    _sizing_eval(obj);
920 }
921
922 /**
923  * Get the label used on the label object
924  *
925  * @param obj The label object
926  * @return The string inside the label
927  * @ingroup Label
928  */
929 EAPI const char *
930 elm_label_label_get(const Evas_Object *obj)
931 {
932    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
933    Widget_Data *wd = elm_widget_data_get(obj);
934    if (!wd) return NULL;
935    return wd->label;
936 }
937
938 /**
939  * Set the wrapping behavior of the label
940  *
941  * @param obj The label object
942  * @param wrap To wrap text or not
943  * @ingroup Label
944  */
945 EAPI void
946 elm_label_line_wrap_set(Evas_Object *obj, Eina_Bool wrap)
947 {
948    ELM_CHECK_WIDTYPE(obj, widtype);
949    Widget_Data *wd = elm_widget_data_get(obj);
950    if (!wd) return;
951    const char *t;
952    if (wd->linewrap == wrap) return;
953    wd->linewrap = wrap;
954    t = eina_stringshare_add(elm_label_label_get(obj));
955    _theme_change(obj);
956    elm_label_label_set(obj, t);
957    eina_stringshare_del(t);
958    wd->changed = 1;
959    _sizing_eval(obj);
960 }
961
962 /**
963  * Get the wrapping behavior of the label
964  *
965  * @param obj The label object
966  * @return To wrap text or not
967  * @ingroup Label
968  */
969 EAPI Eina_Bool
970 elm_label_line_wrap_get(const Evas_Object *obj)
971 {
972    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
973    Widget_Data *wd = elm_widget_data_get(obj);
974    if (!wd) return EINA_FALSE;
975    return wd->linewrap;
976 }
977
978 /**
979  * Set wrap width of the label
980  *
981  * @param obj The label object
982  * @param w The wrap width in pixels at a minimum where words need to wrap
983  * @ingroup Label
984  */
985 EAPI void
986 elm_label_wrap_width_set(Evas_Object *obj, Evas_Coord w)
987 {
988    ELM_CHECK_WIDTYPE(obj, widtype);
989    Widget_Data *wd = elm_widget_data_get(obj);
990    if (!wd) return;
991    if (w < 0) w = 0;
992    if (wd->wrap_w == w) return;
993    if (wd->ellipsis) edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
994    wd->wrap_w = w;
995    _sizing_eval(obj);
996 }
997
998 /**
999  * get wrap width of the label
1000  *
1001  * @param obj The label object
1002  * @return The wrap width in pixels at a minimum where words need to wrap
1003  * @ingroup Label
1004  */
1005 EAPI Evas_Coord
1006 elm_label_wrap_width_get(const Evas_Object *obj)
1007 {
1008    ELM_CHECK_WIDTYPE(obj, widtype) 0;
1009    Widget_Data *wd = elm_widget_data_get(obj);
1010    if (!wd) return 0;
1011    return wd->wrap_w;
1012 }
1013
1014 /**
1015  * Set wrap height of the label
1016  *
1017  * @param obj The label object
1018  * @param w The wrap width in pixels at a minimum where words need to wrap
1019  * @ingroup Label
1020  */
1021 EAPI void
1022 elm_label_wrap_height_set(Evas_Object *obj, Evas_Coord h)
1023 {
1024    ELM_CHECK_WIDTYPE(obj, widtype);
1025    Widget_Data *wd = elm_widget_data_get(obj);
1026    if (!wd) return;
1027    if (h < 0) h = 0;
1028    if (wd->wrap_h == h) return;
1029    if (wd->ellipsis) edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
1030    wd->wrap_h = h;
1031    _sizing_eval(obj);
1032 }
1033
1034 /**
1035  * get wrap width of the label
1036  *
1037  * @param obj The label object
1038  * @return The wrap height in pixels at a minimum where words need to wrap
1039  * @ingroup Label
1040  */
1041 EAPI Evas_Coord
1042 elm_label_wrap_height_get(const Evas_Object *obj)
1043 {
1044    ELM_CHECK_WIDTYPE(obj, widtype) 0;
1045    Widget_Data *wd = elm_widget_data_get(obj);
1046    if (!wd) return 0;
1047    return wd->wrap_h;
1048 }
1049
1050 /**
1051  * Set the font size on the label object
1052  *
1053  * @param obj The label object
1054  * @param size font size
1055  *
1056  * @ingroup Label
1057  */
1058 EAPI void
1059 elm_label_fontsize_set(Evas_Object *obj, int fontsize)
1060 {
1061    ELM_CHECK_WIDTYPE(obj, widtype);
1062    Widget_Data *wd = elm_widget_data_get(obj);
1063    Eina_Strbuf *fontbuf = NULL;
1064    int len, removeflag = 0;
1065
1066    if (!wd) return;
1067    len = strlen(wd->label);
1068    if (len <= 0) return;
1069    fontbuf = eina_strbuf_new();
1070    eina_strbuf_append_printf(fontbuf, "%d", fontsize);
1071
1072    if (fontsize == 0) removeflag = 1; // remove fontsize tag
1073
1074    if (_stringshare_key_value_replace(&wd->label, "font_size", eina_strbuf_string_get(fontbuf), removeflag) == 0)
1075      {
1076        edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
1077        wd->changed = 1;
1078        _sizing_eval(obj);
1079      }
1080    eina_strbuf_free(fontbuf);
1081 }
1082
1083 /**
1084  * Set the text align on the label object
1085  *
1086  * @param obj The label object
1087  * @param align align mode ("left", "center", "right")
1088  *
1089  * @ingroup Label
1090  */
1091 EAPI void
1092 elm_label_text_align_set(Evas_Object *obj, const char *alignmode)
1093 {
1094    ELM_CHECK_WIDTYPE(obj, widtype);
1095    Widget_Data *wd = elm_widget_data_get(obj);
1096    int len;
1097
1098    if (!wd) return;
1099    len = strlen(wd->label);
1100    if (len <= 0) return;
1101
1102    if (_stringshare_key_value_replace(&wd->label, "align", alignmode, 0) == 0)
1103      edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
1104
1105    wd->changed = 1;
1106    _sizing_eval(obj);
1107 }
1108
1109 /**
1110  * Set the text color on the label object
1111  *
1112  * @param obj The label object
1113  * @param r Red property background color of The label object 
1114  * @param g Green property background color of The label object 
1115  * @param b Blue property background color of The label object 
1116  * @param a Alpha property background color of The label object 
1117  *
1118  * @ingroup Label
1119  */
1120 EAPI void
1121 elm_label_text_color_set(Evas_Object *obj, unsigned int r, unsigned int g, unsigned int b, unsigned int a)
1122 {
1123    ELM_CHECK_WIDTYPE(obj, widtype);
1124    Widget_Data *wd = elm_widget_data_get(obj);
1125    Eina_Strbuf *colorbuf = NULL;
1126    int len;
1127
1128    if (!wd) return;
1129    len = strlen(wd->label);
1130    if (len <= 0) return;
1131    colorbuf = eina_strbuf_new();
1132    eina_strbuf_append_printf(colorbuf, "#%02X%02X%02X%02X", r, g, b, a);
1133
1134    if (_stringshare_key_value_replace(&wd->label, "color", eina_strbuf_string_get(colorbuf), 0) == 0)
1135      {
1136        edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
1137        wd->changed = 1;
1138        _sizing_eval(obj);
1139      }
1140    eina_strbuf_free(colorbuf);
1141 }
1142
1143
1144 /**
1145  * Set background color of the label
1146  *
1147  * @param obj The label object
1148  * @param r Red property background color of The label object 
1149  * @param g Green property background color of The label object 
1150  * @param b Blue property background color of The label object 
1151  * @param a Alpha property background color of The label object 
1152  * @ingroup Label
1153  */
1154 EAPI void
1155 elm_label_background_color_set(Evas_Object *obj, unsigned int r, unsigned int g, unsigned int b, unsigned int a)
1156 {
1157    ELM_CHECK_WIDTYPE(obj, widtype);
1158    Widget_Data *wd = elm_widget_data_get(obj);
1159    if (!wd) return;
1160    evas_object_color_set(wd->bg, r, g, b, a);
1161
1162    if (wd->bgcolor == EINA_FALSE)
1163      {
1164         wd->bgcolor = 1;
1165         edje_object_part_swallow(wd->lbl, "label.swallow.background", wd->bg);
1166      }
1167 }
1168
1169 /**
1170  * Set the ellipsis behavior of the label
1171  *
1172  * @param obj The label object
1173  * @param ellipsis To ellipsis text or not
1174  * @ingroup Label
1175  */
1176 EAPI void
1177 elm_label_ellipsis_set(Evas_Object *obj, Eina_Bool ellipsis)
1178 {
1179    ELM_CHECK_WIDTYPE(obj, widtype);
1180    Widget_Data *wd = elm_widget_data_get(obj);
1181    if (!wd) return;
1182    if (wd->ellipsis == ellipsis) return;
1183    wd->ellipsis = ellipsis;
1184    if (wd->linewrap) _theme_change(obj);
1185    edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
1186    wd->changed = 1;
1187    _sizing_eval(obj);
1188 }
1189
1190 /**
1191  * Set the wrapmode of the label
1192  *
1193  * @param obj The label object
1194  * @param wrapmode 0 is charwrap, 1 is wordwrap
1195  * @ingroup Label
1196  */
1197 EAPI void
1198 elm_label_wrap_mode_set(Evas_Object *obj, Eina_Bool wrapmode)
1199 {
1200    ELM_CHECK_WIDTYPE(obj, widtype);
1201    Widget_Data *wd = elm_widget_data_get(obj);
1202    if (!wd) return;
1203    if (wd->wrapmode == wrapmode) return;
1204    wd->wrapmode = wrapmode;
1205    _label_state_change(obj);
1206    wd->changed = 1;
1207    _sizing_eval(obj);
1208 }
1209
1210 /**
1211  * Set the text slide of the label
1212  *
1213  * @param obj The label object
1214  * @param slide To start slide or stop
1215  * @ingroup Label
1216  */
1217 EAPI void
1218 elm_label_slide_set(Evas_Object *obj, Eina_Bool slide)
1219 {
1220    ELM_CHECK_WIDTYPE(obj, widtype);
1221    Widget_Data *wd = elm_widget_data_get(obj);
1222    if (!wd) return;
1223
1224    if (wd->slidingmode == slide) return;
1225    wd->slidingmode = slide;
1226    _label_sliding_change(obj);
1227    wd->changed = 1;
1228    _sizing_eval(obj);
1229 }
1230
1231 /**
1232  * get the text slide mode of the label
1233  *
1234  * @param obj The label object
1235  * @return slide slide mode value
1236  * @ingroup Label
1237  */
1238 EAPI Eina_Bool
1239 elm_label_slide_get(Evas_Object *obj)
1240 {
1241    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
1242    Widget_Data *wd = elm_widget_data_get(obj);
1243    if (!wd) return EINA_FALSE;
1244
1245    return wd->slidingmode;
1246 }
1247
1248 /**
1249  * set the slide duration(speed) of the label
1250  *
1251  * @param obj The label object
1252  * @return The duration time in moving text from slide begin position to slide end position
1253  * @ingroup Label
1254  */
1255 EAPI void
1256 elm_label_slide_duration_set(Evas_Object *obj, int duration)
1257 {
1258    ELM_CHECK_WIDTYPE(obj, widtype);
1259    Widget_Data *wd = elm_widget_data_get(obj);
1260    Edje_Message_Int_Set *msg = alloca(sizeof(Edje_Message_Int_Set) + (sizeof(int)));
1261    
1262    if (!wd) return;
1263    wd->slide_duration = duration;
1264
1265    msg->count=1;
1266    msg->val[0] = (int)wd->slide_duration;
1267
1268    edje_object_message_send(wd->lbl, EDJE_MESSAGE_INT_SET, 0, msg);
1269 }
1270
1271 /**
1272  * get the slide duration(speed) of the label
1273  *
1274  * @param obj The label object
1275  * @return The duration time in moving text from slide begin position to slide end position
1276  * @ingroup Label
1277  */
1278 EAPI int
1279 elm_label_slide_duration_get(Evas_Object *obj)
1280 {
1281    ELM_CHECK_WIDTYPE(obj, widtype) 0;
1282    Widget_Data *wd = elm_widget_data_get(obj);
1283    if (!wd) return 0;
1284    return wd->slide_duration;
1285 }