Support API for set/get cursor position
[platform/core/uifw/inputdelegator.git] / src / w-input-keyboard.cpp
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include "Debug.h"
18
19 #include <app.h>
20 #include <Elementary.h>
21
22 #include <stdlib.h>
23
24 #include "w-input-keyboard.h"
25 #include "w-input-selector.h"
26
27 extern InputKeyboardData g_input_keyboard_data;
28 extern App_Data* app_data;
29
30 Evas_Object *entry;
31
32 bool input_keyboard_init(app_control_h app_control)
33 {
34         int ret = -1;
35         char *default_text = NULL;
36         char *guide_text = NULL;
37         char *return_key_type = "SEND";
38         char *max_text_length = NULL;
39         char *cursor_position_set = "0";
40
41         input_keyboard_deinit();
42
43         ret = app_control_get_extra_data(app_control, APP_CONTROL_DATA_INPUT_DEFAULT_TEXT, &default_text);
44         if (ret == APP_CONTROL_ERROR_NONE) {
45                 g_input_keyboard_data.default_text = default_text;
46         }
47         ret = app_control_get_extra_data(app_control, APP_CONTROL_DATA_INPUT_GUIDE_TEXT, &guide_text);
48         if (ret == APP_CONTROL_ERROR_NONE) {
49                 g_input_keyboard_data.guide_text = guide_text;
50         }
51         ret = app_control_get_extra_data(app_control, "return_key_type", &return_key_type);
52         if (ret == APP_CONTROL_ERROR_NONE) {
53                 g_input_keyboard_data.return_key_type = return_key_type;
54         }
55         ret = app_control_get_extra_data(app_control, "max_text_length", &max_text_length);
56         if (ret == APP_CONTROL_ERROR_NONE) {
57                 g_input_keyboard_data.max_text_length = atoi(max_text_length);
58         }
59         ret = app_control_get_extra_data(app_control, "cursor_position_set", &cursor_position_set);
60         if (ret == APP_CONTROL_ERROR_NONE) {
61                 g_input_keyboard_data.cursor_position_set = atoi(cursor_position_set);
62         }
63
64         return true;
65 }
66
67 void input_keyboard_deinit(void)
68 {
69         if (g_input_keyboard_data.guide_text)
70                 free(g_input_keyboard_data.guide_text);
71
72         if (g_input_keyboard_data.default_text)
73                 free(g_input_keyboard_data.default_text);
74
75         if (g_input_keyboard_data.return_key_type)
76                 free(g_input_keyboard_data.return_key_type);
77
78         g_input_keyboard_data.default_text = NULL;
79         g_input_keyboard_data.guide_text = NULL;
80         g_input_keyboard_data.return_key_type = "SEND";
81         g_input_keyboard_data.max_text_length = KEYBOARD_EDITOR_CHAR_COUNT_MAX;
82         g_input_keyboard_data.cursor_position_set = 0;
83
84         return;
85 }
86
87 void exit_keyboard()
88 {
89         app_control_h app_control;
90         int ret = app_control_create(&app_control);
91         if (ret != APP_CONTROL_ERROR_NONE) {
92                 PRINTFUNC(DLOG_ERROR, "Can not create app_control : %d", ret);
93                 return;
94         }
95
96         const char *getText = elm_entry_entry_get(entry);
97         LOGD("button key clicked!! : getText = %s", getText);
98
99         char cursorPosition[512];
100         snprintf(cursorPosition, sizeof(cursorPosition), "%d", elm_entry_cursor_pos_get(entry));
101
102         char *app_id = NULL;
103         app_control_get_caller(app_data->source_app_control, &app_id);
104         if (app_id != NULL)
105                 app_control_set_app_id(app_control, app_id);
106         app_control_set_operation(app_control, APP_CONTROL_OPERATION_DEFAULT);
107         set_source_caller_app_id(app_control);
108         free(app_id);
109         reply_to_sender_by_callback(getText, "keyboard", NULL, cursorPosition);
110         ui_app_exit();
111 }
112
113 void btn_clicked_cb(void *data, Evas_Object *obj, void *event_info)
114 {
115         exit_keyboard();
116 }
117
118 static Eina_Bool custom_back_cb(void *data, Elm_Object_Item *it)
119 {
120         _back_to_genlist_for_selector();
121         return EINA_TRUE;
122 }
123
124 static void maxlength_cb(void *data, Evas_Object *obj, void *event_info)
125 {
126         LOGD("maxlength_cb : size = %d", KEYBOARD_EDITOR_CHAR_COUNT_MAX);
127         char text[512];
128         snprintf(text, sizeof(text), _(MAX_TEXT_LENGTH_REACH), g_input_keyboard_data.max_text_length);
129         show_popup_toast((const char *)text, false);
130 }
131
132 static void enter_keydown_cb(void *data, Evas_Object *obj, void *event_info)
133 {
134         LOGD("enter_keydown_cb");
135         Evas_Object *entry = obj;
136         if (entry == NULL)
137                 return;
138
139         Ecore_IMF_Context *imf_context = NULL;
140         imf_context = (Ecore_IMF_Context*)elm_entry_imf_context_get(entry);
141         if (imf_context)
142                 ecore_imf_context_input_panel_hide(imf_context);
143
144         elm_object_focus_set(entry, EINA_FALSE);
145
146         exit_keyboard();
147 }
148
149 void create_fullscreen_editor(void *data)
150 {
151         App_Data *ad = (App_Data *)data;
152
153         Evas_Object *box = elm_box_add(ad->naviframe);
154         evas_object_size_hint_weight_set(box, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
155         evas_object_show(box);
156         elm_win_resize_object_add(ad->naviframe, box);
157
158         entry = elm_entry_add(box);
159
160         static Elm_Entry_Filter_Limit_Size limit_filter_data;
161         limit_filter_data.max_char_count = g_input_keyboard_data.max_text_length;
162         elm_entry_markup_filter_append(entry, elm_entry_filter_limit_size, &limit_filter_data);
163         evas_object_smart_callback_add(entry, "maxlength,reached", maxlength_cb, data);
164
165         elm_entry_single_line_set(entry, EINA_TRUE);
166         elm_entry_scrollable_set(entry, EINA_TRUE);
167         elm_scroller_policy_set(entry, ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_AUTO);
168         elm_object_part_text_set(entry, "elm.guide", g_input_keyboard_data.guide_text);
169         elm_entry_entry_set(entry, g_input_keyboard_data.default_text);
170         elm_entry_cursor_end_set(entry);
171         evas_object_size_hint_weight_set(entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
172         evas_object_size_hint_align_set(entry, EVAS_HINT_FILL, EVAS_HINT_FILL);
173         if (g_input_keyboard_data.return_key_type && strcmp(g_input_keyboard_data.return_key_type, "DONE") == 0) {
174                 elm_entry_input_panel_return_key_type_set(entry, ELM_INPUT_PANEL_RETURN_KEY_TYPE_DONE);
175                 evas_object_smart_callback_add(entry, "activated", enter_keydown_cb, ad);
176         }
177         if (g_input_keyboard_data.cursor_position_set != 0) {
178                 elm_entry_cursor_pos_set(entry, g_input_keyboard_data.cursor_position_set);
179         }
180         evas_object_show(entry);
181         elm_box_pack_end(box, entry);
182
183         Evas_Object *btn = elm_button_add(box);
184         elm_object_text_set(btn, g_input_keyboard_data.return_key_type);
185         evas_object_size_hint_weight_set(btn, EVAS_HINT_EXPAND, 0.5);
186         evas_object_size_hint_align_set(btn, EVAS_HINT_FILL, EVAS_HINT_FILL);
187         evas_object_smart_callback_add(btn, "clicked", btn_clicked_cb, NULL);
188         evas_object_show(btn);
189         elm_box_pack_end(box, btn);
190         evas_object_resize(ad->naviframe, 360, 360);
191         evas_object_show(ad->naviframe);
192
193         const char *item_style = NULL;
194         if (_WEARABLE)
195                 item_style = "empty";
196         Elm_Object_Item *nf_item = elm_naviframe_item_push(ad->naviframe, NULL, NULL, NULL, box, item_style);
197         elm_naviframe_item_pop_cb_set(nf_item, custom_back_cb, NULL);
198 }
199
200 static void editfield_focused_cb(void *data, Evas_Object *obj, void *event_info)
201 {
202         Evas_Object *editfield = (Evas_Object *)data;
203         elm_object_signal_emit(editfield, "elm,state,focused", "");
204
205         if (!elm_entry_is_empty(obj))
206                 elm_object_signal_emit(editfield, "elm,action,show,button", "");
207 }
208
209 static void editfield_unfocused_cb(void *data, Evas_Object *obj, void *event_info)
210 {
211         Evas_Object *editfield = (Evas_Object *)data;
212         elm_object_signal_emit(editfield, "elm,state,unfocused", "");
213         elm_object_signal_emit(editfield, "elm,action,hide,button", "");
214 }
215
216 static Evas_Object *create_multiline_editfield_layout(Evas_Object *parent, void *data)
217 {
218         App_Data *ad = (App_Data *)data;
219         Evas_Object *editfield;
220
221         editfield = elm_layout_add(parent);
222         elm_layout_theme_set(editfield, "layout", "editfield", "multiline");
223         evas_object_size_hint_align_set(editfield, EVAS_HINT_FILL, 0.0);
224         evas_object_size_hint_weight_set(editfield, EVAS_HINT_EXPAND, 0.0);
225
226         entry = elm_entry_add(editfield);
227         static Elm_Entry_Filter_Limit_Size limit_filter_data;
228         limit_filter_data.max_char_count = g_input_keyboard_data.max_text_length;
229         elm_entry_markup_filter_append(entry, elm_entry_filter_limit_size, &limit_filter_data);
230         evas_object_smart_callback_add(entry, "maxlength,reached", maxlength_cb, data);
231
232         elm_entry_scrollable_set(entry, EINA_TRUE);
233         elm_scroller_policy_set(entry, ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_AUTO);
234         elm_object_part_text_set(entry, "elm.guide", g_input_keyboard_data.guide_text);
235         elm_entry_entry_set(entry, g_input_keyboard_data.default_text);
236         elm_entry_cursor_end_set(entry);
237         evas_object_smart_callback_add(entry, "focused", editfield_focused_cb, editfield);
238         evas_object_smart_callback_add(entry, "unfocused", editfield_unfocused_cb, editfield);
239         if (g_input_keyboard_data.return_key_type && strcmp(g_input_keyboard_data.return_key_type, "DONE") == 0) {
240                 elm_entry_input_panel_return_key_type_set(entry, ELM_INPUT_PANEL_RETURN_KEY_TYPE_DONE);
241                 evas_object_smart_callback_add(entry, "activated", enter_keydown_cb, ad);
242         }
243         if (g_input_keyboard_data.cursor_position_set != 0) {
244                 elm_entry_cursor_pos_set(entry, g_input_keyboard_data.cursor_position_set);
245         }
246
247         evas_object_show(entry);
248         elm_object_part_content_set(editfield, "elm.swallow.content", entry);
249
250         return editfield;
251 }
252
253 static Evas_Object *create_editfield_view(void *data)
254 {
255         App_Data *ad = (App_Data *)data;
256
257         Evas_Object *main_scroller, *main_box, *editfield;
258
259         main_scroller = elm_scroller_add(ad->naviframe);
260         elm_scroller_bounce_set(main_scroller, EINA_FALSE, EINA_TRUE);
261         evas_object_size_hint_weight_set(main_scroller, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
262         evas_object_size_hint_align_set(main_scroller, EVAS_HINT_FILL, EVAS_HINT_FILL);
263         evas_object_show(main_scroller);
264
265         main_box = elm_box_add(main_scroller);
266         evas_object_size_hint_align_set(main_box, EVAS_HINT_FILL, 0.0);
267         evas_object_size_hint_weight_set(main_box, EVAS_HINT_EXPAND, 0.0);
268         evas_object_show(main_box);
269
270         editfield = create_multiline_editfield_layout(main_box, data);
271         elm_box_pack_end(main_box, editfield);
272         evas_object_show(editfield);
273
274         Evas_Object *btn = elm_button_add(main_box);
275         elm_object_text_set(btn, "OK");
276         evas_object_size_hint_weight_set(btn, 0.5, 0.5);
277         evas_object_size_hint_align_set(btn, EVAS_HINT_FILL, EVAS_HINT_FILL);
278         evas_object_smart_callback_add(btn, "clicked", btn_clicked_cb, NULL);
279         evas_object_show(btn);
280         elm_box_pack_end(main_box, btn);
281
282         elm_object_content_set(main_scroller, main_box);
283
284         evas_object_show(ad->naviframe);
285         const char *item_style = NULL;
286         if (_WEARABLE)
287                 item_style = "empty";
288         Elm_Object_Item *nf_item = elm_naviframe_item_push(ad->naviframe, NULL, NULL, NULL, main_scroller, item_style);
289         elm_naviframe_item_pop_cb_set(nf_item, custom_back_cb, NULL);
290
291         return main_scroller;
292 }
293
294 bool input_keyboard_launch(Evas_Object *window, void *data) {
295         if (window == NULL) {
296                 PRINTFUNC(DLOG_ERROR, "Can not get window");
297                 return false;
298         }
299
300         if (_WEARABLE)
301                 create_fullscreen_editor(data);
302         else
303                 create_editfield_view(data);
304
305         return true;
306 }
307