Merge "[elm_genlist] indentation"
[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    if (!wd) return;
46
47    _elm_theme_object_set(obj, wd->base, "searchbar", "base", elm_widget_style_get(obj));
48
49    if (wd->eb)
50      edje_object_part_swallow(wd->base, "search_textfield", wd->eb);
51    if (wd->cancel_btn)
52      edje_object_part_swallow(wd->base, "button_cancel", wd->cancel_btn);
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
59 _on_focus_hook(void *data, Evas_Object *obj)
60 {
61    Widget_Data *wd = elm_widget_data_get(obj);
62    if (!wd || !wd->base)
63       return;   
64    if (elm_widget_focus_get(obj))
65      {
66         if (wd->cancel_btn_show_mode)
67           {
68              if (wd->cancel_btn_ani_flag) edje_object_signal_emit(wd->base, "CANCELIN", "PROG");
69              else edje_object_signal_emit(wd->base, "CANCELSHOW", "PROG");
70           }
71      }
72 }
73
74 static void _sizing_eval(Evas_Object *obj)
75 {
76    Widget_Data *wd = elm_widget_data_get(obj);
77    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
78
79    if (!wd) return;
80    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
81    edje_object_size_min_restricted_calc(wd->base, &minw, &minh, minw, minh);
82    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
83    evas_object_size_hint_min_set(obj, minw, minh);
84    evas_object_size_hint_max_set(obj, maxw, maxh);
85 }
86
87 static void _clicked(void *data, Evas_Object *obj, void *event_info)
88 {
89    Widget_Data *wd = elm_widget_data_get(data);
90    if (!wd) return;
91
92    if (wd->cancel_btn_show_mode)
93      {
94         if (wd->cancel_btn_ani_flag) edje_object_signal_emit(wd->base, "CANCELIN", "PROG");
95         else edje_object_signal_emit(wd->base, "CANCELSHOW", "PROG");
96      }
97
98    evas_object_smart_callback_call(data, "clicked", NULL);
99 }
100
101 static void _changed(void *data, Evas_Object *obj, void *event_info)
102 {
103    Widget_Data *wd = elm_widget_data_get(data);
104
105    if (!wd) return;
106
107    // TODO : inform to use entry changed callback 
108 //   evas_object_smart_callback_call(data, "changed", NULL);
109 }
110
111 static void _cancel_clicked(void *data, Evas_Object *obj, void *event_info)
112 {
113    Widget_Data *wd = elm_widget_data_get(data);
114    if (!wd) return;
115
116    if (wd->cancel_btn_show_mode)
117      {
118         if (wd->cancel_btn_ani_flag) edje_object_signal_emit(wd->base, "CANCELOUT", "PROG");
119         else edje_object_signal_emit(wd->base, "CANCELHIDE", "PROG");
120      }
121
122    const char* text;
123    text = elm_entry_entry_get(elm_editfield_entry_get(wd->eb));
124    if (text != NULL && strlen(text) > 0) elm_entry_entry_set(elm_editfield_entry_get(wd->eb), NULL);
125
126    evas_object_smart_callback_call(data, "cancel,clicked", NULL);
127 }
128
129 static void
130 _searchicon_clicked(void *data, Evas_Object *obj, const char *emission, const char *source)
131 {
132    Widget_Data *wd = elm_widget_data_get(data);
133    if (!wd) return;
134
135    if (!strcmp(source, "search_icon"))
136      evas_object_smart_callback_call(data, "searchsymbol,clicked", NULL);
137    else if (!strcmp(source, "base_bg"))
138      _clicked(data, obj, NULL); //emission, source);
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_on_focus_hook_set( obj, _on_focus_hook, NULL );
165    elm_widget_can_focus_set(obj, 1 );
166
167    wd->base = edje_object_add(e);
168    if (wd->base == NULL) return NULL;
169
170    _elm_theme_object_set(obj, wd->base, "searchbar", "base", "default");
171
172    //   evas_object_size_hint_weight_set(wd->base, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
173    //   evas_object_size_hint_align_set(wd->base, EVAS_HINT_FILL, EVAS_HINT_FILL);
174
175    // Add Entry
176    wd->eb = elm_editfield_add(parent);
177    elm_object_style_set(wd->eb, "searchbar");
178    edje_object_part_swallow(wd->base, "search_textfield", wd->eb);
179 //   elm_editfield_guide_text_set(wd->eb, "Search");
180    elm_editfield_entry_single_line_set(wd->eb, EINA_TRUE);
181    elm_editfield_eraser_set(wd->eb, EINA_TRUE);
182    evas_object_smart_callback_add(wd->eb, "clicked", _clicked, obj);
183    evas_object_smart_callback_add(elm_editfield_entry_get(wd->eb), "changed", _changed, obj);
184    edje_object_signal_callback_add(wd->base, "mouse,up,1", "*", _searchicon_clicked, obj);
185
186    elm_widget_sub_object_add(obj, wd->eb);
187
188    // Add Button
189    wd->cancel_btn = elm_button_add(parent);
190    edje_object_part_swallow(wd->base, "button_cancel", wd->cancel_btn);
191    elm_object_style_set(wd->cancel_btn, "custom/darkblue");
192    elm_button_label_set(wd->cancel_btn, "Cancel");
193    evas_object_smart_callback_add(wd->cancel_btn, "clicked", _cancel_clicked, obj);
194    elm_widget_sub_object_add(obj, wd->cancel_btn);
195
196    wd->cancel_btn_ani_flag = EINA_FALSE;
197    wd->cancel_btn_show_mode = EINA_TRUE;
198    wd->boundary_mode = EINA_TRUE;
199
200    elm_widget_resize_object_set(obj, wd->base);
201
202    _sizing_eval(obj);
203
204    return obj;
205 }
206
207 /**
208  * set the text of entry
209  *
210  * @param obj The searchbar object
211  * @return void
212  *
213  * @ingroup Searchbar
214  */
215 EAPI void elm_searchbar_text_set(Evas_Object *obj, const char *entry)
216 {
217    Widget_Data *wd = elm_widget_data_get(obj);
218    if (!wd) return;
219
220    elm_entry_entry_set(elm_editfield_entry_get(wd->eb), entry);
221 }
222
223 /**
224  * get the text of entry
225  *
226  * @param obj The searchbar object
227  * @return string pointer of entry
228  *
229  * @ingroup Searchbar
230  */
231 EAPI const char* elm_searchbar_text_get(Evas_Object *obj)
232 {
233    Widget_Data *wd = elm_widget_data_get(obj);
234    if (!wd) return NULL;
235
236    return elm_entry_entry_get(elm_editfield_entry_get(wd->eb));
237 }
238
239 /**
240  * get the pointer of entry
241  *
242  * @param obj The searchbar object
243  * @return the entry object
244  *
245  * @ingroup Searchbar
246  */
247 EAPI Evas_Object *elm_searchbar_entry_get(Evas_Object *obj)
248 {
249    Widget_Data *wd = elm_widget_data_get(obj);
250    if (!wd) return NULL;
251
252    return elm_editfield_entry_get(wd->eb);
253 }
254
255 /**
256  * set the cancel button animation flag
257  *
258  * @param obj The searchbar object
259  * @param cancel_btn_ani_flag The flag of animating cancen button or not
260  * @return void
261  *
262  * @ingroup Searchbar
263  */
264 EAPI void elm_searchbar_cancel_button_animation_set(Evas_Object *obj, Eina_Bool cancel_btn_ani_flag)
265 {
266    Widget_Data *wd = elm_widget_data_get(obj);
267    if (!wd) return;
268
269    if (wd->cancel_btn_ani_flag == cancel_btn_ani_flag) return;
270    else wd->cancel_btn_ani_flag = cancel_btn_ani_flag;
271 }
272
273 /**
274  * set the cancel button show mode
275  *
276  * @param obj The searchbar object
277  * @param visible The flag of cancen button show or not
278  * @return void
279  *
280  * @ingroup Searchbar
281  */
282 EAPI void elm_searchbar_cancel_button_set(Evas_Object *obj, Eina_Bool visible)
283 {
284    Widget_Data *wd = elm_widget_data_get(obj);
285    if (!wd) return;
286
287    if (wd->cancel_btn_show_mode == visible) return;
288    else wd->cancel_btn_show_mode = visible;
289
290    if (!visible)
291      {
292         if (wd->cancel_btn_ani_flag)
293            edje_object_signal_emit(wd->base, "CANCELOUT", "PROG");
294         else
295            edje_object_signal_emit(wd->base, "CANCELHIDE", "PROG");
296      }
297    _sizing_eval(obj);
298 }
299
300 /**
301  * clear searchbar status
302  *
303  * @param obj The searchbar object
304  * @return void
305  *
306  * @ingroup Searchbar
307  */
308 EAPI void elm_searchbar_clear(Evas_Object *obj)
309 {
310    Widget_Data *wd = elm_widget_data_get(obj);
311    if (!wd) return;
312
313    if (wd->cancel_btn_show_mode)
314      {
315         if (wd->cancel_btn_ani_flag)
316            edje_object_signal_emit(wd->base, "CANCELOUT", "PROG");
317         else
318            edje_object_signal_emit(wd->base, "CANCELHIDE", "PROG");
319      }
320 //   elm_entry_entry_set(elm_editfield_entry_get(wd->eb), NULL);
321 }
322
323 /**
324  * set the searchbar boundary rect mode(with bg rect) set
325  *
326  * @param obj The searchbar object
327  * @param boundary The present flag of boundary rect or not
328  * @return void
329  *
330  * @ingroup Searchbar
331  */
332 EAPI void elm_searchbar_boundary_rect_set(Evas_Object *obj, Eina_Bool boundary)
333 {
334    Widget_Data *wd = elm_widget_data_get(obj);
335    if (!wd) return;
336
337    if (wd->boundary_mode == boundary) return;
338    else wd->boundary_mode = boundary;
339
340    if (wd->boundary_mode)
341      {
342         edje_object_signal_emit(wd->base, "BDSHOW", "PROG");
343      }
344    else
345      {
346         edje_object_signal_emit(wd->base, "BDHIDE", "PROG");
347      }
348    _sizing_eval(obj);
349 }