Merge branch 'master' of 165.213.180.234:/git/slp/pkgs/elementary
[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 };
22
23 static void _del_hook(Evas_Object *obj);
24 static void _theme_hook(Evas_Object *obj);
25 static void _sizing_eval(Evas_Object *obj);
26 static void _clicked(void *data, Evas_Object *obj, const char *emission, const char *source);
27 static void _changed(void *data, Evas_Object *obj, const char *emission, const char *source);
28 static void _cancel_clicked(void *data, Evas_Object *obj, const char *emission, const char *source);
29
30 static void _del_hook(Evas_Object *obj)
31 {
32    Widget_Data *wd = elm_widget_data_get(obj);
33
34    if (!wd) return;
35
36    free(wd);
37 }
38
39 static void _theme_hook(Evas_Object *obj)
40 {
41    Widget_Data *wd = elm_widget_data_get(obj);
42    if (!wd) return;
43
44    _elm_theme_object_set(obj, wd->base, "searchbar", "base", elm_widget_style_get(obj));
45
46    if (wd->eb)
47      edje_object_part_swallow(wd->base, "search_textfield", wd->eb);
48    if (wd->cancel_btn)
49      edje_object_part_swallow(wd->base, "button_cancel", wd->cancel_btn);
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
69 _searchicon_clicked(void *data, Evas_Object *obj, const char *emission, const char *source)
70 {
71         if (!strcmp(source, "search_icon"))
72                 evas_object_smart_callback_call(data, "searchsymbol,clicked", NULL);
73 }
74
75 static void _clicked(void *data, Evas_Object *obj, const char *emission, const char *source)
76 {
77    Widget_Data *wd = elm_widget_data_get(data);
78    if (!wd) return;
79
80    elm_entry_cursor_end_set(elm_editfield_entry_get(wd->eb));
81    if (wd->cancel_btn_ani_flag == EINA_TRUE)
82      edje_object_signal_emit(wd->base, "CANCELIN", "PROG");
83    else
84      edje_object_signal_emit(wd->base, "CANCELSHOW", "PROG");
85
86    evas_object_smart_callback_call(data, "clicked", NULL);
87 }
88
89 static void _changed(void *data, Evas_Object *obj, const char *emission, const char *source)
90 {
91    Widget_Data *wd = elm_widget_data_get(data);
92
93    if (!wd) return;
94
95    // TODO : inform to use entry changed callback 
96 //   evas_object_smart_callback_call(data, "changed", NULL);
97 }
98
99 static void _cancel_clicked(void *data, Evas_Object *obj, const char *emission, const char *source)
100 {
101    Widget_Data *wd = elm_widget_data_get(data);
102    if (!wd) return;
103
104    if (wd->cancel_btn_ani_flag == EINA_TRUE)
105      edje_object_signal_emit(wd->base, "CANCELOUT", "PROG");
106    else
107      edje_object_signal_emit(wd->base, "CANCELHIDE", "PROG");
108
109
110    const char* text;
111    text = elm_entry_entry_get(elm_editfield_entry_get(wd->eb));
112    if (text != NULL && strlen(text) > 0)
113      elm_entry_entry_set(elm_editfield_entry_get(wd->eb), NULL);
114
115    evas_object_smart_callback_call(data, "cancel,clicked", NULL);
116 }
117
118 /**
119  * Add a new searchbar to the parent
120  * @param parent The parent object
121  * @return The new object or NULL if it cannot be created
122  *
123  * @ingroup Searchbar
124  */
125 EAPI Evas_Object *elm_searchbar_add(Evas_Object *parent)
126 {
127    Evas_Object *obj;
128    Evas *e;
129    Widget_Data *wd;
130
131    wd = ELM_NEW(Widget_Data);
132    e = evas_object_evas_get(parent);
133    if (e == NULL) return NULL;
134    obj = elm_widget_add(e);
135    if (obj == NULL) return NULL;
136    elm_widget_type_set(obj, "searchbar");
137    elm_widget_sub_object_add(parent, obj);
138    elm_widget_data_set(obj, wd);
139    elm_widget_del_hook_set(obj, _del_hook);
140    elm_widget_theme_hook_set(obj, _theme_hook);
141    elm_widget_can_focus_set(obj, 1 );
142
143    wd->base = edje_object_add(e);
144    if (wd->base == NULL) return NULL;
145
146    _elm_theme_object_set(obj, wd->base, "searchbar", "base", "default");
147
148    //   evas_object_size_hint_weight_set(wd->base, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
149    //   evas_object_size_hint_align_set(wd->base, EVAS_HINT_FILL, EVAS_HINT_FILL);
150
151    // Add Entry
152    wd->eb = elm_editfield_add(parent);
153    elm_object_style_set(wd->eb, "searchbar");
154    edje_object_part_swallow(wd->base, "search_textfield", wd->eb);
155 //   elm_editfield_guide_text_set(wd->eb, "Search");
156    elm_editfield_entry_single_line_set(wd->eb, EINA_TRUE);
157    elm_editfield_eraser_set(wd->eb, EINA_TRUE);
158    evas_object_smart_callback_add(wd->eb, "clicked", _clicked, obj);
159    evas_object_smart_callback_add(elm_editfield_entry_get(wd->eb), "changed", _changed, obj);
160    edje_object_signal_callback_add(wd->base, "mouse,up,1", "search_icon", _searchicon_clicked, obj);
161
162    elm_widget_sub_object_add(obj, wd->eb);
163
164    // Add Button
165    wd->cancel_btn = elm_button_add(parent);
166    edje_object_part_swallow(wd->base, "button_cancel", wd->cancel_btn);
167    elm_object_style_set(wd->cancel_btn, "custom/darkblue");
168    elm_button_label_set(wd->cancel_btn, "Cancel");
169    evas_object_smart_callback_add(wd->cancel_btn, "clicked", _cancel_clicked, obj);
170    elm_widget_sub_object_add(obj, wd->cancel_btn);
171
172    wd->cancel_btn_ani_flag = EINA_FALSE;
173
174    elm_widget_resize_object_set(obj, wd->base);
175
176    _sizing_eval(obj);
177
178    return obj;
179 }
180
181 /**
182  * set the text of entry
183  *
184  * @param obj The searchbar object
185  * @return void
186  *
187  * @ingroup Searchbar
188  */
189 EAPI void elm_searchbar_text_set(Evas_Object *obj, const char *entry)
190 {
191    Widget_Data *wd = elm_widget_data_get(obj);
192    if (!wd) return NULL;
193
194    elm_entry_entry_set(elm_editfield_entry_get(wd->eb), entry);
195 }
196
197 /**
198  * get the text of entry
199  *
200  * @param obj The searchbar object
201  * @return string pointer of entry
202  *
203  * @ingroup Searchbar
204  */
205 EAPI const char* elm_searchbar_text_get(Evas_Object *obj)
206 {
207    Widget_Data *wd = elm_widget_data_get(obj);
208    if (!wd) return NULL;
209
210    return elm_entry_entry_get(elm_editfield_entry_get(wd->eb));
211 }
212
213 /**
214  * get the pointer of entry
215  *
216  * @param obj The searchbar object
217  * @return the entry object
218  *
219  * @ingroup Searchbar
220  */
221 EAPI Evas_Object *elm_searchbar_entry_get(Evas_Object *obj)
222 {
223    Widget_Data *wd = elm_widget_data_get(obj);
224    if (!wd) return NULL;
225
226    return elm_editfield_entry_get(wd->eb);
227 }
228
229 /**
230  * set the cancel button animation flag
231  *
232  * @param obj The searchbar object
233  * @param cancel_btn_ani_flag The flag of animating cancen button or not
234  * @return void
235  *
236  * @ingroup Searchbar
237  */
238 EAPI void elm_searchbar_cancel_button_animation_set(Evas_Object *obj, Eina_Bool cancel_btn_ani_flag)
239 {
240    Widget_Data *wd = elm_widget_data_get(obj);
241    if (!wd) return;
242
243    if (cancel_btn_ani_flag == EINA_TRUE)
244      wd->cancel_btn_ani_flag = EINA_TRUE;
245    else
246      wd->cancel_btn_ani_flag = EINA_FALSE;
247 }