[searchbar] change edit widget from entry to editfield
[framework/uifw/elementary.git] / src / lib / elm_searchbar.c
1 /*
2  * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
3  */
4 #include <Elementary.h>
5 #include "elm_priv.h"
6
7 /**
8  * @defgroup Searchbar Searchbar
9  * @ingroup Elementary
10  *
11  * This is Searchbar. 
12  * It can contain a simple entry and button object.
13  */
14
15 typedef struct _Widget_Data Widget_Data;
16
17 struct _Widget_Data
18 {
19    Evas_Object *base, *eb, *cancel_btn;
20    Eina_Bool cancel_btn_ani_flag;
21 };
22
23 static void _del_hook(Evas_Object *obj);
24 static void _theme_hook(Evas_Object *obj);
25 static void _sizing_eval(Evas_Object *obj);
26 static void _clicked(void *data, Evas_Object *obj, const char *emission, const char *source);
27 static void _changed(void *data, Evas_Object *obj, const char *emission, const char *source);
28 static void _cancel_clicked(void *data, Evas_Object *obj, const char *emission, const char *source);
29 static void _signal_reset_clicked(void *data, Evas_Object *obj, const char *emission, const char *source);
30
31 static void _del_hook(Evas_Object *obj)
32 {
33    Widget_Data *wd = elm_widget_data_get(obj);
34
35    if (!wd) return;
36
37    free(wd);
38 }
39
40 static void _theme_hook(Evas_Object *obj)
41 {
42    Widget_Data *wd = elm_widget_data_get(obj);
43    if (!wd) return;
44
45    _elm_theme_object_set(obj, wd->base, "searchbar", "base", elm_widget_style_get(obj));
46
47    if (wd->eb)
48      edje_object_part_swallow(wd->base, "btn_text", wd->eb);
49    if (wd->cancel_btn)
50      edje_object_part_swallow(wd->base, "button_cancel", wd->cancel_btn);
51
52    edje_object_signal_callback_add(wd->base, "elm,action,click", "", _signal_reset_clicked, obj);
53
54    edje_object_scale_set(wd->cancel_btn, elm_widget_scale_get(obj) * _elm_config->scale);
55    _sizing_eval(obj);
56 }
57
58 static void _sizing_eval(Evas_Object *obj)
59 {
60    Widget_Data *wd = elm_widget_data_get(obj);
61    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
62
63    if (!wd) return;
64    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
65    edje_object_size_min_restricted_calc(wd->base, &minw, &minh, minw, minh);
66    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
67    evas_object_size_hint_min_set(obj, minw, minh);
68    evas_object_size_hint_max_set(obj, maxw, maxh);
69 }
70
71 static void _clicked(void *data, Evas_Object *obj, const char *emission, const char *source)
72 {
73    Widget_Data *wd = elm_widget_data_get(data);
74    if (!wd) return;
75
76    elm_entry_cursor_end_set(elm_editfield_entry_get(wd->eb));
77    if (wd->cancel_btn_ani_flag == EINA_TRUE)
78      edje_object_signal_emit(wd->base, "CANCELIN", "PROG");
79    else
80      edje_object_signal_emit(wd->base, "CANCELSHOW", "PROG");
81
82    evas_object_smart_callback_call(data, "clicked", NULL);
83 }
84
85 static void _changed(void *data, Evas_Object *obj, const char *emission, const char *source)
86 {
87    Widget_Data *wd = elm_widget_data_get(data);
88    if (!wd) return;
89
90    int len = 0;
91    const char* text = elm_entry_entry_get(elm_editfield_entry_get(wd->eb));
92
93 /*
94    if (text != NULL)
95      {
96         len = strlen(text);
97         if (len == 0) 
98           {
99              edje_object_signal_emit(wd->base, "RESETHIDE", "PROG");
100           } 
101         else 
102           {
103              edje_object_signal_emit(wd->base, "RESETSHOW", "PROG");
104           }
105      }
106    else
107      {
108         edje_object_signal_emit(wd->base, "RESETHIDE", "PROG");
109      }
110 */
111
112    evas_object_smart_callback_call(data, "changed", NULL);
113 }
114
115 static void _cancel_clicked(void *data, Evas_Object *obj, const char *emission, const char *source)
116 {
117    Widget_Data *wd = elm_widget_data_get(data);
118    if (!wd) return;
119
120    if (wd->cancel_btn_ani_flag == EINA_TRUE)
121      edje_object_signal_emit(wd->base, "CANCELOUT", "PROG");
122    else
123      edje_object_signal_emit(wd->base, "CANCELHIDE", "PROG");
124
125
126    const char* text;
127    text = elm_entry_entry_get(elm_editfield_entry_get(wd->eb));
128    if (text != NULL && strlen(text) > 0)
129      elm_entry_entry_set(elm_editfield_entry_get(wd->eb), NULL);
130
131    evas_object_smart_callback_call(data, "cancel,clicked", NULL);
132 }
133
134 static void _signal_reset_clicked(void *data, Evas_Object *obj, const char *emission, const char *source)
135 {
136    Widget_Data *wd = elm_widget_data_get(data);
137    if (!wd) return;
138    elm_entry_entry_set(elm_editfield_entry_get(wd->eb), NULL);
139 }
140
141 /**
142  * Add a new searchbar to the parent
143  * @param parent The parent object
144  * @return The new object or NULL if it cannot be created
145  *
146  * @ingroup Searchbar
147  */
148 EAPI Evas_Object *elm_searchbar_add(Evas_Object *parent)
149 {
150    Evas_Object *obj;
151    Evas *e;
152    Widget_Data *wd;
153
154    wd = ELM_NEW(Widget_Data);
155    e = evas_object_evas_get(parent);
156    if (e == NULL) return NULL;
157    obj = elm_widget_add(e);
158    if (obj == NULL) return NULL;
159    elm_widget_type_set(obj, "searchbar");
160    elm_widget_sub_object_add(parent, obj);
161    elm_widget_data_set(obj, wd);
162    elm_widget_del_hook_set(obj, _del_hook);
163    elm_widget_theme_hook_set(obj, _theme_hook);
164    elm_widget_can_focus_set(obj, 1 );
165
166    wd->base = edje_object_add(e);
167    if (wd->base == NULL) return NULL;
168
169    _elm_theme_object_set(obj, wd->base, "searchbar", "base", "default");
170
171    //   evas_object_size_hint_weight_set(wd->base, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
172    //   evas_object_size_hint_align_set(wd->base, EVAS_HINT_FILL, EVAS_HINT_FILL);
173
174    // Add Entry
175    wd->eb = elm_editfield_add(parent);
176    elm_object_style_set(wd->eb, "searchbar");
177    edje_object_part_swallow(wd->base, "btn_text", wd->eb);
178 //   elm_editfield_guide_text_set(di, _("Text Input"));
179    elm_editfield_entry_single_line_set(wd->eb, EINA_TRUE);
180    elm_editfield_eraser_set(wd->eb, EINA_TRUE);
181    evas_object_smart_callback_add(wd->eb, "clicked", _clicked, obj);
182    evas_object_smart_callback_add(wd->eb, "changed", _changed, obj);
183    elm_widget_sub_object_add(obj, wd->eb);
184
185    // Add Button
186    wd->cancel_btn = elm_button_add(parent);
187    edje_object_part_swallow(wd->base, "button_cancel", wd->cancel_btn);
188    elm_object_style_set(wd->cancel_btn, "custom/darkblue");
189    elm_button_label_set(wd->cancel_btn, "Cancel");
190    evas_object_smart_callback_add(wd->cancel_btn, "clicked", _cancel_clicked, obj);
191    elm_widget_sub_object_add(obj, wd->cancel_btn);
192
193    wd->cancel_btn_ani_flag = EINA_FALSE;
194
195    edje_object_signal_callback_add(wd->base, "elm,action,click", "", _signal_reset_clicked, obj);
196
197    elm_widget_resize_object_set(obj, wd->base);
198
199    _sizing_eval(obj);
200
201    return obj;
202 }
203
204 /**
205  * get the text of entry
206  *
207  * @param obj The entry object
208  * @return The title of entry
209  *
210  * @ingroup entry
211  */
212 EAPI void elm_searchbar_text_set(Evas_Object *obj, const char *entry)
213 {
214    Widget_Data *wd = elm_widget_data_get(obj);
215    if (!wd) return NULL;
216
217    elm_entry_entry_set(elm_editfield_entry_get(wd->eb), entry);
218 }
219
220 /**
221  * get the text of entry
222  *
223  * @param obj The entry object
224  * @return The title of entry
225  *
226  * @ingroup entry
227  */
228 EAPI const char* elm_searchbar_text_get(Evas_Object *obj)
229 {
230    Widget_Data *wd = elm_widget_data_get(obj);
231    if (!wd) return NULL;
232
233    return elm_entry_entry_get(elm_editfield_entry_get(wd->eb));
234 }
235
236 /**
237  * get the pointer of entry
238  *
239  * @param obj The entry object
240  * @return The title of entry
241  *
242  * @ingroup entry
243  */
244 EAPI Evas_Object *elm_searchbar_entry_get(Evas_Object *obj)
245 {
246    Widget_Data *wd = elm_widget_data_get(obj);
247    if (!wd) return NULL;
248
249    return elm_editfield_entry_get(wd->eb);
250 }
251
252 /**
253  * get the pointer of entry
254  *
255  * @param obj The entry object
256  * @return The title of entry
257  *
258  * @ingroup entry
259  */
260 EAPI void elm_searchbar_cancel_button_animation_set(Evas_Object *obj, Eina_Bool cancel_btn_ani_flag)
261 {
262    Widget_Data *wd = elm_widget_data_get(obj);
263    if (!wd) return;
264
265    if (cancel_btn_ani_flag == EINA_TRUE)
266      wd->cancel_btn_ani_flag = EINA_TRUE;
267    else
268      wd->cancel_btn_ani_flag = EINA_FALSE;
269 }