Merge "[Searchbar] On Cancel Button click, searchbar should be unfocus."
[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    Eina_Bool cancel_btn_show_mode;
22    Eina_Bool boundary_mode;
23 };
24
25 static void _del_hook(Evas_Object *obj);
26 static void _theme_hook(Evas_Object *obj);
27 static void _on_focus_hook(void *data, Evas_Object *obj);
28 static void _sizing_eval(Evas_Object *obj);
29 static void _clicked(void *data, Evas_Object *obj, void *event_info);
30 static void _changed(void *data, Evas_Object *obj, void *event_info);
31 static void _cancel_clicked(void *data, Evas_Object *obj, void *event_info);
32
33 static void _del_hook(Evas_Object *obj)
34 {
35    Widget_Data *wd = elm_widget_data_get(obj);
36
37    if (!wd) return;
38
39    free(wd);
40 }
41
42 static void _theme_hook(Evas_Object *obj)
43 {
44    Widget_Data *wd = elm_widget_data_get(obj);
45    char buf[4096];
46
47    if (!wd) return;
48
49    _elm_theme_object_set(obj, wd->base, "searchbar", "base", elm_widget_style_get(obj));
50
51    if (wd->eb)
52      edje_object_part_swallow(wd->base, "search_textfield", wd->eb);
53    if (wd->cancel_btn)
54      edje_object_part_swallow(wd->base, "button_cancel", wd->cancel_btn);
55
56    snprintf(buf, sizeof(buf), "searchbar/%s", elm_widget_style_get(obj));
57    elm_object_style_set(wd->eb, buf);
58
59    snprintf(buf, sizeof(buf), "searchbar/%s", elm_widget_style_get(obj));
60    elm_object_style_set(wd->cancel_btn, buf);
61
62    edje_object_scale_set(wd->cancel_btn, elm_widget_scale_get(obj) * _elm_config->scale);
63    _sizing_eval(obj);
64 }
65
66 static void
67 _on_focus_hook(void *data, Evas_Object *obj)
68 {
69    Widget_Data *wd = elm_widget_data_get(obj);
70    if (!wd || !wd->base)
71       return;   
72    if (elm_widget_focus_get(obj))
73      {
74         if (wd->cancel_btn_show_mode)
75           {
76              if (wd->cancel_btn_ani_flag) edje_object_signal_emit(wd->base, "CANCELIN", "PROG");
77              else edje_object_signal_emit(wd->base, "CANCELSHOW", "PROG");
78           }
79      }
80 }
81
82 static void _sizing_eval(Evas_Object *obj)
83 {
84    Widget_Data *wd = elm_widget_data_get(obj);
85    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
86
87    if (!wd) return;
88    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
89    edje_object_size_min_restricted_calc(wd->base, &minw, &minh, minw, minh);
90    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
91    evas_object_size_hint_min_set(obj, minw, minh);
92    evas_object_size_hint_max_set(obj, maxw, maxh);
93 }
94
95 static void _clicked(void *data, Evas_Object *obj, void *event_info)
96 {
97    Widget_Data *wd = elm_widget_data_get(data);
98    if (!wd) return;
99
100    if (wd->cancel_btn_show_mode)
101      {
102         if (wd->cancel_btn_ani_flag) edje_object_signal_emit(wd->base, "CANCELIN", "PROG");
103         else edje_object_signal_emit(wd->base, "CANCELSHOW", "PROG");
104      }
105
106    evas_object_smart_callback_call(data, "clicked", NULL);
107 }
108
109 static void _changed(void *data, Evas_Object *obj, void *event_info)
110 {
111    Widget_Data *wd = elm_widget_data_get(data);
112
113    if (!wd) return;
114
115    // TODO : inform to use entry changed callback 
116 //   evas_object_smart_callback_call(data, "changed", NULL);
117 }
118
119 static void _cancel_clicked(void *data, Evas_Object *obj, void *event_info)
120 {
121    Widget_Data *wd = elm_widget_data_get(data);
122    if (!wd) return;
123
124    if (wd->cancel_btn_show_mode)
125      {
126         if (wd->cancel_btn_ani_flag) edje_object_signal_emit(wd->base, "CANCELOUT", "PROG");
127         else edje_object_signal_emit(wd->base, "CANCELHIDE", "PROG");
128      }
129
130    const char* text;
131    text = elm_entry_entry_get(elm_editfield_entry_get(wd->eb));
132    if (text != NULL && strlen(text) > 0) elm_entry_entry_set(elm_editfield_entry_get(wd->eb), NULL);
133
134    evas_object_smart_callback_call(data, "cancel,clicked", NULL);
135    elm_object_unfocus(data);
136 }
137
138 static void
139 _basebg_clicked(void *data, Evas_Object *obj, const char *emission, const char *source)
140 {
141    Widget_Data *wd = elm_widget_data_get(data);
142
143    if (!wd) return;
144
145    if (!strcmp(source, "base_bg"))
146       _clicked(data, obj, NULL);
147 }
148
149 static void
150 _searchsymbol_clicked(void *data, Evas_Object *obj, const char *emission, const char *source)
151 {
152    Widget_Data *wd = elm_widget_data_get(data);
153
154    if (!wd) return;
155    evas_object_smart_callback_call(data, "searchsymbol,clicked", NULL);
156 }
157
158 /**
159  * Add a new searchbar to the parent
160  * @param parent The parent object
161  * @return The new object or NULL if it cannot be created
162  *
163  * @ingroup Searchbar
164  */
165 EAPI Evas_Object *elm_searchbar_add(Evas_Object *parent)
166 {
167    Evas_Object *obj;
168    Evas *e;
169    Widget_Data *wd;
170    char buf[4096];
171
172    wd = ELM_NEW(Widget_Data);
173    e = evas_object_evas_get(parent);
174    if (e == NULL) return NULL;
175    obj = elm_widget_add(e);
176    if (obj == NULL) return NULL;
177    elm_widget_type_set(obj, "searchbar");
178    elm_widget_sub_object_add(parent, obj);
179    elm_widget_data_set(obj, wd);
180    elm_widget_del_hook_set(obj, _del_hook);
181    elm_widget_theme_hook_set(obj, _theme_hook);
182    elm_widget_on_focus_hook_set( obj, _on_focus_hook, NULL );
183    elm_widget_can_focus_set(obj, 1 );
184
185    wd->base = edje_object_add(e);
186    if (wd->base == NULL) return NULL;
187
188    _elm_theme_object_set(obj, wd->base, "searchbar", "base", "default");
189
190    //   evas_object_size_hint_weight_set(wd->base, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
191    //   evas_object_size_hint_align_set(wd->base, EVAS_HINT_FILL, EVAS_HINT_FILL);
192
193    // Add Entry
194    wd->eb = elm_editfield_add(parent);
195    snprintf(buf, sizeof(buf), "searchbar/%s", elm_widget_style_get(obj));
196    elm_object_style_set(wd->eb, buf);
197
198    edje_object_part_swallow(wd->base, "search_textfield", wd->eb);
199 //   elm_editfield_guide_text_set(wd->eb, "Search");
200    elm_editfield_entry_single_line_set(wd->eb, EINA_TRUE);
201    elm_editfield_eraser_set(wd->eb, EINA_TRUE);
202    evas_object_smart_callback_add(wd->eb, "clicked", _clicked, obj);
203    evas_object_smart_callback_add(elm_editfield_entry_get(wd->eb), "changed", _changed, obj);
204    edje_object_signal_callback_add(wd->base, "mouse,up,1", "*", _basebg_clicked, obj);
205    edje_object_signal_callback_add(wd->base, "elm,action,click", "", _searchsymbol_clicked, obj);
206
207    elm_widget_sub_object_add(obj, wd->eb);
208
209    // Add Button
210    wd->cancel_btn = elm_button_add(parent);
211    edje_object_part_swallow(wd->base, "button_cancel", wd->cancel_btn);
212
213    snprintf(buf, sizeof(buf), "searchbar/%s", elm_widget_style_get(obj));
214    elm_object_style_set(wd->cancel_btn, buf);
215
216    elm_button_label_set(wd->cancel_btn, "Cancel");
217    evas_object_smart_callback_add(wd->cancel_btn, "clicked", _cancel_clicked, obj);
218    elm_widget_sub_object_add(obj, wd->cancel_btn);
219
220    wd->cancel_btn_ani_flag = EINA_FALSE;
221    wd->cancel_btn_show_mode = EINA_TRUE;
222    wd->boundary_mode = EINA_TRUE;
223
224    elm_widget_resize_object_set(obj, wd->base);
225
226    _sizing_eval(obj);
227
228    return obj;
229 }
230
231 /**
232  * set the text of entry
233  *
234  * @param obj The searchbar object
235  * @return void
236  *
237  * @ingroup Searchbar
238  */
239 EAPI void elm_searchbar_text_set(Evas_Object *obj, const char *entry)
240 {
241    Widget_Data *wd = elm_widget_data_get(obj);
242    if (!wd) return;
243
244    elm_entry_entry_set(elm_editfield_entry_get(wd->eb), entry);
245 }
246
247 /**
248  * get the text of entry
249  *
250  * @param obj The searchbar object
251  * @return string pointer of entry
252  *
253  * @ingroup Searchbar
254  */
255 EAPI const char* elm_searchbar_text_get(Evas_Object *obj)
256 {
257    Widget_Data *wd = elm_widget_data_get(obj);
258    if (!wd) return NULL;
259
260    return elm_entry_entry_get(elm_editfield_entry_get(wd->eb));
261 }
262
263 /**
264  * get the pointer of entry
265  *
266  * @param obj The searchbar object
267  * @return the entry object
268  *
269  * @ingroup Searchbar
270  */
271 EAPI Evas_Object *elm_searchbar_entry_get(Evas_Object *obj)
272 {
273    Widget_Data *wd = elm_widget_data_get(obj);
274    if (!wd) return NULL;
275
276    return elm_editfield_entry_get(wd->eb);
277 }
278
279 /**
280  * set the cancel button animation flag
281  *
282  * @param obj The searchbar object
283  * @param cancel_btn_ani_flag The flag of animating cancen button or not
284  * @return void
285  *
286  * @ingroup Searchbar
287  */
288 EAPI void elm_searchbar_cancel_button_animation_set(Evas_Object *obj, Eina_Bool cancel_btn_ani_flag)
289 {
290    Widget_Data *wd = elm_widget_data_get(obj);
291    if (!wd) return;
292
293    if (wd->cancel_btn_ani_flag == cancel_btn_ani_flag) return;
294    else wd->cancel_btn_ani_flag = cancel_btn_ani_flag;
295 }
296
297 /**
298  * set the cancel button show mode
299  *
300  * @param obj The searchbar object
301  * @param visible The flag of cancen button show or not
302  * @return void
303  *
304  * @ingroup Searchbar
305  */
306 EAPI void elm_searchbar_cancel_button_set(Evas_Object *obj, Eina_Bool visible)
307 {
308    Widget_Data *wd = elm_widget_data_get(obj);
309    if (!wd) return;
310
311    if (wd->cancel_btn_show_mode == visible) return;
312    else wd->cancel_btn_show_mode = visible;
313
314    if (!visible)
315      {
316         if (wd->cancel_btn_ani_flag)
317            edje_object_signal_emit(wd->base, "CANCELOUT", "PROG");
318         else
319            edje_object_signal_emit(wd->base, "CANCELHIDE", "PROG");
320      }
321    _sizing_eval(obj);
322 }
323
324 /**
325  * clear searchbar status
326  *
327  * @param obj The searchbar object
328  * @return void
329  *
330  * @ingroup Searchbar
331  */
332 EAPI void elm_searchbar_clear(Evas_Object *obj)
333 {
334    Widget_Data *wd = elm_widget_data_get(obj);
335    if (!wd) return;
336
337    if (wd->cancel_btn_show_mode)
338      {
339         if (wd->cancel_btn_ani_flag)
340            edje_object_signal_emit(wd->base, "CANCELOUT", "PROG");
341         else
342            edje_object_signal_emit(wd->base, "CANCELHIDE", "PROG");
343      }
344 //   elm_entry_entry_set(elm_editfield_entry_get(wd->eb), NULL);
345 }
346
347 /**
348  * set the searchbar boundary rect mode(with bg rect) set
349  *
350  * @param obj The searchbar object
351  * @param boundary The present flag of boundary rect or not
352  * @return void
353  *
354  * @ingroup Searchbar
355  */
356 EAPI void elm_searchbar_boundary_rect_set(Evas_Object *obj, Eina_Bool boundary)
357 {
358    Widget_Data *wd = elm_widget_data_get(obj);
359    if (!wd) return;
360
361    if (wd->boundary_mode == boundary) return;
362    else wd->boundary_mode = boundary;
363
364    if (wd->boundary_mode)
365      {
366         edje_object_signal_emit(wd->base, "BDSHOW", "PROG");
367      }
368    else
369      {
370         edje_object_signal_emit(wd->base, "BDHIDE", "PROG");
371      }
372    _sizing_eval(obj);
373 }