Fix memory leak
[platform/core/uifw/inputdelegator.git] / src / w-input-keyboard.cpp
index 0bfa13f..5e9ae09 100755 (executable)
@@ -34,6 +34,9 @@ bool input_keyboard_init(app_control_h app_control)
        int ret = -1;
        char *default_text = NULL;
        char *guide_text = NULL;
+       char *return_key_type = _("IDS_AMEMO_BUTTON_SEND");
+       char *max_text_length = NULL;
+       char *cursor_position_set = NULL;
 
        input_keyboard_deinit();
 
@@ -45,6 +48,20 @@ bool input_keyboard_init(app_control_h app_control)
        if (ret == APP_CONTROL_ERROR_NONE) {
                g_input_keyboard_data.guide_text = guide_text;
        }
+       ret = app_control_get_extra_data(app_control, "return_key_type", &return_key_type);
+       if (ret == APP_CONTROL_ERROR_NONE) {
+               g_input_keyboard_data.return_key_type = return_key_type;
+       }
+       ret = app_control_get_extra_data(app_control, "max_text_length", &max_text_length);
+       if (ret == APP_CONTROL_ERROR_NONE) {
+               g_input_keyboard_data.max_text_length = atoi(max_text_length);
+               free(max_text_length);
+       }
+       ret = app_control_get_extra_data(app_control, "cursor_position_set", &cursor_position_set);
+       if (ret == APP_CONTROL_ERROR_NONE) {
+               g_input_keyboard_data.cursor_position_set = atoi(cursor_position_set);
+               free(cursor_position_set);
+       }
 
        return true;
 }
@@ -57,13 +74,19 @@ void input_keyboard_deinit(void)
        if (g_input_keyboard_data.default_text)
                free(g_input_keyboard_data.default_text);
 
+       if (g_input_keyboard_data.return_key_type)
+               free(g_input_keyboard_data.return_key_type);
+
        g_input_keyboard_data.default_text = NULL;
        g_input_keyboard_data.guide_text = NULL;
+       g_input_keyboard_data.return_key_type = _("IDS_AMEMO_BUTTON_SEND");
+       g_input_keyboard_data.max_text_length = KEYBOARD_EDITOR_CHAR_COUNT_MAX;
+       g_input_keyboard_data.cursor_position_set = 0;
 
        return;
 }
 
-void btn_clicked_cb(void *data, Evas_Object *obj, void *event_info)
+void exit_keyboard()
 {
        app_control_h app_control;
        int ret = app_control_create(&app_control);
@@ -73,7 +96,10 @@ void btn_clicked_cb(void *data, Evas_Object *obj, void *event_info)
        }
 
        const char *getText = elm_entry_entry_get(entry);
-       LOGD("button key clicked!! : getText = %s", getText);
+       SECURE_LOGD("button key clicked!! : getText = %s", getText);
+
+       char cursorPosition[512];
+       snprintf(cursorPosition, sizeof(cursorPosition), "%d", elm_entry_cursor_pos_get(entry));
 
        char *app_id = NULL;
        app_control_get_caller(app_data->source_app_control, &app_id);
@@ -82,8 +108,13 @@ void btn_clicked_cb(void *data, Evas_Object *obj, void *event_info)
        app_control_set_operation(app_control, APP_CONTROL_OPERATION_DEFAULT);
        set_source_caller_app_id(app_control);
        free(app_id);
-       reply_to_sender_by_callback(getText, "keyboard");
-       elm_exit();
+       reply_to_sender_by_callback(getText, "keyboard", NULL, cursorPosition);
+       ui_app_exit();
+}
+
+void btn_clicked_cb(void *data, Evas_Object *obj, void *event_info)
+{
+       exit_keyboard();
 }
 
 static Eina_Bool custom_back_cb(void *data, Elm_Object_Item *it)
