[Upstream merge r63513] elm map: Coding convention.
[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, *ef, *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 const char *widtype = NULL;
27 static void _del_hook(Evas_Object *obj);
28 static void _theme_hook(Evas_Object *obj);
29 static void _on_focus_hook(void *data, Evas_Object *obj);
30 static void _sizing_eval(Evas_Object *obj);
31 static void _clicked(void *data, Evas_Object *obj, void *event_info);
32 static void _changed(void *data, Evas_Object *obj, void *event_info);
33 static void _cancel_clicked(void *data, Evas_Object *obj, void *event_info);
34
35 static void _del_hook(Evas_Object *obj)
36 {
37    Widget_Data *wd = elm_widget_data_get(obj);
38
39    if (!wd) return;
40    if (wd->idler) ecore_idler_del(wd->idler);
41
42    free(wd);
43 }
44
45 static void _theme_hook(Evas_Object *obj)
46 {
47    Widget_Data *wd = elm_widget_data_get(obj);
48    char buf[4096];
49
50    if (!wd) return;
51
52    _elm_theme_object_set(obj, wd->base, "searchbar", "base", elm_widget_style_get(obj));
53
54    if (wd->ef)
55      edje_object_part_swallow(wd->base, "search_textfield", wd->ef);
56    if (wd->cancel_btn)
57      edje_object_part_swallow(wd->base, "button_cancel", wd->cancel_btn);
58
59    snprintf(buf, sizeof(buf), "searchbar/%s", elm_widget_style_get(obj));
60    elm_object_style_set(wd->ef, buf);
61
62    snprintf(buf, sizeof(buf), "searchbar/%s", elm_widget_style_get(obj));
63    elm_object_style_set(wd->cancel_btn, buf);
64
65    edje_object_scale_set(wd->cancel_btn, elm_widget_scale_get(obj) * _elm_config->scale);
66    _sizing_eval(obj);
67 }
68
69 static void
70 _on_focus_hook(void *data __UNUSED__, Evas_Object *obj)
71 {
72    Widget_Data *wd = elm_widget_data_get(obj);
73    if (!wd || !wd->base)
74       return;
75    if (elm_widget_focus_get(obj))
76      {
77         /*FIXME: Sending clicked signal to focus editfield once open source is merged
78          * remove the below call
79          */
80         elm_object_signal_emit(wd->ef, "clicked", "elm");
81         if (wd->cancel_btn_show_mode)
82           {
83              if (wd->cancel_btn_ani_flag) edje_object_signal_emit(wd->base, "CANCELIN", "PROG");
84              else edje_object_signal_emit(wd->base, "CANCELSHOW", "PROG");
85           }
86      }
87    else
88      {
89         if (wd->cancel_btn_ani_flag) edje_object_signal_emit(wd->base, "CANCELOUT", "PROG");
90         else edje_object_signal_emit(wd->base, "CANCELHIDE", "PROG");
91      }
92 }
93
94 static void _sizing_eval(Evas_Object *obj)
95 {
96    Widget_Data *wd = elm_widget_data_get(obj);
97    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
98
99    if (!wd) return;
100    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
101    edje_object_size_min_restricted_calc(wd->base, &minw, &minh, minw, minh);
102    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
103    evas_object_size_hint_min_set(obj, minw, minh);
104    evas_object_size_hint_max_set(obj, maxw, maxh);
105 }
106
107 static void _clicked(void *data, Evas_Object *obj __UNUSED__,
108                      void *event_info __UNUSED__)
109 {
110    Widget_Data *wd = elm_widget_data_get(data);
111    if (!wd) return;
112
113    evas_object_smart_callback_call(data, "clicked", NULL);
114 }
115
116 static Eina_Bool _delay_changed(void *data)
117 {
118    Widget_Data *wd = elm_widget_data_get(data);
119
120    if (!wd) return ECORE_CALLBACK_CANCEL;
121
122    evas_object_smart_callback_call(data, "delay-changed", NULL);
123    wd->idler = NULL;
124    return ECORE_CALLBACK_CANCEL;
125 }
126
127 static void _changed(void *data, Evas_Object *obj __UNUSED__,
128                      void *event_info __UNUSED__)
129 {
130    Widget_Data *wd = elm_widget_data_get(data);
131
132    if (!wd) return;
133    if (!wd->idler)
134       wd->idler = ecore_idler_add(_delay_changed, data);
135 }
136
137 static void _cancel_clicked(void *data, Evas_Object *obj __UNUSED__,
138                             void *event_info __UNUSED__)
139 {
140    Widget_Data *wd = elm_widget_data_get(data);
141    if (!wd) return;
142
143    const char* text;
144    text = elm_entry_entry_get(elm_editfield_entry_get(wd->ef));
145    if (text != NULL && strlen(text) > 0)
146      elm_entry_entry_set(elm_editfield_entry_get(wd->ef), 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 __UNUSED__,
154                 const char *source)
155 {
156    Widget_Data *wd = elm_widget_data_get(data);
157
158    if (!wd) return;
159
160    if (!strcmp(source, "base_bg"))
161       _clicked(data, obj, NULL);
162 }
163
164 static void
165 _searchsymbol_clicked(void *data, Evas_Object *obj __UNUSED__,
166                       const char *emission __UNUSED__,
167                       const char *source __UNUSED__)
168 {
169    Widget_Data *wd = elm_widget_data_get(data);
170
171    if (!wd) return;
172    evas_object_smart_callback_call(data, "searchsymbol,clicked", NULL);
173 }
174
175 /**
176  * Add a new searchbar to the parent
177  * @param parent The parent object
178  * @return The new object or NULL if it cannot be created
179  *
180  * @ingroup Searchbar
181  */
182 EAPI Evas_Object *elm_searchbar_add(Evas_Object *parent)
183 {
184    Evas_Object *obj;
185    Evas *e;
186    Widget_Data *wd;
187    char buf[4096];
188
189    ELM_WIDGET_STANDARD_SETUP(wd, Widget_Data, parent, e, obj, NULL);
190
191    ELM_SET_WIDTYPE(widtype, "searchbar");
192    elm_widget_type_set(obj, "searchbar");
193    elm_widget_sub_object_add(parent, obj);
194    elm_widget_data_set(obj, wd);
195    elm_widget_del_hook_set(obj, _del_hook);
196    elm_widget_theme_hook_set(obj, _theme_hook);
197    elm_widget_on_focus_hook_set( obj, _on_focus_hook, NULL );
198    elm_widget_can_focus_set(obj, 1 );
199
200    wd->base = edje_object_add(e);
201    if (wd->base == NULL) return NULL;
202
203    _elm_theme_object_set(obj, wd->base, "searchbar", "base", "default");
204
205    // Add Entry
206    wd->ef = elm_editfield_add(parent);
207    elm_object_style_set(wd->ef, "searchbar/default");
208
209    edje_object_part_swallow(wd->base, "search_textfield", wd->ef);
210    elm_editfield_guide_text_set(wd->ef, E_("Search"));
211    elm_editfield_entry_single_line_set(wd->ef, EINA_TRUE);
212    elm_editfield_eraser_set(wd->ef, EINA_TRUE);
213    evas_object_smart_callback_add(wd->ef, "clicked", _clicked, obj);
214    evas_object_smart_callback_add(elm_editfield_entry_get(wd->ef), "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->ef);
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_object_text_set(wd->cancel_btn, E_("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    ELM_CHECK_WIDTYPE(obj, widtype);
253    Widget_Data *wd = elm_widget_data_get(obj);
254    if (!wd) return;
255
256    elm_entry_entry_set(elm_editfield_entry_get(wd->ef), entry);
257 }
258
259 /**
260  * get the text of entry
261  *
262  * @param obj The searchbar object
263  * @return string pointer of entry
264  *
265  * @ingroup Searchbar
266  */
267 EAPI const char* elm_searchbar_text_get(Evas_Object *obj)
268 {
269    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
270    Widget_Data *wd = elm_widget_data_get(obj);
271    if (!wd) return NULL;
272
273    return elm_entry_entry_get(elm_editfield_entry_get(wd->ef));
274 }
275
276 /**
277  * get the pointer of entry
278  *
279  * @param obj The searchbar object
280  * @return the entry object
281  *
282  * @ingroup Searchbar
283  */
284 EAPI Evas_Object *elm_searchbar_entry_get(Evas_Object *obj)
285 {
286    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
287    Widget_Data *wd = elm_widget_data_get(obj);
288    if (!wd) return NULL;
289
290    return elm_editfield_entry_get(wd->ef);
291 }
292
293 /**
294  * get the pointer of editfield
295  *
296  * @param obj The searchbar object
297  * @return the editfield object
298  *
299  * @ingroup Searchbar
300  */
301 EAPI Evas_Object *elm_searchbar_editfield_get(Evas_Object *obj)
302 {
303    ELM_CHECK_WIDTYPE(obj, widtype) NULL;
304    Widget_Data *wd = elm_widget_data_get(obj);
305    if (!wd || !wd->ef) return NULL;
306
307    return wd->ef;
308 }
309
310 /**
311  * set the cancel button animation flag
312  *
313  * @param obj The searchbar object
314  * @param cancel_btn_ani_flag The flag of animating cancen button or not
315  * @return void
316  *
317  * @ingroup Searchbar
318  */
319 EAPI void elm_searchbar_cancel_button_animation_set(Evas_Object *obj, Eina_Bool cancel_btn_ani_flag)
320 {
321    ELM_CHECK_WIDTYPE(obj, widtype);
322    Widget_Data *wd = elm_widget_data_get(obj);
323    if (!wd) return;
324
325    if (wd->cancel_btn_ani_flag == cancel_btn_ani_flag) return;
326    else wd->cancel_btn_ani_flag = cancel_btn_ani_flag;
327 }
328
329 /**
330  * set the cancel button show mode
331  *
332  * @param obj The searchbar object
333  * @param visible The flag of cancen button show or not
334  * @return void
335  *
336  * @ingroup Searchbar
337  */
338 EAPI void elm_searchbar_cancel_button_set(Evas_Object *obj, Eina_Bool visible)
339 {
340    ELM_CHECK_WIDTYPE(obj, widtype);
341    Widget_Data *wd = elm_widget_data_get(obj);
342    if (!wd) return;
343
344    if (wd->cancel_btn_show_mode == visible) return;
345    else wd->cancel_btn_show_mode = visible;
346
347    if (!visible)
348      {
349         if (wd->cancel_btn_ani_flag)
350            edje_object_signal_emit(wd->base, "CANCELOUT", "PROG");
351         else
352            edje_object_signal_emit(wd->base, "CANCELHIDE", "PROG");
353      }
354    _sizing_eval(obj);
355 }
356
357 /**
358  * clear searchbar status
359  *
360  * @param obj The searchbar object
361  * @return void
362  *
363  * @ingroup Searchbar
364  */
365 EAPI void elm_searchbar_clear(Evas_Object *obj)
366 {
367    ELM_CHECK_WIDTYPE(obj, widtype);
368    Widget_Data *wd = elm_widget_data_get(obj);
369    if (!wd) return;
370
371    if (wd->cancel_btn_show_mode)
372      {
373         if (wd->cancel_btn_ani_flag)
374            edje_object_signal_emit(wd->base, "CANCELOUT", "PROG");
375         else
376            edje_object_signal_emit(wd->base, "CANCELHIDE", "PROG");
377      }
378 //   elm_entry_entry_set(elm_editfield_entry_get(wd->ef), NULL);
379 }
380
381 /**
382  * set the searchbar boundary rect mode(with bg rect) set
383  *
384  * @param obj The searchbar object
385  * @param boundary The present flag of boundary rect or not
386  * @return void
387  *
388  * @ingroup Searchbar
389  */
390 EAPI void elm_searchbar_boundary_rect_set(Evas_Object *obj, Eina_Bool boundary)
391 {
392    ELM_CHECK_WIDTYPE(obj, widtype);
393    Widget_Data *wd = elm_widget_data_get(obj);
394    if (!wd) return;
395
396    if (wd->boundary_mode == boundary) return;
397    else wd->boundary_mode = boundary;
398
399    if (wd->boundary_mode)
400      {
401         edje_object_signal_emit(wd->base, "BDSHOW", "PROG");
402      }
403    else
404      {
405         edje_object_signal_emit(wd->base, "BDHIDE", "PROG");
406      }
407    _sizing_eval(obj);
408 }