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    }
808
809    if (wd->slidingmode)
810    {
811            if (wd->ellipsis)
812            {
813                    wd->slidingellipsis = EINA_TRUE;
814                    elm_label_ellipsis_set(obj, EINA_FALSE);
815            }
816            Edje_Message_Int_Set *msg = alloca(sizeof(Edje_Message_Int_Set) + (sizeof(int)));
817            
818            msg->count=1;
819            msg->val[0] = (int)wd->slide_duration;
820
821            edje_object_message_send(wd->lbl, EDJE_MESSAGE_INT_SET, 0, msg);
822            edje_object_signal_emit(wd->lbl, "elm,state,slide,start", "elm");
823    }
824    else
825    {
826            edje_object_signal_emit(wd->lbl, "elm,state,slide,stop", "elm");
827            if (wd->slidingellipsis)
828            {
829                    wd->slidingellipsis = EINA_FALSE;
830                    elm_label_ellipsis_set(obj, EINA_TRUE);
831            }
832    }
833 }
834
835 /**
836  * Add a new label to the parent
837  *
838  * @param parent The parent object
839  * @return The new object or NULL if it cannot be created
840  *
841  * @ingroup Label
842  */
843 EAPI Evas_Object *
844 elm_label_add(Evas_Object *parent)
845 {
846    Evas_Object *obj;
847    Evas *e;
848    Widget_Data *wd;
849
850    wd = ELM_NEW(Widget_Data);
851    e = evas_object_evas_get(parent);
852    wd->bg = evas_object_rectangle_add(e);
853    evas_object_color_set(wd->bg, 0, 0, 0, 0);
854    obj = elm_widget_add(e);
855    ELM_SET_WIDTYPE(widtype, "label");
856    elm_widget_type_set(obj, "label");
857    elm_widget_sub_object_add(parent, obj);
858    elm_widget_data_set(obj, wd);
859    elm_widget_del_hook_set(obj, _del_hook);
860    elm_widget_theme_hook_set(obj, _theme_hook);
861    elm_widget_can_focus_set(obj, 0);
862
863    wd->linewrap = EINA_FALSE;
864    wd->bgcolor = EINA_FALSE;
865    wd->ellipsis = EINA_FALSE;
866    wd->wrapmode = EINA_FALSE;
867    wd->slidingmode = EINA_FALSE;
868    wd->slidingellipsis = EINA_FALSE;
869    wd->wrap_w = 0;
870    wd->wrap_h = 0;
871    wd->slide_duration = 10;
872
873    wd->lbl = edje_object_add(e);
874    _elm_theme_object_set(obj, wd->lbl, "label", "base", "default");
875    wd->label = eina_stringshare_add("<br>");
876    edje_object_part_text_set(wd->lbl, "elm.text", "<br>");
877    elm_widget_resize_object_set(obj, wd->lbl);
878    
879    evas_object_event_callback_add(wd->lbl, EVAS_CALLBACK_RESIZE, _resize, obj);
880    
881    wd->changed = 1;
882    _sizing_eval(obj);
883    return obj;
884 }
885
886 /**
887  * Set the label on the label object
888  *
889  * @param obj The label object
890  * @param label The label will be used on the label object
891  *
892  * @ingroup Label
893  */
894 EAPI void
895 elm_label_label_set(Evas_Object *obj, const char *label)
896 {
897    ELM_CHECK_WIDTYPE(obj, widtype);
898    Widget_Data *wd = elm_widget_data_get(obj);
899    if (!wd) return;
900    if (!label) label = "";
901    eina_stringshare_replace(&wd->label, label);
902    edje_object_part_text_set(wd->lbl, "elm.text", label);
903    wd->changed = 1;
904    _sizing_eval(obj);
905 }
906
907 /**
908  * Get the label used on the label object
909  *
910  * @param obj The label object
911  * @return The string inside the label
912  * @ingroup Label
913  */
914 EAPI const char *
915 elm_label_label_get(const Evas_Object *obj)
916 {
917    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
918    Widget_Data *wd = elm_widget_data_get(obj);
919    if (!wd) return NULL;
920    return wd->label;
921 }
922
923 /**
924  * Set the wrapping behavior of the label
925  *
926  * @param obj The label object
927  * @param wrap To wrap text or not
928  * @ingroup Label
929  */
930 EAPI void
931 elm_label_line_wrap_set(Evas_Object *obj, Eina_Bool wrap)
932 {
933    ELM_CHECK_WIDTYPE(obj, widtype);
934    Widget_Data *wd = elm_widget_data_get(obj);
935    if (!wd) return;
936    const char *t;
937    if (wd->linewrap == wrap) return;
938    wd->linewrap = wrap;
939    t = eina_stringshare_add(elm_label_label_get(obj));
940    _theme_change(obj);
941    elm_label_label_set(obj, t);
942    eina_stringshare_del(t);
943    wd->changed = 1;
944    _sizing_eval(obj);
945 }
946
947 /**
948  * Get the wrapping behavior of the label
949  *
950  * @param obj The label object
951  * @return To wrap text or not
952  * @ingroup Label
953  */
954 EAPI Eina_Bool
955 elm_label_line_wrap_get(const Evas_Object *obj)
956 {
957    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
958    Widget_Data *wd = elm_widget_data_get(obj);
959    if (!wd) return EINA_FALSE;
960    return wd->linewrap;
961 }
962
963 /**
964  * Set wrap width of the label
965  *
966  * @param obj The label object
967  * @param w The wrap width in pixels at a minimum where words need to wrap
968  * @ingroup Label
969  */
970 EAPI void
971 elm_label_wrap_width_set(Evas_Object *obj, Evas_Coord w)
972 {
973    ELM_CHECK_WIDTYPE(obj, widtype);
974    Widget_Data *wd = elm_widget_data_get(obj);
975    if (!wd) return;
976    if (w < 0) w = 0;
977    if (wd->wrap_w == w) return;
978    if (wd->ellipsis) edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
979    wd->wrap_w = w;
980    _sizing_eval(obj);
981 }
982
983 /**
984  * get wrap width of the label
985  *
986  * @param obj The label object
987  * @return The wrap width in pixels at a minimum where words need to wrap
988  * @ingroup Label
989  */
990 EAPI Evas_Coord
991 elm_label_wrap_width_get(const Evas_Object *obj)
992 {
993    ELM_CHECK_WIDTYPE(obj, widtype) 0;
994    Widget_Data *wd = elm_widget_data_get(obj);
995    if (!wd) return 0;
996    return wd->wrap_w;
997 }
998
999 /**
1000  * Set wrap height of the label
1001  *
1002  * @param obj The label object
1003  * @param w The wrap width in pixels at a minimum where words need to wrap
1004  * @ingroup Label
1005  */
1006 EAPI void
1007 elm_label_wrap_height_set(Evas_Object *obj, Evas_Coord h)
1008 {
1009    ELM_CHECK_WIDTYPE(obj, widtype);
1010    Widget_Data *wd = elm_widget_data_get(obj);
1011    if (!wd) return;
1012    if (h < 0) h = 0;
1013    if (wd->wrap_h == h) return;
1014    if (wd->ellipsis) edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
1015    wd->wrap_h = h;
1016    _sizing_eval(obj);
1017 }
1018
1019 /**
1020  * get wrap width of the label
1021  *
1022  * @param obj The label object
1023  * @return The wrap height in pixels at a minimum where words need to wrap
1024  * @ingroup Label
1025  */
1026 EAPI Evas_Coord
1027 elm_label_wrap_height_get(const Evas_Object *obj)
1028 {
1029    ELM_CHECK_WIDTYPE(obj, widtype) 0;
1030    Widget_Data *wd = elm_widget_data_get(obj);
1031    if (!wd) return 0;
1032    return wd->wrap_h;
1033 }
1034
1035 /**
1036  * Set the font size on the label object
1037  *
1038  * @param obj The label object
1039  * @param size font size
1040  *
1041  * @ingroup Label
1042  */
1043 EAPI void
1044 elm_label_fontsize_set(Evas_Object *obj, int fontsize)
1045 {
1046    ELM_CHECK_WIDTYPE(obj, widtype);
1047    Widget_Data *wd = elm_widget_data_get(obj);
1048    Eina_Strbuf *fontbuf = NULL;
1049    int len, removeflag = 0;
1050
1051    if (!wd) return;
1052    len = strlen(wd->label);
1053    if (len <= 0) return;
1054    fontbuf = eina_strbuf_new();
1055    eina_strbuf_append_printf(fontbuf, "%d", fontsize);
1056
1057    if (fontsize == 0) removeflag = 1; // remove fontsize tag
1058
1059    if (_stringshare_key_value_replace(&wd->label, "font_size", eina_strbuf_string_get(fontbuf), removeflag) == 0)
1060      {
1061        edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
1062        wd->changed = 1;
1063        _sizing_eval(obj);
1064      }
1065    eina_strbuf_free(fontbuf);
1066 }
1067
1068 /**
1069  * Set the text align on the label object
1070  *
1071  * @param obj The label object
1072  * @param align align mode ("left", "center", "right")
1073  *
1074  * @ingroup Label
1075  */
1076 EAPI void
1077 elm_label_text_align_set(Evas_Object *obj, const char *alignmode)
1078 {
1079    ELM_CHECK_WIDTYPE(obj, widtype);
1080    Widget_Data *wd = elm_widget_data_get(obj);
1081    int len;
1082
1083    if (!wd) return;
1084    len = strlen(wd->label);
1085    if (len <= 0) return;
1086
1087    if (_stringshare_key_value_replace(&wd->label, "align", alignmode, 0) == 0)
1088      edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
1089
1090    wd->changed = 1;
1091    _sizing_eval(obj);
1092 }
1093
1094 /**
1095  * Set the text color on the label object
1096  *
1097  * @param obj The label object
1098  * @param r Red property background color of The label object 
1099  * @param g Green property background color of The label object 
1100  * @param b Blue property background color of The label object 
1101  * @param a Alpha property background color of The label object 
1102  *
1103  * @ingroup Label
1104  */
1105 EAPI void
1106 elm_label_text_color_set(Evas_Object *obj, unsigned int r, unsigned int g, unsigned int b, unsigned int a)
1107 {
1108    ELM_CHECK_WIDTYPE(obj, widtype);
1109    Widget_Data *wd = elm_widget_data_get(obj);
1110    Eina_Strbuf *colorbuf = NULL;
1111    int len;
1112
1113    if (!wd) return;
1114    len = strlen(wd->label);
1115    if (len <= 0) return;
1116    colorbuf = eina_strbuf_new();
1117    eina_strbuf_append_printf(colorbuf, "#%02X%02X%02X%02X", r, g, b, a);
1118
1119    if (_stringshare_key_value_replace(&wd->label, "color", eina_strbuf_string_get(colorbuf), 0) == 0)
1120      {
1121        edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
1122        wd->changed = 1;
1123        _sizing_eval(obj);
1124      }
1125    eina_strbuf_free(colorbuf);
1126 }
1127
1128
1129 /**
1130  * Set background color of the label
1131  *
1132  * @param obj The label object
1133  * @param r Red property background color of The label object 
1134  * @param g Green property background color of The label object 
1135  * @param b Blue property background color of The label object 
1136  * @param a Alpha property background color of The label object 
1137  * @ingroup Label
1138  */
1139 EAPI void
1140 elm_label_background_color_set(Evas_Object *obj, unsigned int r, unsigned int g, unsigned int b, unsigned int a)
1141 {
1142    ELM_CHECK_WIDTYPE(obj, widtype);
1143    Widget_Data *wd = elm_widget_data_get(obj);
1144    if (!wd) return;
1145    evas_object_color_set(wd->bg, r, g, b, a);
1146
1147    if (wd->bgcolor == EINA_FALSE)
1148      {
1149         wd->bgcolor = 1;
1150         edje_object_part_swallow(wd->lbl, "label.swallow.background", wd->bg);
1151      }
1152 }
1153
1154 /**
1155  * Set the ellipsis behavior of the label
1156  *
1157  * @param obj The label object
1158  * @param ellipsis To ellipsis text or not
1159  * @ingroup Label
1160  */
1161 EAPI void
1162 elm_label_ellipsis_set(Evas_Object *obj, Eina_Bool ellipsis)
1163 {
1164    ELM_CHECK_WIDTYPE(obj, widtype);
1165    Widget_Data *wd = elm_widget_data_get(obj);
1166    if (!wd) return;
1167    if (wd->ellipsis == ellipsis) return;
1168    wd->ellipsis = ellipsis;
1169    if (wd->linewrap) _theme_change(obj);
1170    edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
1171    wd->changed = 1;
1172    _sizing_eval(obj);
1173 }
1174
1175 /**
1176  * Set the wrapmode of the label
1177  *
1178  * @param obj The label object
1179  * @param wrapmode 0 is charwrap, 1 is wordwrap
1180  * @ingroup Label
1181  */
1182 EAPI void
1183 elm_label_wrap_mode_set(Evas_Object *obj, Eina_Bool wrapmode)
1184 {
1185    ELM_CHECK_WIDTYPE(obj, widtype);
1186    Widget_Data *wd = elm_widget_data_get(obj);
1187    if (!wd) return;
1188    if (wd->wrapmode == wrapmode) return;
1189    wd->wrapmode = wrapmode;
1190    _label_state_change(obj);
1191    wd->changed = 1;
1192    _sizing_eval(obj);
1193 }
1194
1195 /**
1196  * Set the text slide of the label
1197  *
1198  * @param obj The label object
1199  * @param slide To start slide or stop
1200  * @ingroup Label
1201  */
1202 EAPI void
1203 elm_label_slide_set(Evas_Object *obj, Eina_Bool slide)
1204 {
1205    ELM_CHECK_WIDTYPE(obj, widtype);
1206    Widget_Data *wd = elm_widget_data_get(obj);
1207    if (!wd) return;
1208
1209    if (wd->slidingmode == slide) return;
1210    wd->slidingmode = slide;
1211    _label_sliding_change(obj);
1212    wd->changed = 1;
1213    _sizing_eval(obj);
1214 }
1215
1216 /**
1217  * get the text slide mode of the label
1218  *
1219  * @param obj The label object
1220  * @return slide slide mode value
1221  * @ingroup Label
1222  */
1223 EAPI Eina_Bool
1224 elm_label_slide_get(Evas_Object *obj)
1225 {
1226    ELM_CHECK_WIDTYPE(obj, widtype);
1227    Widget_Data *wd = elm_widget_data_get(obj);
1228    if (!wd) return EINA_FALSE;
1229
1230    return wd->slidingmode;
1231 }
1232
1233 /**
1234  * set the slide duration(speed) of the label
1235  *
1236  * @param obj The label object
1237  * @return The duration time in moving text from slide begin position to slide end position
1238  * @ingroup Label
1239  */
1240 EAPI void
1241 elm_label_slide_duration_set(Evas_Object *obj, int duration)
1242 {
1243    ELM_CHECK_WIDTYPE(obj, widtype);
1244    Widget_Data *wd = elm_widget_data_get(obj);
1245    Edje_Message_Int_Set *msg = alloca(sizeof(Edje_Message_Int_Set) + (sizeof(int)));
1246    
1247    if (!wd) return;
1248    wd->slide_duration = duration;
1249
1250    msg->count=1;
1251    msg->val[0] = (int)wd->slide_duration;
1252
1253    edje_object_message_send(wd->lbl, EDJE_MESSAGE_INT_SET, 0, msg);
1254 }
1255
1256 /**
1257  * get the slide duration(speed) of the label
1258  *
1259  * @param obj The label object
1260  * @return The duration time in moving text from slide begin position to slide end position
1261  * @ingroup Label
1262  */
1263 EAPI int
1264 elm_label_slide_duration_get(Evas_Object *obj)
1265 {
1266    ELM_CHECK_WIDTYPE(obj, widtype) 0;
1267    Widget_Data *wd = elm_widget_data_get(obj);
1268    if (!wd) return;
1269    return wd->slide_duration;
1270 }