fix for applying wayland (removing X)
[apps/home/settings.git] / setting-common / src / setting-common-draw-searchbar.c
1 /*
2  * setting
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd.
5  *
6  * Contact: MyoungJune Park <mj2004.park@samsung.com>
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  */
21 #include <setting-common-draw-widget.h>
22 #include <glib.h>
23 #include <efl_assist.h>
24
25 /**
26 * Get searchbar text and register redraw_cb by idler.
27 *
28 * @param[in] data
29 * @param[in] search_bar
30 * @param[in] search_text
31 * @param[in] search_idler
32 * @param[in] redraw_cb
33 */
34 EXPORT_PUBLIC
35 int setting_searchbar_redraw(void *data,
36                              Evas_Object *search_bar,
37                              char *search_text,
38                              Ecore_Idler **search_idler,
39                              Ecore_Task_Cb redraw_cb)
40 {
41         SETTING_TRACE_BEGIN;
42         setting_retvm_if(search_bar == NULL, -1, "search_bar parameter is NULL");
43         /*const char *str = elm_object_text_get(search_bar); */
44         Evas_Object *entry = elm_object_part_content_get(search_bar, "elm.swallow.content");
45         const char *str = elm_entry_entry_get(entry);
46         int str_len = safeStrLen(str);
47         setting_retvm_if(str_len > MAX_SEARCH_STR_LEN, -1, "str_len > MAX_SEARCH_STR_LEN");
48
49         memset(search_text, '\0', MAX_SEARCH_STR_LEN + 1);
50         safeCopyStr(search_text, str, MAX_SEARCH_STR_LEN);
51
52         SETTING_TRACE("Update the search view text, search_text=%s",
53                       search_text);
54
55         if (*search_idler) {
56                 ecore_idler_del(*search_idler);
57                 *search_idler = NULL;
58         }
59         *search_idler = ecore_idler_add(redraw_cb, data);
60         return 0;
61 }
62
63
64 static void __searchbar_chg_cb(void *data, Evas_Object *obj, void *event_info)
65 {
66         Evas_Object *searchbar_layout = data;
67         if (elm_object_focus_get(searchbar_layout)) {
68                 if (elm_entry_is_empty(obj)) {
69                         elm_object_signal_emit(searchbar_layout, "elm,state,eraser,hide", "elm");
70                 } else
71                         elm_object_signal_emit(searchbar_layout, "elm,state,eraser,show", "elm");
72         }
73         if (!elm_entry_is_empty(obj))
74                 elm_object_signal_emit(searchbar_layout, "elm,state,guidetext,hide", "elm");
75 }
76
77
78 static void __searchbar_focused_cb(void *data, Evas_Object *obj, void *event_info)
79 {
80         ret_if(!data || !obj);
81         Evas_Object *searchbar_layout = data;
82         if (!elm_entry_is_empty(obj)) {
83                 elm_object_signal_emit(searchbar_layout, "elm,state,eraser,show", "elm");
84         }
85         elm_object_signal_emit(searchbar_layout, "elm,state,guidetext,hide", "elm");
86         /*elm_object_signal_emit(searchbar_layout, "cancel,in", ""); */
87         elm_object_signal_emit(obj, "elm,state,focus,on", "");
88 }
89
90
91 static void __searchbar_unfocused_cb(void *data, Evas_Object *obj, void *event_info)
92 {
93         ret_if(!data || !obj);
94         Evas_Object *searchbar_layout = data;
95         if (elm_entry_is_empty(obj))
96                 elm_object_signal_emit(searchbar_layout, "elm,state,guidetext,show", "elm");
97         elm_object_signal_emit(searchbar_layout, "elm,state,eraser,hide", "elm");
98         elm_object_signal_emit(obj, "elm,state,focus,off", "");
99 }
100
101
102 static void __searchbar_activated_cb(void *data, Evas_Object *obj, void *event_info)
103 {
104         ret_if(!obj);
105         Evas_Object *searchbar_layout = data;
106         Evas_Object *entry = elm_object_part_content_get(searchbar_layout, "elm.swallow.content");
107         ret_if(!entry);
108         elm_object_focus_set(entry, EINA_FALSE);
109 }
110
111 #if 0
112 static void __searchbar_eraser_clicked_cb(void *data, Evas_Object *obj, const char *emission, const char *source)
113 {
114         ret_if(!data);
115         Evas_Object *entry = data;
116         elm_entry_entry_set(entry, "");
117 }
118 #endif
119
120 #if 0
121 static void __searchbar_bg_clicked_cb(void *data, Evas_Object *obj, const char *emission, const char *source)
122 {
123         ret_if(!data);
124         Evas_Object *entry = data;
125         elm_object_focus_set(entry, EINA_TRUE);
126 }
127 #endif
128
129 #if 0
130 static void __searchbar_cancel_clicked_cb(void *data, Evas_Object *obj, void *event_info)
131 {
132         ret_if(!data || !obj);
133         evas_object_hide(obj);
134         Evas_Object *searchbar_layout = data;
135         /*elm_object_signal_emit(searchbar_layout, "cancel,out", ""); */
136         Evas_Object *entry = elm_object_part_content_get(searchbar_layout, "elm.swallow.content");
137         ret_if(!entry);
138         const char *text = elm_entry_entry_get(entry);
139         if (text != NULL && strlen(text) > 0)
140                 elm_entry_entry_set(entry, NULL);
141         elm_object_focus_set(entry, EINA_FALSE);
142 }
143 #endif
144
145 static void __searchbar_searchsymbol_clicked_cb(void *data, Evas_Object *obj, const char *emission, const char *source)
146 {
147         ret_if(!data);
148         Evas_Object *entry = data;
149         elm_object_focus_set(entry, EINA_TRUE);
150 }
151
152 static void ___searchbar_input_panel_event_cb(void *data, Ecore_IMF_Context *ctx, int value)
153 {
154         SETTING_TRACE_BEGIN;
155         retm_if(data == NULL, "Data parameter is NULL");
156         retm_if(ctx == NULL, "obj parameter is NULL");
157         Evas_Object *search_entry = (Evas_Object *)data;
158
159         if (value == ECORE_IMF_INPUT_PANEL_STATE_SHOW) {
160                 SETTING_TRACE("value == ECORE_IMF_INPUT_PANEL_STATE_SHOW");
161                 elm_object_focus_set(search_entry, EINA_TRUE);
162         } else if (value == ECORE_IMF_INPUT_PANEL_STATE_HIDE) {
163                 SETTING_TRACE("value == ECORE_IMF_INPUT_PANEL_STATE_HIDE");
164                 elm_object_focus_set(search_entry, EINA_FALSE);
165         }
166 }
167
168
169 static void __searchbar_entry_max_len_reached(void *data, Evas_Object *obj, void *event_info)
170 {
171         SETTING_TRACE_BEGIN;
172         /*retm_if(data == NULL, "Data parameter is NULL"); */
173         retm_if(!elm_object_focus_get(obj), "Entry is not focused");/*notify only when entry is being focused on. */
174         setting_create_indicator_notification(_(EXCEED_LIMITATION_STR));
175 }
176
177
178
179 /**
180 * Create searchar, register change cb and cancel cb.
181 *
182 * @param[in] data
183 * @param[in] parent
184 * @param[in] change_cb
185 * @param[in] cancel_cb
186 */
187 EXPORT_PUBLIC
188 Evas_Object *setting_create_searchbar(void *data, Evas_Object *parent,
189                                       Evas_Smart_Cb change_cb,
190                                       Evas_Smart_Cb cancel_cb)
191 {
192         SETTING_TRACE_BEGIN;
193         setting_retvm_if(parent == NULL, NULL, "parent parameter is NULL");
194         Evas_Object *searchbar_layout = elm_layout_add(parent);
195
196         elm_layout_file_set(searchbar_layout, SETTING_THEME_EDJ_NAME, "region_searchbar");
197         elm_object_part_content_set(parent, "searchbar", searchbar_layout);
198 #if 0
199         Evas_Object *entry = ea_editfield_add(searchbar_layout, EA_EDITFIELD_SEARCHBAR_FIXED_SIZE);
200
201         if (change_cb) {
202                 evas_object_smart_callback_add(entry, "changed", change_cb, data);
203         } else {
204                 evas_object_smart_callback_add(entry, "changed", __searchbar_chg_cb, searchbar_layout);
205         }
206
207         evas_object_smart_callback_add(entry, "focused", __searchbar_focused_cb, searchbar_layout);
208         evas_object_smart_callback_add(entry, "unfocused", __searchbar_unfocused_cb, searchbar_layout);
209         evas_object_smart_callback_add(entry, "activated", __searchbar_activated_cb, searchbar_layout);
210         elm_entry_input_panel_layout_set(entry, ELM_INPUT_PANEL_LAYOUT_NORMAL);
211         elm_entry_cnp_mode_set(entry, ELM_CNP_MODE_PLAINTEXT);
212
213         elm_object_part_content_set(searchbar_layout, "elm.swallow.content", entry);
214         elm_object_part_text_set(entry, "elm.guide", _("IDS_ST_BODY_SEARCH"));
215         elm_entry_input_panel_return_key_type_set(entry, ELM_INPUT_PANEL_RETURN_KEY_TYPE_SEARCH);
216 #endif
217         Evas_Object *icon = setting_create_icon(searchbar_layout, SETTING_ICON_PATH_CFG"A01-1_title_icon_search.png",
218                                                 NULL, NULL, NULL, NULL);
219         setting_decorate_image_RGBA(icon, 14, 41, 73, 255);
220
221         elm_object_part_content_set(searchbar_layout, "elm.swallow.search.icon", icon);
222         /*"elm.swallow.search.icon" */
223
224 #if 0
225         /* is this required ? */
226         elm_object_signal_callback_add(searchbar_layout, "elm,eraser,clicked", "elm", __searchbar_eraser_clicked_cb, entry);
227         elm_object_signal_callback_add(searchbar_layout, "elm,bg,clicked", "elm", __searchbar_bg_clicked_cb, entry);
228         evas_object_size_hint_weight_set(searchbar_layout, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
229         evas_object_size_hint_align_set(searchbar_layout, EVAS_HINT_FILL, EVAS_HINT_FILL);
230
231
232         Elm_Entry_Filter_Limit_Size *limit_filter_data = calloc(1, sizeof(Elm_Entry_Filter_Limit_Size));
233         if (limit_filter_data) {
234                 limit_filter_data->max_char_count = 0;
235                 limit_filter_data->max_byte_count = 20;
236                 elm_entry_markup_filter_append(entry, elm_entry_filter_limit_size, limit_filter_data);
237                 evas_object_smart_callback_add(entry, "maxlength,reached", __searchbar_entry_max_len_reached, NULL);
238         }
239
240         /*Evas_Object *cancel_btn = elm_button_add(searchbar_layout);
241         elm_object_part_content_set(searchbar_layout, "button_cancel", cancel_btn);
242         elm_object_style_set(cancel_btn, "searchbar/default");
243         elm_object_text_set(cancel_btn, _("IDS_ST_BUTTON_CANCEL_ABB"));
244         elm_object_focus_allow_set(cancel_btn, EINA_FALSE);
245         elm_object_signal_emit(searchbar_layout, "cancel,show", "");
246
247         if (!cancel_cb){
248                 cancel_cb = __searchbar_cancel_clicked_cb;
249         }
250         evas_object_smart_callback_add(cancel_btn, "clicked", cancel_cb, searchbar_layout);*/
251         evas_object_data_set(searchbar_layout, "entry", entry);
252         elm_object_signal_callback_add(searchbar_layout, "elm,action,click", "", __searchbar_searchsymbol_clicked_cb, entry);
253
254         Ecore_IMF_Context *imf_context = (Ecore_IMF_Context *)elm_entry_imf_context_get(entry);
255         if (imf_context) {
256                 ecore_imf_context_input_panel_event_callback_add(imf_context,
257                                                                  ECORE_IMF_INPUT_PANEL_STATE_EVENT,
258                                                                  ___searchbar_input_panel_event_cb,
259                                                                  entry);
260                 SETTING_TRACE("OK to get imf_context -- elm_entry_imf_context_get");
261         } else {
262                 SETTING_TRACE_ERROR("FAILED TO get imf_context -- elm_entry_imf_context_get");
263         }
264
265 #endif
266         return searchbar_layout;
267 }