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