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