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);
40 _del_hook(Evas_Object *obj)
42 Widget_Data *wd = elm_widget_data_get(obj);
44 if (wd->label) eina_stringshare_del(wd->label);
49 _on_focus_hook(void *data, Evas_Object *obj)
51 Widget_Data *wd = elm_widget_data_get(obj);
54 if (!elm_widget_focus_get(obj) && !(elm_widget_disabled_get(obj)) )
56 wd->editing = EINA_FALSE;
57 edje_object_signal_emit(wd->base, "elm,state,over,show", "elm");
58 if(_empty_entry(wd->entry))
62 edje_object_part_text_set(wd->base, "elm.guidetext", wd->guide_text);
63 edje_object_signal_emit(wd->base, "elm,state,guidetext,visible", "elm");
64 wd->show_guide_text = EINA_TRUE;
71 _theme_hook(Evas_Object *obj)
73 Widget_Data *wd = elm_widget_data_get(obj);
76 _elm_theme_object_set(obj, wd->base, "editfield", "base", elm_widget_style_get(obj));
78 edje_object_part_swallow(wd->base, "elm.swallow.content", wd->scroller);
80 edje_object_part_swallow(wd->base, "elm.swallow.content", wd->entry);
82 edje_object_signal_emit(wd->base, "elm,state,over,show", "elm");
84 edje_object_signal_emit(wd->base, "elm,state,eraser,show", "elm");
86 edje_object_signal_emit(wd->base, "elm,state,eraser,hidden", "elm");
87 if(wd->show_guide_text)
89 if(_empty_entry(wd->entry))
93 edje_object_part_text_set(wd->base, "elm.guidetext", wd->guide_text);
94 edje_object_signal_emit(wd->base, "elm,state,guidetext,visible", "elm");
99 edje_object_part_swallow(wd->base, "right_icon", wd->ricon);
101 edje_object_part_swallow(wd->base, "left_icon", wd->licon);
106 _changed_hook(Evas_Object *obj)
108 Widget_Data *wd = elm_widget_data_get(obj);
109 if (wd->needs_size_calc)
112 wd->needs_size_calc = EINA_FALSE;
117 _sizing_eval(Evas_Object *obj)
119 Widget_Data *wd = elm_widget_data_get(obj);
120 Evas_Coord minw = -1, minh = -1;
121 edje_object_size_min_calc(wd->base, &minw, &minh);
122 evas_object_size_hint_min_set(obj, minw, minh);
123 evas_object_size_hint_max_set(obj, -1, -1);
127 _request_sizing_eval(Evas_Object *obj)
129 Widget_Data *wd = elm_widget_data_get(obj);
132 if (wd->needs_size_calc)
134 wd->needs_size_calc = EINA_TRUE;
135 evas_object_smart_changed(obj);
139 _changed_size_hints(void *data, Evas *e, Evas_Object *obj, void *event_info)
142 evas_object_size_hint_weight_get(data, &weight_x, NULL);
143 if (weight_x == EVAS_HINT_EXPAND)
144 _request_sizing_eval(data);
148 _empty_entry(Evas_Object *entry)
154 text = elm_entry_entry_get(entry);
155 if(!text) return EINA_FALSE;
156 strip_text = elm_entry_markup_to_utf8(text);
158 len = strlen(strip_text);
168 _entry_changed_cb(void *data, Evas_Object *obj, void* event_info)
170 Evas_Object *ef_obj = (Evas_Object *)data;
171 Widget_Data *wd = elm_widget_data_get(ef_obj);
173 if(!wd || !wd->base) return;
175 if(!_empty_entry(wd->entry))
179 edje_object_signal_emit(wd->base, "elm,state,guidetext,hidden", "elm");
180 wd->show_guide_text = EINA_FALSE;
186 _signal_mouse_clicked(void *data, Evas_Object *obj, const char *emission, const char *source)
188 Widget_Data *wd = elm_widget_data_get(data);
189 if(!wd || !wd->base) return;
191 if(!strcmp(source, "eraser"))
193 elm_entry_entry_set(wd->entry, "");
194 if(wd->editing == EINA_FALSE) {
197 edje_object_part_text_set(wd->base, "elm.guidetext", wd->guide_text);
198 edje_object_signal_emit(wd->base, "elm,state,guidetext,visible", "elm");
199 wd->show_guide_text = EINA_TRUE;
203 else if(strcmp(source, "left_icon") && strcmp(source, "right_icon") && strcmp(source, "eraser"))
205 edje_object_signal_emit(wd->base, "elm,state,over,hide", "elm");
207 elm_widget_focus_set(wd->scroller, EINA_TRUE);
209 elm_widget_focus_set(wd->entry, EINA_TRUE);
211 if(wd->editing == EINA_FALSE)
212 elm_entry_cursor_end_set(wd->entry);
216 edje_object_signal_emit(wd->base, "elm,state,guidetext,hidden", "elm");
217 wd->show_guide_text = EINA_FALSE;
219 evas_object_smart_callback_call(data, "clicked", NULL);
220 wd->editing = EINA_TRUE;
225 _resize_cb(void *data, Evas *evas, Evas_Object *obj, void *event)
227 Widget_Data *wd = elm_widget_data_get(data);
229 if (!wd || !wd->base) return;
230 evas_object_geometry_get(obj, NULL, NULL, NULL, &h);
234 * Add a new editfield object
236 * @param parent The parent object
237 * @return The new object or NULL if it cannot be created
242 elm_editfield_add(Evas_Object *parent)
248 wd = ELM_NEW(Widget_Data);
249 e = evas_object_evas_get(parent);
252 obj = elm_widget_add(e);
253 ELM_SET_WIDTYPE(widtype, "editfield");
254 elm_widget_type_set(obj, "editfield");
255 elm_widget_sub_object_add(parent, obj);
256 elm_widget_data_set(obj, wd);
257 elm_widget_del_hook_set(obj, _del_hook);
258 elm_widget_theme_hook_set(obj, _theme_hook);
259 elm_widget_changed_hook_set(obj, _changed_hook);
260 elm_widget_on_focus_hook_set( obj, _on_focus_hook, NULL );
261 elm_widget_can_focus_set(obj, EINA_TRUE);
263 wd->base = edje_object_add(e);
264 _elm_theme_object_set(obj, wd->base, "editfield", "base", "default");
265 elm_widget_resize_object_set(obj, wd->base);
266 edje_object_signal_callback_add(wd->base, "mouse,clicked,1", "*",
267 _signal_mouse_clicked, obj);
268 evas_object_event_callback_add(wd->base, EVAS_CALLBACK_RESIZE, _resize_cb, obj);
270 wd->editing = EINA_FALSE;
271 wd->single_line = EINA_FALSE;
272 wd->eraser_show = EINA_FALSE;
274 wd->entry = elm_entry_add(obj);
275 elm_object_style_set(wd->entry, "editfield");
276 evas_object_size_hint_weight_set(wd->entry, 0, EVAS_HINT_EXPAND);
277 evas_object_size_hint_align_set(wd->entry, 0, EVAS_HINT_FILL);
278 evas_object_event_callback_add(wd->entry,
279 EVAS_CALLBACK_CHANGED_SIZE_HINTS,
280 _changed_size_hints, obj);
281 edje_object_part_swallow(wd->base, "elm.swallow.content", wd->entry);
282 evas_object_smart_callback_add(wd->entry, "changed", _entry_changed_cb, obj);
283 elm_widget_sub_object_add(obj, wd->entry);
291 * Set the label of editfield
293 * @param obj The editfield object
294 * @param label The label text
299 elm_editfield_label_set(Evas_Object *obj, const char *label)
301 Widget_Data *wd = elm_widget_data_get(obj);
302 ELM_CHECK_WIDTYPE(obj, widtype);
303 if (!wd || !wd->base)
306 eina_stringshare_del(wd->label);
309 wd->label = eina_stringshare_add(label);
310 edje_object_signal_emit(wd->base, "elm,state,text,visible", "elm");
311 edje_object_signal_emit(wd->base, "elm,state,left,icon,hide", "elm");
316 edje_object_signal_emit(wd->base, "elm,state,text,hidden", "elm");
317 edje_object_signal_emit(wd->base, "elm,state,left,icon,show", "elm");
319 edje_object_message_signal_process(wd->base);
320 edje_object_part_text_set(wd->base, "elm.text", label);
325 * Get the label used on the editfield object
327 * @param obj The editfield object
333 elm_editfield_label_get(Evas_Object *obj)
335 Widget_Data *wd = elm_widget_data_get(obj);
336 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
337 if (!wd || !wd->base)
343 * Get the label used on the editfield object
345 * @param obj The editfield object
351 elm_editfield_guide_text_set(Evas_Object *obj, const char *text)
353 Widget_Data *wd = elm_widget_data_get(obj);
354 ELM_CHECK_WIDTYPE(obj, widtype);
355 if (!wd || !wd->base)
358 eina_stringshare_del(wd->guide_text);
361 wd->guide_text = eina_stringshare_add(text);
362 edje_object_part_text_set(wd->base, "elm.guidetext", wd->guide_text);
363 wd->show_guide_text = EINA_TRUE;
366 wd->guide_text = NULL;
370 * Get the guidance text used on the editfield object
372 * @param obj The editfield object
378 elm_editfield_guide_text_get(Evas_Object *obj)
380 Widget_Data *wd = elm_widget_data_get(obj);
381 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
382 if (!wd || !wd->base)
384 return wd->guide_text;
388 * Get the entry of the editfield object
390 * @param obj The editfield object
391 * @return entry object
397 elm_editfield_entry_get(Evas_Object *obj)
399 Widget_Data *wd = elm_widget_data_get(obj);
400 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
407 * Set the left side icon.
409 * @param obj The editfield object
410 * @param icon The icon object
411 * @return 1 if setting is done, 0 if there is no swallow part for the icon.
416 elm_editfield_left_icon_set(Evas_Object *obj, Evas_Object *icon)
418 Widget_Data *wd = elm_widget_data_get(obj);
419 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
420 if (!wd || !wd->base)
422 if ((wd->licon != icon) && (wd->licon))
423 elm_widget_sub_object_del(obj, wd->licon);
426 if (!(edje_object_part_swallow(wd->base, "left_icon", icon)))
429 elm_widget_sub_object_add(obj, icon);
430 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
431 _changed_size_hints, obj);
432 edje_object_signal_emit(wd->base, "elm,state,left,icon,show", "elm");
433 edje_object_signal_emit(wd->base, "elm,state,text,hidden", "elm");
440 * Get the left side icon
442 * @param obj The editfield object
443 * @return icon object
448 elm_editfield_left_icon_get(Evas_Object *obj)
450 Widget_Data *wd = elm_widget_data_get(obj);
451 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
452 if (!wd || !wd->base || !wd->licon)
458 * Set the right side icon.
460 * @param obj The editfield object
461 * @param icon The icon object
462 * @return 1 if setting is done, 0 if there is no swallow part for the icon.
467 elm_editfield_right_icon_set(Evas_Object *obj, Evas_Object *icon)
469 Widget_Data *wd = elm_widget_data_get(obj);
470 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
471 if (!wd || !wd->base)
473 if ((wd->ricon != icon) && (wd->ricon))
474 elm_widget_sub_object_del(obj, wd->ricon);
477 if ( !(edje_object_part_swallow(wd->base, "right_icon", icon)) )
480 elm_widget_sub_object_add(obj, icon);
481 evas_object_event_callback_add(icon, EVAS_CALLBACK_CHANGED_SIZE_HINTS,
482 _changed_size_hints, obj);
483 edje_object_signal_emit(wd->base, "elm,state,right,icon,show", "elm");
490 * Get the right side icon
492 * @param obj The editfield object
493 * @return icon object
498 elm_editfield_right_icon_get(Evas_Object *obj)
500 Widget_Data *wd = elm_widget_data_get(obj);
501 ELM_CHECK_WIDTYPE(obj, widtype) NULL;
502 if (!wd || !wd->base || !wd->ricon)
508 * Set entry object style as single-line or multi-line.
510 * @param obj The editfield object
511 * @param single_line 1 if single-line , 0 if multi-line
516 elm_editfield_entry_single_line_set(Evas_Object *obj, Eina_Bool single_line)
518 Widget_Data *wd = elm_widget_data_get(obj);
520 ELM_CHECK_WIDTYPE(obj, widtype);
521 if (!wd || !wd->base || wd->single_line == single_line)
523 wd->single_line = single_line;
524 elm_entry_single_line_set(wd->entry, single_line);
527 wd->scroller = elm_scroller_add(obj);
528 elm_scroller_bounce_set(wd->scroller, 0, 0);
529 elm_scroller_policy_set(wd->scroller, ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_OFF);
530 elm_widget_sub_object_add(obj, wd->scroller);
531 elm_scroller_content_min_limit(wd->scroller, 0, 1);
533 edje_object_part_unswallow(wd->base, wd->entry);
534 edje_object_part_swallow(wd->base, "elm.swallow.content", wd->scroller);
535 elm_scroller_content_set(wd->scroller, wd->entry);
538 entry = elm_entry_add(obj);
539 elm_object_style_set(entry, "editfield");
540 evas_object_size_hint_weight_set(entry, 0, EVAS_HINT_EXPAND);
541 evas_object_size_hint_align_set(entry, 0, EVAS_HINT_FILL);
542 evas_object_event_callback_add(entry,
543 EVAS_CALLBACK_CHANGED_SIZE_HINTS,
544 _changed_size_hints, obj);
545 edje_object_part_swallow(wd->base, "elm.swallow.content", entry);
546 evas_object_smart_callback_add(entry, "changed", _entry_changed_cb, obj);
547 elm_widget_sub_object_add(obj, entry);
548 elm_entry_entry_set(entry, elm_entry_entry_get(wd->entry));
550 edje_object_part_unswallow(wd->base, wd->scroller);
551 evas_object_del(wd->entry);
553 edje_object_part_swallow(wd->base, "elm.swallow.content", wd->entry);
558 * Get the current entry object style(single-line or multi-line)
560 * @param obj The editfield object
561 * @return 1 if single-line , 0 if multi-line
566 elm_editfield_entry_single_line_get(Evas_Object *obj)
568 Widget_Data *wd = elm_widget_data_get(obj);
569 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
570 if (!wd || !wd->base)
572 return wd->single_line;
576 * Set enable user to clean all of text.
578 * @param obj The editfield object
579 * @param visible If true, the eraser is visible and user can clean all of text by using eraser.
580 * If false, the eraser is invisible.
585 elm_editfield_eraser_set(Evas_Object *obj, Eina_Bool visible)
587 Widget_Data *wd = elm_widget_data_get(obj);
588 ELM_CHECK_WIDTYPE(obj, widtype);
589 if (!wd || !wd->base)
592 wd->eraser_show = visible;
595 edje_object_signal_emit(wd->base, "elm,state,eraser,show", "elm");
597 edje_object_signal_emit(wd->base, "elm,state,eraser,hidden", "elm");
603 * Get the current state of erase (visible/invisible)
605 * @param obj The editfield object
606 * @return 1 if visible, 0 if invisible
611 elm_editfield_eraser_get(Evas_Object *obj)
613 Widget_Data *wd = elm_widget_data_get(obj);
614 ELM_CHECK_WIDTYPE(obj, widtype) EINA_FALSE;
615 if (!wd || !wd->base)
617 return wd->eraser_show;