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