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