[searchbar] add no bg mode bugfix bg mode
[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    Eina_Bool cancel_btn_show_mode;
22    Eina_Bool background_mode;
23 };
24
25 static void _del_hook(Evas_Object *obj);
26 static void _theme_hook(Evas_Object *obj);
27 static void _sizing_eval(Evas_Object *obj);
28 static void _clicked(void *data, Evas_Object *obj, const char *emission, const char *source);
29 static void _changed(void *data, Evas_Object *obj, const char *emission, const char *source);
30 static void _cancel_clicked(void *data, Evas_Object *obj, const char *emission, const char *source);
31
32 static void _del_hook(Evas_Object *obj)
33 {
34    Widget_Data *wd = elm_widget_data_get(obj);
35
36    if (!wd) return;
37
38    free(wd);
39 }
40
41 static void _theme_hook(Evas_Object *obj)
42 {
43    Widget_Data *wd = elm_widget_data_get(obj);
44    if (!wd) return;
45
46    _elm_theme_object_set(obj, wd->base, "searchbar", "base", elm_widget_style_get(obj));
47
48    if (wd->eb)
49      edje_object_part_swallow(wd->base, "search_textfield", wd->eb);
50    if (wd->cancel_btn)
51      edje_object_part_swallow(wd->base, "button_cancel", wd->cancel_btn);
52
53    edje_object_scale_set(wd->cancel_btn, elm_widget_scale_get(obj) * _elm_config->scale);
54    _sizing_eval(obj);
55 }
56
57 static void _sizing_eval(Evas_Object *obj)
58 {
59    Widget_Data *wd = elm_widget_data_get(obj);
60    Evas_Coord minw = -1, minh = -1, maxw = -1, maxh = -1;
61
62    if (!wd) return;
63    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
64    edje_object_size_min_restricted_calc(wd->base, &minw, &minh, minw, minh);
65    elm_coords_finger_size_adjust(1, &minw, 1, &minh);
66    evas_object_size_hint_min_set(obj, minw, minh);
67    evas_object_size_hint_max_set(obj, maxw, maxh);
68 }
69
70 static void _clicked(void *data, Evas_Object *obj, const char *emission, const char *source)
71 {
72    Widget_Data *wd = elm_widget_data_get(data);
73    if (!wd) return;
74
75    elm_entry_cursor_end_set(elm_editfield_entry_get(wd->eb));
76
77    if (wd->cancel_btn_show_mode)
78    {
79            if (wd->cancel_btn_ani_flag)
80                    edje_object_signal_emit(wd->base, "CANCELIN", "PROG");
81            else
82                    edje_object_signal_emit(wd->base, "CANCELSHOW", "PROG");
83    }
84
85    evas_object_smart_callback_call(data, "clicked", NULL);
86 }
87
88 static void _changed(void *data, Evas_Object *obj, const char *emission, const char *source)
89 {
90    Widget_Data *wd = elm_widget_data_get(data);
91
92    if (!wd) return;
93
94    // TODO : inform to use entry changed callback 
95 //   evas_object_smart_callback_call(data, "changed", NULL);
96 }
97
98 static void _cancel_clicked(void *data, Evas_Object *obj, const char *emission, const char *source)
99 {
100    Widget_Data *wd = elm_widget_data_get(data);
101    if (!wd) return;
102
103    if (wd->cancel_btn_show_mode)
104    {
105            if (wd->cancel_btn_ani_flag)
106                    edje_object_signal_emit(wd->base, "CANCELOUT", "PROG");
107            else
108                    edje_object_signal_emit(wd->base, "CANCELHIDE", "PROG");
109    }
110
111    const char* text;
112    text = elm_entry_entry_get(elm_editfield_entry_get(wd->eb));
113    if (text != NULL && strlen(text) > 0)
114      elm_entry_entry_set(elm_editfield_entry_get(wd->eb), NULL);
115
116    evas_object_smart_callback_call(data, "cancel,clicked", NULL);
117 }
118
119 static void
120 _searchicon_clicked(void *data, Evas_Object *obj, const char *emission, const char *source)
121 {
122    Widget_Data *wd = elm_widget_data_get(data);
123    if (!wd) return;
124
125    if (!strcmp(source, "search_icon"))
126      evas_object_smart_callback_call(data, "searchsymbol,clicked", NULL);
127    else if (!strcmp(source, "base_bg"))
128      _clicked(data, obj, emission, source);
129 }
130
131 /**
132  * Add a new searchbar to the parent
133  * @param parent The parent object
134  * @return The new object or NULL if it cannot be created
135  *
136  * @ingroup Searchbar
137  */
138 EAPI Evas_Object *elm_searchbar_add(Evas_Object *parent)
139 {
140    Evas_Object *obj;
141    Evas *e;
142    Widget_Data *wd;
143
144    wd = ELM_NEW(Widget_Data);
145    e = evas_object_evas_get(parent);
146    if (e == NULL) return NULL;
147    obj = elm_widget_add(e);
148    if (obj == NULL) return NULL;
149    elm_widget_type_set(obj, "searchbar");
150    elm_widget_sub_object_add(parent, obj);
151    elm_widget_data_set(obj, wd);
152    elm_widget_del_hook_set(obj, _del_hook);
153    elm_widget_theme_hook_set(obj, _theme_hook);
154    elm_widget_can_focus_set(obj, 1 );
155
156    wd->base = edje_object_add(e);
157    if (wd->base == NULL) return NULL;
158
159    _elm_theme_object_set(obj, wd->base, "searchbar", "base", "default_with_bg");
160
161    //   evas_object_size_hint_weight_set(wd->base, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
162    //   evas_object_size_hint_align_set(wd->base, EVAS_HINT_FILL, EVAS_HINT_FILL);
163
164    // Add Entry
165    wd->eb = elm_editfield_add(parent);
166    elm_object_style_set(wd->eb, "searchbar_with_bg");
167    edje_object_part_swallow(wd->base, "search_textfield", wd->eb);
168 //   elm_editfield_guide_text_set(wd->eb, "Search");
169    elm_editfield_entry_single_line_set(wd->eb, EINA_TRUE);
170    elm_editfield_eraser_set(wd->eb, EINA_TRUE);
171    evas_object_smart_callback_add(wd->eb, "clicked", _clicked, obj);
172    evas_object_smart_callback_add(elm_editfield_entry_get(wd->eb), "changed", _changed, obj);
173    edje_object_signal_callback_add(wd->base, "mouse,up,1", "*", _searchicon_clicked, obj);
174
175    elm_widget_sub_object_add(obj, wd->eb);
176
177    // Add Button
178    wd->cancel_btn = elm_button_add(parent);
179    edje_object_part_swallow(wd->base, "button_cancel", wd->cancel_btn);
180    elm_object_style_set(wd->cancel_btn, "custom/darkblue");
181    elm_button_label_set(wd->cancel_btn, "Cancel");
182    evas_object_smart_callback_add(wd->cancel_btn, "clicked", _cancel_clicked, obj);
183    elm_widget_sub_object_add(obj, wd->cancel_btn);
184
185    wd->cancel_btn_ani_flag = EINA_FALSE;
186    wd->cancel_btn_show_mode = EINA_TRUE;
187    wd->background_mode = EINA_TRUE;
188
189    elm_widget_resize_object_set(obj, wd->base);
190
191    _sizing_eval(obj);
192
193    return obj;
194 }
195
196 /**
197  * set the text of entry
198  *
199  * @param obj The searchbar object
200  * @return void
201  *
202  * @ingroup Searchbar
203  */
204 EAPI void elm_searchbar_text_set(Evas_Object *obj, const char *entry)
205 {
206    Widget_Data *wd = elm_widget_data_get(obj);
207    if (!wd) return NULL;
208
209    elm_entry_entry_set(elm_editfield_entry_get(wd->eb), entry);
210 }
211
212 /**
213  * get the text of entry
214  *
215  * @param obj The searchbar object
216  * @return string pointer of entry
217  *
218  * @ingroup Searchbar
219  */
220 EAPI const char* elm_searchbar_text_get(Evas_Object *obj)
221 {
222    Widget_Data *wd = elm_widget_data_get(obj);
223    if (!wd) return NULL;
224
225    return elm_entry_entry_get(elm_editfield_entry_get(wd->eb));
226 }
227
228 /**
229  * get the pointer of entry
230  *
231  * @param obj The searchbar object
232  * @return the entry object
233  *
234  * @ingroup Searchbar
235  */
236 EAPI Evas_Object *elm_searchbar_entry_get(Evas_Object *obj)
237 {
238    Widget_Data *wd = elm_widget_data_get(obj);
239    if (!wd) return NULL;
240
241    return elm_editfield_entry_get(wd->eb);
242 }
243
244 /**
245  * set the cancel button animation flag
246  *
247  * @param obj The searchbar object
248  * @param cancel_btn_ani_flag The flag of animating cancen button or not
249  * @return void
250  *
251  * @ingroup Searchbar
252  */
253 EAPI void elm_searchbar_cancel_button_animation_set(Evas_Object *obj, Eina_Bool cancel_btn_ani_flag)
254 {
255    Widget_Data *wd = elm_widget_data_get(obj);
256    if (!wd) return;
257
258    if (wd->cancel_btn_ani_flag == cancel_btn_ani_flag) return;
259    else wd->cancel_btn_ani_flag = cancel_btn_ani_flag;
260 }
261
262 /**
263  * set the cancel button show mode
264  *
265  * @param obj The searchbar object
266  * @param visible The flag of cancen button show or not
267  * @return void
268  *
269  * @ingroup Searchbar
270  */
271 EAPI void elm_searchbar_cancel_button_set(Evas_Object *obj, Eina_Bool visible)
272 {
273    Widget_Data *wd = elm_widget_data_get(obj);
274    if (!wd) return;
275
276    if (wd->cancel_btn_show_mode == visible) return;
277    else wd->cancel_btn_show_mode = visible;
278
279    if (!visible)
280    {
281            if (wd->cancel_btn_show_mode && wd->cancel_btn_ani_flag)
282                    edje_object_signal_emit(wd->base, "CANCELOUT", "PROG");
283            else
284                    edje_object_signal_emit(wd->base, "CANCELHIDE", "PROG");
285
286            _sizing_eval(obj);
287    }
288 }
289
290 /**
291  * clear searchbar content
292  *
293  * @param obj The searchbar object
294  * @return void
295  *
296  * @ingroup Searchbar
297  */
298 EAPI void elm_searchbar_clear(Evas_Object *obj)
299 {
300    Widget_Data *wd = elm_widget_data_get(obj);
301    if (!wd) return;
302
303    if (wd->cancel_btn_show_mode)
304    {
305            if (wd->cancel_btn_ani_flag)
306                    edje_object_signal_emit(wd->base, "CANCELOUT", "PROG");
307            else
308                    edje_object_signal_emit(wd->base, "CANCELHIDE", "PROG");
309    }
310
311    elm_entry_entry_set(elm_editfield_entry_get(wd->eb), NULL);
312 }
313
314 /**
315  * set the searchbar background mode(with bg rect) set
316  *
317  * @param obj The searchbar object
318  * @param bgmode The flag of background mode or not
319  * @return void
320  *
321  * @ingroup Searchbar
322  */
323 EAPI void elm_searchbar_background_set(Evas_Object *obj, Eina_Bool bgmode)
324 {
325    Widget_Data *wd = elm_widget_data_get(obj);
326    if (!wd) return;
327
328    if (wd->background_mode == bgmode) return;
329    else wd->background_mode = bgmode;
330
331    if (wd->background_mode)
332    {
333       _elm_theme_object_set(obj, wd->base, "searchbar", "base", "default_with_bg");
334           elm_object_style_set(wd->eb, "searchbar_with_bg");
335
336    }
337    else
338    {
339       _elm_theme_object_set(obj, wd->base, "searchbar", "base", "default");
340           elm_object_style_set(wd->eb, "searchbar");
341    }
342
343    _sizing_eval(obj);
344 }