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