[Gengrid] Fixed item_show bug. Merge from opensource r74643.
[framework/uifw/elementary.git] / src / lib / elm_label.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 typedef struct _Widget_Data Widget_Data;
5
6 struct _Widget_Data
7 {
8    Evas_Object *lbl;
9    const char *label;
10    const char *format;
11    Ecore_Job *deferred_recalc_job;
12    double slide_duration;
13    Evas_Coord lastw;
14    Evas_Coord wrap_w;
15    Elm_Wrap_Type linewrap;
16    Eina_Bool changed : 1;
17    Eina_Bool ellipsis : 1;
18    Eina_Bool slidingmode : 1;
19    Eina_Bool slidingellipsis : 1;
20 };
21
22 static const char *widtype = NULL;
23 static void _del_hook(Evas_Object *obj);
24 static void _mirrored_set(Evas_Object *obj, Eina_Bool rtl);
25 static void _theme_hook(Evas_Object *obj);
26 static void _sizing_eval(Evas_Object *obj);
27 static int _get_value_in_key_string(const char *oldstring, const char *key, char **value);
28 static int _strbuf_key_value_replace(Eina_Strbuf *srcbuf, const char *key, const char *value, int deleteflag);
29 static int _stringshare_key_value_replace(const char **srcstring, const char *key, const char *value, int deleteflag);
30 static void _label_sliding_change(Evas_Object *obj);
31 static void _label_format_set(Evas_Object *obj, const char *format);
32
33 static void
34 _elm_recalc_job(void *data)
35 {
36    Widget_Data *wd = elm_widget_data_get(data);
37    Evas_Coord minw = -1, minh = -1;
38    Evas_Coord resw;
39    if (!wd) return;
40    evas_event_freeze(evas_object_evas_get(data));
41    wd->deferred_recalc_job = NULL;
42    evas_object_geometry_get(wd->lbl, NULL, NULL, &resw, NULL);
43    if (wd->wrap_w > resw)
44       resw = wd->wrap_w;
45
46    edje_object_size_min_restricted_calc(wd->lbl, &minw, &minh, resw, 0);
47    /* This is a hack to workaround the way min size hints are treated.
48     * If the minimum width is smaller than the restricted width, it means
49     * the mininmum doesn't matter. */
50    if ((minw <= resw) && (minw != wd->wrap_w))
51      {
52         Evas_Coord ominw = -1;
53         evas_object_size_hint_min_get(data, &ominw, NULL);
54         minw = ominw;
55      }
56    evas_object_size_hint_min_set(data, minw, minh);
57    evas_event_thaw(evas_object_evas_get(data));
58    evas_event_thaw_eval(evas_object_evas_get(data));
59 }
60
61 static void
62 _del_hook(Evas_Object *obj)
63 {
64    Widget_Data *wd = elm_widget_data_get(obj);
65    if (!wd) return;
66    evas_event_freeze(evas_object_evas_get(obj));
67    if (wd->deferred_recalc_job) ecore_job_del(wd->deferred_recalc_job);
68    if (wd->label) eina_stringshare_del(wd->label);
69    free(wd);
70    evas_event_thaw(evas_object_evas_get(obj));
71    evas_event_thaw_eval(evas_object_evas_get(obj));
72 }
73
74 static void
75 _theme_change(Evas_Object *obj)
76 {
77    Widget_Data *wd = elm_widget_data_get(obj);
78    if (!wd) return;
79
80    _elm_theme_object_set(obj, wd->lbl, "label", "base",
81          elm_widget_style_get(obj));
82 }
83
84 static void
85 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
86 {
87    Widget_Data *wd = elm_widget_data_get(obj);
88    if (!wd) return;
89    edje_object_mirrored_set(wd->lbl, rtl);
90 }
91
92 static void
93 _theme_hook(Evas_Object *obj)
94 {
95    Widget_Data *wd = elm_widget_data_get(obj);
96    if (!wd) return;
97    evas_event_freeze(evas_object_evas_get(obj));
98    _elm_widget_mirrored_reload(obj);
99    _mirrored_set(obj, elm_widget_mirrored_get(obj));
100    _theme_change(obj);
101    _label_format_set(wd->lbl, wd->format);
102    edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
103    edje_object_scale_set(wd->lbl, elm_widget_scale_get(obj) *
104                          _elm_config->scale);
105    _label_sliding_change(obj);
106    _sizing_eval(obj);
107    evas_event_thaw(evas_object_evas_get(obj));
108    evas_event_thaw_eval(evas_object_evas_get(obj));
109 }
110
111 static void
112 _sizing_eval(Evas_Object *obj)
113 {
114    Widget_Data *wd = elm_widget_data_get(obj);
115    Evas_Coord minw = -1, minh = -1;
116    Evas_Coord resw, resh;
117    if (!wd) return;
118
119    if (wd->linewrap)
120      {
121         evas_object_geometry_get(wd->lbl, NULL, NULL, &resw, &resh);
122         if ((resw == wd->lastw) && (!wd->changed)) return;
123         wd->changed = EINA_FALSE;
124         wd->lastw = resw;
125         _elm_recalc_job(obj);
126         // FIXME: works ok. but NOT for genlist. what should genlist do?
127         //        if (wd->deferred_recalc_job) ecore_job_del(wd->deferred_recalc_job);
128         //        wd->deferred_recalc_job = ecore_job_add(_elm_recalc_job, obj);
129      }
130    else
131      {
132         evas_event_freeze(evas_object_evas_get(obj));
133         evas_object_geometry_get(wd->lbl, NULL, NULL, &resw, &resh);
134         edje_object_size_min_calc(wd->lbl, &minw, &minh);
135         if (wd->wrap_w > 0 && minw > wd->wrap_w) minw = wd->wrap_w;
136         evas_object_size_hint_min_set(obj, minw, minh);
137         evas_event_thaw(evas_object_evas_get(obj));
138         evas_event_thaw_eval(evas_object_evas_get(obj));
139      }
140 }
141
142 static void
143 _lbl_resize(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
144 {
145    Widget_Data *wd = elm_widget_data_get(data);
146    if (!wd) return;
147    if (wd->linewrap) _sizing_eval(data);
148 }
149
150 static void
151 _label_format_set(Evas_Object *obj, const char *format)
152 {
153    if (format)
154       edje_object_part_text_style_user_push(obj, "elm.text", format);
155    else
156       edje_object_part_text_style_user_pop(obj, "elm.text");
157 }
158
159 static int
160 _get_value_in_key_string(const char *oldstring, const char *key, char **value)
161 {
162    char *curlocater, *endtag;
163    int firstindex = 0, foundflag = -1;
164
165    curlocater = strstr(oldstring, key);
166    if (curlocater)
167      {
168         int key_len = strlen(key);
169         endtag = curlocater + key_len;
170         if ((!endtag) || (*endtag != '='))
171           {
172              foundflag = 0;
173              return -1;
174           }
175         firstindex = abs(oldstring - curlocater);
176         firstindex += key_len + 1; // strlen("key") + strlen("=")
177         *value = (char *)oldstring + firstindex;
178
179         foundflag = 1;
180      }
181    else
182      {
183         foundflag = 0;
184      }
185
186    if (foundflag == 1) return 0;
187
188    return -1;
189 }
190
191
192 static int
193 _strbuf_key_value_replace(Eina_Strbuf *srcbuf, const char *key, const char *value, int deleteflag)
194 {
195    char *kvalue;
196    const char *srcstring = NULL;
197
198    srcstring = eina_strbuf_string_get(srcbuf);
199
200    if (_get_value_in_key_string(srcstring, key, &kvalue) == 0)
201      {
202         const char *val_end;
203         int val_end_idx = 0;
204         int key_start_idx = 0;
205         val_end = strchr(kvalue, ' ');
206
207         if (val_end)
208            val_end_idx = val_end - srcstring;
209         else
210            val_end_idx = kvalue - srcstring + strlen(kvalue) - 1;
211
212         /* -1 is because of the '=' */
213         key_start_idx = kvalue - srcstring - 1 - strlen(key);
214         eina_strbuf_remove(srcbuf, key_start_idx, val_end_idx);
215         if (!deleteflag)
216           {
217              eina_strbuf_insert_printf(srcbuf, "%s=%s", key_start_idx, key,
218                    value);
219           }
220      }
221    else if (!deleteflag)
222      {
223         if (*srcstring)
224           {
225              /* -1 because we want it before the ' */
226              eina_strbuf_insert_printf(srcbuf, " %s=%s",
227                    eina_strbuf_length_get(srcbuf) - 1, key, value);
228           }
229         else
230           {
231              eina_strbuf_append_printf(srcbuf, "DEFAULT='%s=%s'", key, value);
232           }
233      }
234    return 0;
235 }
236
237 static int
238 _stringshare_key_value_replace(const char **srcstring, const char *key, const char *value, int deleteflag)
239 {
240    Eina_Strbuf *sharebuf = NULL;
241
242    sharebuf = eina_strbuf_new();
243    eina_strbuf_append(sharebuf, *srcstring);
244    _strbuf_key_value_replace(sharebuf, key, value, deleteflag);
245    eina_stringshare_del(*srcstring);
246    *srcstring = eina_stringshare_add(eina_strbuf_string_get(sharebuf));
247    eina_strbuf_free(sharebuf);
248
249    return 0;
250 }
251
252 static void
253 _label_sliding_change(Evas_Object *obj)
254 {
255    Widget_Data *wd = elm_widget_data_get(obj);
256    if (!wd) return;
257    char *plaintxt;
258    int plainlen = 0;
259
260    // dosen't support multiline sliding effect
261    if (wd->linewrap)
262      {
263         wd->slidingmode = EINA_FALSE;
264         return;
265      }
266
267    plaintxt = _elm_util_mkup_to_text(edje_object_part_text_get(wd->lbl, "elm.text"));
268    if (plaintxt != NULL)
269      {
270         plainlen = strlen(plaintxt);
271         free(plaintxt);
272      }
273    // too short to slide label
274    if (plainlen < 1)
275      {
276         wd->slidingmode = EINA_TRUE;
277         return;
278      }
279
280    if (wd->slidingmode)
281      {
282         Edje_Message_Float_Set *msg = alloca(sizeof(Edje_Message_Float_Set) + (sizeof(double)));
283
284         if (wd->ellipsis)
285           {
286              wd->slidingellipsis = EINA_TRUE;
287              elm_label_ellipsis_set(obj, EINA_FALSE);
288           }
289
290         msg->count = 1;
291         msg->val[0] = wd->slide_duration;
292
293         edje_object_message_send(wd->lbl, EDJE_MESSAGE_FLOAT_SET, 0, msg);
294         edje_object_signal_emit(wd->lbl, "elm,state,slide,start", "elm");
295      }
296    else
297      {
298         edje_object_signal_emit(wd->lbl, "elm,state,slide,stop", "elm");
299         if (wd->slidingellipsis)
300           {
301              wd->slidingellipsis = EINA_FALSE;
302              elm_label_ellipsis_set(obj, EINA_TRUE);
303           }
304      }
305 }
306
307 static void
308 _elm_label_label_set(Evas_Object *obj, const char *item, const char *label)
309 {
310    ELM_CHECK_WIDTYPE(obj, widtype);
311    Widget_Data *wd = elm_widget_data_get(obj);
312    if (!wd) return;
313    if (item && strcmp(item, "default")) return;
314    if (!label) label = "";
315    eina_stringshare_replace(&wd->label, label);
316    _label_format_set(wd->lbl, wd->format);
317    edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
318    wd->changed = 1;
319    _sizing_eval(obj);
320 }
321
322 static const char *
323 _elm_label_label_get(const Evas_Object *obj, const char *item)
324 {
325    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
326    Widget_Data *wd = elm_widget_data_get(obj);
327    if (item && strcmp(item, "default")) return NULL;
328    if (!wd) return NULL;
329    return wd->label;
330 }
331
332 static void
333 _translate_hook(Evas_Object *obj)
334 {
335    evas_object_smart_callback_call(obj, "language,changed", NULL);
336 }
337
338 EAPI Evas_Object *
339 elm_label_add(Evas_Object *parent)
340 {
341    Evas_Object *obj;
342    Evas *e;
343    Widget_Data *wd;
344
345    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
346
347    ELM_SET_WIDTYPE(widtype, "label");
348    elm_widget_type_set(obj, "label");
349    elm_widget_sub_object_add(parent, obj);
350    elm_widget_data_set(obj, wd);
351    elm_widget_del_hook_set(obj, _del_hook);
352    elm_widget_theme_hook_set(obj, _theme_hook);
353    elm_widget_can_focus_set(obj, EINA_FALSE);
354    elm_widget_text_set_hook_set(obj, _elm_label_label_set);
355    elm_widget_text_get_hook_set(obj, _elm_label_label_get);
356    elm_widget_translate_hook_set(obj, _translate_hook);
357
358    wd->linewrap = ELM_WRAP_NONE;
359    wd->ellipsis = EINA_FALSE;
360    wd->slidingmode = EINA_FALSE;
361    wd->slidingellipsis = EINA_FALSE;
362    wd->wrap_w = -1;
363    wd->slide_duration = 10;
364
365    wd->lbl = edje_object_add(e);
366    _elm_theme_object_set(obj, wd->lbl, "label", "base", "default");
367    wd->format = eina_stringshare_add("");
368    wd->label = eina_stringshare_add("<br>");
369    _label_format_set(wd->lbl, wd->format);
370    edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
371
372    elm_widget_resize_object_set(obj, wd->lbl);
373
374    evas_object_event_callback_add(wd->lbl, EVAS_CALLBACK_RESIZE, _lbl_resize, obj);
375
376    _mirrored_set(obj, elm_widget_mirrored_get(obj));
377    wd->changed = 1;
378    _sizing_eval(obj);
379    return obj;
380 }
381
382 EAPI void
383 elm_label_line_wrap_set(Evas_Object *obj, Elm_Wrap_Type wrap)
384 {
385    ELM_CHECK_WIDTYPE(obj, widtype);
386    Widget_Data *wd = elm_widget_data_get(obj);
387    const char *wrap_str;
388    int len;
389
390    if (!wd) return;
391    if (wd->linewrap == wrap) return;
392    wd->linewrap = wrap;
393    len = strlen(wd->label);
394    if (len <= 0) return;
395
396    switch (wrap)
397      {
398       case ELM_WRAP_CHAR:
399          wrap_str = "char";
400          break;
401       case ELM_WRAP_WORD:
402          wrap_str = "word";
403          break;
404       case ELM_WRAP_MIXED:
405          wrap_str = "mixed";
406          break;
407       default:
408          wrap_str = "none";
409          break;
410      }
411
412    if (_stringshare_key_value_replace(&wd->format,
413             "wrap", wrap_str, 0) == 0)
414      {
415         _label_format_set(wd->lbl, wd->format);
416         edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
417         wd->changed = 1;
418         _sizing_eval(obj);
419      }
420 }
421
422 EAPI Elm_Wrap_Type
423 elm_label_line_wrap_get(const Evas_Object *obj)
424 {
425    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
426    Widget_Data *wd = elm_widget_data_get(obj);
427    if (!wd) return EINA_FALSE;
428    return wd->linewrap;
429 }
430
431 EAPI void
432 elm_label_wrap_width_set(Evas_Object *obj, Evas_Coord w)
433 {
434    ELM_CHECK_WIDTYPE(obj, widtype);
435    Widget_Data *wd = elm_widget_data_get(obj);
436    if (!wd) return;
437    if (w < 0) w = 0;
438    if (wd->wrap_w == w) return;
439    if (wd->ellipsis)
440      {
441         _label_format_set(wd->lbl, wd->format);
442         edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
443      }
444    wd->wrap_w = w;
445    _sizing_eval(obj);
446 }
447
448 EAPI Evas_Coord
449 elm_label_wrap_width_get(const Evas_Object *obj)
450 {
451    ELM_CHECK_WIDTYPE(obj, widtype) 0;
452    Widget_Data *wd = elm_widget_data_get(obj);
453    if (!wd) return 0;
454    return wd->wrap_w;
455 }
456
457 EAPI void
458 elm_label_ellipsis_set(Evas_Object *obj, Eina_Bool ellipsis)
459 {
460    ELM_CHECK_WIDTYPE(obj, widtype);
461    Widget_Data *wd = elm_widget_data_get(obj);
462    Eina_Strbuf *fontbuf = NULL;
463    int len, removeflag = 0;
464
465    if (!wd) return;
466    if (wd->ellipsis == ellipsis) return;
467    wd->ellipsis = ellipsis;
468    len = strlen(wd->label);
469    if (len <= 0) return;
470
471    if (ellipsis == EINA_FALSE) removeflag = 1;  // remove fontsize tag
472
473    fontbuf = eina_strbuf_new();
474    eina_strbuf_append_printf(fontbuf, "%f", 1.0);
475
476    if (_stringshare_key_value_replace(&wd->format,
477             "ellipsis", eina_strbuf_string_get(fontbuf), removeflag) == 0)
478      {
479         _label_format_set(wd->lbl, wd->format);
480         edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
481         wd->changed = 1;
482         _sizing_eval(obj);
483      }
484    eina_strbuf_free(fontbuf);
485
486 }
487
488 EAPI Eina_Bool
489 elm_label_ellipsis_get(const Evas_Object *obj)
490 {
491    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
492    Widget_Data *wd = elm_widget_data_get(obj);
493    if (!wd) return EINA_FALSE;
494    return wd->ellipsis;
495 }
496
497 EAPI void
498 elm_label_slide_set(Evas_Object *obj,
499                     Eina_Bool    slide)
500 {
501    ELM_CHECK_WIDTYPE(obj, widtype);
502    Widget_Data *wd = elm_widget_data_get(obj);
503    if (!wd) return;
504
505    if (wd->slidingmode == slide) return;
506    wd->slidingmode = slide;
507    _label_sliding_change(obj);
508    wd->changed = 1;
509    _sizing_eval(obj);
510 }
511
512 EAPI Eina_Bool
513 elm_label_slide_get(const Evas_Object *obj)
514 {
515    ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
516    Widget_Data *wd = elm_widget_data_get(obj);
517    if (!wd) return EINA_FALSE;
518    return wd->slidingmode;
519 }
520
521 EAPI void
522 elm_label_slide_duration_set(Evas_Object *obj, double duration)
523 {
524    ELM_CHECK_WIDTYPE(obj, widtype);
525    Widget_Data *wd = elm_widget_data_get(obj);
526    Edje_Message_Float_Set *msg = alloca(sizeof(Edje_Message_Float_Set) + (sizeof(double)));
527
528    if (!wd) return;
529    wd->slide_duration = duration;
530    msg->count = 1;
531    msg->val[0] = wd->slide_duration;
532    edje_object_message_send(wd->lbl, EDJE_MESSAGE_FLOAT_SET, 0, msg);
533 }
534
535 EAPI double
536 elm_label_slide_duration_get(const Evas_Object *obj)
537 {
538    ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
539    Widget_Data *wd = elm_widget_data_get(obj);
540    if (!wd) return 0;
541    return wd->slide_duration;
542 }
543