1 #include <Elementary.h>
5 * @defgroup Editfield Editfield
8 * This is a editfield. It can contain a simple label and icon objects.
11 //#define ERASER_PADDING (10)
13 typedef struct _Widget_Data Widget_Data;
19 Evas_Object *scroller;
23 const char *guide_text;
24 Eina_Bool needs_size_calc:1;
25 Eina_Bool show_guide_text:1;
27 Eina_Bool single_line:1;
28 Eina_Bool eraser_show:1;
31 static const char *widtype = NULL;
32 static void _del_hook(Evas_Object *obj);
33 static void _theme_hook(Evas_Object *obj);
34 static void _sizing_eval(Evas_Object *obj);
35 static void _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info);
36 static void _on_focus_hook(void *data, Evas_Object *obj);
37 static Eina_Bool _empty_entry(Evas_Object *entry);
39 #ifdef HAVE_CONFORMANT_AUTOSCROLL
40 static const char SIG_CURSOR_CHANGED[] = "cursor,changed";
41 static const char SIG_IMPREGION_CHANGED[] = "impregion,changed";
45 _del_hook(Evas_Object *obj)
47 Widget_Data *wd = elm_widget_data_get(obj);
49 if (wd->label) eina_stringshare_del(wd->label);
53 #ifdef HAVE_CONFORMANT_AUTOSCROLL
55 _imp_region_get_hook(const Evas_Object *obj, Evas_Coord *x, Evas_Coord *y, Evas_Coord *w, Evas_Coord *h)
57 Widget_Data *wd = elm_widget_data_get(obj);
59 elm_widget_imp_region_get(wd->entry, x, y, w, h);
65 _on_focus_hook(void *data, Evas_Object *obj)
67 Widget_Data *wd = elm_widget_data_get(obj);
70 if (!elm_widget_focus_get(obj) && !(elm_widget_disabled_get(obj)) )
72 evas_object_smart_callback_call(obj, "unfocused", NULL);
73 wd->editing = EINA_FALSE;
74 edje_object_signal_emit(wd->base, "elm,state,over,show", "elm");
75 if(_empty_entry(wd->entry))
79 edje_object_part_text_set(wd->base, "elm.guidetext", wd->guide_text);
80 edje_object_signal_emit(wd->base, "elm,state,guidetext,visible", "elm");
81 wd->show_guide_text = EINA_TRUE;
88 _theme_hook(Evas_Object *obj)
90 Widget_Data *wd = elm_widget_data_get(obj);
93 _elm_theme_object_set(obj, wd->base, "editfield", "base", elm_widget_style_get(obj));
95 edje_object_part_swallow(wd->base, "elm.swallow.content", wd->scroller);
97 edje_object_part_swallow(wd->base, "elm.swallow.content", wd->entry);
99 edje_object_signal_emit(wd->base, "elm,state,over,show", "elm");
101 edje_object_signal_emit(wd->base, "elm,state,eraser,show", "elm");
103 edje_object_signal_emit(wd->base, "elm,state,eraser,hidden", "elm");
104 if(wd->show_guide_text)
106 if(_empty_entry(wd->entry))
110 edje_object_part_text_set(wd->base, "elm.guidetext", wd->guide_text);
111 edje_object_signal_emit(wd->base, "elm,state,guidetext,visible", "elm");
116 edje_object_part_swallow(wd->base, "right_icon", wd->ricon);
118 edje_object_part_swallow(wd->base, "left_icon", wd->licon);
123 _changed_hook(Evas_Object *obj)
125 Widget_Data *wd = elm_widget_data_get(obj);
126 if (wd->needs_size_calc)
129 wd->needs_size_calc = EINA_FALSE;
134 _sizing_eval(Evas_Object *obj)
136 Widget_Data *wd = elm_widget_data_get(obj);
137 Evas_Coord minw = -1, minh = -1;
138 edje_object_size_min_calc(wd->base, &minw, &minh);
139 evas_object_size_hint_min_set(obj, minw, minh);
140 evas_object_size_hint_max_set(obj, -1, -1);
144 _request_sizing_eval(Evas_Object *obj)
146 Widget_Data *wd = elm_widget_data_get(obj);
149 if (wd->needs_size_calc)
151 wd->needs_size_calc = EINA_TRUE;
152 evas_object_smart_changed(obj);
156 _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info)
159 evas_object_size_hint_weight_get(data, &weight_x, NULL);
160 if (weight_x == EVAS_HINT_EXPAND)
161 _request_sizing_eval(data);
165 _empty_entry(Evas_Object *entry)
171 text = elm_entry_entry_get(entry);
172 if(!text) return EINA_FALSE;
173 strip_text = elm_entry_markup_to_utf8(text);
175 len = strlen(strip_text);
185 _entry_changed_cb(void *data, Evas_Object *obj, void* event_info)
187 Evas_Object *ef_obj = (Evas_Object *)data;
188 Widget_Data *wd = elm_widget_data_get(ef_obj);
190 if(!wd || !wd->base) return;
192 if(!_empty_entry(wd->entry))
196 edje_object_signal_emit(wd->base, "elm,state,guidetext,hidden", "elm");
197 wd->show_guide_text = EINA_FALSE;
202 #ifdef HAVE_CONFORMANT_AUTOSCROLL
204 _entry_cursor_changed_cb(void *data, Evas_Object *obj, void* event_info)
206 Evas_Object *ef_obj = (Evas_Object *)data;
207 evas_object_smart_callback_call(ef_obj, SIG_IMPREGION_CHANGED, NULL);
212 _signal_mouse_clicked(void *data, Evas_Object *obj, const char *emission, const char *source)
214 Widget_Data *wd = elm_widget_data_get(data);
215 if(!wd || !wd->base) return;
217 if(!strcmp(source, "eraser"))
219 elm_entry_entry_set(wd->entry, "");
220 if(wd->editing == EINA_FALSE) {
223 edje_object_part_text_set(wd->base, "elm.guidetext", wd->guide_text);
224 edje_object_signal_emit(wd->base, "elm,state,guidetext,visible", "elm");
225 wd->show_guide_text = EINA_TRUE;
229 else if(strcmp(source, "left_icon") && strcmp(source, "right_icon") && strcmp(source, "eraser"))
231 edje_object_signal_emit(wd->base, "elm,state,over,hide", "elm");
233 elm_widget_focus_set(wd->scroller, EINA_TRUE);
235 elm_widget_focus_set(wd->entry, EINA_TRUE);
237 if(wd->editing == EINA_FALSE)
238 elm_entry_cursor_end_set(wd->entry);
242 edje_object_signal_emit(wd->base, "elm,state,guidetext,hidden", "elm");
243 wd->show_guide_text = EINA_FALSE;
245 evas_object_smart_callback_call(data, "clicked", NULL);
246 wd->editing = EINA_TRUE;
251 _resize_cb(void *data, Evas *evas, Evas_Object *obj, void *event)
253 Widget_Data *wd = elm_widget_data_get(data);
255 if (!wd || !wd->base) return;
256 evas_object_geometry_get(obj, NULL, NULL, NULL, &h);
260 * Add a new editfield object
262 * @param parent The parent object
263 * @return The new object or NULL if it cannot be created
268 elm_editfield_add(Evas_Object *parent)
274 e = evas_object_evas_get(parent);
277 wd = ELM_NEW(Widget_Data);
278 obj = elm_widget_add(e);
279 ELM_SET_WIDTYPE(widtype, "editfield");
280 elm_widget_type_set(obj, "editfield");
281 elm_widget_sub_object_add(parent, obj);
282 elm_widget_data_set(obj, wd);
283 elm_widget_del_hook_set(obj, _del_hook);
284 elm_widget_theme_hook_set(obj, _theme_hook);
285 elm_widget_changed_hook_set(obj, _changed_hook);
286 elm_widget_on_focus_hook_set( obj, _on_focus_hook, NULL );
287 elm_widget_can_focus_set(obj, EINA_TRUE);
288 #ifdef HAVE_CONFORMANT_AUTOSCROLL
289 elm_widget_imp_region_get_hook_set(obj, _imp_region_get_hook, NULL);
292 wd->base = edje_object_add(e);
293 _elm_theme_object_set(obj, wd->base, "editfield", "base", "default");
294 elm_widget_resize_object_set(obj, wd->base);
295 edje_object_signal_callback_add(wd->base, "mouse,clicked,1", "*",
296 _signal_mouse_clicked, obj);
297 evas_object_event_callback_add(wd->base, EVAS_CALLBACK_RESIZE, _resize_cb, obj);
299 wd->editing = EINA_FALSE;
300 wd->single_line = EINA_FALSE;
301 wd->eraser_show = EINA_FALSE;
303 wd->entry = elm_entry_add(obj);
304 elm_object_style_set(wd->entry, "editfield");
305 evas_object_size_hint_weight_set(wd->entry, 0, EVAS_HINT_EXPAND);
306 evas_object_size_hint_align_set(wd->entry, 0, EVAS_HINT_FILL);
307 evas_object_event_callback_add(wd->entry,
308 EVAS_CALLBACK_CHANGED_SIZE_HINTS,
309 _changed_size_hints, obj);
310 edje_object_part_swallow(wd->base, "elm.swallow.content", wd->entry);
311 evas_object_smart_callback_add(wd->entry, "changed", _entry_changed_cb, obj);
312 elm_widget_sub_object_add(obj, wd->entry);
313 #ifdef HAVE_CONFORMANT_AUTOSCROLL
314 evas_object_smart_callback_add(wd->entry, SIG_CURSOR_CHANGED, _entry_cursor_changed_cb, obj);
323 * Set the label of editfield
325 * @param obj The editfield object
326 * @param label The label text
331 elm_editfield_label_set(Evas_Object *obj, const char *label)
333 Widget_Data *wd = elm_widget_data_get(obj);
334 ELM_CHECK_WIDTYPE(obj, widtype);
335 if (!wd || !wd->base)
338 eina_stringshare_del(wd->label);
341 wd->label = eina_stringshare_add(label);
342 edje_object_signal_emit(wd->base, "elm,state,text,visible", "elm");
343 edje_object_signal_emit(wd->base, "elm,state,left,icon,hide", "elm");
348 edje_object_signal_emit(wd->base, "elm,state,text,hidden", "elm");
349 edje_object_signal_emit(wd->base, "elm,state,left,icon,show", "elm");
351 edje_object_message_signal_process(wd->base);
352 edje_object_part_text_set(wd->base, "elm.text", label);
357 * Get the label used on the editfield object
359 * @param obj The editfield object
365 elm_editfield_label_get(Evas_Object *obj)
367 Widget_Data *wd = elm_widget_data_get(obj);
368 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
369 if (!wd || !wd->base)
375 * Set the guidance text used on the editfield object
377 * @param obj The editfield object
383 elm_editfield_guide_text_set(Evas_Object *obj, const char *text)
385 Widget_Data *wd = elm_widget_data_get(obj);
386 ELM_CHECK_WIDTYPE(obj, widtype);
387 if (!wd || !wd->base)
390 eina_stringshare_del(wd->guide_text);
393 wd->guide_text = eina_stringshare_add(text);
394 edje_object_part_text_set(wd->base, "elm.guidetext", wd->guide_text);
395 wd->show_guide_text = EINA_TRUE;
398 wd->guide_text = NULL;
402 * Get the guidance text used on the editfield object
404 * @param obj The editfield object
410 elm_editfield_guide_text_get(Evas_Object *obj)
412 Widget_Data *wd = elm_widget_data_get(obj);
413 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
414 if (!wd || !wd->base)
416 return wd->guide_text;
420 * Get the entry of the editfield object
422 * @param obj The editfield object
423 * @return entry object
429 elm_editfield_entry_get(Evas_Object *obj)
431 Widget_Data *wd = elm_widget_data_get(obj);
432 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
439 * Set the left side icon.
441 * @param obj The editfield object
442 * @param icon The icon object
447 elm_editfield_left_icon_set(Evas_Object *obj, Evas_Object *icon)
449 Widget_Data *wd = elm_widget_data_get(obj);
450 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
451 if (!wd || !wd->base || !icon)
453 if ((wd->licon != icon) && (wd->licon))
454 elm_widget_sub_object_del(obj, wd->licon);
457 if (!(edje_object_part_swallow(wd->base, "left_icon", icon)))
460 elm_widget_sub_object_add(obj, icon);
461 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
462 _changed_size_hints, obj);
463 edje_object_signal_emit(wd->base, "elm,state,left,icon,show", "elm");
464 edje_object_signal_emit(wd->base, "elm,state,text,hidden", "elm");
471 * Get the left side icon
473 * @param obj The editfield object
474 * @return icon object
479 elm_editfield_left_icon_get(Evas_Object *obj)
481 Widget_Data *wd = elm_widget_data_get(obj);
482 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
483 if (!wd || !wd->base || !wd->licon)
489 * Set the right side icon.
491 * @param obj The editfield object
492 * @param icon The icon object
497 elm_editfield_right_icon_set(Evas_Object *obj, Evas_Object *icon)
499 Widget_Data *wd = elm_widget_data_get(obj);
500 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
501 if (!wd || !wd->base || !icon){
502 printf(" ======= 1 =========\n");
505 if ((wd->ricon != icon) && (wd->ricon))
506 elm_widget_sub_object_del(obj, wd->ricon);
509 if ( !(edje_object_part_swallow(wd->base, "right_icon", icon)) )
511 printf(" ======= 2 =========\n");
515 elm_widget_sub_object_add(obj, icon);
516 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
517 _changed_size_hints, obj);
518 edje_object_signal_emit(wd->base, "elm,state,right,icon,show", "elm");
525 * Get the right side icon
527 * @param obj The editfield object
528 * @return icon object
533 elm_editfield_right_icon_get(Evas_Object *obj)
535 Widget_Data *wd = elm_widget_data_get(obj);
536 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
537 if (!wd || !wd->base || !wd->ricon)
543 * Set entry object style as single-line or multi-line.
545 * @param obj The editfield object
546 * @param single_line 1 if single-line , 0 if multi-line
551 elm_editfield_entry_single_line_set(Evas_Object *obj, Eina_Bool single_line)
553 Widget_Data *wd = elm_widget_data_get(obj);
555 ELM_CHECK_WIDTYPE(obj, widtype);
556 if (!wd || !wd->base || wd->single_line == single_line)
558 wd->single_line = single_line;
559 elm_entry_single_line_set(wd->entry, single_line);
562 wd->scroller = elm_scroller_add(obj);
563 elm_scroller_bounce_set(wd->scroller, 0, 0);
564 elm_scroller_policy_set(wd->scroller, ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_OFF);
565 elm_widget_sub_object_add(obj, wd->scroller);
566 elm_scroller_content_min_limit(wd->scroller, 0, 1);
568 edje_object_part_unswallow(wd->base, wd->entry);
569 edje_object_part_swallow(wd->base, "elm.swallow.content", wd->scroller);
570 elm_scroller_content_set(wd->scroller, wd->entry);
573 entry = elm_entry_add(obj);
574 elm_object_style_set(entry, "editfield");
575 evas_object_size_hint_weight_set(entry, 0, EVAS_HINT_EXPAND);
576 evas_object_size_hint_align_set(entry, 0, EVAS_HINT_FILL);
577 evas_object_event_callback_add(entry,
578 EVAS_CALLBACK_CHANGED_SIZE_HINTS,
579 _changed_size_hints, obj);
580 edje_object_part_swallow(wd->base, "elm.swallow.content", entry);
581 evas_object_smart_callback_add(entry, "changed", _entry_changed_cb, obj);
582 elm_widget_sub_object_add(obj, entry);
583 elm_entry_entry_set(entry, elm_entry_entry_get(wd->entry));
585 edje_object_part_unswallow(wd->base, wd->scroller);
586 evas_object_del(wd->entry);
588 edje_object_part_swallow(wd->base, "elm.swallow.content", wd->entry);
593 * Get the current entry object style(single-line or multi-line)
595 * @param obj The editfield object
596 * @return 1 if single-line , 0 if multi-line
601 elm_editfield_entry_single_line_get(Evas_Object *obj)
603 Widget_Data *wd = elm_widget_data_get(obj);
604 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
605 if (!wd || !wd->base)
607 return wd->single_line;
611 * Set enable user to clean all of text.
613 * @param obj The editfield object
614 * @param visible If true, the eraser is visible and user can clean all of text by using eraser.
615 * If false, the eraser is invisible.
620 elm_editfield_eraser_set(Evas_Object *obj, Eina_Bool visible)
622 Widget_Data *wd = elm_widget_data_get(obj);
623 ELM_CHECK_WIDTYPE(obj, widtype);
624 if (!wd || !wd->base)
627 wd->eraser_show = visible;
630 edje_object_signal_emit(wd->base, "elm,state,eraser,show", "elm");
632 edje_object_signal_emit(wd->base, "elm,state,eraser,hidden", "elm");
638 * Get the current state of erase (visible/invisible)
640 * @param obj The editfield object
641 * @return 1 if visible, 0 if invisible
646 elm_editfield_eraser_get(Evas_Object *obj)
648 Widget_Data *wd = elm_widget_data_get(obj);
649 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
650 if (!wd || !wd->base)
652 return wd->eraser_show;