@@ -95,6 +126,27 @@ static Eina_Bool custom_back_cb(void *data, Elm_Object_Item *it)
 static void maxlength_cb(void *data, Evas_Object *obj, void *event_info)
 {
        LOGD("maxlength_cb : size = %d", KEYBOARD_EDITOR_CHAR_COUNT_MAX);
+       char text[512];
+       const char *guide = _(MAX_TEXT_LENGTH_REACH);
+       snprintf(text, sizeof(text), guide, g_input_keyboard_data.max_text_length);
+       show_popup_toast((const char *)text, false);
+}
+
+static void enter_keydown_cb(void *data, Evas_Object *obj, void *event_info)
+{
+       LOGD("enter_keydown_cb");
+       Evas_Object *entry = obj;
+       if (entry == NULL)
+               return;
+
+       Ecore_IMF_Context *imf_context = NULL;
+       imf_context = (Ecore_IMF_Context*)elm_entry_imf_context_get(entry);
+       if (imf_context)
+               ecore_imf_context_input_panel_hide(imf_context);
+
+       elm_object_focus_set(entry, EINA_FALSE);
+
+       exit_keyboard();
 }
 
 void create_fullscreen_editor(void *data)
@@ -109,7 +161,7 @@ void create_fullscreen_editor(void *data)
        entry = elm_entry_add(box);
 
        static Elm_Entry_Filter_Limit_Size limit_filter_data;
-       limit_filter_data.max_char_count = KEYBOARD_EDITOR_CHAR_COUNT_MAX;
+       limit_filter_data.max_char_count = g_input_keyboard_data.max_text_length;
        elm_entry_markup_filter_append(entry, elm_entry_filter_limit_size, &limit_filter_data);
        evas_object_smart_callback_add(entry, "maxlength,reached", maxlength_cb, data);
 
