[elm_searchbar & elm_tickernoti] Indentation correction.
[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 boundary_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, void *event_info);
29 static void _changed(void *data, Evas_Object *obj, void *event_info);
30 static void _cancel_clicked(void *data, Evas_Object *obj, void *event_info);
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, void *event_info)
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) edje_object_signal_emit(wd->base, "CANCELIN", "PROG");
80         else edje_object_signal_emit(wd->base, "CANCELSHOW", "PROG");
81      }
82
83    evas_object_smart_callback_call(data, "clicked", NULL);
84 }
85
86 static void _changed(void *data, Evas_Object *obj, void *event_info)
87 {
88    Widget_Data *wd = elm_widget_data_get(data);
89
90    if (!wd) return;
91
92    // TODO : inform to use entry changed callback 
93 //   evas_object_smart_callback_call(data, "changed", NULL);
94 }
95
96 static void _cancel_clicked(void *data, Evas_Object *obj, void *event_info)
97 {
98    Widget_Data *wd = elm_widget_data_get(data);
99    if (!wd) return;
100
101    if (wd->cancel_btn_show_mode)
102      {
103         if (wd->cancel_btn_ani_flag) edje_object_signal_emit(wd->base, "CANCELOUT", "PROG");
104         else edje_object_signal_emit(wd->base, "CANCELHIDE", "PROG");
105      }
106
107    const char* text;
108    text = elm_entry_entry_get(elm_editfield_entry_get(wd->eb));
109    if (text != NULL && strlen(text) > 0) elm_entry_entry_set(elm_editfield_entry_get(wd->eb), NULL);
110
111    evas_object_smart_callback_call(data, "cancel,clicked", NULL);
112 }
113
114 static void
115 _searchicon_clicked(void *data, Evas_Object *obj, const char *emission, const char *source)
116 {
117    Widget_Data *wd = elm_widget_data_get(data);
118    if (!wd) return;
119
120    if (!strcmp(source, "search_icon"))
121      evas_object_smart_callback_call(data, "searchsymbol,clicked", NULL);
122    else if (!strcmp(source, "base_bg"))
123      _clicked(data, obj, NULL); //emission, source);
124 }
125
126 /**
127  * Add a new searchbar to the parent
128  * @param parent The parent object
129  * @return The new object or NULL if it cannot be created
130  *
131  * @ingroup Searchbar
132  */
133 EAPI Evas_Object *elm_searchbar_add(Evas_Object *parent)
134 {
135    Evas_Object *obj;
136    Evas *e;
137    Widget_Data *wd;
138
139    wd = ELM_NEW(Widget_Data);
140    e = evas_object_evas_get(parent);
141    if (e == NULL) return NULL;
142    obj = elm_widget_add(e);
143    if (obj == NULL) return NULL;
144    elm_widget_type_set(obj, "searchbar");
145    elm_widget_sub_object_add(parent, obj);
146    elm_widget_data_set(obj, wd);
147    elm_widget_del_hook_set(obj, _del_hook);
148    elm_widget_theme_hook_set(obj, _theme_hook);
149    elm_widget_can_focus_set(obj, 1 );
150
151    wd->base = edje_object_add(e);
152    if (wd->base == NULL) return NULL;
153
154    _elm_theme_object_set(obj, wd->base, "searchbar", "base", "default");
155
156    //   evas_object_size_hint_weight_set(wd->base, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
157    //   evas_object_size_hint_align_set(wd->base, EVAS_HINT_FILL, EVAS_HINT_FILL);
158
159    // Add Entry
160    wd->eb = elm_editfield_add(parent);
161    elm_object_style_set(wd->eb, "searchbar");
162    edje_object_part_swallow(wd->base, "search_textfield", wd->eb);
163 //   elm_editfield_guide_text_set(wd->eb, "Search");
164    elm_editfield_entry_single_line_set(wd->eb, EINA_TRUE);
165    elm_editfield_eraser_set(wd->eb, EINA_TRUE);
166    evas_object_smart_callback_add(wd->eb, "clicked", _clicked, obj);
167    evas_object_smart_callback_add(elm_editfield_entry_get(wd->eb), "changed", _changed, obj);
168    edje_object_signal_callback_add(wd->base, "mouse,up,1", "*", _searchicon_clicked, obj);
169
170    elm_widget_sub_object_add(obj, wd->eb);
171
172    // Add Button
173    wd->cancel_btn = elm_button_add(parent);
174    edje_object_part_swallow(wd->base, "button_cancel", wd->cancel_btn);
175    elm_object_style_set(wd->cancel_btn, "custom/darkblue");
176    elm_button_label_set(wd->cancel_btn, "Cancel");
177    evas_object_smart_callback_add(wd->cancel_btn, "clicked", _cancel_clicked, obj);
178    elm_widget_sub_object_add(obj, wd->cancel_btn);
179
180    wd->cancel_btn_ani_flag = EINA_FALSE;
181    wd->cancel_btn_show_mode = EINA_TRUE;
182    wd->boundary_mode = EINA_TRUE;
183
184    elm_widget_resize_object_set(obj, wd->base);
185
186    _sizing_eval(obj);
187
188    return obj;
189 }
190
191 /**
192  * set the text of entry
193  *
194  * @param obj The searchbar object
195  * @return void
196  *
197  * @ingroup Searchbar
198  */
199 EAPI void elm_searchbar_text_set(Evas_Object *obj, const char *entry)
200 {
201    Widget_Data *wd = elm_widget_data_get(obj);
202    if (!wd) return;
203
204    elm_entry_entry_set(elm_editfield_entry_get(wd->eb), entry);
205 }
206
207 /**
208  * get the text of entry
209  *
210  * @param obj The searchbar object
211  * @return string pointer of entry
212  *
213  * @ingroup Searchbar
214  */
215 EAPI const char* elm_searchbar_text_get(Evas_Object *obj)
216 {
217    Widget_Data *wd = elm_widget_data_get(obj);
218    if (!wd) return NULL;
219
220    return elm_entry_entry_get(elm_editfield_entry_get(wd->eb));
221 }
222
223 /**
224  * get the pointer of entry
225  *
226  * @param obj The searchbar object
227  * @return the entry object
228  *
229  * @ingroup Searchbar
230  */
231 EAPI Evas_Object *elm_searchbar_entry_get(Evas_Object *obj)
232 {
233    Widget_Data *wd = elm_widget_data_get(obj);
234    if (!wd) return NULL;
235
236    return elm_editfield_entry_get(wd->eb);
237 }
238
239 /**
240  * set the cancel button animation flag
241  *
242  * @param obj The searchbar object
243  * @param cancel_btn_ani_flag The flag of animating cancen button or not
244  * @return void
245  *
246  * @ingroup Searchbar
247  */
248 EAPI void elm_searchbar_cancel_button_animation_set(Evas_Object *obj, Eina_Bool cancel_btn_ani_flag)
249 {
250    Widget_Data *wd = elm_widget_data_get(obj);
251    if (!wd) return;
252
253    if (wd->cancel_btn_ani_flag == cancel_btn_ani_flag) return;
254    else wd->cancel_btn_ani_flag = cancel_btn_ani_flag;
255 }
256
257 /**
258  * set the cancel button show mode
259  *
260  * @param obj The searchbar object
261  * @param visible The flag of cancen button show or not
262  * @return void
263  *
264  * @ingroup Searchbar
265  */
266 EAPI void elm_searchbar_cancel_button_set(Evas_Object *obj, Eina_Bool visible)
267 {
268    Widget_Data *wd = elm_widget_data_get(obj);
269    if (!wd) return;
270
271    if (wd->cancel_btn_show_mode == visible) return;
272    else wd->cancel_btn_show_mode = visible;
273
274    if (!visible)
275      {
276         if (wd->cancel_btn_ani_flag)
277           edje_object_signal_emit(wd->base, "CANCELOUT", "PROG");
278         else
279           edje_object_signal_emit(wd->base, "CANCELHIDE", "PROG");
280      }
281    _sizing_eval(obj);
282 }
283
284 /**
285  * clear searchbar status
286  *
287  * @param obj The searchbar object
288  * @return void
289  *
290  * @ingroup Searchbar
291  */
292 EAPI void elm_searchbar_clear(Evas_Object *obj)
293 {
294    Widget_Data *wd = elm_widget_data_get(obj);
295    if (!wd) return;
296
297    if (wd->cancel_btn_show_mode)
298      {
299         if (wd->cancel_btn_ani_flag)
300           edje_object_signal_emit(wd->base, "CANCELOUT", "PROG");
301         else
302           edje_object_signal_emit(wd->base, "CANCELHIDE", "PROG");
303      }
304 //   elm_entry_entry_set(elm_editfield_entry_get(wd->eb), NULL);
305 }
306
307 /**
308  * set the searchbar boundary rect mode(with bg rect) set
309  *
310  * @param obj The searchbar object
311  * @param boundary The present flag of boundary rect or not
312  * @return void
313  *
314  * @ingroup Searchbar
315  */
316 EAPI void elm_searchbar_boundary_rect_set(Evas_Object *obj, Eina_Bool boundary)
317 {
318    Widget_Data *wd = elm_widget_data_get(obj);
319    if (!wd) return;
320
321    if (wd->boundary_mode == boundary) return;
322    else wd->boundary_mode = boundary;
323
324    if (wd->boundary_mode)
325      {
326         edje_object_signal_emit(wd->base, "BDSHOW", "PROG");
327      }
328    else
329      {
330         edje_object_signal_emit(wd->base, "BDHIDE", "PROG");
331      }
332    _sizing_eval(obj);
333 }