Merge branch 'master' of juyung.seo@165.213.180.234:/git/slp2.0/slp2.0-pkgs/EFL-pkgs...
[framework/uifw/elementary.git] / src / lib / elm_searchbar.c
1 #include <Elementary.h>
2 #include "elm_priv.h"
3
4 /**
5  * @defgroup Searchbar Searchbar
6  * @ingroup Elementary
7  *
8  * This is Searchbar. 
9  * It can contain a simple entry and button object.
10  */
11
12 typedef struct _Widget_Data Widget_Data;
13
14 struct _Widget_Data
15 {
16         Evas_Object *base, *eb, *cancel_btn;
17         Eina_Bool cancel_btn_ani_flag;
18 };
19
20 static void _del_hook(Evas_Object *obj);
21 static void _theme_hook(Evas_Object *obj);
22 static void _sizing_eval(Evas_Object *obj);
23 static void _clicked(void *data, Evas_Object *obj, const char *emission, const char *source);
24 static void _changed(void *data, Evas_Object *obj, const char *emission, const char *source);
25 static void _cancel_clicked(void *data, Evas_Object *obj, const char *emission, const char *source);
26 static void _signal_reset_clicked(void *data, Evas_Object *obj, const char *emission, const char *source);
27
28 static void _del_hook(Evas_Object *obj)
29 {
30         Widget_Data *wd = elm_widget_data_get(obj);
31
32         if (!wd) return;
33
34         free(wd);
35 }
36
37 static void _theme_hook(Evas_Object *obj)
38 {
39         Widget_Data *wd = elm_widget_data_get(obj);
40         if (!wd) return;
41
42         _elm_theme_object_set(obj, wd->base, "searchbar", "base", elm_widget_style_get(obj));
43
44         if (wd->eb)
45                 edje_object_part_swallow(wd->base, "btn_text", wd->eb);
46         if (wd->cancel_btn)
47                 edje_object_part_swallow(wd->base, "button_cancel", wd->cancel_btn);
48
49         edje_object_signal_callback_add(wd->base, "elm,action,click", "", _signal_reset_clicked, obj);
50
51         edje_object_scale_set(wd->cancel_btn, elm_widget_scale_get(obj) * _elm_config->scale);
52         _sizing_eval(obj);
53 }
54
55 static void _sizing_eval(Evas_Object *obj)
56 {
57         Widget_Data *wd = elm_widget_data_get(obj);
58         Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
59
60         if (!wd) return;
61         elm_coords_finger_size_adjust(1, &minw, 1, &minh);
62         edje_object_size_min_restricted_calc(wd->base, &minw, &minh, minw, minh);
63         elm_coords_finger_size_adjust(1, &minw, 1, &minh);
64         evas_object_size_hint_min_set(obj, minw, minh);
65         evas_object_size_hint_max_set(obj, maxw, maxh);
66 }
67
68 static void _clicked(void *data, Evas_Object *obj, const char *emission, const char *source)
69 {
70         Widget_Data *wd = elm_widget_data_get(data);
71         if (!wd) return;
72
73         elm_entry_cursor_end_set(wd->eb);
74         if (wd->cancel_btn_ani_flag == EINA_TRUE)
75                 edje_object_signal_emit(wd->base, "CANCELIN", "PROG");
76         else
77                 edje_object_signal_emit(wd->base, "CANCELSHOW", "PROG");
78
79         evas_object_smart_callback_call(data, "clicked", NULL);
80 }
81
82 static void _changed(void *data, Evas_Object *obj, const char *emission, const char *source)
83 {
84         Widget_Data *wd = elm_widget_data_get(data);
85         if (!wd) return;
86
87         int len = 0;
88         const char* text = elm_entry_entry_get(wd->eb);
89
90         if (text != NULL)
91         {
92                 len = strlen(text);
93                 if (len == 0) {
94                         edje_object_signal_emit(wd->base, "RESETHIDE", "PROG");
95                 } else {
96                         edje_object_signal_emit(wd->base, "RESETSHOW", "PROG");
97                 }
98         }
99         else
100         {
101                 edje_object_signal_emit(wd->base, "RESETHIDE", "PROG");
102         }
103
104         evas_object_smart_callback_call(data, "changed", NULL);
105 }
106
107 static void _cancel_clicked(void *data, Evas_Object *obj, const char *emission, const char *source)
108 {
109         Widget_Data *wd = elm_widget_data_get(data);
110         if (!wd) return;
111
112         if (wd->cancel_btn_ani_flag == EINA_TRUE)
113                 edje_object_signal_emit(wd->base, "CANCELOUT", "PROG");
114         else
115                 edje_object_signal_emit(wd->base, "CANCELHIDE", "PROG");
116
117
118         const char* text;
119         text = elm_entry_entry_get(wd->eb);
120         if (text != NULL && strlen(text) > 0)
121                 elm_entry_entry_set(wd->eb, NULL);
122
123         evas_object_smart_callback_call(data, "cancel,clicked", NULL);
124 }
125
126 static void _signal_reset_clicked(void *data, Evas_Object *obj, const char *emission, const char *source)
127 {
128         Widget_Data *wd = elm_widget_data_get(data);
129         if (!wd) return;
130         elm_entry_entry_set(wd->eb, NULL);
131 }
132
133 /**
134  * Add a new searchbar to the parent
135  * @param parent The parent object
136  * @return The new object or NULL if it cannot be created
137  *
138  * @ingroup Searchbar
139  */
140 EAPI Evas_Object *elm_searchbar_add(Evas_Object *parent)
141 {
142
143         Evas_Object *obj;
144         Evas *e;
145         Widget_Data *wd;
146
147         wd = ELM_NEW(Widget_Data);
148         e = evas_object_evas_get(parent);
149         if (e == NULL) return NULL;
150         obj = elm_widget_add(e);
151         if (obj == NULL) return NULL;
152         elm_widget_type_set(obj, "searchbar");
153         elm_widget_sub_object_add(parent, obj);
154         elm_widget_data_set(obj, wd);
155         elm_widget_del_hook_set(obj, _del_hook);
156         elm_widget_theme_hook_set(obj, _theme_hook);
157         elm_widget_can_focus_set(obj, 1 );
158
159         wd->base = edje_object_add(e);
160         if (wd->base == NULL) return NULL;
161
162         _elm_theme_object_set(obj, wd->base, "searchbar", "base", "default");
163
164 //      evas_object_size_hint_weight_set(wd->base, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
165 //      evas_object_size_hint_align_set(wd->base, EVAS_HINT_FILL, EVAS_HINT_FILL);
166
167         // Add Entry
168         wd->eb = elm_entry_add(parent);
169         edje_object_part_swallow(wd->base, "btn_text", wd->eb);
170 //      elm_object_style_set(wd->eb, "search_input");
171         elm_entry_single_line_set(wd->eb, EINA_TRUE);
172         evas_object_smart_callback_add(wd->eb, "clicked", _clicked, obj);
173         evas_object_smart_callback_add(wd->eb, "changed", _changed, obj);
174         elm_widget_sub_object_add(obj, wd->eb);
175
176         // Add Button
177         wd->cancel_btn = elm_button_add(parent);
178         edje_object_part_swallow(wd->base, "button_cancel", wd->cancel_btn);
179         elm_object_style_set(wd->cancel_btn, "custom/darkblue");
180         elm_button_label_set(wd->cancel_btn, "Cancel");
181         evas_object_smart_callback_add(wd->cancel_btn, "clicked", _cancel_clicked, obj);
182         elm_widget_sub_object_add(obj, wd->cancel_btn);
183
184         wd->cancel_btn_ani_flag = EINA_FALSE;
185
186         edje_object_signal_callback_add(wd->base, "elm,action,click", "", _signal_reset_clicked, obj);
187
188         elm_widget_resize_object_set(obj, wd->base);
189
190         _sizing_eval(obj);
191
192         return obj;
193 }
194
195 /**
196  * get the text of entry
197  *
198  * @param obj The entry object
199  * @return The title of entry
200  *
201  * @ingroup entry
202  */
203 EAPI void elm_searchbar_text_set(Evas_Object *obj, const char *entry)
204 {
205    Widget_Data *wd = elm_widget_data_get(obj);
206    if (!wd) return NULL;
207
208    elm_entry_entry_set(wd->eb, entry);
209 }
210
211 /**
212  * get the text of entry
213  *
214  * @param obj The entry object
215  * @return The title of entry
216  *
217  * @ingroup entry
218  */
219 EAPI const char* elm_searchbar_text_get(Evas_Object *obj)
220 {
221    Widget_Data *wd = elm_widget_data_get(obj);
222    if (!wd) return NULL;
223
224    return elm_entry_entry_get(wd->eb);
225 }
226
227 /**
228  * get the pointer of entry
229  *
230  * @param obj The entry object
231  * @return The title of entry
232  *
233  * @ingroup entry
234  */
235 EAPI Evas_Object *elm_searchbar_entry_get(Evas_Object *obj)
236 {
237    Widget_Data *wd = elm_widget_data_get(obj);
238    if (!wd) return NULL;
239
240    return wd->eb;
241 }
242
243 /**
244  * get the pointer of entry
245  *
246  * @param obj The entry object
247  * @return The title of entry
248  *
249  * @ingroup entry
250  */
251 EAPI void elm_searchbar_cancel_button_animation_set(Evas_Object *obj, Eina_Bool cancel_btn_ani_flag)
252 {
253         Widget_Data *wd = elm_widget_data_get(obj);
254         if (!wd) return;
255
256         if (cancel_btn_ani_flag == EINA_TRUE)
257                 wd->cancel_btn_ani_flag = EINA_TRUE;
258         else
259                 wd->cancel_btn_ani_flag = EINA_FALSE;
260
261 }