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