1 #include <Elementary.h>
4 typedef struct _Widget_Data Widget_Data;
11 Ecore_Job *deferred_recalc_job;
12 double slide_duration;
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;
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);
33 _elm_recalc_job(void *data)
35 Widget_Data *wd = elm_widget_data_get(data);
36 Evas_Coord minw = -1, minh = -1;
39 evas_event_freeze(evas_object_evas_get(data));
40 wd->deferred_recalc_job = NULL;
41 evas_object_geometry_get(wd->lbl, NULL, NULL, &resw, NULL);
42 if (wd->wrap_w > resw)
45 edje_object_size_min_restricted_calc(wd->lbl, &minw, &minh, resw, 0);
46 /* This is a hack to workaround the way min size hints are treated.
47 * If the minimum width is smaller than the restricted width, it means
48 * the mininmum doesn't matter. */
49 if ((minw <= resw) && (minw != wd->wrap_w))
51 Evas_Coord ominw = -1;
52 evas_object_size_hint_min_get(data, &ominw, NULL);
55 evas_object_size_hint_min_set(data, minw, minh);
56 evas_event_thaw(evas_object_evas_get(data));
57 evas_event_thaw_eval(evas_object_evas_get(data));
61 _del_hook(Evas_Object *obj)
63 Widget_Data *wd = elm_widget_data_get(obj);
65 evas_event_freeze(evas_object_evas_get(obj));
66 if (wd->deferred_recalc_job) ecore_job_del(wd->deferred_recalc_job);
67 if (wd->label) eina_stringshare_del(wd->label);
69 evas_event_thaw(evas_object_evas_get(obj));
70 evas_event_thaw_eval(evas_object_evas_get(obj));
74 _theme_change(Evas_Object *obj)
76 Widget_Data *wd = elm_widget_data_get(obj);
79 _elm_theme_object_set(obj, wd->lbl, "label", "base",
80 elm_widget_style_get(obj));
84 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
86 Widget_Data *wd = elm_widget_data_get(obj);
88 edje_object_mirrored_set(wd->lbl, rtl);
92 _theme_hook(Evas_Object *obj)
94 Widget_Data *wd = elm_widget_data_get(obj);
96 evas_event_freeze(evas_object_evas_get(obj));
97 _elm_widget_mirrored_reload(obj);
98 _mirrored_set(obj, elm_widget_mirrored_get(obj));
100 edje_object_part_text_style_user_set(wd->lbl, "elm.text", wd->format);
101 edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
102 edje_object_scale_set(wd->lbl, elm_widget_scale_get(obj) *
104 _label_sliding_change(obj);
106 evas_event_thaw(evas_object_evas_get(obj));
107 evas_event_thaw_eval(evas_object_evas_get(obj));
111 _sizing_eval(Evas_Object *obj)
113 Widget_Data *wd = elm_widget_data_get(obj);
114 Evas_Coord minw = -1, minh = -1;
115 Evas_Coord resw, resh;
120 evas_object_geometry_get(wd->lbl, NULL, NULL, &resw, &resh);
121 if ((resw == wd->lastw) && (!wd->changed)) return;
122 wd->changed = EINA_FALSE;
124 _elm_recalc_job(obj);
125 // FIXME: works ok. but NOT for genlist. what should genlist do?
126 // if (wd->deferred_recalc_job) ecore_job_del(wd->deferred_recalc_job);
127 // wd->deferred_recalc_job = ecore_job_add(_elm_recalc_job, obj);
131 evas_event_freeze(evas_object_evas_get(obj));
132 evas_object_geometry_get(wd->lbl, NULL, NULL, &resw, &resh);
133 edje_object_size_min_calc(wd->lbl, &minw, &minh);
134 if (wd->wrap_w > 0 && minw > wd->wrap_w) minw = wd->wrap_w;
135 evas_object_size_hint_min_set(obj, minw, minh);
136 evas_event_thaw(evas_object_evas_get(obj));
137 evas_event_thaw_eval(evas_object_evas_get(obj));
142 _lbl_resize(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
144 Widget_Data *wd = elm_widget_data_get(data);
146 if (wd->linewrap) _sizing_eval(data);
150 _get_value_in_key_string(const char *oldstring, const char *key, char **value)
152 char *curlocater, *endtag;
153 int firstindex = 0, foundflag = -1;
155 curlocater = strstr(oldstring, key);
158 int key_len = strlen(key);
159 endtag = curlocater + key_len;
160 if ((!endtag) || (*endtag != '='))
165 firstindex = abs(oldstring - curlocater);
166 firstindex += key_len + 1; // strlen("key") + strlen("=")
167 *value = (char *)oldstring + firstindex;
176 if (foundflag == 1) return 0;
183 _strbuf_key_value_replace(Eina_Strbuf *srcbuf, const char *key, const char *value, int deleteflag)
186 const char *srcstring = NULL;
188 srcstring = eina_strbuf_string_get(srcbuf);
190 if (_get_value_in_key_string(srcstring, key, &kvalue) == 0)
194 int key_start_idx = 0;
195 val_end = strchr(kvalue, ' ');
198 val_end_idx = val_end - srcstring;
200 val_end_idx = kvalue - srcstring + strlen(kvalue) - 1;
202 /* -1 is because of the '=' */
203 key_start_idx = kvalue - srcstring - 1 - strlen(key);
204 eina_strbuf_remove(srcbuf, key_start_idx, val_end_idx);
207 eina_strbuf_insert_printf(srcbuf, "%s=%s", key_start_idx, key,
211 else if (!deleteflag)
215 /* -1 because we want it before the ' */
216 eina_strbuf_insert_printf(srcbuf, " %s=%s",
217 eina_strbuf_length_get(srcbuf) - 1, key, value);
221 eina_strbuf_append_printf(srcbuf, "DEFAULT='%s=%s'", key, value);
228 _stringshare_key_value_replace(const char **srcstring, const char *key, const char *value, int deleteflag)
230 Eina_Strbuf *sharebuf = NULL;
232 sharebuf = eina_strbuf_new();
233 eina_strbuf_append(sharebuf, *srcstring);
234 _strbuf_key_value_replace(sharebuf, key, value, deleteflag);
235 eina_stringshare_del(*srcstring);
236 *srcstring = eina_stringshare_add(eina_strbuf_string_get(sharebuf));
237 eina_strbuf_free(sharebuf);
243 _label_sliding_change(Evas_Object *obj)
245 Widget_Data *wd = elm_widget_data_get(obj);
250 // dosen't support multiline sliding effect
253 wd->slidingmode = EINA_FALSE;
257 plaintxt = _elm_util_mkup_to_text(edje_object_part_text_get(wd->lbl, "elm.text"));
258 if (plaintxt != NULL)
260 plainlen = strlen(plaintxt);
263 // too short to slide label
266 wd->slidingmode = EINA_TRUE;
272 Edje_Message_Float_Set *msg = alloca(sizeof(Edje_Message_Float_Set) + (sizeof(double)));
276 wd->slidingellipsis = EINA_TRUE;
277 elm_label_ellipsis_set(obj, EINA_FALSE);
281 msg->val[0] = wd->slide_duration;
283 edje_object_message_send(wd->lbl, EDJE_MESSAGE_FLOAT_SET, 0, msg);
284 edje_object_signal_emit(wd->lbl, "elm,state,slide,start", "elm");
288 edje_object_signal_emit(wd->lbl, "elm,state,slide,stop", "elm");
289 if (wd->slidingellipsis)
291 wd->slidingellipsis = EINA_FALSE;
292 elm_label_ellipsis_set(obj, EINA_TRUE);
298 _elm_label_label_set(Evas_Object *obj, const char *item, const char *label)
300 ELM_CHECK_WIDTYPE(obj, widtype);
301 Widget_Data *wd = elm_widget_data_get(obj);
303 if (item && strcmp(item, "default")) return;
304 if (!label) label = "";
305 eina_stringshare_replace(&wd->label, label);
306 edje_object_part_text_style_user_set(wd->lbl, "elm.text",
308 edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
314 _elm_label_label_get(const Evas_Object *obj, const char *item)
316 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
317 Widget_Data *wd = elm_widget_data_get(obj);
318 if (item && strcmp(item, "default")) return NULL;
319 if (!wd) return NULL;
324 _translate_hook(Evas_Object *obj)
326 evas_object_smart_callback_call(obj, "language,changed", NULL);
330 elm_label_add(Evas_Object *parent)
336 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
338 ELM_SET_WIDTYPE(widtype, "label");
339 elm_widget_type_set(obj, "label");
340 elm_widget_sub_object_add(parent, obj);
341 elm_widget_data_set(obj, wd);
342 elm_widget_del_hook_set(obj, _del_hook);
343 elm_widget_theme_hook_set(obj, _theme_hook);
344 elm_widget_can_focus_set(obj, EINA_FALSE);
345 elm_widget_text_set_hook_set(obj, _elm_label_label_set);
346 elm_widget_text_get_hook_set(obj, _elm_label_label_get);
347 elm_widget_translate_hook_set(obj, _translate_hook);
349 wd->linewrap = ELM_WRAP_NONE;
350 wd->ellipsis = EINA_FALSE;
351 wd->slidingmode = EINA_FALSE;
352 wd->slidingellipsis = EINA_FALSE;
354 wd->slide_duration = 10;
356 wd->lbl = edje_object_add(e);
357 _elm_theme_object_set(obj, wd->lbl, "label", "base", "default");
358 wd->format = eina_stringshare_add("");
359 wd->label = eina_stringshare_add("<br>");
360 edje_object_part_text_style_user_set(wd->lbl, "elm.text",
362 edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
364 elm_widget_resize_object_set(obj, wd->lbl);
366 evas_object_event_callback_add(wd->lbl, EVAS_CALLBACK_RESIZE, _lbl_resize, obj);
368 _mirrored_set(obj, elm_widget_mirrored_get(obj));
375 elm_label_label_set(Evas_Object *obj, const char *label)
377 _elm_label_label_set(obj, NULL, label);
381 elm_label_label_get(const Evas_Object *obj)
383 return _elm_label_label_get(obj, NULL);
387 elm_label_line_wrap_set(Evas_Object *obj, Elm_Wrap_Type wrap)
389 ELM_CHECK_WIDTYPE(obj, widtype);
390 Widget_Data *wd = elm_widget_data_get(obj);
391 const char *wrap_str;
395 if (wd->linewrap == wrap) return;
397 len = strlen(wd->label);
398 if (len <= 0) return;
416 if (_stringshare_key_value_replace(&wd->format,
417 "wrap", wrap_str, 0) == 0)
419 edje_object_part_text_style_user_set(wd->lbl, "elm.text",
421 edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
428 elm_label_line_wrap_get(const Evas_Object *obj)
430 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
431 Widget_Data *wd = elm_widget_data_get(obj);
432 if (!wd) return EINA_FALSE;
437 elm_label_wrap_width_set(Evas_Object *obj, Evas_Coord w)
439 ELM_CHECK_WIDTYPE(obj, widtype);
440 Widget_Data *wd = elm_widget_data_get(obj);
443 if (wd->wrap_w == w) return;
446 edje_object_part_text_style_user_set(wd->lbl, "elm.text",
448 edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
455 elm_label_wrap_width_get(const Evas_Object *obj)
457 ELM_CHECK_WIDTYPE(obj, widtype) 0;
458 Widget_Data *wd = elm_widget_data_get(obj);
463 EINA_DEPRECATED EAPI void
464 elm_label_wrap_height_set(Evas_Object *obj __UNUSED__,
465 Evas_Coord h __UNUSED__)
470 EINA_DEPRECATED EAPI Evas_Coord
471 elm_label_wrap_height_get(const Evas_Object *obj __UNUSED__)
476 EINA_DEPRECATED EAPI void
477 elm_label_fontsize_set(Evas_Object *obj __UNUSED__,
478 int fontsize __UNUSED__)
483 EINA_DEPRECATED EAPI void
484 elm_label_text_align_set(Evas_Object *obj __UNUSED__,
485 const char *alignmode __UNUSED__)
490 EINA_DEPRECATED EAPI void
491 elm_label_text_color_set(Evas_Object *obj __UNUSED__,
492 unsigned int r __UNUSED__,
493 unsigned int g __UNUSED__,
494 unsigned int b __UNUSED__,
495 unsigned int a __UNUSED__)
500 EINA_DEPRECATED EAPI void
501 elm_label_background_color_set(Evas_Object *obj __UNUSED__,
502 unsigned int r __UNUSED__,
503 unsigned int g __UNUSED__,
504 unsigned int b __UNUSED__,
505 unsigned int a __UNUSED__)
511 elm_label_ellipsis_set(Evas_Object *obj, Eina_Bool ellipsis)
513 ELM_CHECK_WIDTYPE(obj, widtype);
514 Widget_Data *wd = elm_widget_data_get(obj);
515 Eina_Strbuf *fontbuf = NULL;
516 int len, removeflag = 0;
519 if (wd->ellipsis == ellipsis) return;
520 wd->ellipsis = ellipsis;
521 len = strlen(wd->label);
522 if (len <= 0) return;
524 if (ellipsis == EINA_FALSE) removeflag = 1; // remove fontsize tag
526 fontbuf = eina_strbuf_new();
527 eina_strbuf_append_printf(fontbuf, "%f", 1.0);
529 if (_stringshare_key_value_replace(&wd->format,
530 "ellipsis", eina_strbuf_string_get(fontbuf), removeflag) == 0)
532 edje_object_part_text_style_user_set(wd->lbl, "elm.text",
534 edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
538 eina_strbuf_free(fontbuf);
543 elm_label_ellipsis_get(const Evas_Object *obj)
545 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
546 Widget_Data *wd = elm_widget_data_get(obj);
547 if (!wd) return EINA_FALSE;
552 elm_label_slide_set(Evas_Object *obj,
555 ELM_CHECK_WIDTYPE(obj, widtype);
556 Widget_Data *wd = elm_widget_data_get(obj);
559 if (wd->slidingmode == slide) return;
560 wd->slidingmode = slide;
561 _label_sliding_change(obj);
567 elm_label_slide_get(const Evas_Object *obj)
569 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
570 Widget_Data *wd = elm_widget_data_get(obj);
571 if (!wd) return EINA_FALSE;
572 return wd->slidingmode;
576 elm_label_slide_duration_set(Evas_Object *obj, double duration)
578 ELM_CHECK_WIDTYPE(obj, widtype);
579 Widget_Data *wd = elm_widget_data_get(obj);
580 Edje_Message_Float_Set *msg = alloca(sizeof(Edje_Message_Float_Set) + (sizeof(double)));
583 wd->slide_duration = duration;
585 msg->val[0] = wd->slide_duration;
586 edje_object_message_send(wd->lbl, EDJE_MESSAGE_FLOAT_SET, 0, msg);
590 elm_label_slide_duration_get(const Evas_Object *obj)
592 ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
593 Widget_Data *wd = elm_widget_data_get(obj);
595 return wd->slide_duration;