@@ -121,11 +173,18 @@ void create_fullscreen_editor(void *data)
        elm_entry_cursor_end_set(entry);
        evas_object_size_hint_weight_set(entry, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
        evas_object_size_hint_align_set(entry, EVAS_HINT_FILL, EVAS_HINT_FILL);
+       if (g_input_keyboard_data.return_key_type && strcmp(g_input_keyboard_data.return_key_type, "DONE") == 0) {
+               elm_entry_input_panel_return_key_type_set(entry, ELM_INPUT_PANEL_RETURN_KEY_TYPE_DONE);
+               evas_object_smart_callback_add(entry, "activated", enter_keydown_cb, ad);
+       }
+       if (g_input_keyboard_data.cursor_position_set != 0) {
+               elm_entry_cursor_pos_set(entry, g_input_keyboard_data.cursor_position_set);
+       }
        evas_object_show(entry);
        elm_box_pack_end(box, entry);
 
        Evas_Object *btn = elm_button_add(box);
-       elm_object_text_set(btn, "SEND");
+       elm_object_text_set(btn, g_input_keyboard_data.return_key_type);
        evas_object_size_hint_weight_set(btn, EVAS_HINT_EXPAND, 0.5);
        evas_object_size_hint_align_set(btn, EVAS_HINT_FILL, EVAS_HINT_FILL);
        evas_object_smart_callback_add(btn, "clicked", btn_clicked_cb, NULL);
@@ -134,8 +193,105 @@ void create_fullscreen_editor(void *data)
        evas_object_resize(ad->naviframe, 360, 360);
        evas_object_show(ad->naviframe);
 
-       Elm_Object_Item *nf_item = elm_naviframe_item_push(ad->naviframe, NULL, NULL, NULL, box, "empty");
+       const char *item_style = NULL;
+       if (_WEARABLE)
+               item_style = "empty";
+       Elm_Object_Item *nf_item = elm_naviframe_item_push(ad->naviframe, NULL, NULL, NULL, box, item_style);
+       elm_naviframe_item_pop_cb_set(nf_item, custom_back_cb, NULL);
+}
+
+static void editfield_focused_cb(void *data, Evas_Object *obj, void *event_info)
+{
+       Evas_Object *editfield = (Evas_Object *)data;
+       elm_object_signal_emit(editfield, "elm,state,focused", "");
+
+       if (!elm_entry_is_empty(obj))
+               elm_object_signal_emit(editfield, "elm,action,show,button", "");
+}
+
+static void editfield_unfocused_cb(void *data, Evas_Object *obj, void *event_info)
+{
+       Evas_Object *editfield = (Evas_Object *)data;
+       elm_object_signal_emit(editfield, "elm,state,unfocused", "");
+       elm_object_signal_emit(editfield, "elm,action,hide,button", "");
+}
+
+static Evas_Object *create_multiline_editfield_layout(Evas_Object *parent, void *data)
+{
+       App_Data *ad = (App_Data *)data;
+       Evas_Object *editfield;
+
+       editfield = elm_layout_add(parent);
+       elm_layout_theme_set(editfield, "layout", "editfield", "multiline");
+       evas_object_size_hint_align_set(editfield, EVAS_HINT_FILL, 0.0);
+       evas_object_size_hint_weight_set(editfield, EVAS_HINT_EXPAND, 0.0);
+
+       entry = elm_entry_add(editfield);
+       static Elm_Entry_Filter_Limit_Size limit_filter_data;
+       limit_filter_data.max_char_count = g_input_keyboard_data.max_text_length;
+       elm_entry_markup_filter_append(entry, elm_entry_filter_limit_size, &limit_filter_data);
+       evas_object_smart_callback_add(entry, "maxlength,reached", maxlength_cb, data);
+
+       elm_entry_scrollable_set(entry, EINA_TRUE);
+       elm_scroller_policy_set(entry, ELM_SCROLLER_POLICY_OFF, ELM_SCROLLER_POLICY_AUTO);
+       elm_object_part_text_set(entry, "elm.guide", g_input_keyboard_data.guide_text);
+       elm_entry_entry_set(entry, g_input_keyboard_data.default_text);
+       elm_entry_cursor_end_set(entry);
+       evas_object_smart_callback_add(entry, "focused", editfield_focused_cb, editfield);
+       evas_object_smart_callback_add(entry, "unfocused", editfield_unfocused_cb, editfield);
+       if (g_input_keyboard_data.return_key_type && strcmp(g_input_keyboard_data.return_key_type, "DONE") == 0) {
+               elm_entry_input_panel_return_key_type_set(entry, ELM_INPUT_PANEL_RETURN_KEY_TYPE_DONE);
+               evas_object_smart_callback_add(entry, "activated", enter_keydown_cb, ad);
+       }
+       if (g_input_keyboard_data.cursor_position_set != 0) {
+               elm_entry_cursor_pos_set(entry, g_input_keyboard_data.cursor_position_set);
+       }
+
+       evas_object_show(entry);
+       elm_object_part_content_set(editfield, "elm.swallow.content", entry);
+
+       return editfield;
+}
+
+static Evas_Object *create_editfield_view(void *data)
+{
+       App_Data *ad = (App_Data *)data;
+
+       Evas_Object *main_scroller, *main_box, *editfield;
+
+       main_scroller = elm_scroller_add(ad->naviframe);
+       elm_scroller_bounce_set(main_scroller, EINA_FALSE, EINA_TRUE);
+       evas_object_size_hint_weight_set(main_scroller, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
+       evas_object_size_hint_align_set(main_scroller, EVAS_HINT_FILL, EVAS_HINT_FILL);
+       evas_object_show(main_scroller);
+
+       main_box = elm_box_add(main_scroller);
+       evas_object_size_hint_align_set(main_box, EVAS_HINT_FILL, 0.0);
+       evas_object_size_hint_weight_set(main_box, EVAS_HINT_EXPAND, 0.0);
+       evas_object_show(main_box);
+
+       editfield = create_multiline_editfield_layout(main_box, data);
+       elm_box_pack_end(main_box, editfield);
+       evas_object_show(editfield);
+
+       Evas_Object *btn = elm_button_add(main_box);
+       elm_object_text_set(btn, "OK");
+       evas_object_size_hint_weight_set(btn, 0.5, 0.5);
+       evas_object_size_hint_align_set(btn, EVAS_HINT_FILL, EVAS_HINT_FILL);
+       evas_object_smart_callback_add(btn, "clicked", btn_clicked_cb, NULL);
+       evas_object_show(btn);
+       elm_box_pack_end(main_box, btn);
+
+       elm_object_content_set(main_scroller, main_box);
+
+       evas_object_show(ad->naviframe);
+       const char *item_style = NULL;
+       if (_WEARABLE)
+               item_style = "empty";
+       Elm_Object_Item *nf_item = elm_naviframe_item_push(ad->naviframe, NULL, NULL, NULL, main_scroller, item_style);
        elm_naviframe_item_pop_cb_set(nf_item, custom_back_cb, NULL);
+
+       return main_scroller;
 }
 
 bool input_keyboard_launch(Evas_Object *window, void *data) {
@@ -143,7 +299,12 @@ bool input_keyboard_launch(Evas_Object *window, void *data) {
                PRINTFUNC(DLOG_ERROR, "Can not get window");
                return false;
        }
-       create_fullscreen_editor(data);
+
+       if (_WEARABLE)
+               create_fullscreen_editor(data);
+       else
+               create_editfield_view(data);
+
        return true;
 }