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);
31 static void _label_format_set(Evas_Object *obj, const char *format);
34 _elm_recalc_job(void *data)
36 Widget_Data *wd = elm_widget_data_get(data);
37 Evas_Coord minw = -1, minh = -1;
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)
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))
52 Evas_Coord ominw = -1;
53 evas_object_size_hint_min_get(data, &ominw, NULL);
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));
62 _del_hook(Evas_Object *obj)
64 Widget_Data *wd = elm_widget_data_get(obj);
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);
70 evas_event_thaw(evas_object_evas_get(obj));
71 evas_event_thaw_eval(evas_object_evas_get(obj));
75 _theme_change(Evas_Object *obj)
77 Widget_Data *wd = elm_widget_data_get(obj);
80 _elm_theme_object_set(obj, wd->lbl, "label", "base",
81 elm_widget_style_get(obj));
85 _mirrored_set(Evas_Object *obj, Eina_Bool rtl)
87 Widget_Data *wd = elm_widget_data_get(obj);
89 edje_object_mirrored_set(wd->lbl, rtl);
93 _theme_hook(Evas_Object *obj)
95 Widget_Data *wd = elm_widget_data_get(obj);
97 evas_event_freeze(evas_object_evas_get(obj));
98 _elm_widget_mirrored_reload(obj);
99 _mirrored_set(obj, elm_widget_mirrored_get(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) *
105 _label_sliding_change(obj);
107 evas_event_thaw(evas_object_evas_get(obj));
108 evas_event_thaw_eval(evas_object_evas_get(obj));
112 _sizing_eval(Evas_Object *obj)
114 Widget_Data *wd = elm_widget_data_get(obj);
115 Evas_Coord minw = -1, minh = -1;
116 Evas_Coord resw, resh;
121 evas_object_geometry_get(wd->lbl, NULL, NULL, &resw, &resh);
122 if ((resw == wd->lastw) && (!wd->changed)) return;
123 wd->changed = EINA_FALSE;
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);
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));
143 _lbl_resize(void *data, Evas *e __UNUSED__, Evas_Object *obj __UNUSED__, void *event_info __UNUSED__)
145 Widget_Data *wd = elm_widget_data_get(data);
147 if (wd->linewrap) _sizing_eval(data);
151 _label_format_set(Evas_Object *obj, const char *format)
154 edje_object_part_text_style_user_push(obj, "elm.text", format);
156 edje_object_part_text_style_user_pop(obj, "elm.text");
160 _get_value_in_key_string(const char *oldstring, const char *key, char **value)
162 char *curlocater, *endtag;
163 int firstindex = 0, foundflag = -1;
165 curlocater = strstr(oldstring, key);
168 int key_len = strlen(key);
169 endtag = curlocater + key_len;
170 if ((!endtag) || (*endtag != '='))
175 firstindex = abs(oldstring - curlocater);
176 firstindex += key_len + 1; // strlen("key") + strlen("=")
177 *value = (char *)oldstring + firstindex;
186 if (foundflag == 1) return 0;
193 _strbuf_key_value_replace(Eina_Strbuf *srcbuf, const char *key, const char *value, int deleteflag)
196 const char *srcstring = NULL;
198 srcstring = eina_strbuf_string_get(srcbuf);
200 if (_get_value_in_key_string(srcstring, key, &kvalue) == 0)
204 int key_start_idx = 0;
205 val_end = strchr(kvalue, ' ');
208 val_end_idx = val_end - srcstring;
210 val_end_idx = kvalue - srcstring + strlen(kvalue) - 1;
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);
217 eina_strbuf_insert_printf(srcbuf, "%s=%s", key_start_idx, key,
221 else if (!deleteflag)
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);
231 eina_strbuf_append_printf(srcbuf, "DEFAULT='%s=%s'", key, value);
238 _stringshare_key_value_replace(const char **srcstring, const char *key, const char *value, int deleteflag)
240 Eina_Strbuf *sharebuf = NULL;
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);
253 _label_sliding_change(Evas_Object *obj)
255 Widget_Data *wd = elm_widget_data_get(obj);
260 // dosen't support multiline sliding effect
263 wd->slidingmode = EINA_FALSE;
267 plaintxt = _elm_util_mkup_to_text(edje_object_part_text_get(wd->lbl, "elm.text"));
268 if (plaintxt != NULL)
270 plainlen = strlen(plaintxt);
273 // too short to slide label
276 wd->slidingmode = EINA_TRUE;
282 Edje_Message_Float_Set *msg = alloca(sizeof(Edje_Message_Float_Set) + (sizeof(double)));
286 wd->slidingellipsis = EINA_TRUE;
287 elm_label_ellipsis_set(obj, EINA_FALSE);
291 msg->val[0] = wd->slide_duration;
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");
298 edje_object_signal_emit(wd->lbl, "elm,state,slide,stop", "elm");
299 if (wd->slidingellipsis)
301 wd->slidingellipsis = EINA_FALSE;
302 elm_label_ellipsis_set(obj, EINA_TRUE);
308 _elm_label_label_set(Evas_Object *obj, const char *item, const char *label)
310 ELM_CHECK_WIDTYPE(obj, widtype);
311 Widget_Data *wd = elm_widget_data_get(obj);
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);
323 _elm_label_label_get(const Evas_Object *obj, const char *item)
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;
333 _translate_hook(Evas_Object *obj)
335 evas_object_smart_callback_call(obj, "language,changed", NULL);
339 elm_label_add(Evas_Object *parent)
345 ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
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);
358 wd->linewrap = ELM_WRAP_NONE;
359 wd->ellipsis = EINA_FALSE;
360 wd->slidingmode = EINA_FALSE;
361 wd->slidingellipsis = EINA_FALSE;
363 wd->slide_duration = 10;
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);
372 elm_widget_resize_object_set(obj, wd->lbl);
374 evas_object_event_callback_add(wd->lbl, EVAS_CALLBACK_RESIZE, _lbl_resize, obj);
376 _mirrored_set(obj, elm_widget_mirrored_get(obj));
383 elm_label_line_wrap_set(Evas_Object *obj, Elm_Wrap_Type wrap)
385 ELM_CHECK_WIDTYPE(obj, widtype);
386 Widget_Data *wd = elm_widget_data_get(obj);
387 const char *wrap_str;
391 if (wd->linewrap == wrap) return;
393 len = strlen(wd->label);
394 if (len <= 0) return;
412 if (_stringshare_key_value_replace(&wd->format,
413 "wrap", wrap_str, 0) == 0)
415 _label_format_set(wd->lbl, wd->format);
416 edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
423 elm_label_line_wrap_get(const Evas_Object *obj)
425 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
426 Widget_Data *wd = elm_widget_data_get(obj);
427 if (!wd) return EINA_FALSE;
432 elm_label_wrap_width_set(Evas_Object *obj, Evas_Coord w)
434 ELM_CHECK_WIDTYPE(obj, widtype);
435 Widget_Data *wd = elm_widget_data_get(obj);
438 if (wd->wrap_w == w) return;
441 _label_format_set(wd->lbl, wd->format);
442 edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
449 elm_label_wrap_width_get(const Evas_Object *obj)
451 ELM_CHECK_WIDTYPE(obj, widtype) 0;
452 Widget_Data *wd = elm_widget_data_get(obj);
458 elm_label_ellipsis_set(Evas_Object *obj, Eina_Bool ellipsis)
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;
466 if (wd->ellipsis == ellipsis) return;
467 wd->ellipsis = ellipsis;
468 len = strlen(wd->label);
469 if (len <= 0) return;
471 if (ellipsis == EINA_FALSE) removeflag = 1; // remove fontsize tag
473 fontbuf = eina_strbuf_new();
474 eina_strbuf_append_printf(fontbuf, "%f", 1.0);
476 if (_stringshare_key_value_replace(&wd->format,
477 "ellipsis", eina_strbuf_string_get(fontbuf), removeflag) == 0)
479 _label_format_set(wd->lbl, wd->format);
480 edje_object_part_text_set(wd->lbl, "elm.text", wd->label);
484 eina_strbuf_free(fontbuf);
489 elm_label_ellipsis_get(const Evas_Object *obj)
491 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
492 Widget_Data *wd = elm_widget_data_get(obj);
493 if (!wd) return EINA_FALSE;
498 elm_label_slide_set(Evas_Object *obj,
501 ELM_CHECK_WIDTYPE(obj, widtype);
502 Widget_Data *wd = elm_widget_data_get(obj);
505 if (wd->slidingmode == slide) return;
506 wd->slidingmode = slide;
507 _label_sliding_change(obj);
513 elm_label_slide_get(const Evas_Object *obj)
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;
522 elm_label_slide_duration_set(Evas_Object *obj, double duration)
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)));
529 wd->slide_duration = duration;
531 msg->val[0] = wd->slide_duration;
532 edje_object_message_send(wd->lbl, EDJE_MESSAGE_FLOAT_SET, 0, msg);
536 elm_label_slide_duration_get(const Evas_Object *obj)
538 ELM_CHECK_WIDTYPE(obj, widtype) 0.0;
539 Widget_Data *wd = elm_widget_data_get(obj);
541 return wd->slide_duration;